From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- korganizer/kocore.cpp | 386 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 386 insertions(+) create mode 100644 korganizer/kocore.cpp (limited to 'korganizer/kocore.cpp') diff --git a/korganizer/kocore.cpp b/korganizer/kocore.cpp new file mode 100644 index 000000000..6aca769b5 --- /dev/null +++ b/korganizer/kocore.cpp @@ -0,0 +1,386 @@ +/* + This file is part of KOrganizer. + + Copyright (c) 2001,2003 Cornelius Schumacher + Copyright (C) 2003-2004 Reinhold Kainhofer + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of Qt, and distribute the resulting executable, + without including the source code for Qt in the source distribution. +*/ + +#include "kocore.h" + +#include "koprefs.h" +#include "koglobals.h" +#include "koidentitymanager.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +KOCore *KOCore::mSelf = 0; + +KOCore *KOCore::self() +{ + if ( !mSelf ) { + mSelf = new KOCore; + } + + return mSelf; +} + +KOCore::KOCore() + : mCalendarDecorationsLoaded( false ), mIdentityManager( 0 ) +{ +} + +KOCore::~KOCore() +{ + mSelf = 0; +} + +KTrader::OfferList KOCore::availablePlugins( const QString &type, int version ) +{ + QString constraint; + if ( version >= 0 ) { + constraint = QString("[X-KDE-PluginInterfaceVersion] == %1") + .arg( QString::number( version ) ); + } + + return KTrader::self()->query( type, constraint ); +} + +KTrader::OfferList KOCore::availablePlugins() +{ + return availablePlugins( KOrg::Plugin::serviceType(), + KOrg::Plugin::interfaceVersion() ); +} + +KTrader::OfferList KOCore::availableCalendarDecorations() +{ + return availablePlugins( KOrg::CalendarDecoration::serviceType(), + KOrg::CalendarDecoration::interfaceVersion() ); +} + +KTrader::OfferList KOCore::availableParts() +{ + return availablePlugins( KOrg::Part::serviceType(), + KOrg::Part::interfaceVersion() ); +} + +KTrader::OfferList KOCore::availablePrintPlugins() +{ + return availablePlugins( KOrg::PrintPlugin::serviceType(), + KOrg::PrintPlugin::interfaceVersion() ); +} + +KOrg::Plugin *KOCore::loadPlugin( KService::Ptr service ) +{ + kdDebug(5850) << "loadPlugin: library: " << service->library() << endl; + + if ( !service->hasServiceType( KOrg::Plugin::serviceType() ) ) { + return 0; + } + + KLibFactory *factory = KLibLoader::self()->factory( + service->library().latin1() ); + + if ( !factory ) { + kdDebug(5850) << "KOCore::loadPlugin(): Factory creation failed" << endl; + return 0; + } + + KOrg::PluginFactory *pluginFactory = + static_cast( factory ); + + if ( !pluginFactory ) { + kdDebug(5850) << "KOCore::loadPlugin(): Cast to KOrg::PluginFactory failed" << endl; + return 0; + } + + return pluginFactory->create(); +} + +KOrg::Plugin *KOCore::loadPlugin( const QString &name ) +{ + KTrader::OfferList list = availablePlugins(); + KTrader::OfferList::ConstIterator it; + for( it = list.begin(); it != list.end(); ++it ) { + if ( (*it)->desktopEntryName() == name ) { + return loadPlugin( *it ); + } + } + return 0; +} + +KOrg::CalendarDecoration *KOCore::loadCalendarDecoration(KService::Ptr service) +{ + kdDebug(5850) << "loadCalendarDecoration: library: " << service->library() << endl; + + KLibFactory *factory = KLibLoader::self()->factory(service->library().latin1()); + + if (!factory) { + kdDebug(5850) << "KOCore::loadCalendarDecoration(): Factory creation failed" << endl; + return 0; + } + + KOrg::CalendarDecorationFactory *pluginFactory = + static_cast(factory); + + if (!pluginFactory) { + kdDebug(5850) << "KOCore::loadCalendarDecoration(): Cast failed" << endl; + return 0; + } + + return pluginFactory->create(); +} + +KOrg::CalendarDecoration *KOCore::loadCalendarDecoration( const QString &name ) +{ + KTrader::OfferList list = availableCalendarDecorations(); + KTrader::OfferList::ConstIterator it; + for( it = list.begin(); it != list.end(); ++it ) { + if ( (*it)->desktopEntryName() == name ) { + return loadCalendarDecoration( *it ); + } + } + return 0; +} + +KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent ) +{ + kdDebug(5850) << "loadPart: library: " << service->library() << endl; + + if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) { + return 0; + } + + KLibFactory *factory = KLibLoader::self()->factory( + service->library().latin1() ); + + if ( !factory ) { + kdDebug(5850) << "KOCore::loadPart(): Factory creation failed" << endl; + return 0; + } + + KOrg::PartFactory *pluginFactory = + static_cast( factory ); + + if ( !pluginFactory ) { + kdDebug(5850) << "KOCore::loadPart(): Cast failed" << endl; + return 0; + } + + return pluginFactory->create( parent ); +} + +KOrg::PrintPlugin *KOCore::loadPrintPlugin( KService::Ptr service ) +{ + kdDebug(5850) << "loadPart: print plugin in library: " << service->library() << endl; + + if ( !service->hasServiceType( KOrg::PrintPlugin::serviceType() ) ) { + return 0; + } + + KLibFactory *factory = KLibLoader::self()->factory( + service->library().latin1() ); + + if ( !factory ) { + kdDebug(5850) << "KOCore::loadPrintPlugin(): Factory creation failed" << endl; + return 0; + } + + KOrg::PrintPluginFactory *pluginFactory = + static_cast( factory ); + + if ( !pluginFactory ) { + kdDebug(5850) << "KOCore::loadPrintPlugins(): Cast failed" << endl; + return 0; + } + + return pluginFactory->create(); +} + +void KOCore::addXMLGUIClient( QWidget *wdg, KXMLGUIClient *guiclient ) +{ + mXMLGUIClients.insert( wdg, guiclient ); +} + +void KOCore::removeXMLGUIClient( QWidget *wdg ) +{ + mXMLGUIClients.remove( wdg ); +} + +KXMLGUIClient* KOCore::xmlguiClient( QWidget *wdg ) const +{ + QWidget *topLevel = wdg->topLevelWidget(); + QMap::ConstIterator it = mXMLGUIClients.find( topLevel ); + if ( it != mXMLGUIClients.end() ) + return it.data(); + + return 0; +} + +KOrg::Part *KOCore::loadPart( const QString &name, KOrg::MainWindow *parent ) +{ + KTrader::OfferList list = availableParts(); + KTrader::OfferList::ConstIterator it; + for( it = list.begin(); it != list.end(); ++it ) { + if ( (*it)->desktopEntryName() == name ) { + return loadPart( *it, parent ); + } + } + return 0; +} + +KOrg::PrintPlugin *KOCore::loadPrintPlugin( const QString &name ) +{ + KTrader::OfferList list = availablePrintPlugins(); + KTrader::OfferList::ConstIterator it; + for( it = list.begin(); it != list.end(); ++it ) { + if ( (*it)->desktopEntryName() == name ) { + return loadPrintPlugin( *it ); + } + } + return 0; +} + +KOrg::CalendarDecoration::List KOCore::calendarDecorations() +{ + if ( !mCalendarDecorationsLoaded ) { + QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins; + + mCalendarDecorations.clear(); + KTrader::OfferList plugins = availableCalendarDecorations(); + KTrader::OfferList::ConstIterator it; + for( it = plugins.begin(); it != plugins.end(); ++it ) { + if ( (*it)->hasServiceType("Calendar/Decoration") ) { + QString name = (*it)->desktopEntryName(); + if ( selectedPlugins.find( name ) != selectedPlugins.end() ) { + KOrg::CalendarDecoration *d = loadCalendarDecoration(*it); + mCalendarDecorations.append( d ); + } + } + } + mCalendarDecorationsLoaded = true; + } + + return mCalendarDecorations; +} + +KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent ) +{ + KOrg::Part::List parts; + + QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins; + + KTrader::OfferList plugins = availableParts(); + KTrader::OfferList::ConstIterator it; + for( it = plugins.begin(); it != plugins.end(); ++it ) { + if ( selectedPlugins.find( (*it)->desktopEntryName() ) != + selectedPlugins.end() ) { + KOrg::Part *part = loadPart( *it, parent ); + if ( part ) { + if ( !parent->mainGuiClient() ) { + kdError() << "KOCore::loadParts(): parent has no mainGuiClient." + << endl; + } else { + parent->mainGuiClient()->insertChildClient( part ); + parts.append( part ); + } + } + } + } + return parts; +} + +KOrg::PrintPlugin::List KOCore::loadPrintPlugins() +{ + KOrg::PrintPlugin::List loadedPlugins; + + QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins; + + KTrader::OfferList plugins = availablePrintPlugins(); + KTrader::OfferList::ConstIterator it; + for( it = plugins.begin(); it != plugins.end(); ++it ) { + if ( selectedPlugins.find( (*it)->desktopEntryName() ) != + selectedPlugins.end() ) { + KOrg::PrintPlugin *part = loadPrintPlugin( *it ); + if ( part ) loadedPlugins.append( part ); + } + } + return loadedPlugins; +} + +void KOCore::unloadPlugins() +{ + KOrg::CalendarDecoration *plugin; + for( plugin = mCalendarDecorations.first(); plugin; + plugin = mCalendarDecorations.next() ) { + delete plugin; + } + mCalendarDecorations.clear(); + mCalendarDecorationsLoaded = false; +} + +void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts ) +{ + KOrg::Part *part; + for( part = parts.first(); part; part = parts.next() ) { + parent->mainGuiClient()->removeChildClient( part ); + delete part; + } + parts.clear(); +} + +KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent, + KOrg::Part::List &parts ) +{ + KXMLGUIFactory *factory = parent->mainGuiClient()->factory(); + factory->removeClient( parent->mainGuiClient() ); + + unloadParts( parent, parts ); + KOrg::Part::List list = loadParts( parent ); + + factory->addClient( parent->mainGuiClient() ); + + return list; +} + +void KOCore::reloadPlugins() +{ + mCalendarDecorationsLoaded = false; +// Plugins should be unloaded, but e.g. komonthview keeps using the old ones + unloadPlugins(); + calendarDecorations(); +} + +KPIM::IdentityManager* KOCore::identityManager() +{ + if ( !mIdentityManager ) + mIdentityManager = new KOrg::IdentityManager; + return mIdentityManager; +} -- cgit v1.2.1