• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kutils
 

kutils

  • kutils
kcmoduleloader.cpp
1 /*
2  Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
3  Copyright (c) 2000 Matthias Elter <elter@kde.org>
4  Copyright (c) 2003,2004 Matthias Kretz <kretz@kde.org>
5  Copyright (c) 2004 Frans Englich <frans.englich@telia.com>
6 
7  This file is part of the KDE project
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License version 2, as published by the Free Software Foundation.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include <tqfile.h>
25 #include <tqlabel.h>
26 #include <tqlayout.h>
27 
28 #include <kapplication.h>
29 #include <kdebug.h>
30 #include <klocale.h>
31 #include <kmessagebox.h>
32 #include <kparts/componentfactory.h>
33 
34 #include "kcmoduleloader.h"
35 
36 
37 /***************************************************************/
42 class KCMError : public KCModule
43 {
44  public:
45  KCMError( const TQString& msg, const TQString& details, TQWidget* parent )
46  : KCModule( parent, "KCMError" )
47  {
48  TQVBoxLayout* topLayout = new TQVBoxLayout( this );
49  topLayout->addWidget( new TQLabel( msg, this ) );
50  topLayout->addWidget( new TQLabel( details, this ) );
51  }
52 };
53 /***************************************************************/
54 
55 
56 
57 
58 KCModule* KCModuleLoader::load(const KCModuleInfo &mod, const TQString &libname,
59  KLibLoader *loader, ErrorReporting report, TQWidget * parent,
60  const char * name, const TQStringList & args )
61 {
62  // attempt to load modules with ComponentFactory, only if the symbol init_<lib> exists
63  // (this is because some modules, e.g. kcmkio with multiple modules in the library,
64  // cannot be ported to KGenericFactory)
65  KLibrary *lib = loader->library(TQFile::encodeName(libname.arg(mod.library())));
66  if (lib) {
67  TQString initSym("init_");
68  initSym += libname.arg(mod.library());
69 
70  if ( lib->hasSymbol(TQFile::encodeName(initSym)) )
71  {
72  KLibFactory *factory = lib->factory();
73  if ( factory )
74  {
75  KCModule *module = KParts::ComponentFactory::createInstanceFromFactory<KCModule>( factory, TQT_TQOBJECT(parent), name ? name : mod.handle().latin1(), args );
76  if (module)
77  return module;
78  }
79  // else do a fallback
80  kdDebug(1208) << "Unable to load module using ComponentFactory. Falling back to old loader." << endl;
81  }
82 
83  // get the create_ function
84  TQString factory("create_%1");
85  void *create = lib->symbol(TQFile::encodeName(factory.arg(mod.handle())));
86 
87  if (create)
88  {
89  // create the module
90  KCModule* (*func)(TQWidget *, const char *);
91  func = (KCModule* (*)(TQWidget *, const char *)) create;
92  return func( parent, name ? name : mod.handle().latin1() );
93  }
94  else
95  {
96  TQString libFileName = lib->fileName();
97  lib->unload();
98  return reportError( report, i18n("<qt>There was an error when loading the module '%1'.<br><br>"
99  "The desktop file (%2) as well as the library (%3) was found but "
100  "yet the module could not be loaded properly. Most likely "
101  "the factory declaration was wrong, or the "
102  "create_* function was missing.</qt>")
103  .arg( mod.moduleName() )
104  .arg( mod.fileName() )
105  .arg( libFileName ),
106  TQString::null, parent );
107  }
108 
109  lib->unload();
110  }
111  return reportError( report, i18n("The specified library %1 could not be found.")
112  .arg( mod.library() ), TQString::null, parent );
113  return 0;
114 }
115 
116 KCModule* KCModuleLoader::loadModule(const KCModuleInfo &mod, bool withfallback, TQWidget * parent, const char * name, const TQStringList & args )
117 {
118  return loadModule( mod, None, withfallback, parent, name, args );
119 }
120 
121 KCModule* KCModuleLoader::loadModule(const KCModuleInfo &mod, ErrorReporting report, bool withfallback, TQWidget * parent, const char * name, const TQStringList & args )
122 {
123  /*
124  * Simple libraries as modules are the easiest case:
125  * We just have to load the library and get the module
126  * from the factory.
127  */
128 
129  if ( !mod.service() )
130  {
131  if ( mod.moduleName() == "kcmlisa" || mod.moduleName() == "kcmkiolan" )
132  {
133  return reportError( report,
134  i18n("The module %1 could not be found.")
135  .arg( mod.moduleName() ),
136  i18n("<qt><p>The Lisa and lan:/ ioslave modules "
137  "are not installed by default in Kubuntu, because they are obsolete "
138  "and replaced by zeroconf.<br> If you still wish to use them, you "
139  "should install the lisa package from the Universe repository.</p></qt>"),
140  parent );
141  } else {
142  return reportError( report,
143  i18n("The module %1 could not be found.")
144  .arg( mod.moduleName() ),
145  i18n("<qt><p>The diagnostics is:<br>The desktop file %1 could not be found.</p></qt>").arg(mod.fileName()),
146  parent );
147  }
148  }
149 
150  if (!mod.library().isEmpty())
151  {
152  // get the library loader instance
153 
154  KLibLoader *loader = KLibLoader::self();
155 
156  KCModule *module = load(mod, "kcm_%1", loader, report, parent, name, args );
157  /*
158  * Only try to load libkcm_* if it exists, otherwise KLibLoader::lastErrorMessage would say
159  * "libkcm_foo not found" instead of the real problem with loading kcm_foo.
160  */
161  if (!KLibLoader::findLibrary( TQCString( "libkcm_" ) + TQFile::encodeName( mod.library() ) ).isEmpty() )
162  module = load(mod, "libkcm_%1", loader, report, parent, name, args );
163  if (module)
164  return module;
165  return reportError( report,
166  i18n("The module %1 could not be loaded.")
167  .arg( mod.moduleName() ), TQString::null, parent );
168  }
169 
170  /*
171  * Ok, we could not load the library.
172  * Try to run it as an executable.
173  * This must not be done when calling from kcmshell, or you'll
174  * have infinite recursion
175  * (startService calls kcmshell which calls modloader which calls startService...)
176  *
177  */
178  if(withfallback)
179  {
180  KApplication::startServiceByDesktopPath(mod.fileName(), TQString::null);
181  }
182  else
183  {
184  return reportError( report,
185  i18n("The module %1 is not a valid configuration module.")
186  .arg( mod.moduleName() ), i18n("<qt><p>The diagnostics is:<br>The desktop file %1 does not specify a library.</qt>").arg(mod.fileName()), parent );
187  }
188 
189  return 0;
190 }
191 
192 KCModule* KCModuleLoader::loadModule(const TQString &module, TQWidget *parent,
193  const char *name, const TQStringList & args)
194 {
195  return loadModule(KCModuleInfo(module), None, false, parent, name, args);
196 }
197 
198 KCModule* KCModuleLoader::loadModule(const TQString &module, ErrorReporting
199  report, TQWidget *parent, const char *name, const TQStringList & args)
200 {
201  return loadModule(KCModuleInfo(module), report, false, parent, name, args);
202 }
203 
204 void KCModuleLoader::unloadModule(const KCModuleInfo &mod)
205 {
206  // get the library loader instance
207  KLibLoader *loader = KLibLoader::self();
208 
209  // try to unload the library
210  TQString libname("libkcm_%1");
211  loader->unloadLibrary(TQFile::encodeName(libname.arg(mod.library())));
212 
213  libname = "kcm_%1";
214  loader->unloadLibrary(TQFile::encodeName(libname.arg(mod.library())));
215 }
216 
217 void KCModuleLoader::showLastLoaderError(TQWidget *parent)
218 {
219  KMessageBox::detailedError(parent,
220  i18n("There was an error loading the module."),i18n("<qt><p>The diagnostics is:<br>%1"
221  "<p>Possible reasons:</p><ul><li>An error occurred during your last "
222  "KDE upgrade leaving an orphaned control module<li>You have old third party "
223  "modules lying around.</ul><p>Check these points carefully and try to remove "
224  "the module mentioned in the error message. If this fails, consider contacting "
225  "your distributor or packager.</p></qt>")
226  .arg(KLibLoader::self()->lastErrorMessage()));
227 
228 }
229 
230 bool KCModuleLoader::testModule( const TQString& module )
231 {
232  return testModule( KCModuleInfo( module ) );
233 }
234 
235 bool KCModuleLoader::testModule( const KCModuleInfo& module )
236 {
237  if (!module.service())
238  {
239  kdDebug(1208) << "Module '" << module.fileName() << "' not found." << endl;
240  return true;
241  }
242 
243  bool doLoad = module.service()->property( "X-KDE-Test-Module", TQVariant::Bool ).toBool();
244  if( !doLoad )
245  {
246  return true;
247  }
248  else
249  {
256  KLibLoader* loader = KLibLoader::self();
257  KLibrary* library = loader->library( TQFile::encodeName((TQString("kcm_%1").arg(module.library()))) );
258  if( library )
259  {
260  void *test_func = library->symbol( TQString(TQString("test_%1").arg(module.factoryName())).utf8() );
261  if( test_func )
262  {
263  bool (*func)() = (bool(*)())test_func;
264  if( func() )
265  {
266  return true;
267  }
268  else
269  {
270  return false;
271  }
272  }
273  else
274  {
275  kdDebug(1208) << "The test function for module '" << module.fileName() << "' could not be found." << endl;
276  return true;
277  }
278  }
279  kdDebug(1208) << "The library '" << module.library() << "' could not be found." << endl;
280  return true;
281  }
282 }
283 
284 KCModule* KCModuleLoader::reportError( ErrorReporting report, const TQString & text,
285  TQString details, TQWidget * parent )
286 {
287  if( details.isNull() )
288  details = i18n("<qt><p>The diagnostics is:<br>%1"
289  "<p>Possible reasons:</p><ul><li>An error occurred during your last "
290  "KDE upgrade leaving an orphaned control module<li>You have old third party "
291  "modules lying around.</ul><p>Check these points carefully and try to remove "
292  "the module mentioned in the error message. If this fails, consider contacting "
293  "your distributor or packager.</p></qt>").arg(KLibLoader::self()->lastErrorMessage());
294  if( report & Dialog )
295  KMessageBox::detailedError( parent, text, details );
296  if( report & Inline )
297  return new KCMError( text, details, parent );
298  return 0;
299 }
300 
301 // vim: ts=2 sw=2 et
302 

kutils

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

kutils

Skip menu "kutils"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kutils by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |