~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    This file is part of akonadiresources.

    Copyright (c) 2006 Till Adam <adam@kde.org>
    Copyright (c) 2007 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#ifndef AKONADI_AGENTFACTORY_H
#define AKONADI_AGENTFACTORY_H

#include "akonadi_export.h"
#include "agentbase.h"

#include <QtCore/QObject>
#include <QtCore/QtPlugin>

namespace Akonadi {

class AgentFactoryBasePrivate;

/**
 * @short A factory base class for in-process agents.
 *
 * @see AKONADI_AGENT_FACTORY()
 * @internal
 * @since 4.6
 */
class AKONADI_EXPORT AgentFactoryBase : public QObject
{
  Q_OBJECT

  public:
    /**
     * Creates a new agent factory.
     * Executed in the main thread, performs KDE infrastructure setup.
     *
     * @param catalogName The translation catalog of this resource.
     * @param parent The parent object.
     */
    explicit AgentFactoryBase( const char *catalogName, QObject *parent = 0 );

    virtual ~AgentFactoryBase();

  public Q_SLOTS:
    /**
     * Creates a new agent instace with the given identifier.
     */
    virtual QObject* createInstance( const QString &identifier ) const = 0;

  protected:
    void createComponentData( const QString &identifier ) const;

  private:
    AgentFactoryBasePrivate* const d;
};

/**
 * @short A factory for in-process agents.
 *
 * @see AKONADI_AGENT_FACTORY()
 * @internal
 * @since 4.6
 */
template <typename T>
class AgentFactory : public AgentFactoryBase
{
  public:
    /** reimplemented */
    explicit AgentFactory( const char *catalogName, QObject *parent = 0 )
      : AgentFactoryBase( catalogName, parent )
    {
    }

    QObject* createInstance( const QString &identifier ) const
    {
      createComponentData( identifier );
      T* instance = new T( identifier );

      // check if T also inherits AgentBase::Observer and
      // if it does, automatically register it on itself
      Akonadi::AgentBase::Observer *observer = dynamic_cast<Akonadi::AgentBase::Observer*>( instance );
      if ( observer != 0 )
        instance->registerObserver( observer );

      return instance;
    }
};

}

#ifndef AKONADI_AGENT_FACTORY
/**
 * Macro to create an agent factory for in-process agents.
 *
 * @param agentClass class name of the agent type this factory should create.
 * @param catalogName name of the translation catalog of this agent type.
 * @since 4.6
 */
#define AKONADI_AGENT_FACTORY( agentClass, catalogName ) \
class agentClass ## Factory : public Akonadi::AgentFactory< agentClass > \
{ \
  public: \
    explicit agentClass ## Factory( QObject * parent = 0 ) : Akonadi::AgentFactory< agentClass >( # catalogName, parent ) {\
      setObjectName(# catalogName );\
    } \
}; \
Q_EXPORT_PLUGIN2( catalogName, agentClass ## Factory )

#endif

#endif