korganizer Library API Documentation

kocore.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "kocore.h"
00026 
00027 #include "koprefs.h"
00028 #include "koglobals.h"
00029 #include "koidentitymanager.h"
00030 
00031 #include <calendar/plugin.h>
00032 #include <korganizer/part.h>
00033 
00034 #include <klibloader.h>
00035 #include <kdebug.h>
00036 #include <kconfig.h>
00037 #include <kxmlguifactory.h>
00038 #include <kstandarddirs.h>
00039 #include <klocale.h>
00040 
00041 #include <qwidget.h>
00042 
00043 KOCore *KOCore::mSelf = 0;
00044 
00045 KOCore *KOCore::self()
00046 {
00047   if ( !mSelf ) {
00048     mSelf = new KOCore;
00049   }
00050 
00051   return mSelf;
00052 }
00053 
00054 KOCore::KOCore()
00055   : mCalendarDecorationsLoaded( false ), mHolidays( 0 ), mXMLGUIClient( 0 ),
00056     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 KOrg::Plugin *KOCore::loadPlugin( KService::Ptr service )
00095 {
00096   kdDebug(5850) << "loadPlugin: library: " << service->library() << endl;
00097 
00098   if ( !service->hasServiceType( KOrg::Plugin::serviceType() ) ) {
00099     return 0;
00100   }
00101 
00102   KLibFactory *factory = KLibLoader::self()->factory(
00103       service->library().latin1() );
00104 
00105   if ( !factory ) {
00106     kdDebug(5850) << "KOCore::loadPlugin(): Factory creation failed" << endl;
00107     return 0;
00108   }
00109 
00110   KOrg::PluginFactory *pluginFactory =
00111       static_cast<KOrg::PluginFactory *>( factory );
00112 
00113   if ( !pluginFactory ) {
00114     kdDebug(5850) << "KOCore::loadPlugin(): Cast to KOrg::PluginFactory failed" << endl;
00115     return 0;
00116   }
00117 
00118   return pluginFactory->create();
00119 }
00120 
00121 KOrg::Plugin *KOCore::loadPlugin( const QString &name )
00122 {
00123   KTrader::OfferList list = availablePlugins();
00124   KTrader::OfferList::ConstIterator it;
00125   for( it = list.begin(); it != list.end(); ++it ) {
00126     if ( (*it)->desktopEntryName() == name ) {
00127       return loadPlugin( *it );
00128     }
00129   }
00130   return 0;
00131 }
00132 
00133 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration(KService::Ptr service)
00134 {
00135   kdDebug(5850) << "loadCalendarDecoration: library: " << service->library() << endl;
00136 
00137   KLibFactory *factory = KLibLoader::self()->factory(service->library().latin1());
00138 
00139   if (!factory) {
00140     kdDebug(5850) << "KOCore::loadCalendarDecoration(): Factory creation failed" << endl;
00141     return 0;
00142   }
00143 
00144   KOrg::CalendarDecorationFactory *pluginFactory =
00145       static_cast<KOrg::CalendarDecorationFactory *>(factory);
00146 
00147   if (!pluginFactory) {
00148     kdDebug(5850) << "KOCore::loadCalendarDecoration(): Cast failed" << endl;
00149     return 0;
00150   }
00151 
00152   return pluginFactory->create();
00153 }
00154 
00155 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration( const QString &name )
00156 {
00157   KTrader::OfferList list = availableCalendarDecorations();
00158   KTrader::OfferList::ConstIterator it;
00159   for( it = list.begin(); it != list.end(); ++it ) {
00160     if ( (*it)->desktopEntryName() == name ) {
00161       return loadCalendarDecoration( *it );
00162     }
00163   }
00164   return 0;
00165 }
00166 
00167 KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent )
00168 {
00169   kdDebug(5850) << "loadPart: library: " << service->library() << endl;
00170 
00171   if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) {
00172     return 0;
00173   }
00174 
00175   KLibFactory *factory = KLibLoader::self()->factory(
00176       service->library().latin1() );
00177 
00178   if ( !factory ) {
00179     kdDebug(5850) << "KOCore::loadPart(): Factory creation failed" << endl;
00180     return 0;
00181   }
00182 
00183   KOrg::PartFactory *pluginFactory =
00184       static_cast<KOrg::PartFactory *>( factory );
00185 
00186   if ( !pluginFactory ) {
00187     kdDebug(5850) << "KOCore::loadPart(): Cast failed" << endl;
00188     return 0;
00189   }
00190 
00191   return pluginFactory->create( parent );
00192 }
00193 
00194 void KOCore::setXMLGUIClient( KXMLGUIClient *guiclient )
00195 {
00196   mXMLGUIClient = guiclient;
00197 }
00198 
00199 
00200 KOrg::Part *KOCore::loadPart( const QString &name, KOrg::MainWindow *parent )
00201 {
00202   KTrader::OfferList list = availableParts();
00203   KTrader::OfferList::ConstIterator it;
00204   for( it = list.begin(); it != list.end(); ++it ) {
00205     if ( (*it)->desktopEntryName() == name ) {
00206       return loadPart( *it, parent );
00207     }
00208   }
00209   return 0;
00210 }
00211 
00212 KOrg::CalendarDecoration::List KOCore::calendarDecorations()
00213 {
00214   if ( !mCalendarDecorationsLoaded ) {
00215     QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00216 
00217     mCalendarDecorations.clear();
00218     KTrader::OfferList plugins = availableCalendarDecorations();
00219     KTrader::OfferList::ConstIterator it;
00220     for( it = plugins.begin(); it != plugins.end(); ++it ) {
00221       if ( (*it)->hasServiceType("Calendar/Decoration") ) {
00222         QString name = (*it)->desktopEntryName();
00223         if ( selectedPlugins.find( name ) != selectedPlugins.end() ) {
00224           KOrg::CalendarDecoration *d = loadCalendarDecoration(*it);
00225           mCalendarDecorations.append( d );
00226           if ( name == "holidays" ) mHolidays = d;
00227         }
00228       }
00229     }
00230     mCalendarDecorationsLoaded = true;
00231   }
00232 
00233   return mCalendarDecorations;
00234 }
00235 
00236 KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent )
00237 {
00238   KOrg::Part::List parts;
00239 
00240   QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00241 
00242   KTrader::OfferList plugins = availableParts();
00243   KTrader::OfferList::ConstIterator it;
00244   for( it = plugins.begin(); it != plugins.end(); ++it ) {
00245     if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
00246                                selectedPlugins.end() ) {
00247       KOrg::Part *part = loadPart( *it, parent );
00248       if ( part ) {
00249         if ( !parent->mainGuiClient() ) {
00250           kdError() << "KOCore::loadParts(): parent has no mainGuiClient."
00251                     << endl;
00252         } else {
00253           parent->mainGuiClient()->insertChildClient( part );
00254           parts.append( part );
00255         }
00256       }
00257     }
00258   }
00259   return parts;
00260 }
00261 
00262 void KOCore::unloadPlugins()
00263 {
00264   KOrg::CalendarDecoration *plugin;
00265   for( plugin = mCalendarDecorations.first(); plugin;
00266        plugin = mCalendarDecorations.next() ) {
00267     delete plugin;
00268   }
00269   mCalendarDecorations.clear();
00270   mCalendarDecorationsLoaded = false;
00271   mHolidays = 0;
00272 }
00273 
00274 void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
00275 {
00276   KOrg::Part *part;
00277   for( part = parts.first(); part; part = parts.next() ) {
00278     parent->mainGuiClient()->removeChildClient( part );
00279     delete part;
00280   }
00281   parts.clear();
00282 }
00283 
00284 KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent,
00285                                       KOrg::Part::List &parts )
00286 {
00287   KXMLGUIFactory *factory = parent->mainGuiClient()->factory();
00288   factory->removeClient( parent->mainGuiClient() );
00289 
00290   unloadParts( parent, parts );
00291   KOrg::Part::List list = loadParts( parent );
00292 
00293   factory->addClient( parent->mainGuiClient() );
00294 
00295   return list;
00296 }
00297 
00298 void KOCore::reloadPlugins()
00299 {
00300   mCalendarDecorationsLoaded = false;
00301 // Plugins should be unloaded, but e.g. komonthview keeps using the old ones
00302   unloadPlugins();
00303   calendarDecorations();
00304 }
00305 
00306 QString KOCore::holiday( const QDate &date )
00307 {
00308   calendarDecorations();
00309   if ( mHolidays ) return mHolidays->shortText( date );
00310   else return QString::null;
00311 }
00312 
00313 bool KOCore::isWorkDay( const QDate &date )
00314 {
00315   int mask( ~( KOPrefs::instance()->mWorkWeekMask ) );
00316 
00317   bool nonWorkDay = ( mask & ( 1 << ( date.dayOfWeek() - 1 ) ) );
00318 
00319   nonWorkDay = nonWorkDay || ( KOPrefs::instance()->mExcludeHolidays &&
00320                                !holiday( date ).isEmpty() );
00321 
00322   return !nonWorkDay;
00323 }
00324 
00325 KPIM::IdentityManager* KOCore::identityManager()
00326 {
00327   if ( !mIdentityManager )
00328     mIdentityManager = new KOrg::IdentityManager;
00329   return mIdentityManager;
00330 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jan 31 15:55:48 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003