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