* src/maemo/modest-retrieve-combo-box.c:
[modest] / src / widgets / modest-retrieve-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-retrieve-combo-box.h"
7 #include "modest-defs.h" /* For the conf names. */
8 #include <gtk/gtkliststore.h>
9 #include <gtk/gtkcelllayout.h>
10 #include <gtk/gtkcellrenderertext.h>
11 #include <glib/gi18n.h>
12
13 #include <stdlib.h>
14 #include <string.h> /* For memcpy() */
15
16 /* Include config.h so that _() works: */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 G_DEFINE_TYPE (ModestRetrieveComboBox, modest_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
22
23 #define RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
24         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_COMBO_BOX, ModestRetrieveComboBoxPrivate))
25
26 typedef struct _ModestRetrieveComboBoxPrivate ModestRetrieveComboBoxPrivate;
27
28 struct _ModestRetrieveComboBoxPrivate
29 {
30         GtkTreeModel *model;
31 };
32
33 static void
34 modest_retrieve_combo_box_get_property (GObject *object, guint property_id,
35                                                                                                                         GValue *value, GParamSpec *pspec)
36 {
37         switch (property_id) {
38         default:
39                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
40         }
41 }
42
43 static void
44 modest_retrieve_combo_box_set_property (GObject *object, guint property_id,
45                                                                                                                         const GValue *value, GParamSpec *pspec)
46 {
47         switch (property_id) {
48         default:
49                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
50         }
51 }
52
53 static void
54 modest_retrieve_combo_box_dispose (GObject *object)
55 {
56         if (G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose)
57                 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose (object);
58 }
59
60 static void
61 modest_retrieve_combo_box_finalize (GObject *object)
62 {
63         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66
67         G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->finalize (object);
68 }
69
70 static void
71 modest_retrieve_combo_box_class_init (ModestRetrieveComboBoxClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (ModestRetrieveComboBoxPrivate));
76
77         object_class->get_property = modest_retrieve_combo_box_get_property;
78         object_class->set_property = modest_retrieve_combo_box_set_property;
79         object_class->dispose = modest_retrieve_combo_box_dispose;
80         object_class->finalize = modest_retrieve_combo_box_finalize;
81 }
82
83 enum MODEL_COLS {
84         MODEL_COL_NAME = 0, /* a string */
85         MODEL_COL_CONF_NAME = 1 /* a string */
86 };
87
88 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestTransportStoreProtocol protocol);
89
90 static void
91 modest_retrieve_combo_box_init (ModestRetrieveComboBox *self)
92 {
93         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_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 retrieve.
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_STRING));
100
101         /* Setup the combo box: */
102         GtkComboBox *combobox = GTK_COMBO_BOX (self);
103         gtk_combo_box_set_model (combobox, priv->model);
104
105         /* Retrieve 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         /* The application must call modest_retrieve_combo_box_fill(), specifying POP or IMAP. */
113 }
114
115
116
117 ModestRetrieveComboBox*
118 modest_retrieve_combo_box_new (void)
119 {
120         return g_object_new (MODEST_TYPE_RETRIEVE_COMBO_BOX, NULL);
121 }
122
123 /* Fill the combo box with appropriate choices.
124  * #combobox: The combo box.
125  * @protocol: IMAP or POP.
126  */
127 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestTransportStoreProtocol protocol)
128 {       
129         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
130         
131         /* Remove any existing rows: */
132         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
133         gtk_list_store_clear (liststore);
134         
135         GtkTreeIter iter;
136         gtk_list_store_append (liststore, &iter);
137         gtk_list_store_set (liststore, &iter, 
138                 MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY, 
139                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
140
141         /* We disable messages retrieval finally */
142 /*      /\* Only IMAP should have this option, according to the UI spec: *\/ */
143 /*      if (protocol == MODEST_PROTOCOL_STORE_IMAP) { */
144 /*              gtk_list_store_append (liststore, &iter); */
145 /*              gtk_list_store_set (liststore, &iter,  */
146 /*                      MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES,  */
147 /*                      MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages"), -1); */
148 /*      } */
149         
150         
151         gtk_list_store_append (liststore, &iter);
152         gtk_list_store_set (liststore, &iter, 
153                 MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS, 
154                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
155 }
156
157 /**
158  * Returns the selected retrieve.
159  * or NULL if no retrieve was selected. The result must be freed with g_free().
160  */
161 gchar*
162 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
163 {
164         GtkTreeIter active;
165         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
166         if (found) {
167                 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
168
169                 gchar *retrieve = NULL;
170                 gtk_tree_model_get (priv->model, &active, MODEL_COL_CONF_NAME, &retrieve, -1);
171                 return retrieve;        
172         }
173
174         return NULL; /* Failed. */
175 }
176
177 /* This allows us to pass more than one piece of data to the signal handler,
178  * and get a result: */
179 typedef struct 
180 {
181                 ModestRetrieveComboBox* self;
182                 const gchar* conf_name;
183                 gboolean found;
184 } ForEachData;
185
186 static gboolean
187 on_model_foreach_select_id(GtkTreeModel *model, 
188         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
189 {
190         ForEachData *state = (ForEachData*)(user_data);
191         
192         gboolean result = FALSE;
193         
194         /* Select the item if it has the matching name: */
195         gchar * conf_name = 0;
196         gtk_tree_model_get (model, iter, MODEL_COL_CONF_NAME, &conf_name, -1); 
197         if(conf_name && state->conf_name && (strcmp(conf_name, state->conf_name) == 0)) {
198                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
199                 
200                 state->found = TRUE;
201                 result = TRUE; /* Stop walking the tree. */
202         }
203         g_free (conf_name);
204         
205         return result; /* Whether we keep walking the tree. */
206 }
207
208 /**
209  * Selects the specified retrieve, 
210  * or FALSE if no retrieve was selected.
211  */
212 gboolean
213 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox, const gchar* retrieve)
214 {
215         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
216         
217         /* Create a state instance so we can send two items of data to the signal handler: */
218         ForEachData *state = g_new0 (ForEachData, 1);
219         state->self = combobox;
220         state->conf_name = retrieve;
221         state->found = FALSE;
222         
223         /* Look at each item, and select the one with the correct ID: */
224         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
225
226         const gboolean result = state->found;
227         
228         /* Free the state instance: */
229         g_free(state);
230         
231         return result;
232 }
233