Fix modest_tny_msg_header_get_all_recipients_list (in case from is empty)
[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/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.h>
36
37 #include <stdlib.h>
38 #include <string.h> /* For memcpy() */
39
40 /* Include config.h so that _() works: */
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 G_DEFINE_TYPE (ModestLimitRetrieveComboBox, modest_limit_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
46
47 #define LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, ModestLimitRetrieveComboBoxPrivate))
49
50 typedef struct _ModestLimitRetrieveComboBoxPrivate ModestLimitRetrieveComboBoxPrivate;
51
52 struct _ModestLimitRetrieveComboBoxPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 modest_limit_retrieve_combo_box_get_property (GObject *object, guint property_id,
59                                                                                                                         GValue *value, GParamSpec *pspec)
60 {
61         switch (property_id) {
62         default:
63                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64         }
65 }
66
67 static void
68 modest_limit_retrieve_combo_box_set_property (GObject *object, guint property_id,
69                                                                                                                         const GValue *value, GParamSpec *pspec)
70 {
71         switch (property_id) {
72         default:
73                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
74         }
75 }
76
77 static void
78 modest_limit_retrieve_combo_box_dispose (GObject *object)
79 {
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);
82 }
83
84 static void
85 modest_limit_retrieve_combo_box_finalize (GObject *object)
86 {
87         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
88
89         g_object_unref (G_OBJECT (priv->model));
90
91         G_OBJECT_CLASS (modest_limit_retrieve_combo_box_parent_class)->finalize (object);
92 }
93
94 static void
95 modest_limit_retrieve_combo_box_class_init (ModestLimitRetrieveComboBoxClass *klass)
96 {
97         GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
99         g_type_class_add_private (klass, sizeof (ModestLimitRetrieveComboBoxPrivate));
100
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;
105 }
106
107 enum MODEL_COLS {
108         MODEL_COL_NAME = 0, /* a string */
109         MODEL_COL_NUM = 1 /* an int */
110 };
111
112 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox);
113
114 static void
115 modest_limit_retrieve_combo_box_init (ModestLimitRetrieveComboBox *self)
116 {
117         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
118
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.
122          */
123         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
124
125         /* Setup the combo box: */
126         GtkComboBox *combobox = GTK_COMBO_BOX (self);
127         gtk_combo_box_set_model (combobox, priv->model);
128
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);
135         
136         modest_limit_retrieve_combo_box_fill (self);
137 }
138
139 ModestLimitRetrieveComboBox*
140 modest_limit_retrieve_combo_box_new (void)
141 {
142         return g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_COMBO_BOX, NULL);
143 }
144
145 /* Fill the combo box with appropriate choices.
146  * #combobox: The combo box.
147  * @protocol: IMAP or POP.
148  */
149 static void modest_limit_retrieve_combo_box_fill (ModestLimitRetrieveComboBox *combobox)
150 {       
151         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
152         
153         /* Remove any existing rows: */
154         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
155         gtk_list_store_clear (liststore);
156         
157         GtkTreeIter iter;
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);
160         
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);
163         
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);
166
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);
169
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);
172 }
173
174 /**
175  * Returns the selected limit_retrieve, 
176  * or 0 if no limit_retrieve was selected.
177  */
178 gint
179 modest_limit_retrieve_combo_box_get_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox)
180 {
181         GtkTreeIter active;
182         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
183         if (found) {
184                 ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
185
186                 gint limit_retrieve = 0;
187                 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
188                 return limit_retrieve;  
189         }
190
191         return 0; /* Failed. */
192 }
193
194 /* This allows us to pass more than one piece of data to the signal handler,
195  * and get a result: */
196 typedef struct 
197 {
198                 ModestLimitRetrieveComboBox* self;
199                 gint num;
200                 gboolean found;
201 } ForEachData;
202
203 static gboolean
204 on_model_foreach_select_id(GtkTreeModel *model, 
205         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
206 {
207         ForEachData *state = (ForEachData*)(user_data);
208         
209         gboolean result = FALSE;
210         
211         /* Select the item if it has the matching name: */
212         gint num = 0;
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);
216                 
217                 state->found = TRUE;
218                 result = TRUE; /* Stop walking the tree. */
219         }
220         
221         return result; /* Whether we keep walking the tree. */
222 }
223
224 /**
225  * Selects the specified limit_retrieve, 
226  * or FALSE if no limit_retrieve was selected.
227  */
228 gboolean
229 modest_limit_retrieve_combo_box_set_active_limit_retrieve (ModestLimitRetrieveComboBox *combobox, gint limit_retrieve)
230 {
231         ModestLimitRetrieveComboBoxPrivate *priv = LIMIT_RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
232         
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;
238         
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);
241
242         const gboolean result = state->found;
243         
244         /* Free the state instance: */
245         g_free(state);
246         
247         return result;
248 }
249