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