kpilot/lib

plugin.cc

00001 /* KPilot
00002 **
00003 ** Copyright (C) 2001 by Dan Pilone
00004 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 **
00006 ** This file defines the base class of all KPilot conduit plugins configuration
00007 ** dialogs. This is necessary so that we have a fixed API to talk to from
00008 ** inside KPilot.
00009 **
00010 ** The factories used by KPilot plugins are also documented here.
00011 */
00012 
00013 /*
00014 ** This program is free software; you can redistribute it and/or modify
00015 ** it under the terms of the GNU Lesser General Public License as published by
00016 ** the Free Software Foundation; either version 2.1 of the License, or
00017 ** (at your option) any later version.
00018 **
00019 ** This program is distributed in the hope that it will be useful,
00020 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00022 ** GNU Lesser General Public License for more details.
00023 **
00024 ** You should have received a copy of the GNU Lesser General Public License
00025 ** along with this program in a file called COPYING; if not, write to
00026 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00027 ** MA 02110-1301, USA.
00028 */
00029 
00030 /*
00031 ** Bug reports and questions can be sent to kde-pim@kde.org
00032 */
00033 
00034 #include "options.h"
00035 
00036 #include <stdlib.h>
00037 
00038 #include <qstringlist.h>
00039 #include <qfileinfo.h>
00040 #include <qdir.h>
00041 #include <qregexp.h>
00042 
00043 #include <dcopclient.h>
00044 #include <kapplication.h>
00045 #include <kmessagebox.h>
00046 #include <kstandarddirs.h>
00047 #include <klibloader.h>
00048 
00049 #include "pilotSerialDatabase.h"
00050 #include "pilotLocalDatabase.h"
00051 
00052 #include "plugin.moc"
00053 
00054 ConduitConfigBase::ConduitConfigBase(QWidget *parent,
00055     const char *name) :
00056     QObject(parent,name),
00057     fModified(false),
00058     fWidget(0L),
00059     fConduitName(i18n("Unnamed"))
00060 {
00061     FUNCTIONSETUP;
00062 }
00063 
00064 ConduitConfigBase::~ConduitConfigBase()
00065 {
00066     FUNCTIONSETUP;
00067 }
00068 
00069 /* slot */ void ConduitConfigBase::modified()
00070 {
00071     fModified=true;
00072     emit changed(true);
00073 }
00074 
00075 /* virtual */ QString ConduitConfigBase::maybeSaveText() const
00076 {
00077     FUNCTIONSETUP;
00078 
00079     return i18n("<qt>The <i>%1</i> conduit's settings have been changed. Do you "
00080         "want to save the changes before continuing?</qt>").arg(this->conduitName());
00081 }
00082 
00083 /* virtual */ bool ConduitConfigBase::maybeSave()
00084 {
00085     FUNCTIONSETUP;
00086 
00087     if (!isModified()) return true;
00088 
00089     int r = KMessageBox::questionYesNoCancel(fWidget,
00090         maybeSaveText(),
00091         i18n("%1 Conduit").arg(this->conduitName()), KStdGuiItem::save(), KStdGuiItem::discard());
00092     if (r == KMessageBox::Cancel) return false;
00093     if (r == KMessageBox::Yes) commit();
00094     return true;
00095 }
00096 
00097 ConduitAction::ConduitAction(KPilotLink *p,
00098     const char *name,
00099     const QStringList &args) :
00100     SyncAction(p,name),
00101     fDatabase(0L),
00102     fLocalDatabase(0L),
00103     fSyncDirection(args),
00104     fConflictResolution(SyncAction::eAskUser),
00105     fFirstSync(false)
00106 {
00107     FUNCTIONSETUP;
00108 
00109     QString cResolution(args.grep(QRegExp(CSL1("--conflictResolution \\d*"))).first());
00110     if (cResolution.isEmpty())
00111     {
00112         fConflictResolution=(SyncAction::ConflictResolution)
00113             cResolution.replace(QRegExp(CSL1("--conflictResolution (\\d*)")), CSL1("\\1")).toInt();
00114     }
00115 
00116 #ifdef DEBUG
00117     for (QStringList::ConstIterator it = args.begin();
00118         it != args.end();
00119         ++it)
00120     {
00121         DEBUGLIBRARY << fname << ": " << *it << endl;
00122     }
00123 
00124     DEBUGLIBRARY << fname << ": Direction=" << fSyncDirection.name() << endl;
00125 #endif
00126 }
00127 
00128 /* virtual */ ConduitAction::~ConduitAction()
00129 {
00130     FUNCTIONSETUP;
00131     KPILOT_DELETE(fDatabase);
00132     KPILOT_DELETE(fLocalDatabase);
00133 }
00134 
00135 bool ConduitAction::openDatabases(const QString &name, bool *retrieved)
00136 {
00137     FUNCTIONSETUP;
00138 
00139     DEBUGLIBRARY << fname
00140         << ": Trying to open database "
00141         << name << endl;
00142     DEBUGLIBRARY << fname
00143         << ": Mode="
00144         << (syncMode().isTest() ? "test " : "")
00145         << (syncMode().isLocal() ? "local " : "")
00146         << endl ;
00147 
00148     KPILOT_DELETE(fLocalDatabase);
00149 
00150     QString localPathName = PilotLocalDatabase::getDBPath() + name;
00151     PilotLocalDatabase *localDB = new PilotLocalDatabase( localPathName );
00152 
00153     if (!localDB)
00154     {
00155         kdWarning() << k_funcinfo
00156             << ": Could not initialize object for local copy of database \""
00157             << name
00158             << "\"" << endl;
00159         if (retrieved) *retrieved = false;
00160         return false;
00161     }
00162 
00163     // if there is no backup db yet, fetch it from the palm, open it and set the full sync flag.
00164     if (!localDB->isOpen() )
00165     {
00166         QString dbpath(localDB->dbPathName());
00167         KPILOT_DELETE(localDB);
00168         DEBUGLIBRARY << fname
00169             << ": Backup database " << dbpath
00170             << " not found." << endl;
00171         struct DBInfo dbinfo;
00172 
00173 // TODO Extend findDatabase() with extra overload?
00174         if (deviceLink()->findDatabase(Pilot::toPilot( name ), &dbinfo)<0 )
00175         {
00176             kdWarning() << k_funcinfo
00177                 << ": Could not get DBInfo for " << name << endl;
00178             if (retrieved) *retrieved = false;
00179             return false;
00180         }
00181 
00182         DEBUGLIBRARY << fname
00183                 << ": Found Palm database: " << dbinfo.name <<endl
00184                 << fname << ": type = " << dbinfo.type
00185                 << " creator = " << dbinfo.creator
00186                 << " version = " << dbinfo.version
00187                 << " index = " << dbinfo.index << endl;
00188         dbinfo.flags &= ~dlpDBFlagOpen;
00189 
00190         // make sure the dir for the backup db really exists!
00191         QFileInfo fi(dbpath);
00192         QString path(QFileInfo(dbpath).dir(true).absPath());
00193         if (!path.endsWith(CSL1("/"))) path.append(CSL1("/"));
00194         if (!KStandardDirs::exists(path))
00195         {
00196             DEBUGLIBRARY << fname << ": Trying to create path for database: <"
00197                 << path << ">" << endl;
00198             KStandardDirs::makeDir(path);
00199         }
00200         if (!KStandardDirs::exists(path))
00201         {
00202             DEBUGLIBRARY << fname << ": Database directory does not exist." << endl;
00203             if (retrieved) *retrieved = false;
00204             return false;
00205         }
00206 
00207         if (!deviceLink()->retrieveDatabase(dbpath, &dbinfo) )
00208         {
00209             kdWarning() << k_funcinfo << ": Could not retrieve database "<<name<<" from the handheld."<<endl;
00210             if (retrieved) *retrieved = false;
00211             return false;
00212         }
00213         localDB = new PilotLocalDatabase( localPathName );
00214         if (!localDB || !localDB->isOpen())
00215         {
00216             kdWarning() << k_funcinfo << ": local backup of database "<<name<<" could not be initialized."<<endl;
00217             if (retrieved) *retrieved = false;
00218             return false;
00219         }
00220         if (retrieved) *retrieved=true;
00221     }
00222     fLocalDatabase = localDB;
00223 
00224     fDatabase = deviceLink()->database( name );
00225 
00226     if (!fDatabase)
00227     {
00228         kdWarning() << k_funcinfo
00229             << ": Could not open database \""
00230             << name
00231             << "\" on the pilot."
00232             << endl;
00233     }
00234 
00235     return (fDatabase && fDatabase->isOpen() &&
00236             fLocalDatabase && fLocalDatabase->isOpen() );
00237 }
00238 
00239 
00240 bool ConduitAction::changeSync(SyncMode::Mode m)
00241 {
00242     FUNCTIONSETUP;
00243 
00244     if ( fSyncDirection.isSync() && SyncMode::eFullSync == m)
00245     {
00246         fSyncDirection.setMode(m);
00247         return true;
00248     }
00249     return false;
00250 }
00251 
00252 
00253 namespace PluginUtility
00254 {
00255 
00256 QString findArgument(const QStringList &a, const QString &arg)
00257 {
00258     FUNCTIONSETUP;
00259 
00260     QString search;
00261 
00262     if (arg.startsWith( CSL1("--") ))
00263     {
00264         search = arg;
00265     }
00266     else
00267     {
00268         search = CSL1("--") + arg;
00269     }
00270     search.append( CSL1("=") );
00271 
00272 
00273     QStringList::ConstIterator end = a.end();
00274     for (QStringList::ConstIterator i = a.begin(); i != end; ++i)
00275     {
00276         if ((*i).startsWith( search ))
00277         {
00278             QString s = (*i).mid(search.length());
00279             return s;
00280         }
00281     }
00282 
00283     return QString::null;
00284 }
00285 
00286 /* static */ bool isRunning(const QCString &n)
00287 {
00288     DCOPClient *dcop = KApplication::kApplication()->dcopClient();
00289     QCStringList apps = dcop->registeredApplications();
00290     return apps.contains(n);
00291 }
00292 
00293 
00294 /* static */ unsigned long pluginVersion(const KLibrary *lib)
00295 {
00296     QString symbol = CSL1("version_");
00297     symbol.append(lib->name());
00298 
00299     if (!lib->hasSymbol(symbol.latin1())) return 0;
00300 
00301     unsigned long *p = (unsigned long *)(lib->symbol(symbol.latin1()));
00302     return *p;
00303 }
00304 
00305 
00306 /* static */ QString pluginVersionString(const KLibrary *lib)
00307 {
00308     QString symbol= CSL1("id_");
00309     symbol.append(lib->name());
00310 
00311     if (!lib->hasSymbol(symbol.latin1())) return QString::null;
00312 
00313     return QString::fromLatin1(*((const char **)(lib->symbol(symbol.latin1()))));
00314 }
00315 
00316 
00317 }
00318 
KDE Home | KDE Accessibility Home | Description of Access Keys