Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /* |
---|---|
2 | Copyright (c) 2008 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library 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 library 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 library; 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 | #include "akonadisink.h" |
21 | #include "datasink.h" |
22 | |
23 | #include <akonadi/control.h> |
24 | #include <akonadi/collection.h> |
25 | #include <akonadi/collectionfetchjob.h> |
26 | #include <akonadi/mimetypechecker.h> |
27 | |
28 | #include <kabc/addressee.h> |
29 | |
30 | #include <KComponentData> |
31 | #include <KDebug> |
32 | #include <KUrl> |
33 | |
34 | #include <QCoreApplication> |
35 | #include <QStringList> |
36 | |
37 | #include <opensync/opensync.h> |
38 | #include <opensync/opensync-plugin.h> |
39 | #include <opensync/opensync-format.h> |
40 | |
41 | static KComponentData *kcd = 0; |
42 | static QCoreApplication *app = 0; |
43 | |
44 | static int fakeArgc = 0; |
45 | static char** fakeArgv = 0; |
46 | |
47 | extern "C" |
48 | { |
49 | |
50 | static void* akonadi_initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError **error) |
51 | { |
52 | osync_trace( TRACE_ENTRY, "%s(%p, %p, %p)", __func__, plugin, info, error ); |
53 | |
54 | if ( !app ) |
55 | app = new QCoreApplication( fakeArgc, fakeArgv ); |
56 | if ( !kcd ) |
57 | kcd = new KComponentData( "akonadi_opensync"); |
58 | |
59 | // main sink |
60 | AkonadiSink *mainSink = new AkonadiSink(); |
61 | if ( !mainSink->initialize( plugin, info, error ) ) { |
62 | delete mainSink; |
63 | osync_trace( TRACE_EXIT_ERROR, "%s: NULL", __func__ ); |
64 | return 0; |
65 | } |
66 | |
67 | // object type sinks |
68 | const int numobjs = osync_plugin_info_num_objtypes( info ); |
69 | for ( int i = 0; i < numobjs; ++i ) { |
70 | OSyncObjTypeSink *sink = osync_plugin_info_nth_objtype( info, i ); |
71 | QString sinkName( osync_objtype_sink_get_name( sink ) ); |
72 | kDebug() << "###"<< i << sinkName; |
73 | |
74 | DataSink *ds; |
75 | if ( sinkName == "event" ) |
76 | ds = new DataSink( DataSink::Calendar ); |
77 | else if ( sinkName == "contact" ) |
78 | ds = new DataSink( DataSink::Contacts ); |
79 | |
80 | if ( !ds->initialize( plugin, info, sink, error ) ) { |
81 | delete ds; |
82 | delete mainSink; |
83 | osync_trace( TRACE_EXIT_ERROR, "%s: NULL", __func__ ); |
84 | return 0; |
85 | } |
86 | } |
87 | |
88 | osync_trace( TRACE_EXIT, "%s: %p", __func__, mainSink ); |
89 | return mainSink; |
90 | } |
91 | |
92 | static osync_bool akonadi_discover(void *userdata, OSyncPluginInfo *info, OSyncError **error) |
93 | { |
94 | osync_trace( TRACE_ENTRY, "%s(%p, %p, %p)", __func__, userdata, info, error ); |
95 | kDebug(); |
96 | |
97 | if ( !Akonadi::Control::start() ) |
98 | return false; |
99 | |
100 | // fetch all akonadi collections |
101 | Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob( |
102 | Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive ); |
103 | if ( !job->exec() ) |
104 | return false; |
105 | |
106 | Akonadi::Collection::List cols = job->collections(); |
107 | kDebug() << "found"<< cols.count() << "collections"; |
108 | |
109 | OSyncPluginConfig *config = osync_plugin_info_get_config( info ); |
110 | |
111 | Akonadi::MimeTypeChecker contactMimeChecker; |
112 | contactMimeChecker.addWantedMimeType( KABC::Addressee::mimeType() ); |
113 | Akonadi::MimeTypeChecker calendarMimeTypeChecker; |
114 | calendarMimeTypeChecker.addWantedMimeType( QLatin1String( "text/calendar") ); |
115 | |
116 | const int num_objtypes = osync_plugin_info_num_objtypes( info ); |
117 | for ( int i = 0; i < num_objtypes; ++i ) { |
118 | OSyncObjTypeSink *sink = osync_plugin_info_nth_objtype( info, i ); |
119 | foreach ( const Akonadi::Collection &col, cols ) { |
120 | kDebug() << "creating resource for"<< col.name() << col.contentMimeTypes(); |
121 | // if ( !contactMimeChecker.isWantedCollection( col ) ) // ### TODO |
122 | // continue; |
123 | if ( col.contentMimeTypes().isEmpty() ) |
124 | continue; |
125 | |
126 | OSyncPluginResource *res = osync_plugin_resource_new( error ); |
127 | // TODO error handling? |
128 | osync_plugin_resource_enable( res, TRUE ); |
129 | osync_plugin_resource_set_name( res, col.name().toUtf8() ); // TODO: full path instead of the name |
130 | osync_plugin_resource_set_objtype( res, osync_objtype_sink_get_name( sink ) ); |
131 | osync_plugin_resource_set_url( res, col.url().url().toLatin1() ); |
132 | |
133 | QString formatName; |
134 | if ( calendarMimeChecker.isWantedCollection( col ) ) |
135 | formatName = "vevent20"; |
136 | else if ( contactMimeChecker.isWantedCollection( col ) ) |
137 | formatName = "vcard30"; |
138 | else |
139 | continue; // if the collection is not calendar or contact one, skip it |
140 | |
141 | /*OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env( info ); |
142 | OSyncObjFormat *format = osync_format_env_find_objformat( formatenv, formatName.toLatin1() ); |
143 | osync_plugin_resource_set_objformat( res, format );*/ |
144 | |
145 | osync_plugin_resource_add_objformat_sink( res, osync_objformat_sink_new( formatName.toLatin1(), error ) ); |
146 | |
147 | osync_plugin_config_add_resource( config, res ); |
148 | } |
149 | osync_objtype_sink_set_available( sink, TRUE ); |
150 | } |
151 | |
152 | osync_trace( TRACE_EXIT, "%s", __func__ ); |
153 | return TRUE; |
154 | } |
155 | |
156 | static void akonadi_finalize(void *userdata) |
157 | { |
158 | osync_trace( TRACE_ENTRY, "%s(%p)", __func__, userdata ); |
159 | kDebug(); |
160 | AkonadiSink *sink = reinterpret_cast<AkonadiSink*>( userdata ); |
161 | delete sink; |
162 | delete kcd; |
163 | kcd = 0; |
164 | delete app; |
165 | app = 0; |
166 | osync_trace( TRACE_EXIT, "%s", __func__ ); |
167 | } |
168 | |
169 | KDE_EXPORT osync_bool get_sync_info(OSyncPluginEnv *env, OSyncError **error) |
170 | { |
171 | osync_trace( TRACE_ENTRY, "%s(%p)", __func__, env ); |
172 | |
173 | OSyncPlugin *plugin = osync_plugin_new( error ); |
174 | if ( !plugin ) { |
175 | osync_trace( TRACE_EXIT_ERROR, "%s: Unable to register: %s", __func__, osync_error_print( error ) ); |
176 | return FALSE; |
177 | } |
178 | |
179 | osync_plugin_set_name( plugin, "akonadi-sync"); |
180 | osync_plugin_set_longname( plugin, "Akonadi"); |
181 | osync_plugin_set_description( plugin, "Plugin to synchronize with Akonadi"); |
182 | // osync_plugin_set_config_type(plugin, OSYNC_PLUGIN_NO_CONFIGURATION); |
183 | osync_plugin_set_start_type( plugin, OSYNC_START_TYPE_PROCESS ); |
184 | |
185 | osync_plugin_set_initialize( plugin, akonadi_initialize ); |
186 | osync_plugin_set_finalize( plugin, akonadi_finalize ); |
187 | osync_plugin_set_discover( plugin, akonadi_discover ); |
188 | |
189 | osync_plugin_env_register_plugin( env, plugin ); |
190 | osync_plugin_unref( plugin ); |
191 | |
192 | osync_trace( TRACE_EXIT, "%s", __func__ ); |
193 | return TRUE; |
194 | } |
195 | |
196 | KDE_EXPORT int get_version(void) |
197 | { |
198 | return 1; |
199 | } |
200 | |
201 | }// extern "C" |
202 |
Warning: That file was not part of the compilation database. It may have many parsing errors.