1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-limit-retrieve-combo-box.h"
7 #include <gtk/gtkliststore.h>
8 #include <gtk/gtkcelllayout.h>
9 #include <gtk/gtkcellrenderertext.h>
10 #include <glib/gi18n.h>
13 #include <string.h> /* For memcpy() */
15 /* Include config.h so that _() works: */
20 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
22 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
25 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
27 struct _ModestLimitRetrieveComboBoxPrivate
33 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
34 GValue *value, GParamSpec *pspec)
36 switch (property_id) {
38 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
43 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
44 const GValue *value, GParamSpec *pspec)
46 switch (property_id) {
48 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53 modest_limit_retrieve_combo_box_dispose (GObject *object)
55 if (G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose)
56 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose (object);
60 modest_limit_retrieve_combo_box_finalize (GObject *object)
62 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
64 g_object_unref (G_OBJECT (priv->model));
66 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
70 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
76 object_class->get_property = modest_limit_retrieve_combo_box_get_property;
77 object_class->set_property = modest_limit_retrieve_combo_box_set_property;
78 object_class->dispose = modest_limit_retrieve_combo_box_dispose;
79 object_class->finalize = modest_limit_retrieve_combo_box_finalize;
83 MODEL_COL_NAME = 0, /* a string */
84 MODEL_COL_NUM = 1 /* an int */
87 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
90 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
92 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
94 /* Create a tree model for the combo box,
95 * with a string for the name, and an ID for the limit_retrieve.
96 * This must match our MODEL_COLS enum constants.
98 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
100 /* Setup the combo box: */
101 GtkComboBox *combobox = GTK_COMBO_BOX (self);
102 gtk_combo_box_set_model (combobox, priv->model);
104 /* LimitRetrieve column:
105 * The ID model column in not shown in the view. */
106 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
107 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
108 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
109 "text", MODEL_COL_NAME, NULL);
111 modest_limit_retrieve_combo_box_fill (self);
114 ModestLimitRetrieveComboBox*
115 modest_limit_retrieve_combo_box_new (void)
117 return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
120 /* Fill the combo box with appropriate choices.
121 * #combobox: The combo box.
122 * @protocol: IMAP or POP.
124 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
126 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
128 /* Remove any existing rows: */
129 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
130 gtk_list_store_clear (liststore);
133 gtk_list_store_append (liststore, &iter);
134 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
136 gtk_list_store_append (liststore, &iter);
137 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
139 gtk_list_store_append (liststore, &iter);
140 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
142 gtk_list_store_append (liststore, &iter);
143 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
145 gtk_list_store_append (liststore, &iter);
146 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
150 * Returns the selected limit_retrieve,
151 * or 0 if no limit_retrieve was selected.
154 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
157 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
159 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
161 gint limit_retrieve = 0;
162 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
163 return limit_retrieve;
166 return 0; /* Failed. */
169 /* This allows us to pass more than one piece of data to the signal handler,
170 * and get a result: */
173 ModestLimitRetrieveComboBox* self;
179 on_model_foreach_select_id(GtkTreeModel *model,
180 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
182 ForEachData *state = (ForEachData*)(user_data);
184 gboolean result = FALSE;
186 /* Select the item if it has the matching name: */
188 gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1);
189 if(num == state->num) {
190 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
193 result = TRUE; /* Stop walking the tree. */
196 return result; /* Whether we keep walking the tree. */
200 * Selects the specified limit_retrieve,
201 * or FALSE if no limit_retrieve was selected.
204 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
206 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
208 /* Create a state instance so we can send two items of data to the signal handler: */
209 ForEachData *state = g_new0 (ForEachData, 1);
210 state->self = combobox;
211 state->num = limit_retrieve;
212 state->found = FALSE;
214 /* Look at each item, and select the one with the correct ID: */
215 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
217 const gboolean result = state->found;
219 /* Free the state instance: */