Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

learntab.cpp

Go to the documentation of this file.
00001 /*
00002 Opie-Remote.  emulates remote controlls on an iPaq (and maybe a Zaurus) in Opie.
00003 Copyright (C) 2002 Thomas Stephens
00004  
00005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
00006 License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
00007 version.
00008  
00009 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
00010 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00011 Public License for more details.
00012  
00013 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
00014 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00015 */
00016 
00017 #include "learntab.h"
00018 
00019 /* OPIE */
00020 #include <qpe/qpeapplication.h>
00021 
00022 LearnTab::LearnTab(QWidget *parent, const char *name):QWidget(parent,name)
00023 {
00024     QVBoxLayout *layout = new QVBoxLayout(this);
00025     QHBoxLayout *bottomLayout = new QHBoxLayout(this);
00026 
00027     layout->insertSpacing(0,5);
00028     remotesBox = new QListBox(this, "remotesBox");
00029     layout->insertWidget(0, remotesBox, 1);
00030     remotesBox->insertStringList(getRemotes());
00031 
00032     layout->insertSpacing(-1,5);
00033     layout->insertLayout(-1, bottomLayout);
00034     layout->insertSpacing(-1,5);
00035 
00036     QPushButton *add = new QPushButton("Add", this, "add");
00037     bottomLayout->insertSpacing(-1, 5);
00038     bottomLayout->insertWidget(-1, add);
00039     bottomLayout->insertSpacing(-1, 5);
00040     QPushButton *edit = new QPushButton("Edit", this, "edit");
00041     bottomLayout->insertWidget(-1, edit);
00042     bottomLayout->insertSpacing(-1, 5);
00043     QPushButton *del = new QPushButton("Delete", this, "delete");
00044     bottomLayout->insertWidget(-1, del);
00045     bottomLayout->insertSpacing(-1, 5);
00046 
00047     connect(add, SIGNAL(clicked()), this, SLOT(add()) );
00048     connect(edit, SIGNAL(clicked()), this, SLOT(edit()) );
00049     connect(del, SIGNAL(clicked()), this, SLOT(del()) );
00050 }
00051 
00052 void LearnTab::add()
00053 {
00054     printf("LearnTab::add: add pressed\n");
00055     RecordDialog *dialog = new RecordDialog(this);
00056     QPEApplication::showDialog( dialog );
00057 }
00058 
00059 void LearnTab::edit()
00060 {}
00061 
00062 void LearnTab::del()
00063 {}
00064 
00065 QStringList LearnTab::getRemotes()
00066 {
00067     const char write_buffer[] = "LIST\n";
00068     const char *readbuffer;
00069     int i, numlines;
00070     QStringList list;
00071 
00072     addr.sun_family=AF_UNIX;
00073     strcpy(addr.sun_path,"/dev/lircd");
00074 
00075     fd = socket(AF_UNIX, SOCK_STREAM, 0);
00076     if(fd == -1)
00077     {
00078         QMessageBox *mb = new QMessageBox("Error!",
00079                                           "couldnt connect to socket",
00080                                           QMessageBox::NoIcon,
00081                                           QMessageBox::Ok,
00082                                           QMessageBox::NoButton,
00083                                           QMessageBox::NoButton);
00084         mb->exec();
00085         perror("LearnTab::GetRemotes");
00086         return NULL;
00087     }
00088 
00089     if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1)
00090     {
00091         QMessageBox *mb = new QMessageBox("Error!",
00092                                           "couldnt connect to socket",
00093                                           QMessageBox::NoIcon,
00094                                           QMessageBox::Ok,
00095                                           QMessageBox::NoButton,
00096                                           QMessageBox::NoButton);
00097         mb->exec();
00098         perror("LearnTab::GetRemotes");
00099         return NULL;
00100     }
00101 
00102     write(fd, write_buffer, strlen(write_buffer));
00103 
00104     for(i=0; i<5; i++)
00105     {
00106         printf("%d\n", i);
00107         readbuffer = readPacket();
00108         printf("%s", readbuffer);
00109         printf("%d\n", i);
00110     }
00111 
00112     numlines = atoi(readbuffer);
00113 
00114     for(i=0; i<numlines; i++)
00115     {
00116         list+=readPacket();
00117     }
00118 
00119     if(strcasecmp(readPacket(), "END") != 0)
00120     {
00121         QMessageBox *mb = new QMessageBox("Error!",
00122                                           "bad packet",
00123                                           QMessageBox::NoIcon,
00124                                           QMessageBox::Ok,
00125                                           QMessageBox::NoButton,
00126                                           QMessageBox::NoButton);
00127         mb->exec();
00128         perror("LearnTab::GetRemotes");
00129         return NULL;
00130     }
00131 
00132     ::close(fd);
00133     return list;
00134 }
00135 
00136 //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html
00137 const char *LearnTab::readPacket()
00138 {
00139     static char buffer[PACKET_SIZE+1]="";
00140     char *end;
00141     static int ptr=0,end_len=0;
00142     ssize_t ret;
00143     timeout = 0;
00144 
00145     if(ptr>0)
00146     {
00147         memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1);
00148         ptr=strlen(buffer);
00149         end=strchr(buffer,'\n');
00150     }
00151     else
00152     {
00153         end=NULL;
00154     }
00155     alarm(TIMEOUT);
00156     while(end==NULL)
00157     {
00158         if(PACKET_SIZE<=ptr)
00159         {
00160             fprintf(stderr,"bad packet\n");
00161             ptr=0;
00162             return(NULL);
00163         }
00164         ret=read(fd,buffer+ptr,PACKET_SIZE-ptr);
00165 
00166         if(ret<=0 || timeout)
00167         {
00168             if(timeout)
00169             {
00170                 fprintf(stderr,"timeout\n");
00171             }
00172             else
00173             {
00174                 alarm(0);
00175             }
00176             ptr=0;
00177             return(NULL);
00178         }
00179         buffer[ptr+ret]=0;
00180         ptr=strlen(buffer);
00181         end=strchr(buffer,'\n');
00182     }
00183     alarm(0);timeout=0;
00184 
00185     end[0]=0;
00186     ptr=strlen(buffer)+1;
00187     //#       ifdef DEBUG
00188     //  printf("buffer: -%s-\n",buffer);
00189     //#       endif
00190     return(buffer);
00191 }
00192 

Generated on Sat Nov 5 16:18:02 2005 for OPIE by  doxygen 1.4.2