core.cpp
00001 /* 00002 This file is part of KDE Kontact. 00003 00004 Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org> 00005 Copyright (c) 2002-2003 Daniel Molkentin <molkentin@kde.org> 00006 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include "core.h" 00025 00026 #include <kparts/part.h> 00027 #include <kparts/componentfactory.h> 00028 #include <kdebug.h> 00029 #include <tqtimer.h> 00030 #include <klocale.h> 00031 00032 using namespace Kontact; 00033 00034 class Core::Private 00035 { 00036 public: 00037 TQString lastErrorMessage; 00038 }; 00039 00040 Core::Core( TQWidget *parent, const char *name ) 00041 : KParts::MainWindow( parent, name ) 00042 { 00043 d = new Private; 00044 TQTimer* timer = new TQTimer( this ); 00045 mLastDate = TQDate::currentDate(); 00046 connect(timer, TQT_SIGNAL( timeout() ), TQT_SLOT( checkNewDay() ) ); 00047 timer->start( 1000*60 ); 00048 } 00049 00050 Core::~Core() 00051 { 00052 delete d; 00053 } 00054 00055 KParts::ReadOnlyPart *Core::createPart( const char *libname ) 00056 { 00057 kdDebug(5601) << "Core::createPart(): " << libname << endl; 00058 00059 TQMap<TQCString,KParts::ReadOnlyPart *>::ConstIterator it; 00060 it = mParts.find( libname ); 00061 if ( it != mParts.end() ) return it.data(); 00062 00063 kdDebug(5601) << "Creating new KPart" << endl; 00064 00065 int error = 0; 00066 KParts::ReadOnlyPart *part = 00067 KParts::ComponentFactory:: 00068 createPartInstanceFromLibrary<KParts::ReadOnlyPart> 00069 ( libname, this, 0, TQT_TQOBJECT(this), "kontact", TQStringList(), &error ); 00070 00071 KParts::ReadOnlyPart *pimPart = dynamic_cast<KParts::ReadOnlyPart*>( part ); 00072 if ( pimPart ) { 00073 mParts.insert( libname, pimPart ); 00074 QObject::connect( pimPart, TQT_SIGNAL( destroyed( TQObject * ) ), 00075 TQT_SLOT( slotPartDestroyed( TQObject * ) ) ); 00076 } else { 00077 // TODO move to KParts::ComponentFactory 00078 switch( error ) { 00079 case KParts::ComponentFactory::ErrNoServiceFound: 00080 d->lastErrorMessage = i18n( "No service found" ); 00081 break; 00082 case KParts::ComponentFactory::ErrServiceProvidesNoLibrary: 00083 d->lastErrorMessage = i18n( "Program error: the .desktop file for the service does not have a Library key." ); 00084 break; 00085 case KParts::ComponentFactory::ErrNoLibrary: 00086 d->lastErrorMessage = KLibLoader::self()->lastErrorMessage(); 00087 break; 00088 case KParts::ComponentFactory::ErrNoFactory: 00089 d->lastErrorMessage = i18n( "Program error: the library %1 does not provide a factory." ).arg( libname ); 00090 break; 00091 case KParts::ComponentFactory::ErrNoComponent: 00092 d->lastErrorMessage = i18n( "Program error: the library %1 does not support creating components of the specified type" ).arg( libname ); 00093 break; 00094 } 00095 kdWarning(5601) << d->lastErrorMessage << endl; 00096 } 00097 00098 return pimPart; 00099 } 00100 00101 void Core::slotPartDestroyed( TQObject * obj ) 00102 { 00103 // the part was deleted, we need to remove it from the part map to not return 00104 // a dangling pointer in createPart 00105 TQMap<TQCString, KParts::ReadOnlyPart*>::Iterator end = mParts.end(); 00106 TQMap<TQCString, KParts::ReadOnlyPart*>::Iterator it = mParts.begin(); 00107 for ( ; it != end; ++it ) { 00108 if ( it.data() == obj ) { 00109 mParts.remove( it ); 00110 return; 00111 } 00112 } 00113 } 00114 00115 void Core::checkNewDay() 00116 { 00117 if ( mLastDate != TQDate::currentDate() ) 00118 emit dayChanged( TQDate::currentDate() ); 00119 00120 mLastDate = TQDate::currentDate(); 00121 } 00122 00123 TQString Core::lastErrorMessage() const 00124 { 00125 return d->lastErrorMessage; 00126 } 00127 00128 #include "core.moc" 00129 // vim: sw=2 sts=2 et tw=80