Imported Upstream version 1.4.1
[platform/upstream/syncevolution.git] / src / backends / akonadi / akonadisyncsource.h
1 /*
2     Copyright (c) 2009 Sascha Peilicke <sasch.pe@gmx.de>
3
4     This application is free software; you can redistribute it and/or modify it
5     under the terms of the GNU Library General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or (at your
7     option) any later version.
8
9     This application is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
12     License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this application; see the file COPYING.LIB.  If not, write to the
16     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301, USA.
18 */
19
20 #ifndef AKONADISYNCSOURCE_H
21 #define AKONADISYNCSOURCE_H
22
23 #include "config.h"
24
25 #ifdef ENABLE_AKONADI
26
27 #include <Akonadi/Collection>
28 #include <Akonadi/Item>
29
30 #include <QtCore/QDateTime>
31 #include <QtCore/QStringList>
32
33 #include <syncevo/TrackingSyncSource.h>
34
35 SE_BEGIN_CXX
36
37 class TimeTrackingObserver;
38
39 /**
40  * General purpose Akonadi Sync Source. Choosing the type of data is
41  * done when instantiating it, using the Akonadi MIME subtypes.
42  * Payload is always using the native Akonadi format (no special "raw"
43  * and "engine" formats).
44  *
45  * Change tracking is done via the item uid/revision attributes.
46  *
47  * Databases (collections in Akonadi terminology) are selected via
48  * their int64 ID number.
49  */
50 class AkonadiSyncSource : public TrackingSyncSource
51 {
52 public:
53     /**
54      * @param submime     the MIME type string used by Akonadi
55      *                    to identify contacts, tasks, events, etc.
56      * @param params      the SyncEvolution source parameters
57      */
58     AkonadiSyncSource(const char *submime,
59                       const SyncSourceParams &params);
60     virtual ~AkonadiSyncSource();
61
62     /* methods that have to be implemented to complete TrackingSyncSource */
63     virtual Databases getDatabases();
64     virtual void open();
65     virtual void listAllItems(SyncSourceRevisions::RevisionMap_t &revisions);
66     virtual InsertItemResult insertItem(const std::string &luid, const std::string &item, bool raw);
67     virtual void readItem(const std::string &luid, std::string &item, bool raw);
68     virtual void removeItem(const string &luid);
69     virtual void close();
70     virtual bool isEmpty();
71 private:
72     void start();
73 protected:
74     Akonadi::Collection m_collection;
75
76     /**
77      * MIME types(s) of items, from submime constructor parameter.
78      * Only one format is supported, but it may have different aliases (for example,
79      * application/x-vnd.kde.notes == application/x-vnd.akonadi.note).
80      */
81     QStringList m_mimeTypes;
82
83     /**
84      * The actual type to be used inside the collection. Set after opening
85      * the collection.
86      */
87     QString m_contentMimeType;
88 };
89
90 class AkonadiContactSource : public AkonadiSyncSource
91 {
92 public:
93     AkonadiContactSource(const SyncSourceParams &params)
94         : AkonadiSyncSource("text/directory", params)
95     {
96     }
97
98     virtual std::string getMimeType() const {
99         return "text/vcard";
100     }
101     virtual std::string getMimeVersion() const {
102         return "3.0";
103     }
104
105     void getSynthesisInfo(SynthesisInfo &info,
106                           XMLConfigFragments &fragments)
107     {
108         TrackingSyncSource::getSynthesisInfo(info, fragments);
109
110         /** enable the KDE X- extensions in the Synthesis<->backend conversion */
111         info.m_backendRule = "KDE";
112
113         /*
114          * Disable the default VCARD_BEFOREWRITE_SCRIPT_EVOLUTION.
115          * If any KDE-specific transformations via such a script
116          * are needed, it can be named here and then defined by appending
117          * to the fragments.
118          */
119         info.m_beforeWriteScript = ""; // "$VCARD_BEFOREWRITE_SCRIPT_KDE;";
120         // fragments.m_datatypes["VCARD_BEFOREWRITE_SCRIPT_KDE"] = "<macro name=\"VCARD_BEFOREWRITE_SCRIPT_KDE\"><![DATA[ ... ]]></macro>";
121     }
122 };
123
124 class AkonadiCalendarSource : public AkonadiSyncSource
125 {
126 public:
127     AkonadiCalendarSource(const SyncSourceParams &params)
128         : AkonadiSyncSource("application/x-vnd.akonadi.calendar.event", params)
129     {
130     }
131
132     // TODO: the items are expected to be complete VCALENDAR with
133     // all necessary VTIMEZONEs and one VEVENT (here) resp. VTODO
134     // (AkonadiTodoSource). Not sure what we get from Akonadi.
135     virtual std::string getMimeType() const { return "text/calendar"; }
136     virtual std::string getMimeVersion() const { return "2.0"; }
137 };
138
139 class AkonadiTaskSource : public AkonadiSyncSource
140 {
141 public:
142     AkonadiTaskSource(const SyncSourceParams &params)
143         : AkonadiSyncSource("application/x-vnd.akonadi.calendar.todo", params)
144     {
145     }
146
147     virtual std::string getMimeType() const { return "text/calendar"; }
148     virtual std::string getMimeVersion() const { return "2.0"; }
149 };
150
151 class AkonadiMemoSource : public AkonadiSyncSource
152 {
153 private:
154     QString toKJots(QString data);
155     QString toSynthesis(QString data);
156
157 public:
158     AkonadiMemoSource(const SyncSourceParams &params) :
159         AkonadiSyncSource("text/x-vnd.akonadi.note,application/x-vnd.kde.notes", params)
160     {
161     }
162
163     // TODO: the AkonadiMemoSource is expected to import/export
164     // plain text with the summary in the first line; currently
165     // the AkonadiSyncSource will use VJOURNAL
166     // Also Currently there is no application which uses akonadi backend
167     // to display notes: probably work for knote??
168     virtual std::string getMimeType() const { return "text/plain"; }
169     virtual std::string getMimeVersion() const { return "1.0"; }
170     virtual InsertItemResult insertItem(const std::string &luid, const std::string &item, bool raw);
171     virtual void readItem(const std::string &luid, std::string &item, bool raw);
172 };
173
174 SE_END_CXX
175
176 #endif // ENABLE_AKONADI
177 #endif // AKONADISYNCSOURCE_H