* all:
[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         priv->status_bar   = gtk_statusbar_new ();
135         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
136                                            FALSE);
137 }
138
139
140 static void
141 modest_widget_factory_finalize (GObject *obj)
142 {
143         /* no need to unref account_store; we don't own the reference */
144         
145         G_OBJECT_CLASS(parent_class)->finalize (obj);
146 }
147
148
149 static gboolean
150 init_widgets (ModestWidgetFactory *self)
151 {
152         ModestWidgetFactoryPrivate *priv;
153         TnyFolderStoreQuery *query;
154
155         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
156
157         /* folder view */
158         query = tny_folder_store_query_new ();
159         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
160
161         priv->folder_view =  MODEST_FOLDER_VIEW(modest_folder_view_new (priv->account_store,query));
162         if (!priv->folder_view) {
163                 g_printerr ("modest: cannot instantiate folder view\n");
164                 return FALSE;
165         }       
166         g_object_unref (G_OBJECT (query));
167         
168         /* header view */
169         priv->header_view =
170                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
171         if (!priv->header_view) {
172                 g_printerr ("modest: cannot instantiate header view\n");
173                 return FALSE;
174         }
175                 
176         /* msg preview */
177         priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL));
178         if (!priv->msg_preview) {
179                 g_printerr ("modest: cannot instantiate header view\n");
180                 return FALSE;
181         }
182
183         /* online/offline combo */
184         priv->online_toggle = gtk_toggle_button_new ();
185
186         /* label with number of items, unread items for 
187            the current folder */
188         priv->folder_info_label = gtk_label_new (NULL);
189         
190         return TRUE;
191 }
192
193
194 ModestWidgetFactory*
195 modest_widget_factory_new (ModestTnyAccountStore *account_store)
196 {
197         GObject *obj;
198         ModestWidgetFactoryPrivate *priv;
199
200         g_return_val_if_fail (account_store, NULL);
201         
202         obj  = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL); 
203         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
204         
205         priv->account_store = account_store;
206         
207         if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
208                 g_printerr ("modest: widget factory failed to init widgets\n");
209                 g_object_unref (obj);
210                 return NULL;
211         }
212         
213         return MODEST_WIDGET_FACTORY(obj);
214 }
215
216
217
218
219 ModestFolderView*
220 modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
221 {
222         g_return_val_if_fail (self, NULL);
223         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
224 }
225
226
227 ModestHeaderView*
228 modest_widget_factory_get_header_view (ModestWidgetFactory *self)
229 {
230         g_return_val_if_fail (self, NULL);
231         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
232 }
233
234
235 ModestMsgView*
236 modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
237 {
238         g_return_val_if_fail (self, NULL);
239         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
240 }
241
242
243 ModestAccountView*
244 modest_widget_factory_get_account_view (ModestWidgetFactory *self)
245 {
246         return modest_account_view_new (modest_runtime_get_account_mgr());
247 }
248
249
250
251 GtkWidget*
252 modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
253 {
254         g_return_val_if_fail (self, NULL);
255         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
256 }
257
258
259 GtkWidget*
260 modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
261 {
262         g_return_val_if_fail (self, NULL);
263         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
264 }
265
266
267
268 static const GSList*
269 get_transports (ModestWidgetFactory *self)
270 {
271         ModestWidgetFactoryPrivate *priv;
272         ModestAccountMgr *account_mgr;
273         GSList *transports = NULL;
274         GSList *cursor, *accounts;
275         
276         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
277
278         account_mgr = modest_runtime_get_account_mgr();
279         cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
280         while (cursor) {
281                 ModestAccountData *data;
282                 gchar *account_name = (gchar*)cursor->data;
283
284                 data = modest_account_mgr_get_account_data (account_mgr, account_name);
285                 if (data && data->transport_account) {
286                         ModestPair *pair;
287                         gchar *display_name = g_strdup_printf ("%s (%s)", data->email, account_name);
288                         pair = modest_pair_new ((gpointer) data,
289                                                 (gpointer) display_name , TRUE);
290                         transports = g_slist_prepend (transports, pair);
291                 }
292                 /* don't free account name; it's freed when the transports list is freed */
293                 cursor = cursor->next;
294         }
295         g_slist_free (accounts);
296         
297         return transports;
298 }
299
300
301 #if 0
302 static const GSList*
303 get_stores (ModestWidgetFactory *self, gboolean only_remote)
304 {
305         ModestWidgetFactoryPrivate *priv;
306         TnyAccountStore *account_store;
307         TnyList *stores;
308         TnyIterator *iter;
309         
310         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
311
312         account_store =
313                 tny_platform_factory_new_account_store (priv->fact);                    
314
315         stores = tny_simple_list_new ();
316         tny_account_store_get_accounts (account_store, stores,
317                                          TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
318
319         /* simply return all the stores */
320         if (!only_remote)
321                 return stores;
322
323         /*  remove the non-remote stores from the list */
324         if (only_remote) {
325                 iter = tny_list_create_iterator (stores);
326                 while (!tny_iterator_is_done (iter)) {
327                         TnyAccount *acc = (TnyAccount*)tny_iterator_get_current(iter);
328                         /* is it a local account? if so, remove */
329                         ModestProtocol proto = modest_protocol_info_get_protocol (tny_account_get_proto(acc));
330                         if (modest_protocol_info_protocol_is_local_store(proto))
331                                 tny_list_remove (stores, acc); /* FIXME: iter still valid? */
332                         tny_iterator_next (iter);
333                 }
334                 g_object_unref (G_OBJECT(iter));
335         }
336         return stores;          
337 }
338 #endif
339
340 GtkWidget*
341 modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
342 {
343         ModestWidgetFactoryPrivate *priv;
344         ModestPairList *protos = NULL;
345         GtkWidget* combo_box;
346         
347         g_return_val_if_fail (self, NULL);
348
349         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
350         
351         switch (type) {
352         case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
353                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
354                 break;
355         case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
356                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
357                 break;
358         case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
359                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
360                 break;
361         case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
362                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
363                 break;
364         case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
365                 protos = (ModestPairList *) get_transports (self);
366                 break;
367 /*      case MODEST_COMBO_BOX_TYPE_REMOTE_STORES: */
368 /*              // FIXME */
369 /*              list = get_stores (self, TRUE); /\* get all *remote* stores *\/ */
370 /*              combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL(list)); */
371 /*              g_object_unref (G_OBJECT(list)); */
372 /*              //return combo_box; */
373         default:
374                 g_warning ("invalid combo box type: %d", type);
375                 return NULL;
376         }
377
378         combo_box = modest_combo_box_new (protos);
379         g_slist_free (protos);
380         
381         gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
382         
383         return combo_box;
384 }
385
386
387
388 GtkWidget*
389 modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
390 {
391         g_return_val_if_fail (self, NULL);
392         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
393 }
394
395
396
397 GtkWidget*
398 modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
399 {
400         g_return_val_if_fail (self, NULL);
401         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;
402 }