• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KUtils

kcmoduleloader.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
00003   Copyright (c) 2000 Matthias Elter <elter@kde.org>
00004   Copyright (c) 2003,2004,2006 Matthias Kretz <kretz@kde.org>
00005   Copyright (c) 2004 Frans Englich <frans.englich@telia.com>
00006 
00007   This file is part of the KDE project
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Library General Public
00011   License version 2, as published by the Free Software Foundation.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Library General Public License for more details.
00017 
00018   You should have received a copy of the GNU Library General Public License
00019   along with this library; see the file COPYING.LIB.  If not, write to
00020   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021   Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "kcmoduleloader.h"
00025 
00026 #include <QtCore/QFile>
00027 #include <QtGui/QLabel>
00028 #include <QtGui/QLayout>
00029 
00030 #include <kapplication.h>
00031 #include <kpluginloader.h>
00032 #include <kdebug.h>
00033 #include <klocale.h>
00034 #include <kmessagebox.h>
00035 #include <klibloader.h>
00036 
00037 using namespace KCModuleLoader;
00038 
00039 /***************************************************************/
00044 class KCMError : public KCModule
00045 {
00046     public:
00047         KCMError( const QString& msg, const QString& details, QWidget* parent )
00048             : KCModule( KGlobal::mainComponent(), parent )
00049         {
00050             QVBoxLayout* topLayout = new QVBoxLayout( this );
00051       QLabel *lab = new QLabel( msg, this );
00052       lab->setWordWrap(true);
00053             topLayout->addWidget( lab );
00054       lab = new QLabel(details, this );
00055       lab->setWordWrap(true);
00056             topLayout->addWidget( lab );
00057         }
00058 };
00059 /***************************************************************/
00060 
00061 KCModule *KCModuleLoader::loadModule(const QString &module, ErrorReporting report, QWidget *parent, const QStringList &args)
00062 {
00063   return loadModule( KCModuleInfo( module ), report, parent, args );
00064 }
00065 
00066 KCModule* KCModuleLoader::loadModule(const KCModuleInfo& mod, ErrorReporting report, QWidget* parent, const QStringList& args )
00067 {
00068   /*
00069    * Simple libraries as modules are the easiest case:
00070    *  We just have to load the library and get the module
00071    *  from the factory.
00072    */
00073 
00074   if ( !mod.service() )
00075     return reportError( report,
00076         i18n("The module %1 could not be found.",
00077           mod.moduleName() ), i18n("<qt><p>The diagnostics is:<br />The desktop file %1 could not be found.</p></qt>", mod.fileName()), parent );
00078   if( mod.service()->noDisplay() )
00079     return reportError( report, i18n( "The module %1 is disabled.", mod.moduleName() ),
00080         i18n( "<qt><p>Either the hardware/software the module configures is not available or the module has been disabled by the administrator.</p></qt>" ),
00081         parent );
00082 
00083   if (!mod.library().isEmpty())
00084   {
00085         QString error;
00086         QVariantList args2;
00087         foreach (const QString &arg, args) {
00088             args2 << arg;
00089         }
00090         KCModule *module = KService::createInstance<KCModule>(mod.service(), parent, args2, &error);
00091         if (module) {
00092             return module;
00093         }
00094         // might be using K_EXPORT_COMPONENT_FACTORY
00095         int error2 = 0;
00096         module = KService::createInstance<KCModule>(mod.service(), parent, args, &error2);
00097         if (module) {
00098             kWarning(1208) << "This module still uses K_EXPORT_COMPONENT_FACTORY. Please port it to use KPluginFactory and K_EXPORT_PLUGIN.";
00099             return module;
00100         }
00101         error += KLibLoader::errorString(error2);
00102 //#ifndef NDEBUG
00103         {
00104             // get the create_ function
00105             KLibrary *lib = KLibLoader::self()->library(mod.library());
00106             if (lib) {
00107                 KCModule *(*create)(QWidget *, const char *);
00108                 QByteArray factorymethod("create_");
00109                 factorymethod += mod.handle().toLatin1();
00110                 create = reinterpret_cast<KCModule *(*)(QWidget *, const char*)>(lib->resolveFunction(factorymethod));
00111                 if (create) {
00112                     return create(parent, mod.handle().toLatin1());
00113                     kFatal(1208) << "This module still uses a custom factory method (" << factorymethod << "). This is not supported anymore. Please fix the module.";
00114                 } else {
00115                     kWarning(1208) << "This module has no valid entry symbol at all. The reason could be that it's still using K_EXPORT_COMPONENT_FACTORY with a custom X-KDE-FactoryName which is not supported anymore";
00116                 }
00117                 lib->unload();
00118             }
00119         }
00120 //#endif // NDEBUG
00121         return reportError(report, error, QString(), parent);
00122   }
00123 
00124   /*
00125    * Ok, we could not load the library.
00126    * Try to run it as an executable.
00127    * This must not be done when calling from kcmshell, or you'll
00128    * have infinite recursion
00129    * (startService calls kcmshell which calls modloader which calls startService...)
00130    *
00131    */
00132   return reportError( report,
00133       i18n("The module %1 is not a valid configuration module.", mod.moduleName() ),
00134       i18n("<qt>The diagnostics is:<br />The desktop file %1 does not specify a library.</qt>", mod.fileName()), parent );
00135 }
00136 
00137 
00138 void KCModuleLoader::unloadModule(const KCModuleInfo &mod)
00139 {
00140   // get the library loader instance
00141   KLibLoader *loader = KLibLoader::self();
00142 
00143   // try to unload the library
00144   QString libname("lib%1");
00145   loader->unloadLibrary(libname.arg(mod.library()));
00146 
00147   loader->unloadLibrary(mod.library());
00148 }
00149 
00150 void KCModuleLoader::showLastLoaderError(QWidget *parent)
00151 {
00152   KMessageBox::detailedError(parent,
00153       i18n("There was an error loading the module."),i18n("<qt>The diagnostics is:<br />%1"
00154         "<p>Possible reasons:</p><ul><li>An error occurred during your last "
00155         "KDE upgrade leaving an orphaned control module</li><li>You have old third party "
00156         "modules lying around.</li></ul><p>Check these points carefully and try to remove "
00157         "the module mentioned in the error message. If this fails, consider contacting "
00158         "your distributor or packager.</p></qt>",
00159        KLibLoader::self()->lastErrorMessage()));
00160 
00161 }
00162 
00163 KCModule* KCModuleLoader::reportError( ErrorReporting report, const QString & text,
00164         const QString &details, QWidget * parent )
00165 {
00166     QString realDetails = details;
00167     if (realDetails.isNull()) {
00168         realDetails = i18n("<qt><p>Possible reasons:<ul><li>An error occurred during your last "
00169                 "KDE upgrade leaving an orphaned control module</li><li>You have old third party "
00170                 "modules lying around.</li></ul></p><p>Check these points carefully and try to remove "
00171                 "the module mentioned in the error message. If this fails, consider contacting "
00172                 "your distributor or packager.</p></qt>");
00173     }
00174     if (report & KCModuleLoader::Dialog) {
00175         KMessageBox::detailedError(parent, text, realDetails);
00176     }
00177     if (report & KCModuleLoader::Inline) {
00178         return new KCMError(text, realDetails, parent);
00179     }
00180     return 0;
00181 }
00182 
00183 // vim: ts=4

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal