kocore.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "kocore.h"
00026
00027 #include "koprefs.h"
00028 #include "koglobals.h"
00029
00030 #include <calendar/plugin.h>
00031 #include <korganizer/part.h>
00032
00033 #include <libkcal/calendarresources.h>
00034 #include <libkcal/resourcelocal.h>
00035
00036 #include <klibloader.h>
00037 #include <kdebug.h>
00038 #include <kconfig.h>
00039 #include <kxmlguifactory.h>
00040 #include <kstandarddirs.h>
00041 #include <klocale.h>
00042
00043 #include <qwidget.h>
00044
00045 KOCore *KOCore::mSelf = 0;
00046
00047 KOCore *KOCore::self()
00048 {
00049 if ( !mSelf ) {
00050 mSelf = new KOCore;
00051 }
00052
00053 return mSelf;
00054 }
00055
00056 KOCore::KOCore()
00057 : mCalendarDecorationsLoaded( false ), mHolidays( 0 ), mXMLGUIClient( 0 ),
00058 mCalendarResources( 0 )
00059 {
00060 }
00061
00062 KOCore::~KOCore()
00063 {
00064 delete mCalendarResources;
00065
00066 mSelf = 0;
00067 }
00068
00069 KTrader::OfferList KOCore::availablePlugins( const QString &type, int version )
00070 {
00071 QString constraint;
00072 if ( version >= 0 ) {
00073 constraint = QString("[X-KDE-PluginInterfaceVersion] == %1")
00074 .arg( QString::number( version ) );
00075 }
00076
00077 return KTrader::self()->query( type, constraint );
00078 }
00079
00080 KTrader::OfferList KOCore::availablePlugins()
00081 {
00082 return availablePlugins( KOrg::Plugin::serviceType(),
00083 KOrg::Plugin::interfaceVersion() );
00084 }
00085
00086 KTrader::OfferList KOCore::availableCalendarDecorations()
00087 {
00088 return availablePlugins( KOrg::CalendarDecoration::serviceType(),
00089 KOrg::CalendarDecoration::interfaceVersion() );
00090 }
00091
00092 KTrader::OfferList KOCore::availableParts()
00093 {
00094 return availablePlugins( KOrg::Part::serviceType(),
00095 KOrg::Part::interfaceVersion() );
00096 }
00097
00098 KOrg::Plugin *KOCore::loadPlugin( KService::Ptr service )
00099 {
00100 kdDebug(5850) << "loadPlugin: library: " << service->library() << endl;
00101
00102 if ( !service->hasServiceType( KOrg::Plugin::serviceType() ) ) {
00103 return 0;
00104 }
00105
00106 KLibFactory *factory = KLibLoader::self()->factory(
00107 service->library().latin1() );
00108
00109 if ( !factory ) {
00110 kdDebug(5850) << "KOCore::loadPlugin(): Factory creation failed" << endl;
00111 return 0;
00112 }
00113
00114 KOrg::PluginFactory *pluginFactory =
00115 static_cast<KOrg::PluginFactory *>( factory );
00116
00117 if ( !pluginFactory ) {
00118 kdDebug(5850) << "KOCore::loadPlugin(): Cast to KOrg::PluginFactory failed" << endl;
00119 return 0;
00120 }
00121
00122 return pluginFactory->create();
00123 }
00124
00125 KOrg::Plugin *KOCore::loadPlugin( const QString &name )
00126 {
00127 KTrader::OfferList list = availablePlugins();
00128 KTrader::OfferList::ConstIterator it;
00129 for( it = list.begin(); it != list.end(); ++it ) {
00130 if ( (*it)->desktopEntryName() == name ) {
00131 return loadPlugin( *it );
00132 }
00133 }
00134 return 0;
00135 }
00136
00137 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration(KService::Ptr service)
00138 {
00139 kdDebug(5850) << "loadCalendarDecoration: library: " << service->library() << endl;
00140
00141 KLibFactory *factory = KLibLoader::self()->factory(service->library().latin1());
00142
00143 if (!factory) {
00144 kdDebug(5850) << "KOCore::loadCalendarDecoration(): Factory creation failed" << endl;
00145 return 0;
00146 }
00147
00148 KOrg::CalendarDecorationFactory *pluginFactory =
00149 static_cast<KOrg::CalendarDecorationFactory *>(factory);
00150
00151 if (!pluginFactory) {
00152 kdDebug(5850) << "KOCore::loadCalendarDecoration(): Cast failed" << endl;
00153 return 0;
00154 }
00155
00156 return pluginFactory->create();
00157 }
00158
00159 KOrg::CalendarDecoration *KOCore::loadCalendarDecoration( const QString &name )
00160 {
00161 KTrader::OfferList list = availableCalendarDecorations();
00162 KTrader::OfferList::ConstIterator it;
00163 for( it = list.begin(); it != list.end(); ++it ) {
00164 if ( (*it)->desktopEntryName() == name ) {
00165 return loadCalendarDecoration( *it );
00166 }
00167 }
00168 return 0;
00169 }
00170
00171 KOrg::Part *KOCore::loadPart( KService::Ptr service, KOrg::MainWindow *parent )
00172 {
00173 kdDebug(5850) << "loadPart: library: " << service->library() << endl;
00174
00175 if ( !service->hasServiceType( KOrg::Part::serviceType() ) ) {
00176 return 0;
00177 }
00178
00179 KLibFactory *factory = KLibLoader::self()->factory(
00180 service->library().latin1() );
00181
00182 if ( !factory ) {
00183 kdDebug(5850) << "KOCore::loadPart(): Factory creation failed" << endl;
00184 return 0;
00185 }
00186
00187 KOrg::PartFactory *pluginFactory =
00188 static_cast<KOrg::PartFactory *>( factory );
00189
00190 if ( !pluginFactory ) {
00191 kdDebug(5850) << "KOCore::loadPart(): Cast failed" << endl;
00192 return 0;
00193 }
00194
00195 return pluginFactory->create( parent );
00196 }
00197
00198 void KOCore::setXMLGUIClient( KXMLGUIClient *guiclient )
00199 {
00200 mXMLGUIClient = guiclient;
00201 }
00202
00203
00204 KOrg::Part *KOCore::loadPart( const QString &name, KOrg::MainWindow *parent )
00205 {
00206 KTrader::OfferList list = availableParts();
00207 KTrader::OfferList::ConstIterator it;
00208 for( it = list.begin(); it != list.end(); ++it ) {
00209 if ( (*it)->desktopEntryName() == name ) {
00210 return loadPart( *it, parent );
00211 }
00212 }
00213 return 0;
00214 }
00215
00216 KOrg::CalendarDecoration::List KOCore::calendarDecorations()
00217 {
00218 if ( !mCalendarDecorationsLoaded ) {
00219 QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00220
00221 mCalendarDecorations.clear();
00222 KTrader::OfferList plugins = availableCalendarDecorations();
00223 KTrader::OfferList::ConstIterator it;
00224 for( it = plugins.begin(); it != plugins.end(); ++it ) {
00225 if ( (*it)->hasServiceType("Calendar/Decoration") ) {
00226 QString name = (*it)->desktopEntryName();
00227 if ( selectedPlugins.find( name ) != selectedPlugins.end() ) {
00228 KOrg::CalendarDecoration *d = loadCalendarDecoration(*it);
00229 mCalendarDecorations.append( d );
00230 if ( name == "holidays" ) mHolidays = d;
00231 }
00232 }
00233 }
00234 mCalendarDecorationsLoaded = true;
00235 }
00236
00237 return mCalendarDecorations;
00238 }
00239
00240 KOrg::Part::List KOCore::loadParts( KOrg::MainWindow *parent )
00241 {
00242 KOrg::Part::List parts;
00243
00244 QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
00245
00246 KTrader::OfferList plugins = availableParts();
00247 KTrader::OfferList::ConstIterator it;
00248 for( it = plugins.begin(); it != plugins.end(); ++it ) {
00249 if ( selectedPlugins.find( (*it)->desktopEntryName() ) !=
00250 selectedPlugins.end() ) {
00251 KOrg::Part *part = loadPart( *it, parent );
00252 if ( part ) {
00253 if ( !parent->mainGuiClient() ) {
00254 kdError() << "KOCore::loadParts(): parent has no mainGuiClient."
00255 << endl;
00256 } else {
00257 parent->mainGuiClient()->insertChildClient( part );
00258 parts.append( part );
00259 }
00260 }
00261 }
00262 }
00263 return parts;
00264 }
00265
00266 void KOCore::unloadPlugins()
00267 {
00268 KOrg::CalendarDecoration *plugin;
00269 for( plugin = mCalendarDecorations.first(); plugin;
00270 plugin = mCalendarDecorations.next() ) {
00271 delete plugin;
00272 }
00273 mCalendarDecorations.clear();
00274 mCalendarDecorationsLoaded = false;
00275 mHolidays = 0;
00276 }
00277
00278 void KOCore::unloadParts( KOrg::MainWindow *parent, KOrg::Part::List &parts )
00279 {
00280 KOrg::Part *part;
00281 for( part = parts.first(); part; part = parts.next() ) {
00282 parent->mainGuiClient()->removeChildClient( part );
00283 delete part;
00284 }
00285 parts.clear();
00286 }
00287
00288 KOrg::Part::List KOCore::reloadParts( KOrg::MainWindow *parent,
00289 KOrg::Part::List &parts )
00290 {
00291 KXMLGUIFactory *factory = parent->mainGuiClient()->factory();
00292 factory->removeClient( parent->mainGuiClient() );
00293
00294 unloadParts( parent, parts );
00295 KOrg::Part::List list = loadParts( parent );
00296
00297 factory->addClient( parent->mainGuiClient() );
00298
00299 return list;
00300 }
00301
00302 void KOCore::reloadPlugins()
00303 {
00304 mCalendarDecorationsLoaded = false;
00305
00306 unloadPlugins();
00307 calendarDecorations();
00308 }
00309
00310 QString KOCore::holiday( const QDate &date )
00311 {
00312 calendarDecorations();
00313 if ( mHolidays ) return mHolidays->shortText( date );
00314 else return QString::null;
00315 }
00316
00317 KCal::CalendarResources *KOCore::calendarResources()
00318 {
00319 if ( !mCalendarResources ) {
00320 mCalendarResources = new KCal::CalendarResources( KOPrefs::instance()->mTimeZoneId );
00321
00322 KCal::CalendarResourceManager *manager = mCalendarResources->resourceManager();
00323
00324 if ( manager->isEmpty() ) {
00325 KConfig *config = KOGlobals::self()->config();
00326 config->setGroup("General");
00327 QString fileName = config->readPathEntry( "Active Calendar" );
00328
00329 QString resourceName;
00330 if ( fileName.isEmpty() ) {
00331 fileName = locateLocal( "data", "korganizer/std.ics" );
00332 resourceName = i18n("Default KOrganizer resource");
00333 } else {
00334 resourceName = i18n("Active Calendar");
00335 }
00336
00337 kdDebug(5850) << "Using as default resource: '" << fileName << "'" << endl;
00338
00339 KCal::ResourceCalendar *defaultResource = new KCal::ResourceLocal( fileName );
00340 defaultResource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
00341 defaultResource->setResourceName( resourceName );
00342
00343 manager->add( defaultResource );
00344 manager->setStandardResource( defaultResource );
00345 }
00346 }
00347
00348 return mCalendarResources;
00349 }
This file is part of the documentation for korganizer Library Version 3.2.2.