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 <hildon-widgets/hildon-window.h>
31 #include <hildon-widgets/hildon-note.h>
33 #include <glib/gi18n.h>
34 #include <gtk/gtktreeviewcolumn.h>
35 #include <tny-account-store-view.h>
36 #include <modest-runtime.h>
39 #include <widgets/modest-main-window.h>
40 #include <widgets/modest-msg-edit-window.h>
41 #include <widgets/modest-account-view-window.h>
44 #include "modest-widget-memory.h"
45 #include "modest-window-priv.h"
46 #include "modest-main-window-ui.h"
47 #include "modest-account-mgr.h"
48 #include "modest-conf.h"
49 #include <modest-maemo-utils.h>
50 #include "modest-tny-platform-factory.h"
51 #include "modest-tny-msg.h"
52 #include "modest-mail-operation.h"
53 #include "modest-icon-names.h"
55 /* 'private'/'protected' functions */
56 static void modest_main_window_class_init (ModestMainWindowClass *klass);
57 static void modest_main_window_init (ModestMainWindow *obj);
58 static void modest_main_window_finalize (GObject *obj);
59 static gboolean modest_main_window_window_state_event (GtkWidget *widget,
60 GdkEventWindowState *event,
63 static void connect_signals (ModestMainWindow *self);
64 static void restore_sizes (ModestMainWindow *self);
65 static void save_sizes (ModestMainWindow *self);
74 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
75 struct _ModestMainWindowPrivate {
77 GtkWidget *main_paned;
79 GtkWidget *progress_bar;
81 ModestHeaderView *header_view;
82 ModestFolderView *folder_view;
84 ModestMainWindowStyle style;
86 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
87 MODEST_TYPE_MAIN_WINDOW, \
88 ModestMainWindowPrivate))
90 typedef struct _GetMsgAsyncHelper {
91 ModestMainWindowPrivate *main_window_private;
93 ModestTnyMsgReplyType reply_type;
94 ModestTnyMsgForwardType forward_type;
101 static GtkWindowClass *parent_class = NULL;
103 /* uncomment the following if you have defined any signals */
104 /* static guint signals[LAST_SIGNAL] = {0}; */
107 modest_main_window_get_type (void)
109 static GType my_type = 0;
111 static const GTypeInfo my_info = {
112 sizeof(ModestMainWindowClass),
113 NULL, /* base init */
114 NULL, /* base finalize */
115 (GClassInitFunc) modest_main_window_class_init,
116 NULL, /* class finalize */
117 NULL, /* class data */
118 sizeof(ModestMainWindow),
120 (GInstanceInitFunc) modest_main_window_init,
123 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
131 modest_main_window_class_init (ModestMainWindowClass *klass)
133 GObjectClass *gobject_class;
134 gobject_class = (GObjectClass*) klass;
136 parent_class = g_type_class_peek_parent (klass);
137 gobject_class->finalize = modest_main_window_finalize;
139 g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
144 on_key_changed (ModestConf* conf, const gchar *key, ModestConfEvent event, ModestMainWindow *win)
148 if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME) != 0)
149 return; /* wrong key */
151 /* ok, the device name changed; thus, we have to update the
152 * local folder account name*/
154 modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
155 MODEST_LOCAL_FOLDERS_ACCOUNT_ID,
156 TNY_ACCOUNT_TYPE_STORE);
158 g_printerr ("modest: could not get account\n");
162 if (event == MODEST_CONF_EVENT_KEY_UNSET)
163 tny_account_set_name (account, MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
165 gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
166 MODEST_CONF_DEVICE_NAME, NULL);
167 tny_account_set_name (account, device_name);
168 g_free (device_name);
170 g_object_unref (G_OBJECT(account));
174 modest_main_window_init (ModestMainWindow *obj)
176 ModestMainWindowPrivate *priv;
178 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
180 priv->msg_paned = NULL;
181 priv->main_paned = NULL;
182 priv->main_vbox = NULL;
183 priv->header_view = NULL;
184 priv->folder_view = NULL;
185 priv->style = MODEST_MAIN_WINDOW_STYLE_SPLITTED;
188 priv->progress_bar = gtk_progress_bar_new ();
189 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar), 1.0);
190 gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(priv->progress_bar),
191 PANGO_ELLIPSIZE_END);
195 modest_main_window_finalize (GObject *obj)
197 G_OBJECT_CLASS(parent_class)->finalize (obj);
201 modest_main_window_get_child_widget (ModestMainWindow *self,
202 ModestWidgetType widget_type)
204 ModestMainWindowPrivate *priv;
207 g_return_val_if_fail (self, NULL);
208 g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_WIDGET_TYPE_NUM,
211 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
213 switch (widget_type) {
214 case MODEST_WIDGET_TYPE_HEADER_VIEW:
215 widget = (GtkWidget*)priv->header_view; break;
216 case MODEST_WIDGET_TYPE_FOLDER_VIEW:
217 widget = (GtkWidget*)priv->folder_view; break;
222 return widget ? GTK_WIDGET(widget) : NULL;
228 restore_sizes (ModestMainWindow *self)
231 ModestMainWindowPrivate *priv;
233 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
235 conf = modest_runtime_get_conf ();
237 modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
238 "modest-main-paned");
239 modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
245 save_sizes (ModestMainWindow *self)
248 ModestMainWindowPrivate *priv;
250 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
251 conf = modest_runtime_get_conf ();
253 modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
254 modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
255 "modest-main-paned");
256 modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
260 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
264 win = gtk_scrolled_window_new (NULL, NULL);
265 gtk_scrolled_window_set_policy
266 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
267 GTK_POLICY_AUTOMATIC);
270 gtk_scrolled_window_add_with_viewport
271 (GTK_SCROLLED_WINDOW(win), widget);
273 gtk_container_add (GTK_CONTAINER(win),
281 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMainWindow *self)
288 set_homogeneous (GtkWidget *widget,
291 gtk_tool_item_set_expand (GTK_TOOL_ITEM (widget), TRUE);
292 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), TRUE);
296 get_toolbar (ModestMainWindow *self)
298 GtkWidget *toolbar, *reply_button, *menu;
299 ModestWindowPrivate *parent_priv;
301 parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
304 toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
306 /* Set homogeneous toolbar */
307 gtk_container_foreach (GTK_CONTAINER (toolbar), set_homogeneous, NULL);
309 /* Set reply message tap and hold menu */
310 reply_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarMessageReply");
311 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolbarReplyContextMenu");
312 gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
318 connect_signals (ModestMainWindow *self)
320 ModestWindowPrivate *parent_priv;
321 ModestMainWindowPrivate *priv;
324 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
325 parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
328 g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
329 G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
331 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewContextMenu");
332 gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->folder_view), menu, NULL, 0);
335 /* g_signal_connect (G_OBJECT(priv->header_view), "status_update", */
336 /* G_CALLBACK(modest_ui_actions_on_header_status_update), self); */
337 g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
338 G_CALLBACK(modest_ui_actions_on_header_selected), self);
339 g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
340 G_CALLBACK(modest_ui_actions_on_header_activated), self);
341 g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
342 G_CALLBACK(modest_ui_actions_on_item_not_found), self);
345 g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
346 g_signal_connect (G_OBJECT (self), "window-state-event",
347 G_CALLBACK (modest_main_window_window_state_event),
352 /* modest_maemo_utils_get_device_name will probably change
353 * MODEST_CONF_DEVICE_NAME. If that happens, we update the local folders
354 * account name in the callback
356 g_signal_connect (G_OBJECT(modest_runtime_get_conf()), "key_changed",
357 G_CALLBACK(on_key_changed), self);
359 g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
364 sync_accounts_cb (ModestMainWindow *win)
366 modest_ui_actions_on_send_receive (NULL, MODEST_WINDOW(win));
372 modest_main_window_new (void)
374 ModestMainWindow *self;
375 ModestMainWindowPrivate *priv;
376 ModestWindowPrivate *parent_priv;
377 GtkWidget *header_win, *folder_win;
378 GtkActionGroup *action_group;
379 GError *error = NULL;
380 TnyFolderStoreQuery *query;
382 self = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
383 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
384 parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
386 parent_priv->ui_manager = gtk_ui_manager_new();
387 action_group = gtk_action_group_new ("ModestMainWindowActions");
388 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
390 /* Add common actions */
391 gtk_action_group_add_actions (action_group,
392 modest_action_entries,
393 G_N_ELEMENTS (modest_action_entries),
396 gtk_action_group_add_toggle_actions (action_group,
397 modest_toggle_action_entries,
398 G_N_ELEMENTS (modest_toggle_action_entries),
401 gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
402 g_object_unref (action_group);
404 /* Load the UI definition */
405 gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
406 MODEST_UIDIR "modest-main-window-ui.xml", &error);
408 g_warning ("Could not merge modest-ui.xml: %s", error->message);
409 g_error_free (error);
413 /* Add accelerators */
414 gtk_window_add_accel_group (GTK_WINDOW (self),
415 gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
417 /* add the toolbar */
418 parent_priv->toolbar = get_toolbar(self);
419 hildon_window_add_toolbar (HILDON_WINDOW (self), GTK_TOOLBAR (parent_priv->toolbar));
423 parent_priv->menubar = modest_maemo_utils_menubar_to_menu (parent_priv->ui_manager);
424 hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
427 query = tny_folder_store_query_new ();
428 tny_folder_store_query_add_item (query, NULL,
429 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
430 priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
431 if (!priv->folder_view)
432 g_printerr ("modest: cannot instantiate folder view\n");
433 g_object_unref (G_OBJECT (query));
434 modest_maemo_utils_get_device_name ();
438 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
439 if (!priv->header_view)
440 g_printerr ("modest: cannot instantiate header view\n");
441 modest_header_view_set_style (priv->header_view, MODEST_HEADER_VIEW_STYLE_TWOLINES);
443 folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
444 header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);
447 priv->main_paned = gtk_hpaned_new ();
448 gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
449 gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
450 gtk_widget_show (GTK_WIDGET(priv->header_view));
451 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
453 /* putting it all together... */
454 priv->main_vbox = gtk_vbox_new (FALSE, 6);
455 gtk_box_pack_start (GTK_BOX(priv->main_vbox), priv->main_paned, TRUE, TRUE,0);
457 gtk_container_add (GTK_CONTAINER(self), priv->main_vbox);
458 restore_sizes (MODEST_MAIN_WINDOW(self));
460 gtk_window_set_title (GTK_WINDOW(self), _("Modest"));
461 gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
462 gtk_widget_show_all (priv->main_vbox);
464 /* /\* should we hide the toolbar? *\/ */
465 /* if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL)) */
466 /* gtk_widget_hide (parent_priv->toolbar); */
468 /* Connect signals */
469 connect_signals (self);
471 /* Set account store */
472 tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
473 TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
474 g_idle_add ((GSourceFunc)sync_accounts_cb, self);
475 /* do send & receive when we are idle */
477 g_message ("online? %s",
478 tny_device_is_online (modest_runtime_get_device()) ? "yes" : "no");
480 /* Needed to show the contents of the toolbar */
481 gtk_widget_show_all (GTK_WIDGET (self));
483 return MODEST_WINDOW(self);
487 modest_main_window_close_all (ModestMainWindow *self)
490 GtkResponseType response;
492 /* Create the confirmation dialog MSG-NOT308 */
493 note = hildon_note_new_confirmation_add_buttons (GTK_WINDOW (self),
494 _("emev_nc_close_windows"),
495 _("mcen_db_yes"), GTK_RESPONSE_YES,
496 _("mcen_db_no"), GTK_RESPONSE_NO,
499 response = gtk_dialog_run (GTK_DIALOG (note));
500 gtk_widget_destroy (GTK_WIDGET (note));
502 if (response == GTK_RESPONSE_YES)
509 modest_main_window_set_style (ModestMainWindow *self,
510 ModestMainWindowStyle style)
512 ModestMainWindowPrivate *priv;
513 GtkWidget *scrolled_win;
515 g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
517 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
519 /* no change -> nothing to do */
520 if (priv->style == style)
524 scrolled_win = gtk_widget_get_parent (GTK_WIDGET (priv->header_view));
527 case MODEST_MAIN_WINDOW_STYLE_SIMPLE:
528 /* Remove main paned */
529 g_object_ref (priv->main_paned);
530 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
532 /* Reparent header view with scrolled window */
533 gtk_widget_reparent (scrolled_win, priv->main_vbox);
535 case MODEST_MAIN_WINDOW_STYLE_SPLITTED:
536 /* Remove header view */
537 g_object_ref (scrolled_win);
538 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), scrolled_win);
540 /* Reparent the main paned */
541 gtk_paned_add2 (GTK_PANED (priv->main_paned), scrolled_win);
542 gtk_container_add (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
545 g_return_if_reached ();
549 gtk_widget_show (GTK_WIDGET (priv->main_vbox));
552 ModestMainWindowStyle
553 modest_main_window_get_style (ModestMainWindow *self)
555 ModestMainWindowPrivate *priv;
557 g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (self), -1);
559 priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
564 modest_main_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer userdata)
566 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
567 ModestWindowPrivate *parent_priv;
568 ModestWindowMgr *mgr;
569 gboolean is_fullscreen;
570 GtkAction *fs_toggle_action;
573 mgr = modest_runtime_get_window_mgr ();
575 is_fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
577 parent_priv = MODEST_WINDOW_GET_PRIVATE (widget);
579 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ShowToggleFullscreenMenu");
580 active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)))?1:0;
581 if (is_fullscreen != active) {
582 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), is_fullscreen);