Added files from osso-modest-easysetup
[modest] / src / maemo / easysetup / modest-easysetup-servertype-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
7 #include <stdio.h>
8
9 #include "modest-easysetup-servertype-combo-box.h"
10 #include <gtk/gtkliststore.h>
11 #include <gtk/gtkcelllayout.h>
12 #include <gtk/gtkcellrenderertext.h>
13 #include <glib/gi18n.h>
14
15 #include <stdlib.h>
16 #include <string.h> /* For memcpy() */
17
18 G_DEFINE_TYPE (EasysetupServertypeComboBox, easysetup_servertype_combo_box, GTK_TYPE_COMBO_BOX);
19
20 #define SERVERTYPE_COMBO_BOX_GET_PRIVATE(o) \
21         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, EasysetupServertypeComboBoxPrivate))
22
23 typedef struct _EasysetupServertypeComboBoxPrivate EasysetupServertypeComboBoxPrivate;
24
25 struct _EasysetupServertypeComboBoxPrivate
26 {
27         GtkTreeModel *model;
28 };
29
30 static void
31 easysetup_servertype_combo_box_get_property (GObject *object, guint property_id,
32                                                                                                                         GValue *value, GParamSpec *pspec)
33 {
34         switch (property_id) {
35         default:
36                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
37         }
38 }
39
40 static void
41 easysetup_servertype_combo_box_set_property (GObject *object, guint property_id,
42                                                                                                                         const GValue *value, GParamSpec *pspec)
43 {
44         switch (property_id) {
45         default:
46                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
47         }
48 }
49
50 static void
51 easysetup_servertype_combo_box_dispose (GObject *object)
52 {
53         if (G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose)
54                 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose (object);
55 }
56
57 static void
58 easysetup_servertype_combo_box_finalize (GObject *object)
59 {
60         EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (object);
61
62         g_object_unref (G_OBJECT (priv->model));
63
64         G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->finalize (object);
65 }
66
67 static void
68 easysetup_servertype_combo_box_class_init (EasysetupServertypeComboBoxClass *klass)
69 {
70         GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72         g_type_class_add_private (klass, sizeof (EasysetupServertypeComboBoxPrivate));
73
74         object_class->get_property = easysetup_servertype_combo_box_get_property;
75         object_class->set_property = easysetup_servertype_combo_box_set_property;
76         object_class->dispose = easysetup_servertype_combo_box_dispose;
77         object_class->finalize = easysetup_servertype_combo_box_finalize;
78 }
79
80 enum MODEL_COLS {
81         MODEL_COL_NAME = 0, /* a string */
82         MODEL_COL_ID = 1 /* an int. */
83 };
84
85 static void
86 easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox);
87
88 static void
89 easysetup_servertype_combo_box_init (EasysetupServertypeComboBox *self)
90 {
91         EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (self);
92
93         /* Create a tree model for the combo box,
94          * with a string for the name, and an ID for the servertype.
95          * This must match our MODEL_COLS enum constants.
96          */
97         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
98
99         /* Setup the combo box: */
100         GtkComboBox *combobox = GTK_COMBO_BOX (self);
101         gtk_combo_box_set_model (combobox, priv->model);
102
103         /* Servertype column:
104          * The ID model column in not shown in the view. */
105         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
106         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
107         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
108         "text", MODEL_COL_NAME, NULL);
109         
110         easysetup_servertype_combo_box_fill (self);
111 }
112
113 EasysetupServertypeComboBox*
114 easysetup_servertype_combo_box_new (void)
115 {
116         return g_object_new (EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, NULL);
117 }
118
119 void easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox)
120 {       
121         EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
122         
123         /* Remove any existing rows: */
124         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
125         gtk_list_store_clear (liststore);
126         
127         GtkTreeIter iter;
128         gtk_list_store_append (liststore, &iter);
129         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_STORE_POP, MODEL_COL_NAME, _("mail_fi_emailtype_pop3"), -1);
130         
131         /* Select the POP item: */
132         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
133         
134         gtk_list_store_append (liststore, &iter);
135         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_STORE_IMAP, MODEL_COL_NAME, _("mail_fi_emailtype_imap"), -1);
136 }
137
138 /**
139  * Returns the selected servertype, 
140  * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
141  */
142 ModestProtocol
143 easysetup_servertype_combo_box_get_active_servertype (EasysetupServertypeComboBox *combobox)
144 {
145         GtkTreeIter active;
146         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
147         if (found) {
148                 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
149
150                 ModestProtocol servertype = MODEST_PROTOCOL_UNKNOWN;
151                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
152                 return servertype;      
153         }
154
155         return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
156 }
157
158 /* This allows us to pass more than one piece of data to the signal handler,
159  * and get a result: */
160 typedef struct 
161 {
162                 EasysetupServertypeComboBox* self;
163                 gint id;
164                 gboolean found;
165 } ForEachData;
166
167 static gboolean
168 on_model_foreach_select_id(GtkTreeModel *model, 
169         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
170 {
171         ForEachData *state = (ForEachData*)(user_data);
172         
173         /* Select the item if it has the matching ID: */
174         guint id = 0;
175         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
176         if(id == state->id) {
177                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
178                 
179                 state->found = TRUE;
180                 return TRUE; /* Stop walking the tree. */
181         }
182         
183         return FALSE; /* Keep walking the tree. */
184 }
185
186 /**
187  * Selects the specified servertype, 
188  * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
189  */
190 gboolean
191 easysetup_servertype_combo_box_set_active_servertype (EasysetupServertypeComboBox *combobox, ModestProtocol servertype)
192 {
193         EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
194         
195         /* Create a state instance so we can send two items of data to the signal handler: */
196         ForEachData *state = g_new0 (ForEachData, 1);
197         state->self = combobox;
198         state->id = servertype;
199         state->found = FALSE;
200         
201         /* Look at each item, and select the one with the correct ID: */
202         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
203
204         const gboolean result = state->found;
205         
206         /* Free the state instance: */
207         g_free(state);
208         
209         return result;
210 }
211