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

Kontact Plugin Interface Library

  • sources
  • kde-4.14
  • kdepimlibs
  • 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 <klocalizedstring.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 #include <QDomDocument>
42 
43 #include <unistd.h>
44 
45 using namespace KontactInterface;
46 
51 //@cond PRIVATE
52 class Plugin::Private
53 {
54  public:
55 
56  void partDestroyed();
57  void setXmlFiles();
58  void removeInvisibleToolbarActions( Plugin *plugin );
59 
60  Core *core;
61  QList<KAction*> newActions;
62  QList<KAction*> syncActions;
63  QString identifier;
64  QString title;
65  QString icon;
66  QString executableName;
67  QString serviceName;
68  QByteArray partLibraryName;
69  QByteArray pluginName;
70  KParts::ReadOnlyPart *part;
71  bool hasPart;
72  bool disabled;
73 };
74 //@endcond
75 
76 Plugin::Plugin( Core *core, QObject *parent, const char *appName, const char *pluginName )
77  : KXMLGUIClient( core ), QObject( parent ), d( new Private )
78 {
79  setObjectName( QLatin1String(appName) );
80  core->factory()->addClient( this );
81  KGlobal::locale()->insertCatalog( QLatin1String(appName) );
82 
83  d->pluginName = pluginName ? pluginName : appName;
84  d->core = core;
85  d->hasPart = true;
86  d->part = 0;
87  d->disabled = false;
88 }
89 
90 Plugin::~Plugin()
91 {
92  delete d->part;
93  delete d;
94 }
95 
96 void Plugin::setIdentifier( const QString &identifier )
97 {
98  d->identifier = identifier;
99 }
100 
101 QString Plugin::identifier() const
102 {
103  return d->identifier;
104 }
105 
106 void Plugin::setTitle( const QString &title )
107 {
108  d->title = title;
109 }
110 
111 QString Plugin::title() const
112 {
113  return d->title;
114 }
115 
116 void Plugin::setIcon( const QString &icon )
117 {
118  d->icon = icon;
119 }
120 
121 QString Plugin::icon() const
122 {
123  return d->icon;
124 }
125 
126 void Plugin::setExecutableName( const QString &bin )
127 {
128  d->executableName = bin;
129 }
130 
131 QString Plugin::executableName() const
132 {
133  return d->executableName;
134 }
135 
136 void Plugin::setPartLibraryName( const QByteArray &libName )
137 {
138  d->partLibraryName = libName;
139 }
140 
141 bool Plugin::createDBUSInterface( const QString &serviceType )
142 {
143  Q_UNUSED( serviceType );
144  return false;
145 }
146 
147 bool Plugin::isRunningStandalone() const
148 {
149  return false;
150 }
151 
152 KParts::ReadOnlyPart *Plugin::loadPart()
153 {
154  return core()->createPart( d->partLibraryName );
155 }
156 
157 const KAboutData *Plugin::aboutData() const
158 {
159  KPluginLoader loader( QString::fromLatin1(d->partLibraryName) );
160  KPluginFactory *factory = loader.factory();
161  kDebug() << "filename:" << loader.fileName();
162  kDebug() << "libname:" << d->partLibraryName;
163 
164  if ( factory ) {
165  if ( factory->componentData().isValid() ) {
166  kDebug() << "returning factory component aboutdata";
167  return factory->componentData().aboutData();
168  } else {
169  // If the componentData of the factory is invalid, the likely cause is that
170  // the part has not been ported to use K_PLUGIN_FACTORY/K_EXPORT_PLUGIN yet.
171  // In that case, fallback to the old method of loading component data, which
172  // does only work for old-style parts.
173 
174  kDebug() << "Unable to load component data for" << loader.fileName()
175  << "trying to use the old style plugin system now.";
176  const KComponentData instance =
177  KParts::Factory::partComponentDataFromLibrary( QString::fromLatin1(d->partLibraryName) );
178  if ( instance.isValid() ) {
179  return instance.aboutData();
180  } else {
181  kDebug() << "Invalid instance, unable to get about information!";
182  }
183  }
184  }
185 
186  kError() << "Cannot load instance for" << title();
187  return 0;
188 }
189 
190 KParts::ReadOnlyPart *Plugin::part()
191 {
192  if ( !d->part ) {
193  d->part = createPart();
194  if ( d->part ) {
195  connect( d->part, SIGNAL(destroyed()), SLOT(partDestroyed()) );
196  d->removeInvisibleToolbarActions( this );
197  core()->partLoaded( this, d->part );
198  }
199  }
200  return d->part;
201 }
202 
203 QString Plugin::tipFile() const
204 {
205  return QString();
206 }
207 
208 QString Plugin::registerClient()
209 {
210  if ( d->serviceName.isEmpty() ) {
211  d->serviceName = QLatin1String("org.kde.") + QLatin1String(objectName().toLatin1());
212 #ifdef Q_WS_WIN
213  const QString pid = QString::number( getpid() );
214  d->serviceName.append( QLatin1String(".unique-") + pid );
215 #endif
216  QDBusConnection::sessionBus().registerService( d->serviceName );
217  }
218  return d->serviceName;
219 }
220 
221 int Plugin::weight() const
222 {
223  return 0;
224 }
225 
226 void Plugin::insertNewAction( KAction *action )
227 {
228  d->newActions.append( action );
229 }
230 
231 void Plugin::insertSyncAction( KAction *action )
232 {
233  d->syncActions.append( action );
234 }
235 
236 QList<KAction*> Plugin::newActions() const
237 {
238  return d->newActions;
239 }
240 
241 QList<KAction*> Plugin::syncActions() const
242 {
243  return d->syncActions;
244 }
245 
246 QStringList Plugin::invisibleToolbarActions() const
247 {
248  return QStringList();
249 }
250 
251 bool Plugin::canDecodeMimeData( const QMimeData *data ) const
252 {
253  Q_UNUSED( data );
254  return false;
255 }
256 
257 void Plugin::processDropEvent( QDropEvent * )
258 {
259 }
260 
261 void Plugin::readProperties( const KConfigGroup & )
262 {
263 }
264 
265 void Plugin::saveProperties( KConfigGroup & )
266 {
267 }
268 
269 Core *Plugin::core() const
270 {
271  return d->core;
272 }
273 
274 void Plugin::aboutToSelect()
275 {
276  // Because the 3 korganizer plugins share the same part, we need to switch
277  // that part's XML files every time we are about to show its GUI...
278  d->setXmlFiles();
279 
280  select();
281 }
282 
283 void Plugin::select()
284 {
285 }
286 
287 void Plugin::configUpdated()
288 {
289 }
290 
291 //@cond PRIVATE
292 void Plugin::Private::partDestroyed()
293 {
294  part = 0;
295 }
296 
297 void Plugin::Private::removeInvisibleToolbarActions( Plugin *plugin )
298 {
299  if ( pluginName.isEmpty() ) {
300  return;
301  }
302 
303  // Hide unwanted toolbar action by modifying the XML before createGUI, rather
304  // than doing it by calling removeAction on the toolbar after createGUI. Both
305  // solutions work visually, but only modifying the XML ensures that the
306  // actions don't appear in "edit toolbars". #207296
307  const QStringList hideActions = plugin->invisibleToolbarActions();
308  //kDebug() << "Hiding actions" << hideActions << "from" << pluginName << part;
309  QDomDocument doc = part->domDocument();
310  QDomElement docElem = doc.documentElement();
311  // 1. Iterate over containers
312  for ( QDomElement containerElem = docElem.firstChildElement();
313  !containerElem.isNull(); containerElem = containerElem.nextSiblingElement() ) {
314  if ( QString::compare( containerElem.tagName(), QLatin1String("ToolBar"), Qt::CaseInsensitive ) == 0 ) {
315  // 2. Iterate over actions in toolbars
316  QDomElement actionElem = containerElem.firstChildElement();
317  while ( !actionElem.isNull() ) {
318  QDomElement nextActionElem = actionElem.nextSiblingElement();
319  if ( QString::compare( actionElem.tagName(), QLatin1String("Action"), Qt::CaseInsensitive ) == 0 ) {
320  //kDebug() << "Looking at action" << actionElem.attribute("name");
321  if ( hideActions.contains( actionElem.attribute( QLatin1String("name") ) ) ) {
322  //kDebug() << "REMOVING";
323  containerElem.removeChild( actionElem );
324  }
325  }
326  actionElem = nextActionElem;
327  }
328  }
329  }
330 
331  // Possible optimization: we could do all the above and the writing below
332  // only when (newAppFile does not exist) or (version of domDocument > version of newAppFile) (*)
333  // This requires parsing newAppFile when it exists, though, and better use
334  // the fast kdeui code for that rather than a full QDomDocument.
335  // (*) or when invisibleToolbarActions() changes :)
336 
337  const QString newAppFile =
338  KStandardDirs::locateLocal( "data", QLatin1String("kontact/default-") + QLatin1String(pluginName) + QLatin1String(".rc") );
339  QFile file( newAppFile );
340  if ( !file.open( QFile::WriteOnly ) ) {
341  kWarning() << "error writing to" << newAppFile;
342  return;
343  }
344  file.write( doc.toString().toUtf8() );
345  file.flush();
346 
347  setXmlFiles();
348 }
349 
350 void Plugin::Private::setXmlFiles()
351 {
352  const QString newAppFile =
353  KStandardDirs::locateLocal( "data", QLatin1String("kontact/default-") + QLatin1String(pluginName) + QLatin1String(".rc") );
354  const QString localFile =
355  KStandardDirs::locateLocal( "data", QLatin1String("kontact/local-") + QLatin1String(pluginName) + QLatin1String(".rc") );
356  if ( part->xmlFile() != newAppFile || part->localXMLFile() != localFile ) {
357  part->replaceXMLFile( newAppFile, localFile );
358  }
359 }
360 //@endcond
361 
362 void Plugin::slotConfigUpdated()
363 {
364  configUpdated();
365 }
366 
367 void Plugin::bringToForeground()
368 {
369  if ( d->executableName.isEmpty() ) {
370  return;
371  }
372 #ifdef Q_WS_WIN
373  KPIMUtils::activateWindowForProcess( d->executableName );
374 #else
375  KRun::runCommand( d->executableName, 0 );
376 #endif
377 }
378 
379 Summary *Plugin::createSummaryWidget( QWidget *parent )
380 {
381  Q_UNUSED( parent );
382  return 0;
383 }
384 
385 bool Plugin::showInSideBar() const
386 {
387  return d->hasPart;
388 }
389 
390 void Plugin::setShowInSideBar( bool hasPart )
391 {
392  d->hasPart = hasPart;
393 }
394 
395 bool Plugin::queryClose() const
396 {
397  return true;
398 }
399 
400 void Plugin::setDisabled( bool disabled )
401 {
402  d->disabled = disabled;
403 }
404 
405 bool Plugin::disabled() const
406 {
407  return d->disabled;
408 }
409 
410 void Plugin::shortcutChanged()
411 {
412 }
413 
414 void Plugin::virtual_hook( int, void * )
415 {
416  //BASE::virtual_hook( id, data );
417 }
418 
419 #include "moc_plugin.cpp"
420 
421 // vim: sw=2 et sts=2 tw=80
KontactInterface::Plugin::configUpdated
virtual void configUpdated()
This function is called whenever the config dialog has been closed successfully.
Definition: plugin.cpp:287
QWidget
KontactInterface::Plugin::showInSideBar
virtual bool showInSideBar() const
Returns whether the plugin provides a part that should be shown in the sidebar.
Definition: plugin.cpp:385
KontactInterface::Plugin::saveProperties
virtual void saveProperties(KConfigGroup &)
Session management: save properties.
Definition: plugin.cpp:265
KontactInterface::Plugin::canDecodeMimeData
virtual bool canDecodeMimeData(const QMimeData *data) const
Returns whether the plugin can handle the drag object of the given mime type.
Definition: plugin.cpp:251
QByteArray
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
QDomDocument::toString
QString toString(int indent) const
KontactInterface::Plugin::setShowInSideBar
void setShowInSideBar(bool hasPart)
Set if the plugin provides a part that should be shown in the sidebar.
Definition: plugin.cpp:390
KontactInterface::Plugin::identifier
QString identifier() const
Returns the identifier of the plugin.
Definition: plugin.cpp:101
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QDomNode::nextSiblingElement
QDomElement nextSiblingElement(const QString &tagName) const
KontactInterface::Plugin::registerClient
QString registerClient()
Registers the client at DBus and returns the dbus identifier.
Definition: plugin.cpp:208
KontactInterface::Plugin::shortcutChanged
virtual void shortcutChanged()
Definition: plugin.cpp:410
QDBusConnection::sessionBus
QDBusConnection sessionBus()
QDomDocument::documentElement
QDomElement documentElement() const
KontactInterface::Plugin::setDisabled
void setDisabled(bool value)
Sets whether the plugin shall be disabled.
Definition: plugin.cpp:400
KontactInterface::Plugin::slotConfigUpdated
void slotConfigUpdated()
Definition: plugin.cpp:362
QMimeData
QFile
KontactInterface::Plugin::createPart
virtual KParts::ReadOnlyPart * createPart()=0
Reimplement and return the part here.
KontactInterface::Plugin::icon
QString icon() const
Returns the icon name that is used for the plugin.
Definition: plugin.cpp:121
KontactInterface::Plugin::setIcon
void setIcon(const QString &icon)
Sets the icon name that is used for the plugin.
Definition: plugin.cpp:116
QString::number
QString number(int n, int base)
KontactInterface::Summary
Base class for summary widgets in Kontact.
Definition: summary.h:41
KontactInterface::Plugin::weight
virtual int weight() const
Return the weight of the plugin.
Definition: plugin.cpp:221
QObject
KontactInterface::Plugin::part
KParts::ReadOnlyPart * part()
You can use this method if you need to access the current part.
Definition: plugin.cpp:190
QDropEvent
QObject::setObjectName
void setObjectName(const QString &name)
KontactInterface::Plugin::aboutData
virtual const KAboutData * aboutData() const
Reimplement this method if you want to add your credits to the Kontact about dialog.
Definition: plugin.cpp:157
KontactInterface::Plugin::executableName
QString executableName() const
Returns the name of the executable (if existent).
Definition: plugin.cpp:131
KontactInterface::Plugin::insertSyncAction
void insertSyncAction(KAction *action)
Inserts a custom "Sync" action.
Definition: plugin.cpp:231
KontactInterface::Plugin::~Plugin
virtual ~Plugin()
Destroys the plugin.
Definition: plugin.cpp:90
KontactInterface::Plugin::title
QString title() const
Returns the localized title of the plugin.
Definition: plugin.cpp:111
KontactInterface::Plugin::core
Core * core() const
Returns a pointer to the kontact core object.
Definition: plugin.cpp:269
QString
QList
KontactInterface::Plugin::setTitle
void setTitle(const QString &title)
Sets the localized title of the plugin.
Definition: plugin.cpp:106
KontactInterface::Plugin::tipFile
virtual QString tipFile() const
Reimplement this method and return the a path relative to "data" to the tips file.
Definition: plugin.cpp:203
KontactInterface::Plugin::readProperties
virtual void readProperties(const KConfigGroup &)
Session management: read properties.
Definition: plugin.cpp:261
KontactInterface::Plugin::processDropEvent
virtual void processDropEvent(QDropEvent *)
Process drop event.
Definition: plugin.cpp:257
QStringList
KontactInterface::Plugin::syncActions
QList< KAction * > syncActions() const
Returns the list of custom "Sync" actions.
Definition: plugin.cpp:241
KontactInterface::Core::createPart
KParts::ReadOnlyPart * createPart(const char *library)
Definition: core.cpp:70
QDomNode::removeChild
QDomNode removeChild(const QDomNode &oldChild)
QDomDocument
QDomNode::isNull
bool isNull() const
KontactInterface::Plugin::loadPart
KParts::ReadOnlyPart * loadPart()
Returns the loaded part.
Definition: plugin.cpp:152
KontactInterface::Plugin::invisibleToolbarActions
virtual QStringList invisibleToolbarActions() const
Returns a list of action names that shall be hidden in the main toolbar.
Definition: plugin.cpp:246
KontactInterface::Plugin::disabled
bool disabled() const
Returns whether the plugin is disabled.
Definition: plugin.cpp:405
QLatin1String
KontactInterface::Plugin::setIdentifier
void setIdentifier(const QString &identifier)
Sets the identifier of the plugin.
Definition: plugin.cpp:96
QDomNode::firstChildElement
QDomElement firstChildElement(const QString &tagName) const
KontactInterface::Plugin::Plugin
Plugin(Core *core, QObject *parent, const char *appName, const char *pluginName=0)
Creates a new plugin.
Definition: plugin.cpp:76
KontactInterface::Plugin::isRunningStandalone
virtual bool isRunningStandalone() const
Reimplement this method and return whether a standalone application is still running.
Definition: plugin.cpp:147
KontactInterface::Plugin::createDBUSInterface
virtual bool createDBUSInterface(const QString &serviceType)
Create the D-Bus interface for the given serviceType, if this plugin provides it. ...
Definition: plugin.cpp:141
KontactInterface::Plugin
Base class for all Plugins in Kontact.
Definition: plugin.h:77
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KontactInterface::Plugin::setPartLibraryName
void setPartLibraryName(const QByteArray &name)
Set name of library which contains the KPart used by this plugin.
Definition: plugin.cpp:136
KontactInterface::Core
The abstract interface that represents the Kontact core.
Definition: core.h:39
QDomElement::tagName
QString tagName() const
KontactInterface::Plugin::newActions
QList< KAction * > newActions() const
Returns the list of custom "New" actions.
Definition: plugin.cpp:236
KontactInterface::Plugin::setExecutableName
void setExecutableName(const QString &name)
Sets the name of executable (if existent).
Definition: plugin.cpp:126
KontactInterface::Plugin::createSummaryWidget
virtual Summary * createSummaryWidget(QWidget *parent)
Reimplement this method if you want to add a widget for your application to Kontact's summary page...
Definition: plugin.cpp:379
KontactInterface::Plugin::bringToForeground
virtual void bringToForeground()
Reimplement this method if your application needs a different approach to be brought in the foregroun...
Definition: plugin.cpp:367
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QDomElement
QString::compare
int compare(const QString &other) const
KontactInterface::Plugin::insertNewAction
void insertNewAction(KAction *action)
Inserts a custom "New" action.
Definition: plugin.cpp:226
KontactInterface::Core::partLoaded
virtual void partLoaded(Plugin *plugin, KParts::ReadOnlyPart *part)=0
KontactInterface::Plugin::queryClose
virtual bool queryClose() const
Reimplement this method if you want to add checks before closing the main kontact window...
Definition: plugin.cpp:395
QObject::destroyed
void destroyed(QObject *obj)
KontactInterface::Plugin::select
virtual void select()
This function is called when the plugin is selected by the user before the widget of the KPart belong...
Definition: plugin.cpp:283
QDBusConnection::registerService
bool registerService(const QString &serviceName)
KontactInterface::Plugin::virtual_hook
virtual void virtual_hook(int id, void *data)
Virtual hook for BC extension.
Definition: plugin.cpp:414
KontactInterface::Plugin::aboutToSelect
void aboutToSelect()
Called by kontact when the plugin is selected by the user.
Definition: plugin.cpp:274
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:55 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs 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

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