b9f1f24de70d23ac0856c491a77164109ac2e15e
[modest] / src / modest-ui-actions.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved. 
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29  
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif /*HAVE_CONFIG_H*/
33
34 #include <glib/gi18n.h>
35 #include <glib/gprintf.h>
36 #include <string.h>
37 #include <modest-runtime.h>
38 #include <modest-tny-folder.h>
39 #include <modest-tny-msg.h>
40 #include <modest-tny-account.h>
41 #include <modest-address-book.h>
42 #include "modest-error.h"
43 #include "modest-ui-actions.h"
44 #include "modest-tny-platform-factory.h"
45 #include "modest-platform.h"
46 #include "modest-debug.h"
47 #include <tny-mime-part.h>
48 #include <tny-camel-folder.h>
49 #include <tny-camel-imap-folder.h>
50 #include <tny-camel-pop-folder.h>
51 #ifdef MODEST_TOOLKIT_HILDON2
52 #include <modest-accounts-window.h>
53 #include <hildon/hildon-pannable-area.h>
54 #include <hildon/hildon-gtk.h>
55 #include <modest-header-window.h>
56 #include <modest-folder-window.h>
57 #endif
58
59 #ifdef MODEST_PLATFORM_MAEMO
60 #include "maemo/modest-osso-state-saving.h"
61 #endif /* MODEST_PLATFORM_MAEMO */
62 #ifndef MODEST_TOOLKIT_GTK
63 #include "maemo/modest-hildon-includes.h"
64 #include "maemo/modest-connection-specific-smtp-window.h"
65 #endif /* !MODEST_TOOLKIT_GTK */
66 #include <modest-utils.h>
67
68 #include "widgets/modest-ui-constants.h"
69 #include <widgets/modest-main-window.h>
70 #include <widgets/modest-msg-view-window.h>
71 #include <widgets/modest-account-view-window.h>
72 #include <widgets/modest-details-dialog.h>
73 #include <widgets/modest-attachments-view.h>
74 #include "widgets/modest-folder-view.h"
75 #include "widgets/modest-global-settings-dialog.h"
76 #include "modest-account-mgr-helpers.h"
77 #include "modest-mail-operation.h"
78 #include "modest-text-utils.h"
79 #include <modest-widget-memory.h>
80 #include <tny-error.h>
81 #include <tny-simple-list.h>
82 #include <tny-msg-view.h>
83 #include <tny-device.h>
84 #include <tny-merge-folder.h>
85
86 #include <gtkhtml/gtkhtml.h>
87
88 #define MIN_FREE_SPACE 5 * 1024 * 1024
89 #define MODEST_MOVE_TO_DIALOG_FOLDER_VIEW "move-to-dialog-folder-view"
90
91 typedef struct _GetMsgAsyncHelper {     
92         ModestWindow *window;
93         ModestMailOperation *mail_op;
94         TnyIterator *iter;
95         guint num_ops;
96         GFunc func;     
97         gpointer user_data;
98 } GetMsgAsyncHelper;
99
100 typedef enum _ReplyForwardAction {
101         ACTION_REPLY,
102         ACTION_REPLY_TO_ALL,
103         ACTION_FORWARD
104 } ReplyForwardAction;
105
106 typedef struct _ReplyForwardHelper {
107         guint reply_forward_type;
108         ReplyForwardAction action;
109         gchar *account_name;
110         GtkWidget *parent_window;
111         TnyHeader *header;
112 } ReplyForwardHelper;
113
114 typedef struct _MoveToHelper {
115         GtkTreeRowReference *reference;
116         GtkWidget *banner;
117 } MoveToHelper;
118
119 typedef struct _PasteAsAttachmentHelper {
120         ModestMsgEditWindow *window;
121         GtkWidget *banner;
122 } PasteAsAttachmentHelper;
123
124 typedef struct {
125         TnyList *list;
126         ModestWindow *win;
127 } MoveToInfo;
128
129 /*
130  * The do_headers_action uses this kind of functions to perform some
131  * action to each member of a list of headers
132  */
133 typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data);
134
135 static void     do_headers_action     (ModestWindow *win, 
136                                        HeadersFunc func,
137                                        gpointer user_data);
138
139 static void     open_msg_cb            (ModestMailOperation *mail_op, 
140                                         TnyHeader *header, 
141                                         gboolean canceled,
142                                         TnyMsg *msg,
143                                         GError *err,
144                                         gpointer user_data);
145
146 static void     reply_forward_cb       (ModestMailOperation *mail_op, 
147                                         TnyHeader *header, 
148                                         gboolean canceled,
149                                         TnyMsg *msg,
150                                         GError *err,
151                                         gpointer user_data);
152
153 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
154
155 static void     folder_refreshed_cb    (ModestMailOperation *mail_op, 
156                                         TnyFolder *folder, 
157                                         gpointer user_data);
158
159 static void     on_send_receive_finished (ModestMailOperation  *mail_op, 
160                                           gpointer user_data);
161
162 static gint header_list_count_uncached_msgs (TnyList *header_list);
163
164 static gboolean connect_to_get_msg (ModestWindow *win,
165                                     gint num_of_uncached_msgs,
166                                     TnyAccount *account);
167
168 static gboolean remote_folder_has_leave_on_server (TnyFolderStore *folder);
169
170 static void     do_create_folder (GtkWindow *window, 
171                                   TnyFolderStore *parent_folder, 
172                                   const gchar *suggested_name);
173
174 static TnyAccount *get_account_from_folder_store (TnyFolderStore *folder_store);
175
176 static void modest_ui_actions_on_main_window_move_to (GtkAction *action,
177                                                       GtkWidget *folder_view,
178                                                       TnyFolderStore *dst_folder,
179                                                       ModestMainWindow *win);
180 static void modest_ui_actions_on_folder_window_move_to (GtkWidget *folder_view,
181                                                         TnyFolderStore *dst_folder,
182                                                         TnyList *selection,
183                                                         GtkWindow *win);
184
185 static void modest_ui_actions_on_window_move_to (GtkAction *action,
186                                                  TnyList *list_to_move,
187                                                  TnyFolderStore *dst_folder,
188                                                  ModestWindow *win);
189
190 /*
191  * This function checks whether a TnyFolderStore is a pop account
192  */
193 static gboolean
194 remote_folder_has_leave_on_server (TnyFolderStore *folder)
195 {
196         TnyAccount *account;
197         gboolean result;
198
199         g_return_val_if_fail (TNY_IS_FOLDER_STORE (folder), FALSE);
200         
201         account = get_account_from_folder_store (folder);
202         result = (modest_protocol_registry_protocol_type_has_leave_on_server (modest_runtime_get_protocol_registry (),
203                                                                               modest_tny_account_get_protocol_type (account)));
204         g_object_unref (account);
205
206         return result;
207 }
208
209 /* FIXME: this should be merged with the similar code in modest-account-view-window */
210 /* Show the account creation wizard dialog.
211  * returns: TRUE if an account was created. FALSE if the user cancelled.
212  */
213 gboolean
214 modest_ui_actions_run_account_setup_wizard (ModestWindow *win)
215 {
216         gboolean result = FALSE;
217         GtkWindow *wizard;
218         gint dialog_response;
219
220         /* there is no such wizard yet */
221         wizard = GTK_WINDOW (modest_platform_get_account_settings_wizard ());
222         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(), GTK_WINDOW (wizard), (GtkWindow *) win);
223
224         /* always present a main window in the background 
225          * we do it here, so we cannot end up with two wizards (as this
226          * function might be called in modest_window_mgr_get_main_window as well */
227         if (!win) 
228                 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
229                                                          TRUE);  /* create if not existent */
230
231         gtk_window_set_transient_for (GTK_WINDOW (wizard), GTK_WINDOW (win));
232
233         /* make sure the mainwindow is visible. We need to present the
234            wizard again to give it the focus back. show_all are needed
235            in order to get the widgets properly drawn (MainWindow main
236            paned won't be in its right position and the dialog will be
237            missplaced */
238 #ifndef MODEST_TOOLKIT_HILDON2
239         gtk_widget_show_all (GTK_WIDGET (win));
240         gtk_widget_show_all (GTK_WIDGET (wizard));
241         gtk_window_present (GTK_WINDOW (win));
242         gtk_window_present (GTK_WINDOW (wizard));
243 #endif
244
245         dialog_response = gtk_dialog_run (GTK_DIALOG (wizard));
246         gtk_widget_destroy (GTK_WIDGET (wizard));
247         if (gtk_events_pending ())
248                 gtk_main_iteration ();
249
250         if (dialog_response == GTK_RESPONSE_CANCEL) {
251                 result = FALSE;
252         } else {
253                 /* Check whether an account was created: */
254                 result = modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
255         }
256         return result;
257 }
258
259
260 void   
261 modest_ui_actions_on_about (GtkAction *action, ModestWindow *win)
262 {
263         GtkWidget *about;
264         const gchar *authors[] = {
265                 "Dirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>",
266                 NULL
267         };
268         about = gtk_about_dialog_new ();
269         gtk_about_dialog_set_name (GTK_ABOUT_DIALOG(about), PACKAGE_NAME);
270         gtk_about_dialog_set_version (GTK_ABOUT_DIALOG(about),PACKAGE_VERSION);
271         gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG(about),
272                                         _("Copyright (c) 2006, Nokia Corporation\n"
273                                           "All rights reserved."));
274         gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(about),
275                                        _("a modest e-mail client\n\n"
276                                          "design and implementation: Dirk-Jan C. Binnema\n"
277                                          "contributions from the fine people at KC and Ig\n"
278                                          "uses the tinymail email framework written by Philip van Hoof"));
279         gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(about), authors);
280         gtk_about_dialog_set_website (GTK_ABOUT_DIALOG(about), "http://modest.garage.maemo.org");
281         gtk_window_set_transient_for (GTK_WINDOW (about), GTK_WINDOW (win));
282         gtk_window_set_modal (GTK_WINDOW (about), TRUE);
283         
284         gtk_dialog_run (GTK_DIALOG (about));
285         gtk_widget_destroy(about);
286 }
287
288 /*
289  * Gets the list of currently selected messages. If the win is the
290  * main window, then it returns a newly allocated list of the headers
291  * selected in the header view. If win is the msg view window, then
292  * the value returned is a list with just a single header.
293  *
294  * The caller of this funcion must free the list.
295  */
296 static TnyList *
297 get_selected_headers (ModestWindow *win)
298 {
299         if (MODEST_IS_MAIN_WINDOW(win)) {
300                 GtkWidget *header_view;         
301                 
302                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
303                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
304                 return modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
305                 
306         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
307                 /* for MsgViewWindows, we simply return a list with one element */
308                 TnyHeader *header;
309                 TnyList *list = NULL;
310                 
311                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
312                 if (header != NULL) {
313                         list = tny_simple_list_new ();
314                         tny_list_prepend (list, G_OBJECT(header));
315                         g_object_unref (G_OBJECT(header));
316                 }
317
318                 return list;
319
320 #ifdef MODEST_TOOLKIT_HILDON2
321         } else if (MODEST_IS_HEADER_WINDOW (win)) {
322                 GtkWidget *header_view;
323
324                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
325                 return modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
326 #endif
327         } else
328                 return NULL;
329 }
330
331 static GtkTreeRowReference *
332 get_next_after_selected_headers (ModestHeaderView *header_view)
333 {
334         GtkTreeSelection *sel;
335         GList *selected_rows, *node;
336         GtkTreePath *path;
337         GtkTreeRowReference *result;
338         GtkTreeModel *model;
339
340         model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
341         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
342         selected_rows = gtk_tree_selection_get_selected_rows (sel, NULL);
343
344         if (selected_rows == NULL)
345                 return NULL;
346
347         node = g_list_last (selected_rows);
348         path = gtk_tree_path_copy ((GtkTreePath *) node->data);
349         gtk_tree_path_next (path);
350
351         result = gtk_tree_row_reference_new (model, path);
352
353         gtk_tree_path_free (path);
354         g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
355         g_list_free (selected_rows);
356
357         return result;
358 }
359
360 static void
361 headers_action_mark_as_read (TnyHeader *header,
362                              ModestWindow *win,
363                              gpointer user_data)
364 {
365         TnyHeaderFlags flags;
366
367         g_return_if_fail (TNY_IS_HEADER(header));
368
369         flags = tny_header_get_flags (header);
370         if (flags & TNY_HEADER_FLAG_SEEN) return;
371         tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
372 }
373
374 static void
375 headers_action_mark_as_unread (TnyHeader *header,
376                                ModestWindow *win,
377                                gpointer user_data)
378 {
379         TnyHeaderFlags flags;
380
381         g_return_if_fail (TNY_IS_HEADER(header));
382
383         flags = tny_header_get_flags (header);
384         if (flags & TNY_HEADER_FLAG_SEEN)  {
385                 tny_header_unset_flag (header, TNY_HEADER_FLAG_SEEN);
386         }
387 }
388
389 /** After deleing a message that is currently visible in a window, 
390  * show the next message from the list, or close the window if there are no more messages.
391  **/
392 void 
393 modest_ui_actions_refresh_message_window_after_delete (ModestMsgViewWindow* win)
394 {
395         /* Close msg view window or select next */
396         if (!modest_msg_view_window_select_next_message (win) &&
397             !modest_msg_view_window_select_previous_message (win)) {
398                 gboolean ret_value;
399                 g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);       
400         }
401 }
402
403
404 void
405 modest_ui_actions_on_delete_message (GtkAction *action, ModestWindow *win)
406 {
407         modest_ui_actions_on_edit_mode_delete_message (win);
408 }
409
410 gboolean
411 modest_ui_actions_on_edit_mode_delete_message (ModestWindow *win)
412 {
413         TnyList *header_list = NULL;
414         TnyIterator *iter = NULL;
415         TnyHeader *header = NULL;
416         gchar *message = NULL;
417         gchar *desc = NULL;
418         gint response;
419         ModestWindowMgr *mgr;
420         GtkWidget *header_view = NULL;
421         gboolean retval = TRUE;
422
423         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
424         
425         /* Check first if the header view has the focus */
426         if (MODEST_IS_MAIN_WINDOW (win)) {
427                 header_view = 
428                         modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
429                                                              MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
430                 if (!gtk_widget_is_focus (header_view))
431                         return FALSE;
432         }
433         
434         /* Get the headers, either from the header view (if win is the main window),
435          * or from the message view window: */
436         header_list = get_selected_headers (win);
437         if (!header_list) return FALSE;
438                         
439         /* Check if any of the headers are already opened, or in the process of being opened */
440         if (MODEST_IS_MAIN_WINDOW (win)) {
441                 gint opened_headers = 0;
442
443                 iter = tny_list_create_iterator (header_list);
444                 mgr = modest_runtime_get_window_mgr ();
445                 while (!tny_iterator_is_done (iter)) {
446                         header = TNY_HEADER (tny_iterator_get_current (iter));
447                         if (header) {
448                                 if (modest_window_mgr_find_registered_header (mgr, header, NULL))
449                                         opened_headers++;
450                                 g_object_unref (header);
451                         }
452                         tny_iterator_next (iter);
453                 }
454                 g_object_unref (iter);
455
456                 if (opened_headers > 0) {
457                         gchar *msg;
458
459                         msg = g_strdup_printf (_("mcen_nc_unable_to_delete_n_messages"), 
460                                                opened_headers);
461
462                         modest_platform_run_information_dialog (GTK_WINDOW (win), (const gchar *) msg, FALSE);
463                         
464                         g_free (msg);
465                         g_object_unref (header_list);
466                         return FALSE;
467                 }
468         }
469
470         /* Select message */
471         if (tny_list_get_length(header_list) == 1) {
472                 iter = tny_list_create_iterator (header_list);
473                 header = TNY_HEADER (tny_iterator_get_current (iter));
474                 if (header) {
475                         gchar *subject;
476                         subject = tny_header_dup_subject (header);
477                         if (!subject)
478                                 subject = g_strdup (_("mail_va_no_subject"));
479                         desc = g_strdup_printf ("%s", subject); 
480                         g_free (subject);
481                         g_object_unref (header);
482                 }
483
484                 g_object_unref (iter);
485         }
486         message = g_strdup_printf(ngettext("emev_nc_delete_message", "emev_nc_delete_messages", 
487                                            tny_list_get_length(header_list)), desc);
488
489         /* Confirmation dialog */
490         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
491                                                             message);
492         
493
494         if (response == GTK_RESPONSE_OK) {      
495                 ModestWindow *main_window = NULL;
496                 ModestWindowMgr *mgr = NULL;
497                 GtkTreeModel *model = NULL;
498                 GtkTreeSelection *sel = NULL;
499                 GList *sel_list = NULL, *tmp = NULL;
500                 GtkTreeRowReference *next_row_reference = NULL;
501                 GtkTreeRowReference *prev_row_reference = NULL;
502                 GtkTreePath *next_path = NULL;
503                 GtkTreePath *prev_path = NULL;
504                 ModestMailOperation *mail_op = NULL;
505
506                 /* Find last selected row */                    
507                 if (MODEST_IS_MAIN_WINDOW (win)) {
508                         model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
509                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
510                         sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
511                         for (tmp=sel_list; tmp; tmp=tmp->next) {
512                                 if (tmp->next == NULL) {
513                                         prev_path = gtk_tree_path_copy((GtkTreePath *) tmp->data);
514                                         next_path = gtk_tree_path_copy((GtkTreePath *) tmp->data);
515
516                                         gtk_tree_path_prev (prev_path);
517                                         gtk_tree_path_next (next_path);
518
519                                         prev_row_reference = gtk_tree_row_reference_new (model, prev_path);
520                                         next_row_reference = gtk_tree_row_reference_new (model, next_path);
521                                 }
522                         }
523                 }
524                 
525                 /* Disable window dimming management */
526                 modest_window_disable_dimming (MODEST_WINDOW(win));
527
528                 /* Remove each header. If it's a view window header_view == NULL */
529                 mail_op = modest_mail_operation_new ((GObject *) win);
530                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
531                                                  mail_op);
532                 modest_mail_operation_remove_msgs (mail_op, header_list, FALSE);
533                 g_object_unref (mail_op);
534                 
535                 /* Enable window dimming management */
536                 if (sel != NULL) {
537                         gtk_tree_selection_unselect_all (sel);
538                 }
539                 modest_window_enable_dimming (MODEST_WINDOW(win));
540                 
541                 if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
542                         modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (win));
543                         
544                         /* Get main window */
545                         mgr = modest_runtime_get_window_mgr ();
546                         main_window = modest_window_mgr_get_main_window (mgr, FALSE); /* don't create */
547                 } else if (MODEST_IS_MAIN_WINDOW (win)) {
548                         /* Move cursor to next row */
549                         main_window = win; 
550
551                         /* Select next or previous row */
552                         if (gtk_tree_row_reference_valid (next_row_reference)) {
553                                 gtk_tree_selection_select_path (sel, next_path);
554                         }
555                         else if (gtk_tree_row_reference_valid (prev_row_reference)) {                           
556                                 gtk_tree_selection_select_path (sel, prev_path);
557                         }
558
559                         /* Free */
560                         if (gtk_tree_row_reference_valid (next_row_reference)) 
561                                 gtk_tree_row_reference_free (next_row_reference);
562                         if (next_path != NULL) 
563                                 gtk_tree_path_free (next_path);                         
564                         if (gtk_tree_row_reference_valid (prev_row_reference)) 
565                                 gtk_tree_row_reference_free (prev_row_reference);
566                         if (prev_path != NULL) 
567                                 gtk_tree_path_free (prev_path);
568                 }
569                 
570                 /* Update toolbar dimming state */
571                 if (main_window) {
572                         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
573                         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
574                 }
575
576                 /* Free */
577                 g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
578                 g_list_free (sel_list);
579                 retval = TRUE;
580         } else {
581                 retval = FALSE;
582         }
583
584         /* Free*/
585         g_free(message);
586         g_free(desc);
587         g_object_unref (header_list);
588
589         return retval;
590 }
591
592
593
594
595 /* delete either message or folder, based on where we are */
596 void
597 modest_ui_actions_on_delete_message_or_folder (GtkAction *action, ModestWindow *win)
598 {
599         g_return_if_fail (MODEST_IS_WINDOW(win));
600         
601         /* Check first if the header view has the focus */
602         if (MODEST_IS_MAIN_WINDOW (win)) {
603                 GtkWidget *w;
604                 w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
605                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
606                 if (gtk_widget_is_focus (w)) {
607                         modest_ui_actions_on_delete_folder (action, MODEST_WINDOW(win));
608                         return;
609                 }
610         }
611         modest_ui_actions_on_delete_message (action, win);
612 }
613
614 void
615 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
616 {       
617         ModestWindowMgr *mgr = NULL;
618         
619 #ifdef MODEST_PLATFORM_MAEMO
620         modest_osso_save_state();
621 #endif /* MODEST_PLATFORM_MAEMO */
622
623         g_debug ("closing down, clearing %d item(s) from operation queue",
624                  modest_mail_operation_queue_num_elements
625                  (modest_runtime_get_mail_operation_queue()));
626
627         /* cancel all outstanding operations */
628         modest_mail_operation_queue_cancel_all 
629                 (modest_runtime_get_mail_operation_queue());
630         
631         g_debug ("queue has been cleared");
632
633
634         /* Check if there are opened editing windows */ 
635         mgr = modest_runtime_get_window_mgr ();
636         modest_window_mgr_close_all_windows (mgr);
637
638         /* note: when modest-tny-account-store is finalized,
639            it will automatically set all network connections
640            to offline */
641
642 /*      gtk_main_quit (); */
643 }
644
645 void
646 modest_ui_actions_on_close_window (GtkAction *action, ModestWindow *win)
647 {
648         gboolean ret_value;
649
650         g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
651
652 /*      if (MODEST_IS_MSG_VIEW_WINDOW (win)) { */
653 /*              gtk_widget_destroy (GTK_WIDGET (win)); */
654 /*      } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) { */
655 /*              gboolean ret_value; */
656 /*              g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value); */
657 /*      } else if (MODEST_IS_WINDOW (win)) { */
658 /*              gtk_widget_destroy (GTK_WIDGET (win)); */
659 /*      } else { */
660 /*              g_return_if_reached (); */
661 /*      } */
662 }
663
664 void
665 modest_ui_actions_add_to_contacts (GtkAction *action, ModestWindow *win)
666 {
667        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win));
668        
669        modest_msg_view_window_add_to_contacts (MODEST_MSG_VIEW_WINDOW (win));
670 }
671
672 void
673 modest_ui_actions_on_add_to_contacts (GtkAction *action, ModestWindow *win)
674 {
675         GtkClipboard *clipboard = NULL;
676         gchar *selection = NULL;
677
678         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
679         selection = gtk_clipboard_wait_for_text (clipboard);
680
681         /* Question: why is the clipboard being used here? 
682          * It doesn't really make a lot of sense. */
683
684         if (selection)
685         {
686                 modest_address_book_add_address (selection);
687                 g_free (selection);
688         }
689 }
690
691 void
692 modest_ui_actions_on_new_account (GtkAction *action,
693                                   ModestWindow *window)
694 {
695         modest_ui_actions_run_account_setup_wizard (window);
696 }
697
698 void
699 modest_ui_actions_on_accounts (GtkAction *action,
700                                ModestWindow *win)
701 {
702         /* This is currently only implemented for Maemo */
703         if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE)) {
704                 if (!modest_ui_actions_run_account_setup_wizard (win))
705                         g_debug ("%s: wizard was already running", __FUNCTION__);
706
707                 return;
708         } else {
709                 /* Show the list of accounts */
710                 GtkWindow *account_win = GTK_WINDOW (modest_account_view_window_new ());
711
712                 /* The accounts dialog must be modal */
713                 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (account_win), (GtkWindow *) win);
714                 modest_utils_show_dialog_and_forget (GTK_WINDOW (win), GTK_DIALOG (account_win)); 
715         }
716 }
717
718 void
719 modest_ui_actions_on_smtp_servers (GtkAction *action, ModestWindow *win)
720 {
721         /* This is currently only implemented for Maemo,
722          * because it requires an API (libconic) to detect different connection 
723          * possiblities.
724          */
725 #ifndef MODEST_TOOLKIT_GTK /* Defined in config.h */
726
727         /* Create the window if necessary: */
728         GtkWidget *specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
729         modest_connection_specific_smtp_window_fill_with_connections (
730                 MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), 
731                 modest_runtime_get_account_mgr());
732
733         /* Show the window: */
734         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
735                                      GTK_WINDOW (specific_window), (GtkWindow *) win);
736         gtk_widget_show (specific_window);
737 #endif /* !MODEST_TOOLKIT_GTK */
738 }
739
740 void
741 modest_ui_actions_compose_msg(ModestWindow *win,
742                               const gchar *to_str,
743                               const gchar *cc_str,
744                               const gchar *bcc_str,
745                               const gchar *subject_str,
746                               const gchar *body_str,
747                               GSList *attachments,
748                               gboolean set_as_modified)
749 {
750         gchar *account_name = NULL;
751         TnyMsg *msg = NULL;
752         TnyAccount *account = NULL;
753         TnyFolder *folder = NULL;
754         gchar *from_str = NULL, *signature = NULL, *body = NULL;
755         gboolean use_signature = FALSE;
756         ModestWindow *msg_win = NULL;
757         ModestAccountMgr *mgr = modest_runtime_get_account_mgr();
758         ModestTnyAccountStore *store = modest_runtime_get_account_store();
759         GnomeVFSFileSize total_size, allowed_size;
760
761         /* we check for low-mem */
762         if (modest_platform_check_memory_low (win, TRUE))
763                 goto cleanup;
764
765 #ifdef MODEST_TOOLKIT_HILDON2
766         account_name = g_strdup (modest_window_get_active_account(win));
767 #endif
768         if (!account_name) {
769                 account_name = modest_account_mgr_get_default_account(mgr);
770         }
771         if (!account_name) {
772                 g_printerr ("modest: no account found\n");
773                 goto cleanup;
774         }
775         account = modest_tny_account_store_get_server_account (store, account_name, TNY_ACCOUNT_TYPE_STORE);
776         if (!account) {
777                 g_printerr ("modest: failed to get tnyaccount for '%s'\n", account_name);
778                 goto cleanup;
779         }
780         folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS);
781         if (!folder) {
782                 g_printerr ("modest: failed to find Drafts folder\n");
783                 goto cleanup;
784         }
785         from_str = modest_account_mgr_get_from_string (mgr, account_name);
786         if (!from_str) {
787                 g_printerr ("modest: failed get from string for '%s'\n", account_name);
788                 goto cleanup;
789         }
790
791         signature = modest_account_mgr_get_signature (mgr, account_name, &use_signature);
792         if (body_str != NULL) {
793                 body = use_signature ? g_strconcat(body_str, "\n--\n", signature, NULL) : g_strdup(body_str);
794         } else {
795                 body = use_signature ? g_strconcat("\n--\n", signature, NULL) : g_strdup("");
796         }
797
798         msg = modest_tny_msg_new (to_str, from_str, cc_str, bcc_str, subject_str, body, NULL, NULL, NULL);
799         if (!msg) {
800                 g_printerr ("modest: failed to create new msg\n");
801                 goto cleanup;
802         }
803
804         /* Create and register edit window */
805         /* This is destroyed by TODO. */
806         total_size = 0;
807         allowed_size = MODEST_MAX_ATTACHMENT_SIZE;
808         msg_win = modest_msg_edit_window_new (msg, account_name, FALSE);
809
810         if (!modest_window_mgr_register_window (modest_runtime_get_window_mgr(), msg_win, win)) {
811                 gtk_widget_destroy (GTK_WIDGET (msg_win));
812                 goto cleanup;
813         }
814         modest_msg_edit_window_set_modified (MODEST_MSG_EDIT_WINDOW (msg_win), set_as_modified);
815         gtk_widget_show_all (GTK_WIDGET (msg_win));
816
817         while (attachments) {
818                 total_size +=
819                         modest_msg_edit_window_attach_file_one((ModestMsgEditWindow *)msg_win,
820                                                                attachments->data, allowed_size);
821
822                 if (total_size > allowed_size) {
823                         g_warning ("%s: total size: %u",
824                                    __FUNCTION__, (unsigned int)total_size);
825                         break;
826                 }
827                 allowed_size -= total_size;
828
829                 attachments = g_slist_next(attachments);
830         }
831
832 cleanup:
833         g_free (from_str);
834         g_free (signature);
835         g_free (body);
836         g_free (account_name);
837         if (account) 
838                 g_object_unref (G_OBJECT(account));
839         if (folder)
840                 g_object_unref (G_OBJECT(folder));
841         if (msg)
842                 g_object_unref (G_OBJECT(msg));
843 }
844
845 void
846 modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
847 {
848         /* if there are no accounts yet, just show the wizard */
849         if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE))
850                 if (!modest_ui_actions_run_account_setup_wizard (win))
851                         return;
852                 
853         modest_ui_actions_compose_msg(win, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
854 }
855
856
857 gboolean 
858 modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
859                                        TnyHeader *header,
860                                        TnyMsg *msg)
861 {
862         ModestMailOperationStatus status;
863
864         /* If there is no message or the operation was not successful */
865         status = modest_mail_operation_get_status (mail_op);
866         if (!msg || status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
867                 const GError *error;
868
869                 /* If it's a memory low issue, then show a banner */
870                 error = modest_mail_operation_get_error (mail_op);
871                 if (error && error->domain == MODEST_MAIL_OPERATION_ERROR &&
872                     error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) {
873                         GObject *source = modest_mail_operation_get_source (mail_op);
874                         modest_platform_run_information_dialog (GTK_IS_WINDOW (source) ? GTK_WINDOW (source) : NULL,
875                                                                 dgettext("ke-recv","memr_ib_operation_disabled"),
876                                                                 TRUE);
877                         g_object_unref (source);
878                 }
879
880                 if (error && ((error->code == TNY_SERVICE_ERROR_NO_SUCH_MESSAGE) ||
881                               error->code == TNY_SERVICE_ERROR_MESSAGE_NOT_AVAILABLE)) {
882                         gchar *subject, *msg;
883                         subject = tny_header_dup_subject (header);
884                         if (!subject)
885                                 subject = g_strdup (_("mail_va_no_subject"));;
886                         msg = g_strdup_printf (_("emev_ni_ui_imap_message_not_available_in_server"),
887                                                subject);
888                         modest_platform_run_information_dialog (NULL, msg, FALSE);
889                         g_free (msg);
890                         g_free (subject);
891                 }
892
893                 /* Remove the header from the preregistered uids */
894                 modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),  
895                                                      header);
896
897                 return FALSE;
898         }
899
900         return TRUE;
901 }
902
903 typedef struct {
904         guint idle_handler;
905         gchar *message;
906         GtkWidget *banner;
907 } OpenMsgBannerInfo;
908
909 typedef struct {
910         GtkTreeModel *model;
911         TnyHeader *header;
912         OpenMsgBannerInfo *banner_info;
913         GtkTreeRowReference *rowref;
914 } OpenMsgHelper;
915
916 gboolean
917 open_msg_banner_idle (gpointer userdata)
918 {
919         OpenMsgBannerInfo *banner_info = (OpenMsgBannerInfo *) userdata;
920
921         gdk_threads_enter ();
922         banner_info->idle_handler = 0;
923         banner_info->banner = modest_platform_animation_banner (NULL, NULL, banner_info->message);
924         if (banner_info)
925                 g_object_ref (banner_info->banner);
926         
927         gdk_threads_leave ();
928
929         return FALSE;
930         
931 }
932
933 static GtkWidget *
934 get_header_view_from_window (ModestWindow *window)
935 {
936         GtkWidget *header_view;
937
938         if (MODEST_IS_MAIN_WINDOW (window)) {
939                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
940                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
941 #ifdef MODEST_TOOLKIT_HILDON2
942         } else if (MODEST_IS_HEADER_WINDOW (window)){
943                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (window)));
944 #endif
945         } else {
946                 header_view = NULL;
947         }
948
949         return header_view;
950 }
951
952 static gchar *
953 get_info_from_header (TnyHeader *header, gboolean *is_draft)
954 {
955         TnyFolder *folder;
956         gchar *account = NULL;
957         TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN;
958
959         *is_draft = FALSE;
960
961         folder = tny_header_get_folder (header);
962         /* Gets folder type (OUTBOX headers will be opened in edit window */
963         if (modest_tny_folder_is_local_folder (folder)) {
964                 folder_type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
965                 if (folder_type == TNY_FOLDER_TYPE_INVALID)
966                         g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
967         }
968                 
969         if (folder_type == TNY_FOLDER_TYPE_OUTBOX) {
970                 TnyTransportAccount *traccount = NULL;
971                 ModestTnyAccountStore *accstore = modest_runtime_get_account_store();
972                 traccount = modest_tny_account_store_get_transport_account_from_outbox_header(accstore, header);
973                 if (traccount) {
974                         ModestTnySendQueue *send_queue = NULL;
975                         ModestTnySendQueueStatus status;
976                         gchar *msg_id;
977                         account = g_strdup(modest_tny_account_get_parent_modest_account_name_for_server_account(
978                                                    TNY_ACCOUNT(traccount)));
979                         send_queue = modest_runtime_get_send_queue(traccount, TRUE);
980                         if (TNY_IS_SEND_QUEUE (send_queue)) {
981                                 msg_id = modest_tny_send_queue_get_msg_id (header);
982                                 status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
983                                 g_free (msg_id);
984                                 /* Only open messages in outbox with the editor if they are in Failed state */
985                                 if (status == MODEST_TNY_SEND_QUEUE_FAILED) {
986                                         *is_draft = TRUE;
987                                 }
988 #ifdef MODEST_TOOLKIT_HILDON2
989                                 else {
990                                         /* In Fremantle we can not
991                                            open any message from
992                                            outbox which is not in
993                                            failed state */
994                                         g_object_unref(traccount);
995                                 }
996 #endif
997                         }
998                         g_object_unref(traccount);
999                 } else {
1000                         g_warning("Cannot get transport account for message in outbox!!");
1001                 }
1002         } else if (folder_type == TNY_FOLDER_TYPE_DRAFTS) {
1003                 *is_draft = TRUE; /* Open in editor if the message is in the Drafts folder */
1004         }
1005
1006         g_object_unref (folder);
1007
1008         return account;
1009 }
1010
1011 static void
1012 open_msg_cb (ModestMailOperation *mail_op, 
1013              TnyHeader *header,  
1014              gboolean canceled,
1015              TnyMsg *msg, 
1016              GError *err,
1017              gpointer user_data)
1018 {
1019         ModestWindowMgr *mgr = NULL;
1020         ModestWindow *parent_win = NULL;
1021         ModestWindow *win = NULL;
1022         gchar *account = NULL;
1023         gboolean open_in_editor = FALSE;
1024         OpenMsgHelper *helper = (OpenMsgHelper *) user_data;
1025         
1026         /* Do nothing if there was any problem with the mail
1027            operation. The error will be shown by the error_handler of
1028            the mail operation */
1029         if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
1030                 return;
1031
1032         parent_win = (ModestWindow *) modest_mail_operation_get_source (mail_op);
1033
1034         /* Mark header as read */
1035         headers_action_mark_as_read (header, MODEST_WINDOW(parent_win), NULL);
1036
1037         account = get_info_from_header (header, &open_in_editor);
1038
1039         /* Get account */
1040         if (!account)
1041                 account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (parent_win)));
1042         if (!account)
1043                 account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
1044         
1045         if (open_in_editor) {
1046                 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
1047                 gchar *from_header = NULL, *acc_name;
1048
1049                 from_header = tny_header_dup_from (header);
1050
1051                 /* we cannot edit without a valid account... */
1052                 if (!modest_account_mgr_has_accounts(mgr, TRUE)) {
1053                         if (!modest_ui_actions_run_account_setup_wizard(parent_win)) {
1054                                 modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),
1055                                                                      header);
1056                                 g_free (from_header);
1057                                 goto cleanup;
1058                         }
1059                 }
1060
1061                 acc_name = modest_utils_get_account_name_from_recipient (from_header);
1062                 g_free (from_header);
1063                 if (acc_name) {
1064                         g_free (account);
1065                         account = acc_name;
1066                 }
1067
1068                 win = modest_msg_edit_window_new (msg, account, TRUE);
1069         } else {
1070                 gchar *uid = modest_tny_folder_get_header_unique_id (header);
1071
1072                 if (helper->rowref && helper->model) {          
1073                         win = modest_msg_view_window_new_with_header_model (msg, account, (const gchar*) uid,
1074                                                                             helper->model, helper->rowref);
1075                 } else {
1076                         win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
1077                 }
1078                 g_free (uid);
1079         }
1080         
1081         /* Register and show new window */
1082         if (win != NULL) {
1083                 mgr = modest_runtime_get_window_mgr ();
1084                 if (!modest_window_mgr_register_window (mgr, win, NULL)) {
1085                         gtk_widget_destroy (GTK_WIDGET (win));
1086                         goto cleanup;
1087                 }
1088                 gtk_widget_show_all (GTK_WIDGET(win));
1089         }
1090
1091         /* Update toolbar dimming state */
1092         if (MODEST_IS_MAIN_WINDOW (parent_win)) {
1093                 modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (parent_win));
1094         }
1095
1096 cleanup:
1097         /* Free */
1098         g_free(account);
1099         g_object_unref (parent_win);
1100 }
1101
1102 static gboolean
1103 is_memory_full_error (GError *error)
1104 {
1105         gboolean enough_free_space = TRUE;
1106         GnomeVFSURI *cache_dir_uri;
1107         const gchar *cache_dir;
1108         GnomeVFSFileSize free_space;
1109
1110         cache_dir = tny_account_store_get_cache_dir (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
1111         cache_dir_uri = gnome_vfs_uri_new (cache_dir);
1112         if (gnome_vfs_get_volume_free_space (cache_dir_uri, &free_space) == GNOME_VFS_OK) {
1113                 if (free_space < MIN_FREE_SPACE)
1114                         enough_free_space = FALSE;
1115         }
1116         gnome_vfs_uri_unref (cache_dir_uri);
1117
1118         if ((error->code == TNY_SYSTEM_ERROR_MEMORY ||
1119              /* When asking for a mail and no space left on device
1120                 tinymail returns this error */
1121              error->code == TNY_SERVICE_ERROR_MESSAGE_NOT_AVAILABLE ||
1122              /* When the folder summary could not be read or
1123                 written */
1124              error->code == TNY_IO_ERROR_WRITE ||
1125              error->code == TNY_IO_ERROR_READ) && 
1126             !enough_free_space) {
1127                 return TRUE;
1128         } else {
1129                 return FALSE;
1130         }
1131 }
1132
1133 static gboolean
1134 check_memory_full_error (GtkWidget *parent_window, GError *err)
1135 {
1136         if (err == NULL)
1137                 return FALSE;
1138
1139         if (is_memory_full_error (err))
1140                 modest_platform_information_banner (parent_window,
1141                                                     NULL, dgettext("ke-recv",
1142                                                                    "cerm_device_memory_full"));
1143         else if (err->code == TNY_SYSTEM_ERROR_MEMORY)
1144                 /* If the account was created in memory full
1145                    conditions then tinymail won't be able to
1146                    connect so it'll return this error code */                           
1147                 modest_platform_information_banner (parent_window,
1148                                                     NULL, _("emev_ui_imap_inbox_select_error"));
1149         else
1150                 return FALSE;
1151
1152         return TRUE;
1153 }
1154
1155 void
1156 modest_ui_actions_disk_operations_error_handler (ModestMailOperation *mail_op,
1157                                                  gpointer user_data)
1158 {
1159         const GError *error;
1160         GObject *win = NULL;
1161         ModestMailOperationStatus status;
1162
1163         win = modest_mail_operation_get_source (mail_op);
1164         error = modest_mail_operation_get_error (mail_op);
1165         status = modest_mail_operation_get_status (mail_op);
1166
1167         /* If the mail op has been cancelled then it's not an error:
1168            don't show any message */
1169         if (status != MODEST_MAIL_OPERATION_STATUS_CANCELED) {
1170                 if (is_memory_full_error ((GError *) error)) {
1171                         modest_platform_information_banner ((GtkWidget *) win,
1172                                                             NULL, dgettext("ke-recv",
1173                                                                            "cerm_device_memory_full"));
1174                 } else if (error->code == TNY_SYSTEM_ERROR_MEMORY) {
1175                         modest_platform_information_banner ((GtkWidget *) win,
1176                                                             NULL, _("emev_ui_imap_inbox_select_error"));
1177                 } else if (error->domain == MODEST_MAIL_OPERATION_ERROR &&
1178                            error->code == MODEST_MAIL_OPERATION_ERROR_FILE_IO) {
1179                         modest_platform_information_banner ((GtkWidget *) win,
1180                                                             NULL, dgettext ("hildon-common-strings", "sfil_ni_unable_to_open_file_not_found"));
1181                 } else if (user_data) {
1182                         modest_platform_information_banner ((GtkWidget *) win, 
1183                                                             NULL, user_data);
1184                 }
1185         }
1186
1187         if (win)
1188                 g_object_unref (win);
1189 }
1190
1191 /**
1192  * Returns the account a list of headers belongs to. It returns a
1193  * *new* reference so don't forget to unref it
1194  */
1195 static TnyAccount*
1196 get_account_from_header_list (TnyList *headers)
1197 {
1198         TnyAccount *account = NULL;
1199
1200         if (tny_list_get_length (headers) > 0) {
1201                 TnyIterator *iter = tny_list_create_iterator (headers);
1202                 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
1203                 TnyFolder *folder = tny_header_get_folder (header);
1204                 
1205                 if (!folder) {
1206                         g_object_unref (header);
1207                         
1208                         while (!tny_iterator_is_done (iter)) {
1209                                 header = TNY_HEADER (tny_iterator_get_current (iter));
1210                                 folder = tny_header_get_folder (header);
1211                                 if (folder) 
1212                                         break;
1213                                 g_object_unref (header);
1214                                 header = NULL;
1215                                 tny_iterator_next (iter);
1216                         }
1217                 }
1218
1219                 if (folder) {
1220                         account = tny_folder_get_account (folder);
1221                         g_object_unref (folder);
1222                 }
1223                 
1224                 if (header)
1225                         g_object_unref (header);
1226                 
1227                 g_object_unref (iter);
1228         }
1229         return account;
1230 }
1231
1232 static TnyAccount*
1233 get_account_from_header (TnyHeader *header)
1234 {
1235         TnyAccount *account = NULL;
1236         TnyFolder *folder;
1237
1238         folder = tny_header_get_folder (header);
1239                 
1240         if (folder) {
1241                 account = tny_folder_get_account (folder);
1242                 g_object_unref (folder);
1243         }
1244                 
1245         return account;
1246 }
1247
1248 static void
1249 open_msg_helper_destroyer (gpointer user_data)
1250 {
1251         OpenMsgHelper *helper = (OpenMsgHelper *) user_data;
1252
1253         if (helper->banner_info) {
1254                 g_free (helper->banner_info->message);
1255                 if (helper->banner_info->idle_handler > 0) {
1256                         g_source_remove (helper->banner_info->idle_handler);
1257                         helper->banner_info->idle_handler = 0;
1258                 }
1259                 if (helper->banner_info->banner != NULL) {
1260                         gtk_widget_destroy (helper->banner_info->banner);
1261                         g_object_unref (helper->banner_info->banner);
1262                         helper->banner_info->banner = NULL;
1263                 }
1264                 g_slice_free (OpenMsgBannerInfo, helper->banner_info);
1265                 helper->banner_info = NULL;
1266         }
1267         g_object_unref (helper->model);
1268         g_object_unref (helper->header);
1269         gtk_tree_row_reference_free (helper->rowref);
1270         g_slice_free (OpenMsgHelper, helper);
1271 }
1272
1273 static void
1274 open_msg_performer(gboolean canceled, 
1275                     GError *err,
1276                     GtkWindow *parent_window,
1277                     TnyAccount *account,
1278                     gpointer user_data)
1279 {
1280         ModestMailOperation *mail_op = NULL;
1281         gchar *error_msg;
1282         ModestProtocolType proto;
1283         TnyConnectionStatus status;
1284         gboolean show_open_draft = FALSE;
1285         OpenMsgHelper *helper = NULL;
1286
1287         helper = (OpenMsgHelper *) user_data;
1288
1289         status = tny_account_get_connection_status (account);
1290         if (err || canceled) {
1291                 modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (), helper->header);
1292                 /* Free the helper */
1293                 open_msg_helper_destroyer (helper);
1294                 
1295                 /* In memory full conditions we could get this error here */
1296                 check_memory_full_error ((GtkWidget *) parent_window, err);
1297                 
1298                 goto clean;
1299         }
1300
1301         /* Get the error message depending on the protocol */
1302         proto = modest_tny_account_get_protocol_type (account);
1303         if (proto == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1304                 proto = MODEST_PROTOCOLS_STORE_MAILDIR;
1305         }
1306         
1307         ModestProtocol *protocol;
1308         ModestProtocolRegistry *protocol_registry;
1309         gchar *subject;
1310                 
1311         protocol_registry = modest_runtime_get_protocol_registry ();
1312         subject = tny_header_dup_subject (helper->header);
1313
1314         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, proto);
1315         error_msg = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE, subject);
1316         if (subject)
1317                 g_free (subject);
1318                 
1319         if (error_msg == NULL) {
1320                 error_msg = g_strdup (_("mail_ni_ui_folder_get_msg_folder_error"));
1321         }
1322
1323         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry,
1324                                                             proto,
1325                                                             MODEST_PROTOCOL_REGISTRY_LOCAL_STORE_PROTOCOLS)) { 
1326                 TnyFolder *folder;
1327                 TnyFolderType folder_type;
1328
1329                 folder = tny_header_get_folder (helper->header);
1330                 folder_type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
1331                 show_open_draft = (folder_type == TNY_FOLDER_TYPE_DRAFTS);
1332                 g_object_unref (folder);
1333         }
1334
1335 #ifdef MODEST_TOOLKIT_HILDON2
1336         gboolean is_draft;
1337         gchar *account_name = get_info_from_header (helper->header, &is_draft);
1338
1339         if (!is_draft) {
1340                 ModestWindow *window;
1341                 GtkWidget *header_view;
1342                 gchar *uid;
1343
1344                 header_view = get_header_view_from_window (MODEST_WINDOW (parent_window));
1345                 uid = modest_tny_folder_get_header_unique_id (helper->header);
1346                 if (header_view) {
1347                         window = modest_msg_view_window_new_from_header_view 
1348                                 (MODEST_HEADER_VIEW (header_view), account_name, uid, helper->rowref);
1349                         if (window != NULL) {
1350                                 if (!modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
1351                                                                         window, NULL)) {
1352                                         gtk_widget_destroy (GTK_WIDGET (window));
1353                                 } else {
1354                                         gtk_widget_show_all (GTK_WIDGET(window));
1355                                 }
1356                         }
1357                 }
1358                 g_free (account_name);
1359                 g_free (uid);
1360                 open_msg_helper_destroyer (helper);
1361                 goto clean;
1362         }
1363         g_free (account_name);
1364 #endif
1365         /* Create the mail operation */
1366         mail_op = 
1367                 modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
1368                                                                modest_ui_actions_disk_operations_error_handler,
1369                                                                error_msg, g_free);
1370         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1371                                          mail_op);
1372
1373         if (show_open_draft) {
1374                 helper->banner_info = g_slice_new (OpenMsgBannerInfo);
1375 #ifdef MODEST_TOOLKIT_HILDON2
1376                 helper->banner_info->message = g_strdup (_("mail_me_opening"));
1377 #else
1378                 helper->banner_info->message = g_strdup (_("mail_ib_opening_draft_message"));
1379 #endif
1380                 helper->banner_info->banner = NULL;
1381                 helper->banner_info->idle_handler = g_timeout_add (500, open_msg_banner_idle, 
1382                                                                    helper->banner_info);
1383         }
1384
1385         TnyList *headers;
1386         headers = TNY_LIST (tny_simple_list_new ());
1387         tny_list_prepend (headers, G_OBJECT (helper->header));
1388         modest_mail_operation_get_msgs_full (mail_op,
1389                                              headers,
1390                                              open_msg_cb,
1391                                              helper,
1392                                              open_msg_helper_destroyer);
1393         g_object_unref (headers);
1394
1395         /* Frees */
1396  clean:
1397         if (mail_op)
1398                 g_object_unref (mail_op);
1399         g_object_unref (account);
1400 }
1401
1402 /*
1403  * This function is used by both modest_ui_actions_on_open and
1404  * modest_ui_actions_on_header_activated. This way we always do the
1405  * same when trying to open messages.
1406  */
1407 static void
1408 open_msg_from_header (TnyHeader *header, GtkTreeRowReference *rowref, ModestWindow *win)
1409 {
1410         ModestWindowMgr *mgr = NULL;
1411         TnyAccount *account;
1412         gboolean cached = FALSE;
1413         gboolean found;
1414         GtkWidget *header_view = NULL;
1415         OpenMsgHelper *helper;
1416         ModestWindow *window;
1417                 
1418         g_return_if_fail (header != NULL && rowref != NULL);
1419
1420         mgr = modest_runtime_get_window_mgr ();
1421
1422         /* get model */
1423         header_view = get_header_view_from_window (MODEST_WINDOW (win));
1424         if (header_view == NULL)
1425                 return;
1426
1427         /* Get the account */
1428         account = get_account_from_header (header);
1429         if (!account)
1430                 return;
1431
1432         window = NULL;
1433         found = modest_window_mgr_find_registered_header (mgr, header, &window);
1434                 
1435         /* Do not open again the message and present the
1436            window to the user */
1437         if (found) {
1438                 if (window) {
1439 #ifndef MODEST_TOOLKIT_HILDON2
1440                         gtk_window_present (GTK_WINDOW (window));
1441 #endif
1442                 } else {
1443                         /* the header has been registered already, we don't do
1444                          * anything but wait for the window to come up*/
1445                         g_debug ("header %p already registered, waiting for window", header);
1446                 }
1447                 goto cleanup;
1448         }
1449
1450         /* Open each message */
1451         cached = tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED;
1452         if (!cached) {
1453                 /* Allways download if we are online. */
1454                 if (!tny_device_is_online (modest_runtime_get_device ())) {
1455                         gint response;
1456
1457                         /* If ask for user permission to download the messages */
1458                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
1459                                                                             _("mcen_nc_get_msg"));
1460
1461                         /* End if the user does not want to continue */
1462                         if (response == GTK_RESPONSE_CANCEL) {
1463                                 goto cleanup;
1464                         }
1465                 }
1466         }
1467
1468         /* We register the window for opening */
1469         modest_window_mgr_register_header (mgr, header, NULL);
1470
1471         /* Create the helper. We need to get a reference to the model
1472            here because it could change while the message is readed
1473            (the user could switch between folders) */
1474         helper = g_slice_new (OpenMsgHelper);
1475         helper->model = g_object_ref (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view)));
1476         helper->header = g_object_ref (header);
1477         helper->rowref = gtk_tree_row_reference_copy (rowref);
1478         helper->banner_info = NULL;
1479
1480         /* Connect to the account and perform */
1481         if (!cached) {
1482                 modest_platform_connect_and_perform ((GtkWindow *) win, TRUE, g_object_ref (account), 
1483                                                      open_msg_performer, helper);
1484         } else {
1485                 /* Call directly the performer, do not need to connect */
1486                 open_msg_performer (FALSE, NULL, (GtkWindow *) win, 
1487                                     g_object_ref (account), helper);
1488         }
1489 cleanup:
1490         /* Clean */
1491         if (account)
1492                 g_object_unref (account);
1493 }
1494
1495 void
1496 modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
1497 {
1498         TnyList *headers;
1499         TnyHeader *header;
1500         gint headers_count;
1501         TnyIterator *iter;
1502         
1503         /* we check for low-mem; in that case, show a warning, and don't allow
1504          * opening
1505          */
1506         if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
1507                 return;
1508
1509         /* Get headers */
1510         headers = get_selected_headers (win);
1511         if (!headers)
1512                 return;
1513
1514         headers_count = tny_list_get_length (headers);
1515         if (headers_count != 1) {
1516                 if (headers_count > 1) {
1517                         /* Don't allow activation if there are more than one message selected */
1518                         modest_platform_information_banner (NULL, NULL, _("mcen_ib_select_one_message"));
1519                 }
1520
1521                 g_object_unref (headers);
1522                 return;
1523         }
1524
1525         iter = tny_list_create_iterator (headers);
1526         header = TNY_HEADER (tny_iterator_get_current (iter));
1527         g_object_unref (iter);
1528
1529         /* Open them */
1530         if (header) {
1531                 open_msg_from_header (header, NULL, win);
1532                 g_object_unref (header);
1533         }
1534
1535         g_object_unref(headers);
1536 }
1537
1538 static void
1539 rf_helper_window_closed (gpointer data,
1540                          GObject *object)
1541 {
1542         ReplyForwardHelper *helper = (ReplyForwardHelper *) data;
1543
1544         helper->parent_window = NULL;
1545 }
1546
1547 static ReplyForwardHelper*
1548 create_reply_forward_helper (ReplyForwardAction action, 
1549                              ModestWindow *win,
1550                              guint reply_forward_type,
1551                              TnyHeader *header)
1552 {
1553         ReplyForwardHelper *rf_helper = NULL;
1554         const gchar *active_acc = modest_window_get_active_account (win);
1555
1556         rf_helper = g_slice_new0 (ReplyForwardHelper);
1557         rf_helper->reply_forward_type = reply_forward_type;
1558         rf_helper->action = action;
1559         rf_helper->parent_window = (MODEST_IS_WINDOW (win)) ? GTK_WIDGET (win) : NULL;
1560         rf_helper->header = (header) ? g_object_ref (header) : NULL;
1561         rf_helper->account_name = (active_acc) ? 
1562                 g_strdup (active_acc) :
1563                 modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
1564
1565         /* Note that window could be destroyed just AFTER calling
1566            register_window so we must ensure that this pointer does
1567            not hold invalid references */
1568         if (rf_helper->parent_window)
1569                 g_object_weak_ref (G_OBJECT (rf_helper->parent_window), 
1570                                    rf_helper_window_closed, rf_helper);
1571
1572         return rf_helper;
1573 }
1574
1575 static void
1576 free_reply_forward_helper (gpointer data)
1577 {
1578         ReplyForwardHelper *helper;
1579
1580         helper = (ReplyForwardHelper *) data;
1581         g_free (helper->account_name);
1582         if (helper->header)
1583                 g_object_unref (helper->header);
1584         if (helper->parent_window) 
1585                 g_object_weak_unref (G_OBJECT (helper->parent_window), 
1586                                      rf_helper_window_closed, helper);
1587         g_slice_free (ReplyForwardHelper, helper);
1588 }
1589
1590 static void
1591 reply_forward_cb (ModestMailOperation *mail_op,  
1592                   TnyHeader *header, 
1593                   gboolean canceled,
1594                   TnyMsg *msg,
1595                   GError *err,
1596                   gpointer user_data)
1597 {
1598         TnyMsg *new_msg = NULL;
1599         ReplyForwardHelper *rf_helper;
1600         ModestWindow *msg_win = NULL;
1601         ModestEditType edit_type;
1602         gchar *from = NULL;
1603         TnyAccount *account = NULL;
1604         ModestWindowMgr *mgr = NULL;
1605         gchar *signature = NULL;
1606         gboolean use_signature;
1607
1608         /* If there was any error. The mail operation could be NULL,
1609            this means that we already have the message downloaded and
1610            that we didn't do a mail operation to retrieve it */
1611         rf_helper = (ReplyForwardHelper *) user_data;
1612         if (mail_op && !modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
1613                 goto cleanup;
1614
1615         from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
1616                                                    rf_helper->account_name);
1617         signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr(), 
1618                                                       rf_helper->account_name, 
1619                                                       &use_signature);
1620
1621         /* Create reply mail */
1622         switch (rf_helper->action) {
1623         case ACTION_REPLY:
1624                 new_msg = 
1625                         modest_tny_msg_create_reply_msg (msg, header, from, 
1626                                                          (use_signature) ? signature : NULL,
1627                                                          rf_helper->reply_forward_type,
1628                                                          MODEST_TNY_MSG_REPLY_MODE_SENDER);
1629                 break;
1630         case ACTION_REPLY_TO_ALL:
1631                 new_msg = 
1632                         modest_tny_msg_create_reply_msg (msg, header, from, 
1633                                                          (use_signature) ? signature : NULL, 
1634                                                          rf_helper->reply_forward_type,
1635                                                          MODEST_TNY_MSG_REPLY_MODE_ALL);
1636                 edit_type = MODEST_EDIT_TYPE_REPLY;
1637                 break;
1638         case ACTION_FORWARD:
1639                 new_msg = 
1640                         modest_tny_msg_create_forward_msg (msg, from, (use_signature) ? signature : NULL, 
1641                                                            rf_helper->reply_forward_type);
1642                 edit_type = MODEST_EDIT_TYPE_FORWARD;
1643                 break;
1644         default:
1645                 modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),
1646                                                      header);
1647                 g_return_if_reached ();
1648                 return;
1649         }
1650
1651         g_free (from);
1652         g_free (signature);
1653
1654         if (!new_msg) {
1655                 g_warning ("%s: failed to create message\n", __FUNCTION__);
1656                 goto cleanup;
1657         }
1658
1659         account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(),
1660                                                                        rf_helper->account_name,
1661                                                                        TNY_ACCOUNT_TYPE_STORE);
1662         if (!account) {
1663                 g_warning ("%s: failed to get tnyaccount for '%s'\n", __FUNCTION__, rf_helper->account_name);
1664                 goto cleanup;
1665         }
1666
1667         /* Create and register the windows */
1668         msg_win = modest_msg_edit_window_new (new_msg, rf_helper->account_name, FALSE);
1669         mgr = modest_runtime_get_window_mgr ();
1670         modest_window_mgr_register_window (mgr, msg_win, (ModestWindow *) rf_helper->parent_window);
1671
1672         /* Note that register_window could have deleted the account */
1673         if (MODEST_IS_WINDOW (rf_helper->parent_window)) {
1674                 gdouble parent_zoom;
1675
1676                 parent_zoom = modest_window_get_zoom (MODEST_WINDOW (rf_helper->parent_window));
1677                 modest_window_set_zoom (msg_win, parent_zoom);
1678         }
1679
1680         /* Show edit window */
1681         gtk_widget_show_all (GTK_WIDGET (msg_win));
1682
1683 cleanup:
1684         /* We always unregister the header because the message is
1685            forwarded or replied so the original one is no longer
1686            opened */
1687         modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),
1688                                              header);
1689         if (new_msg)
1690                 g_object_unref (G_OBJECT (new_msg));
1691         if (account)
1692                 g_object_unref (G_OBJECT (account));
1693         free_reply_forward_helper (rf_helper);
1694 }
1695
1696 /* Checks a list of headers. If any of them are not currently
1697  * downloaded (CACHED) then returns TRUE else returns FALSE.
1698  */
1699 static gint
1700 header_list_count_uncached_msgs (TnyList *header_list)
1701 {
1702         TnyIterator *iter;
1703         gint uncached_messages = 0;
1704
1705         iter = tny_list_create_iterator (header_list);
1706         while (!tny_iterator_is_done (iter)) {
1707                 TnyHeader *header;
1708
1709                 header = TNY_HEADER (tny_iterator_get_current (iter));
1710                 if (header) {
1711                         if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED))
1712                                 uncached_messages ++;
1713                         g_object_unref (header);
1714                 }
1715
1716                 tny_iterator_next (iter);
1717         }
1718         g_object_unref (iter);
1719
1720         return uncached_messages;
1721 }
1722
1723 /* Returns FALSE if the user does not want to download the
1724  * messages. Returns TRUE if the user allowed the download.
1725  */
1726 static gboolean
1727 connect_to_get_msg (ModestWindow *win,
1728                     gint num_of_uncached_msgs,
1729                     TnyAccount *account)
1730 {
1731         GtkResponseType response;
1732
1733         /* Allways download if we are online. */
1734         if (tny_device_is_online (modest_runtime_get_device ()))
1735                 return TRUE;
1736
1737         /* If offline, then ask for user permission to download the messages */
1738         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
1739                         ngettext("mcen_nc_get_msg",
1740                         "mcen_nc_get_msgs",
1741                         num_of_uncached_msgs));
1742
1743         if (response == GTK_RESPONSE_CANCEL)
1744                 return FALSE;
1745
1746         return modest_platform_connect_and_wait((GtkWindow *) win, account);
1747 }
1748
1749 static void
1750 reply_forward_performer (gboolean canceled, 
1751                          GError *err,
1752                          GtkWindow *parent_window, 
1753                          TnyAccount *account, 
1754                          gpointer user_data)
1755 {
1756         ReplyForwardHelper *rf_helper = NULL;
1757         ModestMailOperation *mail_op;
1758
1759         rf_helper = (ReplyForwardHelper *) user_data;
1760
1761         if (canceled || err) {
1762                 free_reply_forward_helper (rf_helper);
1763                 return;
1764         }
1765
1766         /* Retrieve the message */
1767         modest_window_mgr_register_header (modest_runtime_get_window_mgr (), rf_helper->header, NULL);
1768         mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (parent_window),
1769                                                                  modest_ui_actions_disk_operations_error_handler,
1770                                                                  NULL, NULL);
1771         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
1772         modest_mail_operation_get_msg (mail_op, rf_helper->header, TRUE, reply_forward_cb, rf_helper);
1773
1774         /* Frees */
1775         g_object_unref(mail_op);
1776 }
1777
1778 /*
1779  * Common code for the reply and forward actions
1780  */
1781 static void
1782 reply_forward (ReplyForwardAction action, ModestWindow *win)
1783 {
1784         ReplyForwardHelper *rf_helper = NULL;
1785         guint reply_forward_type;
1786
1787         g_return_if_fail (MODEST_IS_WINDOW(win));
1788
1789         /* we check for low-mem; in that case, show a warning, and don't allow
1790          * reply/forward (because it could potentially require a lot of memory */
1791         if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
1792                 return;
1793
1794
1795         /* we need an account when editing */
1796         if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
1797                 if (!modest_ui_actions_run_account_setup_wizard (win))
1798                         return;
1799         }
1800
1801         reply_forward_type =
1802                 modest_conf_get_int (modest_runtime_get_conf (),
1803                                      (action == ACTION_FORWARD) ?
1804                                      MODEST_CONF_FORWARD_TYPE :
1805                                      MODEST_CONF_REPLY_TYPE,
1806                                      NULL);
1807
1808         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1809                 TnyMsg *msg = NULL;
1810                 TnyHeader *header = NULL;
1811                 /* Get header and message. Do not free them here, the
1812                    reply_forward_cb must do it */
1813                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW(win));
1814                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
1815
1816                 if (msg && header) {
1817                         /* Create helper */
1818                         rf_helper = create_reply_forward_helper (action, win,
1819                                                                  reply_forward_type, header);
1820                         reply_forward_cb (NULL, header, FALSE, msg, NULL, rf_helper);
1821                 } else {
1822                         g_warning("%s: no message or header found in viewer\n", __FUNCTION__);
1823                 }
1824
1825                 if (msg)
1826                         g_object_unref (msg);
1827                 if (header)
1828                         g_object_unref (header);
1829         } else {
1830                 TnyHeader *header = NULL;
1831                 TnyIterator *iter;
1832                 gboolean do_retrieve = TRUE;
1833                 TnyList *header_list = NULL;
1834
1835                 header_list = get_selected_headers (win);
1836                 if (!header_list)
1837                         return;
1838                 /* Check that only one message is selected for replying */
1839                 if (tny_list_get_length (header_list) != 1) {
1840                         modest_platform_information_banner ((win) ? GTK_WIDGET (win) : NULL,
1841                                                             NULL, _("mcen_ib_select_one_message"));
1842                         g_object_unref (header_list);
1843                         return;
1844                 }
1845
1846                 /* Only reply/forward to one message */
1847                 iter = tny_list_create_iterator (header_list);
1848                 header = TNY_HEADER (tny_iterator_get_current (iter));
1849                 g_object_unref (iter);
1850
1851                 /* Retrieve messages */
1852                 do_retrieve = (action == ACTION_FORWARD) ||
1853                         (reply_forward_type != MODEST_TNY_MSG_REPLY_TYPE_CITE);
1854
1855                 if (do_retrieve) {
1856                         TnyAccount *account = NULL;
1857                         TnyFolder *folder = NULL;
1858                         gdouble download = TRUE;
1859                         guint uncached_msgs = 0;
1860
1861                         folder = tny_header_get_folder (header);
1862                         if (!folder)
1863                                 goto do_retrieve_frees;
1864                         account = tny_folder_get_account (folder);
1865                         if (!account)
1866                                 goto do_retrieve_frees;
1867
1868                         uncached_msgs = header_list_count_uncached_msgs (header_list);
1869
1870                         if (uncached_msgs > 0) {
1871                                 /* Allways download if we are online. */
1872                                 if (!tny_device_is_online (modest_runtime_get_device ())) {
1873                                         gint response;
1874
1875                                         /* If ask for user permission to download the messages */
1876                                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
1877                                                                                             ngettext("mcen_nc_get_msg",
1878                                                                                                      "mcen_nc_get_msgs",
1879                                                                                                      uncached_msgs));
1880
1881                                         /* End if the user does not want to continue */
1882                                         if (response == GTK_RESPONSE_CANCEL)
1883                                                 download = FALSE;
1884                                 }
1885                         }
1886
1887                         if (download) {
1888                                 /* Create helper */
1889                                 rf_helper = create_reply_forward_helper (action, win, 
1890                                                                          reply_forward_type, header);
1891                                 if (uncached_msgs > 0) {
1892                                         modest_platform_connect_and_perform (GTK_WINDOW (win), 
1893                                                                              TRUE, account, 
1894                                                                              reply_forward_performer, 
1895                                                                              rf_helper);
1896                                 } else {
1897                                         reply_forward_performer (FALSE, NULL, GTK_WINDOW (win), 
1898                                                                  account, rf_helper);
1899                                 }
1900                         }
1901                 do_retrieve_frees:
1902                         if (account)
1903                                 g_object_unref (account);
1904                         if (folder)
1905                                 g_object_unref (folder);
1906                 } else {
1907                         reply_forward_cb (NULL, header, FALSE, NULL, NULL, rf_helper);
1908                 }
1909                 /* Frees */
1910                 g_object_unref (header_list);
1911                 g_object_unref (header);
1912         }
1913 }
1914
1915 void
1916 modest_ui_actions_on_reply (GtkAction *action, ModestWindow *win)
1917 {
1918         g_return_if_fail (MODEST_IS_WINDOW(win));
1919
1920         reply_forward (ACTION_REPLY, win);
1921 }
1922
1923 void
1924 modest_ui_actions_on_forward (GtkAction *action, ModestWindow *win)
1925 {
1926         g_return_if_fail (MODEST_IS_WINDOW(win));
1927
1928         reply_forward (ACTION_FORWARD, win);
1929 }
1930
1931 void
1932 modest_ui_actions_on_reply_all (GtkAction *action, ModestWindow *win)
1933 {
1934         g_return_if_fail (MODEST_IS_WINDOW(win));
1935
1936         reply_forward (ACTION_REPLY_TO_ALL, win);
1937 }
1938
1939 void 
1940 modest_ui_actions_on_next (GtkAction *action, 
1941                            ModestWindow *window)
1942 {
1943         if (MODEST_IS_MAIN_WINDOW (window)) {
1944                 GtkWidget *header_view;
1945
1946                 header_view = modest_main_window_get_child_widget (
1947                                 MODEST_MAIN_WINDOW(window),
1948                                 MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1949                 if (!header_view)
1950                         return;
1951         
1952                 modest_header_view_select_next (
1953                                 MODEST_HEADER_VIEW(header_view)); 
1954         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1955                 modest_msg_view_window_select_next_message (
1956                                 MODEST_MSG_VIEW_WINDOW (window));
1957         } else {
1958                 g_return_if_reached ();
1959         }
1960 }
1961
1962 void 
1963 modest_ui_actions_on_prev (GtkAction *action, 
1964                            ModestWindow *window)
1965 {
1966         g_return_if_fail (MODEST_IS_WINDOW(window));
1967
1968         if (MODEST_IS_MAIN_WINDOW (window)) {
1969                 GtkWidget *header_view;
1970                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1971                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1972                 if (!header_view)
1973                         return;
1974                 
1975                 modest_header_view_select_prev (MODEST_HEADER_VIEW(header_view)); 
1976         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1977                 modest_msg_view_window_select_previous_message (MODEST_MSG_VIEW_WINDOW (window));
1978         } else {
1979                 g_return_if_reached ();
1980         }
1981 }
1982
1983 void 
1984 modest_ui_actions_on_sort (GtkAction *action, 
1985                            ModestWindow *window)
1986 {
1987         GtkWidget *header_view = NULL;
1988
1989         g_return_if_fail (MODEST_IS_WINDOW(window));
1990
1991         if (MODEST_IS_MAIN_WINDOW (window)) {
1992                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1993                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1994 #ifdef MODEST_TOOLKIT_HILDON2
1995         } else if (MODEST_IS_HEADER_WINDOW (window)) {
1996                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (window)));
1997 #endif
1998         }
1999
2000         if (!header_view) {
2001                 modest_platform_information_banner (NULL, NULL, _CS("ckdg_ib_nothing_to_sort"));
2002                 
2003                 return;
2004         }
2005
2006         /* Show sorting dialog */
2007         modest_utils_run_sort_dialog (GTK_WINDOW (window), MODEST_SORT_HEADERS);        
2008 }
2009
2010 static void
2011 new_messages_arrived (ModestMailOperation *self, 
2012                       TnyList *new_headers,
2013                       gpointer user_data)
2014 {
2015         GObject *source;
2016         gboolean show_visual_notifications;
2017
2018         source = modest_mail_operation_get_source (self);
2019         show_visual_notifications = (source) ? FALSE : TRUE;
2020         if (source)
2021                 g_object_unref (source);
2022
2023         /* Notify new messages have been downloaded. If the
2024            send&receive was invoked by the user then do not show any
2025            visual notification, only play a sound and activate the LED
2026            (for the Maemo version) */
2027         if (TNY_IS_LIST(new_headers) && (tny_list_get_length (new_headers)) > 0)
2028                 modest_platform_on_new_headers_received (new_headers, 
2029                                                          show_visual_notifications);
2030
2031 }
2032
2033 gboolean
2034 retrieve_all_messages_cb (GObject *source,
2035                           guint num_msgs,
2036                           guint retrieve_limit)
2037 {
2038         GtkWindow *window;
2039         gchar *msg;
2040         gint response;
2041
2042         window = GTK_WINDOW (source);
2043         msg = g_strdup_printf (_("mail_nc_msg_count_limit_exceeded"), 
2044                                num_msgs, retrieve_limit);
2045
2046         /* Ask the user if they want to retrieve all the messages */
2047         response = 
2048                 modest_platform_run_confirmation_dialog_with_buttons (window, msg,
2049                                                                       _("mcen_bd_get_all"),
2050                                                                       _("mcen_bd_newest_only"));
2051         /* Free and return */
2052         g_free (msg);
2053         return (response == GTK_RESPONSE_ACCEPT) ? TRUE : FALSE;
2054 }
2055
2056 typedef struct {
2057         TnyAccount *account;
2058         ModestWindow *win;
2059         gchar *account_name;
2060         gboolean poke_status;
2061         gboolean interactive;
2062         ModestMailOperation *mail_op;
2063 } SendReceiveInfo;
2064
2065 static void
2066 do_send_receive_performer (gboolean canceled, 
2067                            GError *err,
2068                            GtkWindow *parent_window, 
2069                            TnyAccount *account, 
2070                            gpointer user_data)
2071 {
2072         SendReceiveInfo *info;
2073
2074         info = (SendReceiveInfo *) user_data;
2075
2076         if (err || canceled) {
2077                 /* In memory full conditions we could get this error here */
2078                 check_memory_full_error ((GtkWidget *) parent_window, err);
2079
2080                 if (info->mail_op) {
2081                         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
2082                                                             info->mail_op);
2083                 }
2084                 goto clean;
2085         }
2086
2087         /* Set send/receive operation in progress */    
2088         if (info->win && MODEST_IS_MAIN_WINDOW (info->win)) {
2089                 modest_main_window_notify_send_receive_initied (MODEST_MAIN_WINDOW (info->win));
2090         }
2091
2092         if (info->win && MODEST_IS_MAIN_WINDOW (info->win))
2093                 g_signal_connect (G_OBJECT (info->mail_op), "operation-finished", 
2094                                   G_CALLBACK (on_send_receive_finished), 
2095                                   info->win);
2096
2097         /* Send & receive. */
2098         modest_mail_operation_update_account (info->mail_op, info->account_name, info->poke_status, info->interactive,
2099                                               (info->win) ? retrieve_all_messages_cb : NULL, 
2100                                               new_messages_arrived, info->win);
2101         
2102  clean:
2103         /* Frees */
2104         if (info->mail_op)
2105                 g_object_unref (G_OBJECT (info->mail_op));
2106         if (info->account_name)
2107                 g_free (info->account_name);
2108         if (info->win)
2109                 g_object_unref (info->win);
2110         if (info->account)
2111                 g_object_unref (info->account);
2112         g_slice_free (SendReceiveInfo, info);
2113 }
2114
2115 /*
2116  * This function performs the send & receive required actions. The
2117  * window is used to create the mail operation. Typically it should
2118  * always be the main window, but we pass it as argument in order to
2119  * be more flexible.
2120  */
2121 void
2122 modest_ui_actions_do_send_receive (const gchar *account_name, 
2123                                    gboolean force_connection,
2124                                    gboolean poke_status,
2125                                    gboolean interactive,
2126                                    ModestWindow *win)
2127 {
2128         gchar *acc_name = NULL;
2129         SendReceiveInfo *info;
2130         ModestTnyAccountStore *acc_store;
2131
2132         /* If no account name was provided then get the current account, and if
2133            there is no current account then pick the default one: */
2134         if (!account_name) {
2135                 if (win)
2136                         acc_name = g_strdup (modest_window_get_active_account (win));
2137                 if (!acc_name)
2138                         acc_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
2139                 if (!acc_name) {
2140                         g_printerr ("modest: cannot get default account\n");
2141                         return;
2142                 }
2143         } else {
2144                 acc_name = g_strdup (account_name);
2145         }
2146
2147         acc_store = modest_runtime_get_account_store ();
2148
2149         /* Create the info for the connect and perform */
2150         info = g_slice_new (SendReceiveInfo);
2151         info->account_name = acc_name;
2152         info->win = (win) ? g_object_ref (win) : NULL;
2153         info->poke_status = poke_status;
2154         info->interactive = interactive;
2155         info->account = modest_tny_account_store_get_server_account (acc_store, acc_name,
2156                                                                      TNY_ACCOUNT_TYPE_STORE);
2157         /* We need to create the operation here, because otherwise it
2158            could happen that the queue emits the queue-empty signal
2159            while we're trying to connect the account */
2160         info->mail_op = modest_mail_operation_new_with_error_handling ((info->win) ? G_OBJECT (info->win) : NULL,
2161                                                                        modest_ui_actions_disk_operations_error_handler,
2162                                                                        NULL, NULL);
2163         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), info->mail_op);
2164
2165         /* Invoke the connect and perform */
2166         modest_platform_connect_and_perform ((win) ? GTK_WINDOW (win) : NULL, 
2167                                              force_connection, info->account, 
2168                                              do_send_receive_performer, info);
2169 }
2170
2171
2172 static void
2173 modest_ui_actions_do_cancel_send (const gchar *account_name,  
2174                                   ModestWindow *win)
2175 {
2176         TnyTransportAccount *transport_account;
2177         TnySendQueue *send_queue = NULL;
2178         GError *error = NULL;
2179
2180         /* Get transport account */
2181         transport_account =
2182                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
2183                                       (modest_runtime_get_account_store(),
2184                                        account_name,
2185                                        TNY_ACCOUNT_TYPE_TRANSPORT));
2186         if (!transport_account) {
2187                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
2188                 goto frees;
2189         }
2190
2191         /* Get send queue*/
2192         send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account, TRUE));
2193         if (!TNY_IS_SEND_QUEUE(send_queue)) {
2194                 g_set_error (&error, MODEST_MAIL_OPERATION_ERROR,
2195                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2196                              "modest: could not find send queue for account\n");
2197         } else {
2198                 /* Cancel the current send */
2199                 tny_account_cancel (TNY_ACCOUNT (transport_account));
2200
2201                 /* Suspend all pending messages */
2202                 tny_send_queue_cancel (send_queue, TNY_SEND_QUEUE_CANCEL_ACTION_SUSPEND, &error);
2203         }
2204
2205  frees:
2206         if (transport_account != NULL) 
2207                 g_object_unref (G_OBJECT (transport_account));
2208 }
2209
2210 static void
2211 modest_ui_actions_cancel_send_all (ModestWindow *win) 
2212 {
2213         GSList *account_names, *iter;
2214
2215         account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
2216                                                           TRUE);
2217
2218         iter = account_names;
2219         while (iter) {                  
2220                 modest_ui_actions_do_cancel_send ((const char*) iter->data, win);
2221                 iter = g_slist_next (iter);
2222         }
2223
2224         modest_account_mgr_free_account_names (account_names);
2225         account_names = NULL;
2226 }
2227
2228 void
2229 modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
2230
2231 {
2232         /* Check if accounts exist */
2233         gboolean accounts_exist = 
2234                 modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
2235         
2236         /* If not, allow the user to create an account before trying to send/receive. */
2237         if (!accounts_exist)
2238                 modest_ui_actions_on_accounts (NULL, win);
2239         
2240         /* Cancel all sending operaitons */     
2241         modest_ui_actions_cancel_send_all (win);
2242 }
2243
2244 /*
2245  * Refreshes all accounts. This function will be used by automatic
2246  * updates
2247  */
2248 void
2249 modest_ui_actions_do_send_receive_all (ModestWindow *win, 
2250                                        gboolean force_connection,
2251                                        gboolean poke_status,
2252                                        gboolean interactive)
2253 {
2254         GSList *account_names, *iter;
2255
2256         account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
2257                                                           TRUE);
2258
2259         iter = account_names;
2260         while (iter) {                  
2261                 modest_ui_actions_do_send_receive ((const char*) iter->data, 
2262                                                    force_connection, 
2263                                                    poke_status, interactive, win);
2264                 iter = g_slist_next (iter);
2265         }
2266
2267         modest_account_mgr_free_account_names (account_names);
2268         account_names = NULL;
2269 }
2270
2271 /*
2272  * Handler of the click on Send&Receive button in the main toolbar
2273  */
2274 void
2275 modest_ui_actions_on_send_receive (GtkAction *action, ModestWindow *win)
2276 {
2277         /* Check if accounts exist */
2278         gboolean accounts_exist;
2279
2280         accounts_exist =
2281                 modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
2282         
2283         /* If not, allow the user to create an account before trying to send/receive. */
2284         if (!accounts_exist)
2285                 modest_ui_actions_on_accounts (NULL, win);
2286         
2287         /* Refresh the current folder. The if is always TRUE it's just an extra check */
2288         if (MODEST_IS_MAIN_WINDOW (win)) {
2289                 GtkWidget *folder_view;
2290                 TnyFolderStore *folder_store;
2291
2292                 folder_view = 
2293                         modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win), 
2294                                                              MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2295                 if (!folder_view)
2296                         return;
2297                 
2298                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2299         
2300                 if (folder_store)
2301                         g_object_unref (folder_store);
2302                 /* Refresh the active account. Force the connection if needed
2303                    and poke the status of all folders */
2304                 modest_ui_actions_do_send_receive (NULL, TRUE, TRUE, TRUE, win);
2305 #ifdef MODEST_TOOLKIT_HILDON2
2306         } else if (MODEST_IS_ACCOUNTS_WINDOW (win)) {
2307                 modest_ui_actions_do_send_receive_all (win, TRUE, TRUE, TRUE);
2308 #endif
2309         } else {
2310                 const gchar *active_account;
2311                 active_account = modest_window_get_active_account (MODEST_WINDOW (win));
2312
2313                 modest_ui_actions_do_send_receive (active_account, TRUE, TRUE, TRUE, win);
2314         } 
2315         
2316 }
2317
2318
2319 void
2320 modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *main_window)
2321 {
2322         ModestConf *conf;
2323         GtkWidget *header_view;
2324         
2325         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2326
2327         header_view = modest_main_window_get_child_widget (main_window,
2328                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2329         if (!header_view)
2330                 return;
2331
2332         conf = modest_runtime_get_conf ();
2333         
2334         /* what is saved/restored is depending on the style; thus; we save with
2335          * old style, then update the style, and restore for this new style
2336          */
2337         modest_widget_memory_save (conf, G_OBJECT(header_view), MODEST_CONF_HEADER_VIEW_KEY);
2338         
2339         if (modest_header_view_get_style
2340             (MODEST_HEADER_VIEW(header_view)) == MODEST_HEADER_VIEW_STYLE_DETAILS)
2341                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
2342                                               MODEST_HEADER_VIEW_STYLE_TWOLINES);
2343         else
2344                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
2345                                               MODEST_HEADER_VIEW_STYLE_DETAILS);
2346
2347         modest_widget_memory_restore (conf, G_OBJECT(header_view),
2348                                       MODEST_CONF_HEADER_VIEW_KEY);
2349 }
2350
2351
2352 void 
2353 modest_ui_actions_on_header_selected (ModestHeaderView *header_view, 
2354                                       TnyHeader *header,
2355                                       ModestMainWindow *main_window)
2356 {
2357         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2358         g_return_if_fail (MODEST_IS_HEADER_VIEW (header_view));
2359         
2360         /* in the case the folder is empty, show the empty folder message and focus
2361          * folder view */
2362         if (!header && gtk_widget_is_focus (GTK_WIDGET (header_view))) {
2363                 if (modest_header_view_is_empty (header_view)) {
2364                         TnyFolder *folder = modest_header_view_get_folder (header_view);
2365                         GtkWidget *folder_view = 
2366                                 modest_main_window_get_child_widget (main_window,
2367                                                                      MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2368                         if (folder != NULL) {
2369                                 modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view), folder, FALSE);
2370                                 g_object_unref (folder);
2371                         }
2372                         gtk_widget_grab_focus (GTK_WIDGET (folder_view));
2373                         return;
2374                 }
2375         }
2376         /* If no header has been selected then exit */
2377         if (!header)
2378                 return;
2379
2380         /* Update focus */
2381         if (!gtk_widget_is_focus (GTK_WIDGET(header_view)))
2382             gtk_widget_grab_focus (GTK_WIDGET(header_view));
2383
2384         /* Update toolbar dimming state */
2385         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
2386         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
2387 }
2388
2389 void
2390 modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
2391                                        TnyHeader *header,
2392                                        GtkTreePath *path,
2393                                        ModestWindow *window)
2394 {
2395         GtkWidget *open_widget;
2396         GtkTreeRowReference *rowref;
2397
2398         g_return_if_fail (MODEST_IS_WINDOW(window));
2399         g_return_if_fail (MODEST_IS_HEADER_VIEW (header_view));
2400         g_return_if_fail (TNY_IS_HEADER (header));
2401
2402         if (modest_header_view_count_selected_headers (header_view) > 1) {
2403                 /* Don't allow activation if there are more than one message selected */
2404                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_select_one_message"));
2405                 return;
2406         }
2407
2408         /* we check for low-mem; in that case, show a warning, and don't allow
2409          * activating headers
2410          */
2411         if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
2412                 return;
2413
2414         if (MODEST_IS_MAIN_WINDOW (window)) {
2415                 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
2416                 open_widget = modest_window_get_action_widget (MODEST_WINDOW (window), "/MenuBar/EmailMenu/EmailOpenMenu");
2417                 if (!GTK_WIDGET_IS_SENSITIVE (open_widget))
2418                         return;
2419         }
2420
2421         rowref = gtk_tree_row_reference_new (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view)), path);
2422         open_msg_from_header (header, rowref, MODEST_WINDOW (window));
2423         gtk_tree_row_reference_free (rowref);
2424 }
2425
2426 static void
2427 set_active_account_from_tny_account (TnyAccount *account,
2428                                      ModestWindow *window)
2429 {
2430         const gchar *server_acc_name = tny_account_get_id (account);
2431         
2432         /* We need the TnyAccount provided by the
2433            account store because that is the one that
2434            knows the name of the Modest account */
2435         TnyAccount *modest_server_account = modest_server_account = 
2436                 modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store (),
2437                                                              MODEST_TNY_ACCOUNT_STORE_QUERY_ID, 
2438                                                              server_acc_name);
2439         if (!modest_server_account) {
2440                 g_warning ("%s: could not get tny account\n", __FUNCTION__);
2441                 return;
2442         }
2443
2444         /* Update active account, but only if it's not a pseudo-account */
2445         if ((!modest_tny_account_is_virtual_local_folders(modest_server_account)) &&
2446             (!modest_tny_account_is_memory_card_account(modest_server_account))) {
2447                 const gchar *modest_acc_name = 
2448                         modest_tny_account_get_parent_modest_account_name_for_server_account (modest_server_account);
2449                 if (modest_acc_name)
2450                         modest_window_set_active_account (window, modest_acc_name);
2451         }
2452         
2453         g_object_unref (modest_server_account);
2454 }
2455
2456
2457 static void
2458 folder_refreshed_cb (ModestMailOperation *mail_op, 
2459                      TnyFolder *folder, 
2460                      gpointer user_data)
2461 {
2462         ModestMainWindow *win = NULL;
2463         GtkWidget *folder_view;
2464         const GError *error;
2465
2466         g_return_if_fail (TNY_IS_FOLDER (folder));
2467
2468         win = MODEST_MAIN_WINDOW (user_data);
2469
2470         /* Check if the operation failed due to memory low conditions */
2471         error = modest_mail_operation_get_error (mail_op);
2472         if (error && error->domain == MODEST_MAIL_OPERATION_ERROR && 
2473             error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) {
2474                 modest_platform_run_information_dialog (GTK_WINDOW (win),
2475                                                         dgettext("ke-recv","memr_ib_operation_disabled"),
2476                                                         TRUE);
2477                 return;
2478         }
2479
2480         folder_view = 
2481                 modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2482
2483         if (folder_view) {
2484                 TnyFolderStore *current_folder;
2485
2486                 current_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2487                 if (current_folder) {
2488                         gboolean different = ((TnyFolderStore *) folder != current_folder);
2489                         g_object_unref (current_folder);
2490                         if (different)
2491                                 return;
2492                 }
2493         }
2494
2495         /* Check if folder is empty and set headers view contents style */
2496         if (tny_folder_get_all_count (folder) == 0)
2497                 modest_main_window_set_contents_style (win,
2498                                                        MODEST_MAIN_WINDOW_CONTENTS_STYLE_EMPTY);
2499
2500 }
2501
2502 void 
2503 modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
2504                                                TnyFolderStore *folder_store, 
2505                                                gboolean selected,
2506                                                ModestMainWindow *main_window)
2507 {
2508         ModestConf *conf;
2509         GtkWidget *header_view;
2510
2511         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2512
2513         header_view = modest_main_window_get_child_widget(main_window,
2514                                                           MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2515         if (!header_view)
2516                 return;
2517         
2518         conf = modest_runtime_get_conf ();
2519
2520         if (TNY_IS_ACCOUNT (folder_store)) {
2521                 if (selected) {
2522                         set_active_account_from_tny_account (TNY_ACCOUNT (folder_store), MODEST_WINDOW (main_window));
2523                         
2524                         /* Show account details */
2525                         modest_main_window_set_contents_style (main_window, MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS);
2526                 }
2527         } else {
2528                 if (TNY_IS_FOLDER (folder_store) && selected) {
2529                         TnyAccount *account;
2530                         const gchar *account_name = NULL;
2531
2532                         /* Update the active account */
2533                         account = modest_tny_folder_get_account (TNY_FOLDER (folder_store));
2534                         if (account) {
2535                                 set_active_account_from_tny_account (account, MODEST_WINDOW (main_window));
2536                                 account_name = 
2537                                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
2538                                 g_object_unref (account);
2539                                 account = NULL;
2540                         }
2541
2542                         /* Set the header style by default, it could
2543                            be changed later by the refresh callback to
2544                            empty */
2545                         modest_main_window_set_contents_style (main_window, 
2546                                                                MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
2547
2548                         /* Set folder on header view. This function
2549                            will call tny_folder_refresh_async so we
2550                            pass a callback that will be called when
2551                            finished. We use that callback to set the
2552                            empty view if there are no messages */
2553                         modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
2554                                                        TNY_FOLDER (folder_store),
2555                                                        TRUE,
2556                                                        MODEST_WINDOW (main_window),
2557                                                        folder_refreshed_cb,
2558                                                        main_window);
2559                         
2560                         /* Restore configuration. We need to do this
2561                            *after* the set_folder because the widget
2562                            memory asks the header view about its
2563                            folder  */
2564                         modest_widget_memory_restore (modest_runtime_get_conf (), 
2565                                                       G_OBJECT(header_view),
2566                                                       MODEST_CONF_HEADER_VIEW_KEY);
2567                 } else {
2568                         /* No need to save the header view
2569                            configuration for Maemo because it only
2570                            saves the sorting stuff and that it's
2571                            already being done by the sort
2572                            dialog. Remove it when the GNOME version
2573                            has the same behaviour */
2574 #ifdef MODEST_TOOLKIT_GTK
2575                         if (modest_main_window_get_contents_style (main_window) ==
2576                             MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS)
2577                                 modest_widget_memory_save (conf, G_OBJECT (header_view), 
2578                                                            MODEST_CONF_HEADER_VIEW_KEY);
2579 #endif
2580                         modest_header_view_clear (MODEST_HEADER_VIEW(header_view));
2581                 }
2582         }
2583
2584         /* Update dimming state */
2585         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
2586         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
2587 }
2588
2589 void 
2590 modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemType type,
2591                                      ModestWindow *win)
2592 {
2593         GtkWidget *dialog;
2594         gchar *txt, *item;
2595         gboolean online;
2596
2597         item = (type == MODEST_ITEM_TYPE_FOLDER) ? "folder" : "message";
2598         
2599         online = tny_device_is_online (modest_runtime_get_device());
2600
2601         if (online) {
2602                 /* already online -- the item is simply not there... */
2603                 dialog = gtk_message_dialog_new (GTK_WINDOW (win),
2604                                                  GTK_DIALOG_MODAL,
2605                                                  GTK_MESSAGE_WARNING,
2606                                                  GTK_BUTTONS_NONE,
2607                                                  _("The %s you selected cannot be found"),
2608                                                  item);
2609                 gtk_dialog_add_button (GTK_DIALOG (dialog),_("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT);
2610                 gtk_dialog_run (GTK_DIALOG(dialog));
2611         } else {
2612                 dialog = gtk_dialog_new_with_buttons (_("Connection requested"),
2613                                                       GTK_WINDOW (win),
2614                                                       GTK_DIALOG_MODAL,
2615                                                       _("mcen_bd_dialog_cancel"),
2616                                                       GTK_RESPONSE_REJECT,
2617                                                       _("mcen_bd_dialog_ok"),
2618                                                       GTK_RESPONSE_ACCEPT,
2619                                                       NULL);
2620                 txt = g_strdup_printf (_("This %s is not available in offline mode.\n"
2621                                          "Do you want to get online?"), item);
2622                 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
2623                                     gtk_label_new (txt), FALSE, FALSE, 0);
2624                 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
2625                 g_free (txt);
2626
2627                 gtk_window_set_default_size (GTK_WINDOW(dialog), 300, 300);
2628                 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
2629                         /* TODO: Comment about why is this commented out: */
2630                         /* modest_platform_connect_and_wait (); */
2631                 }
2632         }
2633         gtk_widget_destroy (dialog);
2634 }
2635
2636 void
2637 modest_ui_actions_on_msg_link_hover (ModestMsgView *msgview, const gchar* link,
2638                                      ModestWindow *win)
2639 {
2640         /* g_message ("%s %s", __FUNCTION__, link); */
2641 }       
2642
2643
2644 void
2645 modest_ui_actions_on_msg_link_clicked (ModestMsgView *msgview, const gchar* link,
2646                                         ModestWindow *win)
2647 {
2648         modest_platform_activate_uri (link);
2649 }
2650
2651 void
2652 modest_ui_actions_on_msg_link_contextual (ModestMsgView *msgview, const gchar* link,
2653                                           ModestWindow *win)
2654 {
2655         modest_platform_show_uri_popup (link);
2656 }
2657
2658 void
2659 modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview, TnyMimePart *mime_part,
2660                                              ModestWindow *win)
2661 {
2662         /* we check for low-mem; in that case, show a warning, and don't allow
2663          * viewing attachments
2664          */
2665         if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
2666                 return;
2667
2668         modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (win), mime_part);
2669 }
2670
2671 void
2672 modest_ui_actions_on_msg_recpt_activated (ModestMsgView *msgview,
2673                                           const gchar *address,
2674                                           ModestWindow *win)
2675 {
2676         /* g_message ("%s %s", __FUNCTION__, address); */
2677 }
2678
2679 static void
2680 on_save_to_drafts_cb (ModestMailOperation *mail_op, 
2681                       TnyMsg *saved_draft,
2682                       gpointer user_data)
2683 {
2684         ModestMsgEditWindow *edit_window;
2685
2686         /* TODO: in hildon 2 we have to dim and undim the header views while we're saving */
2687 #ifndef MODEST_TOOLKIT_HILDON2
2688         ModestMainWindow *win;
2689
2690         /* FIXME. Make the header view sensitive again. This is a
2691          * temporary hack. See modest_ui_actions_on_save_to_drafts()
2692          * for details */
2693         win = MODEST_MAIN_WINDOW(modest_window_mgr_get_main_window(
2694                                          modest_runtime_get_window_mgr(), FALSE));
2695         if (win != NULL) {
2696                 GtkWidget *hdrview = modest_main_window_get_child_widget(
2697                         win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2698                 if (hdrview) gtk_widget_set_sensitive(hdrview, TRUE);
2699         }
2700 #endif
2701
2702         edit_window = MODEST_MSG_EDIT_WINDOW (user_data);
2703
2704         /* Set draft is there was no error */
2705         if (!modest_mail_operation_get_error (mail_op))
2706                 modest_msg_edit_window_set_draft (edit_window, saved_draft);
2707
2708         g_object_unref(edit_window);
2709 }
2710
2711 static gboolean
2712 enough_space_for_message (ModestMsgEditWindow *edit_window,
2713                           MsgData *data)
2714 {
2715         TnyAccountStore *acc_store;
2716         guint64 available_disk, expected_size;
2717         gint parts_count;
2718         guint64 parts_size;
2719
2720         /* Check size */
2721         acc_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store());
2722         available_disk = modest_utils_get_available_space (NULL);
2723         modest_msg_edit_window_get_parts_size (edit_window, &parts_count, &parts_size);
2724         expected_size = modest_tny_msg_estimate_size (data->plain_body,
2725                                                       data->html_body,
2726                                                       parts_count,
2727                                                       parts_size);
2728
2729         /* Double check: memory full condition or message too big */
2730         if (available_disk < MIN_FREE_SPACE || 
2731             expected_size > available_disk) {
2732
2733                 modest_platform_information_banner (NULL, NULL, 
2734                                                     dgettext("ke-recv", 
2735                                                              "cerm_device_memory_full"));
2736                 return FALSE;
2737         }
2738
2739         /*
2740          * djcb: if we're in low-memory state, we only allow for
2741          * saving messages smaller than
2742          * MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE (see modest-defs.h) this
2743          * should still allow for sending anything critical...
2744          */
2745         if ((expected_size > MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE) &&
2746             modest_platform_check_memory_low (MODEST_WINDOW(edit_window), TRUE))
2747                 return FALSE;
2748
2749         /*
2750          * djcb: we also make sure that the attachments are smaller than the max size
2751          * this is for the case where we'd try to forward a message with attachments 
2752          * bigger than our max allowed size, or sending an message from drafts which
2753          * somehow got past our checks when attaching.
2754          */
2755         if (expected_size > MODEST_MAX_ATTACHMENT_SIZE) {
2756                 modest_platform_run_information_dialog (
2757                         GTK_WINDOW(edit_window),
2758                         dgettext("ke-recv","memr_ib_operation_disabled"),
2759                         TRUE);
2760                 return FALSE;
2761         }
2762
2763         return TRUE;
2764 }
2765
2766 gboolean
2767 modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edit_window)
2768 {
2769         TnyTransportAccount *transport_account;
2770         ModestMailOperation *mail_operation;
2771         MsgData *data;
2772         gchar *account_name, *from;
2773         ModestAccountMgr *account_mgr;
2774         gboolean had_error = FALSE;
2775         ModestMainWindow *win = NULL;
2776
2777         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), FALSE);
2778         
2779         data = modest_msg_edit_window_get_msg_data (edit_window);
2780
2781         /* Check size */
2782         if (!enough_space_for_message (edit_window, data)) {
2783                 modest_msg_edit_window_free_msg_data (edit_window, data);
2784                 return FALSE;
2785         }
2786
2787         account_name = g_strdup (data->account_name);
2788         account_mgr = modest_runtime_get_account_mgr();
2789         if (!account_name)
2790                 account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
2791         if (!account_name) 
2792                 account_name = modest_account_mgr_get_default_account (account_mgr);
2793         if (!account_name) {
2794                 g_printerr ("modest: no account found\n");
2795                 modest_msg_edit_window_free_msg_data (edit_window, data);
2796                 return FALSE;
2797         }
2798
2799         if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
2800                 account_name = g_strdup (data->account_name);
2801         }
2802
2803         transport_account =
2804                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
2805                                       (modest_runtime_get_account_store (),
2806                                        account_name,
2807                                        TNY_ACCOUNT_TYPE_TRANSPORT));
2808         if (!transport_account) {
2809                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
2810                 g_free (account_name);
2811                 modest_msg_edit_window_free_msg_data (edit_window, data);
2812                 return FALSE;
2813         }
2814         from = modest_account_mgr_get_from_string (account_mgr, account_name);
2815
2816         /* Create the mail operation */         
2817         mail_operation = modest_mail_operation_new_with_error_handling (NULL, modest_ui_actions_disk_operations_error_handler,
2818                                                                         NULL, NULL);
2819         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
2820
2821         modest_mail_operation_save_to_drafts (mail_operation,
2822                                               transport_account,
2823                                               data->draft_msg,
2824                                               from,
2825                                               data->to, 
2826                                               data->cc, 
2827                                               data->bcc,
2828                                               data->subject, 
2829                                               data->plain_body, 
2830                                               data->html_body,
2831                                               data->attachments,
2832                                               data->images,
2833                                               data->priority_flags,
2834                                               on_save_to_drafts_cb,
2835                                               g_object_ref(edit_window));
2836
2837 #ifdef MODEST_TOOLKIT_HILDON2
2838         /* In hildon2 we always show the information banner on saving to drafts.
2839          * It will be a system information banner in this case.
2840          */
2841         gchar *text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
2842         modest_platform_information_banner (NULL, NULL, text);
2843         g_free (text);
2844 #else
2845         /* Use the main window as the parent of the banner, if the
2846            main window does not exist it won't be shown, if the parent
2847            window exists then it's properly shown. We don't use the
2848            editor window because it could be closed (save to drafts
2849            could happen after closing the window */
2850         win = (ModestMainWindow *)
2851                 modest_window_mgr_get_main_window( modest_runtime_get_window_mgr(), FALSE);
2852         if (win) {
2853                 gchar *text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
2854                 modest_platform_information_banner (GTK_WIDGET (win), NULL, text);
2855                 g_free (text);
2856         }
2857 #endif
2858         modest_msg_edit_window_set_modified (edit_window, FALSE);
2859
2860         /* Frees */
2861         g_free (from);
2862         g_free (account_name);
2863         g_object_unref (G_OBJECT (transport_account));
2864         g_object_unref (G_OBJECT (mail_operation));
2865
2866         modest_msg_edit_window_free_msg_data (edit_window, data);
2867
2868         /* ** FIXME **
2869          * If the drafts folder is selected then make the header view
2870          * insensitive while the message is being saved to drafts
2871          * (it'll be sensitive again in on_save_to_drafts_cb()). This
2872          * is not very clean but it avoids letting the drafts folder
2873          * in an inconsistent state: the user could edit the message
2874          * being saved and undesirable things would happen.
2875          * In the average case the user won't notice anything at
2876          * all. In the worst case (the user is editing a really big
2877          * file from Drafts) the header view will be insensitive
2878          * during the saving process (10 or 20 seconds, depending on
2879          * the message). Anyway this is just a quick workaround: once
2880          * we find a better solution it should be removed
2881          * See NB#65125 (commend #18) for details.
2882          */
2883         if (!had_error && win != NULL) {
2884                 ModestFolderView *view = MODEST_FOLDER_VIEW(modest_main_window_get_child_widget(
2885                         win, MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW));
2886                 if (view != NULL) {
2887                         TnyFolder *folder = TNY_FOLDER(modest_folder_view_get_selected(view));
2888                         if (folder) {
2889                                 if (modest_tny_folder_is_local_folder(folder)) {
2890                                         TnyFolderType folder_type;
2891                                         folder_type = modest_tny_folder_get_local_or_mmc_folder_type(folder);
2892                                         if (folder_type == TNY_FOLDER_TYPE_DRAFTS) {
2893                                                 GtkWidget *hdrview = modest_main_window_get_child_widget(
2894                                                         win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2895                                                 if (hdrview) gtk_widget_set_sensitive(hdrview, FALSE);
2896                                         }
2897                                 }
2898                         }
2899                         if (folder != NULL) g_object_unref(folder);
2900                 }
2901         }
2902
2903         return !had_error;
2904 }
2905
2906 /* For instance, when clicking the Send toolbar button when editing a message: */
2907 gboolean
2908 modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
2909 {
2910         TnyTransportAccount *transport_account = NULL;
2911         gboolean had_error = FALSE;
2912         MsgData *data;
2913         ModestAccountMgr *account_mgr;
2914         gchar *account_name;
2915         gchar *from;
2916         ModestMailOperation *mail_operation;
2917
2918         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), TRUE);
2919
2920         if (!modest_msg_edit_window_check_names (edit_window, TRUE))
2921                 return TRUE;
2922         
2923         data = modest_msg_edit_window_get_msg_data (edit_window);
2924
2925         /* Check size */
2926         if (!enough_space_for_message (edit_window, data)) {
2927                 modest_msg_edit_window_free_msg_data (edit_window, data);
2928                 return FALSE;
2929         }
2930
2931         account_mgr = modest_runtime_get_account_mgr();
2932         account_name = g_strdup (data->account_name);
2933         if (!account_name)
2934                 account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
2935
2936         if (!account_name) 
2937                 account_name = modest_account_mgr_get_default_account (account_mgr);
2938                 
2939         if (!account_name) {
2940                 modest_msg_edit_window_free_msg_data (edit_window, data);
2941                 /* Run account setup wizard */
2942                 if (!modest_ui_actions_run_account_setup_wizard (MODEST_WINDOW(edit_window))) {
2943                         return TRUE;
2944                 }
2945         }
2946         
2947         /* Get the currently-active transport account for this modest account: */
2948         if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) != 0) {
2949                 transport_account = 
2950                         TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
2951                                               (modest_runtime_get_account_store (), 
2952                                                account_name, TNY_ACCOUNT_TYPE_TRANSPORT));
2953         }
2954         
2955         if (!transport_account) {
2956                 modest_msg_edit_window_free_msg_data (edit_window, data);
2957                 /* Run account setup wizard */
2958                 if (!modest_ui_actions_run_account_setup_wizard(MODEST_WINDOW(edit_window)))
2959                         return TRUE;
2960         }
2961         
2962
2963         /* Create the mail operation */
2964         from = modest_account_mgr_get_from_string (account_mgr, account_name);
2965         mail_operation = modest_mail_operation_new_with_error_handling (NULL, modest_ui_actions_disk_operations_error_handler, NULL, NULL);
2966         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
2967
2968         modest_mail_operation_send_new_mail (mail_operation,
2969                                              transport_account,
2970                                              data->draft_msg,
2971                                              from,
2972                                              data->to,
2973                                              data->cc, 
2974                                              data->bcc,
2975                                              data->subject, 
2976                                              data->plain_body, 
2977                                              data->html_body,
2978                                              data->attachments,
2979                                              data->images,
2980                                              data->priority_flags);
2981
2982         if (modest_mail_operation_get_status (mail_operation) == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS)
2983                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_outbox_waiting_to_be_sent"));
2984
2985
2986         if (modest_mail_operation_get_error (mail_operation) != NULL) {
2987                 const GError *error = modest_mail_operation_get_error (mail_operation);
2988                 if (error->domain == MODEST_MAIL_OPERATION_ERROR &&
2989                     error->code == MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED) {
2990                         g_warning ("%s failed: %s\n", __FUNCTION__, (modest_mail_operation_get_error (mail_operation))->message);
2991                         modest_platform_information_banner (NULL, NULL, _CS("sfil_ni_not_enough_memory"));
2992                         had_error = TRUE;
2993                 }
2994         }
2995                                              
2996         /* Free data: */
2997         g_free (from);
2998         g_free (account_name);
2999         g_object_unref (G_OBJECT (transport_account));
3000         g_object_unref (G_OBJECT (mail_operation));
3001
3002         modest_msg_edit_window_free_msg_data (edit_window, data);
3003
3004         if (!had_error) {
3005                 modest_msg_edit_window_set_sent (edit_window, TRUE);
3006
3007                 /* Save settings and close the window: */
3008                 modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (edit_window));
3009         }
3010
3011         return !had_error;
3012 }
3013
3014 void 
3015 modest_ui_actions_on_toggle_bold (GtkToggleAction *action,
3016                                   ModestMsgEditWindow *window)
3017 {
3018         ModestMsgEditFormatState *format_state = NULL;
3019
3020         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3021         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
3022
3023         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3024                 return;
3025
3026         format_state = modest_msg_edit_window_get_format_state (window);
3027         g_return_if_fail (format_state != NULL);
3028
3029         format_state->bold = gtk_toggle_action_get_active (action);
3030         modest_msg_edit_window_set_format_state (window, format_state);
3031         g_free (format_state);
3032         
3033 }
3034
3035 void 
3036 modest_ui_actions_on_toggle_italics (GtkToggleAction *action,
3037                                      ModestMsgEditWindow *window)
3038 {
3039         ModestMsgEditFormatState *format_state = NULL;
3040
3041         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3042         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
3043
3044         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3045                 return;
3046
3047         format_state = modest_msg_edit_window_get_format_state (window);
3048         g_return_if_fail (format_state != NULL);
3049
3050         format_state->italics = gtk_toggle_action_get_active (action);
3051         modest_msg_edit_window_set_format_state (window, format_state);
3052         g_free (format_state);
3053         
3054 }
3055
3056 void 
3057 modest_ui_actions_on_toggle_bullets (GtkToggleAction *action,
3058                                      ModestMsgEditWindow *window)
3059 {
3060         ModestMsgEditFormatState *format_state = NULL;
3061
3062         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3063         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
3064
3065         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3066                 return;
3067
3068         format_state = modest_msg_edit_window_get_format_state (window);
3069         g_return_if_fail (format_state != NULL);
3070
3071         format_state->bullet = gtk_toggle_action_get_active (action);
3072         modest_msg_edit_window_set_format_state (window, format_state);
3073         g_free (format_state);
3074         
3075 }
3076
3077 void 
3078 modest_ui_actions_on_change_justify (GtkRadioAction *action,
3079                                      GtkRadioAction *selected,
3080                                      ModestMsgEditWindow *window)
3081 {
3082         ModestMsgEditFormatState *format_state = NULL;
3083         GtkJustification value;
3084
3085         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3086
3087         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3088                 return;
3089
3090         value = gtk_radio_action_get_current_value (selected);
3091
3092         format_state = modest_msg_edit_window_get_format_state (window);
3093         g_return_if_fail (format_state != NULL);
3094
3095         format_state->justification = value;
3096         modest_msg_edit_window_set_format_state (window, format_state);
3097         g_free (format_state);
3098 }
3099
3100 void 
3101 modest_ui_actions_on_select_editor_color (GtkAction *action,
3102                                           ModestMsgEditWindow *window)
3103 {
3104         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3105         g_return_if_fail (GTK_IS_ACTION (action));
3106
3107         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3108                 return;
3109
3110         modest_msg_edit_window_select_color (window);
3111 }
3112
3113 void 
3114 modest_ui_actions_on_select_editor_background_color (GtkAction *action,
3115                                                      ModestMsgEditWindow *window)
3116 {
3117         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3118         g_return_if_fail (GTK_IS_ACTION (action));
3119
3120         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3121                 return;
3122
3123         modest_msg_edit_window_select_background_color (window);
3124 }
3125
3126 void 
3127 modest_ui_actions_on_insert_image (GtkAction *action,
3128                                    ModestMsgEditWindow *window)
3129 {
3130         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3131         g_return_if_fail (GTK_IS_ACTION (action));
3132
3133
3134         if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
3135                 return;
3136
3137         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
3138                 return;
3139
3140         modest_msg_edit_window_insert_image (window);
3141 }
3142
3143 void 
3144 modest_ui_actions_on_attach_file (GtkAction *action,
3145                                   ModestMsgEditWindow *window)
3146 {
3147         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3148         g_return_if_fail (GTK_IS_ACTION (action));
3149
3150         if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
3151                 return;
3152         
3153         modest_msg_edit_window_offer_attach_file (window);
3154 }
3155
3156 void 
3157 modest_ui_actions_on_remove_attachments (GtkAction *action,
3158                                          ModestMsgEditWindow *window)
3159 {
3160         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3161         g_return_if_fail (GTK_IS_ACTION (action));
3162
3163         modest_msg_edit_window_remove_attachments (window, NULL);
3164 }
3165
3166
3167 #ifndef MODEST_TOOLKIT_GTK
3168 typedef struct {
3169         guint handler;
3170         gchar *name;
3171         GtkWindow *win;
3172         TnyFolderStore *folder;
3173 } CreateFolderHelper;
3174
3175 static gboolean
3176 show_create_folder_in_timeout (gpointer data)
3177 {
3178         CreateFolderHelper *helper = (CreateFolderHelper *) data;
3179
3180         /* Remove the timeout ASAP, we can not wait until the dialog
3181            is shown because it could take a lot of time and so the
3182            timeout could be called twice or more times */
3183         g_source_remove (helper->handler);
3184
3185         gdk_threads_enter ();
3186         do_create_folder (helper->win, helper->folder, helper->name);
3187         gdk_threads_leave ();
3188
3189         g_object_unref (helper->win);
3190         g_object_unref (helper->folder);
3191         g_free (helper->name);
3192         g_slice_free (CreateFolderHelper, helper);
3193
3194         return FALSE;
3195 }
3196 #endif
3197
3198 static void
3199 do_create_folder_cb (ModestMailOperation *mail_op,
3200                      TnyFolderStore *parent_folder, 
3201                      TnyFolder *new_folder,
3202                      gpointer user_data)
3203 {
3204         gchar *suggested_name = (gchar *) user_data;
3205         GtkWindow *source_win = (GtkWindow *) modest_mail_operation_get_source (mail_op);
3206
3207         if (modest_mail_operation_get_error (mail_op)) {
3208
3209                 /* Show an error. If there was some problem writing to
3210                    disk, show it, otherwise show the generic folder
3211                    create error. We do it here and not in an error
3212                    handler because the call to do_create_folder will
3213                    stop the main loop in a gtk_dialog_run and then,
3214                    the message won't be shown until that dialog is
3215                    closed */
3216                 modest_ui_actions_disk_operations_error_handler (mail_op,
3217                                                                  _("mail_in_ui_folder_create_error"));
3218
3219                 /* Try again. Do *NOT* show any error because the mail
3220                    operations system will do it for us because we
3221                    created the mail_op with new_with_error_handler */
3222 #ifndef MODEST_TOOLKIT_GTK
3223                 CreateFolderHelper *helper;
3224                 helper = g_slice_new0 (CreateFolderHelper);
3225                 helper->name = g_strdup (suggested_name);
3226                 helper->folder = g_object_ref (parent_folder);
3227                 helper->win = g_object_ref (source_win);
3228
3229                 /* Ugly but neccesary stuff. The problem is that the
3230                    dialog when is shown calls a function that destroys
3231                    all the temporary windows, so the banner is
3232                    destroyed */
3233                 helper->handler = g_timeout_add (2000, show_create_folder_in_timeout, helper);
3234 #else
3235                 do_create_folder (source_win, parent_folder, (const gchar *) suggested_name);
3236 #endif
3237         } else {
3238                 /* the 'source_win' is either the ModestMainWindow, or the 'Move to folder'-dialog
3239                  * FIXME: any other? */
3240                 GtkWidget *folder_view;
3241
3242                 if (MODEST_IS_MAIN_WINDOW(source_win)) 
3243                         folder_view = 
3244                                 modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (source_win),
3245                                                                      MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3246                 else
3247                         folder_view = GTK_WIDGET(g_object_get_data (G_OBJECT (source_win),
3248                                                                     MODEST_MOVE_TO_DIALOG_FOLDER_VIEW));
3249
3250                 /* Select the newly created folder. It could happen
3251                    that the widget is no longer there (i.e. the window
3252                    has been destroyed, so we need to check this */
3253                 if (folder_view)
3254                         modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view),
3255                                                           new_folder, FALSE);
3256                 g_object_unref (new_folder);
3257         }
3258         /* Free. Note that the first time it'll be NULL so noop */
3259         g_free (suggested_name);
3260         g_object_unref (source_win);
3261 }
3262
3263 static void
3264 do_create_folder (GtkWindow *parent_window, 
3265                   TnyFolderStore *suggested_parent, 
3266                   const gchar *suggested_name)
3267 {
3268         gint result;
3269         gchar *folder_name = NULL;
3270         TnyFolderStore *parent_folder;
3271
3272         result = modest_platform_run_new_folder_dialog (GTK_WINDOW (parent_window),
3273                                                         suggested_parent,
3274                                                         (gchar *) suggested_name,
3275                                                         &folder_name,
3276                                                         &parent_folder);
3277         
3278         if (result == GTK_RESPONSE_ACCEPT && parent_folder) {
3279                 ModestMailOperation *mail_op;
3280                 
3281                 mail_op  = modest_mail_operation_new ((GObject *) parent_window);
3282                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
3283                                                  mail_op);
3284                 modest_mail_operation_create_folder (mail_op,
3285                                                      parent_folder,
3286                                                      (const gchar *) folder_name,
3287                                                      do_create_folder_cb,
3288                                                      folder_name);
3289                 g_object_unref (mail_op);
3290         }
3291         g_object_unref (parent_folder);
3292 }
3293
3294 static void
3295 create_folder_performer (gboolean canceled, 
3296                          GError *err,
3297                          GtkWindow *parent_window, 
3298                          TnyAccount *account, 
3299                          gpointer user_data)
3300 {
3301         TnyFolderStore *parent_folder = TNY_FOLDER_STORE (user_data);
3302
3303         if (canceled || err) {
3304                 /* In memory full conditions we could get this error here */
3305                 check_memory_full_error ((GtkWidget *) parent_window, err);
3306                 goto frees;
3307         }
3308
3309         /* Run the new folder dialog */
3310         do_create_folder (GTK_WINDOW (parent_window), parent_folder, NULL);
3311
3312  frees:
3313         g_object_unref (parent_folder);
3314 }
3315
3316 static void
3317 modest_ui_actions_create_folder(GtkWidget *parent_window,
3318                                 GtkWidget *folder_view)
3319 {
3320         TnyFolderStore *parent_folder;
3321
3322 #ifdef MODEST_TOOLKIT_HILDON2
3323         const gchar *active_account;
3324         TnyAccount *account;
3325
3326         /* In hildon 2.2 we use the current account as default parent */
3327         active_account = modest_window_get_active_account (MODEST_WINDOW (parent_window));
3328         account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
3329                                                                active_account,
3330                                                                TNY_ACCOUNT_TYPE_STORE);
3331         parent_folder = TNY_FOLDER_STORE (account);
3332 #else
3333         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
3334 #endif
3335         
3336         if (parent_folder) {
3337                 /* The parent folder will be freed in the callback */
3338                 modest_platform_connect_if_remote_and_perform (GTK_WINDOW (parent_window), 
3339                                                                TRUE,
3340                                                                parent_folder,
3341                                                                create_folder_performer, 
3342                                                                parent_folder);
3343         }
3344 #ifdef MODEST_TOOLKIT_HILDON2
3345         g_object_unref (parent_folder);
3346 #endif
3347 }
3348
3349 void 
3350 modest_ui_actions_on_new_folder (GtkAction *action, ModestWindow *window)
3351 {
3352         
3353         g_return_if_fail (MODEST_IS_WINDOW(window));
3354
3355         if (MODEST_IS_MAIN_WINDOW (window)) {
3356                 GtkWidget *folder_view;
3357
3358                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
3359                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3360                 if (!folder_view)
3361                         return;
3362
3363                 modest_ui_actions_create_folder (GTK_WIDGET (window), folder_view);
3364 #ifdef MODEST_TOOLKIT_HILDON2
3365         } else if (MODEST_IS_FOLDER_WINDOW (window)) {
3366                 GtkWidget *folder_view;
3367
3368                 folder_view = GTK_WIDGET (modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (window)));
3369                 modest_ui_actions_create_folder (GTK_WIDGET (window), folder_view);
3370 #endif
3371         } else {
3372                 g_assert_not_reached ();
3373         }
3374 }
3375
3376 static void
3377 modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
3378                                                gpointer user_data)
3379 {
3380         const GError *error = NULL;
3381         const gchar *message = NULL;
3382         
3383         /* Get error message */
3384         error = modest_mail_operation_get_error (mail_op);
3385         if (!error)
3386                 g_return_if_reached ();
3387
3388         if (error->domain == MODEST_MAIL_OPERATION_ERROR &&
3389             error->code == MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS) {
3390                 message = _CS("ckdg_ib_folder_already_exists");
3391         } else if (error->domain == TNY_ERROR_DOMAIN &&
3392                    error->code == TNY_SERVICE_ERROR_STATE) {
3393                 /* This means that the folder is already in use (a
3394                    message is opened for example */
3395                 message = _("emev_ni_internal_error");
3396         } else {
3397                 message = _("emev_ib_ui_imap_unable_to_rename");
3398         }
3399
3400         /* We don't set a parent for the dialog because the dialog
3401            will be destroyed so the banner won't appear */
3402         modest_platform_information_banner (NULL, NULL, message);
3403 }
3404
3405 typedef struct {
3406         TnyFolderStore *folder;
3407         gchar *new_name;
3408 } RenameFolderInfo;
3409
3410 static void
3411 on_rename_folder_cb (ModestMailOperation *mail_op, 
3412                      TnyFolder *new_folder,
3413                      gpointer user_data)
3414 {
3415         ModestFolderView *folder_view;
3416
3417         /* If the window was closed when renaming a folder, or if
3418          * it's not a main window this will happen */
3419         if (!MODEST_IS_FOLDER_VIEW (user_data))
3420                 return;
3421
3422         folder_view = MODEST_FOLDER_VIEW (user_data);
3423         /* Note that if the rename fails new_folder will be NULL */
3424         if (new_folder) {
3425                 modest_folder_view_select_folder (folder_view, new_folder, FALSE);
3426         } else {
3427                 modest_folder_view_select_first_inbox_or_local (folder_view);
3428         }
3429         gtk_widget_grab_focus (GTK_WIDGET (folder_view));       
3430 }
3431
3432 static void
3433 on_rename_folder_performer (gboolean canceled, 
3434                             GError *err, 
3435                             GtkWindow *parent_window, 
3436                             TnyAccount *account, 
3437                             gpointer user_data)
3438 {
3439         ModestMailOperation *mail_op = NULL;
3440         GtkTreeSelection *sel = NULL;
3441         GtkWidget *folder_view = NULL;
3442         RenameFolderInfo *data = (RenameFolderInfo*)user_data;
3443
3444         if (canceled || err) {
3445                 /* In memory full conditions we could get this error here */
3446                 check_memory_full_error ((GtkWidget *) parent_window, err);
3447         } else {
3448
3449                 mail_op = 
3450                         modest_mail_operation_new_with_error_handling (G_OBJECT(parent_window),
3451                                         modest_ui_actions_rename_folder_error_handler,
3452                                         parent_window, NULL);
3453
3454                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
3455                                 mail_op);
3456
3457                 if (MODEST_IS_MAIN_WINDOW(parent_window)) {
3458
3459                         folder_view = modest_main_window_get_child_widget (
3460                                 MODEST_MAIN_WINDOW (parent_window),
3461                                 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3462
3463                         /* Clear the headers view */
3464                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view));
3465                         gtk_tree_selection_unselect_all (sel);
3466                 } else {
3467                         folder_view = NULL;
3468                 }
3469
3470                 /* Actually rename the folder */
3471                 modest_mail_operation_rename_folder (mail_op,
3472                                                      TNY_FOLDER (data->folder),
3473                                                      (const gchar *) (data->new_name),
3474                                                      on_rename_folder_cb,
3475                                                      folder_view);
3476                 g_object_unref (data->folder);
3477                 g_object_unref (mail_op);
3478         }
3479
3480         g_free (data->new_name);
3481         g_free (data);
3482 }
3483
3484 void 
3485 modest_ui_actions_on_rename_folder (GtkAction *action,
3486                                      ModestWindow *window)
3487 {
3488         modest_ui_actions_on_edit_mode_rename_folder (window);
3489 }
3490
3491 gboolean 
3492 modest_ui_actions_on_edit_mode_rename_folder (ModestWindow *window)
3493 {
3494         TnyFolderStore *folder;
3495         GtkWidget *folder_view;
3496         gboolean do_rename = TRUE;
3497
3498         g_return_val_if_fail (MODEST_IS_WINDOW(window), FALSE);
3499
3500         if (MODEST_IS_MAIN_WINDOW (window)) {
3501                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
3502                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3503                 if (!folder_view)
3504                         return FALSE;
3505
3506 #ifdef MODEST_TOOLKIT_HILDON2
3507         } else if (MODEST_IS_FOLDER_WINDOW (window)) {
3508                 folder_view = GTK_WIDGET (modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (window)));
3509 #endif
3510         }
3511
3512         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
3513
3514         if (!folder)
3515                 return FALSE;
3516
3517         if (TNY_IS_FOLDER (folder)) {
3518                 gchar *folder_name = NULL;
3519                 gint response;
3520                 const gchar *current_name;
3521                 TnyFolderStore *parent;
3522
3523                 current_name = tny_folder_get_name (TNY_FOLDER (folder));
3524                 parent = tny_folder_get_folder_store (TNY_FOLDER (folder));
3525                 response = modest_platform_run_rename_folder_dialog (GTK_WINDOW (window),
3526                                                                      parent, current_name, 
3527                                                                      &folder_name);
3528                 g_object_unref (parent);
3529
3530                 if (response != GTK_RESPONSE_ACCEPT || strlen (folder_name) == 0) {
3531                         do_rename = FALSE;
3532                 } else {
3533                         RenameFolderInfo *rename_folder_data = g_new0 (RenameFolderInfo, 1);
3534                         rename_folder_data->folder = g_object_ref (folder);
3535                         rename_folder_data->new_name = folder_name;
3536                         modest_platform_connect_if_remote_and_perform (GTK_WINDOW(window), TRUE,
3537                                         folder, on_rename_folder_performer, rename_folder_data);
3538                 }
3539         }
3540         g_object_unref (folder);
3541         return do_rename;
3542 }
3543
3544 static void
3545 modest_ui_actions_delete_folder_error_handler (ModestMailOperation *mail_op,
3546                                                gpointer user_data)
3547 {
3548         GObject *win = modest_mail_operation_get_source (mail_op);
3549
3550         modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
3551                                                 _("mail_in_ui_folder_delete_error"),
3552                                                 FALSE);
3553         g_object_unref (win);
3554 }
3555
3556 typedef struct {
3557         TnyFolderStore *folder;
3558         gboolean move_to_trash;
3559 } DeleteFolderInfo;
3560
3561 static void
3562 on_delete_folder_cb (gboolean canceled, 
3563                   GError *err,
3564                   GtkWindow *parent_window, 
3565                   TnyAccount *account, 
3566                   gpointer user_data)
3567 {
3568         DeleteFolderInfo *info = (DeleteFolderInfo*) user_data;
3569         GtkWidget *folder_view;
3570         ModestMailOperation *mail_op;
3571         GtkTreeSelection *sel;
3572         
3573         if (!MODEST_IS_WINDOW(parent_window) || canceled || (err!=NULL)) {
3574                 g_object_unref (G_OBJECT (info->folder));
3575                 g_free (info);
3576                 return;
3577         }
3578         
3579         if (MODEST_IS_MAIN_WINDOW (parent_window)) {
3580                 folder_view = modest_main_window_get_child_widget (
3581                         MODEST_MAIN_WINDOW (parent_window),
3582                         MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3583 #ifdef MODEST_TOOLKIT_HILDON2
3584         } else if (MODEST_IS_FOLDER_WINDOW (parent_window)) {
3585                 folder_view = GTK_WIDGET (modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (parent_window)));
3586 #endif
3587         }
3588
3589         /* Unselect the folder before deleting it to free the headers */
3590         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view));
3591         gtk_tree_selection_unselect_all (sel);
3592
3593         /* Create the mail operation */
3594         mail_op =
3595                 modest_mail_operation_new_with_error_handling (G_OBJECT(parent_window),
3596                                 modest_ui_actions_delete_folder_error_handler,
3597                                 NULL, NULL);
3598
3599         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
3600                         mail_op);
3601         modest_mail_operation_remove_folder (mail_op, TNY_FOLDER (info->folder), info->move_to_trash);
3602         
3603         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
3604
3605         g_object_unref (G_OBJECT (mail_op));
3606         g_object_unref (G_OBJECT (info->folder));
3607         g_free (info);
3608 }
3609
3610 static gboolean
3611 delete_folder (ModestWindow *window, gboolean move_to_trash)
3612 {
3613         TnyFolderStore *folder;
3614         GtkWidget *folder_view;
3615         gint response;
3616         gchar *message;
3617         
3618         g_return_val_if_fail (MODEST_IS_WINDOW(window), FALSE);
3619
3620         if (MODEST_IS_MAIN_WINDOW (window)) {
3621
3622                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
3623                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3624 #ifdef MODEST_TOOLKIT_HILDON2
3625         } else if (MODEST_IS_FOLDER_WINDOW (window)) {
3626                 folder_view = GTK_WIDGET (modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (window)));
3627 #endif
3628         }
3629         if (!folder_view)
3630                 return FALSE;
3631
3632         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
3633
3634         /* Show an error if it's an account */
3635         if (!TNY_IS_FOLDER (folder)) {
3636                 modest_platform_run_information_dialog (GTK_WINDOW (window),
3637                                                         _("mail_in_ui_folder_delete_error"),
3638                                                         FALSE);
3639                 g_object_unref (G_OBJECT (folder));
3640                 return FALSE;
3641         }
3642
3643         /* Ask the user */      
3644         message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
3645                                     tny_folder_get_name (TNY_FOLDER (folder)));
3646         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
3647                                                             (const gchar *) message);
3648         g_free (message);
3649
3650         if (response == GTK_RESPONSE_OK) {
3651                 DeleteFolderInfo *info;
3652                 info = g_new0(DeleteFolderInfo, 1);
3653                 info->folder = folder;
3654                 info->move_to_trash = move_to_trash;
3655                 g_object_ref (G_OBJECT (info->folder));
3656                 TnyAccount *account = tny_folder_get_account (TNY_FOLDER (folder));
3657                 modest_platform_connect_if_remote_and_perform (GTK_WINDOW (window), 
3658                                                                TRUE,
3659                                                                TNY_FOLDER_STORE (account), 
3660                                                                on_delete_folder_cb, info);
3661                 g_object_unref (account);
3662                 return TRUE;
3663         } else {
3664                 return FALSE;
3665         }
3666         g_object_unref (G_OBJECT (folder));
3667 }
3668
3669 void
3670 modest_ui_actions_on_delete_folder (GtkAction *action,
3671                                     ModestWindow *window)
3672 {
3673         modest_ui_actions_on_edit_mode_delete_folder (window);
3674 }
3675
3676 gboolean
3677 modest_ui_actions_on_edit_mode_delete_folder (ModestWindow *window)
3678 {
3679         g_return_val_if_fail (MODEST_IS_WINDOW(window), TRUE);
3680         
3681         return delete_folder (window, FALSE);
3682 }
3683
3684 void 
3685 modest_ui_actions_on_move_folder_to_trash_folder (GtkAction *action, ModestMainWindow *main_window)
3686 {
3687         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
3688         
3689         delete_folder (MODEST_WINDOW (main_window), TRUE);
3690 }
3691
3692
3693 typedef struct _PasswordDialogFields {
3694         GtkWidget *username;
3695         GtkWidget *password;
3696         GtkWidget *dialog;
3697 } PasswordDialogFields;
3698
3699 static void
3700 password_dialog_check_field (GtkEditable *editable,
3701                              PasswordDialogFields *fields)
3702 {
3703         const gchar *value;
3704         gboolean any_value_empty = FALSE;
3705
3706         value = gtk_entry_get_text (GTK_ENTRY (fields->username));
3707         if ((value == NULL) || value[0] == '\0') {
3708                 any_value_empty = TRUE;
3709         }
3710         value = gtk_entry_get_text (GTK_ENTRY (fields->password));
3711         if ((value == NULL) || value[0] == '\0') {
3712                 any_value_empty = TRUE;
3713         }
3714         gtk_dialog_set_response_sensitive (GTK_DIALOG (fields->dialog), GTK_RESPONSE_ACCEPT, !any_value_empty);
3715 }
3716
3717 void
3718 modest_ui_actions_on_password_requested (TnyAccountStore *account_store,
3719                                          const gchar* server_account_name,
3720                                          gchar **username,
3721                                          gchar **password,
3722                                          gboolean *cancel,
3723                                          gboolean *remember,
3724                                          ModestMainWindow *main_window)
3725 {
3726         g_return_if_fail(server_account_name);
3727         gboolean completed = FALSE;
3728         PasswordDialogFields *fields = NULL;
3729
3730         /* Initalize output parameters: */
3731         if (cancel)
3732                 *cancel = FALSE;
3733
3734         if (remember)
3735                 *remember = TRUE;
3736
3737 #ifndef MODEST_TOOLKIT_GTK
3738         /* Maemo uses a different (awkward) button order,
3739          * It should probably just use gtk_alternative_dialog_button_order ().
3740          */
3741 #ifdef MODEST_TOOLKIT_HILDON2
3742         GtkWidget *dialog =
3743                 gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
3744                                              NULL,
3745                                              GTK_DIALOG_MODAL,
3746                                              _HL("wdgt_bd_done"),
3747                                              GTK_RESPONSE_ACCEPT,
3748                                              NULL);
3749 #else
3750         GtkWidget *dialog =
3751                 gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
3752                                              NULL,
3753                                              GTK_DIALOG_MODAL,
3754                                              _("mcen_bd_dialog_ok"),
3755                                              GTK_RESPONSE_ACCEPT,
3756                                              _("mcen_bd_dialog_cancel"),
3757                                              GTK_RESPONSE_REJECT,
3758                                              NULL);
3759 #endif /* MODEST_TOOLKIT_HILDON2 */
3760 #else
3761         GtkWidget *dialog = 
3762                 gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
3763                                              NULL,
3764                                              GTK_DIALOG_MODAL,
3765                                              GTK_STOCK_CANCEL,
3766                                              GTK_RESPONSE_REJECT,
3767                                              GTK_STOCK_OK,
3768                                              GTK_RESPONSE_ACCEPT,
3769                                              NULL);
3770 #endif /* MODEST_TOOLKIT_GTK */
3771
3772         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(), GTK_WINDOW (dialog), NULL);
3773
3774         gchar *server_name = modest_account_mgr_get_server_account_hostname (
3775                 modest_runtime_get_account_mgr(), server_account_name);
3776         if (!server_name) {/* This happened once, though I don't know why. murrayc. */
3777                 g_warning("%s: Could not get server name for server account '%s'", __FUNCTION__, server_account_name);
3778                 if (cancel)
3779                         *cancel = TRUE;
3780                 gtk_widget_destroy (dialog);
3781                 return;
3782         }
3783
3784         gchar *txt = g_strdup_printf (_("mail_ia_password_info"), server_name);
3785         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), gtk_label_new(txt),
3786                             FALSE, FALSE, 0);
3787         g_free (txt);
3788         g_free (server_name);
3789         server_name = NULL;
3790
3791         /* username: */
3792         gchar *initial_username = modest_account_mgr_get_server_account_username (
3793                 modest_runtime_get_account_mgr(), server_account_name);
3794
3795         GtkWidget *entry_username = gtk_entry_new ();
3796         if (initial_username)
3797                 gtk_entry_set_text (GTK_ENTRY (entry_username), initial_username);
3798         /* Dim this if a connection has ever succeeded with this username,
3799          * as per the UI spec: */
3800         /* const gboolean username_known =  */
3801         /*      modest_account_mgr_get_server_account_username_has_succeeded( */
3802         /*              modest_runtime_get_account_mgr(), server_account_name); */
3803         /* gtk_widget_set_sensitive (entry_username, !username_known); */
3804
3805         /* We drop the username sensitive code and disallow changing it here
3806          * as tinymail does not support really changing the username in the callback
3807          */
3808         gtk_widget_set_sensitive (entry_username, FALSE);
3809
3810 #ifndef MODEST_TOOLKIT_GTK
3811         /* Auto-capitalization is the default, so let's turn it off: */
3812         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_username), HILDON_GTK_INPUT_MODE_FULL);
3813
3814         /* Create a size group to be used by all captions.
3815          * Note that HildonCaption does not create a default size group if we do not specify one.
3816          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
3817         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
3818
3819         GtkWidget *caption = hildon_caption_new (sizegroup, 
3820                 _("mail_fi_username"), entry_username, NULL, HILDON_CAPTION_MANDATORY);
3821         gtk_widget_show (entry_username);
3822         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption, 
3823                 FALSE, FALSE, MODEST_MARGIN_HALF);
3824         gtk_widget_show (caption);
3825 #else
3826         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_username,
3827                             TRUE, FALSE, 0);
3828 #endif /* !MODEST_TOOLKIT_GTK */
3829
3830         /* password: */
3831         GtkWidget *entry_password = gtk_entry_new ();
3832         gtk_entry_set_visibility (GTK_ENTRY(entry_password), FALSE);
3833         /* gtk_entry_set_invisible_char (GTK_ENTRY(entry_password), "*"); */
3834
3835 #ifndef MODEST_TOOLKIT_GTK
3836         /* Auto-capitalization is the default, so let's turn it off: */
3837         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_password),
3838                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
3839
3840         caption = hildon_caption_new (sizegroup,
3841                 _("mail_fi_password"), entry_password, NULL, HILDON_CAPTION_MANDATORY);
3842         gtk_widget_show (entry_password);
3843         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption,
3844                 FALSE, FALSE, MODEST_MARGIN_HALF);
3845         gtk_widget_show (caption);
3846         g_object_unref (sizegroup);
3847 #else
3848         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_password,
3849                             TRUE, FALSE, 0);
3850 #endif /* !MODEST_TOOLKIT_GTK */
3851
3852         if (initial_username != NULL)
3853                 gtk_widget_grab_focus (GTK_WIDGET (entry_password));
3854
3855 /* This is not in the Maemo UI spec:
3856         remember_pass_check = gtk_check_button_new_with_label (_("Remember password"));
3857         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), remember_pass_check,
3858                             TRUE, FALSE, 0);
3859 */
3860
3861         fields = g_slice_new0 (PasswordDialogFields);
3862         fields->username = entry_username;
3863         fields->password = entry_password;
3864         fields->dialog = dialog;
3865
3866         g_signal_connect (entry_username, "changed", G_CALLBACK (password_dialog_check_field), fields);
3867         g_signal_connect (entry_password, "changed", G_CALLBACK (password_dialog_check_field), fields);
3868         password_dialog_check_field (NULL, fields);
3869
3870         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
3871
3872         while (!completed) {
3873
3874                 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
3875                         if (username) {
3876                                 *username = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_username)));
3877
3878                                 /* Note that an empty field becomes the "" string */
3879                                 if (*username && strlen (*username) > 0) {
3880                                         modest_account_mgr_set_server_account_username (modest_runtime_get_account_mgr(), 
3881                                                                                         server_account_name, 
3882                                                                                         *username);
3883                                         completed = TRUE;
3884
3885                                         const gboolean username_was_changed = 
3886                                                 (strcmp (*username, initial_username) != 0);
3887                                         if (username_was_changed) {
3888                                                 g_warning ("%s: tinymail does not yet support changing the "
3889                                                            "username in the get_password() callback.\n", __FUNCTION__);
3890                                         }
3891                                 } else {
3892                                         g_free (*username);
3893                                         *username = NULL;
3894                                         /* Show error */
3895                                         modest_platform_information_banner (GTK_WIDGET (dialog), NULL, 
3896                                                                             _("mcen_ib_username_pw_incorrect"));
3897                                         completed = FALSE;
3898                                 }
3899                         }
3900
3901                         if (password) {
3902                                 *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_password)));
3903
3904                                 /* We do not save the password in the configuration, 
3905                                  * because this function is only called for passwords that should 
3906                                  * not be remembered:
3907                                  modest_server_account_set_password (
3908                                  modest_runtime_get_account_mgr(), server_account_name, 
3909                                  *password);
3910                                  */
3911                         }
3912                         if (cancel)
3913                                 *cancel   = FALSE;
3914                 } else {
3915 #ifndef MODEST_TOOLKIT_HILDON2
3916                         /* Set parent to NULL or the banner will disappear with its parent dialog */
3917                         modest_platform_information_banner(NULL, NULL, _("mail_ib_login_cancelled"));
3918 #endif
3919                         completed = TRUE;
3920                         if (username)
3921                                 *username = NULL;
3922                         if (password)
3923                                 *password = NULL;
3924                         if (cancel)
3925                                 *cancel   = TRUE;
3926                 }
3927         }
3928
3929 /* This is not in the Maemo UI spec:
3930         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (remember_pass_check)))
3931                 *remember = TRUE;
3932         else
3933                 *remember = FALSE;
3934 */
3935
3936         g_free (initial_username);
3937         gtk_widget_destroy (dialog);
3938         g_slice_free (PasswordDialogFields, fields);
3939         
3940         /* printf ("DEBUG: %s: cancel=%d\n", __FUNCTION__, *cancel); */
3941 }
3942
3943 void
3944 modest_ui_actions_on_cut (GtkAction *action,
3945                           ModestWindow *window)
3946 {
3947         GtkWidget *focused_widget;
3948         GtkClipboard *clipboard;
3949
3950         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3951         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
3952         if (GTK_IS_EDITABLE (focused_widget)) {
3953                 gtk_editable_cut_clipboard (GTK_EDITABLE(focused_widget));
3954                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3955                 gtk_clipboard_store (clipboard);
3956         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
3957                 GtkTextBuffer *buffer;
3958
3959                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
3960                 if (modest_text_utils_buffer_selection_is_valid (buffer)) {
3961                         gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3962                         gtk_clipboard_set_can_store (clipboard, NULL, 0);
3963                         gtk_clipboard_store (clipboard);
3964                 }
3965         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
3966                 TnyList *header_list = modest_header_view_get_selected_headers (
3967                                 MODEST_HEADER_VIEW (focused_widget));
3968                 gboolean continue_download = FALSE;
3969                 gint num_of_unc_msgs;
3970
3971                 num_of_unc_msgs = header_list_count_uncached_msgs(header_list);
3972
3973                 if (num_of_unc_msgs) {
3974                         TnyAccount *account = get_account_from_header_list (header_list);
3975                         if (account) {
3976                                 continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
3977                                 g_object_unref (account);
3978                         }
3979                 }
3980
3981                 if (num_of_unc_msgs == 0 || continue_download) {
3982 /*                      modest_platform_information_banner (
3983                                         NULL, NULL, _CS("mcen_ib_getting_items"));*/
3984                         modest_header_view_cut_selection (
3985                                         MODEST_HEADER_VIEW (focused_widget));
3986                 }
3987
3988                 g_object_unref (header_list);
3989         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
3990                 modest_folder_view_cut_selection (MODEST_FOLDER_VIEW (focused_widget));
3991         }
3992 }
3993
3994 void
3995 modest_ui_actions_on_copy (GtkAction *action,
3996                            ModestWindow *window)
3997 {
3998         GtkClipboard *clipboard;
3999         GtkWidget *focused_widget;
4000         gboolean copied = TRUE;
4001
4002         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4003         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
4004
4005         if (GTK_IS_LABEL (focused_widget)) {
4006                 gchar *selection;
4007                 selection = modest_text_utils_label_get_selection (GTK_LABEL (focused_widget));
4008                 gtk_clipboard_set_text (clipboard, selection, -1);
4009                 g_free (selection);
4010                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
4011                 gtk_clipboard_store (clipboard);
4012         } else if (GTK_IS_EDITABLE (focused_widget)) {
4013                 gtk_editable_copy_clipboard (GTK_EDITABLE(focused_widget));
4014                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
4015                 gtk_clipboard_store (clipboard);
4016         } else if (GTK_IS_HTML (focused_widget)) {
4017                 const gchar *sel;
4018                 int len = -1;
4019                 sel = gtk_html_get_selection_html (GTK_HTML (focused_widget), &len);
4020                 if ((sel == NULL) || (sel[0] == '\0')) {
4021                         copied = FALSE;
4022                 } else {
4023                         gtk_html_copy (GTK_HTML (focused_widget));
4024                         gtk_clipboard_set_can_store (clipboard, NULL, 0);
4025                         gtk_clipboard_store (clipboard);
4026                 }
4027         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
4028                 GtkTextBuffer *buffer;
4029                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
4030                 if (modest_text_utils_buffer_selection_is_valid (buffer)) {
4031                         gtk_text_buffer_copy_clipboard (buffer, clipboard);
4032                         gtk_clipboard_set_can_store (clipboard, NULL, 0);
4033                         gtk_clipboard_store (clipboard);
4034                 }
4035         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
4036                 TnyList *header_list = modest_header_view_get_selected_headers (
4037                                 MODEST_HEADER_VIEW (focused_widget));
4038                 gboolean continue_download = FALSE;
4039                 gint num_of_unc_msgs;
4040
4041                 num_of_unc_msgs = header_list_count_uncached_msgs(header_list);
4042
4043                 if (num_of_unc_msgs) {
4044                         TnyAccount *account = get_account_from_header_list (header_list);
4045                         if (account) {
4046                                 continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
4047                                 g_object_unref (account);
4048                         }
4049                 }
4050
4051                 if (num_of_unc_msgs == 0 || continue_download) {
4052                         modest_platform_information_banner (
4053                                         NULL, NULL, _CS("mcen_ib_getting_items"));
4054                         modest_header_view_copy_selection (
4055                                         MODEST_HEADER_VIEW (focused_widget));
4056                 } else
4057                         copied = FALSE;
4058
4059                 g_object_unref (header_list);
4060
4061         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
4062                 modest_folder_view_copy_selection (MODEST_FOLDER_VIEW (focused_widget));
4063         }
4064
4065         /* Show information banner if there was a copy to clipboard */
4066         if(copied)
4067                 modest_platform_information_banner (
4068                                 NULL, NULL, _CS("ecoc_ib_edwin_copied"));
4069 }
4070
4071 void
4072 modest_ui_actions_on_undo (GtkAction *action,
4073                            ModestWindow *window)
4074 {
4075         ModestEmailClipboard *clipboard = NULL;
4076
4077         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
4078                 modest_msg_edit_window_undo (MODEST_MSG_EDIT_WINDOW (window));
4079         } else if (MODEST_IS_MAIN_WINDOW (window)) {
4080                 /* Clear clipboard source */
4081                 clipboard = modest_runtime_get_email_clipboard ();
4082                 modest_email_clipboard_clear (clipboard);               
4083         }
4084         else {
4085                 g_return_if_reached ();
4086         }
4087 }
4088
4089 void
4090 modest_ui_actions_on_redo (GtkAction *action,
4091                            ModestWindow *window)
4092 {
4093         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
4094                 modest_msg_edit_window_redo (MODEST_MSG_EDIT_WINDOW (window));
4095         }
4096         else {
4097                 g_return_if_reached ();
4098         }
4099 }
4100
4101
4102 static void
4103 destroy_information_note (ModestMailOperation *mail_op, 
4104                           gpointer user_data)
4105 {
4106         /* destroy information note */
4107         gtk_widget_destroy (GTK_WIDGET(user_data));
4108 }
4109
4110 static void
4111 destroy_folder_information_note (ModestMailOperation *mail_op, 
4112                                  TnyFolder *new_folder,
4113                                  gpointer user_data)
4114 {
4115         /* destroy information note */
4116         gtk_widget_destroy (GTK_WIDGET(user_data));
4117 }
4118
4119
4120 static void
4121 paste_as_attachment_free (gpointer data)
4122 {
4123         PasteAsAttachmentHelper *helper = (PasteAsAttachmentHelper *) data;
4124
4125         if (helper->banner) {
4126                 gtk_widget_destroy (helper->banner);
4127                 g_object_unref (helper->banner);
4128         }
4129         g_free (helper);
4130 }
4131
4132 static void
4133 paste_msg_as_attachment_cb (ModestMailOperation *mail_op,
4134                             TnyHeader *header,
4135                             TnyMsg *msg,
4136                             gpointer userdata)
4137 {
4138         PasteAsAttachmentHelper *helper = (PasteAsAttachmentHelper *) userdata;
4139         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (helper->window));
4140
4141         if (msg == NULL)
4142                 return;
4143
4144         modest_msg_edit_window_add_part (MODEST_MSG_EDIT_WINDOW (helper->window), TNY_MIME_PART (msg));
4145         
4146 }
4147
4148 void
4149 modest_ui_actions_on_paste (GtkAction *action,
4150                             ModestWindow *window)
4151 {
4152         GtkWidget *focused_widget = NULL;
4153         GtkWidget *inf_note = NULL;
4154         ModestMailOperation *mail_op = NULL;
4155
4156         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
4157         if (GTK_IS_EDITABLE (focused_widget)) {
4158                 gtk_editable_paste_clipboard (GTK_EDITABLE(focused_widget));
4159         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
4160                 ModestEmailClipboard *e_clipboard = NULL;
4161                 e_clipboard = modest_runtime_get_email_clipboard ();
4162                 if (modest_email_clipboard_cleared (e_clipboard)) {
4163                         GtkTextBuffer *buffer;
4164                         GtkClipboard *clipboard;
4165
4166                         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4167                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
4168                         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4169                 } else if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
4170                         ModestMailOperation *mail_op;
4171                         TnyFolder *src_folder = NULL;
4172                         TnyList *data = NULL;
4173                         gboolean delete;
4174                         PasteAsAttachmentHelper *helper = g_new0 (PasteAsAttachmentHelper, 1);
4175                         helper->window = MODEST_MSG_EDIT_WINDOW (window);
4176                         helper->banner = modest_platform_animation_banner (GTK_WIDGET (window), NULL,
4177                                                                            _CS("ckct_nw_pasting"));
4178                         modest_email_clipboard_get_data (e_clipboard, &src_folder, &data, &delete);
4179                         mail_op = modest_mail_operation_new (G_OBJECT (window));
4180                         if (helper->banner != NULL) {
4181                                 g_object_ref (G_OBJECT (helper->banner));
4182                                 gtk_widget_show (GTK_WIDGET (helper->banner));
4183                         }
4184
4185                         if (data != NULL) {
4186                                 modest_mail_operation_get_msgs_full (mail_op, 
4187                                                                      data,
4188                                                                      (GetMsgAsyncUserCallback) paste_msg_as_attachment_cb,
4189                                                                      helper,
4190                                                                      paste_as_attachment_free);
4191                         }
4192                         /* Free */
4193                         if (data) 
4194                                 g_object_unref (data);
4195                         if (src_folder) 
4196                                 g_object_unref (src_folder);
4197
4198                 }
4199         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
4200                 ModestEmailClipboard *clipboard = NULL;
4201                 TnyFolder *src_folder = NULL;
4202                 TnyFolderStore *folder_store = NULL;
4203                 TnyList *data = NULL;           
4204                 gboolean delete = FALSE;
4205                 
4206                 /* Check clipboard source */
4207                 clipboard = modest_runtime_get_email_clipboard ();
4208                 if (modest_email_clipboard_cleared (clipboard)) 
4209                         return;
4210                 
4211                 /* Get elements to paste */
4212                 modest_email_clipboard_get_data (clipboard, &src_folder, &data, &delete);
4213
4214                 /* Create a new mail operation */
4215                 mail_op = modest_mail_operation_new (G_OBJECT(window));
4216                 
4217                 /* Get destination folder */
4218                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (focused_widget));
4219
4220                 /* transfer messages  */
4221                 if (data != NULL) {
4222                         gint response = 0;
4223
4224                         /* Ask for user confirmation */
4225                         response = 
4226                                 modest_ui_actions_msgs_move_to_confirmation (window, 
4227                                                                              TNY_FOLDER (folder_store), 
4228                                                                              delete,
4229                                                                              data);
4230                         
4231                         if (response == GTK_RESPONSE_OK) {
4232                                 /* Launch notification */
4233                                 inf_note = modest_platform_animation_banner (GTK_WIDGET (window), NULL, 
4234                                                                              _CS("ckct_nw_pasting"));
4235                                 if (inf_note != NULL)  {
4236                                         gtk_window_set_modal (GTK_WINDOW(inf_note), FALSE);
4237                                         gtk_widget_show (GTK_WIDGET(inf_note));
4238                                 }
4239
4240                                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
4241                                 modest_mail_operation_xfer_msgs (mail_op, 
4242                                                                  data,
4243                                                                  TNY_FOLDER (folder_store),
4244                                                                  delete,
4245                                                                  destroy_information_note,
4246                                                                  inf_note);                             
4247                         } else {
4248                                 g_object_unref (mail_op);
4249                         }
4250                         
4251                 } else if (src_folder != NULL) {                        
4252                         /* Launch notification */
4253                         inf_note = modest_platform_animation_banner (GTK_WIDGET (window), NULL, 
4254                                                                      _CS("ckct_nw_pasting"));
4255                         if (inf_note != NULL)  {
4256                                 gtk_window_set_modal (GTK_WINDOW(inf_note), FALSE);
4257                                 gtk_widget_show (GTK_WIDGET(inf_note));
4258                         }
4259                         
4260                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
4261                         modest_mail_operation_xfer_folder (mail_op, 
4262                                                            src_folder,
4263                                                            folder_store,
4264                                                            delete,
4265                                                            destroy_folder_information_note,
4266                                                            inf_note);
4267                 }
4268
4269                 /* Free */
4270                 if (data != NULL) 
4271                         g_object_unref (data);
4272                 if (src_folder != NULL) 
4273                         g_object_unref (src_folder);
4274                 if (folder_store != NULL) 
4275                         g_object_unref (folder_store);
4276         }
4277 }
4278
4279
4280 void
4281 modest_ui_actions_on_select_all (GtkAction *action,
4282                                  ModestWindow *window)
4283 {
4284         GtkWidget *focused_widget;
4285
4286         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
4287         if (MODEST_IS_ATTACHMENTS_VIEW (focused_widget)) {
4288                 modest_attachments_view_select_all (MODEST_ATTACHMENTS_VIEW (focused_widget));
4289         } else if (GTK_IS_LABEL (focused_widget)) {
4290                 gtk_label_select_region (GTK_LABEL (focused_widget), 0, -1);
4291         } else if (GTK_IS_EDITABLE (focused_widget)) {
4292                 gtk_editable_select_region (GTK_EDITABLE(focused_widget), 0, -1);
4293         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
4294                 GtkTextBuffer *buffer;
4295                 GtkTextIter start, end;
4296
4297                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
4298                 gtk_text_buffer_get_start_iter (buffer, &start);
4299                 gtk_text_buffer_get_end_iter (buffer, &end);
4300                 gtk_text_buffer_select_range (buffer, &start, &end);
4301         } else if (GTK_IS_HTML (focused_widget)) {
4302                 gtk_html_select_all (GTK_HTML (focused_widget));
4303         } else if (MODEST_IS_MAIN_WINDOW (window)) {
4304                 GtkWidget *header_view = focused_widget;
4305                 GtkTreeSelection *selection = NULL;
4306                 
4307                 if (!(MODEST_IS_HEADER_VIEW (focused_widget))) {
4308                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
4309                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4310                 }
4311                                 
4312                 /* Disable window dimming management */
4313                 modest_window_disable_dimming (MODEST_WINDOW(window));
4314                 
4315                 /* Select all messages */
4316                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(header_view));
4317                 gtk_tree_selection_select_all (selection);
4318
4319                 /* Set focuse on header view */
4320                 gtk_widget_grab_focus (header_view);
4321
4322                 /* Enable window dimming management */
4323                 modest_window_enable_dimming (MODEST_WINDOW(window));
4324                 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
4325                 modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
4326         }
4327
4328 }
4329
4330 void
4331 modest_ui_actions_on_mark_as_read (GtkAction *action,
4332                                    ModestWindow *window)
4333 {       
4334         g_return_if_fail (MODEST_IS_WINDOW(window));
4335                 
4336         /* Mark each header as read */
4337         do_headers_action (window, headers_action_mark_as_read, NULL);
4338 }
4339
4340 void
4341 modest_ui_actions_on_mark_as_unread (GtkAction *action,
4342                                      ModestWindow *window)
4343 {       
4344         g_return_if_fail (MODEST_IS_WINDOW(window));
4345                 
4346         /* Mark each header as read */
4347         do_headers_action (window, headers_action_mark_as_unread, NULL);
4348 }
4349
4350 void
4351 modest_ui_actions_on_change_zoom (GtkRadioAction *action,
4352                                   GtkRadioAction *selected,
4353                                   ModestWindow *window)
4354 {
4355         gint value;
4356
4357         value = gtk_radio_action_get_current_value (selected);
4358         if (MODEST_IS_WINDOW (window)) {
4359                 modest_window_set_zoom (MODEST_WINDOW (window), ((gdouble)value)/100);
4360         }
4361 }
4362
4363 void
4364 modest_ui_actions_msg_edit_on_change_priority (GtkRadioAction *action,
4365                                                GtkRadioAction *selected,
4366                                                ModestWindow *window)
4367 {
4368         TnyHeaderFlags flags;
4369         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4370
4371         flags = gtk_radio_action_get_current_value (selected);
4372         modest_msg_edit_window_set_priority_flags (MODEST_MSG_EDIT_WINDOW (window), flags);
4373 }
4374
4375 void
4376 modest_ui_actions_msg_edit_on_change_file_format (GtkRadioAction *action,
4377                                                   GtkRadioAction *selected,
4378                                                   ModestWindow *window)
4379 {
4380         gint file_format;
4381
4382         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4383
4384         file_format = gtk_radio_action_get_current_value (selected);
4385         modest_msg_edit_window_set_file_format (MODEST_MSG_EDIT_WINDOW (window), file_format);
4386 }
4387
4388
4389 void
4390 modest_ui_actions_on_zoom_plus (GtkAction *action,
4391                                 ModestWindow *window)
4392 {
4393         g_return_if_fail (MODEST_IS_WINDOW (window));
4394
4395         modest_window_zoom_plus (MODEST_WINDOW (window));
4396 }
4397
4398 void     
4399 modest_ui_actions_on_zoom_minus (GtkAction *action,
4400                                  ModestWindow *window)
4401 {
4402         g_return_if_fail (MODEST_IS_WINDOW (window));
4403
4404         modest_window_zoom_minus (MODEST_WINDOW (window));
4405 }
4406
4407 void     
4408 modest_ui_actions_on_toggle_fullscreen    (GtkToggleAction *toggle,
4409                                            ModestWindow *window)
4410 {
4411         ModestWindowMgr *mgr;
4412         gboolean fullscreen, active;
4413         g_return_if_fail (MODEST_IS_WINDOW (window));
4414
4415         mgr = modest_runtime_get_window_mgr ();
4416
4417         active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle)))?1:0;
4418         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
4419
4420         if (active != fullscreen) {
4421                 modest_window_mgr_set_fullscreen_mode (mgr, active);
4422 #ifndef MODEST_TOOLKIT_HILDON2
4423                 gtk_window_present (GTK_WINDOW (window));
4424 #endif
4425         }
4426 }
4427
4428 void
4429 modest_ui_actions_on_change_fullscreen (GtkAction *action,
4430                                         ModestWindow *window)
4431 {
4432         ModestWindowMgr *mgr;
4433         gboolean fullscreen;
4434
4435         g_return_if_fail (MODEST_IS_WINDOW (window));
4436
4437         mgr = modest_runtime_get_window_mgr ();
4438         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
4439         modest_window_mgr_set_fullscreen_mode (mgr, !fullscreen);
4440
4441 #ifndef MODEST_TOOLKIT_HILDON2
4442         gtk_window_present (GTK_WINDOW (window));
4443 #endif
4444 }
4445
4446 /* 
4447  * Used by modest_ui_actions_on_details to call do_headers_action 
4448  */
4449 static void
4450 headers_action_show_details (TnyHeader *header, 
4451                              ModestWindow *window,
4452                              gpointer user_data)
4453
4454 {
4455         modest_platform_run_header_details_dialog (GTK_WINDOW (window), header);
4456 }
4457
4458 /*
4459  * Show the header details in a ModestDetailsDialog widget
4460  */
4461 void     
4462 modest_ui_actions_on_details (GtkAction *action, 
4463                               ModestWindow *win)
4464 {
4465         TnyList * headers_list;
4466         TnyIterator *iter;
4467         TnyHeader *header;              
4468
4469         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
4470                 TnyMsg *msg;
4471
4472                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
4473                 if (!msg)
4474                         return;
4475                 g_object_unref (msg);           
4476
4477                 headers_list = get_selected_headers (win);
4478                 if (!headers_list)
4479                         return;
4480
4481                 iter = tny_list_create_iterator (headers_list);
4482
4483                 header = TNY_HEADER (tny_iterator_get_current (iter));
4484                 if (header) {
4485                         headers_action_show_details (header, win, NULL);
4486                         g_object_unref (header);
4487                 }
4488
4489                 g_object_unref (iter);
4490                 g_object_unref (headers_list);
4491
4492         } else if (MODEST_IS_MAIN_WINDOW (win)) {
4493                 GtkWidget *folder_view, *header_view;
4494
4495                 /* Check which widget has the focus */
4496                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
4497                                                                     MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
4498                 if (gtk_widget_is_focus (folder_view)) {
4499                         TnyFolderStore *folder_store
4500                                 = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4501                         if (!folder_store) {
4502                                 g_warning ("%s: No item was selected.\n", __FUNCTION__);
4503                                 return; 
4504                         }
4505                         /* Show only when it's a folder */
4506                         /* This function should not be called for account items, 
4507                          * because we dim the menu item for them. */
4508                         if (TNY_IS_FOLDER (folder_store)) {
4509                                 modest_platform_run_folder_details_dialog (GTK_WINDOW (win), 
4510                                                                            TNY_FOLDER (folder_store));
4511                         }
4512
4513                         g_object_unref (folder_store);
4514
4515                 } else {
4516                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
4517                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4518                         /* Show details of each header */
4519                         do_headers_action (win, headers_action_show_details, header_view);
4520                 }
4521 #ifdef MODEST_TOOLKIT_HILDON2
4522         } else if (MODEST_IS_HEADER_WINDOW (win)) {
4523                 TnyFolder *folder;
4524                 GtkWidget *header_view;
4525
4526                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
4527                 folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
4528                 if (folder) {
4529                         modest_platform_run_folder_details_dialog (GTK_WINDOW (win), 
4530                                                                    folder);
4531                         g_object_unref (folder);
4532                 }
4533 #endif
4534         }
4535 }
4536
4537 void     
4538 modest_ui_actions_on_toggle_show_cc (GtkToggleAction *toggle,
4539                                      ModestMsgEditWindow *window)
4540 {
4541         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4542
4543         modest_msg_edit_window_show_cc (window, gtk_toggle_action_get_active (toggle));
4544 }
4545
4546 void     
4547 modest_ui_actions_on_toggle_show_bcc (GtkToggleAction *toggle,
4548                                       ModestMsgEditWindow *window)
4549 {
4550         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4551
4552         modest_msg_edit_window_show_bcc (window, gtk_toggle_action_get_active (toggle));
4553 }
4554
4555 void
4556 modest_ui_actions_toggle_folders_view (GtkAction *action, 
4557                                        ModestMainWindow *main_window)
4558 {
4559         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
4560
4561         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
4562                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SPLIT);
4563         else
4564                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SIMPLE);
4565 }
4566
4567 void 
4568 modest_ui_actions_on_toggle_toolbar (GtkToggleAction *toggle, 
4569                                      ModestWindow *window)
4570 {
4571         gboolean active, fullscreen = FALSE;
4572         ModestWindowMgr *mgr;
4573
4574         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle));
4575
4576         /* Check if we want to toggle the toolbar view in fullscreen
4577            or normal mode */
4578         if (!strcmp (gtk_action_get_name (GTK_ACTION (toggle)), 
4579                      "ViewShowToolbarFullScreen")) {
4580                 fullscreen = TRUE;
4581         }
4582
4583         /* Toggle toolbar */
4584         mgr = modest_runtime_get_window_mgr ();
4585         modest_window_mgr_show_toolbars (mgr, G_TYPE_FROM_INSTANCE (window), active, fullscreen);
4586 }
4587
4588 void     
4589 modest_ui_actions_msg_edit_on_select_font (GtkAction *action,
4590                                            ModestMsgEditWindow *window)
4591 {
4592         modest_msg_edit_window_select_font (window);
4593 }
4594
4595
4596 void
4597 modest_ui_actions_on_folder_display_name_changed (ModestFolderView *folder_view,
4598                                                   const gchar *display_name,
4599                                                   GtkWindow *window)
4600 {
4601         /* don't update the display name if it was already set;
4602          * updating the display name apparently is expensive */
4603         const gchar* old_name = gtk_window_get_title (window);
4604
4605         if (display_name == NULL)
4606                 display_name = " ";
4607
4608         if (old_name && display_name && strcmp (old_name, display_name) == 0)
4609                 return; /* don't do anything */
4610
4611         /* This is usually used to change the title of the main window, which
4612          * is the one that holds the folder view. Note that this change can
4613          * happen even when the widget doesn't have the focus. */
4614         gtk_window_set_title (window, display_name);
4615
4616 }
4617
4618 void
4619 modest_ui_actions_on_select_contacts (GtkAction *action, ModestMsgEditWindow *window)
4620 {
4621         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4622         modest_msg_edit_window_select_contacts (window);
4623 }
4624
4625 void
4626 modest_ui_actions_on_check_names (GtkAction *action, ModestMsgEditWindow *window)
4627 {
4628         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4629         modest_msg_edit_window_check_names (window, FALSE);
4630 }
4631
4632 #ifndef MODEST_TOOLKIT_HILDON2
4633 /*
4634  * This function is used to track changes in the selection of the
4635  * folder view that is inside the "move to" dialog to enable/disable
4636  * the OK button because we do not want the user to select a disallowed
4637  * destination for a folder.
4638  * The user also not desired to be able to use NEW button on items where
4639  * folder creation is not possibel.
4640  */
4641 static void
4642 on_move_to_dialog_folder_selection_changed (ModestFolderView* self,
4643                                             TnyFolderStore *folder_store,
4644                                             gboolean selected,
4645                                             gpointer user_data)
4646 {
4647         GtkWidget *dialog = NULL;
4648         gboolean ok_sensitive = TRUE, new_sensitive = TRUE;
4649         gboolean moving_folder = FALSE;
4650         gboolean is_local_account = TRUE;
4651         GtkWidget *folder_view = NULL;
4652         ModestTnyFolderRules rules;
4653
4654         g_return_if_fail (MODEST_IS_FOLDER_VIEW(self));
4655
4656         if (!selected)
4657                 return;
4658
4659         dialog = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_DIALOG);
4660         if (!dialog)
4661                 return;
4662
4663         /* check if folder_store is an remote account */
4664         if (TNY_IS_ACCOUNT (folder_store)) {
4665                 TnyAccount *local_account = NULL;
4666                 TnyAccount *mmc_account = NULL;
4667                 ModestTnyAccountStore *account_store = NULL;
4668
4669                 account_store = modest_runtime_get_account_store ();
4670                 local_account = modest_tny_account_store_get_local_folders_account (account_store);
4671                 mmc_account = modest_tny_account_store_get_mmc_folders_account (account_store);
4672
4673                 if ((gpointer) local_account != (gpointer) folder_store &&
4674                     (gpointer) mmc_account != (gpointer) folder_store) {
4675                         ModestProtocolType proto;
4676                         proto = modest_tny_account_get_protocol_type (TNY_ACCOUNT (folder_store));
4677                         if (proto == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
4678                                 proto = MODEST_PROTOCOLS_STORE_MAILDIR;
4679                         }
4680                         is_local_account = FALSE;
4681                         /* New button should be dimmed on remote
4682                            POP account root */
4683                         new_sensitive = (modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
4684                                                                                          proto,
4685                                                                                          MODEST_PROTOCOL_REGISTRY_STORE_HAS_FOLDERS));
4686                 }
4687                 g_object_unref (local_account);
4688
4689                 /* It could not exist */
4690                 if (mmc_account)
4691                         g_object_unref (mmc_account);
4692         }
4693
4694         /* Check the target folder rules */
4695         if (TNY_IS_FOLDER (folder_store)) {
4696                 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder_store));
4697                 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
4698                         ok_sensitive = FALSE;
4699                         new_sensitive = FALSE;
4700                         goto end;
4701                 }
4702         }
4703
4704         /* Check if we're moving a folder */
4705         if (MODEST_IS_MAIN_WINDOW (user_data)) {
4706                 /* Get the widgets */
4707                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (user_data),
4708                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
4709                 if (gtk_widget_is_focus (folder_view))
4710                         moving_folder = TRUE;
4711         }
4712
4713         if (moving_folder) {
4714                 TnyFolderStore *moved_folder = NULL, *parent = NULL;
4715
4716                 /* Get the folder to move */
4717                 moved_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4718
4719                 /* Check that we're not moving to the same folder */
4720                 if (TNY_IS_FOLDER (moved_folder)) {
4721                         parent = tny_folder_get_folder_store (TNY_FOLDER (moved_folder));
4722                         if (parent == folder_store)
4723                                 ok_sensitive = FALSE;
4724                         g_object_unref (parent);
4725                 }
4726
4727                 if (ok_sensitive && TNY_IS_ACCOUNT (folder_store)) {
4728                         /* Do not allow to move to an account unless it's the
4729                            local folders account */
4730                         if (!is_local_account)
4731                                 ok_sensitive = FALSE;
4732                 }
4733
4734                 if (ok_sensitive && (moved_folder == folder_store)) {
4735                         /* Do not allow to move to itself */
4736                         ok_sensitive = FALSE;
4737                 }
4738                 g_object_unref (moved_folder);
4739         } else {
4740                 TnyFolder *src_folder = NULL;
4741
4742                 /* Moving a message */
4743                 if (MODEST_IS_MSG_VIEW_WINDOW (user_data)) {
4744
4745                         TnyHeader *header = NULL;
4746                         header = modest_msg_view_window_get_header
4747                                 (MODEST_MSG_VIEW_WINDOW (user_data));
4748                         if (!TNY_IS_HEADER(header))
4749                                 g_warning ("%s: could not get source header", __FUNCTION__);
4750                         else
4751                                 src_folder = tny_header_get_folder (header);
4752
4753                         if (header)
4754                                 g_object_unref (header);
4755                 } else {
4756                         src_folder = 
4757                                 TNY_FOLDER (modest_folder_view_get_selected
4758                                             (MODEST_FOLDER_VIEW (folder_view)));
4759                 }
4760
4761                 if (TNY_IS_FOLDER(src_folder)) {
4762                         /* Do not allow to move the msg to the same folder */
4763                         /* Do not allow to move the msg to an account */
4764                         if ((gpointer) src_folder == (gpointer) folder_store ||
4765                             TNY_IS_ACCOUNT (folder_store))
4766                                 ok_sensitive = FALSE;
4767                         g_object_unref (src_folder);
4768                 } else
4769                         g_warning ("%s: could not get source folder", __FUNCTION__);
4770         }
4771
4772  end:
4773         /* Set sensitivity of the OK and NEW button */
4774         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, ok_sensitive);
4775         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), MODEST_GTK_RESPONSE_NEW_FOLDER, new_sensitive);
4776 }
4777 #endif
4778
4779 static void
4780 on_move_to_dialog_response (GtkDialog *dialog,
4781                             gint       response,
4782                             gpointer   user_data)
4783 {
4784         GtkWidget *parent_win, *folder_view;
4785         MoveToInfo *helper = NULL;
4786
4787         helper = (MoveToInfo *) user_data;
4788
4789         parent_win = (GtkWidget *) helper->win;
4790         folder_view = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog),
4791                                                      MODEST_MOVE_TO_DIALOG_FOLDER_VIEW));
4792
4793         switch (response) {
4794                 TnyFolderStore *dst_folder;
4795
4796         case MODEST_GTK_RESPONSE_NEW_FOLDER:
4797                 modest_ui_actions_create_folder (GTK_WIDGET (dialog), folder_view);
4798                 return;
4799         case GTK_RESPONSE_NONE:
4800         case GTK_RESPONSE_CANCEL:
4801         case GTK_RESPONSE_DELETE_EVENT:
4802                 break;
4803         case GTK_RESPONSE_OK:
4804                 dst_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4805                 /* Do window specific stuff */
4806                 if (MODEST_IS_MAIN_WINDOW (parent_win)) {
4807                         modest_ui_actions_on_main_window_move_to (NULL,
4808                                                                   folder_view,
4809                                                                   dst_folder,
4810                                                                   MODEST_MAIN_WINDOW (parent_win));
4811 #ifdef MODEST_TOOLKIT_HILDON2
4812                 } else if (MODEST_IS_FOLDER_WINDOW (parent_win)) {
4813                         modest_ui_actions_on_folder_window_move_to (folder_view,
4814                                                                     dst_folder,
4815                                                                     helper->list,
4816                                                                     GTK_WINDOW (parent_win));
4817 #endif
4818                 } else {
4819                         /* Moving from headers window in edit mode */
4820                         modest_ui_actions_on_window_move_to (NULL, helper->list,
4821                                                              dst_folder, 
4822                                                              MODEST_WINDOW (parent_win));
4823                 }
4824
4825                 if (dst_folder)
4826                         g_object_unref (dst_folder);
4827
4828                 break;
4829         default:
4830                 g_warning ("%s unexpected response id %d", __FUNCTION__, response);
4831         }
4832
4833         /* Free the helper and exit */
4834         g_object_unref (helper->list);
4835         g_slice_free (MoveToInfo, helper);
4836         gtk_widget_destroy (GTK_WIDGET (dialog));
4837 }
4838
4839 static GtkWidget*
4840 create_move_to_dialog (GtkWindow *win,
4841                        GtkWidget *folder_view)
4842 {
4843         GtkWidget *dialog, *tree_view = NULL;
4844
4845         dialog = modest_platform_create_move_to_dialog (win, &tree_view);
4846
4847 #ifndef MODEST_TOOLKIT_HILDON2
4848         /* Track changes in the selection to
4849          * disable the OK button whenever "Move to" is not possible
4850          * disbale NEW button whenever New is not possible */
4851         g_signal_connect (tree_view,
4852                           "folder_selection_changed",
4853                           G_CALLBACK (on_move_to_dialog_folder_selection_changed),
4854                           win);
4855 #endif
4856
4857         /* It could happen that we're trying to move a message from a
4858            window (msg window for example) after the main window was
4859            closed, so we can not just get the model of the folder
4860            view */
4861         if (MODEST_IS_FOLDER_VIEW (folder_view)) {
4862                 const gchar *visible_id = NULL;
4863
4864                 modest_folder_view_set_style (MODEST_FOLDER_VIEW (tree_view),
4865                                               MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
4866                 modest_folder_view_copy_model (MODEST_FOLDER_VIEW(folder_view), 
4867                                                MODEST_FOLDER_VIEW(tree_view));
4868
4869                 visible_id = 
4870                         modest_folder_view_get_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(folder_view));
4871
4872                 /* Show the same account than the one that is shown in the main window */
4873                 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(tree_view),
4874                                                                              visible_id);
4875         } else {
4876                 const gchar *active_account_name = NULL;
4877                 ModestAccountMgr *mgr = NULL;
4878                 ModestAccountSettings *settings = NULL;
4879                 ModestServerAccountSettings *store_settings = NULL;
4880
4881                 modest_folder_view_set_style (MODEST_FOLDER_VIEW (tree_view),
4882                                               MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
4883                 modest_folder_view_update_model (MODEST_FOLDER_VIEW (tree_view),
4884                                                  TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
4885
4886                 active_account_name = modest_window_get_active_account (MODEST_WINDOW (win));
4887                 mgr = modest_runtime_get_account_mgr ();
4888                 settings = modest_account_mgr_load_account_settings (mgr, active_account_name);
4889
4890                 if (settings) {
4891                         const gchar *store_account_name;
4892                         store_settings = modest_account_settings_get_store_settings (settings);
4893                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
4894
4895                         modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (tree_view),
4896                                                                                      store_account_name);
4897                         g_object_unref (store_settings);
4898                         g_object_unref (settings);
4899                 }
4900         }
4901
4902         /* we keep a pointer to the embedded folder view, so we can
4903          *   retrieve it with get_folder_view_from_move_to_dialog (see
4904          *   above) later (needed for focus handling)
4905          */
4906         g_object_set_data (G_OBJECT(dialog), MODEST_MOVE_TO_DIALOG_FOLDER_VIEW, tree_view);
4907
4908         /* Hide special folders */
4909         modest_folder_view_show_non_move_folders (MODEST_FOLDER_VIEW (tree_view), FALSE);
4910 #ifndef MODEST_TOOLKIT_HILDON2
4911         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (tree_view));
4912 #endif
4913
4914         gtk_widget_show (GTK_WIDGET (tree_view));
4915
4916         return dialog;
4917 }
4918
4919 /*
4920  * Shows a confirmation dialog to the user when we're moving messages
4921  * from a remote server to the local storage. Returns the dialog
4922  * response. If it's other kind of movement then it always returns
4923  * GTK_RESPONSE_OK
4924  *
4925  * This one is used by the next functions:
4926  *      modest_ui_actions_on_paste                      - commented out
4927  *      drag_and_drop_from_header_view (for d&d in modest_folder_view.c)
4928  */
4929 gint
4930 modest_ui_actions_msgs_move_to_confirmation (ModestWindow *win,
4931                                              TnyFolder *dest_folder,
4932                                              gboolean delete,
4933                                              TnyList *headers)
4934 {
4935         gint response = GTK_RESPONSE_OK;
4936         TnyAccount *account = NULL;
4937         TnyFolder *src_folder = NULL;
4938         TnyIterator *iter = NULL;
4939         TnyHeader *header = NULL;
4940
4941         /* return with OK if the destination is a remote folder */
4942         if (modest_tny_folder_is_remote_folder (dest_folder))
4943                 return GTK_RESPONSE_OK;
4944
4945         /* Get source folder */
4946         iter = tny_list_create_iterator (headers);
4947         header = TNY_HEADER (tny_iterator_get_current (iter));
4948         if (header) {
4949                 src_folder = tny_header_get_folder (header);
4950                 g_object_unref (header);
4951         }
4952         g_object_unref (iter);
4953
4954         /* if no src_folder, message may be an attahcment */
4955         if (src_folder == NULL) 
4956                 return GTK_RESPONSE_CANCEL;
4957
4958         /* If the source is a local or MMC folder */
4959         if (!modest_tny_folder_is_remote_folder (src_folder)) {
4960                 g_object_unref (src_folder);
4961                 return GTK_RESPONSE_OK;
4962         }
4963
4964         /* Get the account */
4965         account = tny_folder_get_account (src_folder);
4966
4967         /* now if offline we ask the user */
4968         if(connect_to_get_msg (win, tny_list_get_length (headers), account))
4969                 response = GTK_RESPONSE_OK;
4970         else
4971                 response = GTK_RESPONSE_CANCEL;
4972
4973         /* Frees */
4974         g_object_unref (src_folder);
4975         g_object_unref (account);
4976
4977         return response;
4978 }
4979
4980 static void
4981 move_to_helper_destroyer (gpointer user_data)
4982 {
4983         MoveToHelper *helper = (MoveToHelper *) user_data;
4984
4985         /* Close the "Pasting" information banner */
4986         if (helper->banner) {
4987                 gtk_widget_destroy (GTK_WIDGET (helper->banner));
4988                 g_object_unref (helper->banner);
4989         }
4990         if (gtk_tree_row_reference_valid (helper->reference)) {
4991                 gtk_tree_row_reference_free (helper->reference);
4992                 helper->reference = NULL;
4993         }
4994         g_free (helper);
4995 }
4996
4997 static void
4998 move_to_cb (ModestMailOperation *mail_op, 
4999             gpointer user_data)
5000 {
5001         MoveToHelper *helper = (MoveToHelper *) user_data;
5002
5003         /* Note that the operation could have failed, in that case do
5004            nothing */
5005         if (modest_mail_operation_get_status (mail_op) == 
5006             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
5007
5008                 GObject *object = modest_mail_operation_get_source (mail_op);
5009                 if (MODEST_IS_MSG_VIEW_WINDOW (object)) {
5010                         ModestMsgViewWindow *self = MODEST_MSG_VIEW_WINDOW (object);
5011
5012                         if (!modest_msg_view_window_select_next_message (self) &&
5013                             !modest_msg_view_window_select_previous_message (self)) {
5014                                 /* No more messages to view, so close this window */
5015                                 modest_ui_actions_on_close_window (NULL, MODEST_WINDOW(self));
5016                         }
5017                 } else if (MODEST_IS_MAIN_WINDOW (object) && 
5018                            gtk_tree_row_reference_valid (helper->reference)) {
5019                         GtkWidget *header_view;
5020                         GtkTreePath *path;
5021                         GtkTreeSelection *sel;
5022
5023                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(object),
5024                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
5025                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
5026                         path = gtk_tree_row_reference_get_path (helper->reference);
5027                         /* We need to unselect the previous one
5028                            because we could be copying instead of
5029                            moving */
5030                         gtk_tree_selection_unselect_all (sel);
5031                         gtk_tree_selection_select_path (sel, path);
5032                         gtk_tree_path_free (path);
5033                 }
5034                 g_object_unref (object);
5035         }
5036         /* Destroy the helper */
5037         move_to_helper_destroyer (helper);
5038 }
5039
5040 static void
5041 folder_move_to_cb (ModestMailOperation *mail_op, 
5042                    TnyFolder *new_folder,
5043                    gpointer user_data)
5044 {
5045         GtkWidget *folder_view;
5046         GObject *object;
5047
5048         object = modest_mail_operation_get_source (mail_op);
5049         if (MODEST_IS_MAIN_WINDOW (object)) {
5050                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(object),
5051                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
5052                 g_object_ref (folder_view);
5053                 g_object_unref (object);
5054                 move_to_cb (mail_op, user_data);
5055                 modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view), new_folder, FALSE);
5056                 g_object_unref (folder_view);
5057         } else {
5058                 move_to_cb (mail_op, user_data);
5059         }
5060 }
5061
5062 static void
5063 msgs_move_to_cb (ModestMailOperation *mail_op, 
5064                  gpointer user_data)
5065 {
5066         move_to_cb (mail_op, user_data);
5067 }
5068
5069 void
5070 modest_ui_actions_move_folder_error_handler (ModestMailOperation *mail_op, 
5071                                              gpointer user_data)
5072 {
5073         ModestWindow *main_window = NULL;
5074         
5075         /* Disable next automatic folder selection */
5076         main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
5077                                                          FALSE); /* don't create */
5078         if (main_window) {
5079                 GObject *win = NULL;
5080                 GtkWidget *folder_view = NULL;
5081         
5082                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_window),
5083                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW); 
5084                 modest_folder_view_disable_next_folder_selection (MODEST_FOLDER_VIEW(folder_view));
5085                 
5086                 if (user_data && TNY_IS_FOLDER (user_data)) {
5087                         modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view), 
5088                                                           TNY_FOLDER (user_data), FALSE);
5089                 }
5090
5091                 /* Show notification dialog only if the main window exists */
5092                 win = modest_mail_operation_get_source (mail_op);
5093                 modest_platform_run_information_dialog ((GtkWindow *) win, 
5094                                                         _("mail_in_ui_folder_move_target_error"), 
5095                                                         FALSE);
5096                 if (win)
5097                         g_object_unref (win);
5098         }
5099 }
5100
5101 static void
5102 open_msg_for_purge_cb (ModestMailOperation *mail_op, 
5103                        TnyHeader *header, 
5104                        gboolean canceled,
5105                        TnyMsg *msg, 
5106                        GError *err,
5107                        gpointer user_data)
5108 {
5109         TnyList *parts;
5110         TnyIterator *iter;
5111         gint pending_purges = 0;
5112         gboolean some_purged = FALSE;
5113         ModestWindow *win = MODEST_WINDOW (user_data);
5114         ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
5115
5116         /* If there was any error */
5117         if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
5118                 modest_window_mgr_unregister_header (mgr, header);
5119                 return;
5120         }
5121
5122         /* Once the message has been retrieved for purging, we check if
5123          * it's all ok for purging */
5124
5125         parts = tny_simple_list_new ();
5126         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
5127         iter = tny_list_create_iterator (parts);
5128
5129         while (!tny_iterator_is_done (iter)) {
5130                 TnyMimePart *part;
5131                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
5132                 if (part && (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part))) {
5133                         if (tny_mime_part_is_purged (part))
5134                                 some_purged = TRUE;
5135                         else
5136                                 pending_purges++;
5137                 }
5138
5139                 if (part)
5140                         g_object_unref (part);
5141
5142                 tny_iterator_next (iter);
5143         }
5144         g_object_unref (iter);
5145         
5146
5147         if (pending_purges>0) {
5148                 gint response;
5149                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),_("mcen_nc_purge_file_text_inbox"));
5150
5151                 if (response == GTK_RESPONSE_OK) {
5152                         GtkWidget *info;
5153                         info =
5154                                 modest_platform_animation_banner (GTK_WIDGET (win), NULL, _("mcen_me_inbox_remove_attachments"));
5155                         iter = tny_list_create_iterator (parts);
5156                         while (!tny_iterator_is_done (iter)) {
5157                                 TnyMimePart *part;
5158                                 
5159                                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
5160                                 if (part && (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part)))
5161                                         tny_mime_part_set_purged (part);
5162
5163                                 if (part)
5164                                         g_object_unref (part);
5165
5166                                 tny_iterator_next (iter);
5167                         }
5168                         g_object_unref (iter);
5169                         
5170                         tny_msg_rewrite_cache (msg);
5171
5172                         gtk_widget_destroy (info);
5173                 }
5174         }
5175
5176         modest_window_mgr_unregister_header (mgr, header);
5177
5178         g_object_unref (parts);
5179 }
5180
5181 static void
5182 modest_ui_actions_on_main_window_remove_attachments (GtkAction *action,
5183                                                      ModestMainWindow *win)
5184 {
5185         GtkWidget *header_view;
5186         TnyList *header_list;
5187         TnyHeader *header;
5188         TnyHeaderFlags flags;
5189         ModestWindow *msg_view_window =  NULL;
5190         gboolean found;
5191
5192         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
5193
5194         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
5195                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
5196
5197         header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
5198         if (!header_list) {
5199                 g_warning ("%s: no header selected", __FUNCTION__);
5200                 return;
5201         }
5202         
5203         if (tny_list_get_length (header_list) == 1) {
5204                 TnyIterator *iter = tny_list_create_iterator (header_list);
5205                 header = TNY_HEADER (tny_iterator_get_current (iter));
5206                 g_object_unref (iter);
5207         } else
5208                 return;
5209         
5210         if (!header || !TNY_IS_HEADER(header)) {
5211                 g_warning ("%s: header is not valid", __FUNCTION__);
5212                 return;
5213         }
5214         
5215         found = modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr (),
5216                                                           header, &msg_view_window);
5217         flags = tny_header_get_flags (header);
5218         if (!(flags & TNY_HEADER_FLAG_CACHED))
5219                 return;
5220         if (found) {
5221                 if (msg_view_window != NULL) 
5222                         modest_msg_view_window_remove_attachments (MODEST_MSG_VIEW_WINDOW (msg_view_window), TRUE);
5223                 else {
5224                         /* do nothing; uid was registered before, so window is probably on it's way */
5225                         g_warning ("debug: header %p has already been registered", header);
5226                 }
5227         } else {
5228                 ModestMailOperation *mail_op = NULL;
5229                 modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header, NULL);
5230                 mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (win),
5231                                                                          modest_ui_actions_disk_operations_error_handler,
5232                                                                          NULL, NULL);
5233                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
5234                 modest_mail_operation_get_msg (mail_op, header, FALSE, open_msg_for_purge_cb, win);
5235                 
5236                 g_object_unref (mail_op);
5237         }
5238         if (header)
5239                 g_object_unref (header);
5240         if (header_list)
5241                 g_object_unref (header_list);
5242 }
5243
5244 /*
5245  * Checks if we need a connection to do the transfer and if the user
5246  * wants to connect to complete it
5247  */
5248 void
5249 modest_ui_actions_xfer_messages_check (GtkWindow *parent_window,
5250                                        TnyFolderStore *src_folder,
5251                                        TnyList *headers,
5252                                        TnyFolder *dst_folder,
5253                                        gboolean delete_originals,
5254                                        gboolean *need_connection,
5255                                        gboolean *do_xfer)
5256 {
5257         TnyAccount *src_account;
5258         gint uncached_msgs = 0;
5259
5260         uncached_msgs = header_list_count_uncached_msgs (headers);
5261
5262         /* We don't need any further check if
5263          *
5264          * 1- the source folder is local OR
5265          * 2- the device is already online
5266          */
5267         if (!modest_tny_folder_store_is_remote (src_folder) ||
5268             tny_device_is_online (modest_runtime_get_device())) {
5269                 *need_connection = FALSE;
5270                 *do_xfer = TRUE;
5271                 return;
5272         }
5273
5274         /* We must ask for a connection when
5275          *
5276          *   - the message(s) is not already cached   OR 
5277          *   - the message(s) is cached but the leave_on_server setting
5278          * is FALSE (because we need to sync the source folder to
5279          * delete the message from the server (for IMAP we could do it
5280          * offline, it'll take place the next time we get a
5281          * connection)
5282          */
5283         src_account = get_account_from_folder_store (src_folder);
5284         if (uncached_msgs > 0) {
5285                 guint num_headers;
5286                 const gchar *msg;
5287
5288                 *need_connection = TRUE;
5289                 num_headers = tny_list_get_length (headers);
5290                 msg = ngettext ("mcen_nc_get_msg", "mcen_nc_get_msgs", num_headers);
5291
5292                 if (modest_platform_run_confirmation_dialog (parent_window, msg) ==
5293                     GTK_RESPONSE_CANCEL) {
5294                         *do_xfer = FALSE;
5295                 } else {
5296                         *do_xfer = TRUE;
5297                 }
5298         } else {
5299                 /* The transfer is possible and the user wants to */
5300                 *do_xfer = TRUE;
5301
5302                 if (remote_folder_has_leave_on_server (src_folder) && delete_originals) {
5303                         const gchar *account_name;
5304                         gboolean leave_on_server;
5305                         
5306                         account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (src_account);
5307                         leave_on_server = modest_account_mgr_get_leave_on_server (modest_runtime_get_account_mgr (),
5308                                                                                   account_name);
5309                         
5310                         if (leave_on_server == TRUE) {
5311                                 *need_connection = FALSE;
5312                         } else {
5313                                 *need_connection = TRUE;
5314                         }
5315                 } else {
5316                         *need_connection = FALSE;
5317                 }
5318         }
5319
5320         /* Frees */
5321         g_object_unref (src_account);
5322 }
5323
5324 static void
5325 xfer_messages_error_handler (ModestMailOperation *mail_op, 
5326                              gpointer user_data)
5327 {
5328         ModestWindow *main_window = NULL;
5329
5330         /* Disable next automatic folder selection */
5331         main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
5332                                                          FALSE); /* don't create */
5333         if (main_window) {
5334                 GObject *win = modest_mail_operation_get_source (mail_op);
5335                 modest_platform_run_information_dialog ((GtkWindow *) win, 
5336                                                         _("mail_in_ui_folder_move_target_error"), 
5337                                                         FALSE);
5338                 if (win)
5339                         g_object_unref (win);
5340         }
5341         move_to_helper_destroyer (user_data);
5342 }
5343
5344 typedef struct {
5345         TnyFolderStore *dst_folder;
5346         TnyList *headers;
5347 } XferMsgsHelper;
5348
5349 /**
5350  * Utility function that transfer messages from both the main window
5351  * and the msg view window when using the "Move to" dialog
5352  */
5353 static void
5354 xfer_messages_performer  (gboolean canceled, 
5355                           GError *err,
5356                           GtkWindow *parent_window, 
5357                           TnyAccount *account, 
5358                           gpointer user_data)
5359 {
5360         ModestWindow *win = MODEST_WINDOW (parent_window);
5361         TnyAccount *dst_account = NULL;
5362         gboolean dst_forbids_message_add = FALSE;
5363         XferMsgsHelper *helper;
5364         MoveToHelper *movehelper;
5365         ModestMailOperation *mail_op;
5366
5367         helper = (XferMsgsHelper *) user_data;
5368
5369         if (canceled || err) {
5370                 if (!check_memory_full_error ((GtkWidget *) parent_window, err)) {
5371                         /* Show the proper error message */
5372                         modest_ui_actions_on_account_connection_error (parent_window, account);
5373                 }
5374                 goto end;
5375         }
5376
5377         dst_account = tny_folder_get_account (TNY_FOLDER (helper->dst_folder));
5378
5379         /* tinymail will return NULL for local folders it seems */
5380         dst_forbids_message_add = modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
5381                                                                                   modest_tny_account_get_protocol_type (dst_account),
5382                                                                                   MODEST_PROTOCOL_REGISTRY_STORE_FORBID_MESSAGE_ADD);
5383         g_object_unref (dst_account);
5384
5385         if (dst_forbids_message_add) {
5386                 modest_platform_information_banner (GTK_WIDGET (win),
5387                                                     NULL,
5388                                                     ngettext("mail_in_ui_folder_move_target_error",
5389                                                              "mail_in_ui_folder_move_targets_error",
5390                                                              tny_list_get_length (helper->headers)));
5391                 goto end;
5392         }
5393
5394         movehelper = g_new0 (MoveToHelper, 1);
5395         movehelper->banner = modest_platform_animation_banner (GTK_WIDGET (win), NULL,
5396                                                                _CS("ckct_nw_pasting"));
5397         if (movehelper->banner != NULL)  {
5398                 g_object_ref (movehelper->banner);
5399                 gtk_widget_show (GTK_WIDGET (movehelper->banner));
5400         }
5401
5402         if (MODEST_IS_MAIN_WINDOW (win)) {
5403                 GtkWidget *header_view = 
5404                         modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
5405                                                              MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
5406                 movehelper->reference = get_next_after_selected_headers (MODEST_HEADER_VIEW (header_view));
5407         }
5408
5409         /* Perform the mail operation */
5410         mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT(win),
5411                                                                  xfer_messages_error_handler,
5412                                                                  movehelper, NULL);
5413         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
5414                                          mail_op);
5415
5416         modest_mail_operation_xfer_msgs (mail_op, 
5417                                          helper->headers,
5418                                          TNY_FOLDER (helper->dst_folder),
5419                                          TRUE,
5420                                          msgs_move_to_cb,
5421                                          movehelper);
5422
5423         g_object_unref (G_OBJECT (mail_op));
5424  end:
5425         g_object_unref (helper->dst_folder);
5426         g_object_unref (helper->headers);
5427         g_slice_free (XferMsgsHelper, helper);
5428 }
5429
5430 typedef struct {
5431         TnyFolder *src_folder;
5432         TnyFolderStore *dst_folder;
5433         gboolean delete_original;
5434         GtkWidget *folder_view;
5435 } MoveFolderInfo;
5436
5437 static void
5438 on_move_folder_cb (gboolean canceled, GError *err, GtkWindow *parent_window, 
5439                 TnyAccount *account, gpointer user_data)
5440 {
5441         MoveFolderInfo *info = (MoveFolderInfo*)user_data;
5442         GtkTreeSelection *sel;
5443         ModestMailOperation *mail_op = NULL;
5444         
5445         if (canceled || err || !MODEST_IS_WINDOW (parent_window)) {
5446                 g_object_unref (G_OBJECT (info->src_folder));
5447                 g_object_unref (G_OBJECT (info->dst_folder));
5448                 g_free (info);
5449                 return;
5450         }
5451         
5452         MoveToHelper *helper = g_new0 (MoveToHelper, 1);
5453         helper->banner = modest_platform_animation_banner (GTK_WIDGET (parent_window), NULL,
5454                         _CS("ckct_nw_pasting"));
5455         if (helper->banner != NULL)  {
5456                 g_object_ref (helper->banner);
5457                 gtk_widget_show (GTK_WIDGET(helper->banner));
5458         }
5459         /* Clean folder on header view before moving it */
5460         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (info->folder_view));
5461         gtk_tree_selection_unselect_all (sel);
5462
5463         /* Let gtk events run. We need that the folder
5464            view frees its reference to the source
5465            folder *before* issuing the mail operation
5466            so we need the signal handler of selection
5467            changed to happen before the mail
5468            operation 
5469         while (gtk_events_pending ())
5470                 gtk_main_iteration ();   */
5471
5472         mail_op =
5473                 modest_mail_operation_new_with_error_handling (G_OBJECT(parent_window),
5474                                 modest_ui_actions_move_folder_error_handler,
5475                                 info->src_folder, NULL);
5476         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
5477                         mail_op);
5478
5479         /* Select *after* the changes */
5480         /* TODO: this function hangs UI after transfer */ 
5481         /*                      modest_folder_view_select_folder (MODEST_FOLDER_VIEW(folder_view), */
5482         /*                                                        TNY_FOLDER (src_folder), TRUE); */
5483
5484         if (MODEST_IS_MAIN_WINDOW (parent_window)) {
5485                 modest_folder_view_select_folder (MODEST_FOLDER_VIEW(info->folder_view),
5486                                                   TNY_FOLDER (info->dst_folder), TRUE);
5487         }
5488         modest_mail_operation_xfer_folder (mail_op,
5489                         TNY_FOLDER (info->src_folder),
5490                         info->dst_folder,
5491                         info->delete_original, 
5492                         folder_move_to_cb, 
5493                         helper);
5494         g_object_unref (G_OBJECT (info->src_folder));
5495
5496         /* if (modest_mail_operation_get_status (mail_op) == MODEST_MAIL_OPERATION_STATUS_SUCCESS) {        */
5497         /* } */
5498         
5499         /* Unref mail operation */
5500         g_object_unref (G_OBJECT (mail_op));
5501         g_object_unref (G_OBJECT (info->dst_folder));
5502         g_free (user_data);
5503 }
5504
5505 static TnyAccount *
5506 get_account_from_folder_store (TnyFolderStore *folder_store) 
5507 {
5508         if (TNY_IS_ACCOUNT (folder_store))
5509                 return g_object_ref (folder_store);
5510         else
5511                 return tny_folder_get_account (TNY_FOLDER (folder_store));
5512 }
5513
5514 /*
5515  * UI handler for the "Move to" action when invoked from the
5516  * ModestMainWindow
5517  */
5518 static void 
5519 modest_ui_actions_on_main_window_move_to (GtkAction *action, 
5520                                           GtkWidget *folder_view,
5521                                           TnyFolderStore *dst_folder,
5522                                           ModestMainWindow *win)
5523 {
5524         ModestHeaderView *header_view = NULL;
5525         TnyFolderStore *src_folder = NULL;
5526
5527         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
5528
5529         /* Get the source folder */
5530         src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
5531
5532         /* Get header view */
5533         header_view = (ModestHeaderView *)
5534                 modest_main_window_get_child_widget (win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
5535
5536         /* Get folder or messages to transfer */
5537         if (gtk_widget_is_focus (folder_view)) {
5538                 gboolean do_xfer = TRUE;
5539
5540                 /* Allow only to transfer folders to the local root folder */
5541                 if (TNY_IS_ACCOUNT (dst_folder) && 
5542                     !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (dst_folder) &&
5543                     !modest_tny_account_is_memory_card_account (TNY_ACCOUNT (dst_folder))) {
5544                         do_xfer = FALSE;
5545                 } else if (!TNY_IS_FOLDER (src_folder)) {
5546                         g_warning ("%s: src_folder is not a TnyFolder.\n", __FUNCTION__);
5547                         do_xfer = FALSE;
5548                 }
5549
5550                 if (do_xfer) {
5551                         MoveFolderInfo *info = g_new0 (MoveFolderInfo, 1);
5552                         DoubleConnectionInfo *connect_info = g_slice_new (DoubleConnectionInfo);
5553
5554                         info->src_folder = g_object_ref (src_folder);
5555                         info->dst_folder = g_object_ref (dst_folder);
5556                         info->delete_original = TRUE;
5557                         info->folder_view = folder_view;
5558
5559                         connect_info->callback = on_move_folder_cb;
5560                         connect_info->dst_account = get_account_from_folder_store (TNY_FOLDER_STORE (dst_folder));
5561                         connect_info->data = info;
5562
5563                         modest_platform_double_connect_and_perform(GTK_WINDOW (win), TRUE,
5564                                                                    TNY_FOLDER_STORE (src_folder), 
5565                                                                    connect_info);
5566                 }
5567         } else if (gtk_widget_is_focus (GTK_WIDGET(header_view))) {
5568                 TnyList *headers;
5569
5570                 headers = modest_header_view_get_selected_headers(header_view);
5571
5572                 /* Transfer the messages */
5573                 modest_ui_actions_transfer_messages_helper (GTK_WINDOW (win), TNY_FOLDER (src_folder), 
5574                                                             headers, TNY_FOLDER (dst_folder));
5575
5576                 g_object_unref (headers);
5577         }
5578
5579         /* Frees */
5580         g_object_unref (src_folder);
5581 }
5582
5583 #ifdef MODEST_TOOLKIT_HILDON2
5584 /*
5585  * UI handler for the "Move to" action when invoked from the
5586  * ModestFolderWindow
5587  */
5588 static void 
5589 modest_ui_actions_on_folder_window_move_to (GtkWidget *folder_view,
5590                                             TnyFolderStore *dst_folder,
5591                                             TnyList *selection,
5592                                             GtkWindow *win)
5593 {
5594         TnyFolderStore *src_folder = NULL;
5595         TnyIterator *iterator;
5596
5597         if (tny_list_get_length (selection) != 1)
5598                 return;
5599
5600         iterator = tny_list_create_iterator (selection);
5601         src_folder = TNY_FOLDER_STORE (tny_iterator_get_current (iterator));
5602         g_object_unref (iterator);
5603
5604
5605         gboolean do_xfer = TRUE;
5606
5607         /* Allow only to transfer folders to the local root folder */
5608         if (TNY_IS_ACCOUNT (dst_folder) && 
5609             !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (dst_folder) &&
5610             !modest_tny_account_is_memory_card_account (TNY_ACCOUNT (dst_folder))) {
5611                 do_xfer = FALSE;
5612         } else if (!TNY_IS_FOLDER (src_folder)) {
5613                 g_warning ("%s: src_folder is not a TnyFolder.\n", __FUNCTION__);
5614                 do_xfer = FALSE;
5615         }
5616
5617         if (do_xfer) {
5618                 MoveFolderInfo *info = g_new0 (MoveFolderInfo, 1);
5619                 DoubleConnectionInfo *connect_info = g_slice_new (DoubleConnectionInfo);
5620                 
5621                 info->src_folder = g_object_ref (src_folder);
5622                 info->dst_folder = g_object_ref (dst_folder);
5623                 info->delete_original = TRUE;
5624                 info->folder_view = folder_view;
5625
5626                 connect_info->callback = on_move_folder_cb;
5627                 connect_info->dst_account = get_account_from_folder_store (TNY_FOLDER_STORE (dst_folder));
5628                 connect_info->data = info;
5629
5630                 modest_platform_double_connect_and_perform(GTK_WINDOW (win), TRUE,
5631                                                            TNY_FOLDER_STORE (src_folder), 
5632                                                            connect_info);
5633         }
5634         /* Frees */
5635         g_object_unref (src_folder);
5636 }
5637 #endif
5638
5639
5640 void
5641 modest_ui_actions_transfer_messages_helper (GtkWindow *win,
5642                                             TnyFolder *src_folder,
5643                                             TnyList *headers,
5644                                             TnyFolder *dst_folder)
5645 {
5646         gboolean need_connection = TRUE;
5647         gboolean do_xfer = TRUE;
5648         XferMsgsHelper *helper;
5649
5650         g_return_if_fail (TNY_IS_FOLDER (src_folder));
5651         g_return_if_fail (TNY_IS_FOLDER (dst_folder));
5652         g_return_if_fail (TNY_IS_LIST (headers));
5653         
5654         modest_ui_actions_xfer_messages_check (win, TNY_FOLDER_STORE (src_folder), 
5655                                                headers, TNY_FOLDER (dst_folder),
5656                                                TRUE, &need_connection, 
5657                                                &do_xfer);
5658
5659         /* If we don't want to transfer just return */
5660         if (!do_xfer)
5661                 return;
5662
5663         /* Create the helper */
5664         helper = g_slice_new (XferMsgsHelper);
5665         helper->dst_folder = g_object_ref (dst_folder);
5666         helper->headers = g_object_ref (headers);
5667
5668         if (need_connection) {
5669                 DoubleConnectionInfo *connect_info = g_slice_new (DoubleConnectionInfo);
5670                 connect_info->callback = xfer_messages_performer;
5671                 connect_info->dst_account = tny_folder_get_account (TNY_FOLDER (dst_folder));
5672                 connect_info->data = helper;
5673                 
5674                 modest_platform_double_connect_and_perform(GTK_WINDOW (win), TRUE,
5675                                                            TNY_FOLDER_STORE (src_folder), 
5676                                                            connect_info);
5677         } else {
5678                 TnyAccount *src_account = get_account_from_folder_store (TNY_FOLDER_STORE (src_folder));
5679                 xfer_messages_performer (FALSE, NULL, GTK_WINDOW (win),
5680                                          src_account, helper);
5681                 g_object_unref (src_account);
5682         }
5683 }
5684
5685 /*
5686  * UI handler for the "Move to" action when invoked from the
5687  * ModestMsgViewWindow
5688  */
5689 static void
5690 modest_ui_actions_on_window_move_to (GtkAction *action,
5691                                      TnyList *headers,
5692                                      TnyFolderStore *dst_folder,
5693                                      ModestWindow *win)
5694 {
5695         TnyFolder *src_folder = NULL;
5696
5697         g_return_if_fail (TNY_IS_FOLDER (dst_folder));
5698
5699         if (headers) {
5700                 TnyHeader *header = NULL;
5701                 TnyIterator *iter;
5702
5703                 iter = tny_list_create_iterator (headers);
5704                 header = (TnyHeader *) tny_iterator_get_current (iter);
5705                 src_folder = tny_header_get_folder (header);
5706
5707                 /* Transfer the messages */
5708                 modest_ui_actions_transfer_messages_helper (GTK_WINDOW (win), src_folder, 
5709                                                             headers,
5710                                                             TNY_FOLDER (dst_folder));
5711
5712                 /* Frees */
5713                 g_object_unref (header);
5714                 g_object_unref (iter);
5715                 g_object_unref (src_folder);
5716         }
5717 }
5718
5719 void 
5720 modest_ui_actions_on_move_to (GtkAction *action, 
5721                               ModestWindow *win)
5722 {
5723         modest_ui_actions_on_edit_mode_move_to (win);
5724 }
5725
5726 gboolean 
5727 modest_ui_actions_on_edit_mode_move_to (ModestWindow *win)
5728 {
5729         GtkWidget *dialog = NULL, *folder_view = NULL;
5730         ModestMainWindow *main_window;
5731         MoveToInfo *helper = NULL;
5732
5733         g_return_val_if_fail (MODEST_IS_WINDOW (win), FALSE);
5734
5735         /* Get the main window if exists */
5736         if (MODEST_IS_MAIN_WINDOW (win))
5737                 main_window = MODEST_MAIN_WINDOW (win);
5738         else
5739                 main_window = 
5740                         MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
5741                                                                                FALSE)); /* don't create */
5742
5743         /* Get the folder view widget if exists */
5744         if (main_window)
5745                 folder_view = modest_main_window_get_child_widget (main_window,
5746                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
5747         else
5748                 folder_view = NULL;
5749
5750         /* Create and run the dialog */
5751         dialog = create_move_to_dialog (GTK_WINDOW (win), folder_view);
5752         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
5753                                      GTK_WINDOW (dialog), 
5754                                      (GtkWindow *) win);
5755
5756         /* Create helper */
5757         helper = g_slice_new0 (MoveToInfo);
5758         helper->list = modest_platform_get_list_to_move (MODEST_WINDOW (win));
5759         helper->win = win;
5760
5761         /* Listen to response signal */
5762         g_signal_connect (dialog, "response", G_CALLBACK (on_move_to_dialog_response), helper);
5763
5764         /* Show the dialog */
5765         gtk_widget_show (dialog);
5766
5767         return TRUE;
5768 }
5769
5770 /*
5771  * Calls #HeadersFunc for each header already selected in the main
5772  * window or the message currently being shown in the msg view window
5773  */
5774 static void
5775 do_headers_action (ModestWindow *win, 
5776                    HeadersFunc func,
5777                    gpointer user_data)
5778 {
5779         TnyList *headers_list = NULL;
5780         TnyIterator *iter = NULL;
5781         TnyHeader *header = NULL;
5782         TnyFolder *folder = NULL;
5783
5784         /* Get headers */
5785         headers_list = get_selected_headers (win);
5786         if (!headers_list)
5787                 return;
5788
5789         /* Get the folder */
5790         iter = tny_list_create_iterator (headers_list);
5791         header = TNY_HEADER (tny_iterator_get_current (iter));
5792         if (header) {
5793                 folder = tny_header_get_folder (header);
5794                 g_object_unref (header);
5795         }
5796
5797         /* Call the function for each header */
5798         while (!tny_iterator_is_done (iter)) {
5799                 header = TNY_HEADER (tny_iterator_get_current (iter));
5800                 func (header, win, user_data);
5801                 g_object_unref (header);
5802                 tny_iterator_next (iter);
5803         }
5804
5805         /* Trick: do a poke status in order to speed up the signaling
5806            of observers */
5807         tny_folder_poke_status (folder);
5808
5809         /* Frees */
5810         g_object_unref (folder);
5811         g_object_unref (iter);
5812         g_object_unref (headers_list);
5813 }
5814
5815 void 
5816 modest_ui_actions_view_attachment (GtkAction *action,
5817                                    ModestWindow *window)
5818 {
5819         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
5820                 modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (window), NULL);
5821         } else {
5822                 /* not supported window for this action */
5823                 g_return_if_reached ();
5824         }
5825 }
5826
5827 void
5828 modest_ui_actions_save_attachments (GtkAction *action,
5829                                     ModestWindow *window)
5830 {
5831         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
5832
5833                 if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
5834                         return;
5835
5836                 modest_msg_view_window_save_attachments (MODEST_MSG_VIEW_WINDOW (window), NULL);
5837         } else {
5838                 /* not supported window for this action */
5839                 g_return_if_reached ();
5840         }
5841 }
5842
5843 void
5844 modest_ui_actions_remove_attachments (GtkAction *action,
5845                                       ModestWindow *window)
5846 {
5847         if (MODEST_IS_MAIN_WINDOW (window)) {
5848                 modest_ui_actions_on_main_window_remove_attachments (action, MODEST_MAIN_WINDOW (window));
5849         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
5850                 modest_msg_view_window_remove_attachments (MODEST_MSG_VIEW_WINDOW (window), FALSE);
5851         } else {
5852                 /* not supported window for this action */
5853                 g_return_if_reached ();
5854         }
5855 }
5856
5857 void 
5858 modest_ui_actions_on_settings (GtkAction *action, 
5859                                ModestWindow *win)
5860 {
5861         GtkWidget *dialog;
5862
5863         dialog = modest_platform_get_global_settings_dialog ();
5864         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (win));
5865         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
5866         gtk_widget_show_all (dialog);
5867
5868         gtk_dialog_run (GTK_DIALOG (dialog));
5869
5870         gtk_widget_destroy (dialog);
5871 }
5872
5873 void 
5874 modest_ui_actions_on_help (GtkAction *action, 
5875                            GtkWindow *win)
5876 {
5877         /* Help app is not available at all in fremantle */
5878 #ifndef MODEST_TOOLKIT_HILDON2
5879         const gchar *help_id;
5880
5881         g_return_if_fail (win && GTK_IS_WINDOW(win));
5882         
5883         help_id = modest_window_mgr_get_help_id (modest_runtime_get_window_mgr(), win);
5884
5885         if (help_id)
5886                 modest_platform_show_help (GTK_WINDOW (win), help_id);
5887 #endif
5888 }
5889
5890 void 
5891 modest_ui_actions_on_csm_help (GtkAction *action, 
5892                                GtkWindow *win)
5893 {
5894         /* Help app is not available at all in fremantle */
5895 #ifndef MODEST_TOOLKIT_HILDON2
5896
5897         const gchar* help_id = NULL;
5898         GtkWidget *folder_view;
5899         TnyFolderStore *folder_store;
5900
5901         g_return_if_fail (win && MODEST_IS_MAIN_WINDOW (win));
5902
5903         /* Get selected folder */
5904         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
5905                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
5906         folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
5907
5908         /* Switch help_id */
5909         if (folder_store && TNY_IS_FOLDER (folder_store))
5910                 help_id = modest_tny_folder_get_help_id (TNY_FOLDER (folder_store));
5911
5912         if (folder_store)
5913                 g_object_unref (folder_store);
5914
5915         if (help_id)
5916                 modest_platform_show_help (GTK_WINDOW (win), help_id);
5917         else
5918                 modest_ui_actions_on_help (action, win);
5919 #endif
5920 }
5921
5922 static void     
5923 retrieve_contents_cb (ModestMailOperation *mail_op, 
5924                       TnyHeader *header, 
5925                       gboolean canceled,
5926                       TnyMsg *msg,
5927                       GError *err,
5928                       gpointer user_data)
5929 {
5930         /* We only need this callback to show an error in case of
5931            memory low condition */
5932         modest_ui_actions_msg_retrieval_check (mail_op, header, msg);
5933 }
5934
5935 static void
5936 retrieve_msg_contents_performer (gboolean canceled, 
5937                                  GError *err,
5938                                  GtkWindow *parent_window, 
5939                                  TnyAccount *account, 
5940                                  gpointer user_data)
5941 {
5942         ModestMailOperation *mail_op;
5943         TnyList *headers = TNY_LIST (user_data);
5944
5945         if (err || canceled) {
5946                 check_memory_full_error ((GtkWidget *) parent_window, err);
5947                 goto out;
5948         }
5949
5950         /* Create mail operation */
5951         mail_op = modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
5952                                                                  modest_ui_actions_disk_operations_error_handler, 
5953                                                                  NULL, NULL);
5954         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
5955         modest_mail_operation_get_msgs_full (mail_op, headers, retrieve_contents_cb, NULL, NULL);
5956
5957         /* Frees */
5958         g_object_unref (mail_op);
5959  out:
5960         g_object_unref (headers);
5961         g_object_unref (account);
5962 }
5963
5964 void 
5965 modest_ui_actions_on_retrieve_msg_contents (GtkAction *action,
5966                                             ModestWindow *window)
5967 {
5968         TnyList *headers = NULL;
5969         TnyAccount *account = NULL;
5970         TnyIterator *iter = NULL;
5971         TnyHeader *header = NULL;
5972         TnyFolder *folder = NULL;
5973
5974         /* Get headers */
5975         headers = get_selected_headers (window);
5976         if (!headers)
5977                 return;
5978
5979         /* Pick the account */
5980         iter = tny_list_create_iterator (headers);
5981         header = TNY_HEADER (tny_iterator_get_current (iter));
5982         folder = tny_header_get_folder (header);
5983         account = tny_folder_get_account (folder);
5984         g_object_unref (folder);
5985         g_object_unref (header);
5986         g_object_unref (iter);
5987
5988         /* Connect and perform the message retrieval */
5989         modest_platform_connect_and_perform ((GtkWindow *) window, TRUE,
5990                                              g_object_ref (account), 
5991                                              retrieve_msg_contents_performer, 
5992                                              g_object_ref (headers));
5993
5994         /* Frees */
5995         g_object_unref (account);
5996         g_object_unref (headers);
5997 }
5998
5999 void
6000 modest_ui_actions_check_toolbar_dimming_rules (ModestWindow *window)
6001 {
6002         g_return_if_fail (MODEST_IS_WINDOW (window));
6003
6004         /* Update dimmed */
6005         modest_window_check_dimming_rules_group (window, MODEST_DIMMING_RULES_TOOLBAR);
6006 }
6007
6008 void
6009 modest_ui_actions_check_menu_dimming_rules (ModestWindow *window)
6010 {
6011         g_return_if_fail (MODEST_IS_WINDOW (window));
6012
6013         /* Update dimmed */
6014         modest_window_check_dimming_rules_group (window, MODEST_DIMMING_RULES_MENU);
6015 }
6016
6017 void
6018 modest_ui_actions_on_email_menu_activated (GtkAction *action,
6019                                           ModestWindow *window)
6020 {
6021         g_return_if_fail (MODEST_IS_WINDOW (window));
6022         
6023         /* Update dimmed */
6024         modest_ui_actions_check_menu_dimming_rules (window);
6025 }
6026
6027 void
6028 modest_ui_actions_on_edit_menu_activated (GtkAction *action,
6029                                           ModestWindow *window)
6030 {
6031         g_return_if_fail (MODEST_IS_WINDOW (window));
6032
6033         /* Update dimmed */
6034         modest_ui_actions_check_menu_dimming_rules (window);
6035 }
6036
6037 void
6038 modest_ui_actions_on_view_menu_activated (GtkAction *action,
6039                                           ModestWindow *window)
6040 {
6041         g_return_if_fail (MODEST_IS_WINDOW (window));
6042
6043         /* Update dimmed */
6044         modest_ui_actions_check_menu_dimming_rules (window);
6045 }
6046
6047 void
6048 modest_ui_actions_on_format_menu_activated (GtkAction *action,
6049                                             ModestWindow *window)
6050 {
6051         g_return_if_fail (MODEST_IS_WINDOW (window));
6052
6053         /* Update dimmed */
6054         modest_ui_actions_check_menu_dimming_rules (window);
6055 }
6056
6057 void
6058 modest_ui_actions_on_tools_menu_activated (GtkAction *action,
6059                                           ModestWindow *window)
6060 {
6061         g_return_if_fail (MODEST_IS_WINDOW (window));
6062
6063         /* Update dimmed */
6064         modest_ui_actions_check_menu_dimming_rules (window);
6065 }
6066
6067 void
6068 modest_ui_actions_on_attachment_menu_activated (GtkAction *action,
6069                                           ModestWindow *window)
6070 {
6071         g_return_if_fail (MODEST_IS_WINDOW (window));
6072
6073         /* Update dimmed */
6074         modest_ui_actions_check_menu_dimming_rules (window);
6075 }
6076
6077 void
6078 modest_ui_actions_on_toolbar_csm_menu_activated (GtkAction *action,
6079                                                  ModestWindow *window)
6080 {
6081         g_return_if_fail (MODEST_IS_WINDOW (window));
6082
6083         /* Update dimmed */
6084         modest_ui_actions_check_menu_dimming_rules (window);
6085 }
6086
6087 void
6088 modest_ui_actions_on_folder_view_csm_menu_activated (GtkAction *action,
6089                                                      ModestWindow *window)
6090 {
6091         g_return_if_fail (MODEST_IS_WINDOW (window));
6092
6093         /* Update dimmed */
6094         modest_ui_actions_check_menu_dimming_rules (window);
6095 }
6096
6097 void
6098 modest_ui_actions_on_header_view_csm_menu_activated (GtkAction *action,
6099                                                      ModestWindow *window)
6100 {
6101         g_return_if_fail (MODEST_IS_WINDOW (window));
6102
6103         /* Update dimmed */
6104         modest_ui_actions_check_menu_dimming_rules (window);
6105 }
6106
6107 void
6108 modest_ui_actions_on_search_messages (GtkAction *action, ModestWindow *window)
6109 {
6110         g_return_if_fail (MODEST_IS_WINDOW (window));
6111
6112         /* we check for low-mem; in that case, show a warning, and don't allow
6113          * searching
6114          */
6115         if (modest_platform_check_memory_low (window, TRUE))
6116                 return;
6117         
6118         modest_platform_show_search_messages (GTK_WINDOW (window));
6119 }
6120
6121 void     
6122 modest_ui_actions_on_open_addressbook (GtkAction *action, ModestWindow *win)
6123 {
6124         g_return_if_fail (MODEST_IS_WINDOW (win));
6125
6126
6127         /* we check for low-mem; in that case, show a warning, and don't allow
6128          * for the addressbook
6129          */
6130         if (modest_platform_check_memory_low (win, TRUE))
6131                 return;
6132
6133
6134         modest_platform_show_addressbook (GTK_WINDOW (win));
6135 }
6136
6137
6138 void
6139 modest_ui_actions_on_toggle_find_in_page (GtkToggleAction *action,
6140                                           ModestWindow *window)
6141 {
6142         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
6143
6144         modest_msg_edit_window_toggle_find_toolbar (MODEST_MSG_EDIT_WINDOW (window), gtk_toggle_action_get_active (action));
6145 }
6146
6147 static void 
6148 on_send_receive_finished (ModestMailOperation  *mail_op, 
6149                            gpointer user_data)
6150 {
6151         GtkWidget *header_view, *folder_view;
6152         TnyFolderStore *folder_store;
6153         ModestMainWindow *main_win = MODEST_MAIN_WINDOW (user_data);
6154
6155         /* Set send/receive operation finished */       
6156         modest_main_window_notify_send_receive_completed (main_win);
6157
6158         /* Don't refresh the current folder if there were any errors */
6159         if (modest_mail_operation_get_status (mail_op) !=
6160             MODEST_MAIL_OPERATION_STATUS_SUCCESS)
6161                 return;
6162         
6163         /* Refresh the current folder if we're viewing a window. We do
6164            this because the user won't be able to see the new mails in
6165            the selected folder after a Send&Receive because it only
6166            performs a poke_status, i.e, only the number of read/unread
6167            messages is updated, but the new headers are not
6168            downloaded */
6169         folder_view = modest_main_window_get_child_widget (main_win, 
6170                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
6171         if (!folder_view)
6172                 return;
6173
6174         folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
6175         
6176         /* Do not need to refresh INBOX again because the
6177            update_account does it always automatically */
6178         if (folder_store && TNY_IS_FOLDER (folder_store) && 
6179             tny_folder_get_folder_type (TNY_FOLDER (folder_store)) != TNY_FOLDER_TYPE_INBOX) {
6180                 ModestMailOperation *refresh_op;
6181
6182                 header_view = modest_main_window_get_child_widget (main_win,
6183                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
6184                 
6185                 /* We do not need to set the contents style
6186                    because it hasn't changed. We also do not
6187                    need to save the widget status. Just force
6188                    a refresh */
6189                 refresh_op = modest_mail_operation_new (G_OBJECT (main_win));
6190                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), refresh_op);
6191                 modest_mail_operation_refresh_folder (refresh_op, TNY_FOLDER (folder_store),
6192                                                       folder_refreshed_cb, main_win);
6193                 g_object_unref (refresh_op);
6194         }
6195         
6196         if (folder_store)
6197                 g_object_unref (folder_store);
6198 }
6199
6200
6201 void 
6202 modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self, 
6203                                                 TnyHeader *header, 
6204                                                 TnyMsg *msg, 
6205                                                 GError *err, 
6206                                                 gpointer user_data)
6207 {
6208         const gchar* server_name = NULL;
6209         TnyTransportAccount *server_account;
6210         gchar *message = NULL;
6211
6212         /* Don't show anything if the user cancelled something or the
6213          * send receive request is not interactive. Authentication
6214          * errors are managed by the account store so no need to show
6215          * a dialog here again */
6216         if (err->code == TNY_SYSTEM_ERROR_CANCEL ||
6217             err->code == TNY_SERVICE_ERROR_AUTHENTICATE ||
6218             !modest_tny_send_queue_get_requested_send_receive (MODEST_TNY_SEND_QUEUE (self)))
6219                 return;
6220
6221
6222         /* Get the server name: */
6223         server_account = 
6224                 TNY_TRANSPORT_ACCOUNT (tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE (self)));
6225         if (server_account)
6226                 server_name = tny_account_get_hostname (TNY_ACCOUNT (server_account));          
6227         else
6228                 g_return_if_reached ();
6229
6230         /* Show the appropriate message text for the GError: */
6231         switch (err->code) {
6232         case TNY_SERVICE_ERROR_CONNECT:
6233                 message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
6234                 break;
6235         case TNY_SERVICE_ERROR_SEND:
6236                 message = g_strdup (dgettext("hildon-common-strings", "sfil_ib_unable_to_send"));
6237                 break;
6238         case TNY_SERVICE_ERROR_UNAVAILABLE:
6239                 message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
6240                 break;
6241         default:
6242                 g_warning ("%s: unexpected ERROR %d",
6243                            __FUNCTION__, err->code);
6244                 message = g_strdup (dgettext("hildon-common-strings", "sfil_ib_unable_to_send"));
6245                 break;  
6246         }
6247
6248         modest_platform_run_information_dialog (NULL, message, FALSE);
6249         g_free (message);
6250         g_object_unref (server_account);
6251 }
6252
6253 void
6254 modest_ui_actions_on_send_queue_status_changed (ModestTnySendQueue *send_queue,
6255                                                 gchar *msg_id, 
6256                                                 guint status,
6257                                                 gpointer user_data)
6258 {
6259         ModestMainWindow *main_window = NULL;
6260         ModestWindowMgr *mgr = NULL;
6261         GtkWidget *folder_view = NULL, *header_view = NULL;
6262         TnyFolderStore *selected_folder = NULL;
6263         TnyFolderType folder_type;
6264
6265         mgr = modest_runtime_get_window_mgr ();
6266         main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (mgr,
6267                                                                              FALSE));/* don't create */
6268         if (!main_window)
6269                 return;
6270
6271         /* Check if selected folder is OUTBOX */
6272         folder_view = modest_main_window_get_child_widget (main_window,
6273                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
6274         header_view = modest_main_window_get_child_widget (main_window,
6275                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
6276
6277         selected_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
6278         if (!TNY_IS_FOLDER (selected_folder)) 
6279                 goto frees;
6280
6281         /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
6282 #if GTK_CHECK_VERSION(2, 8, 0) 
6283         folder_type = modest_tny_folder_guess_folder_type (TNY_FOLDER (selected_folder)); 
6284         if (folder_type ==  TNY_FOLDER_TYPE_OUTBOX) {           
6285                 GtkTreeViewColumn *tree_column;
6286
6287                 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (header_view), 
6288                                                         TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN);
6289                 if (tree_column)
6290                         gtk_tree_view_column_queue_resize (tree_column);
6291         }
6292 #else
6293         gtk_widget_queue_draw (header_view);
6294 #endif          
6295
6296         /* Rerun dimming rules, because the message could become deletable for example */
6297         modest_window_check_dimming_rules_group (MODEST_WINDOW (main_window), 
6298                                                  MODEST_DIMMING_RULES_TOOLBAR);
6299         modest_window_check_dimming_rules_group (MODEST_WINDOW (main_window), 
6300                                                  MODEST_DIMMING_RULES_MENU);
6301         
6302         /* Free */
6303  frees:
6304         if (selected_folder != NULL)
6305                 g_object_unref (selected_folder);
6306 }
6307
6308 void 
6309 modest_ui_actions_on_account_connection_error (GtkWindow *parent_window,
6310                                                TnyAccount *account)
6311 {
6312         ModestProtocolType protocol_type;
6313         ModestProtocol *protocol;
6314         gchar *error_note = NULL;
6315         
6316         protocol_type = modest_tny_account_get_protocol_type (account);
6317         protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
6318                                                                   protocol_type);
6319
6320         error_note = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, tny_account_get_hostname (account));
6321         if (error_note == NULL) {
6322                 g_warning ("%s: This should not be reached", __FUNCTION__);
6323         } else {
6324                 modest_platform_run_information_dialog (parent_window, error_note, FALSE);
6325                 g_free (error_note);
6326         }
6327 }
6328
6329 gchar *
6330 modest_ui_actions_get_msg_already_deleted_error_msg (ModestWindow *win)
6331 {
6332         gchar *msg = NULL;
6333         gchar *subject;
6334         TnyFolderStore *folder = NULL;
6335         TnyAccount *account = NULL;
6336         ModestProtocolType proto;
6337         ModestProtocol *protocol;
6338         TnyHeader *header = NULL;
6339
6340         if (MODEST_IS_MAIN_WINDOW (win)) {
6341                 GtkWidget *header_view;
6342                 TnyList* headers = NULL;
6343                 TnyIterator *iter;
6344                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
6345                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
6346                 headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
6347                 if (!headers || tny_list_get_length (headers) == 0) {
6348                         if (headers)
6349                                 g_object_unref (headers);
6350                         return NULL;
6351                 }
6352                 iter = tny_list_create_iterator (headers);
6353                 header = TNY_HEADER (tny_iterator_get_current (iter));
6354                 folder = TNY_FOLDER_STORE (tny_header_get_folder (header));
6355                 g_object_unref (iter);
6356                 g_object_unref (headers);
6357 #ifdef MODEST_TOOLKIT_HILDON2
6358         } else if (MODEST_IS_HEADER_WINDOW (win)) {
6359                 GtkWidget *header_view;
6360                 TnyList* headers = NULL;
6361                 TnyIterator *iter;
6362                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
6363                 headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
6364                 if (!headers || tny_list_get_length (headers) == 0) {
6365                         if (headers)
6366                                 g_object_unref (headers);
6367                         return NULL;
6368                 }
6369                 iter = tny_list_create_iterator (headers);
6370                 header = TNY_HEADER (tny_iterator_get_current (iter));
6371                 folder = TNY_FOLDER_STORE (tny_header_get_folder (header));
6372                 g_object_unref (iter);
6373                 g_object_unref (headers);
6374 #endif
6375         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
6376                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
6377                 folder = TNY_FOLDER_STORE (tny_header_get_folder (header));
6378         }
6379
6380         /* Get the account type */
6381         account = tny_folder_get_account (TNY_FOLDER (folder));
6382         proto = modest_tny_account_get_protocol_type (account);
6383         protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
6384                                                                   proto);
6385
6386         subject = tny_header_dup_subject (header);
6387         msg = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE, subject);
6388         if (subject)
6389                 g_free (subject);
6390         if (msg == NULL) {
6391                 msg = g_strdup_printf (_("mail_ni_ui_folder_get_msg_folder_error"));
6392         }
6393
6394         /* Frees */
6395         g_object_unref (account);
6396         g_object_unref (folder);
6397         g_object_unref (header);
6398
6399         return msg;
6400 }
6401
6402 gboolean
6403 modest_ui_actions_on_delete_account (GtkWindow *parent_window,
6404                                      const gchar *account_name,
6405                                      const gchar *account_title)
6406 {
6407         ModestAccountMgr *account_mgr;
6408         gchar *txt = NULL;
6409         gint response;
6410         ModestProtocol *protocol;
6411         gboolean removed = FALSE;
6412
6413         g_return_val_if_fail (account_name, FALSE);
6414         g_return_val_if_fail (account_title, FALSE);
6415
6416         account_mgr = modest_runtime_get_account_mgr();
6417
6418         /* The warning text depends on the account type: */
6419         protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
6420                                                                   modest_account_mgr_get_store_protocol (account_mgr,
6421                                                                                                          account_name));
6422         txt = modest_protocol_get_translation (protocol,
6423                                                MODEST_PROTOCOL_TRANSLATION_DELETE_MAILBOX,
6424                                                account_title);
6425         if (txt == NULL)
6426                 txt = g_strdup_printf (_("emev_nc_delete_mailbox"), account_title);
6427
6428         response = modest_platform_run_confirmation_dialog (parent_window, txt);
6429         g_free (txt);
6430         txt = NULL;
6431
6432         if (response == GTK_RESPONSE_OK) {
6433                 /* Remove account. If it succeeds then it also removes
6434                    the account from the ModestAccountView: */
6435                 gboolean is_default = FALSE;
6436                 gchar *default_account_name = modest_account_mgr_get_default_account (account_mgr);
6437                 if (default_account_name && (strcmp (default_account_name, account_name) == 0))
6438                         is_default = TRUE;
6439                 g_free (default_account_name);
6440
6441                 removed = modest_account_mgr_remove_account (account_mgr, account_name);
6442                 if (!removed)
6443                         g_warning ("%s: modest_account_mgr_remove_account() failed.\n", __FUNCTION__);
6444         }
6445         return removed;
6446 }