* added licensing boilerplate to source files
[modest] / src / modest-account-mgr.c
index 1eb0d6c..04d2b58 100644 (file)
@@ -1,8 +1,38 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 /* modest-account-mgr.c */
 
-/* insert (c)/licensing information) */
 
 #include <string.h>
+#include "modest-marshal.h"
+#include "modest-account-keys.h"
 #include "modest-account-mgr.h"
 
 /* 'private'/'protected' functions */
@@ -16,14 +46,16 @@ static gchar *get_server_account_keyname (const gchar * accname,
 
 /* list my signals */
 enum {
-       /* MY_SIGNAL_1, */
-       /* MY_SIGNAL_2, */
+       ACCOUNT_CHANGE_SIGNAL,
+       ACCOUNT_REMOVE_SIGNAL,
+       ACCOUNT_ADD_SIGNAL,
        LAST_SIGNAL
 };
 
 typedef struct _ModestAccountMgrPrivate ModestAccountMgrPrivate;
 struct _ModestAccountMgrPrivate {
        ModestConf *modest_conf;
+       GSList *current_accounts;
 };
 
 #define MODEST_ACCOUNT_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -32,8 +64,89 @@ struct _ModestAccountMgrPrivate {
 /* globals */
 static GObjectClass *parent_class = NULL;
 
-/* uncomment the following if you have defined any signals */
-/* static guint signals[LAST_SIGNAL] = {0}; */
+static guint signals[LAST_SIGNAL] = {0};
+
+
+static GSList *
+delete_account_from_list (GSList *list, const gchar *name)
+{
+       GSList *iter, *result;
+
+       iter = list;
+       result = list;
+       while (iter) {
+               if (!strcmp (name, iter->data)) {
+                       result = g_slist_delete_link (list, iter);
+                       break;
+               }
+
+               iter = g_slist_next (iter);
+       }
+       return result;
+}
+
+static GSList *
+find_account_in_list (GSList *list, const gchar *name)
+{
+       GSList *iter, *result;
+
+       iter = list;
+       result = list;
+       while (iter) {
+               if (!strcmp (name, iter->data)) {
+                       return iter;
+                       break;
+               }
+
+               iter = g_slist_next (iter);
+       }
+       return NULL;
+}
+
+/* Map configuration changes to account changes.
+ * Doing this here makes sure all changes are available and external changes
+ * are covered as well. */
+
+static void
+modest_account_mgr_check_change (ModestConf *conf, const gchar *key,
+                                 const gchar *new_value, gpointer user_data)
+{
+       ModestAccountMgr *amgr = user_data;
+       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (amgr);
+
+       if ((strlen (key) > strlen (MODEST_SERVER_ACCOUNT_NAMESPACE "/")
+            && g_str_has_prefix (key, MODEST_SERVER_ACCOUNT_NAMESPACE))) {
+               gchar *subkey = g_strdup(key + strlen (MODEST_SERVER_ACCOUNT_NAMESPACE "/"));
+
+               if (! strstr (subkey, "/")) { /* no more '/' means an account was modified */
+                       if (!new_value) {
+                               priv->current_accounts =
+                                       delete_account_from_list (priv->current_accounts, subkey);
+
+                               g_signal_emit (amgr, signals[ACCOUNT_REMOVE_SIGNAL], 0, subkey);
+                       }
+               }
+               else {
+                       gchar *param;
+
+                       param = strstr (subkey, "/");
+                       param [0] = 0;
+                       param++;
+
+                       /* that's the second case for a new account */
+                       if (!find_account_in_list (priv->current_accounts, subkey) && strstr (param, MODEST_ACCOUNT_PROTO)) {
+                               priv->current_accounts =
+                                       g_slist_prepend (priv->current_accounts, g_strdup (subkey));
+                               g_signal_emit (amgr, signals[ACCOUNT_ADD_SIGNAL], 0, subkey);
+                       }
+
+                       g_signal_emit (amgr, signals[ACCOUNT_CHANGE_SIGNAL], 0, subkey, param, new_value);
+               }
+
+               g_free (subkey);
+       }
+}
+
 
 GType
 modest_account_mgr_get_type (void)
@@ -64,6 +177,7 @@ static void
 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
 {
        GObjectClass *gobject_class;
+       GType paramtypes[3] = {G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER};
 
        gobject_class = (GObjectClass *) klass;
 
@@ -73,12 +187,26 @@ modest_account_mgr_class_init (ModestAccountMgrClass * klass)
        g_type_class_add_private (gobject_class,
                                  sizeof (ModestAccountMgrPrivate));
 
-       /* signal definitions go here, e.g.: */
-/*     signals[MY_SIGNAL_1] = */
-/*             g_signal_new ("my_signal_1",....); */
-/*     signals[MY_SIGNAL_2] = */
-/*             g_signal_new ("my_signal_2",....); */
-/*     etc. */
+       /* signal definitions */
+       signals[ACCOUNT_ADD_SIGNAL] =
+               g_signal_newv ("account-add",
+                              G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
+                              NULL, NULL, NULL,
+                              g_cclosure_marshal_VOID__POINTER,
+                              G_TYPE_NONE, 1, paramtypes);
+
+       signals[ACCOUNT_REMOVE_SIGNAL] =
+               g_signal_newv ("account-remove",
+                              G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
+                              NULL, NULL, NULL,
+                              g_cclosure_marshal_VOID__POINTER,
+                              G_TYPE_NONE, 1, paramtypes);
+       signals[ACCOUNT_CHANGE_SIGNAL] =
+               g_signal_newv ("account-change",
+                              G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
+                              NULL, NULL, NULL,
+                              modest_marshal_VOID__POINTER_POINTER_POINTER,
+                              G_TYPE_NONE, 3, paramtypes);
 }
 
 
@@ -118,6 +246,11 @@ modest_account_mgr_new (ModestConf * conf)
         * ModestConf should outlive the ModestAccountMgr though
         */
        g_object_ref (G_OBJECT (priv->modest_conf = conf));
+
+       priv->current_accounts = modest_account_mgr_account_names (MODEST_ACCOUNT_MGR(obj), NULL);
+
+       g_signal_connect (G_OBJECT (conf), "key-changed",
+                         G_CALLBACK (modest_account_mgr_check_change), obj);
        return obj;
 }
 
@@ -383,8 +516,8 @@ modest_account_mgr_account_names (ModestAccountMgr * self, GError ** err)
 
 static gchar *
 get_account_string (ModestAccountMgr * self, const gchar * name,
-                   const gchar * key, gboolean server_account, GError ** err)
-{
+                   const gchar * key, gboolean server_account, GError ** err) {
+
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
@@ -479,9 +612,9 @@ get_account_bool (ModestAccountMgr * self, const gchar * name,
        gchar *keyname;
        gboolean retval;
 
-       g_return_val_if_fail (self, -1);
-       g_return_val_if_fail (name, -1);
-       g_return_val_if_fail (key, -1);
+       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (name, FALSE);
+       g_return_val_if_fail (key, FALSE);
 
        if (server_account)
                keyname = get_server_account_keyname (name, key);
@@ -489,7 +622,7 @@ get_account_bool (ModestAccountMgr * self, const gchar * name,
                keyname = get_account_keyname (name, key);
 
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       retval = modest_conf_get_int (priv->modest_conf, keyname, err);
+       retval = modest_conf_get_bool (priv->modest_conf, keyname, err);
        g_free (keyname);
 
        return retval;