Implement single-file includes for all E-D-S libraries.
[platform/upstream/evolution-data-server.git] / modules / yahoo-backend / module-yahoo-backend.c
1 /*
2  * module-yahoo-backend.c
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
16  *
17  */
18
19 #include <config.h>
20 #include <glib/gi18n-lib.h>
21
22 #include <libebackend/libebackend.h>
23
24 /* Standard GObject macros */
25 #define E_TYPE_YAHOO_BACKEND \
26         (e_yahoo_backend_get_type ())
27 #define E_YAHOO_BACKEND(obj) \
28         (G_TYPE_CHECK_INSTANCE_CAST \
29         ((obj), E_TYPE_YAHOO_BACKEND, EYahooBackend))
30
31 /* Just for readability... */
32 #define METHOD(x) (CAMEL_NETWORK_SECURITY_METHOD_##x)
33
34 /* IMAP Configuration Details */
35 #define YAHOO_IMAP_BACKEND_NAME         "imapx"
36 #define YAHOO_IMAP_HOST                 "imap.mail.yahoo.com"
37 #define YAHOO_IMAP_PORT                 993
38 #define YAHOO_IMAP_SECURITY_METHOD      METHOD (SSL_ON_ALTERNATE_PORT)
39
40 /* SMTP Configuration Details */
41 #define YAHOO_SMTP_BACKEND_NAME         "smtp"
42 #define YAHOO_SMTP_HOST                 "smtp.mail.yahoo.com"
43 #define YAHOO_SMTP_PORT                 465
44 #define YAHOO_SMTP_SECURITY_METHOD      METHOD (SSL_ON_ALTERNATE_PORT)
45
46 /* Calendar Configuration Details */
47 #define YAHOO_CALENDAR_BACKEND_NAME     "caldav"
48 #define YAHOO_CALENDAR_HOST             "caldav.calendar.yahoo.com"
49 #define YAHOO_CALENDAR_CALDAV_PATH      "/dav/%s/Calendar/%s"
50
51 typedef struct _EYahooBackend EYahooBackend;
52 typedef struct _EYahooBackendClass EYahooBackendClass;
53
54 typedef struct _EYahooBackendFactory EYahooBackendFactory;
55 typedef struct _EYahooBackendFactoryClass EYahooBackendFactoryClass;
56
57 struct _EYahooBackend {
58         ECollectionBackend parent;
59         GWeakRef mail_identity_source;
60 };
61
62 struct _EYahooBackendClass {
63         ECollectionBackendClass parent_class;
64 };
65
66 struct _EYahooBackendFactory {
67         ECollectionBackendFactory parent;
68 };
69
70 struct _EYahooBackendFactoryClass {
71         ECollectionBackendFactoryClass parent_class;
72 };
73
74 /* Module Entry Points */
75 void e_module_load (GTypeModule *type_module);
76 void e_module_unload (GTypeModule *type_module);
77
78 /* Forward Declarations */
79 GType e_yahoo_backend_get_type (void);
80 GType e_yahoo_backend_factory_get_type (void);
81
82 G_DEFINE_DYNAMIC_TYPE (
83         EYahooBackend,
84         e_yahoo_backend,
85         E_TYPE_COLLECTION_BACKEND)
86
87 G_DEFINE_DYNAMIC_TYPE (
88         EYahooBackendFactory,
89         e_yahoo_backend_factory,
90         E_TYPE_COLLECTION_BACKEND_FACTORY)
91
92 static void
93 yahoo_backend_config_calendar_child (ECollectionBackend *backend,
94                                      ESource *source)
95 {
96         EYahooBackend *yahoo_backend;
97         ESource *collection_source;
98         ESource *mail_identity_source;
99         ESourceExtension *extension;
100         ESourceCollection *collection_extension;
101         const gchar *extension_name;
102         const gchar *identity;
103         gchar *display_name = NULL;
104
105         /* FIXME As a future enhancement, we should query Yahoo!
106          *       for a list of user calendars and add them to the
107          *       collection with matching display names and colors. */
108
109         yahoo_backend = E_YAHOO_BACKEND (backend);
110
111         collection_source = e_backend_get_source (E_BACKEND (backend));
112
113         collection_extension = e_source_get_extension (
114                 collection_source, E_SOURCE_EXTENSION_COLLECTION);
115
116         identity = e_source_collection_get_identity (collection_extension);
117
118         /* XXX Assume the calendar's display name can be derived from
119          *     the user's mail identity.  As mentioned above, we should
120          *     really just query Yahoo! for a list of user calendars. */
121         mail_identity_source =
122                 g_weak_ref_get (&yahoo_backend->mail_identity_source);
123         if (mail_identity_source != NULL) {
124                 extension = e_source_get_extension (
125                         mail_identity_source,
126                         E_SOURCE_EXTENSION_MAIL_IDENTITY);
127                 display_name = e_source_mail_identity_dup_name (
128                         E_SOURCE_MAIL_IDENTITY (extension));
129                 if (display_name != NULL)
130                         g_strdelimit (display_name, " ", '_');
131                 g_object_unref (mail_identity_source);
132         }
133
134         extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
135         extension = e_source_get_extension (source, extension_name);
136
137         e_source_authentication_set_host (
138                 E_SOURCE_AUTHENTICATION (extension),
139                 YAHOO_CALENDAR_HOST);
140
141         g_object_bind_property (
142                 collection_extension, "identity",
143                 extension, "user",
144                 G_BINDING_SYNC_CREATE);
145
146         extension_name = E_SOURCE_EXTENSION_SECURITY;
147         extension = e_source_get_extension (source, extension_name);
148
149         e_source_security_set_secure (
150                 E_SOURCE_SECURITY (extension), TRUE);
151
152         extension_name = E_SOURCE_EXTENSION_WEBDAV_BACKEND;
153         extension = e_source_get_extension (source, extension_name);
154
155         e_source_webdav_set_display_name (
156                 E_SOURCE_WEBDAV (extension), display_name);
157
158         if (identity != NULL && display_name != NULL) {
159                 gchar *resource_path;
160
161                 resource_path = g_strdup_printf (
162                         YAHOO_CALENDAR_CALDAV_PATH, identity, display_name);
163                 e_source_webdav_set_resource_path (
164                         E_SOURCE_WEBDAV (extension), resource_path);
165                 g_free (resource_path);
166         }
167
168         g_free (display_name);
169 }
170
171 static void
172 yahoo_backend_add_calendar (ECollectionBackend *backend)
173 {
174         ESource *source;
175         ESourceBackend *extension;
176         ESourceRegistryServer *server;
177         const gchar *backend_name;
178         const gchar *extension_name;
179
180         /* XXX We could just stick a [Calendar] and [Task List] extension
181          *     into the same ESource since all other settings are exactly
182          *     the same.  But it might be confusing if tweaking a setting
183          *     in your Yahoo! Calendar also gets applied to your Yahoo!
184          *     Task List in Evolution. */
185
186         backend_name = YAHOO_CALENDAR_BACKEND_NAME;
187
188         server = e_collection_backend_ref_server (backend);
189
190         /* Add Yahoo! Calendar */
191
192         source = e_collection_backend_new_child (backend, "Calendar");
193         e_source_set_display_name (source, _("Calendar"));
194
195         extension_name = E_SOURCE_EXTENSION_CALENDAR;
196         extension = e_source_get_extension (source, extension_name);
197         e_source_backend_set_backend_name (extension, backend_name);
198
199         yahoo_backend_config_calendar_child (backend, source);
200         e_source_registry_server_add_source (server, source);
201
202         g_object_unref (source);
203
204         /* Add Yahoo! Tasks */
205
206         source = e_collection_backend_new_child (backend, "Tasks");
207         e_source_set_display_name (source, _("Tasks"));
208
209         extension_name = E_SOURCE_EXTENSION_TASK_LIST;
210         extension = e_source_get_extension (source, extension_name);
211         e_source_backend_set_backend_name (extension, backend_name);
212
213         yahoo_backend_config_calendar_child (backend, source);
214         e_source_registry_server_add_source (server, source);
215
216         g_object_unref (source);
217
218         g_object_unref (server);
219 }
220
221 static void
222 yahoo_backend_populate (ECollectionBackend *backend)
223 {
224         GList *list;
225
226         /* Chain up to parent's populate() method. */
227         E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
228                 populate (backend);
229
230         /* Chain up first so we pick up the mail identity source. */
231         list = e_collection_backend_list_calendar_sources (backend);
232         if (list == NULL)
233                 yahoo_backend_add_calendar (backend);
234         g_list_free_full (list, (GDestroyNotify) g_object_unref);
235 }
236
237 static void
238 yahoo_backend_child_added (ECollectionBackend *backend,
239                            ESource *child_source)
240 {
241         EYahooBackend *yahoo_backend;
242         ESource *collection_source;
243         const gchar *extension_name;
244         gboolean is_mail = FALSE;
245
246         yahoo_backend = E_YAHOO_BACKEND (backend);
247         collection_source = e_backend_get_source (E_BACKEND (backend));
248
249         extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
250         is_mail |= e_source_has_extension (child_source, extension_name);
251
252         /* Take special note of the mail identity source.
253          * We need it to build the calendar CalDAV path. */
254         extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
255         if (e_source_has_extension (child_source, extension_name)) {
256                 GWeakRef *weak_ref;
257
258                 weak_ref = &yahoo_backend->mail_identity_source;
259                 g_weak_ref_set (weak_ref, child_source);
260                 is_mail = TRUE;
261         }
262
263         extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
264         is_mail |= e_source_has_extension (child_source, extension_name);
265
266         /* Synchronize mail-related display names with the collection. */
267         if (is_mail)
268                 g_object_bind_property (
269                         collection_source, "display-name",
270                         child_source, "display-name",
271                         G_BINDING_SYNC_CREATE);
272
273         /* Synchronize mail-related user with the collection identity. */
274         extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
275         if (is_mail && e_source_has_extension (child_source, extension_name)) {
276                 ESourceAuthentication *auth_child_extension;
277                 ESourceCollection *collection_extension;
278
279                 extension_name = E_SOURCE_EXTENSION_COLLECTION;
280                 collection_extension = e_source_get_extension (
281                         collection_source, extension_name);
282
283                 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
284                 auth_child_extension = e_source_get_extension (
285                         child_source, extension_name);
286
287                 g_object_bind_property (
288                         collection_extension, "identity",
289                         auth_child_extension, "user",
290                         G_BINDING_SYNC_CREATE);
291         }
292
293         /* Chain up to parent's child_added() method. */
294         E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
295                 child_added (backend, child_source);
296 }
297
298 static void
299 e_yahoo_backend_class_init (EYahooBackendClass *class)
300 {
301         ECollectionBackendClass *backend_class;
302
303         backend_class = E_COLLECTION_BACKEND_CLASS (class);
304         backend_class->populate = yahoo_backend_populate;
305         backend_class->child_added = yahoo_backend_child_added;
306 }
307
308 static void
309 e_yahoo_backend_class_finalize (EYahooBackendClass *class)
310 {
311 }
312
313 static void
314 e_yahoo_backend_init (EYahooBackend *backend)
315 {
316 }
317
318 static void
319 yahoo_backend_prepare_mail_account_source (ESource *source)
320 {
321         ESourceCamel *camel_extension;
322         ESourceExtension *extension;
323         CamelSettings *settings;
324         const gchar *backend_name;
325         const gchar *extension_name;
326
327         backend_name = YAHOO_IMAP_BACKEND_NAME;
328
329         extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
330         extension = e_source_get_extension (source, extension_name);
331
332         e_source_backend_set_backend_name (
333                 E_SOURCE_BACKEND (extension), backend_name);
334
335         extension_name = e_source_camel_get_extension_name (backend_name);
336         camel_extension = e_source_get_extension (source, extension_name);
337         settings = e_source_camel_get_settings (camel_extension);
338
339         /* The "auth-mechanism" should be determined elsewhere. */
340
341         camel_network_settings_set_host (
342                 CAMEL_NETWORK_SETTINGS (settings),
343                 YAHOO_IMAP_HOST);
344
345         camel_network_settings_set_port (
346                 CAMEL_NETWORK_SETTINGS (settings),
347                 YAHOO_IMAP_PORT);
348
349         camel_network_settings_set_security_method (
350                 CAMEL_NETWORK_SETTINGS (settings),
351                 YAHOO_IMAP_SECURITY_METHOD);
352 }
353
354 static void
355 yahoo_backend_prepare_mail_transport_source (ESource *source)
356 {
357         ESourceCamel *camel_extension;
358         ESourceExtension *extension;
359         CamelSettings *settings;
360         const gchar *backend_name;
361         const gchar *extension_name;
362
363         /* Configure the mail transport source. */
364
365         backend_name = YAHOO_SMTP_BACKEND_NAME;
366
367         extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
368         extension = e_source_get_extension (source, extension_name);
369
370         e_source_backend_set_backend_name (
371                 E_SOURCE_BACKEND (extension), backend_name);
372
373         extension_name = e_source_camel_get_extension_name (backend_name);
374         camel_extension = e_source_get_extension (source, extension_name);
375         settings = e_source_camel_get_settings (camel_extension);
376
377         /* The "auth-mechanism" should be determined elsewhere. */
378
379         camel_network_settings_set_host (
380                 CAMEL_NETWORK_SETTINGS (settings),
381                 YAHOO_SMTP_HOST);
382
383         camel_network_settings_set_port (
384                 CAMEL_NETWORK_SETTINGS (settings),
385                 YAHOO_SMTP_PORT);
386
387         camel_network_settings_set_security_method (
388                 CAMEL_NETWORK_SETTINGS (settings),
389                 YAHOO_SMTP_SECURITY_METHOD);
390 }
391
392 static void
393 yahoo_backend_factory_prepare_mail (ECollectionBackendFactory *factory,
394                                     ESource *mail_account_source,
395                                     ESource *mail_identity_source,
396                                     ESource *mail_transport_source)
397 {
398         ECollectionBackendFactoryClass *parent_class;
399
400         /* Chain up to parent's prepare_mail() method. */
401         parent_class =
402                 E_COLLECTION_BACKEND_FACTORY_CLASS (
403                 e_yahoo_backend_factory_parent_class);
404         parent_class->prepare_mail (
405                 factory,
406                 mail_account_source,
407                 mail_identity_source,
408                 mail_transport_source);
409
410         yahoo_backend_prepare_mail_account_source (mail_account_source);
411         yahoo_backend_prepare_mail_transport_source (mail_transport_source);
412 }
413
414 static void
415 e_yahoo_backend_factory_class_init (EYahooBackendFactoryClass *class)
416 {
417         ECollectionBackendFactoryClass *factory_class;
418
419         factory_class = E_COLLECTION_BACKEND_FACTORY_CLASS (class);
420         factory_class->factory_name = "yahoo";
421         factory_class->backend_type = E_TYPE_YAHOO_BACKEND;
422         factory_class->prepare_mail = yahoo_backend_factory_prepare_mail;
423 }
424
425 static void
426 e_yahoo_backend_factory_class_finalize (EYahooBackendFactoryClass *class)
427 {
428 }
429
430 static void
431 e_yahoo_backend_factory_init (EYahooBackendFactory *factory)
432 {
433 }
434
435 G_MODULE_EXPORT void
436 e_module_load (GTypeModule *type_module)
437 {
438         e_yahoo_backend_register_type (type_module);
439         e_yahoo_backend_factory_register_type (type_module);
440 }
441
442 G_MODULE_EXPORT void
443 e_module_unload (GTypeModule *type_module)
444 {
445 }
446