Kate
katepartpluginmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 KatePartPluginInfo::KatePartPluginInfo(KService::Ptr service)
00039 : m_pluginInfo(service)
00040 {
00041 }
00042
00043 QString KatePartPluginInfo::saveName() const
00044 {
00045 QString saveName = m_pluginInfo.pluginName();
00046 if (saveName.isEmpty())
00047 saveName = service()->library();
00048 return saveName;
00049 }
00050
00051 KatePartPluginManager::KatePartPluginManager()
00052 : QObject(),
00053 m_config(new KConfig("katepartpluginsrc", KConfig::NoGlobals))
00054 {
00055 setupPluginList ();
00056 loadConfig ();
00057 }
00058
00059 KatePartPluginManager::~KatePartPluginManager()
00060 {
00061 writeConfig();
00062
00063 unloadAllPlugins ();
00064 delete m_config;
00065 m_config = 0;
00066 }
00067
00068 KatePartPluginManager *KatePartPluginManager::self()
00069 {
00070 return KateGlobal::self()->pluginManager ();
00071 }
00072
00073 void KatePartPluginManager::setupPluginList ()
00074 {
00075 KService::List traderList = KServiceTypeTrader::self()->
00076 query("KTextEditor/Plugin",
00077 "([X-KDE-Version] >= 4.0) and ([X-KDE-Version] <= " + QString("%1.%2").arg(KDE::versionMajor()).arg(KDE::versionMinor()) + ')');
00078
00079 foreach(const KService::Ptr &ptr, traderList)
00080 {
00081 KatePartPluginInfo info(ptr);
00082
00083 info.load = false;
00084 info.plugin = 0L;
00085
00086 m_pluginList.push_back (info);
00087 }
00088 }
00089
00090 void KatePartPluginManager::addDocument(KTextEditor::Document *doc)
00091 {
00092
00093 for (KatePartPluginList::iterator it = m_pluginList.begin();
00094 it != m_pluginList.end(); ++it)
00095 {
00096 if (it->load) {
00097 it->plugin->addDocument(doc);
00098 }
00099 }
00100 }
00101
00102 void KatePartPluginManager::removeDocument(KTextEditor::Document *doc)
00103 {
00104
00105 for (KatePartPluginList::iterator it = m_pluginList.begin();
00106 it != m_pluginList.end(); ++it)
00107 {
00108 if (it->load) {
00109 it->plugin->removeDocument(doc);
00110 }
00111 }
00112 }
00113
00114 void KatePartPluginManager::addView(KTextEditor::View *view)
00115 {
00116
00117 for (KatePartPluginList::iterator it = m_pluginList.begin();
00118 it != m_pluginList.end(); ++it)
00119 {
00120 if (it->load) {
00121 it->plugin->addView(view);
00122 }
00123 }
00124 }
00125
00126 void KatePartPluginManager::removeView(KTextEditor::View *view)
00127 {
00128
00129 for (KatePartPluginList::iterator it = m_pluginList.begin();
00130 it != m_pluginList.end(); ++it)
00131 {
00132 if (it->load) {
00133 it->plugin->removeView(view);
00134 }
00135 }
00136 }
00137
00138 void KatePartPluginManager::loadConfig ()
00139 {
00140
00141 unloadAllPlugins ();
00142
00143 KConfigGroup cg = KConfigGroup(m_config, "Kate Part Plugins");
00144
00145
00146 foreach (const KatePartPluginInfo &plugin, m_pluginList)
00147 plugin.load = cg.readEntry (plugin.service()->library(), false)
00148 || cg.readEntry (plugin.service()->property("X-KDE-PluginInfo-Name").toString(), false);
00149
00150 loadAllPlugins();
00151 }
00152
00153 void KatePartPluginManager::writeConfig()
00154 {
00155 KConfigGroup cg = KConfigGroup( m_config, "Kate Part Plugins" );
00156 foreach(const KatePartPluginInfo &it, m_pluginList)
00157 {
00158 cg.writeEntry (it.saveName(), it.load);
00159 }
00160 }
00161
00162 void KatePartPluginManager::loadAllPlugins ()
00163 {
00164 for (KatePartPluginList::iterator it = m_pluginList.begin();
00165 it != m_pluginList.end(); ++it)
00166 {
00167 if (it->load)
00168 {
00169 loadPlugin(*it);
00170 enablePlugin(*it);
00171 }
00172 }
00173 }
00174
00175 void KatePartPluginManager::unloadAllPlugins ()
00176 {
00177 for (KatePartPluginList::iterator it = m_pluginList.begin();
00178 it != m_pluginList.end(); ++it)
00179 {
00180 if (it->plugin) {
00181 disablePlugin(*it);
00182 unloadPlugin(*it);
00183 }
00184 }
00185 }
00186
00187 void KatePartPluginManager::loadPlugin (KatePartPluginInfo &item)
00188 {
00189 if (item.plugin) return;
00190
00191
00192 QStringList openDependencies = item.dependencies();
00193 if ( !openDependencies.empty() )
00194 {
00195 for (KatePartPluginList::iterator it = m_pluginList.begin();
00196 it != m_pluginList.end(); ++it)
00197 {
00198 if ( openDependencies.contains( it->saveName() ) )
00199 {
00200 loadPlugin( *it );
00201 openDependencies.removeAll( it->saveName() );
00202 }
00203 }
00204 Q_ASSERT( openDependencies.empty() );
00205 }
00206
00207 item.plugin = KTextEditor::createPlugin (item.service(), this);
00208 Q_ASSERT(item.plugin);
00209 item.load = (item.plugin != 0);
00210 }
00211
00212 void KatePartPluginManager::unloadPlugin (KatePartPluginInfo &item)
00213 {
00214 if ( !item.plugin ) return;
00215
00216
00217 for (KatePartPluginList::iterator it = m_pluginList.begin();
00218 it != m_pluginList.end(); ++it)
00219 {
00220 if ( !it->plugin ) continue;
00221
00222 if ( it->dependencies().contains( item.saveName() ) )
00223 {
00224 unloadPlugin( *it );
00225 }
00226 }
00227
00228 delete item.plugin;
00229 item.plugin = 0L;
00230 item.load = false;
00231 }
00232
00233 void KatePartPluginManager::enablePlugin (KatePartPluginInfo &item)
00234 {
00235
00236 if (!item.plugin || !item.load)
00237 return;
00238
00239
00240 foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
00241 if (!doc)
00242 continue;
00243
00244 foreach (KTextEditor::View *view, doc->views()) {
00245 if (!view)
00246 continue;
00247
00248 KXMLGUIFactory *viewFactory = view->factory();
00249 if (viewFactory)
00250 viewFactory->removeClient(view);
00251
00252 item.plugin->addView(view);
00253
00254 if (viewFactory)
00255 viewFactory->addClient(view);
00256 }
00257 }
00258 }
00259
00260 void KatePartPluginManager::disablePlugin (KatePartPluginInfo &item)
00261 {
00262
00263 if (!item.plugin || !item.load)
00264 return;
00265
00266
00267 foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
00268 if (!doc)
00269 continue;
00270
00271 foreach (KTextEditor::View *view, doc->views()) {
00272 if (!view)
00273 continue;
00274
00275 KXMLGUIFactory *viewFactory = view->factory();
00276 if (viewFactory)
00277 viewFactory->removeClient(view);
00278
00279 item.plugin->removeView(view);
00280
00281 if (viewFactory)
00282 viewFactory->addClient(view);
00283 }
00284 }
00285 }
00286
00287