Fix modest_tny_msg_header_get_all_recipients_list (in case from is empty)
[modest] / src / widgets / modest-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-retrieve-combo-box.h"
32 #include "modest-defs.h" /* For the conf names. */
33 #include "modest-account-settings.h"
34 #include <gtk/gtkliststore.h>
35 #include <gtk/gtkcelllayout.h>
36 #include <gtk/gtkcellrenderertext.h>
37 #include <glib/gi18n.h>
38
39 #include <stdlib.h>
40 #include <string.h> /* For memcpy() */
41
42 /* Include config.h so that _() works: */
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 G_DEFINE_TYPE (ModestRetrieveComboBox, modest_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
48
49 #define RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_COMBO_BOX, ModestRetrieveComboBoxPrivate))
51
52 typedef struct _ModestRetrieveComboBoxPrivate ModestRetrieveComboBoxPrivate;
53
54 struct _ModestRetrieveComboBoxPrivate
55 {
56         GtkTreeModel *model;
57 };
58
59 static void
60 modest_retrieve_combo_box_get_property (GObject *object, guint property_id,
61                                                                                                                         GValue *value, GParamSpec *pspec)
62 {
63         switch (property_id) {
64         default:
65                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66         }
67 }
68
69 static void
70 modest_retrieve_combo_box_set_property (GObject *object, guint property_id,
71                                                                                                                         const GValue *value, GParamSpec *pspec)
72 {
73         switch (property_id) {
74         default:
75                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76         }
77 }
78
79 static void
80 modest_retrieve_combo_box_dispose (GObject *object)
81 {
82         if (G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose)
83                 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose (object);
84 }
85
86 static void
87 modest_retrieve_combo_box_finalize (GObject *object)
88 {
89         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
90
91         g_object_unref (G_OBJECT (priv->model));
92
93         G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->finalize (object);
94 }
95
96 static void
97 modest_retrieve_combo_box_class_init (ModestRetrieveComboBoxClass *klass)
98 {
99         GObjectClass *object_class = G_OBJECT_CLASS (klass);
100
101         g_type_class_add_private (klass, sizeof (ModestRetrieveComboBoxPrivate));
102
103         object_class->get_property = modest_retrieve_combo_box_get_property;
104         object_class->set_property = modest_retrieve_combo_box_set_property;
105         object_class->dispose = modest_retrieve_combo_box_dispose;
106         object_class->finalize = modest_retrieve_combo_box_finalize;
107 }
108
109 enum MODEL_COLS {
110         MODEL_COL_NAME = 0, /* a string */
111         MODEL_COL_RETRIEVE_TYPE = 1 /* a gint (a ModestAccountRetrieveType) */
112 };
113
114 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol);
115
116 static void
117 modest_retrieve_combo_box_init (ModestRetrieveComboBox *self)
118 {
119         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (self);
120
121         /* Create a tree model for the combo box,
122          * with a string for the name, and an ID for the retrieve.
123          * This must match our MODEL_COLS enum constants.
124          */
125         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
126
127         /* Setup the combo box: */
128         GtkComboBox *combobox = GTK_COMBO_BOX (self);
129         gtk_combo_box_set_model (combobox, priv->model);
130
131         /* Retrieve column:
132          * The ID model column in not shown in the view. */
133         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
134         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
135         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
136         "text", MODEL_COL_NAME, NULL);
137         
138         /* The application must call modest_retrieve_combo_box_fill(), specifying POP or IMAP. */
139 }
140
141
142
143 ModestRetrieveComboBox*
144 modest_retrieve_combo_box_new (void)
145 {
146         return g_object_new (MODEST_TYPE_RETRIEVE_COMBO_BOX, NULL);
147 }
148
149 /* Fill the combo box with appropriate choices.
150  * #combobox: The combo box.
151  * @protocol: IMAP or POP.
152  */
153 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol)
154 {       
155         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
156         
157         /* Remove any existing rows: */
158         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
159         gtk_list_store_clear (liststore);
160         
161         GtkTreeIter iter;
162         gtk_list_store_append (liststore, &iter);
163         gtk_list_store_set (liststore, &iter, 
164                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY, 
165                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
166
167         /* We disable messages retrieval finally */
168 /*      /\* Only IMAP should have this option, according to the UI spec: *\/ */
169 /*      if (protocol == MODEST_PROTOCOL_STORE_IMAP) { */
170 /*              gtk_list_store_append (liststore, &iter); */
171 /*              gtk_list_store_set (liststore, &iter,  */
172 /*                      MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES,  */
173 /*                      MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages"), -1); */
174 /*      } */
175         
176         
177         gtk_list_store_append (liststore, &iter);
178         gtk_list_store_set (liststore, &iter, 
179                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS, 
180                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
181 }
182
183 /**
184  * Returns the selected retrieve.
185  * or NULL if no retrieve was selected. The result must be freed with g_free().
186  */
187 ModestAccountRetrieveType
188 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
189 {
190         GtkTreeIter active;
191         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
192         if (found) {
193                 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
194
195                 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
196                 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
197                 return retrieve_type;   
198         }
199
200         return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
201 }
202
203 /* This allows us to pass more than one piece of data to the signal handler,
204  * and get a result: */
205 typedef struct 
206 {
207                 ModestRetrieveComboBox* self;
208                 ModestAccountRetrieveType retrieve_type;
209                 gboolean found;
210 } ForEachData;
211
212 static gboolean
213 on_model_foreach_select_id(GtkTreeModel *model, 
214         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
215 {
216         ForEachData *state = (ForEachData*)(user_data);
217         
218         gboolean result = FALSE;
219         
220         /* Select the item if it has the matching name: */
221         ModestAccountRetrieveType retrieve_type;
222         gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1); 
223         if (retrieve_type == state->retrieve_type) {
224                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
225                 
226                 state->found = TRUE;
227                 result = TRUE; /* Stop walking the tree. */
228         }
229         
230         return result; /* Whether we keep walking the tree. */
231 }
232
233 /**
234  * Selects the specified retrieve, 
235  * or FALSE if no retrieve was selected.
236  */
237 gboolean
238 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox, 
239                                                     ModestAccountRetrieveType retrieve_type)
240 {
241         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
242         
243         /* Create a state instance so we can send two items of data to the signal handler: */
244         ForEachData *state = g_new0 (ForEachData, 1);
245         state->self = combobox;
246         state->retrieve_type = retrieve_type;
247         state->found = FALSE;
248         
249         /* Look at each item, and select the one with the correct ID: */
250         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
251
252         const gboolean result = state->found;
253         
254         /* Free the state instance: */
255         g_free(state);
256         
257         return result;
258 }
259