1 /* Copyright (c) 2009, 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 <modest-mailboxes-window.h>
31 #include <hildon/hildon-pannable-area.h>
32 #include <modest-window-mgr.h>
33 #include <modest-signal-mgr.h>
34 #include <modest-runtime.h>
35 #include <modest-platform.h>
36 #include <modest-maemo-utils.h>
37 #include <modest-icon-names.h>
38 #include <modest-ui-constants.h>
39 #include <modest-account-mgr.h>
40 #include <modest-account-mgr-helpers.h>
41 #include <modest-defs.h>
42 #include <modest-ui-actions.h>
43 #include <modest-window.h>
44 #include <hildon/hildon.h>
45 #include <tny-account-store-view.h>
46 #include <modest-header-window.h>
47 #include <modest-ui-dimming-rules.h>
48 #include <modest-ui-dimming-manager.h>
49 #include <modest-window-priv.h>
50 #include "modest-text-utils.h"
51 #include "modest-tny-account.h"
52 #include <modest-folder-window.h>
54 /* 'private'/'protected' functions */
55 static void modest_mailboxes_window_class_init (ModestMailboxesWindowClass *klass);
56 static void modest_mailboxes_window_init (ModestMailboxesWindow *obj);
57 static void modest_mailboxes_window_finalize (GObject *obj);
59 static void connect_signals (ModestMailboxesWindow *self);
60 static void modest_mailboxes_window_disconnect_signals (ModestWindow *self);
62 static void on_mailbox_activated (ModestFolderView *mailboxes_view,
65 static void on_progress_list_changed (ModestWindowMgr *mgr,
66 ModestMailboxesWindow *self);
67 static gboolean on_map_event (GtkWidget *widget,
70 static void update_progress_hint (ModestMailboxesWindow *self);
71 static void on_queue_changed (ModestMailOperationQueue *queue,
72 ModestMailOperation *mail_op,
73 ModestMailOperationQueueNotification type,
74 ModestMailboxesWindow *self);
75 static void on_activity_changed (ModestFolderView *view,
77 ModestMailboxesWindow *folder_window);
78 static gboolean on_key_press(GtkWidget *widget,
82 typedef struct _ModestMailboxesWindowPrivate ModestMailboxesWindowPrivate;
83 struct _ModestMailboxesWindowPrivate {
85 GtkWidget *folder_view;
87 GtkWidget *new_message_button;
92 gchar *current_store_account;
93 gboolean progress_hint;
94 guint queue_change_handler;
96 #define MODEST_MAILBOXES_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
97 MODEST_TYPE_MAILBOXES_WINDOW, \
98 ModestMailboxesWindowPrivate))
101 static GtkWindowClass *parent_class = NULL;
103 /************************************************************************/
106 modest_mailboxes_window_get_type (void)
108 static GType my_type = 0;
110 static const GTypeInfo my_info = {
111 sizeof(ModestMailboxesWindowClass),
112 NULL, /* base init */
113 NULL, /* base finalize */
114 (GClassInitFunc) modest_mailboxes_window_class_init,
115 NULL, /* class finalize */
116 NULL, /* class data */
117 sizeof(ModestMailboxesWindow),
119 (GInstanceInitFunc) modest_mailboxes_window_init,
122 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
123 "ModestMailboxesWindow",
130 modest_mailboxes_window_class_init (ModestMailboxesWindowClass *klass)
132 GObjectClass *gobject_class;
133 gobject_class = (GObjectClass*) klass;
134 ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
136 parent_class = g_type_class_peek_parent (klass);
137 gobject_class->finalize = modest_mailboxes_window_finalize;
139 g_type_class_add_private (gobject_class, sizeof(ModestMailboxesWindowPrivate));
141 modest_window_class->disconnect_signals_func = modest_mailboxes_window_disconnect_signals;
145 modest_mailboxes_window_init (ModestMailboxesWindow *obj)
147 ModestMailboxesWindowPrivate *priv;
149 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
151 priv->sighandlers = NULL;
153 priv->folder_view = NULL;
155 priv->top_vbox = NULL;
157 priv->current_store_account = NULL;
158 priv->progress_hint = FALSE;
159 priv->queue_change_handler = 0;
163 modest_mailboxes_window_finalize (GObject *obj)
165 ModestMailboxesWindowPrivate *priv;
167 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
169 if (priv->current_store_account) {
170 g_free (priv->current_store_account);
171 priv->current_store_account = NULL;
174 /* Sanity check: shouldn't be needed, the window mgr should
175 call this function before */
176 modest_mailboxes_window_disconnect_signals (MODEST_WINDOW (obj));
178 G_OBJECT_CLASS(parent_class)->finalize (obj);
182 modest_mailboxes_window_disconnect_signals (ModestWindow *self)
184 ModestMailboxesWindowPrivate *priv;
185 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
187 if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
188 priv->queue_change_handler))
189 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
190 priv->queue_change_handler);
192 modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
193 priv->sighandlers = NULL;
197 on_visible_account_changed (ModestFolderView *mailboxes_view,
198 const gchar *account_id,
203 modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
204 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
210 name = modest_tny_account_get_parent_modest_account_name_for_server_account (acc);
211 title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
214 gtk_window_set_title (GTK_WINDOW (user_data), title);
217 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
219 g_object_unref (acc);
222 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
227 connect_signals (ModestMailboxesWindow *self)
229 ModestMailboxesWindowPrivate *priv;
231 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
234 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
235 G_OBJECT (priv->folder_view), "folder-activated",
236 G_CALLBACK (on_mailbox_activated), self);
238 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
239 G_OBJECT (modest_runtime_get_window_mgr ()),
240 "progress-list-changed",
241 G_CALLBACK (on_progress_list_changed), self);
243 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
244 G_OBJECT (priv->folder_view),
246 G_CALLBACK (on_activity_changed), self);
248 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
249 G_OBJECT (priv->folder_view),
250 "visible-account-changed",
251 G_CALLBACK (on_visible_account_changed), self);
254 modest_signal_mgr_connect (priv->sighandlers,
255 G_OBJECT (priv->new_message_button),
257 G_CALLBACK (modest_ui_actions_on_new_msg), self);
259 /* connect window keys -> priv->folder_view scroll here? */
260 g_signal_connect(G_OBJECT(self), "key-press-event",
261 G_CALLBACK(on_key_press), self);
265 modest_mailboxes_window_new (const gchar *account)
267 ModestMailboxesWindow *self = NULL;
268 ModestMailboxesWindowPrivate *priv = NULL;
270 GdkPixbuf *window_icon;
272 GtkWidget *action_area_box;
273 GdkPixbuf *new_message_pixbuf;
275 GdkModifierType accel_mods;
276 GtkAccelGroup *accel_group;
277 GtkWidget *top_alignment;
278 #ifdef MODEST_TOOLKIT_HILDON2
279 GtkWidget *live_search;
282 self = MODEST_MAILBOXES_WINDOW(g_object_new(MODEST_TYPE_MAILBOXES_WINDOW, NULL));
283 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
285 pannable = hildon_pannable_area_new ();
286 priv->queue_change_handler =
287 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
289 G_CALLBACK (on_queue_changed),
292 priv->folder_view = modest_platform_create_folder_view (NULL);
293 #ifdef MODEST_TOOLKIT_HILDON2
294 live_search = modest_folder_view_setup_live_search (MODEST_FOLDER_VIEW (priv->folder_view));
295 hildon_live_search_widget_hook (HILDON_LIVE_SEARCH (live_search), GTK_WIDGET (self), priv->folder_view);
297 modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
298 MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
299 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view),
300 MODEST_FOLDER_VIEW_FILTER_HIDE_ACCOUNTS);
302 action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
303 priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
305 hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
306 new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
307 hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
308 g_object_unref (new_message_pixbuf);
310 gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
311 gtk_widget_show_all (priv->new_message_button);
312 hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), TRUE);
314 /* Set account store */
315 tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
316 TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
318 priv->top_vbox = gtk_vbox_new (0, FALSE);
319 top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
320 gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment),
322 MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
324 gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
325 gtk_box_pack_end (GTK_BOX (priv->top_vbox), pannable, TRUE, TRUE, 0);
326 #ifdef MODEST_TOOLKIT_HILDON2
327 gtk_box_pack_end (GTK_BOX (priv->top_vbox), live_search, FALSE, FALSE, 0);
329 gtk_container_add (GTK_CONTAINER (top_alignment), priv->top_vbox);
330 gtk_container_add (GTK_CONTAINER (self), top_alignment);
332 gtk_widget_show (priv->folder_view);
333 gtk_widget_show (pannable);
334 gtk_widget_show (top_alignment);
335 gtk_widget_show (priv->top_vbox);
337 connect_signals (MODEST_MAILBOXES_WINDOW (self));
339 /* Get device name */
340 modest_maemo_utils_get_device_name ();
342 app = hildon_program_get_instance ();
343 hildon_program_add_window (app, HILDON_WINDOW (self));
345 /* Set window icon */
346 window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
348 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
349 g_object_unref (window_icon);
352 /* Dont't restore settings here,
353 * because it requires a gtk_widget_show(),
354 * and we don't want to do that until later,
355 * so that the UI is not visible for non-menu D-Bus activation.
358 g_signal_connect (G_OBJECT (self), "map-event",
359 G_CALLBACK (on_map_event),
361 update_progress_hint (self);
363 accel_group = gtk_accel_group_new ();
364 gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
365 gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
366 accel_key, accel_mods, 0);
367 gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
369 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view),
370 MODEST_FOLDER_VIEW_FILTER_SHOW_ONLY_MAILBOXES);
372 modest_mailboxes_window_set_account (MODEST_MAILBOXES_WINDOW (self), account);
374 return MODEST_WINDOW(self);
378 modest_mailboxes_window_get_folder_view (ModestMailboxesWindow *self)
380 ModestMailboxesWindowPrivate *priv = NULL;
382 g_return_val_if_fail (MODEST_IS_MAILBOXES_WINDOW(self), FALSE);
384 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
386 return MODEST_FOLDER_VIEW (priv->folder_view);
390 modest_mailboxes_window_set_account (ModestMailboxesWindow *self,
391 const gchar *account_name)
393 ModestMailboxesWindowPrivate *priv = NULL;
394 ModestAccountMgr *mgr;
395 ModestAccountSettings *settings = NULL;
396 ModestServerAccountSettings *store_settings = NULL;
398 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
400 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
402 /* Get account data */
403 mgr = modest_runtime_get_account_mgr ();
405 settings = modest_account_mgr_load_account_settings (mgr, account_name);
409 store_settings = modest_account_settings_get_store_settings (settings);
413 if (priv->current_store_account != NULL)
414 g_free (priv->current_store_account);
415 priv->current_store_account = g_strdup (modest_server_account_settings_get_account_name (store_settings));
417 modest_folder_view_set_account_id_of_visible_server_account
418 (MODEST_FOLDER_VIEW (priv->folder_view),
419 priv->current_store_account);
421 modest_window_set_active_account (MODEST_WINDOW (self), account_name);
422 update_progress_hint (self);
426 g_object_unref (store_settings);
428 g_object_unref (settings);
433 on_mailbox_activated (ModestFolderView *mailboxes_view,
437 ModestMailboxesWindowPrivate *priv = NULL;
438 ModestMailboxesWindow *self = (ModestMailboxesWindow *) userdata;
439 GtkWidget *new_window;
441 const gchar *active_account;
443 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
445 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
450 if (!TNY_IS_FOLDER (folder))
453 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
454 active_account = modest_window_get_active_account (MODEST_WINDOW (self));
455 modest_window_set_active_account (MODEST_WINDOW (new_window), active_account);
456 registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
457 MODEST_WINDOW (new_window),
458 MODEST_WINDOW (self));
461 gtk_widget_destroy (new_window);
465 active_account = modest_window_get_active_account (MODEST_WINDOW (self));
466 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), active_account);
467 name = tny_folder_get_name (folder);
469 modest_folder_window_set_mailbox (MODEST_FOLDER_WINDOW (new_window), name);
471 gtk_widget_show (new_window);
477 on_map_event (GtkWidget *widget,
481 ModestMailboxesWindow *self = (ModestMailboxesWindow *) userdata;
482 ModestMailboxesWindowPrivate *priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
484 if (priv->progress_hint) {
485 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
492 has_active_operations (ModestMailboxesWindow *self)
494 GSList *operations = NULL, *node;
495 ModestMailOperationQueue *queue;
496 gboolean has_active = FALSE;
498 queue = modest_runtime_get_mail_operation_queue ();
499 operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
501 for (node = operations; node != NULL; node = g_slist_next (node)) {
502 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
509 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
510 g_slist_free (operations);
517 update_progress_hint (ModestMailboxesWindow *self)
519 ModestMailboxesWindowPrivate *priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
521 if (has_active_operations (self)) {
522 priv->progress_hint = TRUE;
524 priv->progress_hint = FALSE;
527 if (!priv->progress_hint && priv->current_store_account) {
528 priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
529 priv->current_store_account);
532 if (!priv->progress_hint) {
533 priv->progress_hint = modest_folder_view_get_activity (MODEST_FOLDER_VIEW (priv->folder_view));
536 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
538 if (GTK_WIDGET_VISIBLE (self)) {
539 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint ? 1:0);
544 on_progress_list_changed (ModestWindowMgr *mgr,
545 ModestMailboxesWindow *self)
547 update_progress_hint (self);
551 on_mail_operation_started (ModestMailOperation *mail_op,
554 ModestMailboxesWindow *self;
555 ModestMailOperationTypeOperation op_type;
556 GObject *source = NULL;
558 self = MODEST_MAILBOXES_WINDOW (user_data);
559 op_type = modest_mail_operation_get_type_operation (mail_op);
560 source = modest_mail_operation_get_source(mail_op);
561 if (G_OBJECT (self) == source) {
562 update_progress_hint (self);
564 g_object_unref (source);
568 on_mail_operation_finished (ModestMailOperation *mail_op,
571 ModestMailboxesWindow *self;
573 self = MODEST_MAILBOXES_WINDOW (user_data);
575 /* Don't disable the progress hint if there are more pending
576 operations from this window */
577 update_progress_hint (self);
579 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
583 on_queue_changed (ModestMailOperationQueue *queue,
584 ModestMailOperation *mail_op,
585 ModestMailOperationQueueNotification type,
586 ModestMailboxesWindow *self)
588 ModestMailboxesWindowPrivate *priv;
590 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
592 /* If this operations was created by another window, do nothing */
593 if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self)))
596 if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
597 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
600 G_CALLBACK (on_mail_operation_started),
602 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
604 "operation-finished",
605 G_CALLBACK (on_mail_operation_finished),
607 } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
608 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
610 "operation-started");
611 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
613 "operation-finished");
618 on_activity_changed (ModestFolderView *view,
620 ModestMailboxesWindow *self)
622 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW (self));
624 update_progress_hint (self);
629 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
631 ModestMailboxesWindowPrivate *priv;
632 HildonPannableArea *pannable;
634 if (event->type == GDK_KEY_RELEASE)
637 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(user_data);
639 pannable = HILDON_PANNABLE_AREA (gtk_widget_get_parent (priv->folder_view));
641 switch (event->keyval) {
644 modest_maemo_utils_scroll_pannable(pannable, 0, -1);
648 modest_maemo_utils_scroll_pannable(pannable, 0, 1);