kpilot Library API Documentation

JPilotProxy-factory.cc

00001 /* JPilotProxy-factory.cc                     KPilot
00002 **
00003 ** Copyright (C) 2002 by Reinhold Kainhofer
00004 **
00005 ** This file defines the factory for the JPilotProxy-conduit plugin.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00022 ** MA 02111-1307, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 #include "options.h"
00030 
00031 #include <qdir.h>
00032 #include <kinstance.h>
00033 #include <kaboutdata.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <dirent.h>
00037 #include "kpilotlink.h"
00038 #include "kconfig.h"
00039 #include "JPilotProxy-factory.moc"
00040 
00041 
00042 extern "C"
00043 {
00044 
00045 void *init_conduit_JPilotProxy() {
00046     FUNCTIONSETUP;
00047 //  K_EXPORT_COMPONENT_FACTORY( libkspread, KSpreadFactory )
00048     return new JPilotProxyConduitFactory;
00049 }
00050 
00051 };
00052 
00053 
00054 bool JPilotProxyConduitFactory::pluginsloaded=false;
00055 QString JPilotProxyConduitFactory::settingsGroup="JPilotPluginProxy";
00056 QString JPilotProxyConduitFactory::PluginPathes="PluginPathes";
00057 QString JPilotProxyConduitFactory::LoadedPlugins="LoadedPlugins";
00058 
00059 KAboutData *JPilotProxyConduitFactory::fAbout = 0L;
00060 PluginList_t *JPilotProxyConduitFactory::plugins=0L;
00061 
00062 JPilotProxyConduitFactory::JPilotProxyConduitFactory(QObject *p, const char *n) :
00063         KLibFactory(p,n)  {
00064     FUNCTIONSETUP;
00065     fConfig=0L;
00066     plugins=new PluginList_t();
00067     // load the library containing the JPilot API functions. If this fails, any plugin will probably crash KPilot, so just exit!!!
00068     apilib=KLibLoader::self()->globalLibrary("libJPilotAPI");
00069         #ifdef DEBUG
00070     if (!apilib) DEBUGCONDUIT << fname << ": JPilotAPI library could not be loaded\n  error ["<<KLibLoader::self()->lastErrorMessage()<<"]" << endl;
00071     else DEBUGCONDUIT << fname << ": loaded JPilotAPI library" << endl;
00072     #endif
00073   jp_logf(4, "testing...");
00074 
00075     fInstance = new KInstance(n);
00076     fAbout = new KAboutData(n,
00077         I18N_NOOP("JPilotProxy Conduit for KPilot"),
00078         KPILOT_VERSION,
00079         I18N_NOOP("Configures the JPilotProxy Conduit for KPilot"),
00080         KAboutData::License_GPL,
00081         "(C) 2002, Reinhold F. Kainhofer");
00082     fAbout->addAuthor("Reinhold Kainhofer", I18N_NOOP("Original author and maintainer of this conduit"), "reinhold@kainhofer.com", "http://reinhold.kainhofer.com");
00083     fAbout->addCredit("Judd Montgomery", I18N_NOOP("Author of JPilot"),     "judd@engineer.com", "http://www.jpilot.org/");
00084 }
00085 
00086 JPilotProxyConduitFactory::~JPilotProxyConduitFactory() {
00087     FUNCTIONSETUP;
00088 
00089     PluginIterator_t it(*plugins); // iterator for plugin list
00090     for ( ; it.current(); ++it ) {
00091         #ifdef DEBUG
00092         DEBUGCONDUIT<<"unloading library "<< it.current()->info.fullpath<<" ("<<it.current()->info.name<<"), address="<<it.current()->lib<<endl;
00093         #endif
00094         it.current()->exit_cleanup();
00095     }
00096     if (apilib) KLibLoader::self()->unloadLibrary(apilib->fileName());
00097 
00098     KPILOT_DELETE(fInstance);
00099     KPILOT_DELETE(fAbout);
00100 }
00101 
00102 /* virtual */ QObject *JPilotProxyConduitFactory::createObject( QObject *p,
00103     const char *n, const char *c, const QStringList &a) {
00104     FUNCTIONSETUP;
00105 
00106         #ifdef DEBUG
00107     DEBUGCONDUIT << fname << ": Creating object of class "  << c << endl;
00108         #endif
00109 
00110     if (qstrcmp(c,"ConduitConfig")==0) {
00111         QWidget *w = dynamic_cast<QWidget *>(p);
00112 
00113         if (w) {
00114             return createSetupWidget(w,n,a);
00115         } else {
00116                 #ifdef DEBUG
00117             DEBUGCONDUIT << fname << ": Couldn't cast parent to widget." << endl;
00118                 #endif
00119             return 0L;
00120         }
00121     }
00122 
00123     if (qstrcmp(c,"SyncAction")==0) {
00124         KPilotDeviceLink *d = dynamic_cast<KPilotDeviceLink *>(p);
00125 
00126         if (d) {
00127             return createConduit(d,n,a);
00128         } else {
00129             kdError() << k_funcinfo
00130                 << ": Couldn't cast to KPilotDeviceLink."
00131                 << endl;
00132         }
00133     }
00134 
00135     return 0L;
00136 }
00137 
00138 JPlugin*JPilotProxyConduitFactory::addPlugin( QString path, bool on) {
00139     FUNCTIONSETUP;
00140     // TODO: search the plugin list if the plugin was already loaded
00141     JPlugin*newplugin=new JPlugin( path );
00142     #ifdef DEBUG
00143     DEBUGCONDUIT<<"successfully created a JPlugin instance for "<<path<<endl;
00144     #endif
00145     if (newplugin->loaded) {
00146         newplugin->info.sync_on=on;
00147         #ifdef DEBUG
00148         DEBUGCONDUIT<<"loading "<<path<<" was successful"<<endl;
00149         #endif
00150         // if the plugin was loaded successfully, insert it into the list of plugins
00151         plugins->append(newplugin);
00152         jp_startup_info si;
00153         si.base_dir="/usr/local";
00154         newplugin->startup(&si);
00155         return newplugin;
00156     } else delete newplugin;
00157     return 0;
00158 }
00159 
00160 // This is not yet optimal, but should work for now...
00161 int JPilotProxyConduitFactory::removePlugin( QString path) {
00162     FUNCTIONSETUP;
00163     
00164     JPlugin*plugintodel=NULL;
00165 
00166     PluginIterator_t it(*plugins); // iterator for plugin list
00167     for ( ; it.current(); ++it ) {
00168         JPlugin *plug = it.current();
00169         if (plug->lib && strcmp(plug->info.fullpath, path)) plugintodel=plug;
00170     }
00171 
00172     if (plugintodel) {
00173         plugins->take(plugins->find(plugintodel));
00174         plugintodel->exit_cleanup();
00175         delete plugintodel;
00176     }
00177 }
00178 
00179 int JPilotProxyConduitFactory::addPluginPath(QString path, KConfig*fC) {
00180     FUNCTIONSETUP;
00181     // find the list of possible plugins in the directory given by path
00182     QDir dir(path);
00183     QStringList plugs=dir.entryList("*.so");
00184 
00185     for (QStringList::Iterator it = plugs.begin(); it != plugs.end(); ++it ) {
00186         #ifdef DEBUG
00187         DEBUGCONDUIT<<"Load plugin "<<(*it)<<endl;
00188         #endif
00189         bool on=false;
00190         if (fC) {
00191             KConfigGroupSaver cfgs(fC, settingsGroup);
00192             on=fC->readBoolEntry(*it);
00193         }
00194         addPlugin(dir.absFilePath(*it), on);
00195     }
00196 }
00197 
00198 int JPilotProxyConduitFactory::loadPlugins(KConfig*fC) {
00199     FUNCTIONSETUP;
00200     if (!fC) return -1;
00201         
00202     KConfigGroupSaver cfgs(fC, settingsGroup);
00203     
00204     QStringList pathes=fC->readListEntry(PluginPathes);
00205     for (QStringList::Iterator it = pathes.begin(); it != pathes.end(); ++it ) {
00206         addPluginPath(*it, fC);
00207     }
00208     // now load the individual plugins...
00209     QStringList plugs=fC->readListEntry(LoadedPlugins);
00210     for (QStringList::Iterator it = plugs.begin(); it != plugs.end(); ++it ) {
00211         addPlugin(*it, fC->readBoolEntry(*it));
00212     }
00213     pluginsloaded=true;
00214     
00215     QStringList loadedplugs;
00216     // TODO: Write out the plugin list to the config file.
00217 /*XXX   PluginIterator_t it(*plugins); // iterator for plugin list
00218     for ( ; it.current(); ++it ) {
00219         loadedplugs.append(it.current()->info.fullpath);
00220         // TODO:...
00221 //      QStringList pluginfo();
00222 //      fConfig->
00223     }*/
00224     
00225 }
00226 
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:47 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003