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 <modest-scrollable.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-icon-names.h>
37 #include <modest-ui-constants.h>
38 #include <modest-account-mgr.h>
39 #include <modest-account-mgr-helpers.h>
40 #include <modest-defs.h>
41 #include <modest-ui-actions.h>
42 #include <modest-window.h>
43 #include <tny-account-store-view.h>
44 #include <modest-header-window.h>
45 #include <modest-ui-dimming-rules.h>
46 #include <modest-ui-dimming-manager.h>
47 #include <modest-window-priv.h>
48 #include "modest-text-utils.h"
49 #include "modest-tny-account.h"
50 #include <modest-folder-window.h>
51 #ifdef MODEST_TOOLKIT_HILDON2
52 #include <hildon/hildon.h>
54 #ifdef MODEST_PLATFORM_MAEMO
55 #include <modest-maemo-utils.h>
57 #include <gdk/gdkkeysyms.h>
59 /* 'private'/'protected' functions */
60 static void modest_mailboxes_window_class_init (ModestMailboxesWindowClass *klass);
61 static void modest_mailboxes_window_init (ModestMailboxesWindow *obj);
62 static void modest_mailboxes_window_finalize (GObject *obj);
64 static void connect_signals (ModestMailboxesWindow *self);
65 static void modest_mailboxes_window_disconnect_signals (ModestWindow *self);
67 static void on_mailbox_activated (ModestFolderView *mailboxes_view,
70 static void on_progress_list_changed (ModestWindowMgr *mgr,
71 ModestMailboxesWindow *self);
72 static gboolean on_map_event (GtkWidget *widget,
75 static void update_progress_hint (ModestMailboxesWindow *self);
76 static void on_queue_changed (ModestMailOperationQueue *queue,
77 ModestMailOperation *mail_op,
78 ModestMailOperationQueueNotification type,
79 ModestMailboxesWindow *self);
80 static void on_activity_changed (ModestFolderView *view,
82 ModestMailboxesWindow *folder_window);
83 #ifdef MODEST_TOOLKIT_HILDON2
84 static gboolean on_key_press(GtkWidget *widget,
89 typedef struct _ModestMailboxesWindowPrivate ModestMailboxesWindowPrivate;
90 struct _ModestMailboxesWindowPrivate {
92 GtkWidget *folder_view;
94 GtkWidget *new_message_button;
99 gchar *current_store_account;
100 gboolean progress_hint;
101 guint queue_change_handler;
103 #define MODEST_MAILBOXES_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
104 MODEST_TYPE_MAILBOXES_WINDOW, \
105 ModestMailboxesWindowPrivate))
108 static GtkWindowClass *parent_class = NULL;
110 /************************************************************************/
113 modest_mailboxes_window_get_type (void)
115 static GType my_type = 0;
117 static const GTypeInfo my_info = {
118 sizeof(ModestMailboxesWindowClass),
119 NULL, /* base init */
120 NULL, /* base finalize */
121 (GClassInitFunc) modest_mailboxes_window_class_init,
122 NULL, /* class finalize */
123 NULL, /* class data */
124 sizeof(ModestMailboxesWindow),
126 (GInstanceInitFunc) modest_mailboxes_window_init,
129 my_type = g_type_register_static (
130 #ifdef MODEST_TOOLKIT_HILDON2
131 MODEST_TYPE_HILDON2_WINDOW,
133 MODEST_TYPE_SHELL_WINDOW,
135 "ModestMailboxesWindow",
142 modest_mailboxes_window_class_init (ModestMailboxesWindowClass *klass)
144 GObjectClass *gobject_class;
145 gobject_class = (GObjectClass*) klass;
146 ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
148 parent_class = g_type_class_peek_parent (klass);
149 gobject_class->finalize = modest_mailboxes_window_finalize;
151 g_type_class_add_private (gobject_class, sizeof(ModestMailboxesWindowPrivate));
153 modest_window_class->disconnect_signals_func = modest_mailboxes_window_disconnect_signals;
157 modest_mailboxes_window_init (ModestMailboxesWindow *obj)
159 ModestMailboxesWindowPrivate *priv;
161 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
163 priv->sighandlers = NULL;
165 priv->folder_view = NULL;
167 priv->top_vbox = NULL;
169 priv->current_store_account = NULL;
170 priv->progress_hint = FALSE;
171 priv->queue_change_handler = 0;
175 modest_mailboxes_window_finalize (GObject *obj)
177 ModestMailboxesWindowPrivate *priv;
179 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
181 if (priv->current_store_account) {
182 g_free (priv->current_store_account);
183 priv->current_store_account = NULL;
186 /* Sanity check: shouldn't be needed, the window mgr should
187 call this function before */
188 modest_mailboxes_window_disconnect_signals (MODEST_WINDOW (obj));
190 G_OBJECT_CLASS(parent_class)->finalize (obj);
194 modest_mailboxes_window_disconnect_signals (ModestWindow *self)
196 ModestMailboxesWindowPrivate *priv;
197 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
199 if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
200 priv->queue_change_handler))
201 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
202 priv->queue_change_handler);
204 modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
205 priv->sighandlers = NULL;
209 on_visible_account_changed (ModestFolderView *mailboxes_view,
210 const gchar *account_id,
215 modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
216 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
222 name = modest_tny_account_get_parent_modest_account_name_for_server_account (acc);
223 title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
226 modest_window_set_title (MODEST_WINDOW (user_data), title);
229 modest_window_set_title (MODEST_WINDOW (user_data), _("mcen_ap_name"));
231 g_object_unref (acc);
234 modest_window_set_title (MODEST_WINDOW (user_data), _("mcen_ap_name"));
239 connect_signals (ModestMailboxesWindow *self)
241 ModestMailboxesWindowPrivate *priv;
243 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
246 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
247 G_OBJECT (priv->folder_view), "folder-activated",
248 G_CALLBACK (on_mailbox_activated), self);
250 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
251 G_OBJECT (modest_runtime_get_window_mgr ()),
252 "progress-list-changed",
253 G_CALLBACK (on_progress_list_changed), self);
255 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
256 G_OBJECT (priv->folder_view),
258 G_CALLBACK (on_activity_changed), self);
260 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
261 G_OBJECT (priv->folder_view),
262 "visible-account-changed",
263 G_CALLBACK (on_visible_account_changed), self);
266 modest_signal_mgr_connect (priv->sighandlers,
267 G_OBJECT (priv->new_message_button),
269 G_CALLBACK (modest_ui_actions_on_new_msg), self);
271 #ifdef MODEST_TOOLKIT_HILDON2
272 /* connect window keys -> priv->folder_view scroll here? */
273 g_signal_connect(G_OBJECT(self), "key-press-event",
274 G_CALLBACK(on_key_press), self);
279 modest_mailboxes_window_new (const gchar *account)
281 ModestMailboxesWindow *self = NULL;
282 ModestMailboxesWindowPrivate *priv = NULL;
283 GdkPixbuf *window_icon;
284 GtkWidget *scrollable;
285 GtkWidget *top_alignment;
287 self = MODEST_MAILBOXES_WINDOW(g_object_new(MODEST_TYPE_MAILBOXES_WINDOW, NULL));
288 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
290 scrollable = modest_toolkit_factory_create_scrollable (modest_runtime_get_toolkit_factory ());
291 priv->queue_change_handler =
292 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
294 G_CALLBACK (on_queue_changed),
297 priv->folder_view = modest_platform_create_folder_view (NULL);
298 modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
299 MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
300 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view),
301 MODEST_FOLDER_VIEW_FILTER_HIDE_ACCOUNTS);
303 #ifdef MODEST_TOOLKIT_HILDON2
304 GtkWidget *action_area_box;
305 GdkPixbuf *new_message_pixbuf;
306 action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
307 priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
309 hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
310 new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
311 hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
312 g_object_unref (new_message_pixbuf);
314 gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
315 gtk_widget_show_all (priv->new_message_button);
316 hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), TRUE);
319 /* Set account store */
320 tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
321 TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
323 priv->top_vbox = gtk_vbox_new (0, FALSE);
324 top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
325 gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment),
327 MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
329 gtk_container_add (GTK_CONTAINER (scrollable), priv->folder_view);
330 gtk_box_pack_end (GTK_BOX (priv->top_vbox), scrollable, TRUE, TRUE, 0);
331 gtk_container_add (GTK_CONTAINER (top_alignment), priv->top_vbox);
332 gtk_container_add (GTK_CONTAINER (self), top_alignment);
334 gtk_widget_show (priv->folder_view);
335 gtk_widget_show (scrollable);
336 gtk_widget_show (top_alignment);
337 gtk_widget_show (priv->top_vbox);
339 connect_signals (MODEST_MAILBOXES_WINDOW (self));
341 /* Get device name */
342 #ifdef MODEST_PLATFORM_MAEMO
343 modest_maemo_utils_get_device_name ();
346 #ifdef MODEST_TOOLKIT_HILDON2
349 app = hildon_program_get_instance ();
350 hildon_program_add_window (app, HILDON_WINDOW (self));
353 /* Set window icon */
354 window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
356 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
357 g_object_unref (window_icon);
360 /* Dont't restore settings here,
361 * because it requires a gtk_widget_show(),
362 * and we don't want to do that until later,
363 * so that the UI is not visible for non-menu D-Bus activation.
366 g_signal_connect (G_OBJECT (self), "map-event",
367 G_CALLBACK (on_map_event),
369 update_progress_hint (self);
371 #ifdef MODEST_TOOLKIT_HILDON2
373 GdkModifierType accel_mods;
374 GtkAccelGroup *accel_group;
376 accel_group = gtk_accel_group_new ();
377 gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
378 gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
379 accel_key, accel_mods, 0);
380 gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
383 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view),
384 MODEST_FOLDER_VIEW_FILTER_SHOW_ONLY_MAILBOXES);
386 modest_mailboxes_window_set_account (MODEST_MAILBOXES_WINDOW (self), account);
388 return MODEST_WINDOW(self);
392 modest_mailboxes_window_get_folder_view (ModestMailboxesWindow *self)
394 ModestMailboxesWindowPrivate *priv = NULL;
396 g_return_val_if_fail (MODEST_IS_MAILBOXES_WINDOW(self), FALSE);
398 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
400 return MODEST_FOLDER_VIEW (priv->folder_view);
404 modest_mailboxes_window_set_account (ModestMailboxesWindow *self,
405 const gchar *account_name)
407 ModestMailboxesWindowPrivate *priv = NULL;
408 ModestAccountMgr *mgr;
409 ModestAccountSettings *settings = NULL;
410 ModestServerAccountSettings *store_settings = NULL;
412 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
414 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
416 /* Get account data */
417 mgr = modest_runtime_get_account_mgr ();
419 settings = modest_account_mgr_load_account_settings (mgr, account_name);
423 store_settings = modest_account_settings_get_store_settings (settings);
427 if (priv->current_store_account != NULL)
428 g_free (priv->current_store_account);
429 priv->current_store_account = g_strdup (modest_server_account_settings_get_account_name (store_settings));
431 modest_folder_view_set_account_id_of_visible_server_account
432 (MODEST_FOLDER_VIEW (priv->folder_view),
433 priv->current_store_account);
435 modest_window_set_active_account (MODEST_WINDOW (self), account_name);
436 update_progress_hint (self);
440 g_object_unref (store_settings);
442 g_object_unref (settings);
447 on_mailbox_activated (ModestFolderView *mailboxes_view,
451 ModestMailboxesWindowPrivate *priv = NULL;
452 ModestMailboxesWindow *self = (ModestMailboxesWindow *) userdata;
453 GtkWidget *new_window;
455 const gchar *active_account;
457 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
459 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
464 if (!TNY_IS_FOLDER (folder))
467 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
468 registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
469 MODEST_WINDOW (new_window),
470 MODEST_WINDOW (self));
473 gtk_widget_destroy (new_window);
477 active_account = modest_window_get_active_account (MODEST_WINDOW (self));
478 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), active_account);
479 name = tny_folder_get_name (folder);
481 modest_folder_window_set_mailbox (MODEST_FOLDER_WINDOW (new_window), name);
483 gtk_widget_show (new_window);
489 on_map_event (GtkWidget *widget,
493 ModestMailboxesWindow *self = (ModestMailboxesWindow *) userdata;
494 ModestMailboxesWindowPrivate *priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
496 if (priv->progress_hint) {
497 modest_window_show_progress (MODEST_WINDOW (self), TRUE);
504 has_active_operations (ModestMailboxesWindow *self)
506 GSList *operations = NULL, *node;
507 ModestMailOperationQueue *queue;
508 gboolean has_active = FALSE;
510 queue = modest_runtime_get_mail_operation_queue ();
511 operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
513 for (node = operations; node != NULL; node = g_slist_next (node)) {
514 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
521 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
522 g_slist_free (operations);
529 update_progress_hint (ModestMailboxesWindow *self)
531 ModestMailboxesWindowPrivate *priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
533 if (has_active_operations (self)) {
534 priv->progress_hint = TRUE;
536 priv->progress_hint = FALSE;
539 if (!priv->progress_hint && priv->current_store_account) {
540 priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
541 priv->current_store_account);
544 if (!priv->progress_hint) {
545 priv->progress_hint = modest_folder_view_get_activity (MODEST_FOLDER_VIEW (priv->folder_view));
548 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
550 if (GTK_WIDGET_VISIBLE (self)) {
551 modest_window_show_progress (MODEST_WINDOW (self), priv->progress_hint ? 1:0);
556 on_progress_list_changed (ModestWindowMgr *mgr,
557 ModestMailboxesWindow *self)
559 update_progress_hint (self);
563 on_mail_operation_started (ModestMailOperation *mail_op,
566 ModestMailboxesWindow *self;
567 ModestMailOperationTypeOperation op_type;
568 GObject *source = NULL;
570 self = MODEST_MAILBOXES_WINDOW (user_data);
571 op_type = modest_mail_operation_get_type_operation (mail_op);
572 source = modest_mail_operation_get_source(mail_op);
573 if (G_OBJECT (self) == source) {
574 update_progress_hint (self);
576 g_object_unref (source);
580 on_mail_operation_finished (ModestMailOperation *mail_op,
583 ModestMailboxesWindow *self;
585 self = MODEST_MAILBOXES_WINDOW (user_data);
587 /* Don't disable the progress hint if there are more pending
588 operations from this window */
589 update_progress_hint (self);
591 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
595 on_queue_changed (ModestMailOperationQueue *queue,
596 ModestMailOperation *mail_op,
597 ModestMailOperationQueueNotification type,
598 ModestMailboxesWindow *self)
600 ModestMailboxesWindowPrivate *priv;
602 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
604 /* If this operations was created by another window, do nothing */
605 if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self)))
608 if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
609 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
612 G_CALLBACK (on_mail_operation_started),
614 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
616 "operation-finished",
617 G_CALLBACK (on_mail_operation_finished),
619 } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
620 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
622 "operation-started");
623 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
625 "operation-finished");
630 on_activity_changed (ModestFolderView *view,
632 ModestMailboxesWindow *self)
634 g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW (self));
636 update_progress_hint (self);
639 #ifdef MODEST_TOOLKIT_HILDON2
641 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
643 ModestMailboxesWindowPrivate *priv;
644 ModestScrollable *scrollable;
646 if (event->type == GDK_KEY_RELEASE)
649 priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(user_data);
651 scrollable = MODEST_SCROLLABLE (gtk_widget_get_parent (priv->folder_view));
653 switch (event->keyval) {
656 modest_scrollable_scroll (scrollable, 0, -1);
660 modest_scrollable_scroll (scrollable, 0, 1);