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

tdeutils

  • tdeutils
tdecmoduleloader.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 <tdeapplication.h>
29 #include <kdebug.h>
30 #include <tdelocale.h>
31 #include <tdemessagebox.h>
32 #include <tdeparts/componentfactory.h>
33 
34 #include "tdecmoduleloader.h"
35 
36 
37 /***************************************************************/
42 class KCMError : public TDECModule
43 {
44  public:
45  KCMError( const TQString& msg, const TQString& details, TQWidget* parent )
46  : TDECModule( 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 TDECModule* TDECModuleLoader::load(const TDECModuleInfo &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  TDECModule *module = KParts::ComponentFactory::createInstanceFromFactory<TDECModule>( 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  TDECModule* (*func)(TQWidget *, const char *);
91  func = (TDECModule* (*)(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 TDECModule* TDECModuleLoader::loadModule(const TDECModuleInfo &mod, bool withfallback, TQWidget * parent, const char * name, const TQStringList & args )
117 {
118  return loadModule( mod, None, withfallback, parent, name, args );
119 }
120 
121 TDECModule* TDECModuleLoader::loadModule(const TDECModuleInfo &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  TDECModule *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 tdecmshell, or you'll
174  * have infinite recursion
175  * (startService calls tdecmshell which calls modloader which calls startService...)
176  *
177  */
178  if(withfallback)
179  {
180  TDEApplication::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 TDECModule* TDECModuleLoader::loadModule(const TQString &module, TQWidget *parent,
193  const char *name, const TQStringList & args)
194 {
195  return loadModule(TDECModuleInfo(module), None, false, parent, name, args);
196 }
197 
198 TDECModule* TDECModuleLoader::loadModule(const TQString &module, ErrorReporting
199  report, TQWidget *parent, const char *name, const TQStringList & args)
200 {
201  return loadModule(TDECModuleInfo(module), report, false, parent, name, args);
202 }
203 
204 void TDECModuleLoader::unloadModule(const TDECModuleInfo &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 TDECModuleLoader::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  "TDE 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 TDECModuleLoader::testModule( const TQString& module )
231 {
232  return testModule( TDECModuleInfo( module ) );
233 }
234 
235 bool TDECModuleLoader::testModule( const TDECModuleInfo& 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-TDE-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 TDECModule* TDECModuleLoader::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  "TDE 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 }
KLibFactory
TDECModuleLoader::showLastLoaderError
static void showLastLoaderError(TQWidget *parent) KDE_DEPRECATED
Display a message box explaining an error occured and possible reasons to why.
Definition: tdecmoduleloader.cpp:217
TDECModuleInfo
A class that provides information about a TDECModule.
Definition: tdecmoduleinfo.h:49
TDECModuleLoader::Inline
the error report is shown instead of the TDECModule that should have * been loaded ...
Definition: tdecmoduleloader.h:93
TDECModuleLoader::testModule
static bool testModule(const TQString &module)
Checks whether an TDECModule should be shown by running its test function.
Definition: tdecmoduleloader.cpp:230
TDECModuleLoader::None
no error reporting is done
Definition: tdecmoduleloader.h:88
TDECModuleLoader::loadModule
static TDECModule * loadModule(const TDECModuleInfo &module, bool withFallback=true, TQWidget *parent=0, const char *name=0, const TQStringList &args=TQStringList()) KDE_DEPRECATED
Loads a TDECModule.
Definition: tdecmoduleloader.cpp:116
kdDebug
kdbgstream kdDebug(int area=0)
KMessageBox::detailedError
static void detailedError(TQWidget *parent, const TQString &text, const TQString &details, const TQString &caption=TQString::null, int options=Notify)
KLibrary::fileName
TQString fileName() const
TDECModuleLoader::reportError
static TDECModule * reportError(ErrorReporting report, const TQString &text, TQString details, TQWidget *parent)
Returns a TDECModule containing the messages report and text.
Definition: tdecmoduleloader.cpp:284
TDECModuleInfo::factoryName
TQString factoryName() const
Returns the module's factory name, if it's set.
Definition: tdecmoduleinfo.cpp:95
KLibLoader::unloadLibrary
virtual void unloadLibrary(const char *libname)
tdelocale.h
TDECModuleInfo::library
TQString library() const
Definition: tdecmoduleinfo.h:157
KLibrary::unload
void unload() const
TDECModuleInfo::fileName
TQString fileName() const
Definition: tdecmoduleinfo.h:113
KLibrary
TDECModuleInfo::service
KService::Ptr service() const
Definition: tdecmoduleinfo.h:137
KLibrary::factory
KLibFactory * factory()
KLibrary::hasSymbol
bool hasSymbol(const char *name) const
KLibLoader::library
virtual KLibrary * library(const char *libname)
TDECModuleInfo::moduleName
TQString moduleName() const
Definition: tdecmoduleinfo.h:131
TDECModuleLoader::Dialog
shows a dialog with the error report
Definition: tdecmoduleloader.h:97
KLibLoader::self
static KLibLoader * self()
TDECModuleInfo::handle
TQString handle() const
Definition: tdecmoduleinfo.cpp:192
KLibLoader
KLibLoader::findLibrary
static TQString findLibrary(const char *name, const TDEInstance *instance=TDEGlobal::instance())
TDECModule
TDECModuleLoader::ErrorReporting
ErrorReporting
Determines the way errors are reported.
Definition: tdecmoduleloader.h:84
KStdAction::create
TDEAction * create(StdAction id, const char *name, const TQObject *recvr, const char *slot, TDEActionCollection *parent)
KLibrary::symbol
void * symbol(const char *name) const
endl
kndbgstream & endl(kndbgstream &s)
TDECModuleLoader::unloadModule
static void unloadModule(const TDECModuleInfo &mod)
Unloads the module's library.
Definition: tdecmoduleloader.cpp:204

tdeutils

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

tdeutils

Skip menu "tdeutils"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeutils by doxygen 1.8.8
This website is maintained by Timothy Pearson.