* Refactored two error handlers
[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-protocol-info.h"
45 #include "modest-tny-platform-factory.h"
46 #include "modest-platform.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
52 #ifdef MODEST_PLATFORM_MAEMO
53 #include "maemo/modest-osso-state-saving.h"
54 #include "maemo/modest-maemo-utils.h"
55 #include "maemo/modest-hildon-includes.h"
56 #endif /* MODEST_PLATFORM_MAEMO */
57
58 #include "widgets/modest-ui-constants.h"
59 #include <widgets/modest-main-window.h>
60 #include <widgets/modest-msg-view-window.h>
61 #include <widgets/modest-account-view-window.h>
62 #include <widgets/modest-details-dialog.h>
63 #include <widgets/modest-attachments-view.h>
64 #include "widgets/modest-folder-view.h"
65 #include "widgets/modest-global-settings-dialog.h"
66 #include "modest-connection-specific-smtp-window.h"
67 #include "modest-account-mgr-helpers.h"
68 #include "modest-mail-operation.h"
69 #include "modest-text-utils.h"
70
71 #ifdef MODEST_HAVE_EASYSETUP
72 #include "easysetup/modest-easysetup-wizard.h"
73 #endif /* MODEST_HAVE_EASYSETUP */
74
75 #include <modest-widget-memory.h>
76 #include <tny-error.h>
77 #include <tny-simple-list.h>
78 #include <tny-msg-view.h>
79 #include <tny-device.h>
80 #include <tny-merge-folder.h>
81
82 #include <gtkhtml/gtkhtml.h>
83
84 typedef struct _GetMsgAsyncHelper {     
85         ModestWindow *window;
86         ModestMailOperation *mail_op;
87         TnyIterator *iter;
88         guint num_ops;
89         GFunc func;     
90         gpointer user_data;
91 } GetMsgAsyncHelper;
92
93 typedef enum _ReplyForwardAction {
94         ACTION_REPLY,
95         ACTION_REPLY_TO_ALL,
96         ACTION_FORWARD
97 } ReplyForwardAction;
98
99 typedef struct _ReplyForwardHelper {
100         guint reply_forward_type;
101         ReplyForwardAction action;
102         gchar *account_name;
103         GtkWidget *parent_window;
104 } ReplyForwardHelper;
105
106 typedef struct _MoveToHelper {
107         GtkTreeRowReference *reference;
108         GtkWidget *banner;
109 } MoveToHelper;
110
111 typedef struct _PasteAsAttachmentHelper {
112         ModestMsgEditWindow *window;
113         GtkWidget *banner;
114 } PasteAsAttachmentHelper;
115
116
117 /*
118  * The do_headers_action uses this kind of functions to perform some
119  * action to each member of a list of headers
120  */
121 typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data);
122
123 static void     do_headers_action     (ModestWindow *win, 
124                                        HeadersFunc func,
125                                        gpointer user_data);
126
127 static void     open_msg_cb            (ModestMailOperation *mail_op, 
128                                         TnyHeader *header, 
129                                         gboolean canceled,
130                                         TnyMsg *msg,
131                                         GError *err,
132                                         gpointer user_data);
133
134 static void     reply_forward_cb       (ModestMailOperation *mail_op, 
135                                         TnyHeader *header, 
136                                         gboolean canceled,
137                                         TnyMsg *msg,
138                                         GError *err,
139                                         gpointer user_data);
140
141 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
142
143 static void     folder_refreshed_cb    (ModestMailOperation *mail_op, 
144                                         TnyFolder *folder, 
145                                         gpointer user_data);
146
147 static void     on_send_receive_finished (ModestMailOperation  *mail_op, 
148                                           gpointer user_data);
149
150 static gint header_list_count_uncached_msgs (TnyList *header_list);
151
152 static gboolean connect_to_get_msg (ModestWindow *win,
153                                     gint num_of_uncached_msgs,
154                                     TnyAccount *account);
155
156 static gboolean remote_folder_is_pop (const TnyFolderStore *folder);
157
158 static gboolean msgs_already_deleted_from_server ( TnyList *headers,
159                                                    const TnyFolderStore *src_folder);
160
161
162 /*
163  * This function checks whether a TnyFolderStore is a pop account
164  */
165 static gboolean
166 remote_folder_is_pop (const TnyFolderStore *folder)
167 {
168         const gchar *proto = NULL;
169         TnyAccount *account = NULL;
170
171         g_return_val_if_fail (TNY_IS_FOLDER_STORE(folder), FALSE);
172
173         if (TNY_IS_ACCOUNT (folder)) {
174                 account = TNY_ACCOUNT(folder);
175                 g_object_ref(account);
176         } else if (TNY_IS_FOLDER (folder)) {
177                 account = tny_folder_get_account(TNY_FOLDER(folder));
178         }
179
180         if (!account && !TNY_IS_ACCOUNT(account)) {
181                 g_warning ("%s: could not get account", __FUNCTION__);
182                 return FALSE;
183         }
184         
185         proto = tny_account_get_proto(account);
186         g_object_unref (account);
187
188         return proto &&
189           (modest_protocol_info_get_transport_store_protocol (proto) == MODEST_PROTOCOL_STORE_POP);
190 }
191
192 /*
193  * This functions checks whether if a list of messages are already
194  * deleted from the server: that is, if the server is a POP account
195  * and all messages are already cached.
196  */
197 static gboolean
198 msgs_already_deleted_from_server (TnyList *headers, const TnyFolderStore *src_folder)
199 {
200         g_return_val_if_fail (TNY_IS_FOLDER_STORE(src_folder), FALSE);
201         g_return_val_if_fail (TNY_IS_LIST(headers), FALSE);
202
203         gboolean src_is_pop = remote_folder_is_pop (src_folder);
204         gint uncached_msgs = header_list_count_uncached_msgs (headers);
205
206         return (src_is_pop && !uncached_msgs);
207 }
208
209
210 /* FIXME: this should be merged with the similar code in modest-account-view-window */
211 /* Show the account creation wizard dialog.
212  * returns: TRUE if an account was created. FALSE if the user cancelled.
213  */
214 gboolean
215 modest_ui_actions_run_account_setup_wizard (ModestWindow *win)
216 {
217         gboolean result = FALSE;        
218         GtkWindow *dialog, *wizard;
219         gint dialog_response;
220
221         /* Show the easy-setup wizard: */       
222         dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
223         if (dialog && MODEST_IS_EASYSETUP_WIZARD_DIALOG(dialog)) {
224                 /* old wizard is active already; 
225                  */
226                 gtk_window_present (GTK_WINDOW(dialog));
227                 return FALSE;
228         }
229         
230
231         /* there is no such wizard yet */       
232         wizard = GTK_WINDOW (modest_easysetup_wizard_dialog_new ());
233         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(), wizard);
234
235         /* always present a main window in the background 
236          * we do it here, so we cannot end up with two wizards (as this
237          * function might be called in modest_window_mgr_get_main_window as well */
238         if (!win) 
239                 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
240                                                          TRUE);  /* create if not existent */
241         
242         /* make sure the mainwindow is visible */
243         gtk_window_set_transient_for (GTK_WINDOW (wizard), GTK_WINDOW (win));
244         gtk_widget_show_all (GTK_WIDGET(win));
245         gtk_window_present (GTK_WINDOW(win));
246         
247         dialog_response = gtk_dialog_run (GTK_DIALOG (wizard));
248         gtk_widget_destroy (GTK_WIDGET (wizard));
249         if (gtk_events_pending ())
250                 gtk_main_iteration ();
251
252         if (dialog_response == GTK_RESPONSE_CANCEL) {
253                 result = FALSE;
254         } else {
255                 /* Check whether an account was created: */
256                 result = modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
257         }
258         
259         return result;
260 }
261
262
263 void   
264 modest_ui_actions_on_about (GtkAction *action, ModestWindow *win)
265 {
266         GtkWidget *about;
267         const gchar *authors[] = {
268                 "Dirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>",
269                 NULL
270         };
271         about = gtk_about_dialog_new ();
272         gtk_about_dialog_set_name (GTK_ABOUT_DIALOG(about), PACKAGE_NAME);
273         gtk_about_dialog_set_version (GTK_ABOUT_DIALOG(about),PACKAGE_VERSION);
274         gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG(about),
275                                         _("Copyright (c) 2006, Nokia Corporation\n"
276                                           "All rights reserved."));
277         gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(about),
278                                        _("a modest e-mail client\n\n"
279                                          "design and implementation: Dirk-Jan C. Binnema\n"
280                                          "contributions from the fine people at KC and Ig\n"
281                                          "uses the tinymail email framework written by Philip van Hoof"));
282         gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(about), authors);
283         gtk_about_dialog_set_website (GTK_ABOUT_DIALOG(about), "http://modest.garage.maemo.org");
284         gtk_window_set_transient_for (GTK_WINDOW (about), GTK_WINDOW (win));
285         gtk_window_set_modal (GTK_WINDOW (about), TRUE);
286         
287         gtk_dialog_run (GTK_DIALOG (about));
288         gtk_widget_destroy(about);
289 }
290
291 /*
292  * Gets the list of currently selected messages. If the win is the
293  * main window, then it returns a newly allocated list of the headers
294  * selected in the header view. If win is the msg view window, then
295  * the value returned is a list with just a single header.
296  *
297  * The caller of this funcion must free the list.
298  */
299 static TnyList *
300 get_selected_headers (ModestWindow *win)
301 {
302         if (MODEST_IS_MAIN_WINDOW(win)) {
303                 GtkWidget *header_view;         
304                 
305                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
306                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
307                 return modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
308                 
309         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
310                 /* for MsgViewWindows, we simply return a list with one element */
311                 TnyHeader *header;
312                 TnyList *list = NULL;
313                 
314                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
315                 if (header != NULL) {
316                         list = tny_simple_list_new ();
317                         tny_list_prepend (list, G_OBJECT(header));
318                         g_object_unref (G_OBJECT(header));
319                 }
320
321                 return list;
322
323         } else
324                 return NULL;
325 }
326
327 static GtkTreeRowReference *
328 get_next_after_selected_headers (ModestHeaderView *header_view)
329 {
330         GtkTreeSelection *sel;
331         GList *selected_rows, *node;
332         GtkTreePath *path;
333         GtkTreeRowReference *result;
334         GtkTreeModel *model;
335
336         model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
337         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
338         selected_rows = gtk_tree_selection_get_selected_rows (sel, NULL);
339
340         if (selected_rows == NULL)
341                 return NULL;
342
343         node = g_list_last (selected_rows);
344         path = gtk_tree_path_copy ((GtkTreePath *) node->data);
345         gtk_tree_path_next (path);
346
347         result = gtk_tree_row_reference_new (model, path);
348
349         gtk_tree_path_free (path);
350         g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
351         g_list_free (selected_rows);
352
353         return result;
354 }
355
356 static void
357 headers_action_mark_as_read (TnyHeader *header,
358                              ModestWindow *win,
359                              gpointer user_data)
360 {
361         TnyHeaderFlags flags;
362
363         g_return_if_fail (TNY_IS_HEADER(header));
364
365         flags = tny_header_get_flags (header);
366         if (flags & TNY_HEADER_FLAG_SEEN) return;
367         tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
368 }
369
370 static void
371 headers_action_mark_as_unread (TnyHeader *header,
372                                ModestWindow *win,
373                                gpointer user_data)
374 {
375         TnyHeaderFlags flags;
376
377         g_return_if_fail (TNY_IS_HEADER(header));
378
379         flags = tny_header_get_flags (header);
380         if (flags & TNY_HEADER_FLAG_SEEN)  {
381                 tny_header_unset_flag (header, TNY_HEADER_FLAG_SEEN);
382         }
383 }
384
385 /** After deleing a message that is currently visible in a window, 
386  * show the next message from the list, or close the window if there are no more messages.
387  **/
388 void 
389 modest_ui_actions_refresh_message_window_after_delete (ModestMsgViewWindow* win)
390 {
391         /* Close msg view window or select next */
392         if (modest_msg_view_window_last_message_selected (win) &&
393                 modest_msg_view_window_first_message_selected (win)) {
394                 modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (win));
395         } else if (!modest_msg_view_window_select_next_message (win)) {
396                 gboolean ret_value;
397                 g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);       
398         }
399 }
400
401
402 void
403 modest_ui_actions_on_delete_message (GtkAction *action, ModestWindow *win)
404 {
405         TnyList *header_list = NULL;
406         TnyIterator *iter = NULL;
407         TnyHeader *header = NULL;
408         gchar *message = NULL;
409         gchar *desc = NULL;
410         gint response;
411         ModestWindowMgr *mgr;
412         GtkWidget *header_view = NULL;
413
414         g_return_if_fail (MODEST_IS_WINDOW(win));
415         
416         /* Check first if the header view has the focus */
417         if (MODEST_IS_MAIN_WINDOW (win)) {
418                 header_view = 
419                         modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
420                                                              MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
421                 if (!gtk_widget_is_focus (header_view))
422                         return;
423         }
424         
425         /* Get the headers, either from the header view (if win is the main window),
426          * or from the message view window: */
427         header_list = get_selected_headers (win);
428         if (!header_list) return;
429                         
430         /* Check if any of the headers are already opened, or in the process of being opened */
431         if (MODEST_IS_MAIN_WINDOW (win)) {
432                 gint opened_headers = 0;
433
434                 iter = tny_list_create_iterator (header_list);
435                 mgr = modest_runtime_get_window_mgr ();
436                 while (!tny_iterator_is_done (iter)) {
437                         header = TNY_HEADER (tny_iterator_get_current (iter));
438                         if (header) {
439                                 if (modest_window_mgr_find_registered_header (mgr, header, NULL))
440                                         opened_headers++;
441                                 g_object_unref (header);
442                         }
443                         tny_iterator_next (iter);
444                 }
445                 g_object_unref (iter);
446
447                 if (opened_headers > 0) {
448                         gchar *msg;
449
450                         msg = g_strdup_printf (_("mcen_nc_unable_to_delete_n_messages"), 
451                                                opened_headers);
452
453                         modest_platform_run_information_dialog (GTK_WINDOW (win), (const gchar *) msg);
454                         
455                         g_free (msg);
456                         g_object_unref (header_list);
457                         return;
458                 }
459         }
460
461         /* Select message */
462         if (tny_list_get_length(header_list) == 1) {
463                 iter = tny_list_create_iterator (header_list);
464                 header = TNY_HEADER (tny_iterator_get_current (iter));
465                 if (header) {
466                         desc = g_strdup_printf ("%s", tny_header_get_subject (header)); 
467                         g_object_unref (header);
468                 }
469
470                 g_object_unref (iter);
471         }
472         message = g_strdup_printf(ngettext("emev_nc_delete_message", "emev_nc_delete_messages", 
473                                            tny_list_get_length(header_list)), desc);
474
475         /* Confirmation dialog */
476         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
477                                                             message);
478         
479
480         if (response == GTK_RESPONSE_OK) {      
481                 ModestWindow *main_window = NULL;
482                 ModestWindowMgr *mgr = NULL;
483                 GtkTreeModel *model = NULL;
484                 GtkTreeSelection *sel = NULL;
485                 GList *sel_list = NULL, *tmp = NULL;
486                 GtkTreeRowReference *next_row_reference = NULL;
487                 GtkTreeRowReference *prev_row_reference = NULL;
488                 GtkTreePath *next_path = NULL;
489                 GtkTreePath *prev_path = NULL;
490                 ModestMailOperation *mail_op = NULL;
491
492                 /* Find last selected row */                    
493                 if (MODEST_IS_MAIN_WINDOW (win)) {
494                         model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
495                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
496                         sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
497                         for (tmp=sel_list; tmp; tmp=tmp->next) {
498                                 if (tmp->next == NULL) {
499                                         prev_path = gtk_tree_path_copy((GtkTreePath *) tmp->data);
500                                         next_path = gtk_tree_path_copy((GtkTreePath *) tmp->data);
501
502                                         gtk_tree_path_prev (prev_path);
503                                         gtk_tree_path_next (next_path);
504
505                                         prev_row_reference = gtk_tree_row_reference_new (model, prev_path);
506                                         next_row_reference = gtk_tree_row_reference_new (model, next_path);
507                                 }
508                         }
509                 }
510                 
511                 /* Disable window dimming management */
512                 modest_window_disable_dimming (MODEST_WINDOW(win));
513
514                 /* Remove each header. If it's a view window header_view == NULL */
515                 mail_op = modest_mail_operation_new ((GObject *) win);
516                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
517                                                  mail_op);
518                 modest_mail_operation_remove_msgs (mail_op, header_list, FALSE);
519                 g_object_unref (mail_op);
520                 
521                 /* Enable window dimming management */
522                 if (sel != NULL) {
523                         gtk_tree_selection_unselect_all (sel);
524                 }
525                 modest_window_enable_dimming (MODEST_WINDOW(win));
526                 
527                 if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
528                         modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (win));
529                         
530                         /* Get main window */
531                         mgr = modest_runtime_get_window_mgr ();
532                         main_window = modest_window_mgr_get_main_window (mgr, FALSE); /* don't create */
533                 } else {                        
534                         /* Move cursor to next row */
535                         main_window = win; 
536
537                         /* Select next or previous row */
538                         if (gtk_tree_row_reference_valid (next_row_reference)) {
539 /*                              next_path = gtk_tree_row_reference_get_path (row_reference); */
540                                 gtk_tree_selection_select_path (sel, next_path);
541                         }
542                         else if (gtk_tree_row_reference_valid (prev_row_reference)) {                           
543                                 gtk_tree_selection_select_path (sel, prev_path);
544                         }
545
546                         /* Free */
547                         if (next_row_reference != NULL) 
548                                 gtk_tree_row_reference_free (next_row_reference);
549                         if (next_path != NULL) 
550                                 gtk_tree_path_free (next_path);                         
551                         if (prev_row_reference != NULL) 
552                                 gtk_tree_row_reference_free (prev_row_reference);
553                         if (prev_path != NULL) 
554                                 gtk_tree_path_free (prev_path);                         
555                 }
556                 
557                 /* Update toolbar dimming state */
558                 if (main_window)
559                         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
560
561                 /* Free */
562                 g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
563                 g_list_free (sel_list);
564         }
565
566         /* Free*/
567         g_free(message);
568         g_free(desc);
569         g_object_unref (header_list);
570 }
571
572
573
574
575 /* delete either message or folder, based on where we are */
576 void
577 modest_ui_actions_on_delete_message_or_folder (GtkAction *action, ModestWindow *win)
578 {
579         g_return_if_fail (MODEST_IS_WINDOW(win));
580         
581         /* Check first if the header view has the focus */
582         if (MODEST_IS_MAIN_WINDOW (win)) {
583                 GtkWidget *w;
584                 w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
585                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
586                 if (gtk_widget_is_focus (w)) {
587                         modest_ui_actions_on_delete_folder (action, MODEST_MAIN_WINDOW(win));
588                         return;
589                 }
590         }
591         modest_ui_actions_on_delete_message (action, win);
592 }
593
594
595
596 void
597 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
598 {       
599         ModestWindowMgr *mgr = NULL;
600         
601 #ifdef MODEST_PLATFORM_MAEMO
602         modest_osso_save_state();
603 #endif /* MODEST_PLATFORM_MAEMO */
604
605         g_debug ("closing down, clearing %d item(s) from operation queue",
606                  modest_mail_operation_queue_num_elements
607                  (modest_runtime_get_mail_operation_queue()));
608
609         /* cancel all outstanding operations */
610         modest_mail_operation_queue_cancel_all 
611                 (modest_runtime_get_mail_operation_queue());
612         
613         g_debug ("queue has been cleared");
614
615
616         /* Check if there are opened editing windows */ 
617         mgr = modest_runtime_get_window_mgr ();
618         modest_window_mgr_close_all_windows (mgr);
619
620         /* note: when modest-tny-account-store is finalized,
621            it will automatically set all network connections
622            to offline */
623
624 /*      gtk_main_quit (); */
625 }
626
627 void
628 modest_ui_actions_on_close_window (GtkAction *action, ModestWindow *win)
629 {
630         gboolean ret_value;
631
632         g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
633
634 /*      if (MODEST_IS_MSG_VIEW_WINDOW (win)) { */
635 /*              gtk_widget_destroy (GTK_WIDGET (win)); */
636 /*      } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) { */
637 /*              gboolean ret_value; */
638 /*              g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value); */
639 /*      } else if (MODEST_IS_WINDOW (win)) { */
640 /*              gtk_widget_destroy (GTK_WIDGET (win)); */
641 /*      } else { */
642 /*              g_return_if_reached (); */
643 /*      } */
644 }
645
646 void
647 modest_ui_actions_on_add_to_contacts (GtkAction *action, ModestWindow *win)
648 {
649         GtkClipboard *clipboard = NULL;
650         gchar *selection = NULL;
651
652         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
653         selection = gtk_clipboard_wait_for_text (clipboard);
654
655         /* Question: why is the clipboard being used here? 
656          * It doesn't really make a lot of sense. */
657
658         if (selection)
659         {
660                 modest_address_book_add_address (selection);
661                 g_free (selection);
662         }
663 }
664
665 void
666 modest_ui_actions_on_accounts (GtkAction *action, 
667                                ModestWindow *win)
668 {
669         /* This is currently only implemented for Maemo */
670 #ifdef MODEST_PLATFORM_MAEMO /* Defined in config.h */
671         if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE)) {
672                 if (!modest_ui_actions_run_account_setup_wizard (win)) 
673                         g_debug ("%s: wizard was already running", __FUNCTION__);
674                 
675                 return;
676         } else {
677                 /* Show the list of accounts */
678                 GtkWindow *account_win = GTK_WINDOW (modest_account_view_window_new ());
679                 gtk_window_set_transient_for (account_win, GTK_WINDOW (win));
680                 
681                 /* The accounts dialog must be modal */
682                 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), account_win);
683                 modest_maemo_show_dialog_and_forget (GTK_WINDOW (win), GTK_DIALOG (account_win)); 
684         }
685 #else
686         GtkWidget *dialog, *label;
687         
688         /* Create the widgets */
689         
690         dialog = gtk_dialog_new_with_buttons ("Message",
691                                               GTK_WINDOW(win),
692                                               GTK_DIALOG_DESTROY_WITH_PARENT,
693                                               GTK_STOCK_OK,
694                                               GTK_RESPONSE_NONE,
695                                               NULL);
696         label = gtk_label_new ("Hello World!");
697         
698         /* Ensure that the dialog box is destroyed when the user responds. */
699         
700         g_signal_connect_swapped (dialog, "response", 
701                                   G_CALLBACK (gtk_widget_destroy),
702                                   dialog);
703         
704         /* Add the label, and show everything we've added to the dialog. */
705         
706         gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
707                            label);
708         gtk_widget_show_all (dialog);
709 #endif /* MODEST_PLATFORM_MAEMO */
710 }
711
712 static void
713 on_smtp_servers_window_hide (GtkWindow* window, gpointer user_data)
714 {
715         /* Save any changes. */
716         modest_connection_specific_smtp_window_save_server_accounts (
717                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (window));
718         gtk_widget_destroy (GTK_WIDGET (window));
719 }
720
721
722
723 void
724 modest_ui_actions_on_smtp_servers (GtkAction *action, ModestWindow *win)
725 {
726         /* This is currently only implemented for Maemo,
727          * because it requires an API (libconic) to detect different connection 
728          * possiblities.
729          */
730 #ifdef MODEST_PLATFORM_MAEMO /* Defined in config.h */
731         
732         /* Create the window if necessary: */
733         GtkWidget *specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
734         modest_connection_specific_smtp_window_fill_with_connections (
735                 MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), 
736                 modest_runtime_get_account_mgr());
737
738         /* Show the window: */  
739         gtk_window_set_transient_for (GTK_WINDOW (specific_window), GTK_WINDOW (win));
740         gtk_window_set_modal (GTK_WINDOW (specific_window), TRUE);
741         gtk_widget_show (specific_window);
742     
743         /* Save changes when the window is hidden: */
744         g_signal_connect (specific_window, "hide", 
745                 G_CALLBACK (on_smtp_servers_window_hide), win);
746 #endif /* MODEST_PLATFORM_MAEMO */
747 }
748
749 void
750 modest_ui_actions_compose_msg(ModestWindow *win,
751                               const gchar *to_str,
752                               const gchar *cc_str,
753                               const gchar *bcc_str,
754                               const gchar *subject_str,
755                               const gchar *body_str,
756                               GSList *attachments)
757 {
758         gchar *account_name = NULL;
759         TnyMsg *msg = NULL;
760         TnyAccount *account = NULL;
761         TnyFolder *folder = NULL;
762         gchar *from_str = NULL, *signature = NULL, *body = NULL;
763         gboolean use_signature = FALSE;
764         ModestWindow *msg_win = NULL;
765         ModestAccountMgr *mgr = modest_runtime_get_account_mgr();
766         ModestTnyAccountStore *store = modest_runtime_get_account_store();
767
768         if (win) account_name = g_strdup (modest_window_get_active_account (win));
769         if (!account_name) account_name = modest_account_mgr_get_default_account(mgr);
770         if (!account_name) {
771                 g_printerr ("modest: no account found\n");
772                 goto cleanup;
773         }
774         account = modest_tny_account_store_get_server_account (store, account_name, TNY_ACCOUNT_TYPE_STORE);
775         if (!account) {
776                 g_printerr ("modest: failed to get tnyaccount for '%s'\n", account_name);
777                 goto cleanup;
778         }
779         folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS);
780         if (!folder) {
781                 g_printerr ("modest: failed to find Drafts folder\n");
782                 goto cleanup;
783         }
784         from_str = modest_account_mgr_get_from_string (mgr, account_name);
785         if (!from_str) {
786                 g_printerr ("modest: failed get from string for '%s'\n", account_name);
787                 goto cleanup;
788         }
789
790         signature = modest_account_mgr_get_signature (mgr, account_name, &use_signature);
791         if (body_str != NULL) {
792                 body = use_signature ? g_strconcat(body_str, "\n", signature, NULL) : g_strdup(body_str);
793         } else {
794                 body = use_signature ? g_strconcat("\n", signature, NULL) : g_strdup("");
795         }
796
797         msg = modest_tny_msg_new (to_str, from_str, cc_str, bcc_str, subject_str, body, NULL);
798         if (!msg) {
799                 g_printerr ("modest: failed to create new msg\n");
800                 goto cleanup;
801         }
802
803         /* Create and register edit window */
804         /* This is destroyed by TODO. */
805         msg_win = modest_msg_edit_window_new (msg, account_name, FALSE);
806         while (attachments) {
807                 modest_msg_edit_window_attach_file_one((ModestMsgEditWindow *)msg_win,
808                                                        attachments->data);
809                 attachments = g_slist_next(attachments);
810         }
811         modest_window_mgr_register_window (modest_runtime_get_window_mgr(), msg_win);
812
813         if (win) {
814                 gtk_window_set_transient_for (GTK_WINDOW (msg_win),
815                                               GTK_WINDOW (win));
816         }
817         gtk_widget_show_all (GTK_WIDGET (msg_win));
818
819 cleanup:
820         g_free (from_str);
821         g_free (signature);
822         g_free (body);
823         g_free (account_name);
824         if (account) g_object_unref (G_OBJECT(account));
825         if (folder) g_object_unref (G_OBJECT(folder));
826         if (msg_win) g_object_unref (G_OBJECT(msg_win));
827         if (msg) g_object_unref (G_OBJECT(msg));
828 }
829
830 void
831 modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
832 {
833         /* if there are no accounts yet, just show the wizard */
834         if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE))
835                 if (!modest_ui_actions_run_account_setup_wizard (win))
836                         return;
837                 
838         modest_ui_actions_compose_msg(win, NULL, NULL, NULL, NULL, NULL, NULL);
839 }
840
841 gboolean 
842 modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
843                                        TnyHeader *header,
844                                        TnyMsg *msg)
845 {
846         ModestMailOperationStatus status;
847
848         /* If there is no message or the operation was not successful */
849         status = modest_mail_operation_get_status (mail_op);
850         if (!msg || status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
851
852                 /* Remove the header from the preregistered uids */
853                 modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),  
854                                                      header);
855
856                 return FALSE;
857         }
858
859         return TRUE;
860 }
861
862 static void
863 open_msg_cb (ModestMailOperation *mail_op, 
864              TnyHeader *header,  
865              gboolean canceled,
866              TnyMsg *msg, 
867              GError *err,
868              gpointer user_data)
869 {
870         ModestWindowMgr *mgr = NULL;
871         ModestWindow *parent_win = NULL;
872         ModestWindow *win = NULL;
873         TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN;
874         gchar *account = NULL;
875         TnyFolder *folder;
876         gboolean open_in_editor = FALSE;
877         
878         /* Do nothing if there was any problem with the mail
879            operation. The error will be shown by the error_handler of
880            the mail operation */
881         if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
882                 return;
883
884         parent_win = (ModestWindow *) modest_mail_operation_get_source (mail_op);
885         folder = tny_header_get_folder (header);
886
887         /* Mark header as read */
888         headers_action_mark_as_read (header, MODEST_WINDOW(parent_win), NULL);
889
890         /* Gets folder type (OUTBOX headers will be opened in edit window */
891         if (modest_tny_folder_is_local_folder (folder)) {
892                 folder_type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
893                 if (folder_type == TNY_FOLDER_TYPE_INVALID)
894                         g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
895         }
896
897                 
898         if (folder_type == TNY_FOLDER_TYPE_OUTBOX) {
899                 TnyTransportAccount *traccount = NULL;
900                 ModestTnyAccountStore *accstore = modest_runtime_get_account_store();
901                 traccount = modest_tny_account_store_get_transport_account_from_outbox_header(accstore, header);
902                 if (traccount) {
903                         ModestTnySendQueue *send_queue = NULL;
904                         ModestTnySendQueueStatus status;
905                         char *msg_id;
906                         account = g_strdup(modest_tny_account_get_parent_modest_account_name_for_server_account(
907                                                    TNY_ACCOUNT(traccount)));
908                         send_queue = modest_runtime_get_send_queue(traccount);
909                         msg_id = modest_tny_send_queue_get_msg_id (header);
910                         status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
911                         /* Only open messages in outbox with the editor if they are in Failed state */
912                         if (status == MODEST_TNY_SEND_QUEUE_FAILED) {
913                                 open_in_editor = TRUE;
914                         }
915                         g_free(msg_id);
916                         g_object_unref(traccount);
917                 } else {
918                         g_warning("Cannot get transport account for message in outbox!!");
919                 }
920         } else if (folder_type == TNY_FOLDER_TYPE_DRAFTS) {
921                 open_in_editor = TRUE; /* Open in editor if the message is in the Drafts folder */
922         }
923
924         /* Get account */
925         if (!account)
926                 account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (parent_win)));
927         if (!account)
928                 account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
929         
930         if (open_in_editor) {
931                 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
932                 const gchar *from_header = NULL;
933
934                 from_header = tny_header_get_from (header);
935
936                 /* we cannot edit without a valid account... */
937                 if (!modest_account_mgr_has_accounts(mgr, TRUE)) {
938                         if (!modest_ui_actions_run_account_setup_wizard(parent_win))
939                                 goto cleanup;
940                 }
941                 
942                 if (from_header) {
943                         GSList *accounts = modest_account_mgr_account_names (mgr, TRUE);
944                         GSList *node = NULL;
945                         for (node = accounts; node != NULL; node = g_slist_next (node)) {
946                                 gchar *from = modest_account_mgr_get_from_string (mgr, node->data);
947                                 
948                                 if (from && (strcmp (from_header, from) == 0)) {
949                                         g_free (account);
950                                         account = g_strdup (node->data);
951                                         g_free (from);
952                                         break;
953                                 }
954                                 g_free (from);
955                         }
956                         g_slist_foreach (accounts, (GFunc) g_free, NULL);
957                         g_slist_free (accounts);
958                 }
959
960                 win = modest_msg_edit_window_new (msg, account, TRUE);
961
962
963                 /* Show banner */
964                 modest_platform_information_banner (NULL, NULL, _("mail_ib_opening_draft_message"));
965
966         } else {
967                 gchar *uid = modest_tny_folder_get_header_unique_id (header);
968                 
969                 if (MODEST_IS_MAIN_WINDOW (parent_win)) {
970                         GtkWidget *header_view;
971                         GtkTreeSelection *sel;
972                         GList *sel_list = NULL;
973                         GtkTreeModel *model;
974                         
975                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(parent_win),
976                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
977
978                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
979                         sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
980
981                         if (sel_list != NULL) {
982                                 GtkTreeRowReference *row_reference;
983
984                                 row_reference = gtk_tree_row_reference_new (model, (GtkTreePath *) sel_list->data);
985                                 g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
986                                 g_list_free (sel_list);
987                                 
988                                 win = modest_msg_view_window_new_with_header_model (
989                                                 msg, account, (const gchar*) uid,
990                                                 model, row_reference);
991                                 gtk_tree_row_reference_free (row_reference);
992                         } else {
993                                 win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
994                         }
995                 } else {
996                         win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
997                 }
998                 g_free (uid);
999         }
1000         
1001         /* Register and show new window */
1002         if (win != NULL) {
1003                 mgr = modest_runtime_get_window_mgr ();
1004                 modest_window_mgr_register_window (mgr, win);
1005                 g_object_unref (win);
1006                 gtk_widget_show_all (GTK_WIDGET(win));
1007         }
1008
1009         /* Update toolbar dimming state */
1010         if (MODEST_IS_MAIN_WINDOW (parent_win)) {
1011                 modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (parent_win));
1012         }
1013
1014 cleanup:
1015         /* Free */
1016         g_free(account);
1017         g_object_unref (parent_win);
1018         g_object_unref (folder);
1019 }
1020
1021 void
1022 modest_ui_actions_get_msgs_full_error_handler (ModestMailOperation *mail_op,
1023                                                gpointer user_data)
1024 {
1025         const GError *error;
1026         GObject *win = NULL;
1027         const gchar *err_msg;
1028
1029         win = modest_mail_operation_get_source (mail_op);
1030         error = modest_mail_operation_get_error (mail_op);
1031  
1032         /* Select error message */
1033         if (error->code == MODEST_MAIL_OPERATION_ERROR_MESSAGE_SIZE_LIMIT)
1034                 err_msg = _("emev_ni_ui_imap_msg_size_exceed_error");
1035         else
1036                 err_msg = (const gchar *) user_data;
1037
1038         /* Show error */
1039         modest_platform_run_information_dialog ((GtkWindow *) win, err_msg);
1040
1041         if (win)
1042                 g_object_unref (win);
1043 }
1044
1045 /**
1046  * Returns the account a list of headers belongs to. It returns a
1047  * *new* reference so don't forget to unref it
1048  */
1049 static TnyAccount*
1050 get_account_from_header_list (TnyList *headers)
1051 {
1052         TnyAccount *account = NULL;
1053
1054         if (tny_list_get_length (headers) > 0) {
1055                 TnyIterator *iter = tny_list_create_iterator (headers);
1056                 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
1057                 TnyFolder *folder = tny_header_get_folder (header);
1058                 account = tny_folder_get_account (folder);
1059                 g_object_unref (folder);
1060                 g_object_unref (header);
1061                 g_object_unref (iter);
1062         }
1063         return account;
1064 }
1065
1066 static void
1067 open_msgs_performer(gboolean canceled, 
1068                     GError *err,
1069                     GtkWindow *parent_window, 
1070                     TnyAccount *account, 
1071                     gpointer user_data)
1072 {
1073         ModestMailOperation *mail_op;
1074         const gchar *proto_name;
1075         gchar *error_msg;
1076         ModestTransportStoreProtocol proto;
1077         TnyList *not_opened_headers;
1078
1079         not_opened_headers = TNY_LIST (user_data);
1080
1081         /* Get the error message depending on the protocol */
1082         proto_name = tny_account_get_proto (account);
1083         if (proto_name != NULL) {
1084                 proto = modest_protocol_info_get_transport_store_protocol (proto_name);
1085         } else {
1086                 proto = MODEST_PROTOCOL_STORE_MAILDIR;
1087         }
1088         
1089         /* Create the error messages */
1090         if (tny_list_get_length (not_opened_headers) == 1) {
1091                 if (proto == MODEST_PROTOCOL_STORE_POP) {
1092                         error_msg = g_strdup (_("emev_ni_ui_pop3_msg_recv_error"));
1093                 } else if (proto == MODEST_PROTOCOL_STORE_IMAP) {
1094                         TnyIterator *iter = tny_list_create_iterator (not_opened_headers);
1095                         TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
1096                         error_msg = g_strdup_printf (_("emev_ni_ui_imap_message_not_available_in_server"),
1097                                                      tny_header_get_subject (header));
1098                         g_object_unref (header);
1099                         g_object_unref (iter);
1100                 } else {
1101                         error_msg = g_strdup (_("mail_ni_ui_folder_get_msg_folder_error"));
1102                 }
1103         } else {
1104                 error_msg = g_strdup (_("mail_ni_ui_folder_get_msg_folder_error"));
1105         }
1106
1107         /* Create the mail operation */
1108         mail_op = 
1109                 modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
1110                                                                modest_ui_actions_get_msgs_full_error_handler,
1111                                                                error_msg, g_free);
1112         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1113                                          mail_op);
1114                 
1115         modest_mail_operation_get_msgs_full (mail_op,
1116                                              not_opened_headers,
1117                                              open_msg_cb,
1118                                              NULL,
1119                                              NULL);
1120
1121         /* Frees */
1122         g_object_unref (mail_op);
1123         g_object_unref (not_opened_headers);
1124         g_object_unref (account);
1125 }
1126
1127 /*
1128  * This function is used by both modest_ui_actions_on_open and
1129  * modest_ui_actions_on_header_activated. This way we always do the
1130  * same when trying to open messages.
1131  */
1132 static void
1133 open_msgs_from_headers (TnyList *headers, ModestWindow *win)
1134 {
1135         ModestWindowMgr *mgr = NULL;
1136         TnyIterator *iter = NULL, *iter_not_opened = NULL;
1137         TnyList *not_opened_headers = NULL;
1138         TnyHeaderFlags flags = 0;
1139         TnyAccount *account;
1140         gint uncached_msgs = 0;
1141                 
1142         g_return_if_fail (headers != NULL);
1143
1144         /* Check that only one message is selected for opening */
1145         if (tny_list_get_length (headers) != 1) {
1146                 modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
1147                                                         _("mcen_ib_select_one_message"));
1148                 return;
1149         }
1150
1151         mgr = modest_runtime_get_window_mgr ();
1152         iter = tny_list_create_iterator (headers);
1153
1154         /* Get the account */
1155         account = get_account_from_header_list (headers);
1156         
1157         /* Look if we already have a message view for each header. If
1158            true, then remove the header from the list of headers to
1159            open */
1160         not_opened_headers = tny_simple_list_new ();
1161         while (!tny_iterator_is_done (iter)) {
1162
1163                 ModestWindow *window = NULL;
1164                 TnyHeader *header = NULL;
1165                 gboolean found = FALSE;
1166                 
1167                 header = TNY_HEADER (tny_iterator_get_current (iter));
1168                 if (header)
1169                         flags = tny_header_get_flags (header);
1170
1171                 window = NULL;
1172                 found = modest_window_mgr_find_registered_header (mgr, header, &window);
1173                 
1174                 /* Do not open again the message and present the
1175                    window to the user */
1176                 if (found) {
1177                         if (window)
1178                                 gtk_window_present (GTK_WINDOW (window));
1179                         else
1180                                 /* the header has been registered already, we don't do
1181                                  * anything but wait for the window to come up*/
1182                                 g_debug ("header %p already registered, waiting for window", header);
1183                 } else {
1184                         tny_list_append (not_opened_headers, G_OBJECT (header));
1185                 }
1186
1187                 if (header)
1188                         g_object_unref (header);
1189
1190                 tny_iterator_next (iter);
1191         }
1192         g_object_unref (iter);
1193         iter = NULL;
1194
1195         /* Open each message */
1196         if (tny_list_get_length (not_opened_headers) == 0)
1197                 goto cleanup;
1198         
1199         /* If some messages would have to be downloaded, ask the user to 
1200          * make a connection. It's generally easier to do this here (in the mainloop) 
1201          * than later in a thread:
1202          */
1203         if (tny_list_get_length (not_opened_headers) > 0) {
1204                 uncached_msgs = header_list_count_uncached_msgs (not_opened_headers);
1205
1206                 if (uncached_msgs > 0) {
1207                         /* Allways download if we are online. */
1208                         if (!tny_device_is_online (modest_runtime_get_device ())) {
1209                                 gint response;
1210
1211                                 /* If ask for user permission to download the messages */
1212                                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
1213                                                                                     ngettext("mcen_nc_get_msg",
1214                                                                                              "mcen_nc_get_msgs",
1215                                                                                              uncached_msgs));
1216
1217                                 /* End if the user does not want to continue */
1218                                 if (response == GTK_RESPONSE_CANCEL)
1219                                         goto cleanup;
1220                         }
1221                 }
1222         }
1223         
1224         /* Register the headers before actually creating the windows: */
1225         iter_not_opened = tny_list_create_iterator (not_opened_headers);
1226         while (!tny_iterator_is_done (iter_not_opened)) {
1227                 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter_not_opened));
1228                 if (header) {
1229                         modest_window_mgr_register_header (mgr, header, NULL);
1230                         g_object_unref (header);
1231                 }
1232                 tny_iterator_next (iter_not_opened);
1233         }
1234         g_object_unref (iter_not_opened);
1235         iter_not_opened = NULL;
1236
1237         /* Connect to the account and perform */
1238         if (uncached_msgs > 0) {
1239                 modest_platform_connect_and_perform ((GtkWindow *) win, g_object_ref (account), 
1240                                                      open_msgs_performer, g_object_ref (not_opened_headers));
1241         } else {
1242                 /* Call directly the performer, do not need to connect */
1243                 open_msgs_performer (FALSE, NULL, (GtkWindow *) win, g_object_ref (account),
1244                                      g_object_ref (not_opened_headers));
1245         }
1246 cleanup:
1247         /* Clean */
1248         if (account)
1249                 g_object_unref (account);
1250         if (not_opened_headers)
1251                 g_object_unref (not_opened_headers);
1252 }
1253
1254 void
1255 modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
1256 {
1257         TnyList *headers;
1258
1259         /* Get headers */
1260         headers = get_selected_headers (win);
1261         if (!headers)
1262                 return;
1263
1264         /* Open them */
1265         open_msgs_from_headers (headers, win);
1266
1267         g_object_unref(headers);
1268 }
1269
1270
1271 static void
1272 free_reply_forward_helper (gpointer data)
1273 {
1274         ReplyForwardHelper *helper;
1275
1276         helper = (ReplyForwardHelper *) data;
1277         g_free (helper->account_name);
1278         g_slice_free (ReplyForwardHelper, helper);
1279 }
1280
1281 static void
1282 reply_forward_cb (ModestMailOperation *mail_op,  
1283                   TnyHeader *header, 
1284                   gboolean canceled,
1285                   TnyMsg *msg,
1286                   GError *err,
1287                   gpointer user_data)
1288 {
1289         TnyMsg *new_msg;
1290         ReplyForwardHelper *rf_helper;
1291         ModestWindow *msg_win = NULL;
1292         ModestEditType edit_type;
1293         gchar *from = NULL;
1294         TnyAccount *account = NULL;
1295         ModestWindowMgr *mgr = NULL;
1296         gchar *signature = NULL;
1297         gboolean use_signature;
1298
1299         /* If there was any error. The mail operation could be NULL,
1300            this means that we already have the message downloaded and
1301            that we didn't do a mail operation to retrieve it */
1302         if (mail_op && !modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
1303                 return;
1304                         
1305         g_return_if_fail (user_data != NULL);
1306         rf_helper = (ReplyForwardHelper *) user_data;
1307
1308         from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
1309                                                    rf_helper->account_name);
1310         signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr(), 
1311                                                       rf_helper->account_name, 
1312                                                       &use_signature);
1313
1314         /* Create reply mail */
1315         switch (rf_helper->action) {
1316         case ACTION_REPLY:
1317                 new_msg = 
1318                         modest_tny_msg_create_reply_msg (msg, header, from, signature,
1319                                                          rf_helper->reply_forward_type,
1320                                                          MODEST_TNY_MSG_REPLY_MODE_SENDER);
1321                 break;
1322         case ACTION_REPLY_TO_ALL:
1323                 new_msg = 
1324                         modest_tny_msg_create_reply_msg (msg, header, from, signature, rf_helper->reply_forward_type,
1325                                                          MODEST_TNY_MSG_REPLY_MODE_ALL);
1326                 edit_type = MODEST_EDIT_TYPE_REPLY;
1327                 break;
1328         case ACTION_FORWARD:
1329                 new_msg = 
1330                         modest_tny_msg_create_forward_msg (msg, from, signature, rf_helper->reply_forward_type);
1331                 edit_type = MODEST_EDIT_TYPE_FORWARD;
1332                 break;
1333         default:
1334                 g_return_if_reached ();
1335                 return;
1336         }
1337
1338         g_free (signature);
1339
1340         if (!new_msg) {
1341                 g_printerr ("modest: failed to create message\n");
1342                 goto cleanup;
1343         }
1344
1345         account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(),
1346                                                                        rf_helper->account_name,
1347                                                                        TNY_ACCOUNT_TYPE_STORE);
1348         if (!account) {
1349                 g_printerr ("modest: failed to get tnyaccount for '%s'\n", rf_helper->account_name);
1350                 goto cleanup;
1351         }
1352
1353         /* Create and register the windows */
1354         msg_win = modest_msg_edit_window_new (new_msg, rf_helper->account_name, FALSE);
1355         mgr = modest_runtime_get_window_mgr ();
1356         modest_window_mgr_register_window (mgr, msg_win);
1357
1358         if (rf_helper->parent_window != NULL) {
1359                 gdouble parent_zoom;
1360
1361                 parent_zoom = modest_window_get_zoom (MODEST_WINDOW (rf_helper->parent_window));
1362                 modest_window_set_zoom (msg_win, parent_zoom);
1363         }
1364
1365         /* Show edit window */
1366         gtk_widget_show_all (GTK_WIDGET (msg_win));
1367
1368 cleanup:
1369         if (msg_win)
1370                 g_object_unref (msg_win);
1371         if (new_msg)
1372                 g_object_unref (G_OBJECT (new_msg));
1373         if (account)
1374                 g_object_unref (G_OBJECT (account));
1375 /*      g_object_unref (msg); */
1376         free_reply_forward_helper (rf_helper);
1377 }
1378
1379 /* Checks a list of headers. If any of them are not currently
1380  * downloaded (CACHED) then returns TRUE else returns FALSE.
1381  */
1382 static gint
1383 header_list_count_uncached_msgs (TnyList *header_list)
1384 {
1385         TnyIterator *iter;
1386         gint uncached_messages = 0;
1387
1388         iter = tny_list_create_iterator (header_list);
1389         while (!tny_iterator_is_done (iter)) {
1390                 TnyHeader *header;
1391
1392                 header = TNY_HEADER (tny_iterator_get_current (iter));
1393                 if (header) {
1394                         if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED))
1395                                 uncached_messages ++;
1396                         g_object_unref (header);
1397                 }
1398
1399                 tny_iterator_next (iter);
1400         }
1401         g_object_unref (iter);
1402
1403         return uncached_messages;
1404 }
1405
1406 /* Returns FALSE if the user does not want to download the
1407  * messages. Returns TRUE if the user allowed the download.
1408  */
1409 static gboolean
1410 connect_to_get_msg (ModestWindow *win,
1411                     gint num_of_uncached_msgs,
1412                     TnyAccount *account)
1413 {
1414         GtkResponseType response;
1415
1416         /* Allways download if we are online. */
1417         if (tny_device_is_online (modest_runtime_get_device ()))
1418                 return TRUE;
1419
1420         /* If offline, then ask for user permission to download the messages */
1421         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
1422                         ngettext("mcen_nc_get_msg",
1423                         "mcen_nc_get_msgs",
1424                         num_of_uncached_msgs));
1425
1426         if (response == GTK_RESPONSE_CANCEL)
1427                 return FALSE;
1428
1429         return modest_platform_connect_and_wait((GtkWindow *) win, account);
1430 }
1431
1432 /*
1433  * Common code for the reply and forward actions
1434  */
1435 static void
1436 reply_forward (ReplyForwardAction action, ModestWindow *win)
1437 {
1438         ModestMailOperation *mail_op = NULL;
1439         TnyList *header_list = NULL;
1440         ReplyForwardHelper *rf_helper = NULL;
1441         guint reply_forward_type;
1442         gboolean continue_download = TRUE;
1443         gboolean do_retrieve = TRUE;
1444         
1445         g_return_if_fail (MODEST_IS_WINDOW(win));
1446
1447         /* we need an account when editing */
1448         if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
1449                 if (!modest_ui_actions_run_account_setup_wizard (win))
1450                         return;
1451         }
1452         
1453         header_list = get_selected_headers (win);
1454         if (!header_list)
1455                 return;
1456
1457         reply_forward_type = 
1458                 modest_conf_get_int (modest_runtime_get_conf (),
1459                                      (action == ACTION_FORWARD) ? MODEST_CONF_FORWARD_TYPE : MODEST_CONF_REPLY_TYPE,
1460                                      NULL);
1461
1462         /* check if we need to download msg before asking about it */
1463         do_retrieve = (action == ACTION_FORWARD) ||
1464                         (reply_forward_type != MODEST_TNY_MSG_REPLY_TYPE_CITE);
1465
1466         if (do_retrieve){
1467                 gint num_of_unc_msgs;
1468
1469                 /* check that the messages have been previously downloaded */
1470                 num_of_unc_msgs = header_list_count_uncached_msgs(header_list);
1471                 /* If there are any uncached message ask the user
1472                  * whether he/she wants to download them. */
1473                 if (num_of_unc_msgs) {
1474                         TnyAccount *account = get_account_from_header_list (header_list);
1475                         continue_download = connect_to_get_msg (win, num_of_unc_msgs, account);
1476                         g_object_unref (account);
1477                 }
1478         }
1479
1480         if (!continue_download) {
1481                 g_object_unref (header_list);
1482                 return;
1483         }
1484         
1485         /* We assume that we can only select messages of the
1486            same folder and that we reply all of them from the
1487            same account. In fact the interface currently only
1488            allows single selection */
1489         
1490         /* Fill helpers */
1491         rf_helper = g_slice_new0 (ReplyForwardHelper);
1492         rf_helper->reply_forward_type = reply_forward_type;
1493         rf_helper->action = action;
1494         rf_helper->account_name = g_strdup (modest_window_get_active_account (win));
1495         
1496         if ((win != NULL) && (MODEST_IS_WINDOW (win)))
1497                 rf_helper->parent_window = GTK_WIDGET (win);
1498         if (!rf_helper->account_name)
1499                 rf_helper->account_name =
1500                         modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
1501
1502         if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
1503                 TnyMsg *msg;
1504                 TnyHeader *header;
1505                 /* Get header and message. Do not free them here, the
1506                    reply_forward_cb must do it */
1507                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW(win));
1508                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));
1509                 if (!msg || !header) {
1510                         if (msg)
1511                                 g_object_unref (msg);
1512                         g_printerr ("modest: no message found\n");
1513                         return;
1514                 } else {
1515                         reply_forward_cb (NULL, header, FALSE, msg, NULL, rf_helper);
1516                 }
1517                 if (header)
1518                         g_object_unref (header);
1519         } else {
1520                 TnyHeader *header;
1521                 TnyIterator *iter;
1522
1523                 /* Only reply/forward to one message */
1524                 iter = tny_list_create_iterator (header_list);
1525                 header = TNY_HEADER (tny_iterator_get_current (iter));
1526                 g_object_unref (iter);
1527
1528                 if (header) {
1529                         /* Retrieve messages */
1530                         if (do_retrieve) {
1531                                 mail_op = 
1532                                         modest_mail_operation_new_with_error_handling (G_OBJECT(win),
1533                                                                                        modest_ui_actions_get_msgs_full_error_handler, 
1534                                                                                        NULL, NULL);
1535                                 modest_mail_operation_queue_add (
1536                                         modest_runtime_get_mail_operation_queue (), mail_op);
1537                                 
1538                                 modest_mail_operation_get_msg (mail_op,
1539                                                                header,
1540                                                                reply_forward_cb,
1541                                                                rf_helper);
1542                                 /* Clean */
1543                                 g_object_unref(mail_op);
1544                         } else {
1545                                 /* we put a ref here to prevent double unref as the reply
1546                                  * forward callback unrefs the header at its end */
1547                                 reply_forward_cb (NULL, header, FALSE, NULL, NULL, rf_helper);
1548                         }
1549
1550
1551                         g_object_unref (header);
1552                 }
1553
1554         }
1555
1556         /* Free */
1557         g_object_unref (header_list);
1558 }
1559
1560 void
1561 modest_ui_actions_on_reply (GtkAction *action, ModestWindow *win)
1562 {
1563         g_return_if_fail (MODEST_IS_WINDOW(win));
1564
1565         reply_forward (ACTION_REPLY, win);
1566 }
1567
1568 void
1569 modest_ui_actions_on_forward (GtkAction *action, ModestWindow *win)
1570 {
1571         g_return_if_fail (MODEST_IS_WINDOW(win));
1572
1573         reply_forward (ACTION_FORWARD, win);
1574 }
1575
1576 void
1577 modest_ui_actions_on_reply_all (GtkAction *action, ModestWindow *win)
1578 {
1579         g_return_if_fail (MODEST_IS_WINDOW(win));
1580
1581         reply_forward (ACTION_REPLY_TO_ALL, win);
1582 }
1583
1584 void 
1585 modest_ui_actions_on_next (GtkAction *action, 
1586                            ModestWindow *window)
1587 {
1588         if (MODEST_IS_MAIN_WINDOW (window)) {
1589                 GtkWidget *header_view;
1590
1591                 header_view = modest_main_window_get_child_widget (
1592                                 MODEST_MAIN_WINDOW(window),
1593                                 MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1594                 if (!header_view)
1595                         return;
1596         
1597                 modest_header_view_select_next (
1598                                 MODEST_HEADER_VIEW(header_view)); 
1599         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1600                 modest_msg_view_window_select_next_message (
1601                                 MODEST_MSG_VIEW_WINDOW (window));
1602         } else {
1603                 g_return_if_reached ();
1604         }
1605 }
1606
1607 void 
1608 modest_ui_actions_on_prev (GtkAction *action, 
1609                            ModestWindow *window)
1610 {
1611         g_return_if_fail (MODEST_IS_WINDOW(window));
1612
1613         if (MODEST_IS_MAIN_WINDOW (window)) {
1614                 GtkWidget *header_view;
1615                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1616                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1617                 if (!header_view)
1618                         return;
1619                 
1620                 modest_header_view_select_prev (MODEST_HEADER_VIEW(header_view)); 
1621         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1622                 modest_msg_view_window_select_previous_message (MODEST_MSG_VIEW_WINDOW (window));
1623         } else {
1624                 g_return_if_reached ();
1625         }
1626 }
1627
1628 void 
1629 modest_ui_actions_on_sort (GtkAction *action, 
1630                            ModestWindow *window)
1631 {
1632         g_return_if_fail (MODEST_IS_WINDOW(window));
1633
1634         if (MODEST_IS_MAIN_WINDOW (window)) {
1635                 GtkWidget *header_view;
1636                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1637                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1638                 if (!header_view) {
1639                         modest_platform_information_banner (NULL, NULL, _CS("ckdg_ib_nothing_to_sort"));
1640
1641                         return;
1642                 }
1643
1644                 /* Show sorting dialog */
1645                 modest_platform_run_sort_dialog (GTK_WINDOW (window), MODEST_SORT_HEADERS);     
1646         }
1647 }
1648
1649 static void
1650 new_messages_arrived (ModestMailOperation *self, 
1651                       TnyList *new_headers,
1652                       gpointer user_data)
1653 {
1654         GObject *source;
1655
1656         source = modest_mail_operation_get_source (self);
1657
1658         /* Notify new messages have been downloaded. Do not notify if
1659            the send&receive was invoked by the user, i.e, if the mail
1660            operation has a source (the main window) */
1661         if ((new_headers != NULL) && (tny_list_get_length (new_headers) > 0) && !source)
1662                 modest_platform_on_new_headers_received (new_headers);
1663
1664         if (source)
1665                 g_object_unref (source);
1666 }
1667
1668 typedef struct {
1669         TnyAccount *account;
1670         ModestWindow *win;
1671         gchar *account_name;
1672 } SendReceiveInfo;
1673
1674 static void
1675 do_send_receive_performer (gboolean canceled, 
1676                            GError *err,
1677                            GtkWindow *parent_window, 
1678                            TnyAccount *account, 
1679                            gpointer user_data)
1680 {
1681         ModestMailOperation *mail_op;
1682         SendReceiveInfo *info;
1683
1684         info = (SendReceiveInfo *) user_data;
1685
1686         if (err || canceled) {
1687
1688                 goto clean;
1689         }
1690
1691         /* Set send/receive operation in progress */    
1692         if (info->win && MODEST_IS_MAIN_WINDOW (info->win)) {
1693                 modest_main_window_notify_send_receive_initied (MODEST_MAIN_WINDOW (info->win));
1694         }
1695         
1696         mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (info->win),
1697                                                                  modest_ui_actions_send_receive_error_handler,
1698                                                                  NULL, NULL);
1699
1700         if (info->win && MODEST_IS_MAIN_WINDOW (info->win))
1701                 g_signal_connect (G_OBJECT(mail_op), "operation-finished", 
1702                                   G_CALLBACK (on_send_receive_finished), 
1703                                   info->win);
1704
1705         /* Send & receive. */
1706         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
1707         modest_mail_operation_update_account (mail_op, info->account_name, new_messages_arrived, info->win);
1708         g_object_unref (G_OBJECT (mail_op));
1709         
1710  clean:
1711         /* Frees */
1712         if (info->account_name)
1713                 g_free (info->account_name);
1714         if (info->win)
1715                 g_object_unref (info->win);
1716         if (info->account)
1717                 g_object_unref (info->account);
1718         g_slice_free (SendReceiveInfo, info);
1719 }
1720
1721 /*
1722  * This function performs the send & receive required actions. The
1723  * window is used to create the mail operation. Typically it should
1724  * always be the main window, but we pass it as argument in order to
1725  * be more flexible.
1726  */
1727 void
1728 modest_ui_actions_do_send_receive (const gchar *account_name, 
1729                                    ModestWindow *win)
1730 {
1731         gchar *acc_name = NULL;
1732         SendReceiveInfo *info;
1733         ModestTnyAccountStore *acc_store;
1734
1735         /* If no account name was provided then get the current account, and if
1736            there is no current account then pick the default one: */
1737         if (!account_name) {
1738                 if (win)
1739                         acc_name = g_strdup (modest_window_get_active_account (win));
1740                 if (!acc_name)
1741                         acc_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
1742                 if (!acc_name) {
1743                         g_printerr ("modest: cannot get default account\n");
1744                         return;
1745                 }
1746         } else {
1747                 acc_name = g_strdup (account_name);
1748         }
1749
1750         acc_store = modest_runtime_get_account_store ();
1751
1752         /* Create the info for the connect and perform */
1753         info = g_slice_new (SendReceiveInfo);
1754         info->account_name = acc_name;
1755         info->win = (win) ? g_object_ref (win) : NULL;
1756         info->account = modest_tny_account_store_get_server_account (acc_store, acc_name,
1757                                                                      TNY_ACCOUNT_TYPE_STORE);
1758
1759         /* Invoke the connect and perform */
1760         modest_platform_connect_and_perform ((win) ? GTK_WINDOW (win) : NULL, info->account, 
1761                                              do_send_receive_performer, info);
1762 }
1763
1764
1765 static void
1766 modest_ui_actions_do_cancel_send (const gchar *account_name,  
1767                                   ModestWindow *win)
1768 {
1769         TnyTransportAccount *transport_account;
1770         TnySendQueue *send_queue = NULL;
1771         GError *error = NULL;
1772
1773         /* Get transport account */
1774         transport_account =
1775                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
1776                                       (modest_runtime_get_account_store(),
1777                                        account_name,
1778                                        TNY_ACCOUNT_TYPE_TRANSPORT));
1779         if (!transport_account) {
1780                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
1781                 goto frees;
1782         }
1783
1784         /* Get send queue*/
1785         send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
1786         if (!TNY_IS_SEND_QUEUE(send_queue)) {
1787                 g_set_error (&error, MODEST_MAIL_OPERATION_ERROR,
1788                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1789                              "modest: could not find send queue for account\n");
1790         } else {
1791                 /* Keeep messages in outbox folder */
1792                 tny_send_queue_cancel (send_queue, FALSE, &error);
1793         }       
1794
1795  frees:
1796         if (transport_account != NULL) 
1797                 g_object_unref (G_OBJECT (transport_account));
1798 }
1799
1800 static void
1801 modest_ui_actions_cancel_send_all (ModestWindow *win) 
1802 {
1803         GSList *account_names, *iter;
1804
1805         account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
1806                                                           TRUE);
1807
1808         iter = account_names;
1809         while (iter) {                  
1810                 modest_ui_actions_do_cancel_send ((const char*) iter->data, win);
1811                 iter = g_slist_next (iter);
1812         }
1813
1814         modest_account_mgr_free_account_names (account_names);
1815         account_names = NULL;
1816 }
1817
1818 void
1819 modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
1820
1821 {
1822         /* Check if accounts exist */
1823         gboolean accounts_exist = 
1824                 modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
1825         
1826         /* If not, allow the user to create an account before trying to send/receive. */
1827         if (!accounts_exist)
1828                 modest_ui_actions_on_accounts (NULL, win);
1829         
1830         /* Cancel all sending operaitons */     
1831         modest_ui_actions_cancel_send_all (win);
1832 }
1833
1834 /*
1835  * Refreshes all accounts. This function will be used by automatic
1836  * updates
1837  */
1838 void
1839 modest_ui_actions_do_send_receive_all (ModestWindow *win)
1840 {
1841         GSList *account_names, *iter;
1842
1843         account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
1844                                                           TRUE);
1845
1846         iter = account_names;
1847         while (iter) {                  
1848                 modest_ui_actions_do_send_receive ((const char*) iter->data, win);
1849                 iter = g_slist_next (iter);
1850         }
1851
1852         modest_account_mgr_free_account_names (account_names);
1853         account_names = NULL;
1854 }
1855
1856 /*
1857  * Handler of the click on Send&Receive button in the main toolbar
1858  */
1859 void
1860 modest_ui_actions_on_send_receive (GtkAction *action, ModestWindow *win)
1861 {
1862         /* Check if accounts exist */
1863         gboolean accounts_exist = 
1864                 modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
1865         
1866         /* If not, allow the user to create an account before trying to send/receive. */
1867         if (!accounts_exist)
1868                 modest_ui_actions_on_accounts (NULL, win);
1869         
1870         /* Refresh the active account */
1871         modest_ui_actions_do_send_receive (NULL, win);
1872 }
1873
1874
1875 void
1876 modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *main_window)
1877 {
1878         ModestConf *conf;
1879         GtkWidget *header_view;
1880         
1881         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1882
1883         header_view = modest_main_window_get_child_widget (main_window,
1884                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
1885         if (!header_view)
1886                 return;
1887
1888         conf = modest_runtime_get_conf ();
1889         
1890         /* what is saved/restored is depending on the style; thus; we save with
1891          * old style, then update the style, and restore for this new style
1892          */
1893         modest_widget_memory_save (conf, G_OBJECT(header_view), MODEST_CONF_HEADER_VIEW_KEY);
1894         
1895         if (modest_header_view_get_style
1896             (MODEST_HEADER_VIEW(header_view)) == MODEST_HEADER_VIEW_STYLE_DETAILS)
1897                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
1898                                               MODEST_HEADER_VIEW_STYLE_TWOLINES);
1899         else
1900                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
1901                                               MODEST_HEADER_VIEW_STYLE_DETAILS);
1902
1903         modest_widget_memory_restore (conf, G_OBJECT(header_view),
1904                                       MODEST_CONF_HEADER_VIEW_KEY);
1905 }
1906
1907
1908 void 
1909 modest_ui_actions_on_header_selected (ModestHeaderView *header_view, 
1910                                       TnyHeader *header,
1911                                       ModestMainWindow *main_window)
1912 {
1913         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1914         g_return_if_fail (MODEST_IS_HEADER_VIEW (header_view));
1915         
1916         /* in the case the folder is empty, show the empty folder message and focus
1917          * folder view */
1918         if (!header && gtk_widget_is_focus (GTK_WIDGET (header_view))) {
1919                 if (modest_header_view_is_empty (header_view)) {
1920                         TnyFolder *folder = modest_header_view_get_folder (header_view);
1921                         GtkWidget *folder_view = 
1922                                 modest_main_window_get_child_widget (main_window,
1923                                                                      MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1924                         if (folder != NULL) 
1925                                 modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view), folder, FALSE);
1926                         gtk_widget_grab_focus (GTK_WIDGET (folder_view));
1927                         return;
1928                 }
1929         }
1930         /* If no header has been selected then exit */
1931         if (!header)
1932                 return;
1933
1934         /* Update focus */
1935         if (!gtk_widget_is_focus (GTK_WIDGET(header_view)))
1936             gtk_widget_grab_focus (GTK_WIDGET(header_view));
1937
1938         /* Update toolbar dimming state */
1939         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
1940 }
1941
1942 void
1943 modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
1944                                        TnyHeader *header,
1945                                        ModestMainWindow *main_window)
1946 {
1947         TnyList *headers;
1948
1949         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1950         
1951         if (!header)
1952                 return;
1953
1954         if (modest_header_view_count_selected_headers (header_view) > 1) {
1955                 hildon_banner_show_information (NULL, NULL, _("mcen_ib_select_one_message"));
1956                 return;
1957         }
1958
1959
1960 /*      headers = tny_simple_list_new (); */
1961 /*      tny_list_prepend (headers, G_OBJECT (header)); */
1962         headers = modest_header_view_get_selected_headers (header_view);
1963
1964         open_msgs_from_headers (headers, MODEST_WINDOW (main_window));
1965
1966         g_object_unref (headers);
1967 }
1968
1969 static void
1970 set_active_account_from_tny_account (TnyAccount *account,
1971                                      ModestWindow *window)
1972 {
1973         const gchar *server_acc_name = tny_account_get_id (account);
1974         
1975         /* We need the TnyAccount provided by the
1976            account store because that is the one that
1977            knows the name of the Modest account */
1978         TnyAccount *modest_server_account = modest_server_account = 
1979                 modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store (),
1980                                                              MODEST_TNY_ACCOUNT_STORE_QUERY_ID, 
1981                                                              server_acc_name);
1982         if (!modest_server_account) {
1983                 g_warning ("%s: could not get tny account\n", __FUNCTION__);
1984                 return;
1985         }
1986
1987         /* Update active account, but only if it's not a pseudo-account */
1988         if ((!modest_tny_account_is_virtual_local_folders(modest_server_account)) &&
1989             (!modest_tny_account_is_memory_card_account(modest_server_account))) {
1990                 const gchar *modest_acc_name = 
1991                         modest_tny_account_get_parent_modest_account_name_for_server_account (modest_server_account);
1992                 if (modest_acc_name)
1993                         modest_window_set_active_account (window, modest_acc_name);
1994         }
1995         
1996         g_object_unref (modest_server_account);
1997 }
1998
1999
2000 static void
2001 folder_refreshed_cb (ModestMailOperation *mail_op, 
2002                      TnyFolder *folder, 
2003                      gpointer user_data)
2004 {
2005         ModestMainWindow *win = NULL;
2006         GtkWidget *header_view;
2007         gboolean folder_empty = FALSE;
2008         gboolean all_marked_as_deleted = FALSE;
2009
2010         g_return_if_fail (TNY_IS_FOLDER (folder));
2011
2012         win = MODEST_MAIN_WINDOW (user_data);
2013         header_view = 
2014                 modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2015
2016         if (header_view) {
2017                 TnyFolder *current_folder;
2018
2019                 current_folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
2020                 if (current_folder != NULL && folder != current_folder) {
2021                         g_object_unref (current_folder);
2022                         return;
2023                 }
2024                 g_object_unref (current_folder);
2025         }
2026
2027         /* Check if folder is empty and set headers view contents style */
2028         folder_empty = (tny_folder_get_all_count (folder) == 0);
2029         all_marked_as_deleted = modest_header_view_is_empty (MODEST_HEADER_VIEW(header_view));
2030         if (folder_empty || all_marked_as_deleted)
2031                 modest_main_window_set_contents_style (win,
2032                                                        MODEST_MAIN_WINDOW_CONTENTS_STYLE_EMPTY);
2033 }
2034
2035 void 
2036 modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
2037                                                TnyFolderStore *folder_store, 
2038                                                gboolean selected,
2039                                                ModestMainWindow *main_window)
2040 {
2041         ModestConf *conf;
2042         GtkWidget *header_view;
2043
2044         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2045
2046         header_view = modest_main_window_get_child_widget(main_window,
2047                                                           MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2048         if (!header_view)
2049                 return;
2050         
2051         conf = modest_runtime_get_conf ();
2052
2053         if (TNY_IS_ACCOUNT (folder_store)) {
2054                 if (selected) {
2055                         set_active_account_from_tny_account (TNY_ACCOUNT (folder_store), MODEST_WINDOW (main_window));
2056                         
2057                         /* Show account details */
2058                         modest_main_window_set_contents_style (main_window, MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS);
2059                 }
2060         } else {
2061                 if (TNY_IS_FOLDER (folder_store) && selected) {
2062                         
2063                         /* Update the active account */
2064                         TnyAccount *account = modest_tny_folder_get_account (TNY_FOLDER (folder_store));
2065                         if (account) {
2066                                 set_active_account_from_tny_account (account, MODEST_WINDOW (main_window));
2067                                 g_object_unref (account);
2068                                 account = NULL;
2069                         }
2070
2071                         /* Set the header style by default, it could
2072                            be changed later by the refresh callback to
2073                            empty */
2074                         modest_main_window_set_contents_style (main_window, 
2075                                                                MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
2076
2077                         /* Set folder on header view. This function
2078                            will call tny_folder_refresh_async so we
2079                            pass a callback that will be called when
2080                            finished. We use that callback to set the
2081                            empty view if there are no messages */
2082                         modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
2083                                                        TNY_FOLDER (folder_store),
2084                                                        folder_refreshed_cb,
2085                                                        main_window);
2086                         
2087                         /* Restore configuration. We need to do this
2088                            *after* the set_folder because the widget
2089                            memory asks the header view about its
2090                            folder  */
2091                         modest_widget_memory_restore (modest_runtime_get_conf (), 
2092                                                       G_OBJECT(header_view),
2093                                                       MODEST_CONF_HEADER_VIEW_KEY);
2094                 } else {
2095                         /* Update the active account */
2096                         //modest_window_set_active_account (MODEST_WINDOW (main_window), NULL);
2097                         /* Save only if we're seeing headers */
2098                         if (modest_main_window_get_contents_style (main_window) ==
2099                             MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS)
2100                                 modest_widget_memory_save (conf, G_OBJECT (header_view), 
2101                                                            MODEST_CONF_HEADER_VIEW_KEY);
2102                         modest_header_view_clear (MODEST_HEADER_VIEW(header_view));
2103                 }
2104         }
2105
2106         /* Update toolbar dimming state */
2107         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
2108 }
2109
2110 void 
2111 modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemType type,
2112                                      ModestWindow *win)
2113 {
2114         GtkWidget *dialog;
2115         gchar *txt, *item;
2116         gboolean online;
2117
2118         item = (type == MODEST_ITEM_TYPE_FOLDER) ? "folder" : "message";
2119         
2120         online = tny_device_is_online (modest_runtime_get_device());
2121
2122         if (online) {
2123                 /* already online -- the item is simply not there... */
2124                 dialog = gtk_message_dialog_new (GTK_WINDOW (win),
2125                                                  GTK_DIALOG_MODAL,
2126                                                  GTK_MESSAGE_WARNING,
2127                                                  GTK_BUTTONS_NONE,
2128                                                  _("The %s you selected cannot be found"),
2129                                                  item);
2130                 gtk_dialog_add_button (GTK_DIALOG (dialog),_("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT);
2131                 gtk_dialog_run (GTK_DIALOG(dialog));
2132         } else {
2133                 dialog = gtk_dialog_new_with_buttons (_("Connection requested"),
2134                                                       GTK_WINDOW (win),
2135                                                       GTK_DIALOG_MODAL,
2136                                                       _("mcen_bd_dialog_cancel"),
2137                                                       GTK_RESPONSE_REJECT,
2138                                                       _("mcen_bd_dialog_ok"),
2139                                                       GTK_RESPONSE_ACCEPT,
2140                                                       NULL);
2141                 txt = g_strdup_printf (_("This %s is not available in offline mode.\n"
2142                                          "Do you want to get online?"), item);
2143                 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
2144                                     gtk_label_new (txt), FALSE, FALSE, 0);
2145                 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
2146                 g_free (txt);
2147
2148                 gtk_window_set_default_size (GTK_WINDOW(dialog), 300, 300);
2149                 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
2150                         /* TODO: Comment about why is this commented out: */
2151                         /* modest_platform_connect_and_wait (); */
2152                 }
2153         }
2154         gtk_widget_destroy (dialog);
2155 }
2156
2157 void
2158 modest_ui_actions_on_msg_link_hover (ModestMsgView *msgview, const gchar* link,
2159                                      ModestWindow *win)
2160 {
2161         /* g_message ("%s %s", __FUNCTION__, link); */
2162 }       
2163
2164
2165 void
2166 modest_ui_actions_on_msg_link_clicked (ModestMsgView *msgview, const gchar* link,
2167                                         ModestWindow *win)
2168 {
2169         modest_platform_activate_uri (link);
2170 }
2171
2172 void
2173 modest_ui_actions_on_msg_link_contextual (ModestMsgView *msgview, const gchar* link,
2174                                           ModestWindow *win)
2175 {
2176         modest_platform_show_uri_popup (link);
2177 }
2178
2179 void
2180 modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview, TnyMimePart *mime_part,
2181                                              ModestWindow *win)
2182 {
2183         modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (win), mime_part);
2184 }
2185
2186 void
2187 modest_ui_actions_on_msg_recpt_activated (ModestMsgView *msgview,
2188                                           const gchar *address,
2189                                           ModestWindow *win)
2190 {
2191         /* g_message ("%s %s", __FUNCTION__, address); */
2192 }
2193
2194 static void
2195 on_save_to_drafts_cb (ModestMailOperation *mail_op, 
2196                       TnyMsg *saved_draft,
2197                       gpointer user_data)
2198 {
2199         ModestMsgEditWindow *edit_window;
2200         char *info_text;
2201
2202         edit_window = MODEST_MSG_EDIT_WINDOW (user_data);
2203
2204         /* If there was any error do nothing */
2205         if (modest_mail_operation_get_error (mail_op) != NULL)
2206                 return;
2207
2208         modest_msg_edit_window_set_draft (edit_window, saved_draft);
2209         info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
2210         modest_platform_information_banner (NULL, NULL, info_text);
2211         g_free (info_text);
2212 }
2213
2214 void
2215 modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edit_window)
2216 {
2217         TnyTransportAccount *transport_account;
2218         ModestMailOperation *mail_operation;
2219         MsgData *data;
2220         gchar *account_name, *from;
2221         ModestAccountMgr *account_mgr;
2222
2223         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
2224         
2225         data = modest_msg_edit_window_get_msg_data (edit_window);
2226
2227         account_name = g_strdup (data->account_name);
2228         account_mgr = modest_runtime_get_account_mgr();
2229         if (!account_name)
2230                 account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
2231         if (!account_name) 
2232                 account_name = modest_account_mgr_get_default_account (account_mgr);
2233         if (!account_name) {
2234                 g_printerr ("modest: no account found\n");
2235                 modest_msg_edit_window_free_msg_data (edit_window, data);
2236                 return;
2237         }
2238
2239         if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
2240                 account_name = g_strdup (data->account_name);
2241         }
2242
2243         transport_account =
2244                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
2245                                       (modest_runtime_get_account_store(),
2246                                        account_name,
2247                                        TNY_ACCOUNT_TYPE_TRANSPORT));
2248         if (!transport_account) {
2249                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
2250                 g_free (account_name);
2251                 modest_msg_edit_window_free_msg_data (edit_window, data);
2252                 return;
2253         }
2254         from = modest_account_mgr_get_from_string (account_mgr, account_name);
2255
2256         /* Create the mail operation */         
2257         mail_operation = modest_mail_operation_new (G_OBJECT(edit_window));
2258         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
2259
2260         modest_mail_operation_save_to_drafts (mail_operation,
2261                                               transport_account,
2262                                               data->draft_msg,
2263                                               from,
2264                                               data->to, 
2265                                               data->cc, 
2266                                               data->bcc,
2267                                               data->subject, 
2268                                               data->plain_body, 
2269                                               data->html_body,
2270                                               data->attachments,
2271                                               data->images,
2272                                               data->priority_flags,
2273                                               on_save_to_drafts_cb,
2274                                               edit_window);
2275         /* Frees */
2276         g_free (from);
2277         g_free (account_name);
2278         g_object_unref (G_OBJECT (transport_account));
2279         g_object_unref (G_OBJECT (mail_operation));
2280
2281         modest_msg_edit_window_free_msg_data (edit_window, data);
2282         modest_msg_edit_window_reset_modified (edit_window);
2283 }
2284
2285 /* For instance, when clicking the Send toolbar button when editing a message: */
2286 void
2287 modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
2288 {
2289         TnyTransportAccount *transport_account = NULL;
2290
2291         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
2292
2293         if (!modest_msg_edit_window_check_names (edit_window, TRUE))
2294                 return;
2295         
2296         /* FIXME: Code added just for testing. The final version will
2297            use the send queue provided by tinymail and some
2298            classifier */
2299         MsgData *data = modest_msg_edit_window_get_msg_data (edit_window);
2300
2301         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
2302         gchar *account_name = g_strdup (data->account_name);
2303         if (!account_name)
2304                 g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
2305
2306         if (!account_name) 
2307                 account_name = modest_account_mgr_get_default_account (account_mgr);
2308                 
2309         if (!account_name) {
2310                 modest_msg_edit_window_free_msg_data (edit_window, data);
2311                 /* Run account setup wizard */
2312                 if (!modest_ui_actions_run_account_setup_wizard (MODEST_WINDOW(edit_window)))
2313                         return;
2314         }
2315         
2316         /* Get the currently-active transport account for this modest account: */
2317 /*      TnyTransportAccount *transport_account = */
2318 /*              TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection */
2319 /*                                    (modest_runtime_get_account_store(), */
2320 /*                                     account_name)); */
2321         if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) != 0) {
2322                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
2323                                                           (modest_runtime_get_account_store(),
2324                                                            account_name, TNY_ACCOUNT_TYPE_TRANSPORT));
2325         }
2326         
2327         if (!transport_account) {
2328                 /* Run account setup wizard */
2329                 if (!modest_ui_actions_run_account_setup_wizard(MODEST_WINDOW(edit_window)))
2330                         return;
2331         }
2332         
2333         gchar *from = modest_account_mgr_get_from_string (account_mgr, account_name);
2334
2335         /* Create the mail operation */
2336         ModestMailOperation *mail_operation = modest_mail_operation_new (G_OBJECT(edit_window));
2337         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
2338
2339         modest_mail_operation_send_new_mail (mail_operation,
2340                                              transport_account,
2341                                              data->draft_msg,
2342                                              from,
2343                                              data->to, 
2344                                              data->cc, 
2345                                              data->bcc,
2346                                              data->subject, 
2347                                              data->plain_body, 
2348                                              data->html_body,
2349                                              data->attachments,
2350                                              data->images,
2351                                              data->priority_flags);
2352                                              
2353         /* Free data: */
2354         g_free (from);
2355         g_free (account_name);
2356         g_object_unref (G_OBJECT (transport_account));
2357         g_object_unref (G_OBJECT (mail_operation));
2358
2359         modest_msg_edit_window_free_msg_data (edit_window, data);
2360         modest_msg_edit_window_set_sent (edit_window, TRUE);
2361
2362         /* Save settings and close the window: */
2363         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (edit_window));
2364 }
2365
2366 void 
2367 modest_ui_actions_on_toggle_bold (GtkToggleAction *action,
2368                                   ModestMsgEditWindow *window)
2369 {
2370         ModestMsgEditFormatState *format_state = NULL;
2371
2372         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2373         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
2374
2375         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2376                 return;
2377
2378         format_state = modest_msg_edit_window_get_format_state (window);
2379         g_return_if_fail (format_state != NULL);
2380
2381         format_state->bold = gtk_toggle_action_get_active (action);
2382         modest_msg_edit_window_set_format_state (window, format_state);
2383         g_free (format_state);
2384         
2385 }
2386
2387 void 
2388 modest_ui_actions_on_toggle_italics (GtkToggleAction *action,
2389                                      ModestMsgEditWindow *window)
2390 {
2391         ModestMsgEditFormatState *format_state = NULL;
2392
2393         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2394         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
2395
2396         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2397                 return;
2398
2399         format_state = modest_msg_edit_window_get_format_state (window);
2400         g_return_if_fail (format_state != NULL);
2401
2402         format_state->italics = gtk_toggle_action_get_active (action);
2403         modest_msg_edit_window_set_format_state (window, format_state);
2404         g_free (format_state);
2405         
2406 }
2407
2408 void 
2409 modest_ui_actions_on_toggle_bullets (GtkToggleAction *action,
2410                                      ModestMsgEditWindow *window)
2411 {
2412         ModestMsgEditFormatState *format_state = NULL;
2413
2414         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2415         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
2416
2417         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2418                 return;
2419
2420         format_state = modest_msg_edit_window_get_format_state (window);
2421         g_return_if_fail (format_state != NULL);
2422
2423         format_state->bullet = gtk_toggle_action_get_active (action);
2424         modest_msg_edit_window_set_format_state (window, format_state);
2425         g_free (format_state);
2426         
2427 }
2428
2429 void 
2430 modest_ui_actions_on_change_justify (GtkRadioAction *action,
2431                                      GtkRadioAction *selected,
2432                                      ModestMsgEditWindow *window)
2433 {
2434         ModestMsgEditFormatState *format_state = NULL;
2435         GtkJustification value;
2436
2437         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2438
2439         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2440                 return;
2441
2442         value = gtk_radio_action_get_current_value (selected);
2443
2444         format_state = modest_msg_edit_window_get_format_state (window);
2445         g_return_if_fail (format_state != NULL);
2446
2447         format_state->justification = value;
2448         modest_msg_edit_window_set_format_state (window, format_state);
2449         g_free (format_state);
2450 }
2451
2452 void 
2453 modest_ui_actions_on_select_editor_color (GtkAction *action,
2454                                           ModestMsgEditWindow *window)
2455 {
2456         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2457         g_return_if_fail (GTK_IS_ACTION (action));
2458
2459         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2460                 return;
2461
2462         modest_msg_edit_window_select_color (window);
2463 }
2464
2465 void 
2466 modest_ui_actions_on_select_editor_background_color (GtkAction *action,
2467                                                      ModestMsgEditWindow *window)
2468 {
2469         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2470         g_return_if_fail (GTK_IS_ACTION (action));
2471
2472         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2473                 return;
2474
2475         modest_msg_edit_window_select_background_color (window);
2476 }
2477
2478 void 
2479 modest_ui_actions_on_insert_image (GtkAction *action,
2480                                    ModestMsgEditWindow *window)
2481 {
2482         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2483         g_return_if_fail (GTK_IS_ACTION (action));
2484
2485         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
2486                 return;
2487
2488         modest_msg_edit_window_insert_image (window);
2489 }
2490
2491 void 
2492 modest_ui_actions_on_attach_file (GtkAction *action,
2493                                   ModestMsgEditWindow *window)
2494 {
2495         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2496         g_return_if_fail (GTK_IS_ACTION (action));
2497
2498         modest_msg_edit_window_offer_attach_file (window);
2499 }
2500
2501 void 
2502 modest_ui_actions_on_remove_attachments (GtkAction *action,
2503                                          ModestMsgEditWindow *window)
2504 {
2505         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2506         g_return_if_fail (GTK_IS_ACTION (action));
2507
2508         modest_msg_edit_window_remove_attachments (window, NULL);
2509 }
2510
2511 static void
2512 modest_ui_actions_new_folder_error_handler (ModestMailOperation *mail_op,
2513                                             gpointer user_data)
2514 {
2515         ModestMainWindow *window = MODEST_MAIN_WINDOW (user_data);
2516         const GError *error = modest_mail_operation_get_error (mail_op);
2517
2518         if(error) {
2519                 modest_platform_information_banner (GTK_WIDGET (window), NULL,
2520                                                     _("mail_in_ui_folder_create_error"));
2521         }
2522 }
2523
2524 static void
2525 modest_ui_actions_create_folder(GtkWidget *parent_window,
2526                                 GtkWidget *folder_view)
2527 {
2528         TnyFolderStore *parent_folder;
2529
2530         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2531         
2532         if (parent_folder) {
2533                 gboolean finished = FALSE;
2534                 gint result;
2535                 gchar *folder_name = NULL, *suggested_name = NULL;
2536                 const gchar *proto_str = NULL;
2537                 TnyAccount *account;
2538
2539                 if (TNY_IS_ACCOUNT (parent_folder))
2540                         account = g_object_ref (parent_folder);
2541                 else
2542                         account = tny_folder_get_account (TNY_FOLDER (parent_folder));
2543                 proto_str = tny_account_get_proto (TNY_ACCOUNT (account));
2544
2545                 if (proto_str && modest_protocol_info_get_transport_store_protocol (proto_str) ==
2546                     MODEST_PROTOCOL_STORE_POP) {
2547                         finished = TRUE;
2548                         hildon_banner_show_information (NULL, NULL, _("mail_in_ui_folder_create_error"));
2549                 }
2550                 g_object_unref (account);
2551
2552                 /* Run the new folder dialog */
2553                 while (!finished) {
2554                         result = modest_platform_run_new_folder_dialog (GTK_WINDOW (parent_window),
2555                                                                         parent_folder,
2556                                                                         suggested_name,
2557                                                                         &folder_name);
2558
2559                         g_free (suggested_name);
2560                         suggested_name = NULL;
2561
2562                         if (result == GTK_RESPONSE_ACCEPT) {
2563                                 ModestMailOperation *mail_op;
2564                                 TnyFolder *new_folder = NULL;
2565
2566                                 mail_op  = modest_mail_operation_new_with_error_handling (G_OBJECT(parent_window),
2567                                                                                           modest_ui_actions_new_folder_error_handler,
2568                                                                                           parent_window, NULL);
2569
2570                                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
2571                                                                  mail_op);
2572                                 new_folder = modest_mail_operation_create_folder (mail_op,
2573                                                                                   parent_folder,
2574                                                                                   (const gchar *) folder_name);
2575                                 if (new_folder) {
2576                                         modest_folder_view_select_folder (MODEST_FOLDER_VIEW(folder_view), 
2577                                                                           new_folder, TRUE);
2578
2579                                         g_object_unref (new_folder);
2580                                         finished = TRUE;
2581                                 }
2582                                 g_object_unref (mail_op);
2583                         } else {
2584                                 finished = TRUE;
2585                         }
2586
2587                         suggested_name = folder_name;
2588                         folder_name = NULL;
2589                 }
2590
2591                 g_object_unref (parent_folder);
2592         }
2593 }
2594
2595 void 
2596 modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_window)
2597 {
2598         GtkWidget *folder_view;
2599         
2600         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2601
2602         folder_view = modest_main_window_get_child_widget (main_window,
2603                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2604         if (!folder_view)
2605                 return;
2606
2607         modest_ui_actions_create_folder (GTK_WIDGET (main_window), folder_view);
2608 }
2609
2610 static void
2611 modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
2612                                                gpointer user_data)
2613 {
2614         ModestMainWindow *window = MODEST_MAIN_WINDOW (user_data);
2615         const GError *error = NULL;
2616         const gchar *message = NULL;
2617         
2618         /* Get error message */
2619         error = modest_mail_operation_get_error (mail_op);
2620         if (!error)
2621                 g_return_if_reached ();
2622
2623         switch (error->code) {
2624         case MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS:
2625                 message = _CS("ckdg_ib_folder_already_exists");
2626                 break;
2627         default:
2628                 g_warning ("%s: BUG: unexpected error:[%d]: %s", __FUNCTION__,
2629                            error->code, error->message);
2630                 return;
2631         }
2632
2633         modest_platform_information_banner (GTK_WIDGET (window), NULL, message);
2634 }
2635
2636 void 
2637 modest_ui_actions_on_rename_folder (GtkAction *action,
2638                                      ModestMainWindow *main_window)
2639 {
2640         TnyFolderStore *folder;
2641         GtkWidget *folder_view;
2642         GtkWidget *header_view; 
2643
2644         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2645
2646         folder_view = modest_main_window_get_child_widget (main_window,
2647                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2648         if (!folder_view)
2649                 return;
2650
2651         header_view = modest_main_window_get_child_widget (main_window,
2652                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2653         
2654         if (!header_view)
2655                 return;
2656
2657         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2658
2659         if (!folder)
2660                 return;
2661
2662         if (TNY_IS_FOLDER (folder)) {
2663                 gchar *folder_name;
2664                 gint response;
2665                 const gchar *current_name;
2666                 TnyFolderStore *parent;
2667                 gboolean do_rename = TRUE;
2668
2669                 current_name = tny_folder_get_name (TNY_FOLDER (folder));
2670                 parent = tny_folder_get_folder_store (TNY_FOLDER (folder));
2671                 response = modest_platform_run_rename_folder_dialog (GTK_WINDOW (main_window), 
2672                                                                      parent, current_name, 
2673                                                                      &folder_name);
2674                 g_object_unref (parent);
2675
2676                 if (response != GTK_RESPONSE_ACCEPT || strlen (folder_name) == 0) {
2677                         do_rename = FALSE;
2678                 } else if (modest_platform_is_network_folderstore(folder) &&
2679                            !tny_device_is_online (modest_runtime_get_device())) {
2680                         TnyAccount *account = tny_folder_get_account(TNY_FOLDER(folder));
2681                         do_rename = modest_platform_connect_and_wait(GTK_WINDOW(main_window), account);
2682                         g_object_unref(account);
2683                 }
2684
2685                 if (do_rename) {
2686                         ModestMailOperation *mail_op;
2687                         GtkTreeSelection *sel = NULL;
2688
2689                         mail_op = 
2690                                 modest_mail_operation_new_with_error_handling (G_OBJECT(main_window),
2691                                                                                modest_ui_actions_rename_folder_error_handler,
2692                                                                                main_window, NULL);
2693
2694                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
2695                                                          mail_op);
2696
2697                         /* Clear the headers view */
2698                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view));
2699                         gtk_tree_selection_unselect_all (sel);
2700
2701                         /* Select *after* the changes */
2702                         modest_folder_view_select_folder (MODEST_FOLDER_VIEW(folder_view),
2703                                                           TNY_FOLDER(folder), TRUE);
2704
2705                         /* Actually rename the folder */
2706                         modest_mail_operation_rename_folder (mail_op,
2707                                                              TNY_FOLDER (folder),
2708                                                              (const gchar *) folder_name);
2709
2710                         g_object_unref (mail_op);
2711                         g_free (folder_name);
2712                 }
2713         }
2714         g_object_unref (folder);
2715 }
2716
2717 static void
2718 modest_ui_actions_delete_folder_error_handler (ModestMailOperation *mail_op,
2719                                                gpointer user_data)
2720 {
2721         GObject *win = modest_mail_operation_get_source (mail_op);
2722
2723         modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
2724                                                 _("mail_in_ui_folder_delete_error"));
2725         g_object_unref (win);
2726 }
2727
2728 static gboolean
2729 delete_folder (ModestMainWindow *main_window, gboolean move_to_trash) 
2730 {
2731         TnyFolderStore *folder;
2732         GtkWidget *folder_view;
2733         gint response;
2734         gchar *message;
2735         gboolean do_delete = TRUE;
2736
2737         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (main_window), FALSE);
2738
2739         folder_view = modest_main_window_get_child_widget (main_window,
2740                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2741         if (!folder_view)
2742                 return FALSE;
2743
2744         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2745
2746         /* Show an error if it's an account */
2747         if (!TNY_IS_FOLDER (folder)) {
2748                 modest_platform_run_information_dialog (GTK_WINDOW (main_window),
2749                                                         _("mail_in_ui_folder_delete_error"));
2750                 g_object_unref (G_OBJECT (folder));
2751                 return FALSE;
2752         }
2753
2754         /* Ask the user */      
2755         message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
2756                                     tny_folder_get_name (TNY_FOLDER (folder)));
2757         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (main_window),
2758                                                             (const gchar *) message);
2759         g_free (message);
2760
2761         if (response != GTK_RESPONSE_OK) {
2762                 do_delete = FALSE;
2763         } else if (modest_platform_is_network_folderstore(folder) &&
2764                    !tny_device_is_online (modest_runtime_get_device())) {
2765                 TnyAccount *account = tny_folder_get_account(TNY_FOLDER(folder));
2766                 do_delete = modest_platform_connect_and_wait(GTK_WINDOW(main_window), account);
2767                 g_object_unref(account);
2768         }
2769
2770         if (do_delete) {
2771                 ModestMailOperation *mail_op;
2772                 GtkTreeSelection *sel;
2773
2774                 /* Unselect the folder before deleting it to free the headers */
2775                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view));
2776                 gtk_tree_selection_unselect_all (sel);
2777
2778                 /* Create the mail operation */
2779                 mail_op =
2780                         modest_mail_operation_new_with_error_handling (G_OBJECT(main_window),
2781                                                                        modest_ui_actions_delete_folder_error_handler,
2782                                                                        NULL, NULL);
2783
2784                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
2785                                                  mail_op);
2786                 modest_mail_operation_remove_folder (mail_op, TNY_FOLDER (folder), move_to_trash);
2787                 g_object_unref (G_OBJECT (mail_op));
2788         }
2789
2790         g_object_unref (G_OBJECT (folder));
2791
2792         return do_delete;
2793 }
2794
2795 void 
2796 modest_ui_actions_on_delete_folder (GtkAction *action,
2797                                      ModestMainWindow *main_window)
2798 {
2799         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2800
2801         if (delete_folder (main_window, FALSE)) {
2802                 GtkWidget *folder_view;
2803
2804                 folder_view = modest_main_window_get_child_widget (main_window,
2805                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2806                 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
2807         }
2808 }
2809
2810 void 
2811 modest_ui_actions_on_move_folder_to_trash_folder (GtkAction *action, ModestMainWindow *main_window)
2812 {
2813         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2814         
2815         delete_folder (main_window, TRUE);
2816 }
2817
2818
2819 static void
2820 show_error (GtkWidget *parent_widget, const gchar* text)
2821 {
2822         hildon_banner_show_information(parent_widget, NULL, text);
2823         
2824 #if 0
2825         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text)); */
2826         /*
2827           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
2828           (GtkDialogFlags)0,
2829           GTK_MESSAGE_ERROR,
2830           GTK_BUTTONS_OK,
2831           text ));
2832         */
2833                  
2834         gtk_dialog_run (dialog);
2835         gtk_widget_destroy (GTK_WIDGET (dialog));
2836 #endif
2837 }
2838
2839 void
2840 modest_ui_actions_on_password_requested (TnyAccountStore *account_store, 
2841                                          const gchar* server_account_name,
2842                                          gchar **username,
2843                                          gchar **password, 
2844                                          gboolean *cancel, 
2845                                          gboolean *remember,
2846                                          ModestMainWindow *main_window)
2847 {
2848         g_return_if_fail(server_account_name);
2849         
2850         /* Initalize output parameters: */
2851         if (cancel)
2852                 *cancel = FALSE;
2853                 
2854         if (remember)
2855                 *remember = TRUE;
2856                 
2857 #ifdef MODEST_PLATFORM_MAEMO
2858         /* Maemo uses a different (awkward) button order,
2859          * It should probably just use gtk_alternative_dialog_button_order ().
2860          */
2861         GtkWidget *dialog = gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
2862                                               NULL,
2863                                               GTK_DIALOG_MODAL,
2864                                               _("mcen_bd_dialog_ok"),
2865                                               GTK_RESPONSE_ACCEPT,
2866                                               _("mcen_bd_dialog_cancel"),
2867                                               GTK_RESPONSE_REJECT,
2868                                               NULL);
2869 #else
2870         GtkWidget *dialog = gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
2871                                               NULL,
2872                                               GTK_DIALOG_MODAL,
2873                                               GTK_STOCK_CANCEL,
2874                                               GTK_RESPONSE_REJECT,
2875                                               GTK_STOCK_OK,
2876                                               GTK_RESPONSE_ACCEPT,
2877                                               NULL);
2878 #endif /* MODEST_PLATFORM_MAEMO */
2879
2880         gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(main_window));
2881         
2882         gchar *server_name = modest_account_mgr_get_server_account_hostname (
2883                 modest_runtime_get_account_mgr(), server_account_name);
2884         if (!server_name) {/* This happened once, though I don't know why. murrayc. */
2885                 g_warning("%s: Could not get server name for server account '%s'", __FUNCTION__, server_account_name);
2886                 if (cancel)
2887                         *cancel = TRUE;
2888                 return;
2889         }
2890         
2891         /* This causes a warning because the logical ID has no %s in it, 
2892          * though the translation does, but there is not much we can do about that: */
2893         gchar *txt = g_strdup_printf (_("mail_ia_password_info"), server_name);
2894         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), gtk_label_new(txt),
2895                             FALSE, FALSE, 0);
2896         g_free (txt);
2897         g_free (server_name);
2898         server_name = NULL;
2899
2900         /* username: */
2901         gchar *initial_username = modest_account_mgr_get_server_account_username (
2902                 modest_runtime_get_account_mgr(), server_account_name);
2903         
2904         GtkWidget *entry_username = gtk_entry_new ();
2905         if (initial_username)
2906                 gtk_entry_set_text (GTK_ENTRY (entry_username), initial_username);
2907         /* Dim this if a connection has ever succeeded with this username,
2908          * as per the UI spec: */
2909         const gboolean username_known = 
2910                 modest_account_mgr_get_server_account_username_has_succeeded(
2911                         modest_runtime_get_account_mgr(), server_account_name);
2912         gtk_widget_set_sensitive (entry_username, !username_known);
2913         
2914 #ifdef MODEST_PLATFORM_MAEMO
2915         /* Auto-capitalization is the default, so let's turn it off: */
2916         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_username), HILDON_GTK_INPUT_MODE_FULL);
2917         
2918         /* Create a size group to be used by all captions.
2919          * Note that HildonCaption does not create a default size group if we do not specify one.
2920          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
2921         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
2922         
2923         GtkWidget *caption = hildon_caption_new (sizegroup, 
2924                 _("mail_fi_username"), entry_username, NULL, HILDON_CAPTION_MANDATORY);
2925         gtk_widget_show (entry_username);
2926         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption, 
2927                 FALSE, FALSE, MODEST_MARGIN_HALF);
2928         gtk_widget_show (caption);
2929 #else 
2930         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_username,
2931                             TRUE, FALSE, 0);
2932 #endif /* MODEST_PLATFORM_MAEMO */      
2933                             
2934         /* password: */
2935         GtkWidget *entry_password = gtk_entry_new ();
2936         gtk_entry_set_visibility (GTK_ENTRY(entry_password), FALSE);
2937         /* gtk_entry_set_invisible_char (GTK_ENTRY(entry_password), "*"); */
2938         
2939 #ifdef MODEST_PLATFORM_MAEMO
2940         /* Auto-capitalization is the default, so let's turn it off: */
2941         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_password), 
2942                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
2943         
2944         caption = hildon_caption_new (sizegroup, 
2945                 _("mail_fi_password"), entry_password, NULL, HILDON_CAPTION_MANDATORY);
2946         gtk_widget_show (entry_password);
2947         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption, 
2948                 FALSE, FALSE, MODEST_MARGIN_HALF);
2949         gtk_widget_show (caption);
2950         g_object_unref (sizegroup);
2951 #else 
2952         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_password,
2953                             TRUE, FALSE, 0);
2954 #endif /* MODEST_PLATFORM_MAEMO */      
2955
2956         if (initial_username != NULL)
2957                 gtk_widget_grab_focus (GTK_WIDGET (entry_password));
2958                                 
2959 /* This is not in the Maemo UI spec:
2960         remember_pass_check = gtk_check_button_new_with_label (_("Remember password"));
2961         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), remember_pass_check,
2962                             TRUE, FALSE, 0);
2963 */
2964
2965         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
2966         
2967         if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
2968                 if (username) {
2969                         *username = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_username)));
2970                         
2971                         modest_account_mgr_set_server_account_username (
2972                                  modest_runtime_get_account_mgr(), server_account_name, 
2973                                  *username);
2974                                  
2975                         const gboolean username_was_changed = 
2976                                 (strcmp (*username, initial_username) != 0);
2977                         if (username_was_changed) {
2978                                 g_warning ("%s: tinymail does not yet support changing the "
2979                                         "username in the get_password() callback.\n", __FUNCTION__);
2980                         }
2981                 }
2982                         
2983                 if (password) {
2984                         *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_password)));
2985                         
2986                         /* We do not save the password in the configuration, 
2987                          * because this function is only called for passwords that should 
2988                          * not be remembered:
2989                         modest_server_account_set_password (
2990                                  modest_runtime_get_account_mgr(), server_account_name, 
2991                                  *password);
2992                         */
2993                 }
2994                 
2995                 if (cancel)
2996                         *cancel   = FALSE;
2997                         
2998         } else {
2999                 show_error(GTK_WIDGET (main_window), _("mail_ib_login_cancelled"));
3000                 
3001                 if (username)
3002                         *username = NULL;
3003                         
3004                 if (password)
3005                         *password = NULL;
3006                         
3007                 if (cancel)
3008                         *cancel   = TRUE;
3009         }
3010
3011 /* This is not in the Maemo UI spec:
3012         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (remember_pass_check)))
3013                 *remember = TRUE;
3014         else
3015                 *remember = FALSE;
3016 */
3017
3018         gtk_widget_destroy (dialog);
3019         
3020         /* printf ("DEBUG: %s: cancel=%d\n", __FUNCTION__, *cancel); */
3021 }
3022
3023 void
3024 modest_ui_actions_on_cut (GtkAction *action,
3025                           ModestWindow *window)
3026 {
3027         GtkWidget *focused_widget;
3028         GtkClipboard *clipboard;
3029
3030         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3031         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
3032         if (GTK_IS_EDITABLE (focused_widget)) {
3033                 gtk_editable_cut_clipboard (GTK_EDITABLE(focused_widget));
3034                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3035                 gtk_clipboard_store (clipboard);
3036         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
3037                 GtkTextBuffer *buffer;
3038
3039                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
3040                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3041                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3042                 gtk_clipboard_store (clipboard);
3043         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
3044                 TnyList *header_list = modest_header_view_get_selected_headers (
3045                                 MODEST_HEADER_VIEW (focused_widget));
3046                 gboolean continue_download = FALSE;
3047                 gint num_of_unc_msgs;
3048
3049                 num_of_unc_msgs = header_list_count_uncached_msgs(header_list);
3050
3051                 if (num_of_unc_msgs) {
3052                         TnyAccount *account = get_account_from_header_list (header_list);
3053                         continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
3054                         g_object_unref (account);
3055                 }
3056
3057                 if (num_of_unc_msgs == 0 || continue_download) {
3058 /*                      modest_platform_information_banner (
3059                                         NULL, NULL, _CS("mcen_ib_getting_items"));*/
3060                         modest_header_view_cut_selection (
3061                                         MODEST_HEADER_VIEW (focused_widget));
3062                 }
3063
3064                 g_object_unref (header_list);
3065         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
3066                 modest_folder_view_cut_selection (MODEST_FOLDER_VIEW (focused_widget));
3067         }
3068 }
3069
3070 void
3071 modest_ui_actions_on_copy (GtkAction *action,
3072                            ModestWindow *window)
3073 {
3074         GtkClipboard *clipboard;
3075         GtkWidget *focused_widget;
3076         gboolean copied = TRUE;
3077
3078         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3079         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
3080
3081         if (GTK_IS_LABEL (focused_widget)) {
3082                 gtk_clipboard_set_text (clipboard, gtk_label_get_text (GTK_LABEL (focused_widget)), -1);
3083                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3084                 gtk_clipboard_store (clipboard);
3085         } else if (GTK_IS_EDITABLE (focused_widget)) {
3086                 gtk_editable_copy_clipboard (GTK_EDITABLE(focused_widget));
3087                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3088                 gtk_clipboard_store (clipboard);
3089         } else if (GTK_IS_HTML (focused_widget)) {
3090                 gtk_html_copy (GTK_HTML (focused_widget));
3091                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3092                 gtk_clipboard_store (clipboard);
3093         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
3094                 GtkTextBuffer *buffer;
3095                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
3096                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3097                 gtk_clipboard_set_can_store (clipboard, NULL, 0);
3098                 gtk_clipboard_store (clipboard);
3099         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
3100                 TnyList *header_list = modest_header_view_get_selected_headers (
3101                                 MODEST_HEADER_VIEW (focused_widget));
3102                 gboolean continue_download = FALSE;
3103                 gint num_of_unc_msgs;
3104
3105                 num_of_unc_msgs = header_list_count_uncached_msgs(header_list);
3106
3107                 if (num_of_unc_msgs) {
3108                         TnyAccount *account = get_account_from_header_list (header_list);
3109                         continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
3110                         g_object_unref (account);
3111                 }
3112
3113                 if (num_of_unc_msgs == 0 || continue_download) {
3114                         modest_platform_information_banner (
3115                                         NULL, NULL, _CS("mcen_ib_getting_items"));
3116                         modest_header_view_copy_selection (
3117                                         MODEST_HEADER_VIEW (focused_widget));
3118                 } else
3119                         copied = FALSE;
3120
3121                 g_object_unref (header_list);
3122
3123         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
3124                 modest_folder_view_copy_selection (MODEST_FOLDER_VIEW (focused_widget));
3125         }
3126
3127         /* Show information banner if there was a copy to clipboard */
3128         if(copied)
3129                 modest_platform_information_banner (
3130                                 NULL, NULL, _CS("ecoc_ib_edwin_copied"));
3131 }
3132
3133 void
3134 modest_ui_actions_on_undo (GtkAction *action,
3135                            ModestWindow *window)
3136 {
3137         ModestEmailClipboard *clipboard = NULL;
3138
3139         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
3140                 modest_msg_edit_window_undo (MODEST_MSG_EDIT_WINDOW (window));
3141         } else if (MODEST_IS_MAIN_WINDOW (window)) {
3142                 /* Clear clipboard source */
3143                 clipboard = modest_runtime_get_email_clipboard ();
3144                 modest_email_clipboard_clear (clipboard);               
3145         }
3146         else {
3147                 g_return_if_reached ();
3148         }
3149 }
3150
3151 void
3152 modest_ui_actions_on_redo (GtkAction *action,
3153                            ModestWindow *window)
3154 {
3155         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
3156                 modest_msg_edit_window_redo (MODEST_MSG_EDIT_WINDOW (window));
3157         }
3158         else {
3159                 g_return_if_reached ();
3160         }
3161 }
3162
3163
3164 static void
3165 destroy_information_note (ModestMailOperation *mail_op, gpointer user_data)
3166 {
3167         /* destroy information note */
3168         gtk_widget_destroy (GTK_WIDGET(user_data));
3169 }
3170
3171
3172 static void
3173 paste_as_attachment_free (gpointer data)
3174 {
3175         PasteAsAttachmentHelper *helper = (PasteAsAttachmentHelper *) data;
3176
3177         gtk_widget_destroy (helper->banner);
3178         g_object_unref (helper->banner);
3179         g_free (helper);
3180 }
3181
3182 static void
3183 paste_msg_as_attachment_cb (ModestMailOperation *mail_op,
3184                             TnyHeader *header,
3185                             TnyMsg *msg,
3186                             gpointer userdata)
3187 {
3188         PasteAsAttachmentHelper *helper = (PasteAsAttachmentHelper *) userdata;
3189         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (helper->window));
3190
3191         if (msg == NULL)
3192                 return;
3193
3194         modest_msg_edit_window_add_part (MODEST_MSG_EDIT_WINDOW (helper->window), TNY_MIME_PART (msg));
3195         
3196 }
3197
3198 void
3199 modest_ui_actions_on_paste (GtkAction *action,
3200                             ModestWindow *window)
3201 {
3202         GtkWidget *focused_widget = NULL;
3203         GtkWidget *inf_note = NULL;
3204         ModestMailOperation *mail_op = NULL;
3205
3206         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
3207         if (GTK_IS_EDITABLE (focused_widget)) {
3208                 gtk_editable_paste_clipboard (GTK_EDITABLE(focused_widget));
3209         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
3210                 ModestEmailClipboard *e_clipboard = NULL;
3211                 e_clipboard = modest_runtime_get_email_clipboard ();
3212                 if (modest_email_clipboard_cleared (e_clipboard)) {
3213                         GtkTextBuffer *buffer;
3214                         GtkClipboard *clipboard;
3215
3216                         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3217                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
3218                         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3219                 } else if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
3220                         ModestMailOperation *mail_op;
3221                         TnyFolder *src_folder;
3222                         TnyList *data;
3223                         gboolean delete;
3224                         PasteAsAttachmentHelper *helper = g_new0 (PasteAsAttachmentHelper, 1);
3225                         helper->window = MODEST_MSG_EDIT_WINDOW (window);
3226                         helper->banner = modest_platform_animation_banner (GTK_WIDGET (window), NULL,
3227                                                                            _CS("ckct_nw_pasting"));
3228                         modest_email_clipboard_get_data (e_clipboard, &src_folder, &data, &delete);
3229                         mail_op = modest_mail_operation_new (G_OBJECT (window));
3230                         if (helper->banner != NULL) {
3231                                 g_object_ref (G_OBJECT (helper->banner));
3232                                 gtk_window_set_modal (GTK_WINDOW (helper->banner), FALSE);
3233                                 gtk_widget_show (GTK_WIDGET (helper->banner));
3234                         }
3235
3236                         if (data != NULL) {
3237                                 modest_mail_operation_get_msgs_full (mail_op, 
3238                                                                      data,
3239                                                                      (GetMsgAsyncUserCallback) paste_msg_as_attachment_cb,
3240                                                                      helper,
3241                                                                      paste_as_attachment_free);
3242                         }
3243                 }
3244         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
3245                 ModestEmailClipboard *clipboard = NULL;
3246                 TnyFolder *src_folder = NULL;
3247                 TnyFolderStore *folder_store = NULL;
3248                 TnyList *data = NULL;           
3249                 gboolean delete = FALSE;
3250                 
3251                 /* Check clipboard source */
3252                 clipboard = modest_runtime_get_email_clipboard ();
3253                 if (modest_email_clipboard_cleared (clipboard)) 
3254                         return;
3255                 
3256                 /* Get elements to paste */
3257                 modest_email_clipboard_get_data (clipboard, &src_folder, &data, &delete);
3258
3259                 /* Create a new mail operation */
3260                 mail_op = modest_mail_operation_new (G_OBJECT(window));
3261                 
3262                 /* Get destination folder */
3263                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (focused_widget));
3264
3265                 /* transfer messages  */
3266                 if (data != NULL) {
3267                         gint response = 0;
3268
3269                         /* Ask for user confirmation */
3270                         response = 
3271                                 modest_ui_actions_msgs_move_to_confirmation (window, 
3272                                                                              TNY_FOLDER (folder_store), 
3273                                                                              delete,
3274                                                                              data);
3275                         
3276                         if (response == GTK_RESPONSE_OK) {
3277                                 /* Launch notification */
3278                                 inf_note = modest_platform_animation_banner (GTK_WIDGET (window), NULL, 
3279                                                                              _CS("ckct_nw_pasting"));
3280                                 if (inf_note != NULL)  {
3281                                         gtk_window_set_modal (GTK_WINDOW(inf_note), FALSE);
3282                                         gtk_widget_show (GTK_WIDGET(inf_note));
3283                                 }
3284
3285                                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
3286                                 modest_mail_operation_xfer_msgs (mail_op, 
3287                                                                  data,
3288                                                                  TNY_FOLDER (folder_store),
3289                                                                  delete,
3290                                                                  destroy_information_note,
3291                                                                  inf_note);                             
3292                         } else {
3293                                 g_object_unref (mail_op);
3294                         }
3295                         
3296                 } else if (src_folder != NULL) {                        
3297                         /* Launch notification */
3298                         inf_note = modest_platform_animation_banner (GTK_WIDGET (window), NULL, 
3299                                                                      _CS("ckct_nw_pasting"));
3300                         if (inf_note != NULL)  {
3301                                 gtk_window_set_modal (GTK_WINDOW(inf_note), FALSE);
3302                                 gtk_widget_show (GTK_WIDGET(inf_note));
3303                         }
3304                         
3305                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
3306                         modest_mail_operation_xfer_folder (mail_op, 
3307                                                            src_folder,
3308                                                            folder_store,
3309                                                            delete,
3310                                                            destroy_information_note,
3311                                                            inf_note);
3312                 }
3313
3314                 /* Free */
3315                 if (data != NULL) 
3316                         g_object_unref (data);
3317                 if (src_folder != NULL) 
3318                         g_object_unref (src_folder);
3319                 if (folder_store != NULL) 
3320                         g_object_unref (folder_store);
3321         }
3322 }
3323
3324
3325 void
3326 modest_ui_actions_on_select_all (GtkAction *action,
3327                                  ModestWindow *window)
3328 {
3329         GtkWidget *focused_widget;
3330
3331         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
3332         if (MODEST_IS_ATTACHMENTS_VIEW (focused_widget)) {
3333                 modest_attachments_view_select_all (MODEST_ATTACHMENTS_VIEW (focused_widget));
3334         } else if (GTK_IS_LABEL (focused_widget)) {
3335                 gtk_label_select_region (GTK_LABEL (focused_widget), 0, -1);
3336         } else if (GTK_IS_EDITABLE (focused_widget)) {
3337                 gtk_editable_select_region (GTK_EDITABLE(focused_widget), 0, -1);
3338         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
3339                 GtkTextBuffer *buffer;
3340                 GtkTextIter start, end;
3341
3342                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
3343                 gtk_text_buffer_get_start_iter (buffer, &start);
3344                 gtk_text_buffer_get_end_iter (buffer, &end);
3345                 gtk_text_buffer_select_range (buffer, &start, &end);
3346         } else if (GTK_IS_HTML (focused_widget)) {
3347                 gtk_html_select_all (GTK_HTML (focused_widget));
3348         } else if (MODEST_IS_MAIN_WINDOW (window)) {
3349                 GtkWidget *header_view = focused_widget;
3350                 GtkTreeSelection *selection = NULL;
3351                 
3352                 if (!(MODEST_IS_HEADER_VIEW (focused_widget))) {
3353                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
3354                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
3355                 }
3356                                 
3357                 /* Disable window dimming management */
3358                 modest_window_disable_dimming (MODEST_WINDOW(window));
3359                 
3360                 /* Select all messages */
3361                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(header_view));
3362                 gtk_tree_selection_select_all (selection);
3363
3364                 /* Set focuse on header view */
3365                 gtk_widget_grab_focus (header_view);
3366
3367
3368                 /* Enable window dimming management */
3369                 modest_window_enable_dimming (MODEST_WINDOW(window));
3370                 modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
3371         }
3372
3373 }
3374
3375 void
3376 modest_ui_actions_on_mark_as_read (GtkAction *action,
3377                                    ModestWindow *window)
3378 {       
3379         g_return_if_fail (MODEST_IS_WINDOW(window));
3380                 
3381         /* Mark each header as read */
3382         do_headers_action (window, headers_action_mark_as_read, NULL);
3383 }
3384
3385 void
3386 modest_ui_actions_on_mark_as_unread (GtkAction *action,
3387                                      ModestWindow *window)
3388 {       
3389         g_return_if_fail (MODEST_IS_WINDOW(window));
3390                 
3391         /* Mark each header as read */
3392         do_headers_action (window, headers_action_mark_as_unread, NULL);
3393 }
3394
3395 void
3396 modest_ui_actions_on_change_zoom (GtkRadioAction *action,
3397                                   GtkRadioAction *selected,
3398                                   ModestWindow *window)
3399 {
3400         gint value;
3401
3402         value = gtk_radio_action_get_current_value (selected);
3403         if (MODEST_IS_WINDOW (window)) {
3404                 modest_window_set_zoom (MODEST_WINDOW (window), ((gdouble)value)/100);
3405         }
3406 }
3407
3408 void     
3409 modest_ui_actions_msg_edit_on_change_priority (GtkRadioAction *action,
3410                                                GtkRadioAction *selected,
3411                                                ModestWindow *window)
3412 {
3413         TnyHeaderFlags flags;
3414         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3415
3416         flags = gtk_radio_action_get_current_value (selected);
3417         modest_msg_edit_window_set_priority_flags (MODEST_MSG_EDIT_WINDOW (window), flags);
3418 }
3419
3420 void     
3421 modest_ui_actions_msg_edit_on_change_file_format (GtkRadioAction *action,
3422                                                   GtkRadioAction *selected,
3423                                                   ModestWindow *window)
3424 {
3425         gint file_format;
3426
3427         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3428
3429         file_format = gtk_radio_action_get_current_value (selected);
3430         modest_msg_edit_window_set_file_format (MODEST_MSG_EDIT_WINDOW (window), file_format);
3431 }
3432
3433
3434 void     
3435 modest_ui_actions_on_zoom_plus (GtkAction *action,
3436                                 ModestWindow *window)
3437 {
3438         g_return_if_fail (MODEST_IS_WINDOW (window));
3439
3440         modest_window_zoom_plus (MODEST_WINDOW (window));
3441 }
3442
3443 void     
3444 modest_ui_actions_on_zoom_minus (GtkAction *action,
3445                                  ModestWindow *window)
3446 {
3447         g_return_if_fail (MODEST_IS_WINDOW (window));
3448
3449         modest_window_zoom_minus (MODEST_WINDOW (window));
3450 }
3451
3452 void     
3453 modest_ui_actions_on_toggle_fullscreen    (GtkToggleAction *toggle,
3454                                            ModestWindow *window)
3455 {
3456         ModestWindowMgr *mgr;
3457         gboolean fullscreen, active;
3458         g_return_if_fail (MODEST_IS_WINDOW (window));
3459
3460         mgr = modest_runtime_get_window_mgr ();
3461
3462         active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle)))?1:0;
3463         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
3464
3465         if (active != fullscreen) {
3466                 modest_window_mgr_set_fullscreen_mode (mgr, active);
3467                 gtk_window_present (GTK_WINDOW (window));
3468         }
3469 }
3470
3471 void
3472 modest_ui_actions_on_change_fullscreen (GtkAction *action,
3473                                         ModestWindow *window)
3474 {
3475         ModestWindowMgr *mgr;
3476         gboolean fullscreen;
3477
3478         g_return_if_fail (MODEST_IS_WINDOW (window));
3479
3480         mgr = modest_runtime_get_window_mgr ();
3481         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
3482         modest_window_mgr_set_fullscreen_mode (mgr, !fullscreen);
3483
3484         gtk_window_present (GTK_WINDOW (window));
3485 }
3486
3487 /* 
3488  * Used by modest_ui_actions_on_details to call do_headers_action 
3489  */
3490 static void
3491 headers_action_show_details (TnyHeader *header, 
3492                              ModestWindow *window,
3493                              gpointer user_data)
3494
3495 {
3496         GtkWidget *dialog;
3497         
3498         /* Create dialog */
3499         dialog = modest_details_dialog_new_with_header (GTK_WINDOW (window), header);
3500
3501         /* Run dialog */
3502         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
3503         gtk_widget_show_all (dialog);
3504         gtk_dialog_run (GTK_DIALOG (dialog));
3505
3506         gtk_widget_destroy (dialog);
3507 }
3508
3509 /*
3510  * Show the folder details in a ModestDetailsDialog widget
3511  */
3512 static void
3513 show_folder_details (TnyFolder *folder, 
3514                      GtkWindow *window)
3515 {
3516         GtkWidget *dialog;
3517         
3518         /* Create dialog */
3519         dialog = modest_details_dialog_new_with_folder (window, folder);
3520
3521         /* Run dialog */
3522         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
3523         gtk_widget_show_all (dialog);
3524         gtk_dialog_run (GTK_DIALOG (dialog));
3525
3526         gtk_widget_destroy (dialog);
3527 }
3528
3529 /*
3530  * Show the header details in a ModestDetailsDialog widget
3531  */
3532 void     
3533 modest_ui_actions_on_details (GtkAction *action, 
3534                               ModestWindow *win)
3535 {
3536         TnyList * headers_list;
3537         TnyIterator *iter;
3538         TnyHeader *header;              
3539
3540         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
3541                 TnyMsg *msg;
3542
3543                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
3544                 if (!msg)
3545                         return;
3546                 g_object_unref (msg);           
3547
3548                 headers_list = get_selected_headers (win);
3549                 if (!headers_list)
3550                         return;
3551
3552                 iter = tny_list_create_iterator (headers_list);
3553
3554                 header = TNY_HEADER (tny_iterator_get_current (iter));
3555                 if (header) {
3556                         headers_action_show_details (header, win, NULL);
3557                         g_object_unref (header);
3558                 }
3559
3560                 g_object_unref (iter);
3561                 g_object_unref (headers_list);
3562
3563         } else if (MODEST_IS_MAIN_WINDOW (win)) {
3564                 GtkWidget *folder_view, *header_view;
3565
3566                 /* Check which widget has the focus */
3567                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
3568                                                                     MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3569                 if (gtk_widget_is_focus (folder_view)) {
3570                         TnyFolderStore *folder_store
3571                                 = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
3572                         if (!folder_store) {
3573                                 g_warning ("%s: No item was selected.\n", __FUNCTION__);
3574                                 return; 
3575                         }
3576                         /* Show only when it's a folder */
3577                         /* This function should not be called for account items, 
3578                          * because we dim the menu item for them. */
3579                         if (TNY_IS_FOLDER (folder_store)) {
3580                                 show_folder_details (TNY_FOLDER (folder_store), GTK_WINDOW (win));
3581                         }
3582
3583                         g_object_unref (folder_store);
3584
3585                 } else {
3586                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
3587                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
3588                         /* Show details of each header */
3589                         do_headers_action (win, headers_action_show_details, header_view);
3590                 }
3591         }
3592 }
3593
3594 void     
3595 modest_ui_actions_on_toggle_show_cc (GtkToggleAction *toggle,
3596                                      ModestMsgEditWindow *window)
3597 {
3598         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3599
3600         modest_msg_edit_window_show_cc (window, gtk_toggle_action_get_active (toggle));
3601 }
3602
3603 void     
3604 modest_ui_actions_on_toggle_show_bcc (GtkToggleAction *toggle,
3605                                       ModestMsgEditWindow *window)
3606 {
3607         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3608
3609         modest_msg_edit_window_show_bcc (window, gtk_toggle_action_get_active (toggle));
3610 }
3611
3612 void
3613 modest_ui_actions_toggle_folders_view (GtkAction *action, 
3614                                        ModestMainWindow *main_window)
3615 {
3616         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
3617
3618         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
3619                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SPLIT);
3620         else
3621                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SIMPLE);
3622 }
3623
3624 void 
3625 modest_ui_actions_on_toggle_toolbar (GtkToggleAction *toggle, 
3626                                      ModestWindow *window)
3627 {
3628         gboolean active, fullscreen = FALSE;
3629         ModestWindowMgr *mgr;
3630
3631         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle));
3632
3633         /* Check if we want to toggle the toolbar vuew in fullscreen
3634            or normal mode */
3635         if (!strcmp (gtk_action_get_name (GTK_ACTION (toggle)), 
3636                      "ViewShowToolbarFullScreen")) {
3637                 fullscreen = TRUE;
3638         }
3639
3640         /* Toggle toolbar */
3641         mgr = modest_runtime_get_window_mgr ();
3642         modest_window_mgr_show_toolbars (mgr, G_TYPE_FROM_INSTANCE (window), active, fullscreen);
3643 }
3644
3645 void     
3646 modest_ui_actions_msg_edit_on_select_font (GtkAction *action,
3647                                            ModestMsgEditWindow *window)
3648 {
3649         modest_msg_edit_window_select_font (window);
3650 }
3651
3652 void
3653 modest_ui_actions_on_folder_display_name_changed (ModestFolderView *folder_view,
3654                                                   const gchar *display_name,
3655                                                   GtkWindow *window)
3656 {
3657         /* This is usually used to change the title of the main window, which
3658          * is the one that holds the folder view. Note that this change can
3659          * happen even when the widget doesn't have the focus. */
3660         if (display_name)
3661                 gtk_window_set_title (window, display_name);
3662         else
3663                 gtk_window_set_title (window, " ");
3664 }
3665
3666 void
3667 modest_ui_actions_on_select_contacts (GtkAction *action, ModestMsgEditWindow *window)
3668 {
3669         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3670         modest_msg_edit_window_select_contacts (window);
3671 }
3672
3673 void
3674 modest_ui_actions_on_check_names (GtkAction *action, ModestMsgEditWindow *window)
3675 {
3676         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3677         modest_msg_edit_window_check_names (window, FALSE);
3678 }
3679
3680 static void
3681 create_move_to_dialog_on_new_folder(GtkWidget *button, gpointer user_data)
3682 {
3683         modest_ui_actions_create_folder (gtk_widget_get_toplevel (button),
3684                                          GTK_WIDGET (user_data));
3685 }
3686
3687 /*
3688  * This function is used to track changes in the selection of the
3689  * folder view that is inside the "move to" dialog to enable/disable
3690  * the OK button because we do not want the user to select a disallowed
3691  * destination for a folder.
3692  * The user also not desired to be able to use NEW button on items where
3693  * folder creation is not possibel.
3694  */
3695 static void
3696 on_move_to_dialog_folder_selection_changed (ModestFolderView* self,
3697                                             TnyFolderStore *folder_store,
3698                                             gboolean selected,
3699                                             gpointer user_data)
3700 {
3701         GtkWidget *dialog = NULL;
3702         GtkWidget *ok_button = NULL, *new_button = NULL;
3703         GList *children = NULL;
3704         gboolean ok_sensitive = TRUE, new_sensitive = TRUE;
3705         gboolean moving_folder = FALSE;
3706         gboolean is_local_account = TRUE;
3707         GtkWidget *folder_view = NULL;
3708         ModestTnyFolderRules rules;
3709
3710         if (!selected)
3711                 return;
3712
3713         /* Get the OK button */
3714         dialog = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_DIALOG);
3715         if (!dialog)
3716                 return;
3717
3718         children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
3719         ok_button = GTK_WIDGET (children->next->next->data);
3720         new_button = GTK_WIDGET (children->next->data);
3721         g_list_free (children);
3722
3723         /* check if folder_store is an remote account */
3724         if (TNY_IS_ACCOUNT (folder_store)) {
3725                 TnyAccount *local_account = NULL;
3726                 TnyAccount *mmc_account = NULL;
3727                 ModestTnyAccountStore *account_store = NULL;
3728
3729                 account_store = modest_runtime_get_account_store ();
3730                 local_account = modest_tny_account_store_get_local_folders_account (account_store);
3731                 mmc_account = modest_tny_account_store_get_mmc_folders_account (account_store);
3732
3733                 if ((gpointer) local_account != (gpointer) folder_store &&
3734                     (gpointer) mmc_account != (gpointer) folder_store) {
3735                         is_local_account = FALSE;
3736                         /* New button should be dimmed on remote
3737                            account root */
3738                         new_sensitive = FALSE;
3739                 }
3740                 g_object_unref (local_account);
3741         }
3742
3743         /* Check the target folder rules */
3744         if (TNY_IS_FOLDER (folder_store)) {
3745                 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder_store));
3746                 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
3747                         ok_sensitive = FALSE;
3748                         new_sensitive = FALSE;
3749                         goto end;
3750                 }
3751         }
3752
3753         /* Check if we're moving a folder */
3754         if (MODEST_IS_MAIN_WINDOW (user_data)) {
3755                 /* Get the widgets */
3756                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (user_data),
3757                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3758                 if (gtk_widget_is_focus (folder_view))
3759                         moving_folder = TRUE;
3760         }
3761
3762         if (moving_folder) {
3763                 TnyFolderStore *moved_folder = NULL, *parent = NULL;
3764
3765                 /* Get the folder to move */
3766                 moved_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
3767                 
3768                 /* Check that we're not moving to the same folder */
3769                 if (TNY_IS_FOLDER (moved_folder)) {
3770                         parent = tny_folder_get_folder_store (TNY_FOLDER (moved_folder));
3771                         if (parent == folder_store)
3772                                 ok_sensitive = FALSE;
3773                         g_object_unref (parent);
3774                 } 
3775
3776                 if (ok_sensitive && TNY_IS_ACCOUNT (folder_store)) {
3777                         /* Do not allow to move to an account unless it's the
3778                            local folders account */
3779                         if (!is_local_account)
3780                                 ok_sensitive = FALSE;
3781                 } 
3782
3783                 if (ok_sensitive && (moved_folder == folder_store)) {
3784                         /* Do not allow to move to itself */
3785                         ok_sensitive = FALSE;
3786                 }
3787                 g_object_unref (moved_folder);
3788         } else {
3789                 TnyHeader *header = NULL;
3790                 TnyFolder *src_folder = NULL;
3791
3792                 /* Moving a message */
3793                 if (MODEST_IS_MSG_VIEW_WINDOW (user_data)) {
3794                         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (user_data));
3795                         src_folder = tny_header_get_folder (header);
3796                         g_object_unref (header);
3797                 } else {
3798                         src_folder = 
3799                                 TNY_FOLDER (modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view)));
3800                 }
3801
3802                 /* Do not allow to move the msg to the same folder */
3803                 /* Do not allow to move the msg to an account */
3804                 if ((gpointer) src_folder == (gpointer) folder_store ||
3805                     TNY_IS_ACCOUNT (folder_store))
3806                         ok_sensitive = FALSE;
3807                 g_object_unref (src_folder);
3808         }
3809
3810  end:
3811         /* Set sensitivity of the OK button */
3812         gtk_widget_set_sensitive (ok_button, ok_sensitive);
3813         /* Set sensitivity of the NEW button */
3814         gtk_widget_set_sensitive (new_button, new_sensitive);
3815 }
3816
3817 static GtkWidget*
3818 create_move_to_dialog (GtkWindow *win,
3819                        GtkWidget *folder_view,
3820                        GtkWidget **tree_view)
3821 {
3822         GtkWidget *dialog, *scroll;
3823         GtkWidget *new_button;
3824
3825         dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
3826                                               GTK_WINDOW (win),
3827                                               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT,
3828                                               NULL);
3829
3830         gtk_dialog_add_button (GTK_DIALOG (dialog), _("mcen_bd_dialog_ok"), GTK_RESPONSE_ACCEPT);
3831         /* We do this manually so GTK+ does not associate a response ID for
3832          * the button. */
3833         new_button = gtk_button_new_from_stock (_("mcen_bd_new"));
3834         gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->action_area), new_button, FALSE, FALSE, 0);
3835         gtk_dialog_add_button (GTK_DIALOG (dialog), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_REJECT);
3836
3837         /* Create scrolled window */
3838         scroll = gtk_scrolled_window_new (NULL, NULL);
3839         gtk_scrolled_window_set_policy  (GTK_SCROLLED_WINDOW (scroll),
3840                                          GTK_POLICY_AUTOMATIC,
3841                                          GTK_POLICY_AUTOMATIC);
3842
3843         /* Create folder view */
3844         *tree_view = modest_platform_create_folder_view (NULL);
3845
3846         /* Track changes in the selection to
3847          * disable the OK button whenever "Move to" is not possible
3848          * disbale NEW button whenever New is not possible */
3849         g_signal_connect (*tree_view,
3850                           "folder_selection_changed",
3851                           G_CALLBACK (on_move_to_dialog_folder_selection_changed),
3852                           win);
3853
3854         /* Listen to clicks on New button */
3855         g_signal_connect (G_OBJECT (new_button), 
3856                           "clicked", 
3857                           G_CALLBACK(create_move_to_dialog_on_new_folder), 
3858                           *tree_view);
3859
3860         /* It could happen that we're trying to move a message from a
3861            window (msg window for example) after the main window was
3862            closed, so we can not just get the model of the folder
3863            view */
3864         if (MODEST_IS_FOLDER_VIEW (folder_view)) {
3865                 const gchar *visible_id = NULL;
3866
3867                 modest_folder_view_set_style (MODEST_FOLDER_VIEW (*tree_view),
3868                                               MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
3869                 modest_folder_view_copy_model (MODEST_FOLDER_VIEW(folder_view), 
3870                                                MODEST_FOLDER_VIEW(*tree_view));
3871
3872                 visible_id = 
3873                         modest_folder_view_get_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(folder_view));
3874
3875                 /* Show the same account than the one that is shown in the main window */
3876                 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(*tree_view), 
3877                                                                              visible_id);
3878         } else {
3879                 const gchar *active_account_name = NULL;
3880                 ModestAccountMgr *mgr = NULL;
3881                 ModestAccountData *acc_data = NULL;
3882
3883                 modest_folder_view_set_style (MODEST_FOLDER_VIEW (*tree_view),
3884                                               MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
3885                 modest_folder_view_update_model (MODEST_FOLDER_VIEW (*tree_view), 
3886                                                  TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
3887
3888                 active_account_name = modest_window_get_active_account (MODEST_WINDOW (win));
3889                 mgr = modest_runtime_get_account_mgr ();
3890                 acc_data = modest_account_mgr_get_account_data (mgr, active_account_name);
3891
3892                 /* Set the new visible & active account */
3893                 if (acc_data && acc_data->store_account) { 
3894                         modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (*tree_view),
3895                                                                                      acc_data->store_account->account_name);
3896                         modest_account_mgr_free_account_data (mgr, acc_data);
3897                 }
3898         }
3899
3900         /* Hide special folders */
3901         modest_folder_view_show_non_move_folders (MODEST_FOLDER_VIEW (*tree_view), FALSE);
3902         
3903         gtk_container_add (GTK_CONTAINER (scroll), *tree_view);
3904
3905         /* Add scroll to dialog */
3906         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
3907                             scroll, TRUE, TRUE, 0);
3908
3909         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
3910         gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 300);
3911
3912         return dialog;
3913 }
3914
3915 /*
3916  * Returns TRUE if at least one of the headers of the list belongs to
3917  * a message that has been fully retrieved.
3918  */
3919 #if 0 /* no longer in use. delete in 2007.10 */
3920 static gboolean
3921 has_retrieved_msgs (TnyList *list)
3922 {
3923         TnyIterator *iter;
3924         gboolean found = FALSE;
3925
3926         iter = tny_list_create_iterator (list);
3927         while (!tny_iterator_is_done (iter) && !found) {
3928                 TnyHeader *header;
3929                 TnyHeaderFlags flags = 0;
3930
3931                 header = TNY_HEADER (tny_iterator_get_current (iter));
3932                 if (header) {
3933                         flags = tny_header_get_flags (header);
3934                         if (flags & TNY_HEADER_FLAG_CACHED)
3935 /*                      if (!(flags & TNY_HEADER_FLAG_PARTIAL)) */
3936                                 found = TRUE;
3937
3938                         g_object_unref (header);
3939                 }
3940
3941                 if (!found)
3942                         tny_iterator_next (iter);
3943         }
3944         g_object_unref (iter);
3945
3946         return found;
3947 }
3948 #endif /* 0 */
3949
3950
3951 /*
3952  * Shows a confirmation dialog to the user when we're moving messages
3953  * from a remote server to the local storage. Returns the dialog
3954  * response. If it's other kind of movement then it always returns
3955  * GTK_RESPONSE_OK
3956  *
3957  * This one is used by the next functions:
3958  *      modest_ui_actions_on_paste                      - commented out
3959  *      drag_and_drop_from_header_view (for d&d in modest_folder_view.c)
3960  */
3961 gint
3962 modest_ui_actions_msgs_move_to_confirmation (ModestWindow *win,
3963                                              TnyFolder *dest_folder,
3964                                              gboolean delete,
3965                                              TnyList *headers)
3966 {
3967         gint response = GTK_RESPONSE_OK;
3968         TnyAccount *account = NULL;
3969         TnyFolder *src_folder = NULL;
3970         TnyIterator *iter = NULL;
3971         TnyHeader *header = NULL;
3972
3973         /* return with OK if the destination is a remote folder */
3974         if (modest_tny_folder_is_remote_folder (dest_folder))
3975                 return GTK_RESPONSE_OK;
3976
3977         /* Get source folder */
3978         iter = tny_list_create_iterator (headers);
3979         header = TNY_HEADER (tny_iterator_get_current (iter));
3980         if (header) {
3981                 src_folder = tny_header_get_folder (header);
3982                 g_object_unref (header);
3983         }
3984         g_object_unref (iter);
3985
3986         /* if no src_folder, message may be an attahcment */
3987         if (src_folder == NULL) 
3988                 return GTK_RESPONSE_CANCEL;
3989
3990         /* If the source is a local or MMC folder */
3991         if (!modest_tny_folder_is_remote_folder (src_folder)) {
3992                 g_object_unref (src_folder);
3993                 return GTK_RESPONSE_OK;
3994         }
3995
3996         /* Get the account */
3997         account = tny_folder_get_account (src_folder);
3998
3999         /* now if offline we ask the user */
4000         if(connect_to_get_msg (win, tny_list_get_length (headers), account))
4001                 response = GTK_RESPONSE_OK;
4002         else
4003                 response = GTK_RESPONSE_CANCEL;
4004
4005         /* Frees */
4006         g_object_unref (src_folder);
4007         g_object_unref (account);
4008
4009         return response;
4010 }
4011
4012
4013
4014 static void
4015 move_to_cb (ModestMailOperation *mail_op, gpointer user_data)
4016 {
4017         MoveToHelper *helper = (MoveToHelper *) user_data;
4018
4019         /* Note that the operation could have failed, in that case do
4020            nothing */
4021         if (modest_mail_operation_get_status (mail_op) == 
4022             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
4023
4024                 GObject *object = modest_mail_operation_get_source (mail_op);
4025                 if (MODEST_IS_MSG_VIEW_WINDOW (object)) {
4026                         ModestMsgViewWindow *self = MODEST_MSG_VIEW_WINDOW (object);
4027
4028                         if (!modest_msg_view_window_select_next_message (self))
4029                                 if (!modest_msg_view_window_select_previous_message (self))
4030                                         /* No more messages to view, so close this window */
4031                                         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW(self));
4032                 } else if (MODEST_IS_MAIN_WINDOW (object) && helper->reference != NULL) {
4033                         GtkWidget *header_view;
4034                         GtkTreePath *path;
4035                         GtkTreeSelection *sel;
4036
4037                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(object),
4038                                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4039                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
4040                         path = gtk_tree_row_reference_get_path (helper->reference);
4041                         gtk_tree_selection_select_path (sel, path);
4042                         gtk_tree_path_free (path);
4043                 }
4044                 g_object_unref (object);
4045         }
4046
4047         /* Close the "Pasting" information banner */
4048         gtk_widget_destroy (GTK_WIDGET(helper->banner));
4049         if (helper->reference != NULL)
4050                 gtk_tree_row_reference_free (helper->reference);
4051         g_free (helper);
4052 }
4053
4054 void
4055 modest_ui_actions_move_folder_error_handler (ModestMailOperation *mail_op, 
4056                                              gpointer user_data)
4057 {
4058         ModestWindow *main_window = NULL;
4059         GObject *win = NULL;
4060         const GError *error = NULL;
4061         const gchar *message = NULL;
4062         
4063         /* Get error message */
4064         error = modest_mail_operation_get_error (mail_op);
4065         if (error != NULL && error->message != NULL) {
4066                 message = error->message;
4067         } else {
4068                 message = _("mail_in_ui_folder_move_target_error");
4069         }
4070         
4071         /* Disable next automatic folder selection */
4072         main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
4073                                                          FALSE); /* don't create */
4074         if (main_window) {
4075                 GtkWidget *folder_view = NULL;
4076         
4077                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_window),
4078                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW); 
4079                 modest_folder_view_disable_next_folder_selection (MODEST_FOLDER_VIEW(folder_view));
4080                 
4081                 if (user_data && TNY_IS_FOLDER (user_data)) {
4082                         modest_folder_view_select_folder (MODEST_FOLDER_VIEW (folder_view), 
4083                                                           TNY_FOLDER (user_data), FALSE);
4084                 }
4085         }
4086
4087         /* Show notification dialog */
4088         win = modest_mail_operation_get_source (mail_op);
4089         modest_platform_run_information_dialog ((GtkWindow *) win, message);
4090         if (win)
4091                 g_object_unref (win);
4092 }
4093
4094 void
4095 modest_ui_actions_send_receive_error_handler (ModestMailOperation *mail_op, 
4096                                               gpointer user_data)
4097 {
4098         GObject *win = modest_mail_operation_get_source (mail_op);
4099         const GError *error = modest_mail_operation_get_error (mail_op);
4100
4101         g_return_if_fail (error != NULL);
4102         if (error->message != NULL)             
4103                 g_printerr ("modest: %s\n", error->message);
4104         else
4105                 g_printerr ("modest: unkonw error on send&receive operation");
4106
4107         /* Show error message */
4108 /*      if (modest_mail_operation_get_id (mail_op) == MODEST_MAIL_OPERATION_TYPE_RECEIVE) */
4109 /*              modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL, */
4110 /*                                                      _CS("sfil_ib_unable_to_receive")); */
4111 /*      else  */
4112 /*              modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL, */
4113 /*                                                      _CS("sfil_ib_unable_to_send")); */
4114         g_object_unref (win);
4115 }
4116
4117 static void
4118 open_msg_for_purge_cb (ModestMailOperation *mail_op, 
4119                        TnyHeader *header, 
4120                        gboolean canceled,
4121                        TnyMsg *msg, 
4122                        GError *err,
4123                        gpointer user_data)
4124 {
4125         TnyList *parts;
4126         TnyIterator *iter;
4127         gint pending_purges = 0;
4128         gboolean some_purged = FALSE;
4129         ModestWindow *win = MODEST_WINDOW (user_data);
4130         ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
4131
4132         /* If there was any error */
4133         if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
4134                 modest_window_mgr_unregister_header (mgr, header);
4135                 return;
4136         }
4137
4138         /* Once the message has been retrieved for purging, we check if
4139          * it's all ok for purging */
4140
4141         parts = tny_simple_list_new ();
4142         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
4143         iter = tny_list_create_iterator (parts);
4144
4145         while (!tny_iterator_is_done (iter)) {
4146                 TnyMimePart *part;
4147                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
4148                 if (part && (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part))) {
4149                         if (tny_mime_part_is_purged (part))
4150                                 some_purged = TRUE;
4151                         else
4152                                 pending_purges++;
4153                 }
4154
4155                 if (part)
4156                         g_object_unref (part);
4157
4158                 tny_iterator_next (iter);
4159         }
4160         g_object_unref (iter);
4161         
4162
4163         if (pending_purges>0) {
4164                 gint response;
4165                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),_("mcen_nc_purge_file_text_inbox"));
4166
4167                 if (response == GTK_RESPONSE_OK) {
4168                         modest_platform_information_banner (NULL, NULL, _("mcen_ib_removing_attachment"));
4169                         iter = tny_list_create_iterator (parts);
4170                         while (!tny_iterator_is_done (iter)) {
4171                                 TnyMimePart *part;
4172                                 
4173                                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
4174                                 if (part && (tny_mime_part_is_attachment (part) || TNY_IS_MSG (part)))
4175                                         tny_mime_part_set_purged (part);
4176
4177                                 if (part)
4178                                         g_object_unref (part);
4179
4180                                 tny_iterator_next (iter);
4181                         }
4182                         
4183                         tny_msg_rewrite_cache (msg);
4184                 }
4185      /* } else { */
4186                 /* This string no longer exists, refer to NB#75415 for more info */
4187                 /* modest_platform_information_banner (NULL, NULL, _("mail_ib_attachment_already_purged")); */
4188         }
4189         g_object_unref (iter);
4190
4191         modest_window_mgr_unregister_header (mgr, header);
4192
4193         g_object_unref (parts);
4194 }
4195
4196 static void
4197 modest_ui_actions_on_main_window_remove_attachments (GtkAction *action,
4198                                                      ModestMainWindow *win)
4199 {
4200         GtkWidget *header_view;
4201         TnyList *header_list;
4202         TnyIterator *iter;
4203         TnyHeader *header;
4204         TnyHeaderFlags flags;
4205         ModestWindow *msg_view_window =  NULL;
4206         gboolean found;
4207
4208         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
4209
4210         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
4211                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4212
4213         header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
4214         if (!header_list) {
4215                 g_warning ("%s: no header selected", __FUNCTION__);
4216                 return;
4217         }
4218         
4219         if (tny_list_get_length (header_list) == 1) {
4220                 iter = tny_list_create_iterator (header_list);
4221                 header = TNY_HEADER (tny_iterator_get_current (iter));
4222                 g_object_unref (iter);
4223         } else
4224                 return;
4225         
4226         if (!header || !TNY_IS_HEADER(header)) {
4227                 g_warning ("%s: header is not valid", __FUNCTION__);
4228                 return;
4229         }
4230         
4231         found = modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr (),
4232                                                           header, &msg_view_window);
4233         flags = tny_header_get_flags (header);
4234         if (!(flags & TNY_HEADER_FLAG_CACHED))
4235                 return;
4236         if (found) {
4237                 if (msg_view_window != NULL) 
4238                         modest_msg_view_window_remove_attachments (MODEST_MSG_VIEW_WINDOW (msg_view_window), TRUE);
4239                 else {
4240                         /* do nothing; uid was registered before, so window is probably on it's way */
4241                         g_warning ("debug: header %p has already been registered", header);
4242                 }
4243         } else {
4244                 ModestMailOperation *mail_op = NULL;
4245                 modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header, NULL);
4246                 mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (win),
4247                                                                          modest_ui_actions_get_msgs_full_error_handler,
4248                                                                          NULL, NULL);
4249                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
4250                 modest_mail_operation_get_msg (mail_op, header, open_msg_for_purge_cb, win);
4251                 
4252                 g_object_unref (mail_op);
4253         }
4254         if (header)
4255                 g_object_unref (header);
4256         if (header_list)
4257                 g_object_unref (header_list);
4258 }
4259
4260 /**
4261  * Utility function that transfer messages from both the main window
4262  * and the msg view window when using the "Move to" dialog
4263  */
4264 static void
4265 modest_ui_actions_xfer_messages_from_move_to (TnyFolderStore *dst_folder,
4266                                               ModestWindow *win)
4267 {
4268         TnyList *headers = NULL;
4269         TnyAccount *dst_account = NULL;
4270         const gchar *proto_str = NULL;
4271         gboolean dst_is_pop = FALSE;
4272
4273         if (!TNY_IS_FOLDER (dst_folder)) {
4274                 modest_platform_information_banner (GTK_WIDGET (win),
4275                                                     NULL,
4276                                                     _CS("ckdg_ib_unable_to_move_to_current_location"));
4277                 return;
4278         }
4279
4280         dst_account = tny_folder_get_account (TNY_FOLDER (dst_folder));
4281         proto_str = tny_account_get_proto (dst_account);
4282
4283         /* tinymail will return NULL for local folders it seems */
4284         dst_is_pop = proto_str &&
4285                 (modest_protocol_info_get_transport_store_protocol (proto_str) == 
4286                  MODEST_PROTOCOL_STORE_POP);
4287
4288         g_object_unref (dst_account);
4289
4290         /* Get selected headers */
4291         headers = get_selected_headers (MODEST_WINDOW (win));
4292         if (!headers) {
4293                 g_warning ("%s: no headers selected", __FUNCTION__);
4294                 return;
4295         }
4296
4297
4298         if (dst_is_pop) {
4299                 modest_platform_information_banner (GTK_WIDGET (win),
4300                                                     NULL,
4301                                                     ngettext("mail_in_ui_folder_move_target_error",
4302                                                              "mail_in_ui_folder_move_targets_error",
4303                                                              tny_list_get_length (headers)));
4304                 g_object_unref (headers);
4305                 return;
4306         }
4307
4308         MoveToHelper *helper = g_new0 (MoveToHelper, 1);
4309         helper->banner = modest_platform_animation_banner (GTK_WIDGET (win), NULL,
4310                                                            _CS("ckct_nw_pasting"));
4311         if (helper->banner != NULL)  {
4312                 gtk_window_set_modal (GTK_WINDOW(helper->banner), FALSE);
4313                 gtk_widget_show (GTK_WIDGET(helper->banner));
4314         }
4315
4316         if (MODEST_IS_MAIN_WINDOW (win)) {
4317                 GtkWidget *header_view = 
4318                         modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
4319                                                              MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4320                 helper->reference = get_next_after_selected_headers (MODEST_HEADER_VIEW (header_view));
4321         }
4322
4323         ModestMailOperation *mail_op = 
4324                 modest_mail_operation_new_with_error_handling (G_OBJECT(win),
4325                                                                modest_ui_actions_move_folder_error_handler,
4326                                                                NULL, NULL);
4327         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
4328                                          mail_op);
4329
4330         modest_mail_operation_xfer_msgs (mail_op, 
4331                                          headers,
4332                                          TNY_FOLDER (dst_folder),
4333                                          TRUE,
4334                                          move_to_cb,
4335                                          helper);
4336
4337         g_object_unref (G_OBJECT (mail_op));
4338         g_object_unref (headers);
4339 }
4340
4341 /*
4342  * UI handler for the "Move to" action when invoked from the
4343  * ModestMainWindow
4344  */
4345 static void 
4346 modest_ui_actions_on_main_window_move_to (GtkAction *action, 
4347                                           GtkWidget *folder_view,
4348                                           TnyFolderStore *dst_folder,
4349                                           ModestMainWindow *win)
4350 {
4351         ModestHeaderView *header_view = NULL;
4352         ModestMailOperation *mail_op = NULL;
4353         TnyFolderStore *src_folder;
4354         gboolean online = (tny_device_is_online (modest_runtime_get_device()));
4355
4356         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
4357
4358         /* Get the source folder */
4359         src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4360
4361         /* Get header view */
4362         header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
4363
4364         /* Get folder or messages to transfer */
4365         if (gtk_widget_is_focus (folder_view)) {
4366                 GtkTreeSelection *sel;
4367                 gboolean do_xfer = TRUE;
4368
4369                 /* Allow only to transfer folders to the local root folder */
4370                 if (TNY_IS_ACCOUNT (dst_folder) && 
4371                     !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (dst_folder)) {
4372                         do_xfer = FALSE;
4373                 } else if (!TNY_IS_FOLDER (src_folder)) {
4374                         g_warning ("%s: src_folder is not a TnyFolder.\n", __FUNCTION__);
4375                         do_xfer = FALSE;
4376                 } else if (!online && modest_platform_is_network_folderstore(src_folder)) {
4377                         guint num_headers = tny_folder_get_all_count(TNY_FOLDER (src_folder));
4378                         TnyAccount *account = tny_folder_get_account (TNY_FOLDER (src_folder));
4379                         if (!connect_to_get_msg(MODEST_WINDOW (win), num_headers, account))
4380                                 do_xfer = FALSE;
4381                         g_object_unref (account);
4382                 }
4383
4384                 if (do_xfer) {
4385                         MoveToHelper *helper = g_new0 (MoveToHelper, 1);
4386                         helper->banner = modest_platform_animation_banner (GTK_WIDGET (win), NULL,
4387                                                                            _CS("ckct_nw_pasting"));
4388                         if (helper->banner != NULL)  {
4389                                 gtk_window_set_modal (GTK_WINDOW(helper->banner), FALSE);
4390                                 gtk_widget_show (GTK_WIDGET(helper->banner));
4391                         }
4392                         /* Clean folder on header view before moving it */
4393                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_view));
4394                         gtk_tree_selection_unselect_all (sel);
4395
4396                         /* Let gtk events run. We need that the folder
4397                            view frees its reference to the source
4398                            folder *before* issuing the mail operation
4399                            so we need the signal handler of selection
4400                            changed to happen before the mail
4401                            operation */
4402                         while (gtk_events_pending ())
4403                                 gtk_main_iteration ();
4404
4405                         mail_op =
4406                           modest_mail_operation_new_with_error_handling (G_OBJECT(win),
4407                                                                          modest_ui_actions_move_folder_error_handler,
4408                                                                          src_folder, NULL);
4409                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
4410                                                          mail_op);
4411
4412                         /* Select *after* the changes */
4413                         /* TODO: this function hangs UI after transfer */ 
4414 /*                      modest_folder_view_select_folder (MODEST_FOLDER_VIEW(folder_view), */
4415 /*                                                        TNY_FOLDER (src_folder), TRUE); */
4416                         
4417                         modest_mail_operation_xfer_folder (mail_op,
4418                                                            TNY_FOLDER (src_folder),
4419                                                            dst_folder,
4420                                                            TRUE, 
4421                                                            move_to_cb, 
4422                                                            helper);
4423                         /* Unref mail operation */
4424                         g_object_unref (G_OBJECT (mail_op));
4425                 }
4426         } else if (gtk_widget_is_focus (GTK_WIDGET(header_view))) {
4427                 gboolean do_xfer = TRUE;
4428                 /* Ask for confirmation if the source folder is remote and we're not connected */
4429                 if (!online && modest_platform_is_network_folderstore(src_folder)) {
4430                         TnyList *headers = modest_header_view_get_selected_headers(header_view);
4431                         if (!msgs_already_deleted_from_server(headers, src_folder)) {
4432                                 guint num_headers = tny_list_get_length(headers);
4433                                 TnyAccount *account = get_account_from_header_list (headers);
4434                                 if (!connect_to_get_msg(MODEST_WINDOW (win), num_headers, account))
4435                                         do_xfer = FALSE;
4436                                 g_object_unref (account);
4437                         }
4438                         g_object_unref(headers);
4439                 }
4440                 if (do_xfer) /* Transfer messages */
4441                         modest_ui_actions_xfer_messages_from_move_to (dst_folder, MODEST_WINDOW (win));
4442         }
4443
4444     if (src_folder)
4445         g_object_unref (src_folder);
4446 }
4447
4448
4449 /*
4450  * UI handler for the "Move to" action when invoked from the
4451  * ModestMsgViewWindow
4452  */
4453 static void 
4454 modest_ui_actions_on_msg_view_window_move_to (GtkAction *action, 
4455                                               TnyFolderStore *dst_folder,
4456                                               ModestMsgViewWindow *win)
4457 {
4458         TnyHeader *header = NULL;
4459         TnyFolder *src_folder = NULL;
4460         TnyAccount *account = NULL;
4461         gboolean do_xfer = FALSE;
4462
4463         /* Create header list */
4464         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));              
4465         src_folder = TNY_FOLDER (tny_header_get_folder(header));
4466         g_object_unref (header);
4467
4468         account = tny_folder_get_account (src_folder);
4469         if (!modest_platform_is_network_folderstore(TNY_FOLDER_STORE(src_folder))) {
4470                 /* Transfer if the source folder is local */
4471                 do_xfer = TRUE;
4472         } else if (remote_folder_is_pop(TNY_FOLDER_STORE(src_folder))) {
4473                 /* Transfer if the source folder is POP (as it means
4474                  * that the message is already downloaded) */
4475                 do_xfer = TRUE;
4476         } else if (connect_to_get_msg(MODEST_WINDOW(win), 1, account)) {
4477                 /* Transfer after asking confirmation */
4478                 do_xfer = TRUE;
4479         }
4480
4481         if (do_xfer) {
4482                 modest_ui_actions_xfer_messages_from_move_to (dst_folder, MODEST_WINDOW (win));
4483         }
4484         g_object_unref (account);
4485         g_object_unref (src_folder);
4486 }
4487
4488 void 
4489 modest_ui_actions_on_move_to (GtkAction *action, 
4490                               ModestWindow *win)
4491 {
4492         GtkWidget *dialog = NULL, *folder_view = NULL, *tree_view = NULL;
4493         gint result = 0;
4494         TnyFolderStore *dst_folder = NULL;
4495         ModestMainWindow *main_window;
4496
4497         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win) ||
4498                           MODEST_IS_MSG_VIEW_WINDOW (win));
4499
4500         /* Get the main window if exists */
4501         if (MODEST_IS_MAIN_WINDOW (win))
4502                 main_window = MODEST_MAIN_WINDOW (win);
4503         else
4504                 main_window = 
4505                         MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
4506                                                                                FALSE)); /* don't create */
4507
4508         /* Get the folder view widget if exists */
4509         if (main_window)
4510                 folder_view = modest_main_window_get_child_widget (main_window,
4511                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
4512         else
4513                 folder_view = NULL;
4514
4515         /* Create and run the dialog */
4516         dialog = create_move_to_dialog (GTK_WINDOW (win), folder_view, &tree_view);
4517         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (tree_view));
4518         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
4519         result = gtk_dialog_run (GTK_DIALOG(dialog));
4520         g_object_ref (tree_view);
4521         gtk_widget_destroy (dialog);
4522
4523         if (result != GTK_RESPONSE_ACCEPT)
4524                 return;
4525
4526         dst_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
4527         /* Do window specific stuff */
4528         if (MODEST_IS_MAIN_WINDOW (win)) {
4529                 modest_ui_actions_on_main_window_move_to (action,
4530                                                           folder_view,
4531                                                           dst_folder,
4532                                                           MODEST_MAIN_WINDOW (win));
4533         } else {
4534                 modest_ui_actions_on_msg_view_window_move_to (action,
4535                                                               dst_folder,
4536                                                               MODEST_MSG_VIEW_WINDOW (win));
4537         }
4538
4539         if (dst_folder)
4540                 g_object_unref (dst_folder);
4541 }
4542
4543 /*
4544  * Calls #HeadersFunc for each header already selected in the main
4545  * window or the message currently being shown in the msg view window
4546  */
4547 static void
4548 do_headers_action (ModestWindow *win, 
4549                    HeadersFunc func,
4550                    gpointer user_data)
4551 {
4552         TnyList *headers_list = NULL;
4553         TnyIterator *iter = NULL;
4554         TnyHeader *header = NULL;
4555         TnyFolder *folder = NULL;
4556
4557         /* Get headers */
4558         headers_list = get_selected_headers (win);
4559         if (!headers_list)
4560                 return;
4561
4562         /* Get the folder */
4563         iter = tny_list_create_iterator (headers_list);
4564         header = TNY_HEADER (tny_iterator_get_current (iter));
4565         if (header) {
4566                 folder = tny_header_get_folder (header);
4567                 g_object_unref (header);
4568         }
4569
4570         /* Call the function for each header */
4571         while (!tny_iterator_is_done (iter)) {
4572                 header = TNY_HEADER (tny_iterator_get_current (iter));
4573                 func (header, win, user_data);
4574                 g_object_unref (header);
4575                 tny_iterator_next (iter);
4576         }
4577
4578         /* Trick: do a poke status in order to speed up the signaling
4579            of observers */
4580         tny_folder_poke_status (folder);
4581
4582         /* Frees */
4583         g_object_unref (folder);
4584         g_object_unref (iter);
4585         g_object_unref (headers_list);
4586 }
4587
4588 void 
4589 modest_ui_actions_view_attachment (GtkAction *action,
4590                                    ModestWindow *window)
4591 {
4592         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
4593                 modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (window), NULL);
4594         } else {
4595                 /* not supported window for this action */
4596                 g_return_if_reached ();
4597         }
4598 }
4599
4600 void
4601 modest_ui_actions_save_attachments (GtkAction *action,
4602                                     ModestWindow *window)
4603 {
4604         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
4605                 modest_msg_view_window_save_attachments (MODEST_MSG_VIEW_WINDOW (window), NULL);
4606         } else {
4607                 /* not supported window for this action */
4608                 g_return_if_reached ();
4609         }
4610 }
4611
4612 void
4613 modest_ui_actions_remove_attachments (GtkAction *action,
4614                                       ModestWindow *window)
4615 {
4616         if (MODEST_IS_MAIN_WINDOW (window)) {
4617                 modest_ui_actions_on_main_window_remove_attachments (action, MODEST_MAIN_WINDOW (window));
4618         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
4619                 modest_msg_view_window_remove_attachments (MODEST_MSG_VIEW_WINDOW (window), FALSE);
4620         } else {
4621                 /* not supported window for this action */
4622                 g_return_if_reached ();
4623         }
4624 }
4625
4626 void 
4627 modest_ui_actions_on_settings (GtkAction *action, 
4628                                ModestWindow *win)
4629 {
4630         GtkWidget *dialog;
4631
4632         dialog = modest_platform_get_global_settings_dialog ();
4633         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (win));
4634         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
4635         gtk_widget_show_all (dialog);
4636
4637         gtk_dialog_run (GTK_DIALOG (dialog));
4638
4639         gtk_widget_destroy (dialog);
4640 }
4641
4642 void 
4643 modest_ui_actions_on_help (GtkAction *action, 
4644                            GtkWindow *win)
4645 {
4646         const gchar *help_id;
4647
4648         g_return_if_fail (action);
4649         g_return_if_fail (win && GTK_IS_WINDOW(win));
4650         
4651         help_id = modest_window_mgr_get_help_id (modest_runtime_get_window_mgr(), win);
4652         
4653         if (help_id)
4654                 modest_platform_show_help (GTK_WINDOW (win), help_id);
4655         else
4656                 g_warning ("%s: no help for window %p", __FUNCTION__, win);
4657 }
4658
4659 static void
4660 retrieve_msg_contents_performer (gboolean canceled, 
4661                                  GError *err,
4662                                  GtkWindow *parent_window, 
4663                                  TnyAccount *account, 
4664                                  gpointer user_data)
4665 {
4666         ModestMailOperation *mail_op;
4667         TnyList *headers = TNY_LIST (user_data);
4668
4669         if (err || canceled) {
4670                 /* Show an error ? */
4671                 goto out;
4672         }
4673
4674         /* Create mail operation */
4675         mail_op = modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
4676                                                                  modest_ui_actions_get_msgs_full_error_handler, 
4677                                                                  NULL, NULL);
4678         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
4679         modest_mail_operation_get_msgs_full (mail_op, headers, NULL, NULL, NULL);
4680
4681         /* Frees */
4682         g_object_unref (mail_op);
4683  out:
4684         g_object_unref (headers);
4685         g_object_unref (account);
4686 }
4687
4688 void 
4689 modest_ui_actions_on_retrieve_msg_contents (GtkAction *action,
4690                                             ModestWindow *window)
4691 {
4692         TnyList *headers = NULL;
4693         TnyAccount *account = NULL;
4694         TnyIterator *iter = NULL;
4695         TnyHeader *header = NULL;
4696         TnyFolder *folder = NULL;
4697
4698         /* Get headers */
4699         headers = get_selected_headers (window);
4700         if (!headers)
4701                 return;
4702
4703         /* Pick the account */
4704         iter = tny_list_create_iterator (headers);
4705         header = TNY_HEADER (tny_iterator_get_current (iter));
4706         folder = tny_header_get_folder (header);
4707         account = tny_folder_get_account (folder);
4708         g_object_unref (folder);
4709         g_object_unref (header);
4710         g_object_unref (iter);
4711
4712         /* Connect and perform the message retrieval */
4713         modest_platform_connect_and_perform ((GtkWindow *) window, 
4714                                              g_object_ref (account), 
4715                                              retrieve_msg_contents_performer, 
4716                                              g_object_ref (headers));
4717
4718         /* Frees */
4719         g_object_unref (account);
4720         g_object_unref (headers);
4721 }
4722
4723 void
4724 modest_ui_actions_on_email_menu_activated (GtkAction *action,
4725                                           ModestWindow *window)
4726 {
4727         g_return_if_fail (MODEST_IS_WINDOW (window));
4728         
4729         /* Update dimmed */     
4730         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4731 }
4732
4733 void
4734 modest_ui_actions_on_edit_menu_activated (GtkAction *action,
4735                                           ModestWindow *window)
4736 {
4737         g_return_if_fail (MODEST_IS_WINDOW (window));
4738
4739         /* Update dimmed */     
4740         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4741 }
4742
4743 void
4744 modest_ui_actions_on_view_menu_activated (GtkAction *action,
4745                                           ModestWindow *window)
4746 {
4747         g_return_if_fail (MODEST_IS_WINDOW (window));
4748
4749         /* Update dimmed */     
4750         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4751 }
4752
4753 void
4754 modest_ui_actions_on_format_menu_activated (GtkAction *action,
4755                                             ModestWindow *window)
4756 {
4757         g_return_if_fail (MODEST_IS_WINDOW (window));
4758
4759         /* Update dimmed */     
4760         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4761 }
4762
4763 void
4764 modest_ui_actions_on_tools_menu_activated (GtkAction *action,
4765                                           ModestWindow *window)
4766 {
4767         g_return_if_fail (MODEST_IS_WINDOW (window));
4768
4769         /* Update dimmed */     
4770         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4771 }
4772
4773 void
4774 modest_ui_actions_on_attachment_menu_activated (GtkAction *action,
4775                                           ModestWindow *window)
4776 {
4777         g_return_if_fail (MODEST_IS_WINDOW (window));
4778
4779         /* Update dimmed */     
4780         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4781 }
4782
4783 void
4784 modest_ui_actions_on_toolbar_csm_menu_activated (GtkAction *action,
4785                                                  ModestWindow *window)
4786 {
4787         g_return_if_fail (MODEST_IS_WINDOW (window));
4788
4789         /* Update dimmed */     
4790         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4791 }
4792
4793 void
4794 modest_ui_actions_on_folder_view_csm_menu_activated (GtkAction *action,
4795                                                      ModestWindow *window)
4796 {
4797         g_return_if_fail (MODEST_IS_WINDOW (window));
4798
4799         /* Update dimmed */     
4800         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4801 }
4802
4803 void
4804 modest_ui_actions_on_header_view_csm_menu_activated (GtkAction *action,
4805                                                      ModestWindow *window)
4806 {
4807         g_return_if_fail (MODEST_IS_WINDOW (window));
4808
4809         /* Update dimmed */     
4810         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
4811 }
4812
4813 void
4814 modest_ui_actions_check_toolbar_dimming_rules (ModestWindow *window)
4815 {
4816         g_return_if_fail (MODEST_IS_WINDOW (window));
4817
4818         /* Update dimmed */     
4819         modest_window_check_dimming_rules_group (window, "ModestToolbarDimmingRules");  
4820 }
4821
4822 void
4823 modest_ui_actions_on_search_messages (GtkAction *action, ModestWindow *window)
4824 {
4825         g_return_if_fail (MODEST_IS_WINDOW (window));
4826
4827         modest_platform_show_search_messages (GTK_WINDOW (window));
4828 }
4829
4830 void     
4831 modest_ui_actions_on_open_addressbook (GtkAction *action, ModestWindow *win)
4832 {
4833         g_return_if_fail (MODEST_IS_WINDOW (win));
4834         modest_platform_show_addressbook (GTK_WINDOW (win));
4835 }
4836
4837
4838 void
4839 modest_ui_actions_on_toggle_find_in_page (GtkToggleAction *action,
4840                                           ModestWindow *window)
4841 {
4842         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
4843
4844         modest_msg_edit_window_toggle_find_toolbar (MODEST_MSG_EDIT_WINDOW (window), gtk_toggle_action_get_active (action));
4845 }
4846
4847 static void 
4848 on_send_receive_finished (ModestMailOperation  *mail_op, 
4849                            gpointer user_data)
4850 {
4851         GtkWidget *header_view, *folder_view;
4852         TnyFolderStore *folder_store;
4853         ModestMainWindow *main_win = MODEST_MAIN_WINDOW (user_data);
4854
4855         /* Set send/receive operation finished */       
4856         modest_main_window_notify_send_receive_completed (main_win);
4857
4858         /* Don't refresh the current folder if there were any errors */
4859         if (modest_mail_operation_get_status (mail_op) !=
4860             MODEST_MAIL_OPERATION_STATUS_SUCCESS)
4861                 return;
4862         
4863         /* Refresh the current folder if we're viewing a window. We do
4864            this because the user won't be able to see the new mails in
4865            the selected folder after a Send&Receive because it only
4866            performs a poke_status, i.e, only the number of read/unread
4867            messages is updated, but the new headers are not
4868            downloaded */
4869         folder_view = modest_main_window_get_child_widget (main_win, 
4870                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
4871         if (!folder_view)
4872                 return;
4873
4874         folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4875         
4876         /* Do not need to refresh INBOX again because the
4877            update_account does it always automatically */
4878         if (folder_store && TNY_IS_FOLDER (folder_store) && 
4879             tny_folder_get_folder_type (TNY_FOLDER (folder_store)) != TNY_FOLDER_TYPE_INBOX) {
4880
4881                 header_view = modest_main_window_get_child_widget (main_win,
4882                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4883                 
4884                 /* We do not need to set the contents style
4885                    because it hasn't changed. We also do not
4886                    need to save the widget status. Just force
4887                    a refresh */
4888                 modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
4889                                                TNY_FOLDER (folder_store),
4890                                                folder_refreshed_cb,
4891                                                main_win);
4892         }
4893         
4894         if (folder_store)
4895                 g_object_unref (folder_store);
4896 }
4897
4898
4899 void 
4900 modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self, 
4901                                                 TnyHeader *header, 
4902                                                 TnyMsg *msg, 
4903                                                 GError *err, 
4904                                                 gpointer user_data)
4905 {
4906         const gchar* server_name = NULL;
4907         TnyTransportAccount *server_account;
4908         gchar *message = NULL;
4909
4910         /* Don't show anything if the user cancelled something */
4911         if (err->code == TNY_TRANSPORT_ACCOUNT_ERROR_SEND_USER_CANCEL)
4912                 return;
4913
4914         /* Get the server name: */
4915         server_account = 
4916                 TNY_TRANSPORT_ACCOUNT (tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE (self)));
4917         if (server_account) {
4918                 server_name = tny_account_get_hostname (TNY_ACCOUNT (server_account));
4919                         
4920                 g_object_unref (server_account);
4921                 server_account = NULL;
4922         }
4923         
4924         g_return_if_fail (server_name);
4925
4926         /* Show the appropriate message text for the GError: */
4927         switch (err->code) {
4928         case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_HOST_LOOKUP_FAILED:
4929                 message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
4930                 break;
4931         case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_SERVICE_UNAVAILABLE:
4932                 message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
4933                 break;
4934         case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_AUTHENTICATION_NOT_SUPPORTED:
4935                 message = g_strdup_printf (_("emev_ni_ui_smtp_authentication_fail_error"), server_name);
4936                 break;
4937         case TNY_TRANSPORT_ACCOUNT_ERROR_SEND:
4938                 message = g_strdup (_("emev_ib_ui_smtp_send_error"));
4939                 break;
4940         default:
4941                 g_return_if_reached ();
4942         }
4943         
4944         /* TODO if the username or the password where not defined we
4945            should show the Accounts Settings dialog or the Connection
4946            specific SMTP server window */
4947
4948         modest_platform_run_information_dialog (NULL, message);
4949         g_free (message);
4950 }
4951
4952 void
4953 modest_ui_actions_on_send_queue_status_changed (ModestTnySendQueue *send_queue,
4954                                                 gchar *msg_id, 
4955                                                 guint status,
4956                                                 gpointer user_data)
4957 {
4958         ModestMainWindow *main_window = NULL;
4959         ModestWindowMgr *mgr = NULL;
4960         GtkWidget *folder_view = NULL, *header_view = NULL;
4961         TnyFolderStore *selected_folder = NULL;
4962         TnyFolderType folder_type;
4963
4964         mgr = modest_runtime_get_window_mgr ();
4965         main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (mgr,
4966                                                                              FALSE));/* don't create */
4967         if (!main_window)
4968                 return;
4969
4970         /* Check if selected folder is OUTBOX */
4971         folder_view = modest_main_window_get_child_widget (main_window,
4972                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
4973         header_view = modest_main_window_get_child_widget (main_window,
4974                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
4975
4976         selected_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
4977         if (!TNY_IS_FOLDER (selected_folder)) 
4978                 goto frees;
4979
4980         /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
4981 #if GTK_CHECK_VERSION(2, 8, 0) 
4982         folder_type = modest_tny_folder_guess_folder_type (TNY_FOLDER (selected_folder)); 
4983         if (folder_type ==  TNY_FOLDER_TYPE_OUTBOX) {           
4984                 GtkTreeViewColumn *tree_column;
4985
4986                 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (header_view), 
4987                                                         TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN);
4988                 gtk_tree_view_column_queue_resize (tree_column);
4989         }
4990 #else
4991         gtk_widget_queue_draw (header_view);
4992 #endif          
4993         
4994         /* Free */
4995  frees:
4996         if (selected_folder != NULL)
4997                 g_object_unref (selected_folder);
4998 }
4999
5000 void 
5001 modest_ui_actions_on_account_connection_error (GtkWindow *parent_window,
5002                                                TnyAccount *account)
5003 {
5004         ModestTransportStoreProtocol proto;
5005         const gchar *proto_name;
5006         gchar *error_note = NULL;
5007         
5008         proto_name = tny_account_get_proto (account);
5009         proto = modest_protocol_info_get_transport_store_protocol (proto_name);
5010         
5011         switch (proto) {
5012         case MODEST_PROTOCOL_STORE_POP:
5013                 error_note = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), 
5014                                               tny_account_get_hostname (account));
5015                 break;
5016         case MODEST_PROTOCOL_STORE_IMAP:
5017                 error_note = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), 
5018                                               tny_account_get_hostname (account));
5019                 break;
5020         case MODEST_PROTOCOL_STORE_MAILDIR:
5021         case MODEST_PROTOCOL_STORE_MBOX:
5022                 error_note = g_strdup (_("emev_nc_mailbox_notavailable"));
5023                 break;
5024         default:
5025                 g_warning ("%s: This should not be reached", __FUNCTION__);
5026         }
5027
5028         if (error_note) {
5029                 modest_platform_run_information_dialog (parent_window, error_note);
5030                 g_free (error_note);
5031         }
5032 }