1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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"
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
46 #include "modest-mail-operation.h"
47 #include "widgets/modest-header-view-priv.h"
49 /* 'private'/'protected' functions */
50 static void modest_widget_factory_class_init (ModestWidgetFactoryClass *klass);
51 static void modest_widget_factory_init (ModestWidgetFactory *obj);
52 static void modest_widget_factory_finalize (GObject *obj);
62 typedef struct _ModestWidgetFactoryPrivate ModestWidgetFactoryPrivate;
63 struct _ModestWidgetFactoryPrivate {
65 TnyPlatformFactory *fact;
66 TnyAccountStore *account_store;
68 ModestHeaderView *header_view;
69 ModestFolderView *folder_view;
70 ModestMsgView *msg_preview;
72 GtkWidget *progress_bar;
73 GtkWidget *status_bar;
74 GtkWidget *folder_info_label;
76 GtkWidget *online_toggle;
78 #define MODEST_WIDGET_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
79 MODEST_TYPE_WIDGET_FACTORY, \
80 ModestWidgetFactoryPrivate))
82 static GObjectClass *parent_class = NULL;
84 /* uncomment the following if you have defined any signals */
85 /* static guint signals[LAST_SIGNAL] = {0}; */
88 modest_widget_factory_get_type (void)
90 static GType my_type = 0;
92 static const GTypeInfo my_info = {
93 sizeof(ModestWidgetFactoryClass),
95 NULL, /* base finalize */
96 (GClassInitFunc) modest_widget_factory_class_init,
97 NULL, /* class finalize */
98 NULL, /* class data */
99 sizeof(ModestWidgetFactory),
101 (GInstanceInitFunc) modest_widget_factory_init,
104 my_type = g_type_register_static (G_TYPE_OBJECT,
105 "ModestWidgetFactory",
112 modest_widget_factory_class_init (ModestWidgetFactoryClass *klass)
114 GObjectClass *gobject_class;
115 gobject_class = (GObjectClass*) klass;
117 parent_class = g_type_class_peek_parent (klass);
118 gobject_class->finalize = modest_widget_factory_finalize;
120 g_type_class_add_private (gobject_class, sizeof(ModestWidgetFactoryPrivate));
124 modest_widget_factory_init (ModestWidgetFactory *obj)
126 ModestWidgetFactoryPrivate *priv;
127 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
129 priv->fact = modest_tny_platform_factory_get_instance ();
130 priv->account_store = tny_platform_factory_new_account_store (priv->fact);
132 priv->progress_bar = gtk_progress_bar_new ();
133 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar),
135 priv->status_bar = gtk_statusbar_new ();
136 gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
142 modest_widget_factory_finalize (GObject *obj)
144 ModestWidgetFactoryPrivate *priv;
145 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
147 G_OBJECT_CLASS(parent_class)->finalize (obj);
152 init_widgets (ModestWidgetFactory *self)
154 ModestWidgetFactoryPrivate *priv;
155 TnyFolderStoreQuery *query;
157 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
160 query = tny_folder_store_query_new ();
161 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
162 if (!(priv->folder_view =
163 MODEST_FOLDER_VIEW(modest_folder_view_new (MODEST_TNY_ACCOUNT_STORE (priv->account_store),
165 g_printerr ("modest: cannot instantiate folder view\n");
168 g_object_unref (G_OBJECT (query));
171 if (!(priv->header_view =
172 MODEST_HEADER_VIEW(modest_header_view_new (NULL,MODEST_HEADER_VIEW_STYLE_DETAILS)))) {
173 g_printerr ("modest: cannot instantiate header view\n");
178 if (!(priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL)))) {
179 g_printerr ("modest: cannot instantiate header view\n");
185 /* online/offline combo */
186 priv->online_toggle = gtk_toggle_button_new ();
188 /* label with number of items, unread items for
189 the current folder */
190 priv->folder_info_label = gtk_label_new (NULL);
192 /* init_signals (self); */
199 modest_widget_factory_new (void)
202 ModestWidgetFactoryPrivate *priv;
204 obj = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL);
205 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
207 if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
208 g_printerr ("modest: widget factory failed to init widgets\n");
209 g_object_unref (obj);
213 return MODEST_WIDGET_FACTORY(obj);
220 modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
222 g_return_val_if_fail (self, NULL);
223 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
228 modest_widget_factory_get_header_view (ModestWidgetFactory *self)
230 g_return_val_if_fail (self, NULL);
231 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
236 modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
238 g_return_val_if_fail (self, NULL);
239 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
244 modest_widget_factory_get_account_view (ModestWidgetFactory *self)
246 ModestWidgetFactoryPrivate *priv;
247 ModestAccountMgr *account_mgr;
249 g_return_val_if_fail (self, NULL);
250 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
252 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
253 (MODEST_TNY_PLATFORM_FACTORY(priv->fact));
254 return modest_account_view_new (account_mgr);
260 modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
262 g_return_val_if_fail (self, NULL);
263 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
268 modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
270 g_return_val_if_fail (self, NULL);
271 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
277 get_transports (ModestWidgetFactory *self)
279 ModestWidgetFactoryPrivate *priv;
280 ModestAccountMgr *account_mgr;
281 GSList *transports = NULL;
282 GSList *cursor, *accounts;
284 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
287 modest_tny_platform_factory_get_account_mgr_instance
288 (MODEST_TNY_PLATFORM_FACTORY(priv->fact));
289 cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
291 ModestAccountData *data;
292 gchar *account_name = (gchar*)cursor->data;
294 data = modest_account_mgr_get_account_data (account_mgr, account_name);
295 if (data && data->transport_account) {
297 gchar *display_name = g_strdup_printf ("%s (%s)", data->email, account_name);
298 pair = modest_pair_new ((gpointer) data,
299 (gpointer) display_name , TRUE);
300 transports = g_slist_prepend (transports, pair);
302 /* don't free account name; it's freed when the transports list is freed */
303 cursor = cursor->next;
305 g_slist_free (accounts);
313 get_stores (ModestWidgetFactory *self, gboolean only_remote)
315 ModestWidgetFactoryPrivate *priv;
316 TnyAccountStore *account_store;
320 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
323 tny_platform_factory_new_account_store (priv->fact);
325 stores = tny_simple_list_new ();
326 tny_account_store_get_accounts (account_store, stores,
327 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
329 /* simply return all the stores */
333 /* remove the non-remote stores from the list */
335 iter = tny_list_create_iterator (stores);
336 while (!tny_iterator_is_done (iter)) {
337 TnyAccount *acc = (TnyAccount*)tny_iterator_get_current(iter);
338 /* is it a local account? if so, remove */
339 ModestProtocol proto = modest_protocol_info_get_protocol (tny_account_get_proto(acc));
340 if (modest_protocol_info_protocol_is_local_store(proto))
341 tny_list_remove (stores, acc); /* FIXME: iter still valid? */
342 tny_iterator_next (iter);
344 g_object_unref (G_OBJECT(iter));
351 modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
353 ModestWidgetFactoryPrivate *priv;
354 ModestPairList *protos = NULL;
355 GtkWidget* combo_box;
357 g_return_val_if_fail (self, NULL);
359 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
362 case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
363 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
365 case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
366 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
368 case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
369 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
371 case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
372 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
374 case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
375 protos = (ModestPairList *) get_transports (self);
377 /* case MODEST_COMBO_BOX_TYPE_REMOTE_STORES: */
379 /* list = get_stores (self, TRUE); /\* get all *remote* stores *\/ */
380 /* combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL(list)); */
381 /* g_object_unref (G_OBJECT(list)); */
382 /* //return combo_box; */
384 g_warning ("invalid combo box type: %d", type);
388 combo_box = modest_combo_box_new (protos);
389 g_slist_free (protos);
391 gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
399 modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
401 g_return_val_if_fail (self, NULL);
402 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
408 modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
410 g_return_val_if_fail (self, NULL);
411 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;