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