dba56395954453f8a8425f829f493dee6b34b725
[modest] / src / modest-widget-factory.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <tny-gtk-account-list-model.h>
33 #include <tny-gtk-folder-store-tree-model.h>
34 #include <tny-account-store.h>
35 #include <tny-simple-list.h>
36 #include <tny-device.h>
37 #include <tny-folder-store-query.h>
38 #include "modest-widget-factory.h"
39 #include "modest-widget-memory.h"
40 #include <modest-protocol-info.h>
41 #include "modest-tny-platform-factory.h"
42
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
45 #include <modest-runtime.h>
46
47 #include "modest-mail-operation.h"
48 #include "widgets/modest-header-view-priv.h"
49
50 /* 'private'/'protected' functions */
51 static void modest_widget_factory_class_init    (ModestWidgetFactoryClass *klass);
52 static void modest_widget_factory_init          (ModestWidgetFactory *obj);
53 static void modest_widget_factory_finalize      (GObject *obj);
54
55
56 /* list my signals */
57 enum {
58         /* MY_SIGNAL_1, */
59         /* MY_SIGNAL_2, */
60         LAST_SIGNAL
61 };
62
63 typedef struct _ModestWidgetFactoryPrivate ModestWidgetFactoryPrivate;
64 struct _ModestWidgetFactoryPrivate {    
65
66         ModestTnyAccountStore       *account_store;
67         
68         ModestHeaderView            *header_view;
69         ModestFolderView            *folder_view;
70         ModestMsgView               *msg_preview;
71
72         GtkWidget                   *progress_bar;
73         GtkWidget                   *status_bar;
74         GtkWidget                   *folder_info_label;
75
76         GtkWidget                   *online_toggle;
77 };
78 #define MODEST_WIDGET_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
79                                                    MODEST_TYPE_WIDGET_FACTORY, \
80                                                    ModestWidgetFactoryPrivate))
81 /* globals */
82 static GObjectClass *parent_class = NULL;
83
84 /* uncomment the following if you have defined any signals */
85 /* static guint signals[LAST_SIGNAL] = {0}; */
86
87 GType
88 modest_widget_factory_get_type (void)
89 {
90         static GType my_type = 0;
91         if (!my_type) {
92                 static const GTypeInfo my_info = {
93                         sizeof(ModestWidgetFactoryClass),
94                         NULL,           /* base init */
95                         NULL,           /* base finalize */
96                         (GClassInitFunc) modest_widget_factory_class_init,
97                         NULL,           /* class finalize */
98                         NULL,           /* class data */
99                         sizeof(ModestWidgetFactory),
100                         1,              /* n_preallocs */
101                         (GInstanceInitFunc) modest_widget_factory_init,
102                         NULL
103                 };
104                 my_type = g_type_register_static (G_TYPE_OBJECT,
105                                                   "ModestWidgetFactory",
106                                                   &my_info, 0);
107         }
108         return my_type;
109 }
110
111 static void
112 modest_widget_factory_class_init (ModestWidgetFactoryClass *klass)
113 {
114         GObjectClass *gobject_class;
115         gobject_class = (GObjectClass*) klass;
116
117         parent_class            = g_type_class_peek_parent (klass);
118         gobject_class->finalize = modest_widget_factory_finalize;
119
120         g_type_class_add_private (gobject_class, sizeof(ModestWidgetFactoryPrivate));
121 }
122
123 static void
124 modest_widget_factory_init (ModestWidgetFactory *obj)
125 {
126         ModestWidgetFactoryPrivate *priv;
127         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
128         
129         priv->account_store = NULL;
130         
131         priv->progress_bar = gtk_progress_bar_new ();
132         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar),
133                                        1.0);
134         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(priv->progress_bar),
135                                         PANGO_ELLIPSIZE_END);
136         priv->status_bar   = gtk_statusbar_new ();
137         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
138                                            FALSE);
139 }
140
141
142 static void
143 modest_widget_factory_finalize (GObject *obj)
144 {
145         /* no need to unref account_store; we don't own the reference */
146         
147         G_OBJECT_CLASS(parent_class)->finalize (obj);
148 }
149
150
151 static gboolean
152 init_widgets (ModestWidgetFactory *self)
153 {
154         ModestWidgetFactoryPrivate *priv;
155         TnyFolderStoreQuery *query;
156
157         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
158
159         /* folder view */
160         query = tny_folder_store_query_new ();
161         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
162
163         priv->folder_view =  MODEST_FOLDER_VIEW(modest_folder_view_new (priv->account_store,query));
164         if (!priv->folder_view) {
165                 g_printerr ("modest: cannot instantiate folder view\n");
166                 return FALSE;
167         }       
168         g_object_unref (G_OBJECT (query));
169         
170         /* header view */
171         priv->header_view =
172                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
173         if (!priv->header_view) {
174                 g_printerr ("modest: cannot instantiate header view\n");
175                 return FALSE;
176         }
177                 
178         /* msg preview */
179         priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL));
180         if (!priv->msg_preview) {
181                 g_printerr ("modest: cannot instantiate header view\n");
182                 return FALSE;
183         }
184
185         /* online/offline combo */
186         priv->online_toggle = gtk_toggle_button_new ();
187
188         /* label with number of items, unread items for 
189            the current folder */
190         priv->folder_info_label = gtk_label_new (NULL);
191         
192         return TRUE;
193 }
194
195
196 ModestWidgetFactory*
197 modest_widget_factory_new (ModestTnyAccountStore *account_store)
198 {
199         GObject *obj;
200         ModestWidgetFactoryPrivate *priv;
201
202         g_return_val_if_fail (account_store, NULL);
203         
204         obj  = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL); 
205         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
206         
207         priv->account_store = account_store;
208         
209         if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
210                 g_printerr ("modest: widget factory failed to init widgets\n");
211                 g_object_unref (obj);
212                 return NULL;
213         }
214         
215         return MODEST_WIDGET_FACTORY(obj);
216 }
217
218
219
220
221 ModestFolderView*
222 modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
223 {
224         g_return_val_if_fail (self, NULL);
225         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
226 }
227
228
229 ModestHeaderView*
230 modest_widget_factory_get_header_view (ModestWidgetFactory *self)
231 {
232         g_return_val_if_fail (self, NULL);
233         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
234 }
235
236
237 ModestMsgView*
238 modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
239 {
240         g_return_val_if_fail (self, NULL);
241         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
242 }
243
244
245 ModestAccountView*
246 modest_widget_factory_get_account_view (ModestWidgetFactory *self)
247 {
248         return modest_account_view_new (modest_runtime_get_account_mgr());
249 }
250
251
252
253 GtkWidget*
254 modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
255 {
256         g_return_val_if_fail (self, NULL);
257         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
258 }
259
260
261 GtkWidget*
262 modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
263 {
264         g_return_val_if_fail (self, NULL);
265         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
266 }
267
268
269
270 static const GSList*
271 get_transports (ModestWidgetFactory *self)
272 {
273         ModestWidgetFactoryPrivate *priv;
274         ModestAccountMgr *account_mgr;
275         GSList *transports = NULL;
276         GSList *cursor, *accounts;
277         
278         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
279
280         account_mgr = modest_runtime_get_account_mgr();
281         cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
282         while (cursor) {
283                 gchar *account_name = (gchar*)cursor->data;
284                 gchar *from_string  = modest_account_mgr_get_from_string (account_mgr,
285                                                                             account_name);
286                 if (!from_string)  {
287                         /* something went wrong: ignore this one */
288                         g_free (account_name);
289                         cursor->data = NULL;
290                 } else {
291                         ModestPair *pair;
292                         pair = modest_pair_new ((gpointer) account_name,
293                                                 (gpointer) from_string , TRUE);
294                         transports = g_slist_prepend (transports, pair);
295                 } /* don't free account name; it's freed when the transports list is freed */
296                 cursor = cursor->next;
297         }
298         g_slist_free (accounts);
299         return transports;
300 }
301
302
303 GtkWidget*
304 modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
305 {
306         ModestWidgetFactoryPrivate *priv;
307         ModestPairList *protos = NULL;
308         GtkWidget* combo_box;
309         
310         g_return_val_if_fail (self, NULL);
311
312         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
313         
314         switch (type) {
315         case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
316                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
317                 break;
318         case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
319                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
320                 break;
321         case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
322                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
323                 break;
324         case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
325                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
326                 break;
327         case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
328                 protos = (ModestPairList *) get_transports (self);
329                 break;
330         default:
331                 g_warning ("invalid combo box type: %d", type);
332                 return NULL;
333         }
334
335         combo_box = modest_combo_box_new (protos);
336         g_slist_free (protos);
337         
338         gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
339         
340         return combo_box;
341 }
342
343
344
345 GtkWidget*
346 modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
347 {
348         g_return_val_if_fail (self, NULL);
349         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
350 }
351
352
353
354 GtkWidget*
355 modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
356 {
357         g_return_val_if_fail (self, NULL);
358         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;
359 }