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 // Only try to load libkcm_* if it exists, otherwise KLibLoader::lastErrorMessage would say 00092 // "libkcm_foo not found" instead of the real problem with loading kcm_foo. 00093 if ( !KLibLoader::findLibrary( QCString( "libkcm_" ) + QFile::encodeName( mod.library() ) ).isEmpty() ) 00094 module = load(mod, "libkcm_%1", loader, parent, name, args ); 00095 } 00096 if (module) 00097 return module; 00098 } 00099 else 00100 kdWarning() << "Module " << mod.fileName() << " doesn't specify a library!" << endl; 00101 00102 /* 00103 * Ok, we could not load the library. 00104 * Try to run it as an executable. 00105 * This must not be done when calling from kcmshell, or you'll 00106 * have infinite recursion 00107 * (startService calls kcmshell which calls modloader which calls startService...) 00108 * 00109 */ 00110 if(withfallback) 00111 KApplication::startServiceByDesktopPath(mod.fileName(), QString::null); 00112 return 0; 00113 } 00114 00115 KCModule* KCModuleLoader::loadModule(const QString &module, QWidget *parent, 00116 const char *name, const QStringList & args) 00117 { 00118 KService::Ptr serv = KService::serviceByDesktopName(module); 00119 if (!serv) 00120 { 00121 kdError(1208) << i18n("Module %1 not found!").arg(module) << endl; 00122 return 0; 00123 } 00124 00125 KCModuleInfo mod(serv); 00126 00127 return loadModule(mod, false, parent, name, args); 00128 } 00129 00130 void KCModuleLoader::unloadModule(const KCModuleInfo &mod) 00131 { 00132 // get the library loader instance 00133 KLibLoader *loader = KLibLoader::self(); 00134 00135 // try to unload the library 00136 QString libname("libkcm_%1"); 00137 loader->unloadLibrary(QFile::encodeName(libname.arg(mod.library()))); 00138 00139 libname = "kcm_%1"; 00140 loader->unloadLibrary(QFile::encodeName(libname.arg(mod.library()))); 00141 } 00142 00143 void KCModuleLoader::showLastLoaderError(QWidget *parent) 00144 { 00145 KMessageBox::detailedError(parent, 00146 i18n("There was an error loading the module."),i18n("<qt><p>The diagnostics is:<br>%1" 00147 "<p>Possible reasons:</p><ul><li>An error occurred during your last " 00148 "KDE upgrade leaving an orphaned control module<li>You have old third party " 00149 "modules lying around.</ul><p>Check these points carefully and try to remove " 00150 "the module mentioned in the error message. If this fails, consider contacting " 00151 "your distributor or packager.</p></qt>") 00152 .arg(KLibLoader::self()->lastErrorMessage())); 00153 00154 } 00155 00156 // vim: ts=2 sw=2 et
KDE Logo
This file is part of the documentation for kutils Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 17 11:31:54 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003