Documentation: Menus.docbook Composer menu Message and correcct formatting.
[kdepim.git] / korganizer / kocore.cpp
blobac1add71d7bcd82d2f277dee3f7e40f2b623be75
1 /*
2 This file is part of KOrganizer.
4 Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 As a special exception, permission is given to link this program
22 with any edition of Qt, and distribute the resulting executable,
23 without including the source code for Qt in the source distribution.
26 #include "kocore.h"
27 #include "koprefs.h"
29 #include <calendarsupport/identitymanager.h>
31 #include <KDebug>
32 #include <KServiceTypeTrader>
33 #include <KXMLGUIFactory>
35 KOCore *KOCore::mSelf = 0;
37 KOCore *KOCore::self()
39 if ( !mSelf ) {
40 mSelf = new KOCore;
43 return mSelf;
46 KOCore::KOCore()
47 : mCalendarDecorationsLoaded( false ), mIdentityManager( 0 )
51 KOCore::~KOCore()
53 mSelf = 0;
56 KService::List KOCore::availablePlugins( const QString &type, int version )
58 QString constraint;
59 if ( version >= 0 ) {
60 constraint =
61 QString( "[X-KDE-PluginInterfaceVersion] == %1" ).arg( QString::number( version ) );
64 return KServiceTypeTrader::self()->query( type, constraint );
67 KService::List KOCore::availablePlugins()
69 return availablePlugins( CalendarSupport::Plugin::serviceType(),
70 CalendarSupport::Plugin::interfaceVersion() );
73 KService::List KOCore::availableCalendarDecorations()
75 return availablePlugins( EventViews::CalendarDecoration::Decoration::serviceType(),
76 EventViews::CalendarDecoration::Decoration::interfaceVersion() );
79 KService::List KOCore::availableParts()
81 return availablePlugins( KOrg::Part::serviceType(), KOrg::Part::interfaceVersion() );
84 KService::List KOCore::availablePrintPlugins()
86 return
87 availablePlugins( KOrg::PrintPlugin::serviceType(), KOrg::PrintPlugin::interfaceVersion() );
90 CalendarSupport::Plugin *KOCore::loadPlugin( KService::Ptr service )
92 kDebug() << service->library();
94 if ( !service->hasServiceType( CalendarSupport::Plugin::serviceType() ) ) {
95 return 0;
98 KPluginLoader loader( *service );
99 KPluginFactory *factory = loader.factory();
101 if ( !factory ) {
102 kDebug() << "Factory creation failed";
103 return 0;
106 CalendarSupport::PluginFactory *pluginFactory =
107 static_cast<CalendarSupport::PluginFactory *>( factory );
109 if ( !pluginFactory ) {
110 kDebug() << "Cast failed";
111 return 0;
114 return pluginFactory->createPluginFactory();
117 CalendarSupport::Plugin *KOCore::loadPlugin( const QString &name )
119 KService::List list = availablePlugins();
120 KService::List::ConstIterator it;
121 for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
122 if ( (*it)->desktopEntryName() == name ) {
123 return loadPlugin( *it );
126 return 0;
129 EventViews::CalendarDecoration::Decoration *KOCore::loadCalendarDecoration( KService::Ptr service )
131 KPluginLoader loader( *service );
132 KPluginFactory *factory = loader.factory();
134 if ( !factory ) {
135 kDebug() << "Factory creation failed";
136 return 0;
139 EventViews::CalendarDecoration::DecorationFactory *pluginFactory =
140 static_cast<EventViews::CalendarDecoration::DecorationFactory *>( factory );
142 if ( !pluginFactory ) {
143 kDebug() << "Cast failed";
144 return 0;
147 return pluginFactory->createPluginFactory();
150 EventViews::CalendarDecoration::Decoration *KOCore::loadCalendarDecoration( const QString &name )
152 KService::List list = availableCalendarDecorations();
153 KService::List::ConstIterator it;
154 for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
155 if ( (*it)->desktopEntryName() == name ) {
156 return loadCalendarDecoration( *it );
159 return 0;
162 KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent )
164 kDebug() << service->library();
166 if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) {
167 return 0;
170 KPluginLoader loader( *service );
171 KPluginFactory *factory = loader.factory();
173 if ( !factory ) {
174 kDebug() << "Factory creation failed";
175 return 0;
178 KOrg::PartFactory *pluginFactory =
179 static_cast<KOrg::PartFactory *>( factory );
181 if ( !pluginFactory ) {
182 kDebug() << "Cast failed";
183 return 0;
186 return pluginFactory->createPluginFactory( parent );
189 KOrg::PrintPlugin *KOCore::loadPrintPlugin( KService::Ptr service )
191 kDebug() << service->library();
193 if ( !service->hasServiceType( KOrg::PrintPlugin::serviceType() ) ) {
194 return 0;
197 KPluginLoader loader( *service );
198 KPluginFactory *factory = loader.factory();
200 if ( !factory ) {
201 kDebug() << "Factory creation failed";
202 return 0;
205 KOrg::PrintPluginFactory *pluginFactory =
206 static_cast<KOrg::PrintPluginFactory *>( factory );
208 if ( !pluginFactory ) {
209 kDebug() << "Cast failed";
210 return 0;
213 return pluginFactory->createPluginFactory();
216 void KOCore::addXMLGUIClient( QWidget *wdg, KXMLGUIClient *guiclient )
218 mXMLGUIClients.insert( wdg, guiclient );
221 void KOCore::removeXMLGUIClient( QWidget *wdg )
223 mXMLGUIClients.remove( wdg );
226 KXMLGUIClient *KOCore::xmlguiClient( QWidget *wdg ) const
228 if ( !wdg ) {
229 return 0;
232 QWidget *topLevel = wdg->topLevelWidget();
233 QMap<QWidget*, KXMLGUIClient*>::ConstIterator it = mXMLGUIClients.find( topLevel );
234 if ( it != mXMLGUIClients.constEnd() ) {
235 return it.value();
238 return 0;
241 KOrg::Part *KOCore::loadPart( const QString &name, KOrg::MainWindow *parent )
243 KService::List list = availableParts();
244 KService::List::ConstIterator it;
245 for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
246 if ( (*it)->desktopEntryName() == name ) {
247 return loadPart( *it, parent );
250 return 0;
253 KOrg::PrintPlugin *KOCore::loadPrintPlugin( const QString &name )
255 KService::List list = availablePrintPlugins();
256 KService::List::ConstIterator it;
257 for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
258 if ( (*it)->desktopEntryName() == name ) {
259 return loadPrintPlugin( *it );
262 return 0;
265 EventViews::CalendarDecoration::Decoration::List KOCore::loadCalendarDecorations()
267 if ( !mCalendarDecorationsLoaded ) {
268 QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
270 mCalendarDecorations.clear();
271 KService::List plugins = availableCalendarDecorations();
272 KService::List::ConstIterator it;
273 for ( it = plugins.constBegin(); it != plugins.constEnd(); ++it ) {
274 if ( (*it)->hasServiceType( EventViews::CalendarDecoration::Decoration::serviceType() ) ) {
275 QString name = (*it)->desktopEntryName();
276 if ( selectedPlugins.contains( name ) ) {
277 EventViews::CalendarDecoration::Decoration *d = loadCalendarDecoration(*it);
278 mCalendarDecorations.append( d );
282 mCalendarDecorationsLoaded = true;
285 return mCalendarDecorations;
288 KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent )
290 KOrg::Part::List parts;
292 QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
294 KService::List plugins = availableParts();
295 KService::List::ConstIterator it;
296 for ( it = plugins.constBegin(); it != plugins.constEnd(); ++it ) {
297 if ( selectedPlugins.contains( (*it)->desktopEntryName() ) ) {
298 KOrg::Part *part = loadPart( *it, parent );
299 if ( part ) {
300 if ( !parent->mainGuiClient() ) {
301 kError() << "parent has no mainGuiClient.";
302 } else {
303 parent->mainGuiClient()->insertChildClient( part );
304 parts.append( part );
309 return parts;
312 KOrg::PrintPlugin::List KOCore::loadPrintPlugins()
314 KOrg::PrintPlugin::List loadedPlugins;
316 EventViews::PrefsPtr viewPrefs = KOPrefs::instance()->eventViewsPreferences();
317 QStringList selectedPlugins = viewPrefs->selectedPlugins();
319 KService::List plugins = availablePrintPlugins();
320 KService::List::ConstIterator it;
321 for ( it = plugins.constBegin(); it != plugins.constEnd(); ++it ) {
322 if ( selectedPlugins.contains( (*it)->desktopEntryName() ) ) {
323 KOrg::PrintPlugin *part = loadPrintPlugin( *it );
324 if ( part ) {
325 loadedPlugins.append( part );
329 return loadedPlugins;
332 void KOCore::unloadPlugins()
334 qDeleteAll( mCalendarDecorations );
335 mCalendarDecorations.clear();
336 mCalendarDecorationsLoaded = false;
339 void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
341 foreach ( KOrg::Part *part, parts ) {
342 parent->mainGuiClient()->removeChildClient( part );
343 delete part;
345 parts.clear();
348 KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
350 KXMLGUIFactory *factory = parent->mainGuiClient()->factory();
351 factory->removeClient( parent->mainGuiClient() );
353 unloadParts( parent, parts );
354 KOrg::Part::List list = loadParts( parent );
356 factory->addClient( parent->mainGuiClient() );
358 return list;
361 void KOCore::reloadPlugins()
363 // TODO: does this still apply?
364 // Plugins should be unloaded, but e.g. komonthview keeps using the old ones
365 unloadPlugins();
366 loadCalendarDecorations();
369 KPIMIdentities::IdentityManager *KOCore::identityManager()
371 if ( !mIdentityManager ) {
372 mIdentityManager = new CalendarSupport::IdentityManager;
374 return mIdentityManager;