2 * Copyright (C) 2007 Nokia Corporation, all rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the Nokia Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "modest-limit-retrieve-combo-box.h"
33 #include <glib/gi18n.h>
36 #include <string.h> /* For memcpy() */
38 /* Include config.h so that _() works: */
43 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
45 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
46 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
48 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
50 struct _ModestLimitRetrieveComboBoxPrivate
56 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
57 GValue *value, GParamSpec *pspec)
59 switch (property_id) {
61 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
67 const GValue *value, GParamSpec *pspec)
69 switch (property_id) {
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76 modest_limit_retrieve_combo_box_dispose (GObject *object)
78 if (G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose)
79 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose (object);
83 modest_limit_retrieve_combo_box_finalize (GObject *object)
85 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
87 g_object_unref (G_OBJECT (priv->model));
89 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
93 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
99 object_class->get_property = modest_limit_retrieve_combo_box_get_property;
100 object_class->set_property = modest_limit_retrieve_combo_box_set_property;
101 object_class->dispose = modest_limit_retrieve_combo_box_dispose;
102 object_class->finalize = modest_limit_retrieve_combo_box_finalize;
106 MODEL_COL_NAME = 0, /* a string */
107 MODEL_COL_NUM = 1 /* an int */
110 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
113 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
115 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
117 /* Create a tree model for the combo box,
118 * with a string for the name, and an ID for the limit_retrieve.
119 * This must match our MODEL_COLS enum constants.
121 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
123 /* Setup the combo box: */
124 GtkComboBox *combobox = GTK_COMBO_BOX (self);
125 gtk_combo_box_set_model (combobox, priv->model);
127 /* LimitRetrieve column:
128 * The ID model column in not shown in the view. */
129 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
130 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
131 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
132 "text", MODEL_COL_NAME, NULL);
134 modest_limit_retrieve_combo_box_fill (self);
137 ModestLimitRetrieveComboBox*
138 modest_limit_retrieve_combo_box_new (void)
140 return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
143 /* Fill the combo box with appropriate choices.
144 * #combobox: The combo box.
145 * @protocol: IMAP or POP.
147 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
149 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
151 /* Remove any existing rows: */
152 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
153 gtk_list_store_clear (liststore);
156 gtk_list_store_append (liststore, &iter);
157 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
159 gtk_list_store_append (liststore, &iter);
160 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
162 gtk_list_store_append (liststore, &iter);
163 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
165 gtk_list_store_append (liststore, &iter);
166 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
168 gtk_list_store_append (liststore, &iter);
169 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
173 * Returns the selected limit_retrieve,
174 * or 0 if no limit_retrieve was selected.
177 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
180 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
182 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
184 gint limit_retrieve = 0;
185 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
186 return limit_retrieve;
189 return 0; /* Failed. */
192 /* This allows us to pass more than one piece of data to the signal handler,
193 * and get a result: */
196 ModestLimitRetrieveComboBox* self;
202 on_model_foreach_select_id(GtkTreeModel *model,
203 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
205 ForEachData *state = (ForEachData*)(user_data);
207 gboolean result = FALSE;
209 /* Select the item if it has the matching name: */
211 gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1);
212 if(num == state->num) {
213 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
216 result = TRUE; /* Stop walking the tree. */
219 return result; /* Whether we keep walking the tree. */
223 * Selects the specified limit_retrieve,
224 * or FALSE if no limit_retrieve was selected.
227 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
229 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
231 /* Create a state instance so we can send two items of data to the signal handler: */
232 ForEachData *state = g_new0 (ForEachData, 1);
233 state->self = combobox;
234 state->num = limit_retrieve;
235 state->found = FALSE;
237 /* Look at each item, and select the one with the correct ID: */
238 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
240 const gboolean result = state->found;
242 /* Free the state instance: */