korganizer

kocore.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include "kocore.h"
00027 
00028 #include "koprefs.h"
00029 #include "koglobals.h"
00030 #include "koidentitymanager.h"
00031 
00032 #include <calendar/plugin.h>
00033 #include <korganizer/part.h>
00034 
00035 #include <klibloader.h>
00036 #include <kdebug.h>
00037 #include <kconfig.h>
00038 #include <kxmlguifactory.h>
00039 #include <kstandarddirs.h>
00040 #include <klocale.h>
00041 
00042 #include <qwidget.h>
00043 
00044 KOCore *KOCore::mSelf = 0;
00045 
00046 KOCore *KOCore::self()
00047 {
00048   if ( !mSelf ) {
00049     mSelf = new KOCore;
00050   }
00051 
00052   return mSelf;
00053 }
00054 
00055 KOCore::KOCore()
00056   : mCalendarDecorationsLoaded( false ), mIdentityManager( 0 )
00057 {
00058 }
00059 
00060 KOCore::~KOCore()
00061 {
00062   mSelf = 0;
00063 }
00064 
00065 KTrader::OfferList KOCore::availablePlugins( const QString &type, int version )
00066 {
00067   QString constraint;
00068   if ( version >= 0 ) {
00069     constraint = QString("[X-KDE-PluginInterfaceVersion] == %1")
00070                  .arg( QString::number( version ) );
00071   }
00072 
00073   return KTrader::self()->query( type, constraint );
00074 }
00075 
00076 KTrader::OfferList KOCore::availablePlugins()
00077 {
00078   return availablePlugins( KOrg::Plugin::serviceType(),
00079                            KOrg::Plugin::interfaceVersion() );
00080 }
00081 
00082 KTrader::OfferList KOCore::availableCalendarDecorations()
00083 {
00084   return availablePlugins( KOrg::CalendarDecoration::serviceType(),
00085                            KOrg::CalendarDecoration::interfaceVersion() );
00086 }
00087 
00088 KTrader::OfferList KOCore::availableParts()
00089 {
00090   return availablePlugins( KOrg::Part::serviceType(),
00091                            KOrg::Part::interfaceVersion() );
00092 }
00093 
00094 KTrader::OfferList KOCore::availablePrintPlugins()
00095 {
00096   return availablePlugins( KOrg::PrintPlugin::serviceType(),
00097                            KOrg::PrintPlugin::interfaceVersion() );
00098 }
00099 
00100 KOrg::Plugin *KOCore::loadPlugin( KService::Ptr service )
00101 {
00102   kdDebug(5850) << "loadPlugin: library: " << service->library() << endl;
00103 
00104   if ( !service->hasServiceType( KOrg::Plugin::serviceType() ) ) {
00105     return 0;
00106   }
00107 
00108   KLibFactory *factory = KLibLoader::self()->factory(
00109       service->library().latin1() );
00110 
00111   if ( !factory ) {
00112     kdDebug(5850) << "KOCore::loadPlugin(): Factory creation failed" << endl;
00113     return 0;
00114   }
00115 
00116   KOrg::PluginFactory *pluginFactory =
00117       static_cast<KOrg::PluginFactory *>( factory );
00118 
00119   if ( !pluginFactory ) {
00120     kdDebug(5850) << "KOCore::loadPlugin(): Cast to KOrg::PluginFactory failed" << endl;
00121     return 0;
00122   }
00123 
00124   return pluginFactory->create();
00125 }
00126 
00127 KOrg::Plugin *KOCore::loadPlugin( const QString &name )
00128 {
00129   KTrader::OfferList list = availablePlugins();
00130   KTrader::OfferList::ConstIterator it;
00131   for( it = list.begin(); it != list.end(); ++it ) {
00132     if ( (*it)->desktopEntryName() == name ) {
00133       return loadPlugin( *it );
00134     }
00135   }
00136   return 0;
00137 }
00138 
00139 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration(KService::Ptr service)
00140 {
00141   kdDebug(5850) << "loadCalendarDecoration: library: " << service->library() << endl;
00142 
00143   KLibFactory *factory = KLibLoader::self()->factory(service->library().latin1());
00144 
00145   if (!factory) {
00146     kdDebug(5850) << "KOCore::loadCalendarDecoration(): Factory creation failed" << endl;
00147     return 0;
00148   }
00149 
00150   KOrg::CalendarDecorationFactory *pluginFactory =
00151       static_cast<KOrg::CalendarDecorationFactory *>(factory);
00152 
00153   if (!pluginFactory) {
00154     kdDebug(5850) << "KOCore::loadCalendarDecoration(): Cast failed" << endl;
00155     return 0;
00156   }
00157 
00158   return pluginFactory->create();
00159 }
00160 
00161 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration( const QString &name )
00162 {
00163   KTrader::OfferList list = availableCalendarDecorations();
00164   KTrader::OfferList::ConstIterator it;
00165   for( it = list.begin(); it != list.end(); ++it ) {
00166     if ( (*it)->desktopEntryName() == name ) {
00167       return loadCalendarDecoration( *it );
00168     }
00169   }
00170   return 0;
00171 }
00172 
00173 KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent )
00174 {
00175   kdDebug(5850) << "loadPart: library: " << service->library() << endl;
00176 
00177   if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) {
00178     return 0;
00179   }
00180 
00181   KLibFactory *factory = KLibLoader::self()->factory(
00182       service->library().latin1() );
00183 
00184   if ( !factory ) {
00185     kdDebug(5850) << "KOCore::loadPart(): Factory creation failed" << endl;
00186     return 0;
00187   }
00188 
00189   KOrg::PartFactory *pluginFactory =
00190       static_cast<KOrg::PartFactory *>( factory );
00191 
00192   if ( !pluginFactory ) {
00193     kdDebug(5850) << "KOCore::loadPart(): Cast failed" << endl;
00194     return 0;
00195   }
00196 
00197   return pluginFactory->create( parent );
00198 }
00199 
00200 KOrg::PrintPlugin *KOCore::loadPrintPlugin( KService::Ptr service )
00201 {
00202   kdDebug(5850) << "loadPart: print plugin in library: " << service->library() << endl;
00203 
00204   if ( !service->hasServiceType( KOrg::PrintPlugin::serviceType() ) ) {
00205     return 0;
00206   }
00207 
00208   KLibFactory *factory = KLibLoader::self()->factory(
00209       service->library().latin1() );
00210 
00211   if ( !factory ) {
00212     kdDebug(5850) << "KOCore::loadPrintPlugin(): Factory creation failed" << endl;
00213     return 0;
00214   }
00215 
00216   KOrg::PrintPluginFactory *pluginFactory =
00217       static_cast<KOrg::PrintPluginFactory *>( factory );
00218 
00219   if ( !pluginFactory ) {
00220     kdDebug(5850) << "KOCore::loadPrintPlugins(): Cast failed" << endl;
00221     return 0;
00222   }
00223 
00224   return pluginFactory->create();
00225 }
00226 
00227 void KOCore::addXMLGUIClient( QWidget *wdg, KXMLGUIClient *guiclient )
00228 {
00229   mXMLGUIClients.insert( wdg, guiclient );
00230 }
00231 
00232 void KOCore::removeXMLGUIClient( QWidget *wdg )
00233 {
00234   mXMLGUIClients.remove( wdg );
00235 }
00236 
00237 KXMLGUIClient* KOCore::xmlguiClient( QWidget *wdg ) const
00238 {
00239   QWidget *topLevel = wdg->topLevelWidget();
00240   QMap<QWidget*, KXMLGUIClient*>::ConstIterator it = mXMLGUIClients.find( topLevel );
00241   if ( it != mXMLGUIClients.end() )
00242     return it.data();
00243 
00244   return 0;
00245 }
00246 
00247 KOrg::Part *KOCore::loadPart( const QString &name, KOrg::MainWindow *parent )
00248 {
00249   KTrader::OfferList list = availableParts();
00250   KTrader::OfferList::ConstIterator it;
00251   for( it = list.begin(); it != list.end(); ++it ) {
00252     if ( (*it)->desktopEntryName() == name ) {
00253       return loadPart( *it, parent );
00254     }
00255   }
00256   return 0;
00257 }
00258 
00259 KOrg::PrintPlugin *KOCore::loadPrintPlugin( const QString &name )
00260 {
00261   KTrader::OfferList list = availablePrintPlugins();
00262   KTrader::OfferList::ConstIterator it;
00263   for( it = list.begin(); it != list.end(); ++it ) {
00264     if ( (*it)->desktopEntryName() == name ) {
00265       return loadPrintPlugin( *it );
00266     }
00267   }
00268   return 0;
00269 }
00270 
00271 KOrg::CalendarDecoration::List KOCore::calendarDecorations()
00272 {
00273   if ( !mCalendarDecorationsLoaded ) {
00274     QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00275 
00276     mCalendarDecorations.clear();
00277     KTrader::OfferList plugins = availableCalendarDecorations();
00278     KTrader::OfferList::ConstIterator it;
00279     for( it = plugins.begin(); it != plugins.end(); ++it ) {
00280       if ( (*it)->hasServiceType("Calendar/Decoration") ) {
00281         QString name = (*it)->desktopEntryName();
00282         if ( selectedPlugins.find( name ) != selectedPlugins.end() ) {
00283           KOrg::CalendarDecoration *d = loadCalendarDecoration(*it);
00284           mCalendarDecorations.append( d );
00285         }
00286       }
00287     }
00288     mCalendarDecorationsLoaded = true;
00289   }
00290 
00291   return mCalendarDecorations;
00292 }
00293 
00294 KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent )
00295 {
00296   KOrg::Part::List parts;
00297 
00298   QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00299 
00300   KTrader::OfferList plugins = availableParts();
00301   KTrader::OfferList::ConstIterator it;
00302   for( it = plugins.begin(); it != plugins.end(); ++it ) {
00303     if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
00304                                selectedPlugins.end() ) {
00305       KOrg::Part *part = loadPart( *it, parent );
00306       if ( part ) {
00307         if ( !parent->mainGuiClient() ) {
00308           kdError() << "KOCore::loadParts(): parent has no mainGuiClient."
00309                     << endl;
00310         } else {
00311           parent->mainGuiClient()->insertChildClient( part );
00312           parts.append( part );
00313         }
00314       }
00315     }
00316   }
00317   return parts;
00318 }
00319 
00320 KOrg::PrintPlugin::List KOCore::loadPrintPlugins()
00321 {
00322   KOrg::PrintPlugin::List loadedPlugins;
00323 
00324   QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00325 
00326   KTrader::OfferList plugins = availablePrintPlugins();
00327   KTrader::OfferList::ConstIterator it;
00328   for( it = plugins.begin(); it != plugins.end(); ++it ) {
00329     if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
00330                                selectedPlugins.end() ) {
00331       KOrg::PrintPlugin *part = loadPrintPlugin( *it );
00332       if ( part ) loadedPlugins.append( part );
00333     }
00334   }
00335   return loadedPlugins;
00336 }
00337 
00338 void KOCore::unloadPlugins()
00339 {
00340   KOrg::CalendarDecoration *plugin;
00341   for( plugin = mCalendarDecorations.first(); plugin;
00342        plugin = mCalendarDecorations.next() ) {
00343     delete plugin;
00344   }
00345   mCalendarDecorations.clear();
00346   mCalendarDecorationsLoaded = false;
00347 }
00348 
00349 void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
00350 {
00351   KOrg::Part *part;
00352   for( part = parts.first(); part; part = parts.next() ) {
00353     parent->mainGuiClient()->removeChildClient( part );
00354     delete part;
00355   }
00356   parts.clear();
00357 }
00358 
00359 KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent,
00360                                       KOrg::Part::List &parts )
00361 {
00362   KXMLGUIFactory *factory = parent->mainGuiClient()->factory();
00363   factory->removeClient( parent->mainGuiClient() );
00364 
00365   unloadParts( parent, parts );
00366   KOrg::Part::List list = loadParts( parent );
00367 
00368   factory->addClient( parent->mainGuiClient() );
00369 
00370   return list;
00371 }
00372 
00373 void KOCore::reloadPlugins()
00374 {
00375   mCalendarDecorationsLoaded = false;
00376 // Plugins should be unloaded, but e.g. komonthview keeps using the old ones
00377   unloadPlugins();
00378   calendarDecorations();
00379 }
00380 
00381 KPIM::IdentityManager* KOCore::identityManager()
00382 {
00383   if ( !mIdentityManager )
00384     mIdentityManager = new KOrg::IdentityManager;
00385   return mIdentityManager;
00386 }
KDE Home | KDE Accessibility Home | Description of Access Keys