+ /* Create the items for the Tools->Send&Receive submenu */
+ refresh_action_name = g_strconcat ("SendReceive", account_name, NULL);
+ refresh_account_action = gtk_action_new ((const gchar*) refresh_action_name,
+ display_name, NULL, NULL);
+ gtk_action_group_add_action (priv->view_additions_group, refresh_account_action);
+
+ merge_id = (guint8) gtk_ui_manager_new_merge_id (parent_priv->ui_manager);
+ priv->merge_ids = g_byte_array_append (priv->merge_ids, &merge_id, 1);
+ gtk_ui_manager_add_ui (parent_priv->ui_manager,
+ merge_id,
+ "/MenuBar/ToolsMenu/ToolsSendReceiveMainMenu/ToolsMenuAdditions",
+ item_name,
+ refresh_action_name,
+ GTK_UI_MANAGER_MENUITEM,
+ FALSE);
+ g_free (refresh_action_name);
+
+ g_signal_connect_data (G_OBJECT (refresh_account_action),
+ "activate",
+ G_CALLBACK (on_refresh_account_action_activated),
+ g_strdup (account_name),
+ (GClosureNotify) g_free,
+ 0);
+
+ /* Create item and add it to the send&receive
+ CSM. If there is only one account then
+ it'll be no menu */
+ if (num_accounts > 1) {
+ GtkWidget *label = gtk_label_new(NULL);
+ gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+ if (default_account && (strcmp(account_name, default_account) == 0)) {
+ gchar *escaped = g_markup_printf_escaped ("<b>%s</b>", display_name);
+ gtk_label_set_markup (GTK_LABEL (label), escaped);
+ g_free (escaped);
+ } else {
+ gtk_label_set_text (GTK_LABEL (label), display_name);
+ }
+
+ item = gtk_menu_item_new ();
+ gtk_container_add (GTK_CONTAINER (item), label);
+
+ gtk_menu_shell_prepend (GTK_MENU_SHELL (priv->accounts_popup),
+ GTK_WIDGET (item));
+ g_signal_connect_data (G_OBJECT (item),
+ "activate",
+ G_CALLBACK (on_send_receive_csm_activated),
+ g_strdup (account_name),
+ (GClosureNotify) g_free,
+ 0);
+ }
+ g_free (item_name);
+ }
+
+ /* Frees */
+ g_free (display_name);
+ }
+
+ gtk_ui_manager_insert_action_group (parent_priv->ui_manager, priv->view_additions_group, 1);
+
+ /* We cannot do this in the loop above because this relies on the action
+ * group being inserted. This makes the default account appear in bold.
+ * I agree it is a rather ugly way, but I don't see another possibility. armin. */
+ for (i = 0; i < num_accounts; i++) {
+ gchar *item_name, *path;
+ GtkWidget *item;
+ ModestAccountSettings *settings;
+ const gchar *account_name;
+ gboolean is_default;
+
+ settings = (ModestAccountSettings *) g_slist_nth_data (accounts, i);
+ account_name = modest_account_settings_get_account_name (settings);
+ is_default = (account_name && default_account && !strcmp (account_name, default_account));
+
+ /* Get the item of the view menu */
+ item_name = g_strconcat (account_name, "Menu", NULL);
+ path = g_strconcat ("/MenuBar/ViewMenu/ViewMenuAdditions/", item_name, NULL);
+ item = gtk_ui_manager_get_widget (parent_priv->ui_manager, path);
+ g_free(path);
+
+ if (item) {
+ GtkWidget *child = gtk_bin_get_child (GTK_BIN (item));
+ if (GTK_IS_LABEL (child)) {
+ const gchar *cur_name = gtk_label_get_text (GTK_LABEL (child));
+ if (is_default) {
+ gchar *bold_name = g_markup_printf_escaped("<b>%s</b>", cur_name);
+ gtk_label_set_markup (GTK_LABEL (child), bold_name);
+ g_free (bold_name);
+ }
+ gtk_label_set_ellipsize (GTK_LABEL (child), PANGO_ELLIPSIZE_END);
+ }
+ }
+
+ /* Get the item of the tools menu */
+ path = g_strconcat("/MenuBar/ToolsMenu/ToolsSendReceiveMainMenu/ToolsMenuAdditions/", item_name, NULL);
+ item = gtk_ui_manager_get_widget (parent_priv->ui_manager, path);
+ g_free (path);
+
+ if (item) {
+ GtkWidget *child = gtk_bin_get_child (GTK_BIN (item));
+ if (GTK_IS_LABEL (child)) {
+ const gchar *cur_name = gtk_label_get_text (GTK_LABEL (child));
+ if (is_default) {
+ gchar *bold_name = g_markup_printf_escaped("<b>%s</b>", cur_name);
+ gtk_label_set_markup (GTK_LABEL (child), bold_name);
+ g_free (bold_name);
+ }
+ gtk_label_set_ellipsize (GTK_LABEL (child), PANGO_ELLIPSIZE_END);
+ }
+ }
+
+ g_free(item_name);
+ g_object_unref (settings);
+ }
+
+ if (num_accounts > 1) {
+ /* Disconnect the tap-and-hold-query if it's connected */
+ if (modest_signal_mgr_is_connected (priv->sighandlers,
+ G_OBJECT (send_receive_button),
+ "tap-and-hold-query"))
+ priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
+ G_OBJECT (send_receive_button),
+ "tap-and-hold-query");
+
+ /* Mandatory in order to view the menu contents */
+ gtk_widget_show_all (priv->accounts_popup);
+
+ /* Setup tap_and_hold just if was not done before*/
+ if (!gtk_menu_get_attach_widget (GTK_MENU (priv->accounts_popup)))
+ gtk_widget_tap_and_hold_setup (send_receive_button, priv->accounts_popup, NULL, 0);
+ } else {
+ /* Connect the tap-and-hold-query in order not to show the CSM */
+ if (!modest_signal_mgr_is_connected (priv->sighandlers,
+ G_OBJECT (send_receive_button),
+ "tap-and-hold-query"))
+ priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (send_receive_button),
+ "tap-and-hold-query",
+ G_CALLBACK (tap_and_hold_query_cb),
+ NULL);
+ }
+
+ /* Frees */
+ g_slist_free (accounts);
+ g_free (default_account);
+
+
+ /* Make sure that at least one account is viewed if there are any
+ * accounts, for instance when adding the first account: */
+ set_at_least_one_account_visible (self);
+}
+
+static void
+wrap_in_scrolled_window (GtkWidget *win, GtkWidget *widget)
+{
+ if (!gtk_widget_set_scroll_adjustments (widget, NULL, NULL))
+ gtk_scrolled_window_add_with_viewport
+ (GTK_SCROLLED_WINDOW(win), widget);
+ else
+ gtk_container_add (GTK_CONTAINER(win),
+ widget);
+}
+
+
+typedef struct {
+ TnySendQueue *queue;
+ guint signal;
+} QueueErrorSignal;
+
+static void
+modest_main_window_cleanup_queue_error_signals (ModestMainWindow *self)
+{
+ ModestMainWindowPrivate *priv = MODEST_MAIN_WINDOW_GET_PRIVATE (self);
+
+ GList *oerrsignals = priv->queue_err_signals;
+ while (oerrsignals) {
+ QueueErrorSignal *esignal = (QueueErrorSignal *) oerrsignals->data;
+ g_signal_handler_disconnect (esignal->queue, esignal->signal);
+ g_slice_free (QueueErrorSignal, esignal);
+ oerrsignals = g_list_next (oerrsignals);
+ }
+ g_list_free (priv->queue_err_signals);
+ priv->queue_err_signals = NULL;
+}
+
+
+static void
+_folder_view_csm_menu_activated (GtkWidget *widget, gpointer user_data)
+{
+ g_return_if_fail (MODEST_IS_MAIN_WINDOW (user_data));
+
+ /* Update dimmed */
+ modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW(user_data));
+}
+
+static void
+_header_view_csm_menu_activated (GtkWidget *widget, gpointer user_data)
+{
+ g_return_if_fail (MODEST_IS_MAIN_WINDOW (user_data));
+
+ /* Update visibility */
+
+ /* Update dimmed */
+ modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW(user_data));
+}
+
+static void
+modest_main_window_disconnect_signals (ModestWindow *self)
+{
+ ModestMainWindowPrivate *priv;
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+ modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
+ priv->sighandlers = NULL;
+}
+
+static void
+connect_signals (ModestMainWindow *self)
+{
+ ModestWindowPrivate *parent_priv;
+ ModestMainWindowPrivate *priv;
+ GtkWidget *menu;
+
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+ parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
+
+ /* folder view */
+
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT(priv->folder_view), "key-press-event",
+ G_CALLBACK(on_inner_widgets_key_pressed), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers, G_OBJECT(priv->folder_view),
+ "folder_selection_changed",
+ G_CALLBACK (on_folder_selection_changed),
+ self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->folder_view),
+ "folder-display-name-changed",
+ G_CALLBACK (modest_ui_actions_on_folder_display_name_changed),
+ self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT (priv->folder_view),
+ "focus-in-event",
+ G_CALLBACK (on_folder_view_focus_in),
+ self);
+
+ /* Folder view CSM */
+ menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewCSM");
+ gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->folder_view), menu, NULL, 0);
+ priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, G_OBJECT(priv->folder_view), "tap-and-hold",
+ G_CALLBACK(_folder_view_csm_menu_activated),
+ self);
+ /* header view */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "header_selected",
+ G_CALLBACK(modest_ui_actions_on_header_selected), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "header_activated",
+ G_CALLBACK(modest_ui_actions_on_header_activated), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "item_not_found",
+ G_CALLBACK(modest_ui_actions_on_item_not_found), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "key-press-event",
+ G_CALLBACK(on_inner_widgets_key_pressed), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "msg_count_changed",
+ G_CALLBACK(on_msg_count_changed), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT (priv->header_view), "focus-in-event",
+ G_CALLBACK (on_header_view_focus_in), self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (priv->header_view),
+ "updating-msg-list",
+ G_CALLBACK (on_updating_msg_list),
+ self);
+
+ /* Header view CSM */
+ menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/HeaderViewCSM");
+ gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->header_view), menu, NULL, 0);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), "tap-and-hold",
+ G_CALLBACK(_header_view_csm_menu_activated),
+ self);
+
+ /* window */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,G_OBJECT (self), "window-state-event",
+ G_CALLBACK (modest_main_window_window_state_event),
+ NULL);
+ /* we don't register this in sighandlers, as it should be run after disconnecting all signals,
+ * in destroy stage */
+ g_signal_connect (G_OBJECT (self), "destroy", G_CALLBACK (on_window_destroy), NULL);
+
+ g_signal_connect (G_OBJECT (self), "notify::visible", G_CALLBACK (on_window_hide), NULL);
+
+ /* Mail Operation Queue */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (modest_runtime_get_mail_operation_queue ()),
+ "queue-changed",
+ G_CALLBACK (on_queue_changed), self);
+
+ /* Track changes in the device name */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT(modest_runtime_get_conf ()),
+ "key_changed",
+ G_CALLBACK (on_configuration_key_changed),
+ self);
+
+ /* Track account changes. We need to refresh the toolbar */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (modest_runtime_get_account_store ()),
+ "account_inserted",
+ G_CALLBACK (on_account_inserted),
+ self);
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (modest_runtime_get_account_store ()),
+ "account_removed",
+ G_CALLBACK (on_account_removed),
+ self);
+
+ /* We need to refresh the send & receive menu to change the bold
+ * account when the default account changes. */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (modest_runtime_get_account_mgr ()),
+ "default_account_changed",
+ G_CALLBACK (on_default_account_changed),
+ self);
+
+ /* Account store */
+ priv->sighandlers =
+ modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (modest_runtime_get_account_store ()),
+ "account_changed",
+ G_CALLBACK (on_account_changed),
+ self);
+}
+
+static void
+on_hildon_program_is_topmost_notify(GObject *self,
+ GParamSpec *propert_param,
+ gpointer user_data)
+{
+ HildonProgram *app = HILDON_PROGRAM (self);
+
+ /* Note that use of hildon_program_set_can_hibernate()
+ * is generally referred to as "setting the killable flag",
+ * though hibernation does not seem equal to death.
+ * murrayc */
+
+ if (hildon_program_get_is_topmost (app)) {
+ /* Prevent hibernation when the progam comes to the foreground,
+ * because hibernation should only happen when the application
+ * is in the background: */
+ hildon_program_set_can_hibernate (app, FALSE);
+
+ /* Remove new mail visual notifications */
+ modest_platform_remove_new_mail_notifications (TRUE);
+ } else {
+ /* Allow hibernation if the program has gone to the background: */
+
+ /* However, prevent hibernation while the settings are being changed: */
+ const gboolean hibernation_prevented =
+ modest_window_mgr_get_hibernation_is_prevented (
+ modest_runtime_get_window_mgr ());
+
+ if (hibernation_prevented)
+ hildon_program_set_can_hibernate (app, FALSE);
+ else {
+ /* Allow hibernation, after saving the state: */
+ modest_osso_save_state();
+ hildon_program_set_can_hibernate (app, TRUE);
+ }
+ }
+}
+
+typedef struct
+{
+ GtkWidget *folder_win;
+ gulong handler_id;
+} ShowHelper;
+
+static void
+modest_main_window_on_show (GtkWidget *self, gpointer user_data)
+{
+ ShowHelper *helper = (ShowHelper *) user_data;
+ GtkWidget *folder_win = helper->folder_win;
+ ModestMainWindowPrivate *priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+ priv->folder_view = MODEST_FOLDER_VIEW (modest_platform_create_folder_view (NULL));
+ wrap_in_scrolled_window (folder_win, GTK_WIDGET(priv->folder_view));
+
+ gtk_widget_show (GTK_WIDGET (priv->folder_view));
+
+ /* Connect signals */
+ connect_signals (MODEST_MAIN_WINDOW (self));
+
+ /* Set account store */
+ tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
+ TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
+
+ /* Load previous osso state, for instance if we are being restored from
+ * hibernation: */
+ modest_osso_load_state ();
+
+ /* Restore window & widget settings */
+ priv->wait_for_settings = TRUE;
+ restore_settings (MODEST_MAIN_WINDOW(self), TRUE);
+ priv->wait_for_settings = FALSE;
+
+ /* Check if accounts exist and show the account wizard if not */
+ gboolean accounts_exist =
+ modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
+
+ if (!accounts_exist) {
+ /* This is necessary to have the main window shown behind the dialog
+ It's an ugly hack... jschmid */
+ gtk_widget_show_all(GTK_WIDGET(self));
+ modest_ui_actions_on_accounts (NULL, MODEST_WINDOW(self));
+ } else {
+ GSList *accounts;
+ GtkAction *send_receive_all;
+ ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
+ accounts = modest_account_mgr_account_names (modest_runtime_get_account_mgr (), TRUE);
+ send_receive_all = gtk_ui_manager_get_action (parent_priv->ui_manager,
+ "/MenuBar/ToolsMenu/ToolsSendReceiveMainMenu/ToolsSendReceiveAllMenu");
+ gtk_action_set_visible (send_receive_all, g_slist_length (accounts) > 0);
+ modest_account_mgr_free_account_names (accounts);
+ update_menus (MODEST_MAIN_WINDOW (self));
+ }
+
+ /* Never call this function again (NOTE that it could happen
+ as we hide the main window instead of closing it while
+ there are operations ongoing) and free the helper */
+ g_signal_handler_disconnect (self, helper->handler_id);
+ g_slice_free (ShowHelper, helper);
+}
+
+static void
+osso_display_event_cb (osso_display_state_t state,
+ gpointer data)
+{
+ ModestMainWindowPrivate *priv = MODEST_MAIN_WINDOW_GET_PRIVATE (data);
+
+ priv->display_state = state;
+
+ /* Stop blinking if the screen becomes on */
+ if (priv->display_state == OSSO_DISPLAY_ON)
+ modest_platform_remove_new_mail_notifications (TRUE);
+}
+
+ModestWindow *
+modest_main_window_new (void)
+{
+ ModestMainWindow *self = NULL;
+ ModestMainWindowPrivate *priv = NULL;
+ ModestWindowPrivate *parent_priv = NULL;
+ GtkWidget *folder_win = NULL;
+ ModestDimmingRulesGroup *menu_rules_group = NULL;
+ ModestDimmingRulesGroup *toolbar_rules_group = NULL;
+ GtkActionGroup *action_group = NULL;
+ GError *error = NULL;
+ HildonProgram *app;
+ ModestConf *conf = NULL;
+ GtkAction *action = NULL;
+ GdkPixbuf *window_icon;
+ ShowHelper *helper;
+
+ self = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+ parent_priv = MODEST_WINDOW_GET_PRIVATE(self);