KRename – powerful batch renamer
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.

182 lines
5.3KB

  1. /***************************************************************************
  2. pluginloader.cpp - description
  3. -------------------
  4. begin : Sun Dec 30 2001
  5. copyright : (C) 2001 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. /*
  17. * Parts of this code are copied from
  18. * Franz Schmid's <Franz.Schmid@altmuehlnet.de>
  19. * Scribus.
  20. */
  21. // Own includes
  22. #include "pluginloader.h"
  23. #include "fileplugin.h"
  24. #include "datetime.h"
  25. #include "mydirplugin.h"
  26. #include "permission.h"
  27. #include "pictureplugin.h"
  28. #include "commandplugin.h"
  29. #include "dateplugin.h"
  30. #include "encodingplugin.h"
  31. #include "translitplugin.h"
  32. // QT includes
  33. #include <tqregexp.h>
  34. #include <tqstringlist.h>
  35. // KDE includes
  36. #include <tdeapplication.h>
  37. #include <kservice.h>
  38. /* use a prime number here */
  39. #define DICT_SIZE 43
  40. #define BRACKET_CACHE_COUNT 19
  41. PluginLoader::PluginLoader()
  42. {
  43. libs.setAutoDelete( true );
  44. m_loaded = false;
  45. m_file = false;
  46. }
  47. PluginLoader::~PluginLoader()
  48. { }
  49. PluginLoader* PluginLoader::instance()
  50. {
  51. if( !m_plugin )
  52. m_plugin = new PluginLoader();
  53. return m_plugin;
  54. }
  55. PluginLoader* PluginLoader::m_plugin = NULL;
  56. void PluginLoader::loadPlugins( bool fileplugins )
  57. {
  58. int i;
  59. // make sure all plugins with display
  60. // get deleted and the GUI is rebuild
  61. if( m_loaded )
  62. {
  63. for( i=0;i<NUM_INTERNAL_PLUGINS;i++ )
  64. {
  65. for( unsigned int z=0;z<libs.count();z++ )
  66. if( libs.at(z)->plugin == m_internal_plugins[i] )
  67. {
  68. libs.remove( z );
  69. break;
  70. }
  71. }
  72. }
  73. m_loaded = true;
  74. m_internal_plugins[0] = new CommandPlugin();
  75. m_internal_plugins[1] = new MyPermPlugin();
  76. m_internal_plugins[2] = new MyDatePlugin();
  77. m_internal_plugins[3] = new MyDirPlugin();
  78. m_internal_plugins[4] = new PicturePlugin();
  79. m_internal_plugins[5] = new EncodingPlugin();
  80. m_internal_plugins[6] = new DatePlugin();
  81. m_internal_plugins[7] = new TranslitPlugin();
  82. for( i=0;i<NUM_INTERNAL_PLUGINS;i++ )
  83. addPlugin( m_internal_plugins[i] );
  84. if( fileplugins && !m_file )
  85. loadFilePlugins();
  86. m_bracket_map.clear();
  87. TQPtrListIterator<PluginLoader::PluginLibrary> it( libs );
  88. for( ; it.current(); ++it ) {
  89. if( (*it)->plugin->type() == TYPE_BRACKET ) {
  90. const TQStringList list = (*it)->plugin->getKeys();
  91. for( unsigned int i = 0; i < list.count(); i++ )
  92. m_bracket_map.insert( list[i].lower(), (*it)->plugin );
  93. }
  94. }
  95. }
  96. void PluginLoader::clearCache()
  97. {
  98. TQPtrListIterator<PluginLoader::PluginLibrary> it( libs );
  99. for( ; it.current(); ++it )
  100. (*it)->plugin->clearCache();
  101. }
  102. Plugin* PluginLoader::findPlugin( const TQString & token )
  103. {
  104. // Optimize a little bit for speed and convert only once to lower()
  105. TQString lower = token.lower();
  106. if( m_bracket_cache.contains( lower ) )
  107. return m_bracket_cache[lower];
  108. /* The new version is slower than the old one :-(
  109. */
  110. TQMap<TQString,Plugin*>::Iterator it;
  111. for ( it = m_bracket_map.begin(); it != m_bracket_map.end(); ++it )
  112. {
  113. if( TQRegExp( it.key() ).exactMatch( lower ) )
  114. {
  115. m_bracket_cache.insert( lower, it.data(), true );
  116. if( m_bracket_cache.count() > BRACKET_CACHE_COUNT )
  117. m_bracket_cache.remove( m_bracket_cache.begin() );
  118. return it.data();
  119. }
  120. }
  121. // add typos to the cache, too:
  122. // So that we find immediately that this key is not supported.
  123. m_bracket_cache.insert( lower, NULL, true );
  124. if( m_bracket_cache.count() > BRACKET_CACHE_COUNT )
  125. m_bracket_cache.remove( m_bracket_cache.begin() );
  126. return NULL;
  127. }
  128. void PluginLoader::addPlugin( Plugin* plugin )
  129. {
  130. PluginLibrary* listitem=new PluginLibrary;
  131. listitem->plugin = plugin;
  132. listitem->check = NULL;
  133. listitem->usePlugin = plugin->alwaysUsed();
  134. libs.append( listitem );
  135. }
  136. void PluginLoader::loadFilePlugins()
  137. {
  138. KService::List list = KService::allServices();
  139. for( unsigned int i = 0; i < list.count(); i++ ) {
  140. KService* s = (KService*)list[i];
  141. if( !s->terminal() && s->type() == "Service" && s->hasServiceType( "KFilePlugin" ) ) {
  142. FilePlugin* tdefileplugin = new FilePlugin( s );
  143. if( tdefileplugin->isValid() ) {
  144. addPlugin( tdefileplugin );
  145. kapp->processEvents();
  146. } else
  147. delete tdefileplugin;
  148. }
  149. }
  150. m_file = true;
  151. }
  152. #include "pluginloader.moc"