kutils Library API Documentation

kcmoduleloader.cpp

00001 /*
00002   Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
00003   Copyright (c) 2000 Matthias Elter <elter@kde.org>
00004 
00005   This file is part of the KDE project
00006 
00007   This library is free software; you can redistribute it and/or
00008   modify it under the terms of the GNU Library General Public
00009   License version 2, as published by the Free Software Foundation.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019   Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qfile.h>
00023 
00024 #include <kapplication.h>
00025 #include <kmessagebox.h>
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 #include <kparts/componentfactory.h>
00029 
00030 #include "kcmoduleloader.h"
00031 
00032 KCModule* KCModuleLoader::load(const KCModuleInfo &mod, const QString &libname, KLibLoader *loader, QWidget * parent, const char * name, const QStringList & args )
00033 {
00034   // attempt to load modules with ComponentFactory, only if the symbol init_<lib> exists
00035   // (this is because some modules, e.g. kcmkio with multiple modules in the library,
00036   // cannot be ported to KGenericFactory)
00037   KLibrary *lib = loader->library(QFile::encodeName(libname.arg(mod.library())));
00038   if (lib) {
00039     QString initSym("init_");
00040     initSym += libname.arg(mod.library());
00041 
00042     if ( lib->hasSymbol(QFile::encodeName(initSym)) )
00043     {
00044       // Reuse "lib" instead of letting createInstanceFromLibrary recreate it
00045       //KCModule *module = KParts::ComponentFactory::createInstanceFromLibrary<KCModule>(QFile::encodeName(libname.arg(mod.library())));
00046       KLibFactory *factory = lib->factory();
00047       if ( factory )
00048       {
00049         KCModule *module = KParts::ComponentFactory::createInstanceFromFactory<KCModule>( factory, parent, name ? name : mod.handle().latin1(), args );
00050         if (module)
00051           return module;
00052       }
00053       // else do a fallback
00054       kdDebug(1208) << "Unable to load module using ComponentFactory! Falling back to old loader." << endl;
00055     }
00056 
00057     // get the create_ function
00058     QString factory("create_%1");
00059     void *create = lib->symbol(QFile::encodeName(factory.arg(mod.handle())));
00060 
00061     if (create)
00062     {
00063       // create the module
00064       KCModule* (*func)(QWidget *, const char *);
00065       func = (KCModule* (*)(QWidget *, const char *)) create;
00066       return  func( parent, name ? name : mod.handle().latin1() );
00067     }
00068 
00069     lib->unload();
00070   }
00071   return 0;
00072 }
00073 
00074 
00075 KCModule* KCModuleLoader::loadModule(const KCModuleInfo &mod, bool withfallback, QWidget * parent, const char * name, const QStringList & args )
00076 {
00077   /*
00078    * Simple libraries as modules are the easiest case:
00079    *  We just have to load the library and get the module
00080    *  from the factory.
00081    */
00082 
00083   if (!mod.library().isEmpty())
00084   {
00085     // get the library loader instance
00086 
00087     KLibLoader *loader = KLibLoader::self();
00088 
00089     KCModule *module = load(mod, "kcm_%1", loader, parent, name, args );
00090     if (!module)
00091       module = load(mod, "libkcm_%1", loader, parent, name, args );
00092     if (module)
00093       return module;
00094   }
00095   else
00096     kdWarning() << "Module " << mod.fileName() << " doesn't specify a library!" << endl;
00097 
00098   /*
00099    * Ok, we could not load the library.
00100    * Try to run it as an executable.
00101    * This must not be done when calling from kcmshell, or you'll
00102    * have infinite recursion
00103    * (startService calls kcmshell which calls modloader which calls startService...)
00104    *
00105    */
00106   if(withfallback)
00107     KApplication::startServiceByDesktopPath(mod.fileName(), QString::null);
00108   return 0;
00109 }
00110 
00111 KCModule* KCModuleLoader::loadModule(const QString &module, QWidget *parent,
00112       const char *name, const QStringList & args)
00113 {
00114     KService::Ptr serv = KService::serviceByDesktopName(module);
00115     if (!serv)
00116     {
00117         kdError(1208) << i18n("Module %1 not found!").arg(module) << endl;
00118         return 0;
00119     }
00120 
00121     KCModuleInfo mod(serv);
00122 
00123     return loadModule(mod, false, parent, name, args);
00124 }
00125 
00126 void KCModuleLoader::unloadModule(const KCModuleInfo &mod)
00127 {
00128   // get the library loader instance
00129   KLibLoader *loader = KLibLoader::self();
00130 
00131   // try to unload the library
00132   QString libname("libkcm_%1");
00133   loader->unloadLibrary(QFile::encodeName(libname.arg(mod.library())));
00134 
00135   libname = "kcm_%1";
00136   loader->unloadLibrary(QFile::encodeName(libname.arg(mod.library())));
00137 }
00138 
00139 void KCModuleLoader::showLastLoaderError(QWidget *parent)
00140 {
00141   KMessageBox::detailedError(parent,
00142       i18n("There was an error loading the module."),i18n("<qt><p>The diagnostics is:<br>%1"
00143         "<p>Possible reasons:</p><ul><li>An error occurred during your last "
00144         "KDE upgrade leaving an orphaned control module<li>You have old third party "
00145         "modules lying around.</ul><p>Check these points carefully and try to remove "
00146         "the module mentioned in the error message. If this fails, consider contacting "
00147         "your distributor or packager.</p></qt>")
00148       .arg(KLibLoader::self()->lastErrorMessage()));
00149 
00150 }
00151 
00152 // vim: ts=2 sw=2 et
KDE Logo
This file is part of the documentation for kutils Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 12 09:08:07 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003