Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / libedataserver / e-account-list.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003 Ximian, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
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  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "e-account-list.h"
25 #include "e-account.h"
26
27 #include <string.h>
28
29 struct EAccountListPrivate {
30         GConfClient *gconf;
31         guint notify_id;
32 };
33
34 enum {
35         ACCOUNT_ADDED,
36         ACCOUNT_CHANGED,
37         ACCOUNT_REMOVED,
38         LAST_SIGNAL
39 };
40
41 static guint signals [LAST_SIGNAL] = { 0 };
42
43 static void dispose (GObject *);
44 static void finalize (GObject *);
45
46 G_DEFINE_TYPE (EAccountList, e_account_list, E_TYPE_LIST);
47
48 static void
49 e_account_list_class_init (EAccountListClass *klass)
50 {
51         GObjectClass *object_class = G_OBJECT_CLASS (klass);
52
53         /* virtual method override */
54         object_class->dispose = dispose;
55         object_class->finalize = finalize;
56
57         /* signals */
58         signals[ACCOUNT_ADDED] =
59                 g_signal_new ("account-added",
60                               G_OBJECT_CLASS_TYPE (object_class),
61                               G_SIGNAL_RUN_LAST,
62                               G_STRUCT_OFFSET (EAccountListClass, account_added),
63                               NULL, NULL,
64                               g_cclosure_marshal_VOID__OBJECT,
65                               G_TYPE_NONE, 1,
66                               E_TYPE_ACCOUNT);
67         signals[ACCOUNT_CHANGED] =
68                 g_signal_new ("account-changed",
69                               G_OBJECT_CLASS_TYPE (object_class),
70                               G_SIGNAL_RUN_LAST,
71                               G_STRUCT_OFFSET (EAccountListClass, account_changed),
72                               NULL, NULL,
73                               g_cclosure_marshal_VOID__OBJECT,
74                               G_TYPE_NONE, 1,
75                               E_TYPE_ACCOUNT);
76         signals[ACCOUNT_REMOVED] =
77                 g_signal_new ("account-removed",
78                               G_OBJECT_CLASS_TYPE (object_class),
79                               G_SIGNAL_RUN_LAST,
80                               G_STRUCT_OFFSET (EAccountListClass, account_removed),
81                               NULL, NULL,
82                               g_cclosure_marshal_VOID__OBJECT,
83                               G_TYPE_NONE, 1,
84                               E_TYPE_ACCOUNT);
85 }
86
87 static void
88 e_account_list_init (EAccountList *account_list)
89 {
90         account_list->priv = g_new0 (EAccountListPrivate, 1);
91 }
92
93 static void
94 dispose (GObject *object)
95 {
96         EAccountList *account_list = E_ACCOUNT_LIST (object);
97
98         if (account_list->priv->gconf) {
99                 if (account_list->priv->notify_id) {
100                         gconf_client_notify_remove (account_list->priv->gconf,
101                                                     account_list->priv->notify_id);
102                 }
103                 g_object_unref (account_list->priv->gconf);
104                 account_list->priv->gconf = NULL;
105         }
106
107         G_OBJECT_CLASS (e_account_list_parent_class)->dispose (object);
108 }
109
110 static void
111 finalize (GObject *object)
112 {
113         EAccountList *account_list = E_ACCOUNT_LIST (object);
114
115         g_free (account_list->priv);
116
117         G_OBJECT_CLASS (e_account_list_parent_class)->finalize (object);
118 }
119
120 static void
121 gconf_accounts_changed (GConfClient *client, guint cnxn_id,
122                         GConfEntry *entry, gpointer user_data)
123 {
124         EAccountList *account_list = user_data;
125         GSList *list, *l, *new_accounts = NULL;
126         EAccount *account;
127         EList *old_accounts;
128         EIterator *iter;
129         char *uid;
130
131         old_accounts = e_list_duplicate (E_LIST (account_list));
132
133         list = gconf_client_get_list (client, "/apps/evolution/mail/accounts",
134                                       GCONF_VALUE_STRING, NULL);
135         for (l = list; l; l = l->next) {
136                 uid = e_account_uid_from_xml (l->data);
137                 if (!uid)
138                         continue;
139
140                 /* See if this is an existing account */
141                 for (iter = e_list_get_iterator (old_accounts);
142                      e_iterator_is_valid (iter);
143                      e_iterator_next (iter)) {
144                         account = (EAccount *)e_iterator_get (iter);
145                         if (!strcmp (account->uid, uid)) {
146                                 /* The account still exists, so remove
147                                  * it from "old_accounts" and update it.
148                                  */
149                                 e_iterator_delete (iter);
150                                 if (e_account_set_from_xml (account, l->data))
151                                         g_signal_emit (account_list, signals[ACCOUNT_CHANGED], 0, account);
152                                 goto next;
153                         }
154                 }
155
156                 /* Must be a new account */
157                 account = e_account_new_from_xml (l->data);
158                 e_list_append (E_LIST (account_list), account);
159                 new_accounts = g_slist_prepend (new_accounts, account);
160
161         next:
162                 g_free (uid);
163                 g_object_unref (iter);
164         }
165
166         if (list) {
167                 g_slist_foreach (list, (GFunc) g_free, NULL);
168                 g_slist_free (list);
169         }
170
171         /* Now emit signals for each added account. (We do this after
172          * adding all of them because otherwise if the signal handler
173          * calls e_account_list_get_default_account() it will end up
174          * causing the first account in the list to become the
175          * default.)
176          */
177         for (l = new_accounts; l; l = l->next) {
178                 account = l->data;
179                 g_signal_emit (account_list, signals[ACCOUNT_ADDED], 0, account);
180                 g_object_unref (account);
181         }
182         g_slist_free (new_accounts);
183
184         /* Anything left in old_accounts must have been deleted */
185         for (iter = e_list_get_iterator (old_accounts);
186              e_iterator_is_valid (iter);
187              e_iterator_next (iter)) {
188                 account = (EAccount *)e_iterator_get (iter);
189                 e_list_remove (E_LIST (account_list), account);
190                 g_signal_emit (account_list, signals[ACCOUNT_REMOVED], 0, account);
191         }
192         g_object_unref (iter);
193         g_object_unref (old_accounts);
194 }
195
196 static void *
197 copy_func (const void *data, void *closure)
198 {
199         GObject *object = (GObject *)data;
200
201         g_object_ref (object);
202         return object;
203 }
204
205 static void
206 free_func (void *data, void *closure)
207 {
208         g_object_unref (data);
209 }
210
211 /**
212  * e_account_list_new:
213  * @gconf: a #GConfClient
214  *
215  * Reads the list of accounts from @gconf and listens for changes.
216  * Will emit %account_added, %account_changed, and %account_removed
217  * signals according to notifications from GConf.
218  *
219  * You can modify the list using e_list_append(), e_list_remove(), and
220  * e_iterator_delete(). After adding, removing, or changing accounts,
221  * you must call e_account_list_save() to push the changes back to
222  * GConf.
223  *
224  * Return value: the list of accounts
225  **/
226 EAccountList *
227 e_account_list_new (GConfClient *gconf)
228 {
229         EAccountList *account_list;
230
231         g_return_val_if_fail (GCONF_IS_CLIENT (gconf), NULL);
232
233         account_list = g_object_new (E_TYPE_ACCOUNT_LIST, NULL);
234         e_account_list_construct (account_list, gconf);
235
236         return account_list;
237 }
238
239 void
240 e_account_list_construct (EAccountList *account_list, GConfClient *gconf)
241 {
242         g_return_if_fail (GCONF_IS_CLIENT (gconf));
243
244         e_list_construct (E_LIST (account_list), copy_func, free_func, NULL);
245         account_list->priv->gconf = gconf;
246         g_object_ref (gconf);
247
248         gconf_client_add_dir (account_list->priv->gconf,
249                               "/apps/evolution/mail/accounts",
250                               GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
251         account_list->priv->notify_id =
252                 gconf_client_notify_add (account_list->priv->gconf,
253                                          "/apps/evolution/mail/accounts",
254                                          gconf_accounts_changed, account_list,
255                                          NULL, NULL);
256
257         gconf_accounts_changed (account_list->priv->gconf,
258                                 account_list->priv->notify_id,
259                                 NULL, account_list);
260 }
261
262 /**
263  * e_account_list_save:
264  * @account_list: an #EAccountList
265  *
266  * Saves @account_list to GConf. Signals will be emitted for changes.
267  **/
268 void
269 e_account_list_save (EAccountList *account_list)
270 {
271         GSList *list = NULL;
272         EAccount *account;
273         EIterator *iter;
274         char *xmlbuf;
275
276         for (iter = e_list_get_iterator (E_LIST (account_list));
277              e_iterator_is_valid (iter);
278              e_iterator_next (iter)) {
279                 account = (EAccount *)e_iterator_get (iter);
280
281                 xmlbuf = e_account_to_xml (account);
282                 if (xmlbuf)
283                         list = g_slist_append (list, xmlbuf);
284         }
285         g_object_unref (iter);
286
287         gconf_client_set_list (account_list->priv->gconf,
288                                "/apps/evolution/mail/accounts",
289                                GCONF_VALUE_STRING, list, NULL);
290
291         while (list) {
292                 g_free (list->data);
293                 list = g_slist_remove (list, list->data);
294         }
295
296         gconf_client_suggest_sync (account_list->priv->gconf, NULL);
297 }
298
299 void 
300 e_account_list_prune_proxies (EAccountList *account_list)
301 {
302         EAccount *account;
303         EIterator *iter;
304         
305         for (iter = e_list_get_iterator (E_LIST (account_list));
306              e_iterator_is_valid (iter);
307              e_iterator_next (iter)) {
308                 account = (EAccount *)e_iterator_get (iter);
309                 if (account->parent_uid) 
310                         e_account_list_remove (account_list, account);
311         }
312
313         e_account_list_save (account_list);
314         g_object_unref (iter);
315 }
316
317 void 
318 e_account_list_remove_account_proxies (EAccountList *accounts, EAccount *account)
319 {
320         EAccount *child_account;
321
322         while ( (child_account = (EAccount *)e_account_list_find (accounts, E_ACCOUNT_FIND_PARENT_UID, account->uid))) {
323                 e_account_list_remove (accounts, child_account);
324                 child_account = NULL;
325         }
326
327         e_account_list_save (accounts);
328 }
329
330 int
331 e_account_list_account_has_proxies (EAccountList *accounts, EAccount *account)
332 {
333         if (e_account_list_find (accounts, E_ACCOUNT_FIND_PARENT_UID, account->uid))
334                 return TRUE;
335
336         return FALSE;
337 }
338
339 /**
340  * e_account_list_add:
341  * @accounts: 
342  * @account: 
343  * 
344  * Add an account to the account list.  Will emit the account-changed
345  * event.
346  **/
347 void
348 e_account_list_add(EAccountList *accounts, EAccount *account)
349 {
350         /* FIXME: should we check for duplicate accounts? */
351
352         e_list_append ((EList *)accounts, account);
353         g_signal_emit(accounts, signals[ACCOUNT_ADDED], 0, account);
354 }
355
356 /**
357  * e_account_list_change:
358  * @accounts: 
359  * @account: 
360  * 
361  * Signal that the details of an account have changed.
362  **/
363 void
364 e_account_list_change(EAccountList *accounts, EAccount *account)
365 {
366         /* maybe the account should do this itself ... */
367         g_signal_emit(accounts, signals[ACCOUNT_CHANGED], 0, account);
368 }
369
370 /**
371  * e_account_list_remove:
372  * @accounts: 
373  * @account: 
374  * 
375  * Remove an account from the account list, and emit the
376  * account-removed signal.  If the account was the default account,
377  * then reset the default to the first account.
378  **/
379 void
380 e_account_list_remove(EAccountList *accounts, EAccount *account)
381 {
382         if (account == e_account_list_get_default(accounts))
383                 gconf_client_unset (accounts->priv->gconf, "/apps/evolution/mail/default_account", NULL);
384
385         /* not sure if need to ref but no harm */
386         g_object_ref (account);
387         e_list_remove ((EList *) accounts, account);
388         g_signal_emit(accounts, signals[ACCOUNT_REMOVED], 0, account);
389         g_object_unref (account);
390 }
391
392 /**
393  * e_account_list_get_default:
394  * @accounts: 
395  * 
396  * Get the default account.  If no default is specified, or the default
397  * has become stale, then the first account is made the default.
398  * 
399  * Return value: The account or NULL if no accounts are defined.
400  **/
401 const EAccount *
402 e_account_list_get_default(EAccountList *accounts)
403 {
404         char *uid;
405         EIterator *it;
406         const EAccount *account = NULL;
407
408         uid = gconf_client_get_string (accounts->priv->gconf, "/apps/evolution/mail/default_account", NULL);
409         it = e_list_get_iterator ((EList *)accounts);
410
411         if (uid) {
412                 for (;e_iterator_is_valid (it);e_iterator_next (it)) {
413                         account = (const EAccount *)e_iterator_get (it);
414
415                         if (!strcmp(uid, account->uid))
416                                 break;
417                         account = NULL;
418                 }
419                 e_iterator_reset(it);
420         }
421
422         /* no uid or uid not found, @it will be at the first account */
423         if (account == NULL && e_iterator_is_valid(it)) {
424                 account = (const EAccount *) e_iterator_get (it);
425                 gconf_client_set_string (accounts->priv->gconf, "/apps/evolution/mail/default_account", account->uid, NULL);
426         }
427
428         g_object_unref(it);
429         g_free(uid);
430
431         return account;
432 }
433
434 /**
435  * e_account_list_set_default:
436  * @accounts: 
437  * @account: 
438  * 
439  * Set the account @account to be the default account.
440  **/
441 void
442 e_account_list_set_default(EAccountList *accounts, EAccount *account)
443 {
444         gconf_client_set_string (accounts->priv->gconf, "/apps/evolution/mail/default_account", account->uid, NULL);
445 }
446
447 /**
448  * e_account_list_find:
449  * @accounts: 
450  * @type: Type of search.
451  * @key: Search key.
452  * 
453  * Perform a search of the account list on a single key.
454  *
455  * @type must be set from one of the following search types:
456  * E_ACCOUNT_FIND_NAME - Find an account by account name.
457  * E_ACCOUNT_FIND_ID_NAME - Find an account by the owner's identity name.
458  * E_ACCOUNT_FIND_ID_ADDRESS - Find an account by the owner's identity address.
459  * 
460  * Return value: The account or NULL if it doesn't exist.
461  **/
462 const EAccount *
463 e_account_list_find(EAccountList *accounts, e_account_find_t type, const char *key)
464 {
465         char *val;
466         EIterator *it;
467         const EAccount *account = NULL;
468
469         /* this could use a callback for more flexibility ...
470            ... but this makes the common cases easier */
471
472         if (!key)
473                 return NULL;
474         
475         for (it = e_list_get_iterator ((EList *)accounts);
476              e_iterator_is_valid (it);
477              e_iterator_next (it)) {
478                 int found = 0;
479
480                 account = (const EAccount *)e_iterator_get (it);
481
482                 val = NULL;
483                 switch(type) {
484                 case E_ACCOUNT_FIND_NAME:
485                         found = strcmp(account->name, key) == 0;
486                         break;
487                 case E_ACCOUNT_FIND_UID:
488                         found = strcmp(account->uid, key) == 0;
489                         break;
490                 case E_ACCOUNT_FIND_ID_NAME:
491                         if (account->id)
492                                 found = strcmp(account->id->name, key) == 0;
493                         break;
494                 case E_ACCOUNT_FIND_ID_ADDRESS:
495                         if (account->id)
496                                 found = g_ascii_strcasecmp(account->id->address, key) == 0;
497                         break;
498                 case E_ACCOUNT_FIND_PARENT_UID:
499                         if (account->parent_uid)
500                                 found = strcmp(account->parent_uid, key) == 0;
501                         break;
502                 }
503
504                 if (found)
505                         break;
506
507                 account = NULL;
508         }
509         g_object_unref(it);
510
511         return account;
512 }
513