kcmoduleloader.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
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   
00035   
00036   
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       
00045       
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       
00054       kdDebug(1208) << "Unable to load module using ComponentFactory! Falling back to old loader." << endl;
00055     }
00056 
00057     
00058     QString factory("create_%1");
00059     void *create = lib->symbol(QFile::encodeName(factory.arg(mod.handle())));
00060 
00061     if (create)
00062     {
00063       
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 
00079 
00080 
00081 
00082 
00083   if (!mod.library().isEmpty())
00084   {
00085     
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 
00100 
00101 
00102 
00103 
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   
00129   KLibLoader *loader = KLibLoader::self();
00130 
00131   
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 
 
This file is part of the documentation for kutils Library Version 3.2.0.