kpilot Library API Documentation

plugin.cc

00001 /* plugin.cc                            KPilot
00002 **
00003 ** Copyright (C) 2001 by Dan Pilone
00004 **
00005 ** This file defines the base class of all KPilot conduit plugins configuration
00006 ** dialogs. This is necessary so that we have a fixed API to talk to from
00007 ** inside KPilot.
00008 **
00009 ** The factories used by KPilot plugins are also documented here.
00010 */
00011 
00012 /*
00013 ** This program is free software; you can redistribute it and/or modify
00014 ** it under the terms of the GNU Lesser General Public License as published by
00015 ** the Free Software Foundation; either version 2.1 of the License, or
00016 ** (at your option) any later version.
00017 **
00018 ** This program is distributed in the hope that it will be useful,
00019 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00021 ** GNU Lesser General Public License for more details.
00022 **
00023 ** You should have received a copy of the GNU Lesser General Public License
00024 ** along with this program in a file called COPYING; if not, write to
00025 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00026 ** MA 02111-1307, USA.
00027 */
00028 
00029 /*
00030 ** Bug reports and questions can be sent to kde-pim@kde.org
00031 */
00032 
00033 #include "options.h"
00034 
00035 #include <stdlib.h>
00036 
00037 #include <qstringlist.h>
00038 #include <qfileinfo.h>
00039 #include <qdir.h>
00040 #include <qregexp.h>
00041 
00042 #include <dcopclient.h>
00043 #include <kapplication.h>
00044 #include <kmessagebox.h>
00045 #include <kstandarddirs.h>
00046 
00047 #include "pilotSerialDatabase.h"
00048 #include "pilotLocalDatabase.h"
00049 
00050 #include "plugin.moc"
00051 
00052 ConduitConfigBase::ConduitConfigBase(QWidget *parent,
00053     const char *name) :
00054     QObject(parent,name),
00055     fModified(false),
00056     fWidget(0L),
00057     fConduitName(i18n("Unnamed"))
00058 {
00059     FUNCTIONSETUP;
00060 }
00061 
00062 ConduitConfigBase::~ConduitConfigBase()
00063 {
00064     FUNCTIONSETUP;
00065 }
00066 
00067 /* slot */ void ConduitConfigBase::modified()
00068 {
00069     fModified=true;
00070 }
00071 
00072 /* virtual */ QString ConduitConfigBase::maybeSaveText() const
00073 {
00074     FUNCTIONSETUP;
00075 
00076     return i18n("<qt>The <i>%1</i> conduit's settings have been changed. Do you "
00077         "want to save the changes before continuing?</qt>").arg(this->conduitName());
00078 }
00079 
00080 /* virtual */ bool ConduitConfigBase::maybeSave(KConfig *c)
00081 {
00082     FUNCTIONSETUP;
00083 
00084     if (!isModified()) return true;
00085 
00086     int r = KMessageBox::questionYesNoCancel(fWidget,
00087         maybeSaveText(),
00088         i18n("%1 Conduit").arg(this->conduitName()));
00089     if (r == KMessageBox::Cancel) return false;
00090     if (r == KMessageBox::Yes) commit(c);
00091     return true;
00092 }
00093 
00094 ConduitConfig::ConduitConfig(QWidget *parent,
00095     const char *name,
00096     const QStringList &args) :
00097     UIDialog(parent,name,PluginUtility::isModal(args)),
00098     fConfig(0L),
00099     fConduitName(i18n("Unnamed"))
00100 {
00101     FUNCTIONSETUP;
00102 }
00103 
00104 
00105 /* virtual */ ConduitConfig::~ConduitConfig()
00106 {
00107     FUNCTIONSETUP;
00108 }
00109 
00110 ConduitAction::ConduitAction(KPilotDeviceLink *p,
00111     const char *name,
00112     const QStringList &args) :
00113     SyncAction(p,name),
00114     fConfig(0L),
00115     fDatabase(0L),
00116     fLocalDatabase(0L),
00117     fTest(args.contains(CSL1("--test"))),
00118     fBackup(args.contains(CSL1("--backup"))),
00119     fLocal(args.contains(CSL1("--local"))),
00120     fConflictResolution(SyncAction::eAskUser),
00121     fFirstSync(false)
00122 {
00123     FUNCTIONSETUP;
00124     if (args.contains(CSL1("--copyPCToHH"))) fSyncDirection=SyncAction::eCopyPCToHH;
00125     else if (args.contains(CSL1("--copyHHToPC"))) fSyncDirection=SyncAction::eCopyHHToPC;
00126     else if (args.contains(CSL1("--full"))) fSyncDirection=SyncAction::eFullSync;
00127     else fSyncDirection=SyncAction::eFastSync;
00128 
00129     QString cResolution(args.grep(QRegExp("--conflictResolution \\d*")).first());
00130     if (cResolution.isEmpty())
00131     {
00132         fConflictResolution=(SyncAction::eConflictResolution)
00133             cResolution.replace(QRegExp("--conflictResolution (\\d*)"), "\\1").toInt();
00134     }
00135 
00136 #ifdef DEBUG
00137     for (QStringList::ConstIterator it = args.begin();
00138         it != args.end();
00139         ++it)
00140     {
00141         DEBUGCONDUIT << fname << ": " << *it << endl;
00142     }
00143 #endif
00144 }
00145 
00146 /* virtual */ ConduitAction::~ConduitAction()
00147 {
00148     FUNCTIONSETUP;
00149     KPILOT_DELETE(fDatabase);
00150     KPILOT_DELETE(fLocalDatabase);
00151 }
00152 
00153 bool ConduitAction::openDatabases_(const QString &name, bool *retrieved)
00154 {
00155     FUNCTIONSETUP;
00156 
00157 #ifdef DEBUG
00158     DEBUGCONDUIT << fname
00159         << ": Trying to open database "
00160         << name << endl;
00161 #endif
00162 
00163     fLocalDatabase = new PilotLocalDatabase(name, true);
00164 
00165     if (!fLocalDatabase)
00166     {
00167         kdWarning() << k_funcinfo
00168             << ": Could not initialize object for local copy of database \""
00169             << name
00170             << "\"" << endl;
00171             return false;
00172     }
00173 
00174     // if there is no backup db yet, fetch it from the palm, open it and set the full sync flag.
00175     if (!fLocalDatabase->isDBOpen() )
00176     {
00177         QString dbpath(dynamic_cast<PilotLocalDatabase*>(fLocalDatabase)->dbPathName());
00178         KPILOT_DELETE(fLocalDatabase);
00179 #ifdef DEBUG
00180         DEBUGCONDUIT << fname
00181             << ": Backup database "<< dbpath <<" could not be opened. Will fetch a copy from the palm and do a full sync"<<endl;
00182 #endif
00183         struct DBInfo dbinfo;
00184         if (fHandle->findDatabase(name.latin1(), &dbinfo)<0 )
00185         {
00186 #ifdef DEBUG
00187             DEBUGCONDUIT << fname
00188                 << ": Could not get DBInfo for "<<name<<"! "<<endl;
00189 #endif
00190             return false;
00191         }
00192 #ifdef DEBUG
00193             DEBUGCONDUIT << fname
00194                     << ": Found Palm database: "<<dbinfo.name<<endl
00195                     <<"type = "<< dbinfo.type<<endl
00196                     <<"creator = "<< dbinfo.creator<<endl
00197                     <<"version = "<< dbinfo.version<<endl
00198                     <<"index = "<< dbinfo.index<<endl;
00199 #endif
00200         dbinfo.flags &= ~dlpDBFlagOpen;
00201 
00202         // make sure the dir for the backup db really exists!
00203         QFileInfo fi(dbpath);
00204         QString path(QFileInfo(dbpath).dir(TRUE).absPath());
00205         if (!KStandardDirs::exists(path)) {
00206             KStandardDirs::makeDir(path);
00207         }
00208         if (!fHandle->retrieveDatabase(dbpath, &dbinfo) )
00209         {
00210 #ifdef DEBUG
00211             DEBUGCONDUIT << fname << ": Could not retrieve database "<<name<<" from the handheld."<<endl;
00212 #endif
00213             return false;
00214         }
00215         fLocalDatabase = new PilotLocalDatabase(name, true);
00216         if (!fLocalDatabase || !fLocalDatabase->isDBOpen())
00217         {
00218 #ifdef DEBUG
00219             DEBUGCONDUIT << fname << ": local backup of database "<<name<<" could not be initialized."<<endl;
00220 #endif
00221             return false;
00222         }
00223         if (retrieved) *retrieved=true;
00224     }
00225 
00226     // These latin1()'s are ok, since database names are latin1-coded.
00227     fDatabase = new PilotSerialDatabase(pilotSocket(), name /* On pilot */,
00228         this, name.latin1() /* QObject name */);
00229 
00230     if (!fDatabase)
00231     {
00232         kdWarning() << k_funcinfo
00233             << ": Could not open database \""
00234             << name
00235             << "\" on the pilot."
00236             << endl;
00237     }
00238 
00239     return (fDatabase && fDatabase->isDBOpen() &&
00240             fLocalDatabase && fLocalDatabase->isDBOpen() );
00241 }
00242 
00243 // This whole function is for debugging purposes only.
00244 bool ConduitAction::openDatabases_(const QString &dbName,const QString &localPath)
00245 {
00246     FUNCTIONSETUP;
00247 #ifdef DEBUG
00248     DEBUGCONDUIT << fname << ": Doing local test mode for " << dbName << endl;
00249 #endif
00250     if (localPath.isNull())
00251     {
00252 #ifdef DEBUG
00253         DEBUGCONDUIT << fname
00254             << ": local mode test for one database only."
00255             << endl;
00256 #endif
00257         fDatabase = new PilotLocalDatabase(dbName);
00258         fLocalDatabase = 0L;
00259         return false;
00260     }
00261 
00262     fDatabase = new PilotLocalDatabase(localPath,dbName);
00263     fLocalDatabase= new PilotLocalDatabase(dbName, true); // From default
00264     if (!fLocalDatabase || !fDatabase)
00265     {
00266 #ifdef DEBUG
00267         const QString *where2 = PilotLocalDatabase::getDBPath();
00268 
00269         QString none = CSL1("<null>");
00270         DEBUGCONDUIT << fname
00271             << ": Could not open both local copies of \""
00272             << dbName
00273             << "\"" << endl
00274             << "Using \""
00275             << (where2 ? *where2 : none)
00276             << "\" and \""
00277             << (localPath.isEmpty() ? localPath : none)
00278             << "\""
00279             << endl;
00280 #endif
00281     }
00282 #ifdef DEBUG
00283     if (fLocalDatabase)
00284     {
00285         DEBUGCONDUIT << fname
00286             << ": Opened local database "
00287             << fLocalDatabase->dbPathName()
00288             << (fLocalDatabase->isDBOpen() ? " OK" : "")
00289             << endl;
00290     }
00291     if (fDatabase)
00292     {
00293         DEBUGCONDUIT << fname
00294             << ": Opened database "
00295             << fDatabase->dbPathName()
00296             << (fDatabase->isDBOpen() ? " OK" : "")
00297             << endl;
00298     }
00299 #endif
00300 
00301     return (fDatabase && fLocalDatabase);
00302 }
00303 
00304 bool ConduitAction::openDatabases(const QString &dbName, bool*retrieved)
00305 {
00306     FUNCTIONSETUP;
00307 
00308     /*
00309     ** We should look into the --local flag passed
00310     ** to the conduit and act accordingly, but until
00311     ** that is implemented ..
00312     */
00313 #ifdef DEBUG
00314     DEBUGCONDUIT << fname
00315         << ": Mode="
00316         << (isTest() ? "test " : "")
00317         << (isLocal() ? "local " : "")
00318         << endl ;
00319 #endif
00320 
00321     if (isTest() && isLocal())
00322     {
00323         return openDatabases_(dbName,CSL1("/tmp/"));
00324     }
00325     else
00326     {
00327         return openDatabases_(dbName, retrieved);
00328     }
00329 }
00330 
00331 int PluginUtility::findHandle(const QStringList &a)
00332 {
00333     FUNCTIONSETUP;
00334 
00335     int handle = -1;
00336     for (QStringList::ConstIterator i = a.begin();
00337         i != a.end(); ++i)
00338     {
00339         if ((*i).left(7) == CSL1("handle="))
00340         {
00341             QString s = (*i).mid(7);
00342             if (s.isEmpty()) continue;
00343 
00344             handle = s.toInt();
00345 #ifdef DEBUG
00346             DEBUGCONDUIT << fname
00347                 << ": Got handle "
00348                 << handle
00349                 << endl;
00350 #endif
00351             if (handle<1)
00352             {
00353                 kdWarning() << k_funcinfo
00354                     << ": Improbable handle value found."
00355                     << endl;
00356             }
00357             return handle;
00358         }
00359     }
00360 
00361 #ifdef DEBUG
00362     DEBUGCONDUIT << fname
00363         << ": No handle= parameter found."
00364         << endl;
00365 #endif
00366 
00367     return -1;
00368 }
00369 
00370 bool PluginUtility::isModal(const QStringList &a)
00371 {
00372     return a.contains(CSL1("modal"));
00373 }
00374 
00375 /* static */ bool PluginUtility::isRunning(const QCString &n)
00376 {
00377     FUNCTIONSETUP;
00378 
00379     DCOPClient *dcop = KApplication::kApplication()->dcopClient();
00380     QCStringList apps = dcop->registeredApplications();
00381     return apps.contains(n);
00382 }
00383 
00384 ConduitConfigImplementation::ConduitConfigImplementation(QWidget *w, const char *n,
00385     const QStringList &a,
00386     ConduitConfigBase *(*f)(QWidget *, const char *)) :
00387     ConduitConfig(w,n,a)
00388 {
00389     FUNCTIONSETUP;
00390     fConfigWidget=f(widget(),n);
00391     fConfigWidget->widget()->adjustSize();
00392     fConfigWidget->widget()->show();
00393     fConduitName=fConfigWidget->conduitName();
00394 #ifdef DEBUG
00395     DEBUGCONDUIT << fname << ": Created conduit " << fConduitName << endl;
00396 #endif
00397     widget()->adjustSize();
00398     adjustSize();
00399 }
00400 
00401 ConduitConfigImplementation::~ConduitConfigImplementation()
00402 {
00403     FUNCTIONSETUP;
00404 }
00405 
00406 void ConduitConfigImplementation::readSettings()
00407 {
00408     if (fConfig) fConfigWidget->load(fConfig);
00409 }
00410 
00411 void ConduitConfigImplementation::commitChanges()
00412 {
00413     if (fConfig) fConfigWidget->commit(fConfig);
00414 }
00415 
KDE Logo
This file is part of the documentation for kpilot Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:49 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003