KontactInterface

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

KDE's Doxygen guidelines are available online.