X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fmodest-ui-actions.c;h=4a7aeefda2a64f14b71d7fbda7c302e0ce0a087a;hp=5ba54d3b61adeeb55b2a2b4ccffeef8361c3ef7e;hb=b267362311d6c82f71987833ad14ddfaedc21390;hpb=d903cdb18704d7c5c20e583880e05c8d2aee7d36 diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index 5ba54d3..4a7aeef 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -63,10 +63,12 @@ #include #include -typedef struct _GetMsgAsyncHelper { +typedef struct _GetMsgAsyncHelper { ModestWindow *window; + ModestMailOperation *mail_op; TnyIterator *iter; - GFunc func; + guint num_ops; + GFunc func; gpointer user_data; } GetMsgAsyncHelper; @@ -82,16 +84,26 @@ guint reply_forward_type; gchar *account_name; } ReplyForwardHelper; +/* + * The do_headers_action uses this kind of functions to perform some + * action to each member of a list of headers + */ +typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data); + +static void +do_headers_action (ModestWindow *win, + HeadersFunc func, + gpointer user_data); + static void reply_forward_func (gpointer data, gpointer user_data); -static void read_msg_func (gpointer data, gpointer user_data); -static void get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, + +static void get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data); static void reply_forward (ReplyForwardAction action, ModestWindow *win); static gchar* ask_for_folder_name (GtkWindow *parent_window, const gchar *title); - void modest_ui_actions_on_about (GtkAction *action, ModestWindow *win) { @@ -148,12 +160,26 @@ get_selected_headers (ModestWindow *win) return NULL; } +static void +headers_action_delete (TnyHeader *header, + ModestWindow *win, + gpointer user_data) +{ + ModestMailOperation *mail_op; + + /* TODO: add confirmation dialog */ + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE, G_OBJECT(win)); + modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), + mail_op); + + /* Always delete. TODO: Move to trash still not supported */ + modest_mail_operation_remove_msg (mail_op, header, FALSE); + g_object_unref (G_OBJECT (mail_op)); +} + void modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win) { - TnyList *header_list; - TnyIterator *iter; - g_return_if_fail (MODEST_IS_WINDOW(win)); if (MODEST_IS_MSG_EDIT_WINDOW (win)) { @@ -162,36 +188,8 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win) return; } - header_list = get_selected_headers (win); - - if (header_list) { - iter = tny_list_create_iterator (header_list); - do { - TnyHeader *header; - ModestMailOperation *mail_op; - - header = TNY_HEADER (tny_iterator_get_current (iter)); - /* TODO: thick grain mail operation involving - a list of objects. Composite pattern ??? */ - /* TODO: add confirmation dialog */ - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE); - modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), - mail_op); - - /* Always delete. TODO: Move to trash still not supported */ - modest_mail_operation_remove_msg (mail_op, header, FALSE); - - /* Frees */ - g_object_unref (G_OBJECT (mail_op)); - g_object_unref (G_OBJECT (header)); - - tny_iterator_next (iter); - - } while (!tny_iterator_is_done (iter)); - - /* Free iter */ - g_object_unref (G_OBJECT (iter)); - } + /* Remove each header */ + do_headers_action (win, headers_action_delete, NULL); if (MODEST_IS_MSG_VIEW_WINDOW (win)) { gtk_widget_destroy (GTK_WIDGET(win)); @@ -371,13 +369,28 @@ cleanup: g_object_unref (G_OBJECT(folder)); } +static void +headers_action_open (TnyHeader *header, + ModestWindow *win, + gpointer user_data) +{ + modest_ui_actions_on_header_activated (MODEST_HEADER_VIEW (user_data), + header, + MODEST_MAIN_WINDOW (win)); +} void modest_ui_actions_on_open (GtkAction *action, ModestWindow *win) { - modest_runtime_not_implemented (GTK_WINDOW(win)); /* FIXME */ -} + GtkWidget *header_view; + + /* Get header view */ + header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win), + MODEST_WIDGET_TYPE_HEADER_VIEW); + /* Open each message */ + do_headers_action (win, headers_action_open, header_view); +} static void @@ -477,8 +490,8 @@ cleanup: if (account) g_object_unref (G_OBJECT (account)); - g_free (rf_helper->account_name); - g_slice_free (ReplyForwardHelper, rf_helper); +/* g_free (rf_helper->account_name); */ +/* g_slice_free (ReplyForwardHelper, rf_helper); */ } /* * Common code for the reply and forward actions @@ -489,7 +502,6 @@ reply_forward (ReplyForwardAction action, ModestWindow *win) TnyList *header_list; guint reply_forward_type; TnyHeader *header; - TnyFolder *folder; GetMsgAsyncHelper *helper; ReplyForwardHelper *rf_helper; @@ -522,6 +534,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win) helper->func = reply_forward_func; helper->iter = tny_list_create_iterator (header_list); helper->user_data = rf_helper; + helper->num_ops = tny_list_get_length (header_list); if (MODEST_IS_MSG_VIEW_WINDOW(win)) { TnyMsg *msg; @@ -533,14 +546,11 @@ reply_forward (ReplyForwardAction action, ModestWindow *win) reply_forward_func (msg, helper); } else { header = TNY_HEADER (tny_iterator_get_current (helper->iter)); - folder = tny_header_get_folder (header); - if (folder) { - /* The callback will call it per each header */ - tny_folder_get_msg_async (folder, header, get_msg_cb, NULL, helper); - g_object_unref (G_OBJECT (folder)); - } else - g_printerr ("modest: no folder for header\n"); + helper->mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win)); + modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), helper->mail_op); + modest_mail_operation_process_msg (helper->mail_op, header, helper->num_ops, get_msg_cb, helper); + /* Clean */ g_object_unref (G_OBJECT (header)); } @@ -657,7 +667,7 @@ action_send (const gchar* account_name) return FALSE; } - //modest_tny_send_queue_flush (send_queue); + modest_tny_send_queue_flush (send_queue); g_object_unref (G_OBJECT(send_queue)); g_object_unref (G_OBJECT(tny_account)); @@ -667,7 +677,8 @@ action_send (const gchar* account_name) static gboolean -action_receive (const gchar* account_name) +action_receive (const gchar* account_name, + ModestWindow *win) { TnyAccount *tny_account; ModestMailOperation *mail_op; @@ -685,7 +696,7 @@ action_receive (const gchar* account_name) /* Create the mail operation */ /* TODO: The spec wants us to first do any pending deletions, before receiving. */ - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); modest_mail_operation_update_account (mail_op, TNY_STORE_ACCOUNT(tny_account)); @@ -744,7 +755,7 @@ do_send_receive_current_or_default (ModestWindow *win) * for SMTP we should send, * first receiving, then sending: */ - if (!action_receive(account_name)) + if (!action_receive(account_name, win)) g_printerr ("modest: failed to receive\n"); if (!action_send(account_name)) g_printerr ("modest: failed to send\n"); @@ -775,7 +786,7 @@ do_send_receive_auto (ModestWindow *win) * for SMTP we should send, * first receiving, then sending: */ - if (!action_receive(account_name)) + if (!action_receive(account_name, win)) g_printerr ("modest: failed to receive for account %s\n", account_name); if (!action_send(account_name)) g_printerr ("modest: failed to send for account %s\n", account_name); @@ -848,37 +859,6 @@ modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow * MODEST_CONF_HEADER_VIEW_KEY); } - - -/* - * Marks a message as read and passes it to the msg preview widget - */ -static void -read_msg_func (gpointer data, gpointer user_data) -{ - TnyMsg *msg; - TnyHeader *header; - GetMsgAsyncHelper *helper; - TnyHeaderFlags header_flags; - GtkWidget *msg_preview; - - msg = TNY_MSG (data); - helper = (GetMsgAsyncHelper *) user_data; - - msg_preview = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (helper->window), - MODEST_WIDGET_TYPE_MSG_PREVIEW); - if (!msg_preview) - return; - - header = TNY_HEADER (tny_iterator_get_current (helper->iter)); - header_flags = tny_header_get_flags (header); - tny_header_set_flags (header, header_flags | TNY_HEADER_FLAG_SEEN); - g_object_unref (G_OBJECT (header)); - - /* Set message on msg view */ - modest_msg_view_set_message (MODEST_MSG_VIEW(msg_preview), msg); -} - /* * This function is a generic handler for the tny_folder_get_msg_async * call. It expects as user_data a #GetMsgAsyncHelper. This helper @@ -895,6 +875,7 @@ get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data) helper = (GetMsgAsyncHelper *) user_data; + /* Check errors */ if ((*err && ((*err)->code == TNY_FOLDER_ERROR_GET_MSG)) || !msg) { modest_ui_actions_on_item_not_found (NULL, MODEST_ITEM_TYPE_MESSAGE, @@ -903,22 +884,33 @@ get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data) } /* Call user function */ - helper->func (msg, user_data); + if (helper->func) + helper->func (msg, user_data); /* Process next element (if exists) */ tny_iterator_next (helper->iter); if (tny_iterator_is_done (helper->iter)) { + /* Notify the queue */ + if (helper->mail_op != NULL) + modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), helper->mail_op); + + /* Free resources */ TnyList *headers; + ReplyForwardHelper *rf_helper = (ReplyForwardHelper *) helper->user_data; headers = tny_iterator_get_list (helper->iter); - /* Free resources */ g_object_unref (G_OBJECT (headers)); g_object_unref (G_OBJECT (helper->iter)); + g_object_unref (G_OBJECT (helper->mail_op)); + if (rf_helper != NULL) { + g_free (rf_helper->account_name); + g_slice_free (ReplyForwardHelper, rf_helper); + } g_slice_free (GetMsgAsyncHelper, helper); } else { TnyHeader *header; - header = TNY_HEADER (tny_iterator_get_current (helper->iter)); - tny_folder_get_msg_async (folder, header, - get_msg_cb, NULL, helper); + header = TNY_HEADER (tny_iterator_get_current (helper->iter)); + modest_mail_operation_process_msg (helper->mail_op, header, helper->num_ops, get_msg_cb, helper); + g_object_unref (G_OBJECT(header)); } } @@ -928,24 +920,11 @@ modest_ui_actions_on_header_selected (ModestHeaderView *header_view, TnyHeader *header, ModestMainWindow *main_window) { - TnyFolder *folder; - GetMsgAsyncHelper *helper; - TnyList *list; - g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window)); - /* when there's no header, clear the msgview */ - if (!header) { - GtkWidget *msg_preview; - - /* Clear msg preview if exists */ - msg_preview = modest_main_window_get_child_widget(main_window, - MODEST_WIDGET_TYPE_MSG_PREVIEW); - - if (msg_preview) - modest_msg_view_set_message (MODEST_MSG_VIEW(msg_preview), NULL); + /* If no header has been selected then exit */ + if (!header) return; - } /* Update Main window title */ if (GTK_WIDGET_HAS_FOCUS (header_view)) { @@ -955,36 +934,18 @@ modest_ui_actions_on_header_selected (ModestHeaderView *header_view, else gtk_window_set_title (GTK_WINDOW (main_window), _("mail_va_no_subject")); } - - /* Create list */ - list = tny_simple_list_new (); - tny_list_prepend (list, G_OBJECT (header)); - - /* Fill helper data */ - helper = g_slice_new0 (GetMsgAsyncHelper); - helper->window = MODEST_WINDOW (main_window); - helper->iter = tny_list_create_iterator (list); - helper->func = read_msg_func; - - folder = tny_header_get_folder (TNY_HEADER(header)); - - tny_folder_get_msg_async (TNY_FOLDER(folder), - header, get_msg_cb, - NULL, helper); - - /* Frees */ - g_object_unref (G_OBJECT (folder)); } void -modest_ui_actions_on_header_activated (ModestHeaderView *folder_view, TnyHeader *header, +modest_ui_actions_on_header_activated (ModestHeaderView *header_view, TnyHeader *header, ModestMainWindow *main_window) { ModestWindow *win = NULL; TnyFolder *folder = NULL; TnyMsg *msg = NULL; + TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN; ModestWindowMgr *mgr; GtkTreeModel *model; GtkTreeIter iter; @@ -1001,6 +962,8 @@ modest_ui_actions_on_header_activated (ModestHeaderView *folder_view, TnyHeader g_printerr ("modest: cannot get folder for header\n"); return; } + if (modest_tny_folder_is_local_folder (folder)) + folder_type = modest_tny_folder_get_local_folder_type (folder); /* FIXME: make async?; check error */ msg = tny_folder_get_msg (folder, header, NULL); @@ -1021,12 +984,19 @@ modest_ui_actions_on_header_activated (ModestHeaderView *folder_view, TnyHeader if (!account) account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr()); - sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view)); + sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view)); sel_list = gtk_tree_selection_get_selected_rows (sel, &model); if (sel_list != NULL) { gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) sel_list->data); - win = modest_msg_view_window_new_with_header_model (msg, account, model, iter); + switch (folder_type) { + case TNY_FOLDER_TYPE_DRAFTS: + win = modest_msg_edit_window_new (msg, account); + break; + default: + win = modest_msg_view_window_new_with_header_model (msg, account, model, iter); + } + g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL); g_list_free (sel_list); } else { @@ -1206,7 +1176,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi from = modest_account_mgr_get_from_string (account_mgr, account_name); /* Create the mail operation */ - mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO); + mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(edit_window)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation); modest_mail_operation_save_to_drafts (mail_operation, @@ -1272,7 +1242,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window) from = modest_account_mgr_get_from_string (account_mgr, account_name); /* Create the mail operation */ - mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND); + mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND, G_OBJECT(edit_window)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation); modest_mail_operation_send_new_mail (mail_operation, @@ -1493,7 +1463,7 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo if (result == GTK_RESPONSE_REJECT) { finished = TRUE; } else { - ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO); + ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(main_window)); TnyFolder *new_folder = NULL; modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), @@ -1547,7 +1517,7 @@ modest_ui_actions_on_rename_folder (GtkAction *action, if (folder_name != NULL && strlen (folder_name) > 0) { ModestMailOperation *mail_op; - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(main_window)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); @@ -1587,7 +1557,7 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash) g_free (message); if (response == GTK_RESPONSE_OK) { - ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE); + ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE, G_OBJECT(main_window)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); @@ -1858,17 +1828,19 @@ modest_ui_actions_on_change_fullscreen (GtkAction *action, gtk_window_present (GTK_WINDOW (window)); } -/* - * Show the header details in a ModestDetailsDialog widget +/* + * Used by modest_ui_actions_on_details to call do_headers_action */ static void -show_header_details (TnyHeader *header, - GtkWindow *window) +headers_action_show_details (TnyHeader *header, + ModestWindow *window, + gpointer user_data) + { GtkWidget *dialog; /* Create dialog */ - dialog = modest_details_dialog_new_with_header (window, header); + dialog = modest_details_dialog_new_with_header (GTK_WINDOW (window), header); /* Run dialog */ gtk_widget_show_all (dialog); @@ -1896,7 +1868,9 @@ show_folder_details (TnyFolder *folder, gtk_widget_destroy (dialog); } - +/* + * Show the header details in a ModestDetailsDialog widget + */ void modest_ui_actions_on_details (GtkAction *action, ModestWindow *win) @@ -1919,7 +1893,7 @@ modest_ui_actions_on_details (GtkAction *action, iter = tny_list_create_iterator (headers_list); header = TNY_HEADER (tny_iterator_get_current (iter)); - show_header_details (header, GTK_WINDOW (win)); + headers_action_show_details (header, win, NULL); g_object_unref (header); g_object_unref (iter); @@ -1944,23 +1918,8 @@ modest_ui_actions_on_details (GtkAction *action, } else { header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win), MODEST_WIDGET_TYPE_HEADER_VIEW); - if (!gtk_widget_is_focus (header_view)) - return; - - headers_list = get_selected_headers (win); - if (!headers_list) - return; - - iter = tny_list_create_iterator (headers_list); - while (!tny_iterator_is_done (iter)) { - - header = TNY_HEADER (tny_iterator_get_current (iter)); - show_header_details (header, GTK_WINDOW (win)); - g_object_unref (header); - - tny_iterator_next (iter); - } - g_object_unref (iter); + /* Show details of each header */ + do_headers_action (win, headers_action_show_details, header_view); } } } @@ -2210,7 +2169,7 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action, src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view)); if (TNY_IS_FOLDER (src_folder)) { - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); @@ -2240,7 +2199,7 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action, /* Transfer messages */ if (response == GTK_RESPONSE_OK) { - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); @@ -2307,7 +2266,7 @@ modest_ui_actions_on_msg_view_window_move_to (GtkAction *action, ModestMailOperation *mail_op; /* Create mail op */ - mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win)); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); @@ -2339,3 +2298,33 @@ modest_ui_actions_on_move_to (GtkAction *action, modest_ui_actions_on_msg_view_window_move_to (action, MODEST_MSG_VIEW_WINDOW (win)); } + +/* + * Calls #HeadersFunc for each header already selected in the main + * window or the message currently being shown in the msg view window + */ +static void +do_headers_action (ModestWindow *win, + HeadersFunc func, + gpointer user_data) +{ + TnyList *headers_list; + TnyIterator *iter; + + /* Get headers */ + headers_list = get_selected_headers (win); + if (!headers_list) + return; + + /* Call the function for each header */ + iter = tny_list_create_iterator (headers_list); + while (!tny_iterator_is_done (iter)) { + TnyHeader *header; + + header = TNY_HEADER (tny_iterator_get_current (iter)); + func (header, win, user_data); + g_object_unref (header); + tny_iterator_next (iter); + } + g_object_unref (iter); +}