00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "buttondialog.h"
00018
00019 ButtonDialog::ButtonDialog(QString buttonName, QWidget *parent, const char*name, bool modal, WFlags f):QDialog(parent, name, modal, f)
00020 {
00021 setCaption(tr(buttonName));
00022
00023 QVBoxLayout *layout = new QVBoxLayout(this);
00024
00025 QHBoxLayout *hlayout1 = new QHBoxLayout(this);
00026 QHBoxLayout *hlayout2 = new QHBoxLayout(this);
00027 QHBoxLayout *hlayout3 = new QHBoxLayout(this);
00028
00029 layout->addSpacing(5);
00030 layout->addLayout(hlayout1);
00031 layout->addSpacing(5);
00032 layout->addLayout(hlayout2);
00033 layout->addSpacing(5);
00034 layout->addLayout(hlayout3);
00035 layout->addSpacing(5);
00036
00037 remote = new QComboBox(false, this, "remote");
00038 QLabel *remoteLabel = new QLabel(remote, "Remote: ", this, "remoteLabel");
00039 hlayout1->addSpacing(5);
00040 hlayout1->addWidget(remoteLabel);
00041 hlayout1->addSpacing(5);
00042 hlayout1->addWidget(remote);
00043 hlayout1->addSpacing(5);
00044 remote->insertItem("Remote ");
00045 remote->insertStringList(getRemotes());
00046 connect(remote, SIGNAL(activated(const QString&)), this, SLOT(remoteSelected(const QString&)) );
00047
00048 button = new QComboBox(false, this, "button");
00049 QLabel *buttonLabel = new QLabel(remote, "Button: ", this, "buttonLabel");
00050 hlayout2->addSpacing(5);
00051 hlayout2->addWidget(buttonLabel);
00052 hlayout2->addSpacing(5);
00053 hlayout2->addWidget(button);
00054 hlayout2->addSpacing(5);
00055 button->insertItem("Button ");
00056 connect(button, SIGNAL(activated(const QString&)), this, SLOT(buttonSelected(const QString&)) );
00057
00058 label = new QLineEdit(this, "label");
00059 label->setText(buttonName);
00060 QLabel *labelLabel = new QLabel(label, "Label: ", this, "labelLabel");
00061 hlayout3->addSpacing(5);
00062 hlayout3->addWidget(labelLabel);
00063 hlayout3->addSpacing(5);
00064 hlayout3->addWidget(label);
00065 hlayout3->addSpacing(5);
00066 }
00067
00068 void ButtonDialog::remoteSelected(const QString &string)
00069 {
00070 button->insertStringList(getButtons(string.latin1()) );
00071 list="SEND_ONCE";
00072 list+=string;
00073 }
00074
00075 void ButtonDialog::buttonSelected(const QString &string)
00076 {
00077 list+=string;
00078 }
00079
00080 QStringList ButtonDialog::getList()
00081 {
00082 return list;
00083 }
00084
00085 QString ButtonDialog::getLabel()
00086 {
00087 return label->text();
00088 }
00089
00090 QStringList ButtonDialog::getRemotes()
00091 {
00092 const char write_buffer[] = "LIST\n";
00093 const char *readbuffer;
00094 int i, numlines;
00095 QStringList list;
00096
00097 addr.sun_family=AF_UNIX;
00098 strcpy(addr.sun_path,"/dev/lircd");
00099
00100 fd = socket(AF_UNIX, SOCK_STREAM, 0);
00101 if(fd == -1)
00102 {
00103 QMessageBox *mb = new QMessageBox("Error!",
00104 "couldnt connect to socket",
00105 QMessageBox::NoIcon,
00106 QMessageBox::Ok,
00107 QMessageBox::NoButton,
00108 QMessageBox::NoButton);
00109 mb->exec();
00110 perror("ButtonDialog::GetRemotes");
00111 return NULL;
00112 }
00113
00114 if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1)
00115 {
00116 QMessageBox *mb = new QMessageBox("Error!",
00117 "couldnt connect to socket",
00118 QMessageBox::NoIcon,
00119 QMessageBox::Ok,
00120 QMessageBox::NoButton,
00121 QMessageBox::NoButton);
00122 mb->exec();
00123 perror("ButtonDialog::GetRemotes");
00124 return NULL;
00125 }
00126
00127 write(fd, write_buffer, strlen(write_buffer));
00128
00129 for(i=0; i<5; i++)
00130 {
00131 printf("%d\n", i);
00132 readbuffer = readPacket();
00133 printf("%s", readbuffer);
00134 printf("%d\n", i);
00135 }
00136
00137 numlines = atoi(readbuffer);
00138
00139 for(i=0; i<numlines; i++)
00140 {
00141 list+=readPacket();
00142 }
00143
00144 if(strcasecmp(readPacket(), "END") != 0)
00145 {
00146 QMessageBox *mb = new QMessageBox("Error!",
00147 "bad packet",
00148 QMessageBox::NoIcon,
00149 QMessageBox::Ok,
00150 QMessageBox::NoButton,
00151 QMessageBox::NoButton);
00152 mb->exec();
00153 perror("ButtonDialog::GetRemotes");
00154 return NULL;
00155 }
00156
00157 ::close(fd);
00158 return list;
00159 }
00160
00161 QStringList ButtonDialog::getButtons(const char *remoteName)
00162 {
00163 QString write_buffer = "LIST ";
00164 const char *readbuffer;
00165 int i, j, numlines;
00166 QStringList list;
00167 QString string;
00168
00169 write_buffer += remoteName;
00170 write_buffer += '\n';
00171
00172 fd = socket(AF_UNIX, SOCK_STREAM, 0);
00173 if(fd == -1)
00174 {
00175 QMessageBox *mb = new QMessageBox("Error!",
00176 "couldnt connect to socket",
00177 QMessageBox::NoIcon,
00178 QMessageBox::Ok,
00179 QMessageBox::NoButton,
00180 QMessageBox::NoButton);
00181 mb->exec();
00182 perror("ButtonDialog::GetButtons");
00183 return NULL;
00184 }
00185
00186
00187 if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1)
00188 {
00189 QMessageBox *mb = new QMessageBox("Error!",
00190 "couldnt connect to socket",
00191 QMessageBox::NoIcon,
00192 QMessageBox::Ok,
00193 QMessageBox::NoButton,
00194 QMessageBox::NoButton);
00195 mb->exec();
00196 perror("ButtonDialog::GetButtons");
00197 return NULL;
00198 }
00199
00200 write(fd, write_buffer.latin1(), strlen(write_buffer) );
00201
00202 for(i=0; i<5; i++)
00203 {
00204 readbuffer = readPacket();
00205 }
00206
00207 numlines = atoi(readbuffer);
00208
00209 for(i=0; i<numlines; i++)
00210 {
00211 list+=readPacket();
00212 for(j=0; j<list[i].length(); j++)
00213 {
00214 if(list[i][j] == ' ')
00215 break;
00216 }
00217 list[i].remove(0, j+1);
00218 }
00219
00220 if(strcasecmp(readPacket(), "END") != 0)
00221 {
00222 QMessageBox *mb = new QMessageBox("Error!",
00223 "bad packet",
00224 QMessageBox::NoIcon,
00225 QMessageBox::Ok,
00226 QMessageBox::NoButton,
00227 QMessageBox::NoButton);
00228 mb->exec();
00229 perror("ButtonDialog::GetButtons");
00230 return NULL;
00231 }
00232
00233 ::close(fd);
00234 return list;
00235 }
00236
00237
00238
00239 const char *ButtonDialog::readPacket()
00240 {
00241 static char buffer[PACKET_SIZE+1]="";
00242 char *end;
00243 static int ptr=0,end_len=0;
00244 ssize_t ret;
00245 timeout = 0;
00246
00247 if(ptr>0)
00248 {
00249 memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1);
00250 ptr=strlen(buffer);
00251 end=strchr(buffer,'\n');
00252 }
00253 else
00254 {
00255 end=NULL;
00256 }
00257 alarm(TIMEOUT);
00258 while(end==NULL)
00259 {
00260 if(PACKET_SIZE<=ptr)
00261 {
00262 fprintf(stderr,"bad packet\n");
00263 ptr=0;
00264 return(NULL);
00265 }
00266 ret=read(fd,buffer+ptr,PACKET_SIZE-ptr);
00267
00268 if(ret<=0 || timeout)
00269 {
00270 if(timeout)
00271 {
00272 fprintf(stderr,"timeout\n");
00273 }
00274 else
00275 {
00276 alarm(0);
00277 }
00278 ptr=0;
00279 return(NULL);
00280 }
00281 buffer[ptr+ret]=0;
00282 ptr=strlen(buffer);
00283 end=strchr(buffer,'\n');
00284 }
00285 alarm(0);timeout=0;
00286
00287 end[0]=0;
00288 ptr=strlen(buffer)+1;
00289
00290
00291
00292 return(buffer);
00293 }