File indexing completed on 2024-05-12 05:21:08

0001 /*
0002   This file is part of the KDE Kontact Plugin Interface Library.
0003 
0004   SPDX-FileCopyrightText: 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
0005   SPDX-FileCopyrightText: 2002-2003 Daniel Molkentin <molkentin@kde.org>
0006   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0007 
0008   SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "core.h"
0012 #include "kontactinterface_debug.h"
0013 
0014 #include <KPluginFactory>
0015 #include <KPluginMetaData>
0016 
0017 #include <QDateTime>
0018 #include <QTimer>
0019 
0020 using namespace KontactInterface;
0021 
0022 //@cond PRIVATE
0023 class Q_DECL_HIDDEN KontactInterface::CorePrivate
0024 {
0025     Core *const q;
0026 
0027 public:
0028     explicit CorePrivate(Core *qq);
0029 
0030     void slotPartDestroyed(QObject *);
0031     void checkNewDay();
0032 
0033     QString lastErrorMessage;
0034     QDate mLastDate;
0035     QMap<QByteArray, KParts::Part *> mParts;
0036 };
0037 
0038 CorePrivate::CorePrivate(Core *qq)
0039     : q(qq)
0040     , mLastDate(QDate::currentDate())
0041 {
0042 }
0043 //@endcond
0044 
0045 Core::Core(QWidget *parent, Qt::WindowFlags f)
0046     : KParts::MainWindow(parent, f)
0047     , d(new CorePrivate(this))
0048 {
0049     auto timer = new QTimer(this);
0050     connect(timer, &QTimer::timeout, this, [this]() {
0051         d->checkNewDay();
0052     });
0053     timer->start(1000 * 60);
0054 }
0055 
0056 Core::~Core() = default;
0057 
0058 KParts::Part *Core::createPart(const char *libname)
0059 {
0060     qCDebug(KONTACTINTERFACE_LOG) << libname;
0061 
0062     QMap<QByteArray, KParts::Part *>::ConstIterator it;
0063     it = d->mParts.constFind(libname);
0064     if (it != d->mParts.constEnd()) {
0065         return it.value();
0066     }
0067 
0068     qCDebug(KONTACTINTERFACE_LOG) << "Creating new KPart";
0069     const auto result = KPluginFactory::instantiatePlugin<KParts::Part>(KPluginMetaData(QString::fromLatin1(libname)), this);
0070     if (result.plugin) {
0071         d->mParts.insert(libname, result.plugin);
0072         QObject::connect(result.plugin, &KParts::Part::destroyed, this, [this](QObject *obj) {
0073             d->slotPartDestroyed(obj);
0074         });
0075     } else {
0076         d->lastErrorMessage = result.errorString;
0077         qCWarning(KONTACTINTERFACE_LOG) << d->lastErrorMessage;
0078     }
0079     return result.plugin;
0080 }
0081 
0082 //@cond PRIVATE
0083 void CorePrivate::slotPartDestroyed(QObject *obj)
0084 {
0085     // the part was deleted, we need to remove it from the part map to not return
0086     // a dangling pointer in createPart
0087     const QMap<QByteArray, KParts::Part *>::Iterator end = mParts.end();
0088     QMap<QByteArray, KParts::Part *>::Iterator it = mParts.begin();
0089     for (; it != end; ++it) {
0090         if (it.value() == obj) {
0091             mParts.erase(it);
0092             return;
0093         }
0094     }
0095 }
0096 
0097 void CorePrivate::checkNewDay()
0098 {
0099     if (mLastDate != QDate::currentDate()) {
0100         Q_EMIT q->dayChanged(QDate::currentDate());
0101     }
0102 
0103     mLastDate = QDate::currentDate();
0104 }
0105 //@endcond
0106 
0107 QString Core::lastErrorMessage() const
0108 {
0109     return d->lastErrorMessage;
0110 }
0111 
0112 #include "moc_core.cpp"