• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

Kontact Plugin Interface Library

  • kontactinterface
plugin.cpp
1 /*
2  This file is part of the KDE Kontact Plugin Interface Library.
3 
4  Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
5  Copyright (c) 2002-2003 Daniel Molkentin <molkentin@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "plugin.h"
24 #include <QFile>
25 #include "core.h"
26 
27 #include <kpimutils/processes.h>
28 
29 #include <kparts/componentfactory.h>
30 #include <kxmlguifactory.h>
31 #include <kaboutdata.h>
32 #include <kglobal.h>
33 #include <klocale.h>
34 #include <kdebug.h>
35 #include <kcomponentdata.h>
36 #include <kstandarddirs.h>
37 #include <krun.h>
38 
39 #include <QObject>
40 #include <QDBusConnection>
41 
42 #include <unistd.h>
43 
44 using namespace KontactInterface;
45 
50 //@cond PRIVATE
51 class Plugin::Private
52 {
53  public:
54 
55  void partDestroyed();
56  void setXmlFiles();
57  void removeInvisibleToolbarActions( Plugin *plugin );
58 
59  Core *core;
60  QList<KAction*> newActions;
61  QList<KAction*> syncActions;
62  QString identifier;
63  QString title;
64  QString icon;
65  QString executableName;
66  QString serviceName;
67  QByteArray partLibraryName;
68  QByteArray pluginName;
69  bool hasPart;
70  KParts::ReadOnlyPart *part;
71  bool disabled;
72 };
73 //@endcond
74 
75 Plugin::Plugin( Core *core, QObject *parent, const char *appName, const char *pluginName )
76  : KXMLGUIClient( core ), QObject( parent ), d( new Private )
77 {
78  setObjectName( appName );
79  core->factory()->addClient( this );
80  KGlobal::locale()->insertCatalog( appName );
81 
82  d->pluginName = pluginName ? pluginName : appName;
83  d->core = core;
84  d->hasPart = true;
85  d->part = 0;
86  d->disabled = false;
87 }
88 
89 Plugin::~Plugin()
90 {
91  delete d->part;
92  delete d;
93 }
94 
95 void Plugin::setIdentifier( const QString &identifier )
96 {
97  d->identifier = identifier;
98 }
99 
100 QString Plugin::identifier() const
101 {
102  return d->identifier;
103 }
104 
105 void Plugin::setTitle( const QString &title )
106 {
107  d->title = title;
108 }
109 
110 QString Plugin::title() const
111 {
112  return d->title;
113 }
114 
115 void Plugin::setIcon( const QString &icon )
116 {
117  d->icon = icon;
118 }
119 
120 QString Plugin::icon() const
121 {
122  return d->icon;
123 }
124 
125 void Plugin::setExecutableName( const QString &bin )
126 {
127  d->executableName = bin;
128 }
129 
130 QString Plugin::executableName() const
131 {
132  return d->executableName;
133 }
134 
135 void Plugin::setPartLibraryName( const QByteArray &libName )
136 {
137  d->partLibraryName = libName;
138 }
139 
140 bool Plugin::createDBUSInterface( const QString &serviceType )
141 {
142  Q_UNUSED( serviceType );
143  return false;
144 }
145 
146 bool Plugin::isRunningStandalone() const
147 {
148  return false;
149 }
150 
151 KParts::ReadOnlyPart *Plugin::loadPart()
152 {
153  return core()->createPart( d->partLibraryName );
154 }
155 
156 const KAboutData *Plugin::aboutData() const
157 {
158  KPluginLoader loader( d->partLibraryName );
159  KPluginFactory *factory = loader.factory();
160  kDebug() << "filename:" << loader.fileName();
161  kDebug() << "libname:" << d->partLibraryName;
162 
163  if ( factory ) {
164  if ( factory->componentData().isValid() ) {
165  kDebug() << "returning factory component aboutdata";
166  return factory->componentData().aboutData();
167  } else {
168  // If the componentData of the factory is invalid, the likely cause is that
169  // the part has not been ported to use K_PLUGIN_FACTORY/K_EXPORT_PLUGIN yet.
170  // In that case, fallback to the old method of loading component data, which
171  // does only work for old-style parts.
172 
173  kDebug() << "Unable to load component data for" << loader.fileName()
174  << "trying to use the old style plugin system now.";
175  const KComponentData instance =
176  KParts::Factory::partComponentDataFromLibrary( d->partLibraryName );
177  if ( instance.isValid() ) {
178  return instance.aboutData();
179  } else {
180  kDebug() << "Invalid instance, unable to get about information!";
181  }
182  }
183  }
184 
185  kError() << "Cannot load instance for" << title();
186  return 0;
187 }
188 
189 KParts::ReadOnlyPart *Plugin::part()
190 {
191  if ( !d->part ) {
192  d->part = createPart();
193  if ( d->part ) {
194  connect( d->part, SIGNAL(destroyed()), SLOT(partDestroyed()) );
195  d->removeInvisibleToolbarActions( this );
196  core()->partLoaded( this, d->part );
197  }
198  }
199  return d->part;
200 }
201 
202 QString Plugin::tipFile() const
203 {
204  return QString();
205 }
206 
207 QString Plugin::registerClient()
208 {
209  if ( d->serviceName.isEmpty() ) {
210  d->serviceName = "org.kde." + objectName().toLatin1();
211 #ifdef Q_WS_WIN
212  const QString pid = QString::number( getpid() );
213  d->serviceName.append( ".unique-" + pid );
214 #endif
215  QDBusConnection::sessionBus().registerService( d->serviceName );
216  }
217  return d->serviceName;
218 }
219 
220 int Plugin::weight() const
221 {
222  return 0;
223 }
224 
225 void Plugin::insertNewAction( KAction *action )
226 {
227  d->newActions.append( action );
228 }
229 
230 void Plugin::insertSyncAction( KAction *action )
231 {
232  d->syncActions.append( action );
233 }
234 
235 QList<KAction*> Plugin::newActions() const
236 {
237  return d->newActions;
238 }
239 
240 QList<KAction*> Plugin::syncActions() const
241 {
242  return d->syncActions;
243 }
244 
245 QStringList Plugin::invisibleToolbarActions() const
246 {
247  return QStringList();
248 }
249 
250 bool Plugin::canDecodeMimeData( const QMimeData *data ) const
251 {
252  Q_UNUSED( data );
253  return false;
254 }
255 
256 void Plugin::processDropEvent( QDropEvent * )
257 {
258 }
259 
260 void Plugin::readProperties( const KConfigGroup & )
261 {
262 }
263 
264 void Plugin::saveProperties( KConfigGroup & )
265 {
266 }
267 
268 Core *Plugin::core() const
269 {
270  return d->core;
271 }
272 
273 void Plugin::aboutToSelect()
274 {
275  // Because the 3 korganizer plugins share the same part, we need to switch
276  // that part's XML files every time we are about to show its GUI...
277  d->setXmlFiles();
278 
279  select();
280 }
281 
282 void Plugin::select()
283 {
284 }
285 
286 void Plugin::configUpdated()
287 {
288 }
289 
290 //@cond PRIVATE
291 void Plugin::Private::partDestroyed()
292 {
293  part = 0;
294 }
295 
296 void Plugin::Private::removeInvisibleToolbarActions( Plugin *plugin )
297 {
298  if ( pluginName.isEmpty() ) {
299  return;
300  }
301 
302  // Hide unwanted toolbar action by modifying the XML before createGUI, rather
303  // than doing it by calling removeAction on the toolbar after createGUI. Both
304  // solutions work visually, but only modifying the XML ensures that the
305  // actions don't appear in "edit toolbars". #207296
306  const QStringList hideActions = plugin->invisibleToolbarActions();
307  //kDebug() << "Hiding actions" << hideActions << "from" << pluginName << part;
308  QDomDocument doc = part->domDocument();
309  QDomElement docElem = doc.documentElement();
310  // 1. Iterate over containers
311  for ( QDomElement containerElem = docElem.firstChildElement();
312  !containerElem.isNull(); containerElem = containerElem.nextSiblingElement() ) {
313  if ( QString::compare( containerElem.tagName(), "ToolBar", Qt::CaseInsensitive ) == 0 ) {
314  // 2. Iterate over actions in toolbars
315  QDomElement actionElem = containerElem.firstChildElement();
316  while ( !actionElem.isNull() ) {
317  QDomElement nextActionElem = actionElem.nextSiblingElement();
318  if ( QString::compare( actionElem.tagName(), "Action", Qt::CaseInsensitive ) == 0 ) {
319  //kDebug() << "Looking at action" << actionElem.attribute("name");
320  if ( hideActions.contains( actionElem.attribute( "name" ) ) ) {
321  //kDebug() << "REMOVING";
322  containerElem.removeChild( actionElem );
323  }
324  }
325  actionElem = nextActionElem;
326  }
327  }
328  }
329 
330  // Possible optimization: we could do all the above and the writing below
331  // only when (newAppFile does not exist) or (version of domDocument > version of newAppFile) (*)
332  // This requires parsing newAppFile when it exists, though, and better use
333  // the fast kdeui code for that rather than a full QDomDocument.
334  // (*) or when invisibleToolbarActions() changes :)
335 
336  const QString newAppFile =
337  KStandardDirs::locateLocal( "data", "kontact/default-" + pluginName + ".rc" );
338  QFile file( newAppFile );
339  if ( !file.open( QFile::WriteOnly ) ) {
340  kWarning() << "error writing to" << newAppFile;
341  return;
342  }
343  file.write( doc.toString().toUtf8() );
344  file.flush();
345 
346  setXmlFiles();
347 }
348 
349 void Plugin::Private::setXmlFiles()
350 {
351  const QString newAppFile =
352  KStandardDirs::locateLocal( "data", "kontact/default-" + pluginName + ".rc" );
353  const QString localFile =
354  KStandardDirs::locateLocal( "data", "kontact/local-" + pluginName + ".rc" );
355  if ( part->xmlFile() != newAppFile || part->localXMLFile() != localFile ) {
356  part->replaceXMLFile( newAppFile, localFile );
357  }
358 }
359 //@endcond
360 
361 void Plugin::slotConfigUpdated()
362 {
363  configUpdated();
364 }
365 
366 void Plugin::bringToForeground()
367 {
368  if ( d->executableName.isEmpty() ) {
369  return;
370  }
371 #ifdef Q_WS_WIN
372  KPIMUtils::activateWindowForProcess( d->executableName );
373 #else
374  KRun::runCommand( d->executableName, 0 );
375 #endif
376 }
377 
378 Summary *Plugin::createSummaryWidget( QWidget *parent )
379 {
380  Q_UNUSED( parent );
381  return 0;
382 }
383 
384 bool Plugin::showInSideBar() const
385 {
386  return d->hasPart;
387 }
388 
389 void Plugin::setShowInSideBar( bool hasPart )
390 {
391  d->hasPart = hasPart;
392 }
393 
394 bool Plugin::queryClose() const
395 {
396  return true;
397 }
398 
399 void Plugin::setDisabled( bool disabled )
400 {
401  d->disabled = disabled;
402 }
403 
404 bool Plugin::disabled() const
405 {
406  return d->disabled;
407 }
408 
409 void Plugin::virtual_hook( int, void * )
410 {
411  //BASE::virtual_hook( id, data );
412 }
413 
414 #include "moc_plugin.cpp"
415 
416 // vim: sw=2 et sts=2 tw=80
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:09 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kontact Plugin Interface Library

Skip menu "Kontact Plugin Interface Library"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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