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

Kate

katepartpluginmanager.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright 2001 Joseph Wenninger <jowenn@kde.org>
00004    Copyright 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
00005    Copyright 2007 Dominik Haumann <dhaumann kde org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "katepartpluginmanager.h"
00023 #include "katepartpluginmanager.moc"
00024 
00025 #include "kateglobal.h"
00026 
00027 #include <ktexteditor/plugin.h>
00028 #include <ktexteditor/document.h>
00029 #include <ktexteditor/view.h>
00030 #include <kconfig.h>
00031 #include <kconfiggroup.h>
00032 #include <kxmlguifactory.h>
00033 #include <kplugininfo.h>
00034 
00035 #include <kservicetypetrader.h>
00036 #include <kdebug.h>
00037 
00038 QString KatePartPluginInfo::saveName() const
00039 {
00040   QString saveName = service->property("X-KDE-PluginInfo-Name").toString();
00041 
00042   if (saveName.isEmpty())
00043     saveName = service->library();
00044   return saveName;
00045 }
00046 
00047 KatePartPluginManager::KatePartPluginManager()
00048   : QObject(),
00049     m_config(new KConfig("katepartpluginsrc", KConfig::NoGlobals))
00050 {
00051   setupPluginList ();
00052   loadConfig ();
00053 }
00054 
00055 KatePartPluginManager::~KatePartPluginManager()
00056 {
00057   writeConfig();
00058   // than unload the plugins
00059   unloadAllPlugins ();
00060   delete m_config;
00061   m_config = 0;
00062 }
00063 
00064 KatePartPluginManager *KatePartPluginManager::self()
00065 {
00066   return KateGlobal::self()->pluginManager ();
00067 }
00068 
00069 void KatePartPluginManager::setupPluginList ()
00070 {
00071   KService::List traderList = KServiceTypeTrader::self()->
00072       query("KTextEditor/Plugin",
00073             "([X-KDE-Version] >= 4.0) and ([X-KDE-Version] <= " + QString("%1.%2").arg(KDE::versionMajor()).arg(KDE::versionMinor()) + ')');
00074 
00075   foreach(const KService::Ptr &ptr, traderList)
00076   {
00077     KatePartPluginInfo info;
00078 
00079     info.load = false;
00080     info.service = ptr;
00081     info.plugin = 0L;
00082 
00083     m_pluginList.push_back (info);
00084   }
00085 }
00086 
00087 void KatePartPluginManager::addDocument(KTextEditor::Document *doc)
00088 {
00089   //kDebug() << doc;
00090   for (KatePartPluginList::iterator it = m_pluginList.begin();
00091       it != m_pluginList.end(); ++it)
00092   {
00093     if (it->load) {
00094       it->plugin->addDocument(doc);
00095     }
00096   }
00097 }
00098 
00099 void KatePartPluginManager::removeDocument(KTextEditor::Document *doc)
00100 {
00101   //kDebug() << doc;
00102   for (KatePartPluginList::iterator it = m_pluginList.begin();
00103       it != m_pluginList.end(); ++it)
00104   {
00105     if (it->load) {
00106       it->plugin->removeDocument(doc);
00107     }
00108   }
00109 }
00110 
00111 void KatePartPluginManager::addView(KTextEditor::View *view)
00112 {
00113   //kDebug() << view;
00114   for (KatePartPluginList::iterator it = m_pluginList.begin();
00115       it != m_pluginList.end(); ++it)
00116   {
00117     if (it->load) {
00118       it->plugin->addView(view);
00119     }
00120   }
00121 }
00122 
00123 void KatePartPluginManager::removeView(KTextEditor::View *view)
00124 {
00125   //kDebug() << view;
00126   for (KatePartPluginList::iterator it = m_pluginList.begin();
00127       it != m_pluginList.end(); ++it)
00128   {
00129     if (it->load) {
00130       it->plugin->removeView(view);
00131     }
00132   }
00133 }
00134 
00135 void KatePartPluginManager::loadConfig ()
00136 {
00137   // first: unload the plugins
00138   unloadAllPlugins ();
00139 
00140   KConfigGroup cg = KConfigGroup(m_config, "Kate Part Plugins");
00141 
00142   // disable all plugin if no config...
00143   foreach (const KatePartPluginInfo &plugin, m_pluginList)
00144     plugin.load = cg.readEntry (plugin.service->library(), false)
00145                || cg.readEntry (plugin.service->property("X-KDE-PluginInfo-Name").toString(), false);
00146 
00147   loadAllPlugins();
00148 }
00149 
00150 void KatePartPluginManager::writeConfig()
00151 {
00152   KConfigGroup cg = KConfigGroup( m_config, "Kate Part Plugins" );
00153   foreach(const KatePartPluginInfo &it, m_pluginList)
00154   {
00155     cg.writeEntry (it.saveName(), it.load);
00156   }
00157 }
00158 
00159 void KatePartPluginManager::loadAllPlugins ()
00160 {
00161   for (KatePartPluginList::iterator it = m_pluginList.begin();
00162       it != m_pluginList.end(); ++it)
00163   {
00164     if (it->load)
00165     {
00166       loadPlugin(*it);
00167       enablePlugin(*it);
00168     }
00169   }
00170 }
00171 
00172 void KatePartPluginManager::unloadAllPlugins ()
00173 {
00174   for (KatePartPluginList::iterator it = m_pluginList.begin();
00175        it != m_pluginList.end(); ++it)
00176   {
00177     if (it->plugin) {
00178       disablePlugin(*it);
00179       unloadPlugin(*it);
00180     }
00181   }
00182 }
00183 
00184 void KatePartPluginManager::loadPlugin (KatePartPluginInfo &item)
00185 {
00186   if (item.plugin) return;
00187 
00188   // make sure all dependencies are loaded beforehand
00189   QStringList openDependencies = KPluginInfo( item.service ).dependencies();
00190   if ( !openDependencies.empty() )
00191   {
00192     for (KatePartPluginList::iterator it = m_pluginList.begin();
00193       it != m_pluginList.end(); ++it)
00194     {
00195       if ( openDependencies.contains( it->saveName() ) )
00196       {
00197         loadPlugin( *it );
00198         openDependencies.removeAll( it->saveName() );
00199       }
00200     }
00201     Q_ASSERT( openDependencies.empty() );
00202   }
00203 
00204   item.plugin = KTextEditor::createPlugin (item.service, this);
00205   Q_ASSERT(item.plugin);
00206   item.load = (item.plugin != 0);
00207 }
00208 
00209 void KatePartPluginManager::unloadPlugin (KatePartPluginInfo &item)
00210 {
00211   if ( !item.plugin ) return;
00212 
00213   // make sure dependent plugins are unloaded beforehand
00214   for (KatePartPluginList::iterator it = m_pluginList.begin();
00215     it != m_pluginList.end(); ++it)
00216   {
00217     if ( !it->plugin ) continue;
00218 
00219     if ( KPluginInfo( it->service ).dependencies().contains( item.saveName() ) )
00220     {
00221       unloadPlugin( *it );
00222     }
00223   }
00224 
00225   delete item.plugin;
00226   item.plugin = 0L;
00227   item.load = false;
00228 }
00229 
00230 void KatePartPluginManager::enablePlugin (KatePartPluginInfo &item)
00231 {
00232   // plugin around at all?
00233   if (!item.plugin || !item.load)
00234     return;
00235 
00236   // register docs and views
00237   foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
00238     if (!doc)
00239       continue;
00240 
00241     foreach (KTextEditor::View *view, doc->views()) {
00242       if (!view)
00243         continue;
00244 
00245       KXMLGUIFactory *viewFactory = view->factory();
00246       if (viewFactory)
00247         viewFactory->removeClient(view);
00248 
00249       item.plugin->addView(view);
00250 
00251       if (viewFactory)
00252         viewFactory->addClient(view);
00253     }
00254   }
00255 }
00256 
00257 void KatePartPluginManager::disablePlugin (KatePartPluginInfo &item)
00258 {
00259   // plugin around at all?
00260   if (!item.plugin || !item.load)
00261     return;
00262 
00263   // de-register docs and views
00264   foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
00265     if (!doc)
00266       continue;
00267 
00268     foreach (KTextEditor::View *view, doc->views()) {
00269       if (!view)
00270         continue;
00271 
00272       KXMLGUIFactory *viewFactory = view->factory();
00273       if (viewFactory)
00274         viewFactory->removeClient(view);
00275 
00276       item.plugin->removeView(view);
00277 
00278       if (viewFactory)
00279         viewFactory->addClient(view);
00280     }
00281   }
00282 }
00283 
00284 // kate: space-indent on; indent-width 2; replace-tabs on;

Kate

Skip menu "Kate"
  • 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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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