a2e70b90ae5935f074cc05939d2a734fe32c9e65
[modest] / src / widgets / modest-limit-retrieve-combo-box.c
1 /*
2  * Copyright (C) 2007 Nokia Corporation, all rights reserved.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
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.
17  *
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.
29  */
30
31 #include "modest-limit-retrieve-combo-box.h"
32 #include <gtk/gtk.h>
33 #include <glib/gi18n.h>
34
35 #include <stdlib.h>
36 #include <string.h> /* For memcpy() */
37
38 /* Include config.h so that _() works: */
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
44
45 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
46         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
47
48 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
49
50 struct _ModestLimitRetrieveComboBoxPrivate
51 {
52         GtkTreeModel *model;
53 };
54
55 static void
56 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
57                                                                                                                         GValue *value, GParamSpec *pspec)
58 {
59         switch (property_id) {
60         default:
61                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
62         }
63 }
64
65 static void
66 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
67                                                                                                                         const GValue *value, GParamSpec *pspec)
68 {
69         switch (property_id) {
70         default:
71                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
72         }
73 }
74
75 static void
76 modest_limit_retrieve_combo_box_dispose (GObject *object)
77 {
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);
80 }
81
82 static void
83 modest_limit_retrieve_combo_box_finalize (GObject *object)
84 {
85         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
86
87         g_object_unref (G_OBJECT (priv->model));
88
89         G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
90 }
91
92 static void
93 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
94 {
95         GObjectClass *object_class = G_OBJECT_CLASS (klass);
96
97         g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
98
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;
103 }
104
105 enum MODEL_COLS {
106         MODEL_COL_NAME = 0, /* a string */
107         MODEL_COL_NUM = 1 /* an int */
108 };
109
110 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
111
112 static void
113 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
114 {
115         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
116
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.
120          */
121         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
122
123         /* Setup the combo box: */
124         GtkComboBox *combobox = GTK_COMBO_BOX (self);
125         gtk_combo_box_set_model (combobox, priv->model);
126
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);
133         
134         modest_limit_retrieve_combo_box_fill (self);
135 }
136
137 ModestLimitRetrieveComboBox*
138 modest_limit_retrieve_combo_box_new (void)
139 {
140         return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
141 }
142
143 /* Fill the combo box with appropriate choices.
144  * #combobox: The combo box.
145  * @protocol: IMAP or POP.
146  */
147 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
148 {       
149         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
150         
151         /* Remove any existing rows: */
152         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
153         gtk_list_store_clear (liststore);
154         
155         GtkTreeIter iter;
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);
158         
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);
161         
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);
164
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);
167
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);
170 }
171
172 /**
173  * Returns the selected limit_retrieve, 
174  * or 0 if no limit_retrieve was selected.
175  */
176 gint
177 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
178 {
179         GtkTreeIter active;
180         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
181         if (found) {
182                 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
183
184                 gint limit_retrieve = 0;
185                 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
186                 return limit_retrieve;  
187         }
188
189         return 0; /* Failed. */
190 }
191
192 /* This allows us to pass more than one piece of data to the signal handler,
193  * and get a result: */
194 typedef struct 
195 {
196                 ModestLimitRetrieveComboBox* self;
197                 gint num;
198                 gboolean found;
199 } ForEachData;
200
201 static gboolean
202 on_model_foreach_select_id(GtkTreeModel *model, 
203         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
204 {
205         ForEachData *state = (ForEachData*)(user_data);
206         
207         gboolean result = FALSE;
208         
209         /* Select the item if it has the matching name: */
210         gint num = 0;
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);
214                 
215                 state->found = TRUE;
216                 result = TRUE; /* Stop walking the tree. */
217         }
218         
219         return result; /* Whether we keep walking the tree. */
220 }
221
222 /**
223  * Selects the specified limit_retrieve, 
224  * or FALSE if no limit_retrieve was selected.
225  */
226 gboolean
227 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
228 {
229         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
230         
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;
236         
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);
239
240         const gboolean result = state->found;
241         
242         /* Free the state instance: */
243         g_free(state);
244         
245         return result;
246 }
247