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