Modified webpage: now tinymail repository is in gitorious.
[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/gtk.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 (ModestRetrieveComboBox, modest_retrieve_combo_box, GTK_TYPE_COMBO_BOX);
46
47 #define RETRIEVE_COMBO_BOX_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_COMBO_BOX, ModestRetrieveComboBoxPrivate))
49
50 typedef struct _ModestRetrieveComboBoxPrivate ModestRetrieveComboBoxPrivate;
51
52 struct _ModestRetrieveComboBoxPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 modest_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_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_retrieve_combo_box_dispose (GObject *object)
79 {
80         if (G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose)
81                 G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->dispose (object);
82 }
83
84 static void
85 modest_retrieve_combo_box_finalize (GObject *object)
86 {
87         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (object);
88
89         g_object_unref (G_OBJECT (priv->model));
90
91         G_OBJECT_CLASS (modest_retrieve_combo_box_parent_class)->finalize (object);
92 }
93
94 static void
95 modest_retrieve_combo_box_class_init (ModestRetrieveComboBoxClass *klass)
96 {
97         GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
99         g_type_class_add_private (klass, sizeof (ModestRetrieveComboBoxPrivate));
100
101         object_class->get_property = modest_retrieve_combo_box_get_property;
102         object_class->set_property = modest_retrieve_combo_box_set_property;
103         object_class->dispose = modest_retrieve_combo_box_dispose;
104         object_class->finalize = modest_retrieve_combo_box_finalize;
105 }
106
107 enum MODEL_COLS {
108         MODEL_COL_NAME = 0, /* a string */
109         MODEL_COL_RETRIEVE_TYPE = 1 /* a gint (a ModestAccountRetrieveType) */
110 };
111
112 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol);
113
114 static void
115 modest_retrieve_combo_box_init (ModestRetrieveComboBox *self)
116 {
117         ModestRetrieveComboBoxPrivate *priv = 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 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         /* Retrieve 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         /* The application must call modest_retrieve_combo_box_fill(), specifying POP or IMAP. */
137 }
138
139
140
141 ModestRetrieveComboBox*
142 modest_retrieve_combo_box_new (void)
143 {
144         return g_object_new (MODEST_TYPE_RETRIEVE_COMBO_BOX, NULL);
145 }
146
147 /* Fill the combo box with appropriate choices.
148  * #combobox: The combo box.
149  * @protocol: IMAP or POP.
150  */
151 void modest_retrieve_combo_box_fill (ModestRetrieveComboBox *combobox, ModestProtocolType protocol)
152 {       
153         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
154         
155         /* Remove any existing rows: */
156         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
157         gtk_list_store_clear (liststore);
158         
159         GtkTreeIter iter;
160         gtk_list_store_append (liststore, &iter);
161         gtk_list_store_set (liststore, &iter, 
162                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY, 
163                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
164
165         /* We disable messages retrieval finally */
166 /*      /\* Only IMAP should have this option, according to the UI spec: *\/ */
167 /*      if (protocol == MODEST_PROTOCOL_STORE_IMAP) { */
168 /*              gtk_list_store_append (liststore, &iter); */
169 /*              gtk_list_store_set (liststore, &iter,  */
170 /*                      MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES,  */
171 /*                      MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages"), -1); */
172 /*      } */
173         
174         
175         gtk_list_store_append (liststore, &iter);
176         gtk_list_store_set (liststore, &iter, 
177                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS, 
178                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
179 }
180
181 /**
182  * Returns the selected retrieve.
183  * or NULL if no retrieve was selected. The result must be freed with g_free().
184  */
185 ModestAccountRetrieveType
186 modest_retrieve_combo_box_get_active_retrieve_conf (ModestRetrieveComboBox *combobox)
187 {
188         GtkTreeIter active;
189         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
190         if (found) {
191                 ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
192
193                 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
194                 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
195                 return retrieve_type;   
196         }
197
198         return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
199 }
200
201 /* This allows us to pass more than one piece of data to the signal handler,
202  * and get a result: */
203 typedef struct 
204 {
205                 ModestRetrieveComboBox* self;
206                 ModestAccountRetrieveType retrieve_type;
207                 gboolean found;
208 } ForEachData;
209
210 static gboolean
211 on_model_foreach_select_id(GtkTreeModel *model, 
212         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
213 {
214         ForEachData *state = (ForEachData*)(user_data);
215         
216         gboolean result = FALSE;
217         
218         /* Select the item if it has the matching name: */
219         ModestAccountRetrieveType retrieve_type;
220         gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1); 
221         if (retrieve_type == state->retrieve_type) {
222                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
223                 
224                 state->found = TRUE;
225                 result = TRUE; /* Stop walking the tree. */
226         }
227         
228         return result; /* Whether we keep walking the tree. */
229 }
230
231 /**
232  * Selects the specified retrieve, 
233  * or FALSE if no retrieve was selected.
234  */
235 gboolean
236 modest_retrieve_combo_box_set_active_retrieve_conf (ModestRetrieveComboBox *combobox, 
237                                                     ModestAccountRetrieveType retrieve_type)
238 {
239         ModestRetrieveComboBoxPrivate *priv = RETRIEVE_COMBO_BOX_GET_PRIVATE (combobox);
240         
241         /* Create a state instance so we can send two items of data to the signal handler: */
242         ForEachData *state = g_new0 (ForEachData, 1);
243         state->self = combobox;
244         state->retrieve_type = retrieve_type;
245         state->found = FALSE;
246         
247         /* Look at each item, and select the one with the correct ID: */
248         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
249
250         const gboolean result = state->found;
251         
252         /* Free the state instance: */
253         g_free(state);
254         
255         return result;
256 }
257