1 /* Copyright (c) 2007, Nokia Corporation
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>
14 #include <string.h> /* For memcpy() */
16 /* Include config.h so that _() works: */
21 G_DEFINE_TYPE (ModestRetrieveComboBox, modest_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
23 #define RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
24 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_COMBO_BOX, ModestRetrieveComboBoxPrivate))
26 typedef struct _ModestRetrieveComboBoxPrivate ModestRetrieveComboBoxPrivate;
28 struct _ModestRetrieveComboBoxPrivate
34 modest_retrieve_combo_box_get_property (GObject *object, guint property_id,
35 GValue *value, GParamSpec *pspec)
37 switch (property_id) {
39 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
44 modest_retrieve_combo_box_set_property (GObject *object, guint property_id,
45 const GValue *value, GParamSpec *pspec)
47 switch (property_id) {
49 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
54 modest_retrieve_combo_box_dispose (GObject *object)
56 if (G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose)
57 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose (object);
61 modest_retrieve_combo_box_finalize (GObject *object)
63 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
65 g_object_unref (G_OBJECT (priv->model));
67 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->finalize (object);
71 modest_retrieve_combo_box_class_init (ModestRetrieveComboBoxClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 g_type_class_add_private (klass, sizeof (ModestRetrieveComboBoxPrivate));
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;
84 MODEL_COL_NAME = 0, /* a string */
85 MODEL_COL_CONF_NAME = 1 /* a string */
88 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestTransportStoreProtocol protocol);
91 modest_retrieve_combo_box_init (ModestRetrieveComboBox *self)
93 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
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.
99 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
101 /* Setup the combo box: */
102 GtkComboBox *combobox = GTK_COMBO_BOX (self);
103 gtk_combo_box_set_model (combobox, priv->model);
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);
112 /* The application must call modest_retrieve_combo_box_fill(), specifying POP or IMAP. */
117 ModestRetrieveComboBox*
118 modest_retrieve_combo_box_new (void)
120 return g_object_new (MODEST_TYPE_RETRIEVE_COMBO_BOX, NULL);
123 /* Fill the combo box with appropriate choices.
124 * #combobox: The combo box.
125 * @protocol: IMAP or POP.
127 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestTransportStoreProtocol protocol)
129 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
131 /* Remove any existing rows: */
132 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
133 gtk_list_store_clear (liststore);
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);
141 /* Only IMAP should have this option, according to the UI spec: */
142 if (protocol == MODEST_PROTOCOL_STORE_IMAP) {
143 gtk_list_store_append (liststore, &iter);
144 gtk_list_store_set (liststore, &iter,
145 MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES,
146 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages"), -1);
150 gtk_list_store_append (liststore, &iter);
151 gtk_list_store_set (liststore, &iter,
152 MODEL_COL_CONF_NAME, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS,
153 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
157 * Returns the selected retrieve.
158 * or NULL if no retrieve was selected. The result must be freed with g_free().
161 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
164 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
166 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
168 gchar *retrieve = NULL;
169 gtk_tree_model_get (priv->model, &active, MODEL_COL_CONF_NAME, &retrieve, -1);
173 return NULL; /* Failed. */
176 /* This allows us to pass more than one piece of data to the signal handler,
177 * and get a result: */
180 ModestRetrieveComboBox* self;
181 const gchar* conf_name;
186 on_model_foreach_select_id(GtkTreeModel *model,
187 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
189 ForEachData *state = (ForEachData*)(user_data);
191 gboolean result = FALSE;
193 /* Select the item if it has the matching name: */
194 gchar * conf_name = 0;
195 gtk_tree_model_get (model, iter, MODEL_COL_CONF_NAME, &conf_name, -1);
196 if(conf_name && state->conf_name && (strcmp(conf_name, state->conf_name) == 0)) {
197 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
200 result = TRUE; /* Stop walking the tree. */
204 return result; /* Whether we keep walking the tree. */
208 * Selects the specified retrieve,
209 * or FALSE if no retrieve was selected.
212 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox, const gchar* retrieve)
214 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
216 /* Create a state instance so we can send two items of data to the signal handler: */
217 ForEachData *state = g_new0 (ForEachData, 1);
218 state->self = combobox;
219 state->conf_name = retrieve;
220 state->found = FALSE;
222 /* Look at each item, and select the one with the correct ID: */
223 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
225 const gboolean result = state->found;
227 /* Free the state instance: */