1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003 Ximian, Inc.
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.
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.
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.
24 #include "e-account-list.h"
25 #include "e-account.h"
29 struct EAccountListPrivate {
41 static guint signals [LAST_SIGNAL] = { 0 };
43 static void dispose (GObject *);
44 static void finalize (GObject *);
46 G_DEFINE_TYPE (EAccountList, e_account_list, E_TYPE_LIST);
49 e_account_list_class_init (EAccountListClass *klass)
51 GObjectClass *object_class = G_OBJECT_CLASS (klass);
53 /* virtual method override */
54 object_class->dispose = dispose;
55 object_class->finalize = finalize;
58 signals[ACCOUNT_ADDED] =
59 g_signal_new ("account-added",
60 G_OBJECT_CLASS_TYPE (object_class),
62 G_STRUCT_OFFSET (EAccountListClass, account_added),
64 g_cclosure_marshal_VOID__OBJECT,
67 signals[ACCOUNT_CHANGED] =
68 g_signal_new ("account-changed",
69 G_OBJECT_CLASS_TYPE (object_class),
71 G_STRUCT_OFFSET (EAccountListClass, account_changed),
73 g_cclosure_marshal_VOID__OBJECT,
76 signals[ACCOUNT_REMOVED] =
77 g_signal_new ("account-removed",
78 G_OBJECT_CLASS_TYPE (object_class),
80 G_STRUCT_OFFSET (EAccountListClass, account_removed),
82 g_cclosure_marshal_VOID__OBJECT,
88 e_account_list_init (EAccountList *account_list)
90 account_list->priv = g_new0 (EAccountListPrivate, 1);
94 dispose (GObject *object)
96 EAccountList *account_list = E_ACCOUNT_LIST (object);
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);
103 g_object_unref (account_list->priv->gconf);
104 account_list->priv->gconf = NULL;
107 G_OBJECT_CLASS (e_account_list_parent_class)->dispose (object);
111 finalize (GObject *object)
113 EAccountList *account_list = E_ACCOUNT_LIST (object);
115 g_free (account_list->priv);
117 G_OBJECT_CLASS (e_account_list_parent_class)->finalize (object);
121 gconf_accounts_changed (GConfClient *client, guint cnxn_id,
122 GConfEntry *entry, gpointer user_data)
124 EAccountList *account_list = user_data;
125 GSList *list, *l, *new_accounts = NULL;
131 old_accounts = e_list_duplicate (E_LIST (account_list));
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);
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.
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);
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);
163 g_object_unref (iter);
167 g_slist_foreach (list, (GFunc) g_free, NULL);
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
177 for (l = new_accounts; l; l = l->next) {
179 g_signal_emit (account_list, signals[ACCOUNT_ADDED], 0, account);
180 g_object_unref (account);
182 g_slist_free (new_accounts);
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);
192 g_object_unref (iter);
193 g_object_unref (old_accounts);
197 copy_func (const void *data, void *closure)
199 GObject *object = (GObject *)data;
201 g_object_ref (object);
206 free_func (void *data, void *closure)
208 g_object_unref (data);
212 * e_account_list_new:
213 * @gconf: a #GConfClient
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.
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
224 * Return value: the list of accounts
227 e_account_list_new (GConfClient *gconf)
229 EAccountList *account_list;
231 g_return_val_if_fail (GCONF_IS_CLIENT (gconf), NULL);
233 account_list = g_object_new (E_TYPE_ACCOUNT_LIST, NULL);
234 e_account_list_construct (account_list, gconf);
240 e_account_list_construct (EAccountList *account_list, GConfClient *gconf)
242 g_return_if_fail (GCONF_IS_CLIENT (gconf));
244 e_list_construct (E_LIST (account_list), copy_func, free_func, NULL);
245 account_list->priv->gconf = gconf;
246 g_object_ref (gconf);
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,
257 gconf_accounts_changed (account_list->priv->gconf,
258 account_list->priv->notify_id,
263 * e_account_list_save:
264 * @account_list: an #EAccountList
266 * Saves @account_list to GConf. Signals will be emitted for changes.
269 e_account_list_save (EAccountList *account_list)
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);
281 xmlbuf = e_account_to_xml (account);
283 list = g_slist_append (list, xmlbuf);
285 g_object_unref (iter);
287 gconf_client_set_list (account_list->priv->gconf,
288 "/apps/evolution/mail/accounts",
289 GCONF_VALUE_STRING, list, NULL);
293 list = g_slist_remove (list, list->data);
296 gconf_client_suggest_sync (account_list->priv->gconf, NULL);
300 e_account_list_prune_proxies (EAccountList *account_list)
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);
313 e_account_list_save (account_list);
314 g_object_unref (iter);
318 e_account_list_remove_account_proxies (EAccountList *accounts, EAccount *account)
320 EAccount *child_account;
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;
327 e_account_list_save (accounts);
331 e_account_list_account_has_proxies (EAccountList *accounts, EAccount *account)
333 if (e_account_list_find (accounts, E_ACCOUNT_FIND_PARENT_UID, account->uid))
340 * e_account_list_add:
344 * Add an account to the account list. Will emit the account-changed
348 e_account_list_add(EAccountList *accounts, EAccount *account)
350 /* FIXME: should we check for duplicate accounts? */
352 e_list_append ((EList *)accounts, account);
353 g_signal_emit(accounts, signals[ACCOUNT_ADDED], 0, account);
357 * e_account_list_change:
361 * Signal that the details of an account have changed.
364 e_account_list_change(EAccountList *accounts, EAccount *account)
366 /* maybe the account should do this itself ... */
367 g_signal_emit(accounts, signals[ACCOUNT_CHANGED], 0, account);
371 * e_account_list_remove:
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.
380 e_account_list_remove(EAccountList *accounts, EAccount *account)
382 if (account == e_account_list_get_default(accounts))
383 gconf_client_unset (accounts->priv->gconf, "/apps/evolution/mail/default_account", NULL);
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);
393 * e_account_list_get_default:
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.
399 * Return value: The account or NULL if no accounts are defined.
402 e_account_list_get_default(EAccountList *accounts)
406 const EAccount *account = NULL;
408 uid = gconf_client_get_string (accounts->priv->gconf, "/apps/evolution/mail/default_account", NULL);
409 it = e_list_get_iterator ((EList *)accounts);
412 for (;e_iterator_is_valid (it);e_iterator_next (it)) {
413 account = (const EAccount *)e_iterator_get (it);
415 if (!strcmp(uid, account->uid))
419 e_iterator_reset(it);
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);
435 * e_account_list_set_default:
439 * Set the account @account to be the default account.
442 e_account_list_set_default(EAccountList *accounts, EAccount *account)
444 gconf_client_set_string (accounts->priv->gconf, "/apps/evolution/mail/default_account", account->uid, NULL);
448 * e_account_list_find:
450 * @type: Type of search.
453 * Perform a search of the account list on a single key.
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.
460 * Return value: The account or NULL if it doesn't exist.
463 e_account_list_find(EAccountList *accounts, e_account_find_t type, const char *key)
467 const EAccount *account = NULL;
469 /* this could use a callback for more flexibility ...
470 ... but this makes the common cases easier */
475 for (it = e_list_get_iterator ((EList *)accounts);
476 e_iterator_is_valid (it);
477 e_iterator_next (it)) {
480 account = (const EAccount *)e_iterator_get (it);
484 case E_ACCOUNT_FIND_NAME:
485 found = strcmp(account->name, key) == 0;
487 case E_ACCOUNT_FIND_UID:
488 found = strcmp(account->uid, key) == 0;
490 case E_ACCOUNT_FIND_ID_NAME:
492 found = strcmp(account->id->name, key) == 0;
494 case E_ACCOUNT_FIND_ID_ADDRESS:
496 found = g_ascii_strcasecmp(account->id->address, key) == 0;
498 case E_ACCOUNT_FIND_PARENT_UID:
499 if (account->parent_uid)
500 found = strcmp(account->parent_uid, key) == 0;