TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
10KB

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