e7fde865dd590fb680743a088c7c50e72a590c22
[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, ModestProtocol 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, ModestProtocol 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         gtk_list_store_append (liststore, &iter);
142         gtk_list_store_set (liststore, &iter, 
143                 MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES, 
144                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages"), -1);
145         
146         /* Only IMAP should have this option, according to the UI spec: */
147         if (protocol == MODEST_PROTOCOL_STORE_IMAP) {
148                 gtk_list_store_append (liststore, &iter);
149                 gtk_list_store_set (liststore, &iter, 
150                         MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS, 
151                         MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
152         }
153 }
154
155 /**
156  * Returns the selected retrieve.
157  * or NULL if no retrieve was selected. The result must be freed with g_free().
158  */
159 gchar*
160 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
161 {
162         GtkTreeIter active;
163         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
164         if (found) {
165                 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
166
167                 gchar *retrieve = NULL;
168                 gtk_tree_model_get (priv->model, &active, MODEL_COL_CONF_NAME, &retrieve, -1);
169                 return retrieve;        
170         }
171
172         return NULL; /* Failed. */
173 }
174
175 /* This allows us to pass more than one piece of data to the signal handler,
176  * and get a result: */
177 typedef struct 
178 {
179                 ModestRetrieveComboBox* self;
180                 const gchar* conf_name;
181                 gboolean found;
182 } ForEachData;
183
184 static gboolean
185 on_model_foreach_select_id(GtkTreeModel *model, 
186         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
187 {
188         ForEachData *state = (ForEachData*)(user_data);
189         
190         gboolean result = FALSE;
191         
192         /* Select the item if it has the matching name: */
193         gchar * conf_name = 0;
194         gtk_tree_model_get (model, iter, MODEL_COL_CONF_NAME, &conf_name, -1); 
195         if(conf_name && state->conf_name && (strcmp(conf_name, state->conf_name) == 0)) {
196                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
197                 
198                 state->found = TRUE;
199                 result = TRUE; /* Stop walking the tree. */
200         }
201         g_free (conf_name);
202         
203         return result; /* Whether we keep walking the tree. */
204 }
205
206 /**
207  * Selects the specified retrieve, 
208  * or FALSE if no retrieve was selected.
209  */
210 gboolean
211 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox, const gchar* retrieve)
212 {
213         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
214         
215         /* Create a state instance so we can send two items of data to the signal handler: */
216         ForEachData *state = g_new0 (ForEachData, 1);
217         state->self = combobox;
218         state->conf_name = retrieve;
219         state->found = FALSE;
220         
221         /* Look at each item, and select the one with the correct ID: */
222         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
223
224         const gboolean result = state->found;
225         
226         /* Free the state instance: */
227         g_free(state);
228         
229         return result;
230 }
231