kpilot

kpilotlink.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 ** Copyright (C) 2006-2007 Adriaan de Groot <groot@kde.org>
00006 ** Copyright (C) 2007 Jason 'vanRijn' Kasper <vR@movingparts.net>
00007 **
00008 */
00009 
00010 /*
00011 ** This program is free software; you can redistribute it and/or modify
00012 ** it under the terms of the GNU Lesser General Public License as published by
00013 ** the Free Software Foundation; either version 2.1 of the License, or
00014 ** (at your option) any later version.
00015 **
00016 ** This program is distributed in the hope that it will be useful,
00017 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 ** GNU Lesser General Public License for more details.
00020 **
00021 ** You should have received a copy of the GNU Lesser General Public License
00022 ** along with this program in a file called COPYING; if not, write to
00023 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 ** MA 02110-1301, USA.
00025 */
00026 
00027 /*
00028 ** Bug reports and questions can be sent to kde-pim@kde.org
00029 */
00030 
00031 #include "options.h"
00032 
00033 
00034 
00035 #include <sys/stat.h>
00036 #include <sys/types.h>
00037 #include <stdio.h>
00038 #include <unistd.h>
00039 #include <fcntl.h>
00040 #include <errno.h>
00041 
00042 #include <iostream>
00043 
00044 #include <pi-source.h>
00045 #include <pi-socket.h>
00046 #include <pi-dlp.h>
00047 #include <pi-file.h>
00048 #include <pi-buffer.h>
00049 
00050 #include <qdir.h>
00051 #include <qtimer.h>
00052 #include <qdatetime.h>
00053 #include <qthread.h>
00054 
00055 #include <kconfig.h>
00056 #include <kmessagebox.h>
00057 #include <kstandarddirs.h>
00058 #include <kurl.h>
00059 #include <kio/netaccess.h>
00060 
00061 #include "pilotUser.h"
00062 #include "pilotSysInfo.h"
00063 #include "pilotCard.h"
00064 #include "pilotSerialDatabase.h"
00065 #include "pilotLocalDatabase.h"
00066 
00067 #include "kpilotlink.moc"
00068 
00073 class TickleThread : public QThread
00074 {
00075 public:
00076     TickleThread(KPilotLink *d, bool *done, int timeout) :
00077         QThread(),
00078         fHandle(d),
00079         fDone(done),
00080         fTimeout(timeout)
00081     { };
00082     virtual ~TickleThread();
00083 
00084     virtual void run();
00085 
00086     static const int ChecksPerSecond = 5;
00087     static const int SecondsPerTickle = 5;
00088 
00089 private:
00090     KPilotLink *fHandle;
00091     bool *fDone;
00092     int fTimeout;
00093 } ;
00094 
00095 TickleThread::~TickleThread()
00096 {
00097 }
00098 
00099 void TickleThread::run()
00100 {
00101     FUNCTIONSETUP;
00102     int subseconds = ChecksPerSecond;
00103     int ticktock = SecondsPerTickle;
00104     int timeout = fTimeout;
00105     DEBUGKPILOT << fname << ": Running for "
00106         << timeout << " seconds." << endl;
00107     DEBUGKPILOT << fname << ": Done @" << (void *) fDone << endl;
00108 
00109     while (!(*fDone))
00110     {
00111         QThread::msleep(1000/ChecksPerSecond);
00112         if (!(--subseconds))
00113         {
00114             if (timeout)
00115             {
00116                 if (!(--timeout))
00117                 {
00118                     QApplication::postEvent(fHandle, new QEvent(static_cast<QEvent::Type>(KPilotLink::EventTickleTimeout)));
00119                     break;
00120                 }
00121             }
00122             subseconds=ChecksPerSecond;
00123             if (!(--ticktock))
00124             {
00125                 ticktock=SecondsPerTickle;
00126                 fHandle->tickle();
00127             }
00128         }
00129     }
00130 }
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 KPilotLink::KPilotLink( QObject *parent, const char *name ) :
00141     QObject( parent, name ),
00142     fPilotPath(QString::null),
00143     fPilotUser(0L),
00144     fPilotSysInfo(0L),
00145     fTickleDone(true),
00146     fTickleThread(0L)
00147 
00148 {
00149     FUNCTIONSETUP;
00150 
00151     fPilotUser = new KPilotUser();
00152     strncpy( fPilotUser->data()->username, "Henk Westbroek",
00153         sizeof(fPilotUser->data()->username)-1);
00154     fPilotUser->setLastSuccessfulSyncDate( 1139171019 );
00155 
00156     fPilotSysInfo = new KPilotSysInfo();
00157     memset(fPilotSysInfo->sysInfo()->prodID, 0,
00158         sizeof(fPilotSysInfo->sysInfo()->prodID));
00159     strncpy(fPilotSysInfo->sysInfo()->prodID, "LocalLink",
00160         sizeof(fPilotSysInfo->sysInfo()->prodID)-1);
00161     fPilotSysInfo->sysInfo()->prodIDLength =
00162         strlen(fPilotSysInfo->sysInfo()->prodID);
00163 }
00164 
00165 KPilotLink::~KPilotLink()
00166 {
00167     FUNCTIONSETUP;
00168     KPILOT_DELETE(fPilotUser);
00169     KPILOT_DELETE(fPilotSysInfo);
00170 }
00171 
00172 /* virtual */ bool KPilotLink::event(QEvent *e)
00173 {
00174     if ((int)e->type() == EventTickleTimeout)
00175     {
00176         stopTickle();
00177         emit timeout();
00178         return true;
00179     }
00180     else return QObject::event(e);
00181 }
00182 
00183 /*
00184 Start a tickle thread with the indicated timeout.
00185 */
00186 void KPilotLink::startTickle(unsigned int timeout)
00187 {
00188     FUNCTIONSETUP;
00189 
00190     Q_ASSERT(fTickleDone);
00191 
00192     /*
00193     ** We've told the thread to finish up, but it hasn't
00194     ** done so yet - so wait for it to do so, should be
00195     ** only 200ms at most.
00196     */
00197     if (fTickleDone && fTickleThread)
00198     {
00199         fTickleThread->wait();
00200         KPILOT_DELETE(fTickleThread);
00201     }
00202 
00203     DEBUGKPILOT << fname << ": Done @" << (void *) (&fTickleDone) << endl;
00204 
00205     fTickleDone = false;
00206     fTickleThread = new TickleThread(this,&fTickleDone,timeout);
00207     fTickleThread->start();
00208 }
00209 
00210 void KPilotLink::stopTickle()
00211 {
00212     FUNCTIONSETUP;
00213     fTickleDone = true;
00214     if (fTickleThread)
00215     {
00216         fTickleThread->wait();
00217         KPILOT_DELETE(fTickleThread);
00218     }
00219 }
00220 
00221 unsigned int KPilotLink::installFiles(const QStringList & l, const bool deleteFiles)
00222 {
00223     FUNCTIONSETUP;
00224 
00225     QStringList::ConstIterator i,e;
00226     unsigned int k = 0;
00227     unsigned int n = 0;
00228     unsigned int total = l.count();
00229 
00230     for (i = l.begin(), e = l.end(); i != e; ++i)
00231     {
00232         emit logProgress(QString::null,
00233             (int) ((100.0 / total) * (float) n));
00234 
00235         if (installFile(*i, deleteFiles))
00236             k++;
00237         n++;
00238     }
00239     emit logProgress(QString::null, 100);
00240 
00241     return k;
00242 }
00243 
00244 void KPilotLink::addSyncLogEntry(const QString & entry, bool log)
00245 {
00246     FUNCTIONSETUP;
00247     if (entry.isEmpty()) return;
00248 
00249     addSyncLogEntryImpl(entry);
00250     if (log)
00251     {
00252         emit logMessage(entry);
00253     }
00254 }
00255 
00256 
00257 /* virtual */ int KPilotLink::openConduit()
00258 {
00259     return 0;
00260 }
00261 
00262 /* virtual */ int KPilotLink::pilotSocket() const
00263 {
00264     return -1;
00265 }
00266 
00267 /* virtual */ PilotDatabase *KPilotLink::database( const DBInfo *info )
00268 {
00269     FUNCTIONSETUP;
00270     return database( Pilot::fromPilot( info->name ) );
00271 }
00272