1 /* Copyright (c) 2007, Nokia Corporation
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>
15 #include <string.h> /* For memcpy() */
17 /* Include config.h so that _() works: */
22 G_DEFINE_TYPE (ModestRetrieveComboBox, modest_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
24 #define RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
25 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_COMBO_BOX, ModestRetrieveComboBoxPrivate))
27 typedef struct _ModestRetrieveComboBoxPrivate ModestRetrieveComboBoxPrivate;
29 struct _ModestRetrieveComboBoxPrivate
35 modest_retrieve_combo_box_get_property (GObject *object, guint property_id,
36 GValue *value, GParamSpec *pspec)
38 switch (property_id) {
40 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
45 modest_retrieve_combo_box_set_property (GObject *object, guint property_id,
46 const GValue *value, GParamSpec *pspec)
48 switch (property_id) {
50 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
55 modest_retrieve_combo_box_dispose (GObject *object)
57 if (G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose)
58 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose (object);
62 modest_retrieve_combo_box_finalize (GObject *object)
64 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
66 g_object_unref (G_OBJECT (priv->model));
68 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->finalize (object);
72 modest_retrieve_combo_box_class_init (ModestRetrieveComboBoxClass *klass)
74 GObjectClass *object_class = G_OBJECT_CLASS (klass);
76 g_type_class_add_private (klass, sizeof (ModestRetrieveComboBoxPrivate));
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;
85 MODEL_COL_NAME = 0, /* a string */
86 MODEL_COL_RETRIEVE_TYPE = 1 /* a gint (a ModestAccountRetrieveType) */
89 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol);
92 modest_retrieve_combo_box_init (ModestRetrieveComboBox *self)
94 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
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.
100 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
102 /* Setup the combo box: */
103 GtkComboBox *combobox = GTK_COMBO_BOX (self);
104 gtk_combo_box_set_model (combobox, priv->model);
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);
113 /* The application must call modest_retrieve_combo_box_fill(), specifying POP or IMAP. */
118 ModestRetrieveComboBox*
119 modest_retrieve_combo_box_new (void)
121 return g_object_new (MODEST_TYPE_RETRIEVE_COMBO_BOX, NULL);
124 /* Fill the combo box with appropriate choices.
125 * #combobox: The combo box.
126 * @protocol: IMAP or POP.
128 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol)
130 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
132 /* Remove any existing rows: */
133 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
134 gtk_list_store_clear (liststore);
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);
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); */
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);
159 * Returns the selected retrieve.
160 * or NULL if no retrieve was selected. The result must be freed with g_free().
162 ModestAccountRetrieveType
163 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
166 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
168 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
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;
175 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
178 /* This allows us to pass more than one piece of data to the signal handler,
179 * and get a result: */
182 ModestRetrieveComboBox* self;
183 ModestAccountRetrieveType retrieve_type;
188 on_model_foreach_select_id(GtkTreeModel *model,
189 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
191 ForEachData *state = (ForEachData*)(user_data);
193 gboolean result = FALSE;
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);
202 result = TRUE; /* Stop walking the tree. */
205 return result; /* Whether we keep walking the tree. */
209 * Selects the specified retrieve,
210 * or FALSE if no retrieve was selected.
213 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox,
214 ModestAccountRetrieveType retrieve_type)
216 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
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;
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);
227 const gboolean result = state->found;
229 /* Free the state instance: */