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>
68 {
69 public:
70  /** reimplemented */
71  explicit AgentFactory(const char *catalogName, QObject *parent = nullptr)
72  : AgentFactoryBase(catalogName, parent)
73  {
74  }
75 
76  QObject *createInstance(const QString &identifier) const override
77  {
78  createComponentData(identifier);
79  T *instance = new T(identifier);
80 
81  // check if T also inherits AgentBase::Observer and
82  // if it does, automatically register it on itself
83  auto observer = dynamic_cast<Akonadi::AgentBase::Observer *>(instance);
84  if (observer != nullptr) {
85  instance->registerObserver(observer);
86  }
87 
88  return instance;
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:67
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:76
AgentFactory(const char *catalogName, QObject *parent=nullptr)
reimplemented
Definition: agentfactory.h:71
QObject * parent() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Mar 21 2023 03:56:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.