korganizer

kocore.cpp
1/*
2 This file is part of KOrganizer.
3
4 Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 As a special exception, permission is given to link this program
22 with any edition of TQt, and distribute the resulting executable,
23 without including the source code for TQt in the source distribution.
24*/
25
26#include "kocore.h"
27
28#include "koprefs.h"
29#include "koglobals.h"
30#include "koidentitymanager.h"
31
32#include <calendar/plugin.h>
33#include <korganizer/part.h>
34
35#include <klibloader.h>
36#include <kdebug.h>
37#include <tdeconfig.h>
38#include <kxmlguifactory.h>
39#include <tdestandarddirs.h>
40#include <tdelocale.h>
41
42#include <tqwidget.h>
43
44KOCore *KOCore::mSelf = 0;
45
46KOCore *KOCore::self()
47{
48 if ( !mSelf ) {
49 mSelf = new KOCore;
50 }
51
52 return mSelf;
53}
54
55KOCore::KOCore()
56 : mCalendarDecorationsLoaded( false ), mIdentityManager( 0 )
57{
58}
59
60KOCore::~KOCore()
61{
62 mSelf = 0;
63}
64
65TDETrader::OfferList KOCore::availablePlugins( const TQString &type, int version )
66{
67 TQString constraint;
68 if ( version >= 0 ) {
69 constraint = TQString("[X-TDE-PluginInterfaceVersion] == %1")
70 .arg( TQString::number( version ) );
71 }
72
73 return TDETrader::self()->query( type, constraint );
74}
75
76TDETrader::OfferList KOCore::availablePlugins()
77{
78 return availablePlugins( KOrg::Plugin::serviceType(),
79 KOrg::Plugin::interfaceVersion() );
80}
81
82TDETrader::OfferList KOCore::availableCalendarDecorations()
83{
84 return availablePlugins( KOrg::CalendarDecoration::serviceType(),
85 KOrg::CalendarDecoration::interfaceVersion() );
86}
87
88TDETrader::OfferList KOCore::availableParts()
89{
90 return availablePlugins( KOrg::Part::serviceType(),
91 KOrg::Part::interfaceVersion() );
92}
93
94TDETrader::OfferList KOCore::availablePrintPlugins()
95{
96 return availablePlugins( KOrg::PrintPlugin::serviceType(),
97 KOrg::PrintPlugin::interfaceVersion() );
98}
99
100KOrg::Plugin *KOCore::loadPlugin( KService::Ptr service )
101{
102 kdDebug(5850) << "loadPlugin: library: " << service->library() << endl;
103
104 if ( !service->hasServiceType( KOrg::Plugin::serviceType() ) ) {
105 return 0;
106 }
107
108 KLibFactory *factory = KLibLoader::self()->factory(
109 service->library().latin1() );
110
111 if ( !factory ) {
112 kdDebug(5850) << "KOCore::loadPlugin(): Factory creation failed" << endl;
113 return 0;
114 }
115
116 KOrg::PluginFactory *pluginFactory =
117 static_cast<KOrg::PluginFactory *>( factory );
118
119 if ( !pluginFactory ) {
120 kdDebug(5850) << "KOCore::loadPlugin(): Cast to KOrg::PluginFactory failed" << endl;
121 return 0;
122 }
123
124 return pluginFactory->create();
125}
126
127KOrg::Plugin *KOCore::loadPlugin( const TQString &name )
128{
129 TDETrader::OfferList list = availablePlugins();
130 TDETrader::OfferList::ConstIterator it;
131 for( it = list.begin(); it != list.end(); ++it ) {
132 if ( (*it)->desktopEntryName() == name ) {
133 return loadPlugin( *it );
134 }
135 }
136 return 0;
137}
138
139KOrg::CalendarDecoration *KOCore::loadCalendarDecoration(KService::Ptr service)
140{
141 kdDebug(5850) << "loadCalendarDecoration: library: " << service->library() << endl;
142
143 KLibFactory *factory = KLibLoader::self()->factory(service->library().latin1());
144
145 if (!factory) {
146 kdDebug(5850) << "KOCore::loadCalendarDecoration(): Factory creation failed" << endl;
147 return 0;
148 }
149
150 KOrg::CalendarDecorationFactory *pluginFactory =
151 static_cast<KOrg::CalendarDecorationFactory *>(factory);
152
153 if (!pluginFactory) {
154 kdDebug(5850) << "KOCore::loadCalendarDecoration(): Cast failed" << endl;
155 return 0;
156 }
157
158 return pluginFactory->create();
159}
160
161KOrg::CalendarDecoration *KOCore::loadCalendarDecoration( const TQString &name )
162{
163 TDETrader::OfferList list = availableCalendarDecorations();
164 TDETrader::OfferList::ConstIterator it;
165 for( it = list.begin(); it != list.end(); ++it ) {
166 if ( (*it)->desktopEntryName() == name ) {
167 return loadCalendarDecoration( *it );
168 }
169 }
170 return 0;
171}
172
173KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent )
174{
175 kdDebug(5850) << "loadPart: library: " << service->library() << endl;
176
177 if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) {
178 return 0;
179 }
180
181 KLibFactory *factory = KLibLoader::self()->factory(
182 service->library().latin1() );
183
184 if ( !factory ) {
185 kdDebug(5850) << "KOCore::loadPart(): Factory creation failed" << endl;
186 return 0;
187 }
188
189 KOrg::PartFactory *pluginFactory =
190 static_cast<KOrg::PartFactory *>( factory );
191
192 if ( !pluginFactory ) {
193 kdDebug(5850) << "KOCore::loadPart(): Cast failed" << endl;
194 return 0;
195 }
196
197 return pluginFactory->create( parent );
198}
199
200KOrg::PrintPlugin *KOCore::loadPrintPlugin( KService::Ptr service )
201{
202 kdDebug(5850) << "loadPart: print plugin in library: " << service->library() << endl;
203
204 if ( !service->hasServiceType( KOrg::PrintPlugin::serviceType() ) ) {
205 return 0;
206 }
207
208 KLibFactory *factory = KLibLoader::self()->factory(
209 service->library().latin1() );
210
211 if ( !factory ) {
212 kdDebug(5850) << "KOCore::loadPrintPlugin(): Factory creation failed" << endl;
213 return 0;
214 }
215
216 KOrg::PrintPluginFactory *pluginFactory =
217 static_cast<KOrg::PrintPluginFactory *>( factory );
218
219 if ( !pluginFactory ) {
220 kdDebug(5850) << "KOCore::loadPrintPlugins(): Cast failed" << endl;
221 return 0;
222 }
223
224 return pluginFactory->create();
225}
226
227void KOCore::addXMLGUIClient( TQWidget *wdg, KXMLGUIClient *guiclient )
228{
229 mXMLGUIClients.insert( wdg, guiclient );
230}
231
232void KOCore::removeXMLGUIClient( TQWidget *wdg )
233{
234 mXMLGUIClients.remove( wdg );
235}
236
237KXMLGUIClient* KOCore::xmlguiClient( TQWidget *wdg ) const
238{
239 TQWidget *topLevel = wdg->topLevelWidget();
240 TQMap<TQWidget*, KXMLGUIClient*>::ConstIterator it = mXMLGUIClients.find( topLevel );
241 if ( it != mXMLGUIClients.end() )
242 return it.data();
243
244 return 0;
245}
246
247KOrg::Part *KOCore::loadPart( const TQString &name, KOrg::MainWindow *parent )
248{
249 TDETrader::OfferList list = availableParts();
250 TDETrader::OfferList::ConstIterator it;
251 for( it = list.begin(); it != list.end(); ++it ) {
252 if ( (*it)->desktopEntryName() == name ) {
253 return loadPart( *it, parent );
254 }
255 }
256 return 0;
257}
258
259KOrg::PrintPlugin *KOCore::loadPrintPlugin( const TQString &name )
260{
261 TDETrader::OfferList list = availablePrintPlugins();
262 TDETrader::OfferList::ConstIterator it;
263 for( it = list.begin(); it != list.end(); ++it ) {
264 if ( (*it)->desktopEntryName() == name ) {
265 return loadPrintPlugin( *it );
266 }
267 }
268 return 0;
269}
270
271KOrg::CalendarDecoration::List KOCore::calendarDecorations()
272{
273 if ( !mCalendarDecorationsLoaded ) {
274 TQStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
275
276 mCalendarDecorations.clear();
277 TDETrader::OfferList plugins = availableCalendarDecorations();
278 TDETrader::OfferList::ConstIterator it;
279 for( it = plugins.begin(); it != plugins.end(); ++it ) {
280 if ( (*it)->hasServiceType("Calendar/Decoration") ) {
281 TQString name = (*it)->desktopEntryName();
282 if ( selectedPlugins.find( name ) != selectedPlugins.end() ) {
283 KOrg::CalendarDecoration *d = loadCalendarDecoration(*it);
284 mCalendarDecorations.append( d );
285 }
286 }
287 }
288 mCalendarDecorationsLoaded = true;
289 }
290
291 return mCalendarDecorations;
292}
293
294KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent )
295{
296 KOrg::Part::List parts;
297
298 TQStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
299
300 TDETrader::OfferList plugins = availableParts();
301 TDETrader::OfferList::ConstIterator it;
302 for( it = plugins.begin(); it != plugins.end(); ++it ) {
303 if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
304 selectedPlugins.end() ) {
305 KOrg::Part *part = loadPart( *it, parent );
306 if ( part ) {
307 if ( !parent->mainGuiClient() ) {
308 kdError() << "KOCore::loadParts(): parent has no mainGuiClient."
309 << endl;
310 } else {
311 parent->mainGuiClient()->insertChildClient( part );
312 parts.append( part );
313 }
314 }
315 }
316 }
317 return parts;
318}
319
320KOrg::PrintPlugin::List KOCore::loadPrintPlugins()
321{
322 KOrg::PrintPlugin::List loadedPlugins;
323
324 TQStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
325
326 TDETrader::OfferList plugins = availablePrintPlugins();
327 TDETrader::OfferList::ConstIterator it;
328 for( it = plugins.begin(); it != plugins.end(); ++it ) {
329 if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
330 selectedPlugins.end() ) {
331 KOrg::PrintPlugin *part = loadPrintPlugin( *it );
332 if ( part ) loadedPlugins.append( part );
333 }
334 }
335 return loadedPlugins;
336}
337
338void KOCore::unloadPlugins()
339{
341 for( plugin = mCalendarDecorations.first(); plugin;
342 plugin = mCalendarDecorations.next() ) {
343 delete plugin;
344 }
345 mCalendarDecorations.clear();
346 mCalendarDecorationsLoaded = false;
347}
348
349void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
350{
351 KOrg::Part *part;
352 for( part = parts.first(); part; part = parts.next() ) {
353 parent->mainGuiClient()->removeChildClient( part );
354 delete part;
355 }
356 parts.clear();
357}
358
359KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent,
360 KOrg::Part::List &parts )
361{
362 KXMLGUIFactory *factory = parent->mainGuiClient()->factory();
363 factory->removeClient( parent->mainGuiClient() );
364
365 unloadParts( parent, parts );
366 KOrg::Part::List list = loadParts( parent );
367
368 factory->addClient( parent->mainGuiClient() );
369
370 return list;
371}
372
373void KOCore::reloadPlugins()
374{
375 mCalendarDecorationsLoaded = false;
376// Plugins should be unloaded, but e.g. komonthview keeps using the old ones
377 unloadPlugins();
378 calendarDecorations();
379}
380
381KPIM::IdentityManager* KOCore::identityManager()
382{
383 if ( !mIdentityManager )
384 mIdentityManager = new KOrg::IdentityManager;
385 return mIdentityManager;
386}
This class provides the interface for a date dependent decoration.
interface for korganizer main window
Definition: mainwindow.h:41
virtual KXMLGUIClient * mainGuiClient()=0
Return XML GUI client of this main window.
Base class for KOrganizer printing classes.
Definition: printplugin.h:52