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"
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.h>
38 #include <string.h> /* For memcpy() */
40 /* Include config.h so that _() works: */
45 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
47 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
50 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
52 struct _ModestLimitRetrieveComboBoxPrivate
58 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
59 GValue *value, GParamSpec *pspec)
61 switch (property_id) {
63 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
69 const GValue *value, GParamSpec *pspec)
71 switch (property_id) {
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78 modest_limit_retrieve_combo_box_dispose (GObject *object)
80 if (G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose)
81 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->dispose (object);
85 modest_limit_retrieve_combo_box_finalize (GObject *object)
87 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
89 g_object_unref (G_OBJECT (priv->model));
91 G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
95 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
99 g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
101 object_class->get_property = modest_limit_retrieve_combo_box_get_property;
102 object_class->set_property = modest_limit_retrieve_combo_box_set_property;
103 object_class->dispose = modest_limit_retrieve_combo_box_dispose;
104 object_class->finalize = modest_limit_retrieve_combo_box_finalize;
108 MODEL_COL_NAME = 0, /* a string */
109 MODEL_COL_NUM = 1 /* an int */
112 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
115 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
117 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
119 /* Create a tree model for the combo box,
120 * with a string for the name, and an ID for the limit_retrieve.
121 * This must match our MODEL_COLS enum constants.
123 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
125 /* Setup the combo box: */
126 GtkComboBox *combobox = GTK_COMBO_BOX (self);
127 gtk_combo_box_set_model (combobox, priv->model);
129 /* LimitRetrieve column:
130 * The ID model column in not shown in the view. */
131 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
132 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
133 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
134 "text", MODEL_COL_NAME, NULL);
136 modest_limit_retrieve_combo_box_fill (self);
139 ModestLimitRetrieveComboBox*
140 modest_limit_retrieve_combo_box_new (void)
142 return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
145 /* Fill the combo box with appropriate choices.
146 * #combobox: The combo box.
147 * @protocol: IMAP or POP.
149 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
151 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
153 /* Remove any existing rows: */
154 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
155 gtk_list_store_clear (liststore);
158 gtk_list_store_append (liststore, &iter);
159 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
161 gtk_list_store_append (liststore, &iter);
162 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
164 gtk_list_store_append (liststore, &iter);
165 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
167 gtk_list_store_append (liststore, &iter);
168 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
170 gtk_list_store_append (liststore, &iter);
171 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
175 * Returns the selected limit_retrieve,
176 * or 0 if no limit_retrieve was selected.
179 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
182 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
184 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
186 gint limit_retrieve = 0;
187 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
188 return limit_retrieve;
191 return 0; /* Failed. */
194 /* This allows us to pass more than one piece of data to the signal handler,
195 * and get a result: */
198 ModestLimitRetrieveComboBox* self;
204 on_model_foreach_select_id(GtkTreeModel *model,
205 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
207 ForEachData *state = (ForEachData*)(user_data);
209 gboolean result = FALSE;
211 /* Select the item if it has the matching name: */
213 gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1);
214 if(num == state->num) {
215 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
218 result = TRUE; /* Stop walking the tree. */
221 return result; /* Whether we keep walking the tree. */
225 * Selects the specified limit_retrieve,
226 * or FALSE if no limit_retrieve was selected.
229 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
231 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
233 /* Create a state instance so we can send two items of data to the signal handler: */
234 ForEachData *state = g_new0 (ForEachData, 1);
235 state->self = combobox;
236 state->num = limit_retrieve;
237 state->found = FALSE;
239 /* Look at each item, and select the one with the correct ID: */
240 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
242 const gboolean result = state->found;
244 /* Free the state instance: */