• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • utils
katepartpluginmanager.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2001-2010 Christoph Cullmann <cullmann@kde.org>
4  * Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
5  * Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
6  * Copyright (C) 2007 Dominik Haumann <dhaumann kde org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "katepartpluginmanager.h"
25 #include "katepartpluginmanager.moc"
26 
27 #include "kateglobal.h"
28 
29 #include <ktexteditor/plugin.h>
30 #include <ktexteditor/document.h>
31 #include <ktexteditor/view.h>
32 #include <kconfig.h>
33 #include <kconfiggroup.h>
34 #include <kxmlguifactory.h>
35 #include <kplugininfo.h>
36 
37 #include <kservicetypetrader.h>
38 #include <kdebug.h>
39 
40 KatePartPluginInfo::KatePartPluginInfo(KService::Ptr service)
41  : m_pluginInfo(service)
42 {
43 }
44 
45 QString KatePartPluginInfo::saveName() const
46 {
47  QString saveName = m_pluginInfo.pluginName();
48  if (saveName.isEmpty())
49  saveName = service()->library();
50  return saveName;
51 }
52 
53 KatePartPluginManager::KatePartPluginManager()
54  : QObject(),
55  m_config(new KConfig("katepartpluginsrc", KConfig::NoGlobals))
56 {
57  setupPluginList ();
58  loadConfig ();
59 }
60 
61 KatePartPluginManager::~KatePartPluginManager()
62 {
63  writeConfig();
64  // than unload the plugins
65  unloadAllPlugins ();
66  delete m_config;
67  m_config = 0;
68 }
69 
70 KatePartPluginManager *KatePartPluginManager::self()
71 {
72  return KateGlobal::self()->pluginManager ();
73 }
74 
75 void KatePartPluginManager::setupPluginList ()
76 {
77  KService::List traderList = KServiceTypeTrader::self()->
78  query("KTextEditor/Plugin");
79 
80  foreach(const KService::Ptr &ptr, traderList)
81  {
82  QVariant version = ptr->property("X-KDE-Version", QVariant::String);
83  QStringList numbers = qvariant_cast<QString>(version).split('.');
84  unsigned int kdeVersion = KDE_MAKE_VERSION(numbers.value(0).toUInt(),
85  numbers.value(1).toUInt(),
86  numbers.value(2).toUInt());
87 
88  if (KDE_MAKE_VERSION(4,0,0) <= kdeVersion && kdeVersion <= KDE::version())
89  {
90  KatePartPluginInfo info(ptr);
91 
92  info.load = false;
93  info.plugin = 0L;
94 
95  m_pluginList.push_back (info);
96  }
97  }
98 }
99 
100 void KatePartPluginManager::addDocument(KTextEditor::Document *doc)
101 {
102  //kDebug() << doc;
103  for (KatePartPluginList::iterator it = m_pluginList.begin();
104  it != m_pluginList.end(); ++it)
105  {
106  if (it->load) {
107  it->plugin->addDocument(doc);
108  }
109  }
110 }
111 
112 void KatePartPluginManager::removeDocument(KTextEditor::Document *doc)
113 {
114  //kDebug() << doc;
115  for (KatePartPluginList::iterator it = m_pluginList.begin();
116  it != m_pluginList.end(); ++it)
117  {
118  if (it->load) {
119  it->plugin->removeDocument(doc);
120  }
121  }
122 }
123 
124 void KatePartPluginManager::addView(KTextEditor::View *view)
125 {
126  //kDebug() << view;
127  for (KatePartPluginList::iterator it = m_pluginList.begin();
128  it != m_pluginList.end(); ++it)
129  {
130  if (it->load) {
131  it->plugin->addView(view);
132  }
133  }
134 }
135 
136 void KatePartPluginManager::removeView(KTextEditor::View *view)
137 {
138  //kDebug() << view;
139  for (KatePartPluginList::iterator it = m_pluginList.begin();
140  it != m_pluginList.end(); ++it)
141  {
142  if (it->load) {
143  it->plugin->removeView(view);
144  }
145  }
146 }
147 
148 void KatePartPluginManager::loadConfig ()
149 {
150  // first: unload the plugins
151  unloadAllPlugins ();
152 
153  KConfigGroup cg = KConfigGroup(m_config, "Kate Part Plugins");
154 
155  // disable all plugin if no config...
156  foreach (const KatePartPluginInfo &plugin, m_pluginList) {
157  bool enabledByDefault = plugin.isEnabledByDefault();
158  plugin.load = cg.readEntry (plugin.saveName(), enabledByDefault);
159  }
160 
161  loadAllPlugins();
162 }
163 
164 void KatePartPluginManager::writeConfig()
165 {
166  KConfigGroup cg = KConfigGroup( m_config, "Kate Part Plugins" );
167  foreach(const KatePartPluginInfo &it, m_pluginList)
168  {
169  cg.writeEntry (it.saveName(), it.load);
170  }
171 }
172 
173 void KatePartPluginManager::loadAllPlugins ()
174 {
175  for (KatePartPluginList::iterator it = m_pluginList.begin();
176  it != m_pluginList.end(); ++it)
177  {
178  if (it->load)
179  {
180  loadPlugin(*it);
181  enablePlugin(*it);
182  }
183  }
184 }
185 
186 void KatePartPluginManager::unloadAllPlugins ()
187 {
188  for (KatePartPluginList::iterator it = m_pluginList.begin();
189  it != m_pluginList.end(); ++it)
190  {
191  if (it->plugin) {
192  disablePlugin(*it);
193  unloadPlugin(*it);
194  }
195  }
196 }
197 
198 void KatePartPluginManager::loadPlugin (KatePartPluginInfo &item)
199 {
200  if (item.plugin) return;
201 
202  // make sure all dependencies are loaded beforehand
203  QStringList openDependencies = item.dependencies();
204  if ( !openDependencies.empty() )
205  {
206  for (KatePartPluginList::iterator it = m_pluginList.begin();
207  it != m_pluginList.end(); ++it)
208  {
209  if ( openDependencies.contains( it->saveName() ) )
210  {
211  loadPlugin( *it );
212  openDependencies.removeAll( it->saveName() );
213  }
214  }
215  Q_ASSERT( openDependencies.empty() );
216  }
217 
218  // try to load, else reset load flag!
219  QString error;
220  item.plugin = item.service()->createInstance<KTextEditor::Plugin>(this, QVariantList(), &error);
221  if ( !item.plugin )
222  kWarning() << "failed to load plugin" << item.service()->name() << ":" << error;
223  item.load = (item.plugin != 0);
224 }
225 
226 void KatePartPluginManager::unloadPlugin (KatePartPluginInfo &item)
227 {
228  if ( !item.plugin ) return;
229 
230  // make sure dependent plugins are unloaded beforehand
231  for (KatePartPluginList::iterator it = m_pluginList.begin();
232  it != m_pluginList.end(); ++it)
233  {
234  if ( !it->plugin ) continue;
235 
236  if ( it->dependencies().contains( item.saveName() ) )
237  {
238  unloadPlugin( *it );
239  }
240  }
241 
242  delete item.plugin;
243  item.plugin = 0L;
244  item.load = false;
245 }
246 
247 void KatePartPluginManager::enablePlugin (KatePartPluginInfo &item)
248 {
249  // plugin around at all?
250  if (!item.plugin || !item.load)
251  return;
252 
253  // register docs and views
254  foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
255  if (!doc)
256  continue;
257 
258  foreach (KTextEditor::View *view, doc->views()) {
259  if (!view)
260  continue;
261 
262  KXMLGUIFactory *viewFactory = view->factory();
263  if (viewFactory)
264  viewFactory->removeClient(view);
265 
266  item.plugin->addView(view);
267 
268  if (viewFactory)
269  viewFactory->addClient(view);
270  }
271  }
272 }
273 
274 void KatePartPluginManager::disablePlugin (KatePartPluginInfo &item)
275 {
276  // plugin around at all?
277  if (!item.plugin || !item.load)
278  return;
279 
280  // de-register docs and views
281  foreach (KTextEditor::Document *doc, KateGlobal::self()->documents()) {
282  if (!doc)
283  continue;
284 
285  foreach (KTextEditor::View *view, doc->views()) {
286  if (!view)
287  continue;
288 
289  KXMLGUIFactory *viewFactory = view->factory();
290  if (viewFactory)
291  viewFactory->removeClient(view);
292 
293  item.plugin->removeView(view);
294 
295  if (viewFactory)
296  viewFactory->addClient(view);
297  }
298  }
299 }
300 
301 // kate: space-indent on; indent-width 2; replace-tabs on;
KatePartPluginInfo
Definition: katepartpluginmanager.h:39
KatePartPluginManager::~KatePartPluginManager
~KatePartPluginManager()
Definition: katepartpluginmanager.cpp:61
KatePartPluginInfo::dependencies
QStringList dependencies() const
Definition: katepartpluginmanager.h:47
QList::push_back
void push_back(const T &value)
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
KatePartPluginInfo::service
KService::Ptr service() const
Definition: katepartpluginmanager.h:46
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KatePartPluginManager::removeView
void removeView(KTextEditor::View *view)
Definition: katepartpluginmanager.cpp:136
KatePartPluginManager::enablePlugin
void enablePlugin(KatePartPluginInfo &item)
Definition: katepartpluginmanager.cpp:247
KatePartPluginManager::unloadPlugin
void unloadPlugin(KatePartPluginInfo &item)
Definition: katepartpluginmanager.cpp:226
QList::value
T value(int i) const
KatePartPluginManager::writeConfig
void writeConfig()
Definition: katepartpluginmanager.cpp:164
KatePartPluginManager::removeDocument
void removeDocument(KTextEditor::Document *doc)
Definition: katepartpluginmanager.cpp:112
QList::empty
bool empty() const
katepartpluginmanager.h
QObject
kateglobal.h
QString::isEmpty
bool isEmpty() const
QList::removeAll
int removeAll(const T &value)
KatePartPluginInfo::isEnabledByDefault
bool isEnabledByDefault() const
Definition: katepartpluginmanager.h:48
KatePartPluginInfo::plugin
KTextEditor::Plugin * plugin
Definition: katepartpluginmanager.h:44
QString
KatePartPluginInfo::KatePartPluginInfo
KatePartPluginInfo(KService::Ptr service)
Definition: katepartpluginmanager.cpp:40
KateGlobal::pluginManager
KatePartPluginManager * pluginManager()
global plugin manager
Definition: kateglobal.h:279
QStringList
KatePartPluginManager::self
static KatePartPluginManager * self()
Definition: katepartpluginmanager.cpp:70
QList::end
iterator end()
KatePartPluginManager::disablePlugin
void disablePlugin(KatePartPluginInfo &item)
Definition: katepartpluginmanager.cpp:274
KatePartPluginManager::unloadAllPlugins
void unloadAllPlugins()
Definition: katepartpluginmanager.cpp:186
KatePartPluginManager::loadAllPlugins
void loadAllPlugins()
Definition: katepartpluginmanager.cpp:173
KatePartPluginManager::addDocument
void addDocument(KTextEditor::Document *doc)
Definition: katepartpluginmanager.cpp:100
KatePartPluginInfo::saveName
QString saveName() const
Definition: katepartpluginmanager.cpp:45
KatePartPluginManager::addView
void addView(KTextEditor::View *view)
Definition: katepartpluginmanager.cpp:124
KatePartPluginManager
Definition: katepartpluginmanager.h:56
KatePartPluginInfo::load
bool load
Definition: katepartpluginmanager.h:43
QList::begin
iterator begin()
KatePartPluginManager::loadConfig
void loadConfig()
Definition: katepartpluginmanager.cpp:148
KatePartPluginManager::KatePartPluginManager
KatePartPluginManager()
Definition: katepartpluginmanager.cpp:53
QVariant
KatePartPluginManager::loadPlugin
void loadPlugin(KatePartPluginInfo &item)
Definition: katepartpluginmanager.cpp:198
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal