2 * module-yahoo-backend.c
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.
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.
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/>
20 #include <glib/gi18n-lib.h>
22 #include <libebackend/libebackend.h>
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))
31 /* Just for readability... */
32 #define METHOD(x) (CAMEL_NETWORK_SECURITY_METHOD_##x)
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)
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)
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 #define YAHOO_CALENDAR_RESOURCE_ID "Calendar"
52 /* Tasks Configuration Details
53 * (mostly the same as calendar) */
54 #define YAHOO_TASKS_RESOURCE_ID "Tasks"
56 typedef struct _EYahooBackend EYahooBackend;
57 typedef struct _EYahooBackendClass EYahooBackendClass;
59 typedef struct _EYahooBackendFactory EYahooBackendFactory;
60 typedef struct _EYahooBackendFactoryClass EYahooBackendFactoryClass;
62 struct _EYahooBackend {
63 ECollectionBackend parent;
64 GWeakRef mail_identity_source;
67 struct _EYahooBackendClass {
68 ECollectionBackendClass parent_class;
71 struct _EYahooBackendFactory {
72 ECollectionBackendFactory parent;
75 struct _EYahooBackendFactoryClass {
76 ECollectionBackendFactoryClass parent_class;
79 /* Module Entry Points */
80 void e_module_load (GTypeModule *type_module);
81 void e_module_unload (GTypeModule *type_module);
83 /* Forward Declarations */
84 GType e_yahoo_backend_get_type (void);
85 GType e_yahoo_backend_factory_get_type (void);
87 G_DEFINE_DYNAMIC_TYPE (
90 E_TYPE_COLLECTION_BACKEND)
92 G_DEFINE_DYNAMIC_TYPE (
94 e_yahoo_backend_factory,
95 E_TYPE_COLLECTION_BACKEND_FACTORY)
98 yahoo_backend_config_calendar_child (ECollectionBackend *backend,
101 EYahooBackend *yahoo_backend;
102 ESource *collection_source;
103 ESource *mail_identity_source;
104 ESourceExtension *extension;
105 ESourceCollection *collection_extension;
106 const gchar *extension_name;
107 const gchar *identity;
108 gchar *display_name = NULL;
110 /* FIXME As a future enhancement, we should query Yahoo!
111 * for a list of user calendars and add them to the
112 * collection with matching display names and colors. */
114 yahoo_backend = E_YAHOO_BACKEND (backend);
116 collection_source = e_backend_get_source (E_BACKEND (backend));
118 collection_extension = e_source_get_extension (
119 collection_source, E_SOURCE_EXTENSION_COLLECTION);
121 identity = e_source_collection_get_identity (collection_extension);
123 /* XXX Assume the calendar's display name can be derived from
124 * the user's mail identity. As mentioned above, we should
125 * really just query Yahoo! for a list of user calendars. */
126 mail_identity_source =
127 g_weak_ref_get (&yahoo_backend->mail_identity_source);
128 if (mail_identity_source != NULL) {
129 extension = e_source_get_extension (
130 mail_identity_source,
131 E_SOURCE_EXTENSION_MAIL_IDENTITY);
132 display_name = e_source_mail_identity_dup_name (
133 E_SOURCE_MAIL_IDENTITY (extension));
134 if (display_name != NULL)
135 g_strdelimit (display_name, " ", '_');
136 g_object_unref (mail_identity_source);
139 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
140 extension = e_source_get_extension (source, extension_name);
142 e_source_authentication_set_host (
143 E_SOURCE_AUTHENTICATION (extension),
144 YAHOO_CALENDAR_HOST);
146 g_object_bind_property (
147 collection_extension, "identity",
149 G_BINDING_SYNC_CREATE);
151 extension_name = E_SOURCE_EXTENSION_SECURITY;
152 extension = e_source_get_extension (source, extension_name);
154 e_source_security_set_secure (
155 E_SOURCE_SECURITY (extension), TRUE);
157 extension_name = E_SOURCE_EXTENSION_WEBDAV_BACKEND;
158 extension = e_source_get_extension (source, extension_name);
160 e_source_webdav_set_display_name (
161 E_SOURCE_WEBDAV (extension), display_name);
163 if (identity != NULL && display_name != NULL) {
164 gchar *resource_path;
166 resource_path = g_strdup_printf (
167 YAHOO_CALENDAR_CALDAV_PATH, identity, display_name);
168 e_source_webdav_set_resource_path (
169 E_SOURCE_WEBDAV (extension), resource_path);
170 g_free (resource_path);
173 g_free (display_name);
177 yahoo_backend_add_calendar (ECollectionBackend *backend)
180 ESourceBackend *extension;
181 ESourceRegistryServer *server;
182 const gchar *backend_name;
183 const gchar *extension_name;
184 const gchar *resource_id;
186 /* XXX We could just stick a [Calendar] and [Task List] extension
187 * into the same ESource since all other settings are exactly
188 * the same. But it might be confusing if tweaking a setting
189 * in your Yahoo! Calendar also gets applied to your Yahoo!
190 * Task List in Evolution. */
192 backend_name = YAHOO_CALENDAR_BACKEND_NAME;
194 server = e_collection_backend_ref_server (backend);
196 /* Add Yahoo! Calendar */
198 resource_id = YAHOO_CALENDAR_RESOURCE_ID;
199 source = e_collection_backend_new_child (backend, resource_id);
200 e_source_set_display_name (source, _("Calendar"));
202 extension_name = E_SOURCE_EXTENSION_CALENDAR;
203 extension = e_source_get_extension (source, extension_name);
204 e_source_backend_set_backend_name (extension, backend_name);
206 yahoo_backend_config_calendar_child (backend, source);
207 e_source_registry_server_add_source (server, source);
209 g_object_unref (source);
211 /* Add Yahoo! Tasks */
213 resource_id = YAHOO_TASKS_RESOURCE_ID;
214 source = e_collection_backend_new_child (backend, resource_id);
215 e_source_set_display_name (source, _("Tasks"));
217 extension_name = E_SOURCE_EXTENSION_TASK_LIST;
218 extension = e_source_get_extension (source, extension_name);
219 e_source_backend_set_backend_name (extension, backend_name);
221 yahoo_backend_config_calendar_child (backend, source);
222 e_source_registry_server_add_source (server, source);
224 g_object_unref (source);
226 g_object_unref (server);
230 yahoo_backend_populate (ECollectionBackend *backend)
234 /* Chain up to parent's populate() method. */
235 E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
238 /* Chain up first so we pick up the mail identity source. */
239 list = e_collection_backend_list_calendar_sources (backend);
241 yahoo_backend_add_calendar (backend);
242 g_list_free_full (list, (GDestroyNotify) g_object_unref);
246 yahoo_backend_dup_resource_id (ECollectionBackend *backend,
247 ESource *child_source)
249 const gchar *extension_name;
251 /* XXX This is trival for now since we only
252 * add one calendar and one task list. */
254 extension_name = E_SOURCE_EXTENSION_CALENDAR;
255 if (e_source_has_extension (child_source, extension_name))
256 return g_strdup (YAHOO_CALENDAR_RESOURCE_ID);
258 extension_name = E_SOURCE_EXTENSION_TASK_LIST;
259 if (e_source_has_extension (child_source, extension_name))
260 return g_strdup (YAHOO_TASKS_RESOURCE_ID);
266 yahoo_backend_child_added (ECollectionBackend *backend,
267 ESource *child_source)
269 EYahooBackend *yahoo_backend;
270 ESource *collection_source;
271 const gchar *extension_name;
272 gboolean is_mail = FALSE;
274 yahoo_backend = E_YAHOO_BACKEND (backend);
275 collection_source = e_backend_get_source (E_BACKEND (backend));
277 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
278 is_mail |= e_source_has_extension (child_source, extension_name);
280 /* Take special note of the mail identity source.
281 * We need it to build the calendar CalDAV path. */
282 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
283 if (e_source_has_extension (child_source, extension_name)) {
286 weak_ref = &yahoo_backend->mail_identity_source;
287 g_weak_ref_set (weak_ref, child_source);
291 extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
292 is_mail |= e_source_has_extension (child_source, extension_name);
294 /* Synchronize mail-related display names with the collection. */
296 g_object_bind_property (
297 collection_source, "display-name",
298 child_source, "display-name",
299 G_BINDING_SYNC_CREATE);
301 /* Synchronize mail-related user with the collection identity. */
302 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
303 if (is_mail && e_source_has_extension (child_source, extension_name)) {
304 ESourceAuthentication *auth_child_extension;
305 ESourceCollection *collection_extension;
307 extension_name = E_SOURCE_EXTENSION_COLLECTION;
308 collection_extension = e_source_get_extension (
309 collection_source, extension_name);
311 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
312 auth_child_extension = e_source_get_extension (
313 child_source, extension_name);
315 g_object_bind_property (
316 collection_extension, "identity",
317 auth_child_extension, "user",
318 G_BINDING_SYNC_CREATE);
321 /* Chain up to parent's child_added() method. */
322 E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
323 child_added (backend, child_source);
327 e_yahoo_backend_class_init (EYahooBackendClass *class)
329 ECollectionBackendClass *backend_class;
331 backend_class = E_COLLECTION_BACKEND_CLASS (class);
332 backend_class->populate = yahoo_backend_populate;
333 backend_class->dup_resource_id = yahoo_backend_dup_resource_id;
334 backend_class->child_added = yahoo_backend_child_added;
338 e_yahoo_backend_class_finalize (EYahooBackendClass *class)
343 e_yahoo_backend_init (EYahooBackend *backend)
348 yahoo_backend_prepare_mail_account_source (ESource *source)
350 ESourceCamel *camel_extension;
351 ESourceExtension *extension;
352 CamelSettings *settings;
353 const gchar *backend_name;
354 const gchar *extension_name;
356 backend_name = YAHOO_IMAP_BACKEND_NAME;
358 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
359 extension = e_source_get_extension (source, extension_name);
361 e_source_backend_set_backend_name (
362 E_SOURCE_BACKEND (extension), backend_name);
364 extension_name = e_source_camel_get_extension_name (backend_name);
365 camel_extension = e_source_get_extension (source, extension_name);
366 settings = e_source_camel_get_settings (camel_extension);
368 /* The "auth-mechanism" should be determined elsewhere. */
370 camel_network_settings_set_host (
371 CAMEL_NETWORK_SETTINGS (settings),
374 camel_network_settings_set_port (
375 CAMEL_NETWORK_SETTINGS (settings),
378 camel_network_settings_set_security_method (
379 CAMEL_NETWORK_SETTINGS (settings),
380 YAHOO_IMAP_SECURITY_METHOD);
384 yahoo_backend_prepare_mail_transport_source (ESource *source)
386 ESourceCamel *camel_extension;
387 ESourceExtension *extension;
388 CamelSettings *settings;
389 const gchar *backend_name;
390 const gchar *extension_name;
392 /* Configure the mail transport source. */
394 backend_name = YAHOO_SMTP_BACKEND_NAME;
396 extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
397 extension = e_source_get_extension (source, extension_name);
399 e_source_backend_set_backend_name (
400 E_SOURCE_BACKEND (extension), backend_name);
402 extension_name = e_source_camel_get_extension_name (backend_name);
403 camel_extension = e_source_get_extension (source, extension_name);
404 settings = e_source_camel_get_settings (camel_extension);
406 /* The "auth-mechanism" should be determined elsewhere. */
408 camel_network_settings_set_host (
409 CAMEL_NETWORK_SETTINGS (settings),
412 camel_network_settings_set_port (
413 CAMEL_NETWORK_SETTINGS (settings),
416 camel_network_settings_set_security_method (
417 CAMEL_NETWORK_SETTINGS (settings),
418 YAHOO_SMTP_SECURITY_METHOD);
422 yahoo_backend_factory_prepare_mail (ECollectionBackendFactory *factory,
423 ESource *mail_account_source,
424 ESource *mail_identity_source,
425 ESource *mail_transport_source)
427 ECollectionBackendFactoryClass *parent_class;
429 /* Chain up to parent's prepare_mail() method. */
431 E_COLLECTION_BACKEND_FACTORY_CLASS (
432 e_yahoo_backend_factory_parent_class);
433 parent_class->prepare_mail (
436 mail_identity_source,
437 mail_transport_source);
439 yahoo_backend_prepare_mail_account_source (mail_account_source);
440 yahoo_backend_prepare_mail_transport_source (mail_transport_source);
444 e_yahoo_backend_factory_class_init (EYahooBackendFactoryClass *class)
446 ECollectionBackendFactoryClass *factory_class;
448 factory_class = E_COLLECTION_BACKEND_FACTORY_CLASS (class);
449 factory_class->factory_name = "yahoo";
450 factory_class->backend_type = E_TYPE_YAHOO_BACKEND;
451 factory_class->prepare_mail = yahoo_backend_factory_prepare_mail;
455 e_yahoo_backend_factory_class_finalize (EYahooBackendFactoryClass *class)
460 e_yahoo_backend_factory_init (EYahooBackendFactory *factory)
465 e_module_load (GTypeModule *type_module)
467 e_yahoo_backend_register_type (type_module);
468 e_yahoo_backend_factory_register_type (type_module);
472 e_module_unload (GTypeModule *type_module)