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"
51 typedef struct _EYahooBackend EYahooBackend;
52 typedef struct _EYahooBackendClass EYahooBackendClass;
54 typedef struct _EYahooBackendFactory EYahooBackendFactory;
55 typedef struct _EYahooBackendFactoryClass EYahooBackendFactoryClass;
57 struct _EYahooBackend {
58 ECollectionBackend parent;
59 GWeakRef mail_identity_source;
62 struct _EYahooBackendClass {
63 ECollectionBackendClass parent_class;
66 struct _EYahooBackendFactory {
67 ECollectionBackendFactory parent;
70 struct _EYahooBackendFactoryClass {
71 ECollectionBackendFactoryClass parent_class;
74 /* Module Entry Points */
75 void e_module_load (GTypeModule *type_module);
76 void e_module_unload (GTypeModule *type_module);
78 /* Forward Declarations */
79 GType e_yahoo_backend_get_type (void);
80 GType e_yahoo_backend_factory_get_type (void);
82 G_DEFINE_DYNAMIC_TYPE (
85 E_TYPE_COLLECTION_BACKEND)
87 G_DEFINE_DYNAMIC_TYPE (
89 e_yahoo_backend_factory,
90 E_TYPE_COLLECTION_BACKEND_FACTORY)
93 yahoo_backend_config_calendar_child (ECollectionBackend *backend,
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;
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. */
109 yahoo_backend = E_YAHOO_BACKEND (backend);
111 collection_source = e_backend_get_source (E_BACKEND (backend));
113 collection_extension = e_source_get_extension (
114 collection_source, E_SOURCE_EXTENSION_COLLECTION);
116 identity = e_source_collection_get_identity (collection_extension);
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);
134 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
135 extension = e_source_get_extension (source, extension_name);
137 e_source_authentication_set_host (
138 E_SOURCE_AUTHENTICATION (extension),
139 YAHOO_CALENDAR_HOST);
141 g_object_bind_property (
142 collection_extension, "identity",
144 G_BINDING_SYNC_CREATE);
146 extension_name = E_SOURCE_EXTENSION_SECURITY;
147 extension = e_source_get_extension (source, extension_name);
149 e_source_security_set_secure (
150 E_SOURCE_SECURITY (extension), TRUE);
152 extension_name = E_SOURCE_EXTENSION_WEBDAV_BACKEND;
153 extension = e_source_get_extension (source, extension_name);
155 e_source_webdav_set_display_name (
156 E_SOURCE_WEBDAV (extension), display_name);
158 if (identity != NULL && display_name != NULL) {
159 gchar *resource_path;
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);
168 g_free (display_name);
172 yahoo_backend_add_calendar (ECollectionBackend *backend)
175 ESourceBackend *extension;
176 ESourceRegistryServer *server;
177 const gchar *backend_name;
178 const gchar *extension_name;
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. */
186 backend_name = YAHOO_CALENDAR_BACKEND_NAME;
188 server = e_collection_backend_ref_server (backend);
190 /* Add Yahoo! Calendar */
192 source = e_collection_backend_new_child (backend, "Calendar");
193 e_source_set_display_name (source, _("Calendar"));
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);
199 yahoo_backend_config_calendar_child (backend, source);
200 e_source_registry_server_add_source (server, source);
202 g_object_unref (source);
204 /* Add Yahoo! Tasks */
206 source = e_collection_backend_new_child (backend, "Tasks");
207 e_source_set_display_name (source, _("Tasks"));
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);
213 yahoo_backend_config_calendar_child (backend, source);
214 e_source_registry_server_add_source (server, source);
216 g_object_unref (source);
218 g_object_unref (server);
222 yahoo_backend_populate (ECollectionBackend *backend)
226 /* Chain up to parent's populate() method. */
227 E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
230 /* Chain up first so we pick up the mail identity source. */
231 list = e_collection_backend_list_calendar_sources (backend);
233 yahoo_backend_add_calendar (backend);
234 g_list_free_full (list, (GDestroyNotify) g_object_unref);
238 yahoo_backend_child_added (ECollectionBackend *backend,
239 ESource *child_source)
241 EYahooBackend *yahoo_backend;
242 ESource *collection_source;
243 const gchar *extension_name;
244 gboolean is_mail = FALSE;
246 yahoo_backend = E_YAHOO_BACKEND (backend);
247 collection_source = e_backend_get_source (E_BACKEND (backend));
249 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
250 is_mail |= e_source_has_extension (child_source, extension_name);
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)) {
258 weak_ref = &yahoo_backend->mail_identity_source;
259 g_weak_ref_set (weak_ref, child_source);
263 extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
264 is_mail |= e_source_has_extension (child_source, extension_name);
266 /* Synchronize mail-related display names with the collection. */
268 g_object_bind_property (
269 collection_source, "display-name",
270 child_source, "display-name",
271 G_BINDING_SYNC_CREATE);
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;
279 extension_name = E_SOURCE_EXTENSION_COLLECTION;
280 collection_extension = e_source_get_extension (
281 collection_source, extension_name);
283 extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
284 auth_child_extension = e_source_get_extension (
285 child_source, extension_name);
287 g_object_bind_property (
288 collection_extension, "identity",
289 auth_child_extension, "user",
290 G_BINDING_SYNC_CREATE);
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);
299 e_yahoo_backend_class_init (EYahooBackendClass *class)
301 ECollectionBackendClass *backend_class;
303 backend_class = E_COLLECTION_BACKEND_CLASS (class);
304 backend_class->populate = yahoo_backend_populate;
305 backend_class->child_added = yahoo_backend_child_added;
309 e_yahoo_backend_class_finalize (EYahooBackendClass *class)
314 e_yahoo_backend_init (EYahooBackend *backend)
319 yahoo_backend_prepare_mail_account_source (ESource *source)
321 ESourceCamel *camel_extension;
322 ESourceExtension *extension;
323 CamelSettings *settings;
324 const gchar *backend_name;
325 const gchar *extension_name;
327 backend_name = YAHOO_IMAP_BACKEND_NAME;
329 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
330 extension = e_source_get_extension (source, extension_name);
332 e_source_backend_set_backend_name (
333 E_SOURCE_BACKEND (extension), backend_name);
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);
339 /* The "auth-mechanism" should be determined elsewhere. */
341 camel_network_settings_set_host (
342 CAMEL_NETWORK_SETTINGS (settings),
345 camel_network_settings_set_port (
346 CAMEL_NETWORK_SETTINGS (settings),
349 camel_network_settings_set_security_method (
350 CAMEL_NETWORK_SETTINGS (settings),
351 YAHOO_IMAP_SECURITY_METHOD);
355 yahoo_backend_prepare_mail_transport_source (ESource *source)
357 ESourceCamel *camel_extension;
358 ESourceExtension *extension;
359 CamelSettings *settings;
360 const gchar *backend_name;
361 const gchar *extension_name;
363 /* Configure the mail transport source. */
365 backend_name = YAHOO_SMTP_BACKEND_NAME;
367 extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
368 extension = e_source_get_extension (source, extension_name);
370 e_source_backend_set_backend_name (
371 E_SOURCE_BACKEND (extension), backend_name);
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);
377 /* The "auth-mechanism" should be determined elsewhere. */
379 camel_network_settings_set_host (
380 CAMEL_NETWORK_SETTINGS (settings),
383 camel_network_settings_set_port (
384 CAMEL_NETWORK_SETTINGS (settings),
387 camel_network_settings_set_security_method (
388 CAMEL_NETWORK_SETTINGS (settings),
389 YAHOO_SMTP_SECURITY_METHOD);
393 yahoo_backend_factory_prepare_mail (ECollectionBackendFactory *factory,
394 ESource *mail_account_source,
395 ESource *mail_identity_source,
396 ESource *mail_transport_source)
398 ECollectionBackendFactoryClass *parent_class;
400 /* Chain up to parent's prepare_mail() method. */
402 E_COLLECTION_BACKEND_FACTORY_CLASS (
403 e_yahoo_backend_factory_parent_class);
404 parent_class->prepare_mail (
407 mail_identity_source,
408 mail_transport_source);
410 yahoo_backend_prepare_mail_account_source (mail_account_source);
411 yahoo_backend_prepare_mail_transport_source (mail_transport_source);
415 e_yahoo_backend_factory_class_init (EYahooBackendFactoryClass *class)
417 ECollectionBackendFactoryClass *factory_class;
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;
426 e_yahoo_backend_factory_class_finalize (EYahooBackendFactoryClass *class)
431 e_yahoo_backend_factory_init (EYahooBackendFactory *factory)
436 e_module_load (GTypeModule *type_module)
438 e_yahoo_backend_register_type (type_module);
439 e_yahoo_backend_factory_register_type (type_module);
443 e_module_unload (GTypeModule *type_module)