Akonadi

agentfactory.h
1 /*
2  This file is part of akonadiresources.
3 
4  SPDX-FileCopyrightText: 2006 Till Adam <[email protected]>
5  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #pragma once
11 
12 #include "agentbase.h"
13 #include "akonadiagentbase_export.h"
14 
15 #include <QObject>
16 
17 #include <memory>
18 
19 namespace Akonadi
20 {
21 class AgentFactoryBasePrivate;
22 
23 /**
24  * @short A factory base class for in-process agents.
25  *
26  * @see AKONADI_AGENT_FACTORY()
27  * @internal
28  * @since 4.6
29  */
30 class AKONADIAGENTBASE_EXPORT AgentFactoryBase : public QObject
31 {
32  Q_OBJECT
33 
34 public:
35  /**
36  * Creates a new agent factory.
37  * Executed in the main thread, performs KDE infrastructure setup.
38  *
39  * @param catalogName The translation catalog of this resource.
40  * @param parent The parent object.
41  */
42  explicit AgentFactoryBase(const char *catalogName, QObject *parent = nullptr);
43 
44  ~AgentFactoryBase() override;
45 
46 public Q_SLOTS:
47  /**
48  * Creates a new agent instance with the given identifier.
49  */
50  virtual QObject *createInstance(const QString &identifier) const = 0;
51 
52 protected:
53  void createComponentData(const QString &identifier) const;
54 
55 private:
56  std::unique_ptr<AgentFactoryBasePrivate> const d;
57 };
58 
59 /**
60  * @short A factory for in-process agents.
61  *
62  * @see AKONADI_AGENT_FACTORY()
63  * @internal
64  * @since 4.6
65  */
66 template<typename T> class AgentFactory : public AgentFactoryBase
67 {
68 public:
69  /** reimplemented */
70  explicit AgentFactory(const char *catalogName, QObject *parent = nullptr)
71  : AgentFactoryBase(catalogName, parent)
72  {
73  }
74 
75  QObject *createInstance(const QString &identifier) const override
76  {
77  createComponentData(identifier);
78  T *instance = new T(identifier);
79 
80  // check if T also inherits AgentBase::Observer and
81  // if it does, automatically register it on itself
82  auto observer = dynamic_cast<Akonadi::AgentBase::Observer *>(instance);
83  if (observer != nullptr) {
84  instance->registerObserver(observer);
85  }
86 
87  return instance;
88  }
89 };
90 
91 }
92 
A factory base class for in-process agents.
Definition: agentfactory.h:30
A factory for in-process agents.
Definition: agentfactory.h:66
The interface for reacting on monitored or replayed changes.
Definition: agentbase.h:178
QObject * createInstance(const QString &identifier) const override
Creates a new agent instance with the given identifier.
Definition: agentfactory.h:75
AgentFactory(const char *catalogName, QObject *parent=nullptr)
reimplemented
Definition: agentfactory.h:70
QObject * parent() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.