* Fixes NB#91689. fixes a wrong check for ASCII
[modest] / src / widgets / modest-limit-retrieve-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
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>
11
12 #include <stdlib.h>
13 #include <string.h> /* For memcpy() */
14
15 /* Include config.h so that _() works: */
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
21
22 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
23         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
24
25 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
26
27 struct _ModestLimitRetrieveComboBoxPrivate
28 {
29         GtkTreeModel *model;
30 };
31
32 static void
33 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
34                                                                                                                         GValue *value, GParamSpec *pspec)
35 {
36         switch (property_id) {
37         default:
38                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
39         }
40 }
41
42 static void
43 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
44                                                                                                                         const GValue *value, GParamSpec *pspec)
45 {
46         switch (property_id) {
47         default:
48                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
49         }
50 }
51
52 static void
53 modest_limit_retrieve_combo_box_dispose (GObject *object)
54 {
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);
57 }
58
59 static void
60 modest_limit_retrieve_combo_box_finalize (GObject *object)
61 {
62         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
63
64         g_object_unref (G_OBJECT (priv->model));
65
66         G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
67 }
68
69 static void
70 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
71 {
72         GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74         g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
75
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;
80 }
81
82 enum MODEL_COLS {
83         MODEL_COL_NAME = 0, /* a string */
84         MODEL_COL_NUM = 1 /* an int */
85 };
86
87 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
88
89 static void
90 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
91 {
92         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
93
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.
97          */
98         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
99
100         /* Setup the combo box: */
101         GtkComboBox *combobox = GTK_COMBO_BOX (self);
102         gtk_combo_box_set_model (combobox, priv->model);
103
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);
110         
111         modest_limit_retrieve_combo_box_fill (self);
112 }
113
114 ModestLimitRetrieveComboBox*
115 modest_limit_retrieve_combo_box_new (void)
116 {
117         return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
118 }
119
120 /* Fill the combo box with appropriate choices.
121  * #combobox: The combo box.
122  * @protocol: IMAP or POP.
123  */
124 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
125 {       
126         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
127         
128         /* Remove any existing rows: */
129         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
130         gtk_list_store_clear (liststore);
131         
132         GtkTreeIter iter;
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);
135         
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);
138         
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);
141
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);
144
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);
147 }
148
149 /**
150  * Returns the selected limit_retrieve, 
151  * or 0 if no limit_retrieve was selected.
152  */
153 gint
154 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
155 {
156         GtkTreeIter active;
157         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
158         if (found) {
159                 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
160
161                 gint limit_retrieve = 0;
162                 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
163                 return limit_retrieve;  
164         }
165
166         return 0; /* Failed. */
167 }
168
169 /* This allows us to pass more than one piece of data to the signal handler,
170  * and get a result: */
171 typedef struct 
172 {
173                 ModestLimitRetrieveComboBox* self;
174                 gint num;
175                 gboolean found;
176 } ForEachData;
177
178 static gboolean
179 on_model_foreach_select_id(GtkTreeModel *model, 
180         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
181 {
182         ForEachData *state = (ForEachData*)(user_data);
183         
184         gboolean result = FALSE;
185         
186         /* Select the item if it has the matching name: */
187         gint num = 0;
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);
191                 
192                 state->found = TRUE;
193                 result = TRUE; /* Stop walking the tree. */
194         }
195         
196         return result; /* Whether we keep walking the tree. */
197 }
198
199 /**
200  * Selects the specified limit_retrieve, 
201  * or FALSE if no limit_retrieve was selected.
202  */
203 gboolean
204 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
205 {
206         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
207         
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;
213         
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);
216
217         const gboolean result = state->found;
218         
219         /* Free the state instance: */
220         g_free(state);
221         
222         return result;
223 }
224