kpilot/lib

kpilotlink.cc

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 QCustomEvent(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 void KPilotLink::customEvent(QCustomEvent *e)
00173 {
00174     if ((int)e->type() == EventTickleTimeout)
00175     {
00176         stopTickle();
00177         emit timeout();
00178     }
00179 }
00180 
00181 /*
00182 Start a tickle thread with the indicated timeout.
00183 */
00184 void KPilotLink::startTickle(unsigned int timeout)
00185 {
00186     FUNCTIONSETUP;
00187 
00188     Q_ASSERT(fTickleDone);
00189 
00190     /*
00191     ** We've told the thread to finish up, but it hasn't
00192     ** done so yet - so wait for it to do so, should be
00193     ** only 200ms at most.
00194     */
00195     if (fTickleDone && fTickleThread)
00196     {
00197         fTickleThread->wait();
00198         KPILOT_DELETE(fTickleThread);
00199     }
00200 
00201     DEBUGKPILOT << fname << ": Done @" << (void *) (&fTickleDone) << endl;
00202 
00203     fTickleDone = false;
00204     fTickleThread = new TickleThread(this,&fTickleDone,timeout);
00205     fTickleThread->start();
00206 }
00207 
00208 void KPilotLink::stopTickle()
00209 {
00210     FUNCTIONSETUP;
00211     fTickleDone = true;
00212     if (fTickleThread)
00213     {
00214         fTickleThread->wait();
00215         KPILOT_DELETE(fTickleThread);
00216     }
00217 }
00218 
00219 unsigned int KPilotLink::installFiles(const QStringList & l, const bool deleteFiles)
00220 {
00221     FUNCTIONSETUP;
00222 
00223     QStringList::ConstIterator i,e;
00224     unsigned int k = 0;
00225     unsigned int n = 0;
00226     unsigned int total = l.count();
00227 
00228     for (i = l.begin(), e = l.end(); i != e; ++i)
00229     {
00230         emit logProgress(QString::null,
00231             (int) ((100.0 / total) * (float) n));
00232 
00233         if (installFile(*i, deleteFiles))
00234             k++;
00235         n++;
00236     }
00237     emit logProgress(QString::null, 100);
00238 
00239     return k;
00240 }
00241 
00242 void KPilotLink::addSyncLogEntry(const QString & entry, bool log)
00243 {
00244     FUNCTIONSETUP;
00245     if (entry.isEmpty()) return;
00246 
00247     addSyncLogEntryImpl(entry);
00248     if (log)
00249     {
00250         emit logMessage(entry);
00251     }
00252 }
00253 
00254 
00255 /* virtual */ int KPilotLink::openConduit()
00256 {
00257     return 0;
00258 }
00259 
00260 /* virtual */ int KPilotLink::pilotSocket() const
00261 {
00262     return -1;
00263 }
00264 
00265 /* virtual */ PilotDatabase *KPilotLink::database( const DBInfo *info )
00266 {
00267     FUNCTIONSETUP;
00268     return database( Pilot::fromPilot( info->name ) );
00269 }
00270 
KDE Home | KDE Accessibility Home | Description of Access Keys