LImit the number of changes for correcting scroll to avoid infinite loop.
[modest] / src / maemo / easysetup / modest-easysetup-provider-combo-box.c
index 3caded6..c39b80c 100644 (file)
@@ -1,6 +1,30 @@
-/* Copyright (c) 2007, Nokia Corporation
+/* 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.
  */
 
 #include "modest-easysetup-provider-combo-box.h"
@@ -8,6 +32,10 @@
 #include <gtk/gtkcelllayout.h>
 #include <gtk/gtkcellrenderertext.h>
 #include <glib/gi18n.h>
+#include <modest-text-utils.h>
+#include "modest-protocol-registry.h"
+#include "modest-runtime.h"
+#include <modest-account-protocol.h>
 
 #include <stdlib.h>
 #include <string.h> /* For memcpy() */
@@ -80,11 +108,39 @@ easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
 }
 
 enum MODEL_COLS {
-       MODEL_COL_NAME = 0,
-       MODEL_COL_ID = 1 /* a string, not an int. */
+       MODEL_COL_ID, /* a string, not an int. */
+       MODEL_COL_NAME,
+       MODEL_COL_ID_TYPE
 };
 
 
+/*
+ * strictly, we should sort providers with mcc=0 after the other ones.... but, we don't have
+ * that info here, so ignoring for now.
+ */
+static gint
+provider_sort_func (GtkTreeModel *model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer user_data)
+{
+       gchar *prov1, *prov2;
+       gint retval;
+       
+       gtk_tree_model_get (model, iter1, MODEL_COL_NAME, &prov1, -1);
+       gtk_tree_model_get (model, iter2, MODEL_COL_NAME, &prov2, -1);
+
+       if (strcmp (prov1, prov2) == 0) 
+               retval = 0;
+       else if (strcmp (_("mcen_va_serviceprovider_other"), prov1) == 0)
+               retval = -1;
+       else if (strcmp (_("mcen_va_serviceprovider_other"), prov2) == 0)
+               retval = 1;
+       else
+               retval = modest_text_utils_utf8_strcmp (prov1, prov2, TRUE);
+       
+       g_free (prov1);
+       g_free (prov2);
+
+       return retval;
+}
 
 static void
 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
@@ -92,10 +148,10 @@ easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
        EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
 
        /* Create a tree model for the combo box,
-        * with a string for the name, and a string for the ID (e.g. "vodafone.it").
+        * with a string for the name, and a string for the ID (e.g. "vodafone.it"), and the mcc
         * This must match our MODEL_COLS enum constants.
         */
-       priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
+       priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT));
 
        /* Setup the combo box: */
        GtkComboBox *combobox = GTK_COMBO_BOX (self);
@@ -105,11 +161,14 @@ easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
         * The ID model column in not shown in the view. */
        GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
-       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
-       "text", MODEL_COL_NAME, NULL);
+       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,  "text", MODEL_COL_NAME, NULL);
        
-       /* The application should call easysetup_provider_combo_box_fill()
-        * to actually add some rows. */
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(priv->model),
+                                             MODEL_COL_NAME, GTK_SORT_ASCENDING);
+       gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(priv->model),
+                                         MODEL_COL_NAME,
+                                         (GtkTreeIterCompareFunc)provider_sort_func,
+                                         NULL, NULL);
 }
 
 EasysetupProviderComboBox*
@@ -118,56 +177,109 @@ easysetup_provider_combo_box_new (void)
        return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
 }
 
-void easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, gint country_id)
+void
+easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, 
+                                  ModestPresets *presets,
+                                  gint mcc)
 {      
-       EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
-       
-       /* Remove any existing rows: */
-       GtkListStore *liststore = GTK_LIST_STORE (priv->model);
-       gtk_list_store_clear (liststore);
-       
-       /* Add the appropriate rows for this country, from the presets file: */
+       GtkTreeIter other_iter;
+       EasysetupProviderComboBoxPrivate *priv;
+       GtkListStore *liststore;        
+       GSList *provider_ids_used_already = NULL, *provider_protos, *tmp;
        gchar ** provider_ids = NULL;
-       gchar ** provider_names = modest_presets_get_providers (presets, country_id, 
-               TRUE /* include_globals */, &provider_ids);
-       
-       gchar ** iter_provider_names = provider_names;
-       gchar ** iter_provider_ids = provider_ids;
-       while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids)
-       {
+       gchar ** provider_names;        
+       gchar ** iter_provider_names;
+       gchar ** iter_provider_ids;
+       ModestProtocolRegistry *registry;
+
+       g_return_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox));
+
+       priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
+       liststore = GTK_LIST_STORE (priv->model);
+       gtk_list_store_clear (liststore);
+       provider_names = modest_presets_get_providers (presets, mcc, TRUE, &provider_ids);
+
+       iter_provider_names = provider_names;
+       iter_provider_ids = provider_ids;
+
+       while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids) {
                const gchar* provider_name = *iter_provider_names;
-               if(!provider_name)
-                       continue;
-                       
                const gchar* provider_id = *iter_provider_ids;
-               if(!provider_id)
-                       continue;
-               
-               /* printf("debug: provider_name=%s\n", provider_name); */
-       
-               /* Add the row: */
-               GtkTreeIter iter;
-               gtk_list_store_append (liststore, &iter);
-               gtk_list_store_set(liststore, &iter, MODEL_COL_ID, provider_id, MODEL_COL_NAME, provider_name, -1);
+
+               /* Prevent duplicate providers: */
+               if (g_slist_find_custom (provider_ids_used_already, 
+                                        provider_id, (GCompareFunc)strcmp) == NULL) {
+                       /* printf("debug: provider_name=%s\n", provider_name); */
+
+                       /* Add the row: */
+                       GtkTreeIter iter;
+                       gtk_list_store_append (liststore, &iter);
+                       
+                       gtk_list_store_set(liststore, &iter, 
+                                          MODEL_COL_ID, provider_id, 
+                                          MODEL_COL_NAME, provider_name, 
+                                          MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_PROVIDER,
+                                          -1);
+                       
+                       provider_ids_used_already = g_slist_prepend (
+                               provider_ids_used_already, (gpointer)g_strdup (provider_id));
+               }
                
                ++iter_provider_names;
-               ++iter_provider_ids;    
+               ++iter_provider_ids;
        }
        
        /* Free the result of modest_presets_get_providers()
         * as specified by its documentation: */
        g_strfreev (provider_names);
        g_strfreev (provider_ids);
+
+       /* Add the provider protocols */
+       registry = modest_runtime_get_protocol_registry ();
+       provider_protos = modest_protocol_registry_get_by_tag (registry, 
+                                                              MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
+       tmp = provider_protos;
+       while (tmp) {
+               GtkTreeIter iter;
+               ModestProtocol *proto = MODEST_PROTOCOL (tmp->data);
+
+               /* only add store protocols, no need to duplicate them */
+               if (modest_protocol_registry_protocol_type_has_tag (registry, 
+                                                                   modest_protocol_get_type_id (proto),
+                                                                   MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) {
+                       gboolean supported;
+
+                       supported = modest_account_protocol_is_supported (MODEST_ACCOUNT_PROTOCOL (proto));
+
+                       if (supported) {
+                               const gchar *name = modest_protocol_get_display_name (proto);
+
+                               gtk_list_store_append (liststore, &iter);
+                               gtk_list_store_set (liststore, &iter,
+                                                   MODEL_COL_ID, modest_protocol_get_name (proto),
+                                                   MODEL_COL_NAME, name,
+                                                   MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_PLUGIN_PROTOCOL,
+                                                   -1);
+                       }
+               }
+               tmp = g_slist_next (tmp);
+       }
+       g_slist_free (provider_protos);
        
        /* Add the "Other" item: */
        /* Note that ID 0 means "Other" for us: */
-       /* TODO: We need a Logical ID for this text. */
-       GtkTreeIter iter;
-       gtk_list_store_append (liststore, &iter);
-       gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("Other..."), -1);
-       
+       gtk_list_store_prepend (liststore, &other_iter);
+       gtk_list_store_set (liststore, &other_iter,
+                           MODEL_COL_ID, 0,
+                           MODEL_COL_NAME, _("mcen_va_serviceprovider_other"),
+                           MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER,
+                           -1);
+
        /* Select the "Other" item: */
-       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
+       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &other_iter);
+       
+       g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
+       g_slist_free (provider_ids_used_already);
 }
 
 /**
@@ -178,6 +290,9 @@ gchar*
 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
 {
        GtkTreeIter active;
+
+       g_return_val_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox), NULL);
+
        const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
        if (found) {
                EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
@@ -189,3 +304,37 @@ easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *
 
        return NULL; /* Failed. */
 }
+
+void 
+easysetup_provider_combo_box_set_others_provider (EasysetupProviderComboBox *combobox)
+{
+       GtkTreeModel *model;
+       GtkTreeIter others_iter;
+
+       g_return_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox));
+       
+       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
+       
+       if (gtk_tree_model_get_iter_first (model, &others_iter))
+               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &others_iter);
+}
+
+EasysetupProviderComboBoxIdType 
+easysetup_provider_combo_box_get_active_id_type (EasysetupProviderComboBox *combobox)
+{
+       GtkTreeIter active;
+
+       g_return_val_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX (combobox), 
+                             EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER);
+
+       if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active)) {
+               EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
+               EasysetupProviderComboBoxIdType id_type;
+
+               gtk_tree_model_get (priv->model, &active, MODEL_COL_ID_TYPE, &id_type, -1);
+               return id_type; 
+       } else {
+               /* Fallback to other */
+               return EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER;
+       }
+}