Kontact Plugin Interface Library
core.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 #include "core.h"
00025
00026 #include <kdebug.h>
00027 #include <kparts/part.h>
00028 #include <kparts/componentfactory.h>
00029
00030 #include <QDateTime>
00031 #include <QTimer>
00032
00033 using namespace KontactInterface;
00034
00035
00036 class KontactInterface::Core::Private
00037 {
00038 Core *const q;
00039
00040 public:
00041 explicit Private( Core *qq );
00042
00043 void slotPartDestroyed( QObject * );
00044 void checkNewDay();
00045
00046 QString lastErrorMessage;
00047 QDate mLastDate;
00048 QMap<QByteArray,KParts::ReadOnlyPart *> mParts;
00049 };
00050
00051 Core::Private::Private( Core *qq )
00052 : q( qq ), mLastDate( QDate::currentDate() )
00053 {
00054 }
00055
00056
00057 Core::Core( QWidget *parent, Qt::WindowFlags f )
00058 : KParts::MainWindow( parent, f ), d( new Private( this ) )
00059 {
00060 QTimer *timer = new QTimer( this );
00061 connect( timer, SIGNAL(timeout()), SLOT(checkNewDay()) );
00062 timer->start( 1000 * 60 );
00063 }
00064
00065 Core::~Core()
00066 {
00067 delete d;
00068 }
00069
00070 KParts::ReadOnlyPart *Core::createPart( const char *libname )
00071 {
00072 kDebug() << libname;
00073
00074 QMap<QByteArray,KParts::ReadOnlyPart *>::ConstIterator it;
00075 it = d->mParts.constFind( libname );
00076 if ( it != d->mParts.constEnd() ) {
00077 return it.value();
00078 }
00079
00080 kDebug() << "Creating new KPart";
00081
00082 KPluginLoader loader( libname );
00083 kDebug() << loader.fileName();
00084 KPluginFactory *factory = loader.factory();
00085 KParts::ReadOnlyPart *part = 0;
00086 if ( factory ) {
00087 part = factory->create<KParts::ReadOnlyPart>( this );
00088 }
00089
00090 if (part) {
00091 d->mParts.insert( libname, part );
00092 QObject::connect( part, SIGNAL(destroyed(QObject *)),
00093 SLOT(slotPartDestroyed(QObject *)) );
00094 } else {
00095 d->lastErrorMessage = loader.errorString();
00096 kWarning() << d->lastErrorMessage;
00097 }
00098
00099 return part;
00100 }
00101
00102
00103 void Core::Private::slotPartDestroyed( QObject *obj )
00104 {
00105
00106
00107 QMap<QByteArray, KParts::ReadOnlyPart*>::Iterator end = mParts.end();
00108 QMap<QByteArray, KParts::ReadOnlyPart*>::Iterator it = mParts.begin();
00109 for ( ; it != end; ++it ) {
00110 if ( it.value() == obj ) {
00111 mParts.erase( it );
00112 return;
00113 }
00114 }
00115 }
00116
00117 void Core::Private::checkNewDay()
00118 {
00119 if ( mLastDate != QDate::currentDate() ) {
00120 emit q->dayChanged( QDate::currentDate() );
00121 }
00122
00123 mLastDate = QDate::currentDate();
00124 }
00125
00126
00127 QString Core::lastErrorMessage() const
00128 {
00129 return d->lastErrorMessage;
00130 }
00131
00132 #include "core.moc"
00133