KontactInterface

core.cpp
1/*
2 This file is part of the KDE Kontact Plugin Interface Library.
3
4 SPDX-FileCopyrightText: 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
5 SPDX-FileCopyrightText: 2002-2003 Daniel Molkentin <molkentin@kde.org>
6 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "core.h"
12#include "kontactinterface_debug.h"
13
14#include <KPluginFactory>
15#include <KPluginMetaData>
16
17#include <QDateTime>
18#include <QTimer>
19
20using namespace KontactInterface;
21
22//@cond PRIVATE
23class Q_DECL_HIDDEN KontactInterface::CorePrivate
24{
25 Core *const q;
26
27public:
28 explicit CorePrivate(Core *qq);
29
30 void slotPartDestroyed(QObject *);
31 void checkNewDay();
32
33 QString lastErrorMessage;
34 QDate mLastDate;
36};
37
38CorePrivate::CorePrivate(Core *qq)
39 : q(qq)
40 , mLastDate(QDate::currentDate())
41{
42}
43//@endcond
44
46 : KParts::MainWindow(parent, f)
47 , d(new CorePrivate(this))
48{
49 auto timer = new QTimer(this);
50 connect(timer, &QTimer::timeout, this, [this]() {
51 d->checkNewDay();
52 });
53 timer->start(1000 * 60);
54}
55
56Core::~Core() = default;
57
58KParts::Part *Core::createPart(const char *libname)
59{
60 qCDebug(KONTACTINTERFACE_LOG) << libname;
61
63 it = d->mParts.constFind(libname);
64 if (it != d->mParts.constEnd()) {
65 return it.value();
66 }
67
68 qCDebug(KONTACTINTERFACE_LOG) << "Creating new KPart";
69 const auto result = KPluginFactory::instantiatePlugin<KParts::Part>(KPluginMetaData(QString::fromLatin1(libname)), this);
70 if (result.plugin) {
71 d->mParts.insert(libname, result.plugin);
72 QObject::connect(result.plugin, &KParts::Part::destroyed, this, [this](QObject *obj) {
73 d->slotPartDestroyed(obj);
74 });
75 } else {
76 d->lastErrorMessage = result.errorString;
77 qCWarning(KONTACTINTERFACE_LOG) << d->lastErrorMessage;
78 }
79 return result.plugin;
80}
81
82//@cond PRIVATE
83void CorePrivate::slotPartDestroyed(QObject *obj)
84{
85 // the part was deleted, we need to remove it from the part map to not return
86 // a dangling pointer in createPart
87 const QMap<QByteArray, KParts::Part *>::Iterator end = mParts.end();
89 for (; it != end; ++it) {
90 if (it.value() == obj) {
91 mParts.erase(it);
92 return;
93 }
94 }
95}
96
97void CorePrivate::checkNewDay()
98{
99 if (mLastDate != QDate::currentDate()) {
100 Q_EMIT q->dayChanged(QDate::currentDate());
101 }
102
103 mLastDate = QDate::currentDate();
104}
105//@endcond
106
108{
109 return d->lastErrorMessage;
110}
111
112#include "moc_core.cpp"
The abstract interface that represents the Kontact core.
Definition core.h:27
Core(QWidget *parent=nullptr, Qt::WindowFlags flags={})
Creates a new core object.
Definition core.cpp:45
~Core() override
Destroys the core object.
KParts::Part * createPart(const char *library)
Definition core.cpp:58
QString lastErrorMessage() const
Returns the last error message for problems during KParts loading.
Definition core.cpp:107
QDate currentDate()
ConstIterator
const_iterator constEnd() const const
const_iterator constFind(const Key &key) const const
T value(const Key &key, const T &defaultValue) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
QString fromLatin1(QByteArrayView str)
typedef WindowFlags
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.