fa1d7b3e1d1ec6ac03b1dd180a641d1fa04390d2
[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
45 #include "modest-tny-platform-factory.h"
46 #include "modest-platform.h"
47
48 #ifdef MODEST_PLATFORM_MAEMO
49 #include "maemo/modest-osso-state-saving.h"
50 #endif /* MODEST_PLATFORM_MAEMO */
51
52 #include "widgets/modest-ui-constants.h"
53 #include <widgets/modest-main-window.h>
54 #include <widgets/modest-msg-view-window.h>
55 #include <widgets/modest-account-view-window.h>
56 #include <widgets/modest-details-dialog.h>
57 #include <widgets/modest-attachments-view.h>
58 #include "widgets/modest-folder-view.h"
59 #include "widgets/modest-global-settings-dialog.h"
60 #include "modest-connection-specific-smtp-window.h"
61 #include "modest-account-mgr-helpers.h"
62 #include "modest-mail-operation.h"
63 #include "modest-text-utils.h"
64
65 #ifdef MODEST_HAVE_EASYSETUP
66 #include "easysetup/modest-easysetup-wizard.h"
67 #endif /* MODEST_HAVE_EASYSETUP */
68
69 #include <modest-widget-memory.h>
70 #include <tny-error.h>
71 #include <tny-simple-list.h>
72 #include <tny-msg-view.h>
73 #include <tny-device.h>
74 #include <tny-merge-folder.h>
75
76 #include <gtkhtml/gtkhtml.h>
77
78 typedef struct _GetMsgAsyncHelper {     
79         ModestWindow *window;
80         ModestMailOperation *mail_op;
81         TnyIterator *iter;
82         guint num_ops;
83         GFunc func;     
84         gpointer user_data;
85 } GetMsgAsyncHelper;
86
87 typedef enum _ReplyForwardAction {
88         ACTION_REPLY,
89         ACTION_REPLY_TO_ALL,
90         ACTION_FORWARD
91 } ReplyForwardAction;
92
93 typedef struct _ReplyForwardHelper {
94         guint reply_forward_type;
95         ReplyForwardAction action;
96         gchar *account_name;
97         GtkWidget *parent_window;
98 } ReplyForwardHelper;
99
100
101 /*
102  * The do_headers_action uses this kind of functions to perform some
103  * action to each member of a list of headers
104  */
105 typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data);
106
107 static void
108 do_headers_action (ModestWindow *win, 
109                    HeadersFunc func,
110                    gpointer user_data);
111
112
113 static void     open_msg_cb            (ModestMailOperation *mail_op, 
114                                         TnyHeader *header, 
115                                         TnyMsg *msg,
116                                         gpointer user_data);
117
118 static void     reply_forward_cb       (ModestMailOperation *mail_op, 
119                                         TnyHeader *header, 
120                                         TnyMsg *msg,
121                                         gpointer user_data);
122
123 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
124
125
126 static void     _on_send_receive_progress_changed (ModestMailOperation  *mail_op, 
127                                                    ModestMailOperationState *state,
128                                                    gpointer user_data);
129
130
131
132 static void
133 run_account_setup_wizard (ModestWindow *win)
134 {
135         ModestEasysetupWizardDialog *wizard;
136
137         g_return_if_fail (MODEST_IS_WINDOW(win));
138         
139         wizard = modest_easysetup_wizard_dialog_new ();
140         gtk_window_set_transient_for (GTK_WINDOW (wizard), GTK_WINDOW (win));
141         gtk_dialog_run (GTK_DIALOG (wizard));
142         gtk_widget_destroy (GTK_WIDGET (wizard));
143 }
144
145
146 void   
147 modest_ui_actions_on_about (GtkAction *action, ModestWindow *win)
148 {
149         GtkWidget *about;
150         const gchar *authors[] = {
151                 "Dirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>",
152                 NULL
153         };
154         about = gtk_about_dialog_new ();
155         gtk_about_dialog_set_name (GTK_ABOUT_DIALOG(about), PACKAGE_NAME);
156         gtk_about_dialog_set_version (GTK_ABOUT_DIALOG(about),PACKAGE_VERSION);
157         gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG(about),
158                                         _("Copyright (c) 2006, Nokia Corporation\n"
159                                           "All rights reserved."));
160         gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(about),
161                                        _("a modest e-mail client\n\n"
162                                          "design and implementation: Dirk-Jan C. Binnema\n"
163                                          "contributions from the fine people at KC and Ig\n"
164                                          "uses the tinymail email framework written by Philip van Hoof"));
165         gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(about), authors);
166         gtk_about_dialog_set_website (GTK_ABOUT_DIALOG(about), "http://modest.garage.maemo.org");
167         
168         gtk_dialog_run (GTK_DIALOG (about));
169         gtk_widget_destroy(about);
170 }
171
172 /*
173  * Gets the list of currently selected messages. If the win is the
174  * main window, then it returns a newly allocated list of the headers
175  * selected in the header view. If win is the msg view window, then
176  * the value returned is a list with just a single header.
177  *
178  * The caller of this funcion must free the list.
179  */
180 static TnyList *
181 get_selected_headers (ModestWindow *win)
182 {
183         if (MODEST_IS_MAIN_WINDOW(win)) {
184                 GtkWidget *header_view;         
185                 
186                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
187                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
188                 return modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
189                 
190         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
191                 /* for MsgViewWindows, we simply return a list with one element */
192                 TnyHeader *header;
193                 TnyList *list = NULL;
194                 
195                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
196                 if (header != NULL) {
197                         list = tny_simple_list_new ();
198                         tny_list_prepend (list, G_OBJECT(header));
199                         g_object_unref (G_OBJECT(header));
200                 }
201
202                 return list;
203
204         } else
205                 return NULL;
206 }
207
208 static void
209 headers_action_mark_as_read (TnyHeader *header,
210                              ModestWindow *win,
211                              gpointer user_data)
212 {
213         TnyHeaderFlags flags;
214
215         g_return_if_fail (TNY_IS_HEADER(header));
216
217         flags = tny_header_get_flags (header);
218         if (flags & TNY_HEADER_FLAG_SEEN) return;
219         tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
220 }
221
222 static void
223 headers_action_mark_as_unread (TnyHeader *header,
224                                ModestWindow *win,
225                                gpointer user_data)
226 {
227         TnyHeaderFlags flags;
228
229         g_return_if_fail (TNY_IS_HEADER(header));
230
231         flags = tny_header_get_flags (header);
232         if (flags & TNY_HEADER_FLAG_SEEN)  {
233                 tny_header_unset_flags (header, TNY_HEADER_FLAG_SEEN);
234         }
235 }
236
237
238 static void
239 headers_action_delete (TnyHeader *header,
240                        ModestWindow *win,
241                        gpointer user_data)
242 {
243         ModestMailOperation *mail_op = NULL;
244
245         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_DELETE, G_OBJECT(win));
246         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
247                                          mail_op);
248         
249         /* Always delete. TODO: Move to trash still not supported */
250         modest_mail_operation_remove_msg (mail_op, header, FALSE);
251         g_object_unref (G_OBJECT (mail_op));
252 }
253
254 void
255 modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
256 {
257         TnyList *header_list = NULL;
258         TnyIterator *iter = NULL;
259         TnyHeader *header = NULL;
260         gchar *message = NULL;
261         gchar *desc = NULL;
262         gint response;
263         gboolean found;
264         ModestWindowMgr *mgr;
265         GtkWidget *header_view;
266
267         g_return_if_fail (MODEST_IS_WINDOW(win));
268
269         /* Check first if the header view has the focus */
270         if (MODEST_IS_MAIN_WINDOW (win)) {
271                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
272                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
273                 if (!gtk_widget_is_focus (header_view))
274                         return;
275         }
276
277         header_list = get_selected_headers (win);
278         if (!header_list) return;
279
280         /* Check if any of the headers is already opened */
281         iter = tny_list_create_iterator (header_list);
282         found = FALSE;
283         mgr = modest_runtime_get_window_mgr ();
284         while (!tny_iterator_is_done (iter) && !found) {
285                 header = TNY_HEADER (tny_iterator_get_current (iter));
286                 if (modest_window_mgr_find_window_by_header (mgr, header))
287                         found = TRUE;
288                 g_object_unref (header);
289                 tny_iterator_next (iter);
290         }
291         g_object_unref (iter);
292
293         if (found) {
294                 gchar *num, *msg;
295
296                 num = g_strdup_printf ("%d", tny_list_get_length (header_list));
297                 msg = g_strdup_printf (_("mcen_nc_unable_to_delete_n_messages"), num);
298
299                 modest_platform_run_information_dialog (GTK_WINDOW (win), (const gchar *) msg);
300
301                 g_free (msg);
302                 g_free (num);
303                 g_object_unref (header_list);
304                 return;
305         }
306
307         /* Select message */
308         if (tny_list_get_length(header_list) == 1) {
309                 iter = tny_list_create_iterator (header_list);
310                 header = TNY_HEADER (tny_iterator_get_current (iter));
311                 desc = g_strdup_printf ("%s", tny_header_get_subject (header)); 
312                 g_object_unref (header);
313                 g_object_unref (iter);
314         }
315         message = g_strdup_printf(ngettext("emev_nc_delete_message", "emev_nc_delete_messages", 
316                                            tny_list_get_length(header_list)), desc);
317
318         /* Confirmation dialog */               
319         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
320                                                             message);
321         
322
323         if (response == GTK_RESPONSE_OK) {
324                 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
325                         gboolean ret_value;
326                         g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
327                         return;
328                 }
329                 
330                 /* Remove each header */
331                 do_headers_action (win, headers_action_delete, NULL);
332
333                 if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
334                         gtk_widget_destroy (GTK_WIDGET(win));
335                 } 
336         }
337
338         /* free */
339         g_free(message);
340         g_free(desc);
341         g_object_unref (header_list);
342 }
343
344
345 void
346 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
347 {
348 #ifdef MODEST_PLATFORM_MAEMO
349         modest_osso_save_state();
350 #endif /* MODEST_PLATFORM_MAEMO */
351
352         /* FIXME: we need to cancel all actions/threads here,
353          so we really quit */
354
355         gtk_main_quit ();
356 }
357
358 void
359 modest_ui_actions_on_close_window (GtkAction *action, ModestWindow *win)
360 {
361         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
362                 gtk_widget_destroy (GTK_WIDGET (win));
363         } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
364                 gboolean ret_value;
365                 g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
366         } else if (MODEST_IS_WINDOW (win)) {
367                 gtk_widget_destroy (GTK_WIDGET (win));
368         } else {
369                 g_return_if_reached ();
370         }
371 }
372
373 void
374 modest_ui_actions_on_add_to_contacts (GtkAction *action, ModestWindow *win)
375 {
376         GtkClipboard *clipboard = NULL;
377         gchar *selection = NULL;
378
379         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
380         selection = gtk_clipboard_wait_for_text (clipboard);
381
382         /* Question: why is the clipboard being used here? 
383          * It doesn't really make a lot of sense. */
384
385         if (selection)
386         {
387                 modest_address_book_add_address (selection);
388                 g_free (selection);
389         }
390 }
391
392 void
393 modest_ui_actions_on_accounts (GtkAction *action, ModestWindow *win)
394 {
395         /* This is currently only implemented for Maemo */
396 #ifdef MODEST_PLATFORM_MAEMO /* Defined in config.h */
397         if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
398                 run_account_setup_wizard (win);
399                 return;
400         } else  {
401                 /* Show the list of accounts: */
402                 GtkDialog *account_win = GTK_DIALOG(modest_account_view_window_new ());
403                 gtk_window_set_transient_for (GTK_WINDOW (account_win), GTK_WINDOW(win));
404                 gtk_dialog_run (account_win);
405                 gtk_widget_destroy (GTK_WIDGET(account_win));
406         }
407 #else
408         GtkWidget *dialog, *label;
409         
410         /* Create the widgets */
411         
412         dialog = gtk_dialog_new_with_buttons ("Message",
413                                               GTK_WINDOW(win),
414                                               GTK_DIALOG_DESTROY_WITH_PARENT,
415                                               GTK_STOCK_OK,
416                                               GTK_RESPONSE_NONE,
417                                               NULL);
418         label = gtk_label_new ("Hello World!");
419         
420         /* Ensure that the dialog box is destroyed when the user responds. */
421         
422         g_signal_connect_swapped (dialog, "response", 
423                                   G_CALLBACK (gtk_widget_destroy),
424                                   dialog);
425         
426         /* Add the label, and show everything we've added to the dialog. */
427         
428         gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
429                            label);
430         gtk_widget_show_all (dialog);
431 #endif /* MODEST_PLATFORM_MAEMO */
432 }
433
434 static void
435 on_smtp_servers_window_hide (GtkWindow* window, gpointer user_data)
436 {
437         ModestWindow *main_window = MODEST_WINDOW (user_data);
438         
439         /* Save any changes. */
440         modest_connection_specific_smtp_window_save_server_accounts (
441                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (window), 
442                         modest_window_get_active_account (main_window));
443         gtk_widget_destroy (GTK_WIDGET (window));
444 }
445
446
447
448 void
449 modest_ui_actions_on_smtp_servers (GtkAction *action, ModestWindow *win)
450 {
451         /* This is currently only implemented for Maemo,
452          * because it requires an API (libconic) to detect different connection 
453          * possiblities.
454          */
455 #ifdef MODEST_PLATFORM_MAEMO /* Defined in config.h */
456         
457         /* Create the window if necessary: */
458         const gchar *active_account_name = modest_window_get_active_account (win);
459         
460         /* TODO: Dim the menu item (not in the UI spec)? or show a warning,
461          * or show the default account?
462          * If we show the default account then the account name should be shown in 
463          * the window when we show it. */
464         if (!active_account_name) {
465                 g_warning ("%s: No account is active.", __FUNCTION__);
466                 return;
467         }
468                 
469         GtkWidget *specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
470         modest_connection_specific_smtp_window_fill_with_connections (
471                 MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), 
472                 modest_runtime_get_account_mgr(), 
473                 active_account_name);
474
475         /* Show the window: */  
476         gtk_window_set_transient_for (GTK_WINDOW (specific_window), GTK_WINDOW (win));
477         gtk_window_set_modal (GTK_WINDOW (specific_window), TRUE);
478         gtk_widget_show (specific_window);
479     
480         /* Save changes when the window is hidden: */
481         g_signal_connect (specific_window, "hide", 
482                 G_CALLBACK (on_smtp_servers_window_hide), win);
483 #endif /* MODEST_PLATFORM_MAEMO */
484 }
485
486 void
487 modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
488 {
489         ModestWindow *msg_win = NULL;
490         TnyMsg *msg = NULL;
491         TnyFolder *folder = NULL;
492         gchar *account_name = NULL;
493         gchar *from_str = NULL;
494 /*      GError *err = NULL; */
495         TnyAccount *account = NULL;
496         ModestWindowMgr *mgr;
497         gchar *signature = NULL, *blank_and_signature = NULL;
498
499         /* if there are no accounts yet, just show the wizard */
500         if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE)) {
501                         run_account_setup_wizard (win);
502                         return;
503         }
504         
505         account_name = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
506         if (!account_name)
507                 account_name = g_strdup (modest_window_get_active_account (win));
508         if (!account_name) {
509                 g_printerr ("modest: no account found\n");
510                 goto cleanup;
511         }
512         
513         account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(),
514                                                                        account_name,
515                                                                        TNY_ACCOUNT_TYPE_STORE);
516         if (!account) {
517                 g_printerr ("modest: failed to get tnyaccount for '%s'\n", account_name);
518                 goto cleanup;
519         }
520
521         from_str = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(), account_name);
522         if (!from_str) {
523                 g_printerr ("modest: failed get from string for '%s'\n", account_name);
524                 goto cleanup;
525         }
526
527         if (modest_account_mgr_get_bool (modest_runtime_get_account_mgr (), account_name,
528                                          MODEST_ACCOUNT_USE_SIGNATURE, FALSE)) {
529                 signature = modest_account_mgr_get_string (modest_runtime_get_account_mgr (), account_name,
530                                                            MODEST_ACCOUNT_SIGNATURE, FALSE);
531                 blank_and_signature = g_strconcat ("\n", signature, NULL);
532                 g_free (signature);
533         } else {
534                 blank_and_signature = g_strdup ("");
535         }
536
537         msg = modest_tny_msg_new ("", from_str, "", "", "", blank_and_signature, NULL);
538         if (!msg) {
539                 g_printerr ("modest: failed to create new msg\n");
540                 goto cleanup;
541         }
542         
543         folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS);
544         if (!folder) {
545                 g_printerr ("modest: failed to find Drafts folder\n");
546                 goto cleanup;
547         }
548         
549
550         /* Create and register edit window */
551         /* This is destroyed by TOOD. */
552         msg_win = modest_msg_edit_window_new (msg, account_name);
553         mgr = modest_runtime_get_window_mgr ();
554         modest_window_mgr_register_window (mgr, msg_win);
555
556         if (win)
557                 gtk_window_set_transient_for (GTK_WINDOW (msg_win),
558                                               GTK_WINDOW (win));        
559         gtk_widget_show_all (GTK_WIDGET (msg_win));
560
561 cleanup:
562         g_free (account_name);
563         g_free (from_str);
564         g_free (blank_and_signature);
565         if (msg_win)
566                 g_object_unref (msg_win);
567         if (account)
568                 g_object_unref (G_OBJECT(account));
569         if (msg)
570                 g_object_unref (G_OBJECT(msg));
571         if (folder)
572                 g_object_unref (G_OBJECT(folder));
573 }
574
575 static void
576 open_msg_cb (ModestMailOperation *mail_op, 
577              TnyHeader *header, 
578              TnyMsg *msg, 
579              gpointer user_data)
580 {
581         ModestWindowMgr *mgr = NULL;
582         ModestWindow *parent_win = NULL;
583         ModestWindow *win = NULL;
584         TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN;
585         gchar *account = NULL;
586         TnyFolder *folder;
587         
588         /* TODO: Show an error? (review the specs) */
589         if (!msg)
590                 return;
591
592         parent_win = (ModestWindow *) modest_mail_operation_get_source (mail_op);
593         folder = tny_header_get_folder (header);
594
595         /* Mark header as read */
596         headers_action_mark_as_read (header, MODEST_WINDOW(parent_win), NULL);
597
598         /* Get account */
599         account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
600         if (!account)
601                 account =  g_strdup (modest_window_get_active_account (MODEST_WINDOW (parent_win)));
602         
603         /* Gets folder type (OUTBOX headers will be opened in edit window */
604         if (modest_tny_folder_is_local_folder (folder))
605                 folder_type = modest_tny_folder_get_local_folder_type (folder);
606
607         /* If the header is in the drafts folder then open the editor,
608            else the message view window */
609         if (folder_type == TNY_FOLDER_TYPE_DRAFTS) {
610                 /* we cannot edit without a valid account... */
611                 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
612                         run_account_setup_wizard(parent_win);
613                         goto cleanup;
614                 }
615                 win = modest_msg_edit_window_new (msg, account);
616         } else {
617                 gchar *uid = modest_tny_folder_get_header_unique_id (header);
618
619                 if (MODEST_IS_MAIN_WINDOW (parent_win)) {
620                         GtkWidget *header_view;
621                         GtkTreeSelection *sel;
622                         GList *sel_list = NULL;
623                         GtkTreeModel *model;
624                 
625                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(parent_win),
626                                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
627
628                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
629                         sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
630
631                         if (sel_list != NULL) {
632                                 GtkTreeRowReference *row_reference;
633
634                                 row_reference = gtk_tree_row_reference_new (model, (GtkTreePath *) sel_list->data);
635                                 g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
636                                 g_list_free (sel_list);
637                                 
638                                 win = modest_msg_view_window_new_with_header_model (msg, 
639                                                                                     account,
640                                                                                     (const gchar*) uid,
641                                                                                     model, 
642                                                                                     row_reference);
643                                 gtk_tree_row_reference_free (row_reference);
644                         } else {
645                                 win = modest_msg_view_window_new (msg, account, (const gchar*) uid);
646                         }
647                 } else {
648                         win = modest_msg_view_window_new (msg, account, (const gchar*) uid);
649                 }
650                 g_free (uid);
651         }
652         
653         /* Register and show new window */
654         if (win != NULL) {
655                 mgr = modest_runtime_get_window_mgr ();
656                 modest_window_mgr_register_window (mgr, win);
657                 g_object_unref (win);
658                 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_win));
659                 gtk_widget_show_all (GTK_WIDGET(win));
660         }
661
662 cleanup:
663         /* Free */
664         g_free(account);
665         g_object_unref (parent_win);
666         g_object_unref (msg);
667         g_object_unref (folder);
668 }
669
670 /*
671  * This function is the error handler of the
672  * modest_mail_operation_get_msgs_full operation
673  */
674 static void
675 modest_ui_actions_get_msgs_full_error_handler (ModestMailOperation *mail_op,
676                                                gpointer user_data)
677 {
678         const GError *error;
679
680         error = modest_mail_operation_get_error (mail_op);
681         if (error->code == MODEST_MAIL_OPERATION_ERROR_MESSAGE_SIZE_LIMIT) {
682                 GObject *win = modest_mail_operation_get_source (mail_op);
683
684                 modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
685                                                         error->message);
686                 g_object_unref (win);
687         }
688 }
689
690 /*
691  * This function is used by both modest_ui_actions_on_open and
692  * modest_ui_actions_on_header_activated. This way we always do the
693  * same when trying to open messages.
694  */
695 static void
696 _modest_ui_actions_open (TnyList *headers, ModestWindow *win)
697 {
698         ModestWindowMgr *mgr;
699         TnyIterator *iter;
700         ModestMailOperation *mail_op;
701         TnyList *not_opened_headers;
702
703         /* Look if we already have a message view for each header. If
704            true, then remove the header from the list of headers to
705            open */
706         mgr = modest_runtime_get_window_mgr ();
707         iter = tny_list_create_iterator (headers);
708         not_opened_headers = tny_simple_list_new ();
709         while (!tny_iterator_is_done (iter)) {
710                 ModestWindow *window;
711                 TnyHeader *header;
712                 
713                 header = TNY_HEADER (tny_iterator_get_current (iter));
714                 window = modest_window_mgr_find_window_by_header (mgr, header);
715                 /* Do not open again the message and present the
716                    window to the user */
717                 if (window)
718                         gtk_window_present (GTK_WINDOW (window));
719                 else
720                         tny_list_append (not_opened_headers, G_OBJECT (header));
721
722                 g_object_unref (header);
723                 tny_iterator_next (iter);
724         }
725         
726         /* Open each message */
727         mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
728                                                                  G_OBJECT (win), 
729                                                                  modest_ui_actions_get_msgs_full_error_handler, 
730                                                                  NULL);
731         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
732         if (tny_list_get_length (not_opened_headers) > 1) {
733                 modest_mail_operation_get_msgs_full (mail_op, 
734                                                      not_opened_headers, 
735                                                      open_msg_cb, 
736                                                      NULL, 
737                                                      NULL);
738         } else {
739                 TnyIterator *iter = tny_list_create_iterator (not_opened_headers);
740                 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
741                 modest_mail_operation_get_msg (mail_op, header, open_msg_cb, NULL);
742                 g_object_unref (header);
743                 g_object_unref (iter);
744         }
745
746         /* Clean */
747         g_object_unref (not_opened_headers);
748         g_object_unref (iter);
749         g_object_unref (mail_op);
750 }
751
752 void
753 modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
754 {
755         TnyList *headers;
756
757         /* Get headers */
758         headers = get_selected_headers (win);
759         if (!headers)
760                 return;
761
762         /* Open them */
763         _modest_ui_actions_open (headers, win);
764
765         g_object_unref(headers);
766 }
767
768
769 static void
770 free_reply_forward_helper (gpointer data)
771 {
772         ReplyForwardHelper *helper;
773
774         helper = (ReplyForwardHelper *) data;
775         g_free (helper->account_name);
776         g_slice_free (ReplyForwardHelper, helper);
777 }
778
779 static void
780 reply_forward_cb (ModestMailOperation *mail_op, 
781                   TnyHeader *header, 
782                   TnyMsg *msg,
783                   gpointer user_data)
784 {
785         TnyMsg *new_msg;
786         ReplyForwardHelper *rf_helper;
787         ModestWindow *msg_win = NULL;
788         ModestEditType edit_type;
789         gchar *from = NULL;
790         TnyAccount *account = NULL;
791         ModestWindowMgr *mgr = NULL;
792         gchar *signature = NULL;
793                         
794         g_return_if_fail (user_data != NULL);
795         rf_helper = (ReplyForwardHelper *) user_data;
796
797         from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
798                                                    rf_helper->account_name);
799         if (modest_account_mgr_get_bool (modest_runtime_get_account_mgr(),
800                                          rf_helper->account_name,
801                                          MODEST_ACCOUNT_USE_SIGNATURE, FALSE)) {
802                 signature = modest_account_mgr_get_string (modest_runtime_get_account_mgr (),
803                                                            rf_helper->account_name,
804                                                            MODEST_ACCOUNT_SIGNATURE, FALSE);
805         }
806
807         /* Create reply mail */
808         switch (rf_helper->action) {
809         case ACTION_REPLY:
810                 new_msg = 
811                         modest_tny_msg_create_reply_msg (msg,  from, signature,
812                                                          rf_helper->reply_forward_type,
813                                                          MODEST_TNY_MSG_REPLY_MODE_SENDER);
814                 break;
815         case ACTION_REPLY_TO_ALL:
816                 new_msg = 
817                         modest_tny_msg_create_reply_msg (msg, from, signature, rf_helper->reply_forward_type,
818                                                          MODEST_TNY_MSG_REPLY_MODE_ALL);
819                 edit_type = MODEST_EDIT_TYPE_REPLY;
820                 break;
821         case ACTION_FORWARD:
822                 new_msg = 
823                         modest_tny_msg_create_forward_msg (msg, from, signature, rf_helper->reply_forward_type);
824                 edit_type = MODEST_EDIT_TYPE_FORWARD;
825                 break;
826         default:
827                 g_return_if_reached ();
828                 return;
829         }
830
831         g_free (signature);
832
833         if (!new_msg) {
834                 g_printerr ("modest: failed to create message\n");
835                 goto cleanup;
836         }
837
838         account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(),
839                                                                        rf_helper->account_name,
840                                                                        TNY_ACCOUNT_TYPE_STORE);
841         if (!account) {
842                 g_printerr ("modest: failed to get tnyaccount for '%s'\n", rf_helper->account_name);
843                 goto cleanup;
844         }
845
846         /* Create and register the windows */
847         msg_win = modest_msg_edit_window_new (new_msg, rf_helper->account_name);
848         mgr = modest_runtime_get_window_mgr ();
849         modest_window_mgr_register_window (mgr, msg_win);
850
851         if (rf_helper->parent_window != NULL) {
852                 gdouble parent_zoom;
853
854                 parent_zoom = modest_window_get_zoom (MODEST_WINDOW (rf_helper->parent_window));
855                 modest_window_set_zoom (msg_win, parent_zoom);
856         }
857
858         /* Show edit window */
859         gtk_widget_show_all (GTK_WIDGET (msg_win));
860
861 cleanup:
862         if (msg_win)
863                 g_object_unref (msg_win);
864         if (new_msg)
865                 g_object_unref (G_OBJECT (new_msg));
866         if (account)
867                 g_object_unref (G_OBJECT (account));
868         g_object_unref (msg);
869         g_object_unref (header);
870 }
871
872 /*
873  * Checks a list of headers. If any of them are not currently
874  * downloaded (CACHED) then it asks the user for permission to
875  * download them.
876  *
877  * Returns FALSE if the user does not want to download the
878  * messages. Returns TRUE if the user allowed the download or if all
879  * of them are currently downloaded
880  */
881 static gboolean
882 download_uncached_messages (TnyList *header_list, GtkWindow *win)
883 {
884         TnyIterator *iter;
885         gboolean retval;
886         gint uncached_messages = 0;
887
888         iter = tny_list_create_iterator (header_list);
889         while (!tny_iterator_is_done (iter)) {
890                 TnyHeader *header;
891                 TnyHeaderFlags flags;
892
893                 header = TNY_HEADER (tny_iterator_get_current (iter));
894                 flags = tny_header_get_flags (header);
895                 /* TODO: is this the right flag?, it seems that some
896                    headers that have been previously downloaded do not
897                    come with it */
898                 if (! (flags & TNY_HEADER_FLAG_CACHED))
899                         uncached_messages ++;
900                 g_object_unref (header);
901                 tny_iterator_next (iter);
902         }
903         g_object_unref (iter);
904
905         /* Ask for user permission to download the messages */
906         retval = TRUE;
907         if (uncached_messages > 0) {
908                 GtkResponseType response;
909                 response = 
910                         modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
911                                                                  ngettext("mcen_nc_get_msg",
912                                                                           "mcen_nc_get_msgs",
913                                                                          uncached_messages));
914                 if (response == GTK_RESPONSE_CANCEL)
915                         retval = FALSE;
916         }
917         return retval;
918 }
919
920
921 /*
922  * Common code for the reply and forward actions
923  */
924 static void
925 reply_forward (ReplyForwardAction action, ModestWindow *win)
926 {
927         ModestMailOperation *mail_op = NULL;
928         TnyList *header_list = NULL;
929         ReplyForwardHelper *rf_helper = NULL;
930         guint reply_forward_type;
931         gboolean continue_download;
932         
933         g_return_if_fail (MODEST_IS_WINDOW(win));
934
935         /* we need an account when editing */
936         if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
937                 run_account_setup_wizard (win);
938                 return;
939         }
940         
941         header_list = get_selected_headers (win);
942         if (!header_list)
943                 return;
944
945         /* Check that the messages have been previously downloaded */
946         continue_download = download_uncached_messages (header_list, GTK_WINDOW (win));
947         if (!continue_download) {
948                 g_object_unref (header_list);
949                 return;
950         }
951         
952         reply_forward_type = 
953                 modest_conf_get_int (modest_runtime_get_conf (),
954                                      (action == ACTION_FORWARD) ? MODEST_CONF_FORWARD_TYPE : MODEST_CONF_REPLY_TYPE,
955                                      NULL);
956         /* We assume that we can only select messages of the
957            same folder and that we reply all of them from the
958            same account. In fact the interface currently only
959            allows single selection */
960         
961         /* Fill helpers */
962         rf_helper = g_slice_new0 (ReplyForwardHelper);
963         rf_helper->reply_forward_type = reply_forward_type;
964         rf_helper->action = action;
965         rf_helper->account_name = g_strdup (modest_window_get_active_account (win));
966         if ((win != NULL) && (MODEST_IS_WINDOW (win)))
967                 rf_helper->parent_window = GTK_WIDGET (win);
968         if (!rf_helper->account_name)
969                 rf_helper->account_name =
970                         modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
971
972         if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
973                 TnyMsg *msg;
974                 TnyHeader *header;
975                 /* Get header and message. Do not free them here, the
976                    reply_forward_cb must do it */
977                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW(win));
978                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));
979                 if (!msg || !header) {
980                         if (msg)
981                                 g_object_unref (msg);
982                         if (header)
983                                 g_object_unref (header);
984                         g_printerr ("modest: no message found\n");
985                         return;
986                 } else
987                         reply_forward_cb (NULL, header, msg, rf_helper);
988         } else {
989                 /* Retrieve messages */
990                 mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
991                                                                          G_OBJECT(win),
992                                                                          modest_ui_actions_get_msgs_full_error_handler, 
993                                                                          NULL);
994                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
995                 modest_mail_operation_get_msgs_full (mail_op, 
996                                                      header_list, 
997                                                      reply_forward_cb, 
998                                                      rf_helper, 
999                                                      free_reply_forward_helper);
1000
1001                 /* Clean */
1002                 g_object_unref(mail_op);
1003         }
1004
1005         /* Free */
1006         g_object_unref (header_list);
1007 }
1008
1009 void
1010 modest_ui_actions_on_reply (GtkAction *action, ModestWindow *win)
1011 {
1012         g_return_if_fail (MODEST_IS_WINDOW(win));
1013
1014         reply_forward (ACTION_REPLY, win);
1015 }
1016
1017 void
1018 modest_ui_actions_on_forward (GtkAction *action, ModestWindow *win)
1019 {
1020         g_return_if_fail (MODEST_IS_WINDOW(win));
1021
1022         reply_forward (ACTION_FORWARD, win);
1023 }
1024
1025 void
1026 modest_ui_actions_on_reply_all (GtkAction *action, ModestWindow *win)
1027 {
1028         g_return_if_fail (MODEST_IS_WINDOW(win));
1029
1030         reply_forward (ACTION_REPLY_TO_ALL, win);
1031 }
1032
1033 void 
1034 modest_ui_actions_on_next (GtkAction *action, 
1035                            ModestWindow *window)
1036 {
1037         if (MODEST_IS_MAIN_WINDOW (window)) {
1038                 GtkWidget *header_view;
1039
1040                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1041                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1042                 if (!header_view)
1043                         return;
1044         
1045                 modest_header_view_select_next (MODEST_HEADER_VIEW(header_view)); 
1046         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1047                 modest_msg_view_window_select_next_message (MODEST_MSG_VIEW_WINDOW (window));
1048         } else {
1049                 g_return_if_reached ();
1050         }
1051 }
1052
1053 void 
1054 modest_ui_actions_on_prev (GtkAction *action, 
1055                            ModestWindow *window)
1056 {
1057         g_return_if_fail (MODEST_IS_WINDOW(window));
1058
1059         if (MODEST_IS_MAIN_WINDOW (window)) {
1060                 GtkWidget *header_view;
1061                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1062                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1063                 if (!header_view)
1064                         return;
1065                 
1066                 modest_header_view_select_prev (MODEST_HEADER_VIEW(header_view)); 
1067         } else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
1068                 modest_msg_view_window_select_previous_message (MODEST_MSG_VIEW_WINDOW (window));
1069         } else {
1070                 g_return_if_reached ();
1071         }
1072 }
1073
1074 void 
1075 modest_ui_actions_on_sort (GtkAction *action, 
1076                            ModestWindow *window)
1077 {
1078         g_return_if_fail (MODEST_IS_WINDOW(window));
1079
1080         if (MODEST_IS_MAIN_WINDOW (window)) {
1081                 GtkWidget *header_view;
1082                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
1083                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1084                 if (!header_view)
1085                         return;
1086
1087                 /* Show sorting dialog */
1088                 modest_platform_run_sort_dialog (GTK_WINDOW (window), MODEST_SORT_HEADERS);     
1089         }
1090 }
1091
1092 static void
1093 new_messages_arrived (ModestMailOperation *self, 
1094                       gint new_messages,
1095                       gpointer user_data)
1096 {
1097         if (new_messages == 0)
1098                 return;
1099
1100         modest_platform_on_new_msg ();
1101 }
1102
1103 /*
1104  * This function performs the send & receive required actions. The
1105  * window is used to create the mail operation. Typically it should
1106  * always be the main window, but we pass it as argument in order to
1107  * be more flexible.
1108  */
1109 void
1110 modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
1111 {
1112         gchar *acc_name = NULL;
1113         ModestMailOperation *mail_op;
1114
1115         /* If no account name was provided then get the current account, and if
1116            there is no current account then pick the default one: */
1117         if (!account_name) {
1118                 acc_name = g_strdup (modest_window_get_active_account(win));
1119                 if (!acc_name)
1120                         acc_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
1121                 if (!acc_name) {
1122                         g_printerr ("modest: cannot get default account\n");
1123                         return;
1124                 }
1125         } else {
1126                 acc_name = g_strdup (account_name);
1127         }
1128
1129         /* Set send/receive operation in progress */    
1130         modest_main_window_notify_send_receive_initied (MODEST_MAIN_WINDOW(win));
1131
1132         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(win));
1133         g_signal_connect (G_OBJECT(mail_op), "progress-changed", 
1134                           G_CALLBACK (_on_send_receive_progress_changed), 
1135                           win);
1136
1137         /* Send & receive. */
1138         /* TODO: The spec wants us to first do any pending deletions, before receiving. */
1139         /* Receive and then send. The operation is tagged initially as
1140            a receive operation because the account update performs a
1141            receive and then a send. The operation changes its type
1142            internally, so the progress objects will receive the proper
1143            progress information */
1144         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
1145         modest_mail_operation_update_account (mail_op, acc_name, new_messages_arrived, NULL);
1146         g_object_unref (G_OBJECT (mail_op));
1147         
1148         /* Free */
1149         g_free (acc_name);
1150 }
1151
1152 /*
1153  * Refreshes all accounts. This function will be used by automatic
1154  * updates
1155  */
1156 void
1157 modest_ui_actions_do_send_receive_all (ModestWindow *win)
1158 {
1159         GSList *account_names, *iter;
1160
1161         account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
1162                                                           TRUE);
1163
1164         iter = account_names;
1165         while (iter) {                  
1166                 modest_ui_actions_do_send_receive ((const char*) iter->data, win);
1167                 iter = g_slist_next (iter);
1168         }
1169
1170         modest_account_mgr_free_account_names (account_names);
1171         account_names = NULL;
1172 }
1173
1174 /*
1175  * Handler of the click on Send&Receive button in the main toolbar
1176  */
1177 void
1178 modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
1179 {
1180         /* Check if accounts exist */
1181         gboolean accounts_exist = 
1182                 modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
1183         
1184         /* If not, allow the user to create an account before trying to send/receive. */
1185         if (!accounts_exist)
1186                 modest_ui_actions_on_accounts (NULL, win);
1187         
1188         /* Refresh the active account */
1189         modest_ui_actions_do_send_receive (NULL, win);
1190 }
1191
1192
1193 void
1194 modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *main_window)
1195 {
1196         ModestConf *conf;
1197         GtkWidget *header_view;
1198         
1199         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1200
1201         header_view = modest_main_window_get_child_widget (main_window,
1202                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1203         if (!header_view)
1204                 return;
1205
1206         conf = modest_runtime_get_conf ();
1207         
1208         /* what is saved/restored is depending on the style; thus; we save with
1209          * old style, then update the style, and restore for this new style
1210          */
1211         modest_widget_memory_save (conf, G_OBJECT(header_view), MODEST_CONF_HEADER_VIEW_KEY);
1212         
1213         if (modest_header_view_get_style
1214             (MODEST_HEADER_VIEW(header_view)) == MODEST_HEADER_VIEW_STYLE_DETAILS)
1215                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
1216                                               MODEST_HEADER_VIEW_STYLE_TWOLINES);
1217         else
1218                 modest_header_view_set_style (MODEST_HEADER_VIEW(header_view),
1219                                               MODEST_HEADER_VIEW_STYLE_DETAILS);
1220
1221         modest_widget_memory_restore (conf, G_OBJECT(header_view),
1222                                       MODEST_CONF_HEADER_VIEW_KEY);
1223 }
1224
1225
1226 void 
1227 modest_ui_actions_on_header_selected (ModestHeaderView *header_view, 
1228                                       TnyHeader *header,
1229                                       ModestMainWindow *main_window)
1230 {
1231         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1232         g_return_if_fail (MODEST_IS_HEADER_VIEW (header_view));
1233         
1234         /* If no header has been selected then exit */
1235         if (!header)
1236                 return;
1237
1238         /* Update focus */
1239         if (!gtk_widget_is_focus (GTK_WIDGET(header_view)))
1240             gtk_widget_grab_focus (GTK_WIDGET(header_view));
1241
1242         /* Update Main window title */
1243         if (gtk_widget_is_focus (GTK_WIDGET(header_view))) {
1244                 const gchar *subject = tny_header_get_subject (header);
1245                 if (subject && strlen(subject) > 0)
1246                         gtk_window_set_title (GTK_WINDOW (main_window), subject);
1247                 else
1248                         gtk_window_set_title (GTK_WINDOW (main_window), _("mail_va_no_subject"));
1249         }
1250
1251         /* Update toolbar dimming state */
1252         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
1253 }
1254
1255 void
1256 modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
1257                                        TnyHeader *header,
1258                                        ModestMainWindow *main_window)
1259 {
1260         TnyList *headers;
1261
1262         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1263         
1264         if (!header)
1265                 return;
1266
1267         headers = tny_simple_list_new ();
1268         tny_list_prepend (headers, G_OBJECT (header));
1269
1270         _modest_ui_actions_open (headers, MODEST_WINDOW (main_window));
1271
1272         g_object_unref (headers);
1273 }
1274
1275 static void
1276 set_active_account_from_tny_account (TnyAccount *account,
1277                                      ModestWindow *window)
1278 {
1279         const gchar *server_acc_name = tny_account_get_id (account);
1280         
1281         /* We need the TnyAccount provided by the
1282            account store because that is the one that
1283            knows the name of the Modest account */
1284         TnyAccount *modest_server_account = modest_server_account = 
1285                 modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store (),
1286                                                              MODEST_TNY_ACCOUNT_STORE_QUERY_ID, 
1287                                                              server_acc_name);
1288         
1289         const gchar *modest_acc_name = 
1290                 modest_tny_account_get_parent_modest_account_name_for_server_account (modest_server_account);
1291         modest_window_set_active_account (window, modest_acc_name);
1292         g_object_unref (modest_server_account);
1293 }
1294
1295
1296 static void
1297 folder_refreshed_cb (const GObject *obj, 
1298                      TnyFolder *folder, 
1299                      gpointer user_data)
1300 {
1301         ModestMainWindow *win = NULL;
1302         GtkWidget *header_view;
1303
1304         g_return_if_fail (TNY_IS_FOLDER (folder));
1305
1306         win = MODEST_MAIN_WINDOW (user_data);
1307         header_view = 
1308                 modest_main_window_get_child_widget(win, MODEST_WIDGET_TYPE_HEADER_VIEW);
1309
1310         /* Check if folder is empty and set headers view contents style */
1311         if (tny_folder_get_all_count (folder) == 0) {
1312         printf ("DEBUG: %s: tny_folder_get_all_count() returned 0.\n", __FUNCTION__);
1313                 modest_main_window_set_contents_style (win,
1314                                                        MODEST_MAIN_WINDOW_CONTENTS_STYLE_EMPTY);
1315         } else {
1316                 printf ("DEBUG: %s: tny_folder_get_all_count() returned >0.\n", __FUNCTION__);
1317         }
1318 }
1319
1320 void 
1321 modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
1322                                                TnyFolderStore *folder_store, 
1323                                                gboolean selected,
1324                                                ModestMainWindow *main_window)
1325 {
1326         ModestConf *conf;
1327         GtkWidget *header_view;
1328
1329         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1330
1331         header_view = modest_main_window_get_child_widget(main_window,
1332                                                           MODEST_WIDGET_TYPE_HEADER_VIEW);
1333         if (!header_view)
1334                 return;
1335         
1336         conf = modest_runtime_get_conf ();
1337
1338         if (TNY_IS_ACCOUNT (folder_store)) {
1339                 if (selected) {
1340                         /* Update active account */
1341                         set_active_account_from_tny_account (TNY_ACCOUNT (folder_store), MODEST_WINDOW (main_window));
1342                         /* Show account details */
1343                         modest_main_window_set_contents_style (main_window, MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS);
1344                 }
1345         } else {
1346                 if (TNY_IS_FOLDER (folder_store) && selected) {
1347                         
1348                         /* Update the active account */
1349                         TnyAccount *account = modest_tny_folder_get_account (TNY_FOLDER (folder_store));
1350                         if (account) {
1351                                 set_active_account_from_tny_account (account, MODEST_WINDOW (main_window));
1352                                 g_object_unref (account);
1353                                 account = NULL;
1354                         }
1355
1356                         /* Set the header style by default, it could
1357                            be changed later by the refresh callback to
1358                            empty */
1359                         modest_main_window_set_contents_style (main_window, 
1360                                                                MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
1361
1362                         /* Set folder on header view. This function
1363                            will call tny_folder_refresh_async so we
1364                            pass a callback that will be called when
1365                            finished. We use that callback to set the
1366                            empty view if there are no messages */
1367                         modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
1368                                                        TNY_FOLDER (folder_store),
1369                                                        folder_refreshed_cb,
1370                                                        main_window);
1371                         
1372                         /* Restore configuration. We need to do this
1373                            *after* the set_folder because the widget
1374                            memory asks the header view about its
1375                            folder  */
1376                         modest_widget_memory_restore (modest_runtime_get_conf (), 
1377                                                       G_OBJECT(header_view),
1378                                                       MODEST_CONF_HEADER_VIEW_KEY);
1379                 } else {
1380                         /* Update the active account */
1381                         modest_window_set_active_account (MODEST_WINDOW (main_window), NULL);
1382                         /* Save only if we're seeing headers */
1383                         if (modest_main_window_get_contents_style (main_window) ==
1384                             MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS)
1385                                 modest_widget_memory_save (conf, G_OBJECT (header_view), 
1386                                                            MODEST_CONF_HEADER_VIEW_KEY);
1387                         modest_header_view_clear (MODEST_HEADER_VIEW(header_view));
1388                 }
1389         }
1390
1391         /* Update toolbar dimming state */
1392         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
1393 }
1394
1395 void 
1396 modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemType type,
1397                                      ModestWindow *win)
1398 {
1399         GtkWidget *dialog;
1400         gchar *txt, *item;
1401         gboolean online;
1402
1403         item = (type == MODEST_ITEM_TYPE_FOLDER) ? "folder" : "message";
1404         
1405         if (g_main_depth > 0)   
1406                 gdk_threads_enter ();
1407         online = tny_device_is_online (modest_runtime_get_device());
1408
1409         if (online) {
1410                 /* already online -- the item is simply not there... */
1411                 dialog = gtk_message_dialog_new (GTK_WINDOW (win),
1412                                                  GTK_DIALOG_MODAL,
1413                                                  GTK_MESSAGE_WARNING,
1414                                                  GTK_BUTTONS_OK,
1415                                                  _("The %s you selected cannot be found"),
1416                                                  item);
1417                 gtk_dialog_run (GTK_DIALOG(dialog));
1418         } else {
1419                 dialog = gtk_dialog_new_with_buttons (_("Connection requested"),
1420                                                       GTK_WINDOW (win),
1421                                                       GTK_DIALOG_MODAL,
1422                                                       GTK_STOCK_CANCEL,
1423                                                       GTK_RESPONSE_REJECT,
1424                                                       GTK_STOCK_OK,
1425                                                       GTK_RESPONSE_ACCEPT,
1426                                                       NULL);
1427                 txt = g_strdup_printf (_("This %s is not available in offline mode.\n"
1428                                          "Do you want to get online?"), item);
1429                 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
1430                                     gtk_label_new (txt), FALSE, FALSE, 0);
1431                 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
1432                 g_free (txt);
1433
1434                 gtk_window_set_default_size (GTK_WINDOW(dialog), 300, 300);
1435                 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
1436 //                      modest_platform_connect_and_wait ();
1437                 }
1438         }
1439         gtk_widget_destroy (dialog);
1440         if (g_main_depth > 0)   
1441                 gdk_threads_leave ();
1442 }
1443
1444 void
1445 modest_ui_actions_on_msg_link_hover (ModestMsgView *msgview, const gchar* link,
1446                                      ModestWindow *win)
1447 {
1448         /* g_message ("%s %s", __FUNCTION__, link); */
1449 }       
1450
1451
1452 void
1453 modest_ui_actions_on_msg_link_clicked (ModestMsgView *msgview, const gchar* link,
1454                                         ModestWindow *win)
1455 {
1456         modest_platform_activate_uri (link);
1457 }
1458
1459 void
1460 modest_ui_actions_on_msg_link_contextual (ModestMsgView *msgview, const gchar* link,
1461                                           ModestWindow *win)
1462 {
1463         modest_platform_show_uri_popup (link);
1464 }
1465
1466 void
1467 modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview, TnyMimePart *mime_part,
1468                                              ModestWindow *win)
1469 {
1470         modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (win), mime_part);
1471 }
1472
1473 void
1474 modest_ui_actions_on_msg_recpt_activated (ModestMsgView *msgview,
1475                                           const gchar *address,
1476                                           ModestWindow *win)
1477 {
1478         /* g_message ("%s %s", __FUNCTION__, address); */
1479 }
1480
1481 void
1482 modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edit_window)
1483 {
1484         TnyTransportAccount *transport_account;
1485         ModestMailOperation *mail_operation;
1486         MsgData *data;
1487         gchar *account_name, *from;
1488         ModestAccountMgr *account_mgr;
1489         gchar *info_text = NULL;
1490
1491         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
1492         
1493         data = modest_msg_edit_window_get_msg_data (edit_window);
1494
1495         account_mgr = modest_runtime_get_account_mgr();
1496         account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
1497         if (!account_name) 
1498                 account_name = modest_account_mgr_get_default_account (account_mgr);
1499         if (!account_name) {
1500                 g_printerr ("modest: no account found\n");
1501                 modest_msg_edit_window_free_msg_data (edit_window, data);
1502                 return;
1503         }
1504
1505         if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1506                 account_name = g_strdup (data->account_name);
1507         }
1508
1509         transport_account =
1510                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_account
1511                                       (modest_runtime_get_account_store(),
1512                                        account_name,
1513                                        TNY_ACCOUNT_TYPE_TRANSPORT));
1514         if (!transport_account) {
1515                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
1516                 g_free (account_name);
1517                 modest_msg_edit_window_free_msg_data (edit_window, data);
1518                 return;
1519         }
1520         from = modest_account_mgr_get_from_string (account_mgr, account_name);
1521
1522         /* Create the mail operation */         
1523         mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, G_OBJECT(edit_window));
1524         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
1525
1526         modest_mail_operation_save_to_drafts (mail_operation,
1527                                               transport_account,
1528                                               data->draft_msg,
1529                                               from,
1530                                               data->to, 
1531                                               data->cc, 
1532                                               data->bcc,
1533                                               data->subject, 
1534                                               data->plain_body, 
1535                                               data->html_body,
1536                                               data->attachments,
1537                                               data->priority_flags);
1538         /* Frees */
1539         g_free (from);
1540         g_free (account_name);
1541         g_object_unref (G_OBJECT (transport_account));
1542         g_object_unref (G_OBJECT (mail_operation));
1543
1544         modest_msg_edit_window_free_msg_data (edit_window, data);
1545
1546         info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
1547         modest_platform_information_banner (NULL, NULL, info_text);
1548         g_free (info_text);
1549 }
1550
1551 /* For instance, when clicking the Send toolbar button when editing a message: */
1552 void
1553 modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
1554 {
1555         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
1556
1557         if (!modest_msg_edit_window_check_names (edit_window))
1558                 return;
1559         
1560         /* FIXME: Code added just for testing. The final version will
1561            use the send queue provided by tinymail and some
1562            classifier */
1563         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1564         gchar *account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
1565         if (!account_name) 
1566                 account_name = modest_account_mgr_get_default_account (account_mgr);
1567                 
1568         if (!account_name) {
1569                 g_printerr ("modest: no account found\n");
1570                 return;
1571         }
1572         
1573         MsgData *data = modest_msg_edit_window_get_msg_data (edit_window);
1574
1575         if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1576                 account_name = g_strdup (data->account_name);
1577         }
1578         
1579         /* Get the currently-active transport account for this modest account: */
1580         TnyTransportAccount *transport_account =
1581                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
1582                                       (modest_runtime_get_account_store(),
1583                                        account_name));
1584         if (!transport_account) {
1585                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
1586                 g_free (account_name);
1587                 modest_msg_edit_window_free_msg_data (edit_window, data);
1588                 return;
1589         }
1590         
1591         gchar *from = modest_account_mgr_get_from_string (account_mgr, account_name);
1592
1593         /* mail content checks and dialogs */
1594         if (data->subject == NULL || data->subject[0] == '\0') {
1595                 GtkResponseType response;
1596                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (edit_window),
1597                                                                     _("mcen_nc_subject_is_empty_send"));
1598                 if (response == GTK_RESPONSE_CANCEL) {
1599                         g_free (account_name);
1600                         return;
1601                 }
1602         }
1603
1604         if (data->plain_body == NULL || data->plain_body[0] == '\0') {
1605                 GtkResponseType response;
1606                 gchar *note_message;
1607                 gchar *note_subject = data->subject;
1608                 if (note_subject == NULL || note_subject[0] == '\0')
1609                         note_subject = _("mail_va_no_subject");
1610                 note_message = g_strdup_printf (_("emev_ni_ui_smtp_message_null"), note_subject);
1611                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (edit_window),
1612                                                                     note_message);
1613                 g_free (note_message);
1614                 if (response == GTK_RESPONSE_CANCEL) {
1615                         g_free (account_name);
1616                         return;
1617                 }
1618         }
1619
1620         modest_platform_information_banner (NULL, NULL, _("mcen_ib_outbox_waiting_to_be_sent"));
1621
1622         /* Create the mail operation */
1623         ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, G_OBJECT(edit_window));
1624         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
1625
1626         modest_mail_operation_send_new_mail (mail_operation,
1627                                              transport_account,
1628                                              data->draft_msg,
1629                                              from,
1630                                              data->to, 
1631                                              data->cc, 
1632                                              data->bcc,
1633                                              data->subject, 
1634                                              data->plain_body, 
1635                                              data->html_body,
1636                                              data->attachments,
1637                                              data->priority_flags);
1638                                              
1639         /* Free data: */
1640         g_free (from);
1641         g_free (account_name);
1642         g_object_unref (G_OBJECT (transport_account));
1643         g_object_unref (G_OBJECT (mail_operation));
1644
1645         modest_msg_edit_window_free_msg_data (edit_window, data);
1646         modest_msg_edit_window_set_sent (edit_window, TRUE);
1647
1648         /* Save settings and close the window: */
1649         gtk_widget_destroy (GTK_WIDGET (edit_window));
1650 }
1651
1652 void 
1653 modest_ui_actions_on_toggle_bold (GtkToggleAction *action,
1654                                   ModestMsgEditWindow *window)
1655 {
1656         ModestMsgEditFormatState *format_state = NULL;
1657
1658         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1659         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
1660
1661         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1662                 return;
1663
1664         format_state = modest_msg_edit_window_get_format_state (window);
1665         g_return_if_fail (format_state != NULL);
1666
1667         format_state->bold = gtk_toggle_action_get_active (action);
1668         modest_msg_edit_window_set_format_state (window, format_state);
1669         g_free (format_state);
1670         
1671 }
1672
1673 void 
1674 modest_ui_actions_on_toggle_italics (GtkToggleAction *action,
1675                                      ModestMsgEditWindow *window)
1676 {
1677         ModestMsgEditFormatState *format_state = NULL;
1678
1679         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1680         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
1681
1682         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1683                 return;
1684
1685         format_state = modest_msg_edit_window_get_format_state (window);
1686         g_return_if_fail (format_state != NULL);
1687
1688         format_state->italics = gtk_toggle_action_get_active (action);
1689         modest_msg_edit_window_set_format_state (window, format_state);
1690         g_free (format_state);
1691         
1692 }
1693
1694 void 
1695 modest_ui_actions_on_toggle_bullets (GtkToggleAction *action,
1696                                      ModestMsgEditWindow *window)
1697 {
1698         ModestMsgEditFormatState *format_state = NULL;
1699
1700         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1701         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
1702
1703         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1704                 return;
1705
1706         format_state = modest_msg_edit_window_get_format_state (window);
1707         g_return_if_fail (format_state != NULL);
1708
1709         format_state->bullet = gtk_toggle_action_get_active (action);
1710         modest_msg_edit_window_set_format_state (window, format_state);
1711         g_free (format_state);
1712         
1713 }
1714
1715 void 
1716 modest_ui_actions_on_change_justify (GtkRadioAction *action,
1717                                      GtkRadioAction *selected,
1718                                      ModestMsgEditWindow *window)
1719 {
1720         ModestMsgEditFormatState *format_state = NULL;
1721         GtkJustification value;
1722
1723         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1724
1725         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1726                 return;
1727
1728         value = gtk_radio_action_get_current_value (selected);
1729
1730         format_state = modest_msg_edit_window_get_format_state (window);
1731         g_return_if_fail (format_state != NULL);
1732
1733         format_state->justification = value;
1734         modest_msg_edit_window_set_format_state (window, format_state);
1735         g_free (format_state);
1736 }
1737
1738 void 
1739 modest_ui_actions_on_select_editor_color (GtkAction *action,
1740                                           ModestMsgEditWindow *window)
1741 {
1742         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1743         g_return_if_fail (GTK_IS_ACTION (action));
1744
1745         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1746                 return;
1747
1748         modest_msg_edit_window_select_color (window);
1749 }
1750
1751 void 
1752 modest_ui_actions_on_select_editor_background_color (GtkAction *action,
1753                                                      ModestMsgEditWindow *window)
1754 {
1755         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1756         g_return_if_fail (GTK_IS_ACTION (action));
1757
1758         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1759                 return;
1760
1761         modest_msg_edit_window_select_background_color (window);
1762 }
1763
1764 void 
1765 modest_ui_actions_on_insert_image (GtkAction *action,
1766                                    ModestMsgEditWindow *window)
1767 {
1768         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1769         g_return_if_fail (GTK_IS_ACTION (action));
1770
1771         if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
1772                 return;
1773
1774         modest_msg_edit_window_insert_image (window);
1775 }
1776
1777 void 
1778 modest_ui_actions_on_attach_file (GtkAction *action,
1779                                   ModestMsgEditWindow *window)
1780 {
1781         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1782         g_return_if_fail (GTK_IS_ACTION (action));
1783
1784         modest_msg_edit_window_attach_file (window);
1785 }
1786
1787 void 
1788 modest_ui_actions_on_remove_attachments (GtkAction *action,
1789                                          ModestMsgEditWindow *window)
1790 {
1791         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1792         g_return_if_fail (GTK_IS_ACTION (action));
1793
1794         modest_msg_edit_window_remove_attachments (window, NULL);
1795 }
1796
1797 void 
1798 modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_window)
1799 {
1800         TnyFolderStore *parent_folder;
1801         GtkWidget *folder_view;
1802         
1803         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1804
1805         folder_view = modest_main_window_get_child_widget (main_window,
1806                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1807         if (!folder_view)
1808                 return;
1809
1810         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1811         
1812         if (parent_folder) {
1813                 gboolean finished = FALSE;
1814                 gint result;
1815                 gchar *folder_name = NULL, *suggested_name = NULL;
1816
1817                 /* Run the new folder dialog */
1818                 while (!finished) {
1819                         result = modest_platform_run_new_folder_dialog (GTK_WINDOW (main_window),
1820                                                                         parent_folder,
1821                                                                         suggested_name,
1822                                                                         &folder_name);
1823
1824                         if (result == GTK_RESPONSE_REJECT) {
1825                                 finished = TRUE;
1826                         } else {
1827                                 ModestMailOperation *mail_op;
1828                                 TnyFolder *new_folder = NULL;
1829
1830                                 mail_op  = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, 
1831                                                                       G_OBJECT(main_window));
1832                                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
1833                                                                  mail_op);
1834                                 new_folder = modest_mail_operation_create_folder (mail_op,
1835                                                                                   parent_folder,
1836                                                                                   (const gchar *) folder_name);
1837                                 if (new_folder) {
1838                                         g_object_unref (new_folder);
1839                                         finished = TRUE;
1840                                 }
1841                                 g_object_unref (mail_op);
1842                         }
1843                         g_free (folder_name);
1844                         folder_name = NULL;
1845                 }
1846
1847                 g_object_unref (parent_folder);
1848         }
1849 }
1850
1851 void 
1852 modest_ui_actions_on_rename_folder (GtkAction *action,
1853                                      ModestMainWindow *main_window)
1854 {
1855         TnyFolderStore *folder;
1856         GtkWidget *folder_view;
1857         GtkWidget *header_view; 
1858
1859         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1860
1861         folder_view = modest_main_window_get_child_widget (main_window,
1862                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1863         if (!folder_view)
1864                 return;
1865
1866         header_view = modest_main_window_get_child_widget (main_window,
1867                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1868         
1869         if (!header_view)
1870                 return;
1871
1872         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1873         
1874         if (folder && TNY_IS_FOLDER (folder)) {
1875                 gchar *folder_name;
1876                 gint response;
1877                 const gchar *current_name;
1878
1879                 current_name = tny_folder_get_name (TNY_FOLDER (folder));
1880                 response = modest_platform_run_rename_folder_dialog (GTK_WINDOW (main_window), NULL,
1881                                                                      current_name, &folder_name);
1882
1883                 if (response == GTK_RESPONSE_OK && strlen (folder_name) > 0) {
1884                         ModestMailOperation *mail_op;
1885
1886                         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, G_OBJECT(main_window));
1887                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1888                                                          mail_op);
1889
1890                         modest_header_view_clear (MODEST_HEADER_VIEW (header_view));
1891
1892                         modest_mail_operation_rename_folder (mail_op,
1893                                                              TNY_FOLDER (folder),
1894                                                              (const gchar *) folder_name);
1895
1896                         g_object_unref (mail_op);
1897                         g_free (folder_name);
1898                 }
1899                 g_object_unref (folder);
1900         }
1901 }
1902
1903 static void
1904 modest_ui_actions_delete_folder_error_handler (ModestMailOperation *mail_op,
1905                                                gpointer user_data)
1906 {
1907         GObject *win = modest_mail_operation_get_source (mail_op);
1908
1909         modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
1910                                                 _("mail_in_ui_folder_delete_error"));
1911         g_object_unref (win);
1912 }
1913
1914 static void
1915 delete_folder (ModestMainWindow *main_window, gboolean move_to_trash) 
1916 {
1917         TnyFolderStore *folder;
1918         GtkWidget *folder_view;
1919         gint response;
1920         gchar *message;
1921         
1922         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1923
1924         folder_view = modest_main_window_get_child_widget (main_window,
1925                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1926         if (!folder_view)
1927                 return;
1928
1929         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
1930
1931         /* Show an error if it's an account */
1932         if (!TNY_IS_FOLDER (folder)) {
1933                 modest_platform_run_information_dialog (GTK_WINDOW (main_window),
1934                                                         _("mail_in_ui_folder_delete_error"));
1935                 return ;
1936         }
1937
1938         /* Ask the user */      
1939         message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
1940                                     tny_folder_get_name (TNY_FOLDER (folder)));
1941         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (main_window), 
1942                                                             (const gchar *) message);
1943         g_free (message);
1944
1945         if (response == GTK_RESPONSE_OK) {
1946                 ModestMailOperation *mail_op = 
1947                         modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_DELETE, 
1948                                                                        G_OBJECT(main_window),
1949                                                                        modest_ui_actions_delete_folder_error_handler,
1950                                                                        NULL);
1951
1952                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1953                                                  mail_op);
1954                 modest_mail_operation_remove_folder (mail_op, TNY_FOLDER (folder), move_to_trash);
1955                 g_object_unref (G_OBJECT (mail_op));
1956         }
1957
1958         g_object_unref (G_OBJECT (folder));
1959 }
1960
1961 void 
1962 modest_ui_actions_on_delete_folder (GtkAction *action,
1963                                      ModestMainWindow *main_window)
1964 {
1965         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1966
1967         delete_folder (main_window, FALSE);
1968 }
1969
1970 void 
1971 modest_ui_actions_on_move_folder_to_trash_folder (GtkAction *action, ModestMainWindow *main_window)
1972 {
1973         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
1974         
1975         delete_folder (main_window, TRUE);
1976 }
1977
1978 void
1979 modest_ui_actions_on_password_requested (TnyAccountStore *account_store, 
1980                                          const gchar* server_account_name,
1981                                          gchar **username,
1982                                          gchar **password, 
1983                                          gboolean *cancel, 
1984                                          gboolean *remember,
1985                                          ModestMainWindow *main_window)
1986 {
1987         g_return_if_fail(server_account_name);
1988         /* printf("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1989         
1990         /* Initalize output parameters: */
1991         if (cancel)
1992                 *cancel = FALSE;
1993                 
1994         if (remember)
1995                 *remember = TRUE;
1996                 
1997 #ifdef MODEST_PLATFORM_MAEMO
1998         /* Maemo uses a different (awkward) button order,
1999          * It should probably just use gtk_alternative_dialog_button_order ().
2000          */
2001         GtkWidget *dialog = gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
2002                                               NULL,
2003                                               GTK_DIALOG_MODAL,
2004                                               GTK_STOCK_OK,
2005                                               GTK_RESPONSE_ACCEPT,
2006                                               GTK_STOCK_CANCEL,
2007                                               GTK_RESPONSE_REJECT,
2008                                               NULL);
2009 #else
2010         GtkWidget *dialog = gtk_dialog_new_with_buttons (_("mail_ti_password_protected"),
2011                                               NULL,
2012                                               GTK_DIALOG_MODAL,
2013                                               GTK_STOCK_CANCEL,
2014                                               GTK_RESPONSE_REJECT,
2015                                               GTK_STOCK_OK,
2016                                               GTK_RESPONSE_ACCEPT,
2017                                               NULL);
2018 #endif /* MODEST_PLATFORM_MAEMO */
2019
2020         gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(main_window));
2021         
2022         gchar *server_name = modest_server_account_get_hostname (
2023                 modest_runtime_get_account_mgr(), server_account_name);
2024         if (!server_name) {/* This happened once, though I don't know why. murrayc. */
2025                 g_warning("%s: Could not get server name for server account '%s'", __FUNCTION__, server_account_name);
2026                 *cancel = TRUE;
2027                 return;
2028         }
2029         
2030         /* This causes a warning because the logical ID has no %s in it, 
2031          * though the translation does, but there is not much we can do about that: */
2032         gchar *txt = g_strdup_printf (_("mail_ia_password_info"), server_name);
2033         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), gtk_label_new(txt),
2034                             FALSE, FALSE, 0);
2035         g_free (txt);
2036         g_free (server_name);
2037         server_name = NULL;
2038
2039         /* username: */
2040         gchar *initial_username = modest_server_account_get_username (
2041                 modest_runtime_get_account_mgr(), server_account_name);
2042         
2043         GtkWidget *entry_username = gtk_entry_new ();
2044         if (initial_username)
2045                 gtk_entry_set_text (GTK_ENTRY (entry_username), initial_username);
2046         /* Dim this if a connection has ever succeeded with this username,
2047          * as per the UI spec: */
2048         const gboolean username_known = 
2049                 modest_server_account_get_username_has_succeeded(
2050                         modest_runtime_get_account_mgr(), server_account_name);
2051         gtk_widget_set_sensitive (entry_username, !username_known);
2052         
2053 #ifdef MODEST_PLATFORM_MAEMO
2054         /* Auto-capitalization is the default, so let's turn it off: */
2055         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_username), HILDON_GTK_INPUT_MODE_FULL);
2056         
2057         /* Create a size group to be used by all captions.
2058          * Note that HildonCaption does not create a default size group if we do not specify one.
2059          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
2060         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
2061         
2062         GtkWidget *caption = hildon_caption_new (sizegroup, 
2063                 _("mail_fi_username"), entry_username, NULL, HILDON_CAPTION_MANDATORY);
2064         gtk_widget_show (entry_username);
2065         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption, 
2066                 FALSE, FALSE, MODEST_MARGIN_HALF);
2067         gtk_widget_show (caption);
2068 #else 
2069         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_username,
2070                             TRUE, FALSE, 0);
2071 #endif /* MODEST_PLATFORM_MAEMO */      
2072                             
2073         /* password: */
2074         GtkWidget *entry_password = gtk_entry_new ();
2075         gtk_entry_set_visibility (GTK_ENTRY(entry_password), FALSE);
2076         /* gtk_entry_set_invisible_char (GTK_ENTRY(entry_password), "*"); */
2077         
2078 #ifdef MODEST_PLATFORM_MAEMO
2079         /* Auto-capitalization is the default, so let's turn it off: */
2080         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry_password), 
2081                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
2082         
2083         caption = hildon_caption_new (sizegroup, 
2084                 _("mail_fi_password"), entry_password, NULL, HILDON_CAPTION_MANDATORY);
2085         gtk_widget_show (entry_password);
2086         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), caption, 
2087                 FALSE, FALSE, MODEST_MARGIN_HALF);
2088         gtk_widget_show (caption);
2089         g_object_unref (sizegroup);
2090 #else 
2091         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry_password,
2092                             TRUE, FALSE, 0);
2093 #endif /* MODEST_PLATFORM_MAEMO */      
2094                                 
2095 /* This is not in the Maemo UI spec:
2096         remember_pass_check = gtk_check_button_new_with_label (_("Remember password"));
2097         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), remember_pass_check,
2098                             TRUE, FALSE, 0);
2099 */
2100
2101         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
2102         
2103         if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
2104                 if (username) {
2105                         *username = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_username)));
2106                         
2107                         modest_server_account_set_username (
2108                                  modest_runtime_get_account_mgr(), server_account_name, 
2109                                  *username);
2110                                  
2111                         const gboolean username_was_changed = 
2112                                 (strcmp (*username, initial_username) != 0);
2113                         if (username_was_changed) {
2114                                 g_warning ("%s: tinymail does not yet support changing the "
2115                                         "username in the get_password() callback.\n", __FUNCTION__);
2116                         }
2117                 }
2118                         
2119                 if (password) {
2120                         *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_password)));
2121                         
2122                         /* We do not save the password in the configuration, 
2123                          * because this function is only called for passwords that should 
2124                          * not be remembered:
2125                         modest_server_account_set_password (
2126                                  modest_runtime_get_account_mgr(), server_account_name, 
2127                                  *password);
2128                         */
2129                 }
2130                 
2131                 if (cancel)
2132                         *cancel   = FALSE;
2133                         
2134         } else {
2135                 if (username)
2136                         *username = NULL;
2137                         
2138                 if (password)
2139                         *password = NULL;
2140                         
2141                 if (cancel)
2142                         *cancel   = TRUE;
2143         }
2144
2145 /* This is not in the Maemo UI spec:
2146         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (remember_pass_check)))
2147                 *remember = TRUE;
2148         else
2149                 *remember = FALSE;
2150 */
2151
2152         gtk_widget_destroy (dialog);
2153         
2154         /* printf ("DEBUG: %s: cancel=%d\n", __FUNCTION__, *cancel); */
2155 }
2156
2157 void
2158 modest_ui_actions_on_cut (GtkAction *action,
2159                           ModestWindow *window)
2160 {
2161         GtkWidget *focused_widget;
2162
2163         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
2164         if (GTK_IS_EDITABLE (focused_widget)) {
2165                 gtk_editable_cut_clipboard (GTK_EDITABLE(focused_widget));
2166         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
2167                 GtkTextBuffer *buffer;
2168                 GtkClipboard *clipboard;
2169
2170                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2171                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
2172                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2173         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
2174                 modest_header_view_cut_selection (MODEST_HEADER_VIEW (focused_widget));
2175         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
2176                 modest_folder_view_cut_selection (MODEST_FOLDER_VIEW (focused_widget));
2177         }
2178 }
2179
2180 void
2181 modest_ui_actions_on_copy (GtkAction *action,
2182                            ModestWindow *window)
2183 {
2184         GtkClipboard *clipboard;
2185         GtkWidget *focused_widget;
2186
2187         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2188         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
2189
2190         if (GTK_IS_LABEL (focused_widget)) {
2191                 gtk_clipboard_set_text (clipboard, gtk_label_get_text (GTK_LABEL (focused_widget)), -1);
2192         } else if (GTK_IS_EDITABLE (focused_widget)) {
2193                 gtk_editable_copy_clipboard (GTK_EDITABLE(focused_widget));
2194         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
2195                 GtkTextBuffer *buffer;
2196                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
2197                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
2198                 modest_header_view_copy_selection (MODEST_HEADER_VIEW (focused_widget));
2199         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
2200                 TnyList *header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (focused_widget));
2201                 TnyIterator *iter = tny_list_create_iterator (header_list);
2202                 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
2203                 TnyFolder *folder = tny_header_get_folder (header);
2204                 TnyAccount *account = tny_folder_get_account (folder);
2205                 const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (account));
2206                 /* If it's POP then ask */
2207                 gboolean ask = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
2208                        MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
2209                 g_object_unref (account);
2210                 g_object_unref (folder);
2211                 g_object_unref (header);
2212                 g_object_unref (iter);
2213                 
2214                 /* Check that the messages have been previously downloaded */
2215                 gboolean continue_download = TRUE;
2216                 if (ask)
2217                         continue_download = download_uncached_messages (header_list, GTK_WINDOW (window));
2218                 if (continue_download)
2219                         modest_header_view_copy_selection (MODEST_HEADER_VIEW (focused_widget));
2220                 g_object_unref (header_list);
2221         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
2222                 modest_folder_view_copy_selection (MODEST_FOLDER_VIEW (focused_widget));
2223         }    
2224
2225         /* Show information banner */
2226         modest_platform_information_banner (NULL, NULL, _CS("ecoc_ib_edwin_copied"));
2227         
2228 }
2229
2230 void
2231 modest_ui_actions_on_undo (GtkAction *action,
2232                            ModestWindow *window)
2233 {
2234         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
2235                 modest_msg_edit_window_undo (MODEST_MSG_EDIT_WINDOW (window));
2236         } else {
2237                 g_return_if_reached ();
2238         }
2239 }
2240
2241 void
2242 modest_ui_actions_on_paste (GtkAction *action,
2243                             ModestWindow *window)
2244 {
2245         GtkWidget *focused_widget;
2246         ModestMailOperation *mail_op = NULL;
2247
2248         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
2249         if (GTK_IS_EDITABLE (focused_widget)) {
2250                 gtk_editable_paste_clipboard (GTK_EDITABLE(focused_widget));
2251         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
2252                 GtkTextBuffer *buffer;
2253                 GtkClipboard *clipboard;
2254
2255                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2256                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
2257                 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
2258         } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
2259                 ModestEmailClipboard *clipboard = NULL;
2260                 TnyFolder *src_folder = NULL;
2261                 TnyFolderStore *folder_store = NULL;
2262                 TnyList *data = NULL;           
2263                 gboolean delete = FALSE;
2264                 
2265                 /* Check clipboard source */
2266                 clipboard = modest_runtime_get_email_clipboard ();
2267                 if (modest_email_clipboard_cleared (clipboard)) 
2268                         return;
2269                 
2270                 /* Get elements to paste */
2271                 modest_email_clipboard_get_data (clipboard, &src_folder, &data, &delete);
2272
2273                 /* Create a new mail operation */
2274                 mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(window));
2275                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
2276                                                  mail_op);
2277                 
2278                 /* Get destination folder */
2279                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (focused_widget));
2280
2281                 /* transfer messages  */
2282                 if (data != NULL) {
2283                         modest_mail_operation_xfer_msgs (mail_op, 
2284                                                          data,
2285                                                          TNY_FOLDER (folder_store),
2286                                                          delete,
2287                                                          NULL,
2288                                                          NULL);
2289                         
2290                 } else if (src_folder != NULL) {                        
2291                         modest_mail_operation_xfer_folder (mail_op, 
2292                                                            src_folder,
2293                                                            folder_store,
2294                                                            delete);
2295                 }
2296
2297                 /* Free */
2298                 if (data != NULL) 
2299                         g_object_unref (data);
2300                 if (src_folder != NULL) 
2301                         g_object_unref (src_folder);
2302                 if (folder_store != NULL) 
2303                         g_object_unref (folder_store);
2304         }
2305 }
2306
2307 void
2308 modest_ui_actions_on_select_all (GtkAction *action,
2309                                  ModestWindow *window)
2310 {
2311         GtkWidget *focused_widget;
2312
2313         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
2314         if (MODEST_IS_ATTACHMENTS_VIEW (focused_widget)) {
2315                 modest_attachments_view_select_all (MODEST_ATTACHMENTS_VIEW (focused_widget));
2316         } else if (GTK_IS_LABEL (focused_widget)) {
2317                 gtk_label_select_region (GTK_LABEL (focused_widget), 0, -1);
2318         } else if (GTK_IS_EDITABLE (focused_widget)) {
2319                 gtk_editable_select_region (GTK_EDITABLE(focused_widget), 0, -1);
2320         } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
2321                 GtkTextBuffer *buffer;
2322                 GtkTextIter start, end;
2323
2324                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
2325                 gtk_text_buffer_get_start_iter (buffer, &start);
2326                 gtk_text_buffer_get_end_iter (buffer, &end);
2327                 gtk_text_buffer_select_range (buffer, &start, &end);
2328         }
2329         else if ((MODEST_IS_FOLDER_VIEW (focused_widget)) ||
2330                  (MODEST_IS_HEADER_VIEW (focused_widget))) {
2331                 
2332                 GtkTreeSelection *selection = NULL;
2333
2334                 /* Get header view */           
2335                 GtkWidget *header_view = focused_widget;
2336                 if (MODEST_IS_FOLDER_VIEW (focused_widget))
2337                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (window),
2338                                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
2339
2340                 /* Select all messages */
2341                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(header_view));
2342                 gtk_tree_selection_select_all (selection);
2343
2344                 /* Set focuse on header view */
2345                 gtk_widget_grab_focus (header_view);
2346
2347         } else if (GTK_IS_HTML (focused_widget)) {
2348                 gtk_html_select_all (GTK_HTML (focused_widget));
2349         }
2350 }
2351
2352 void
2353 modest_ui_actions_on_mark_as_read (GtkAction *action,
2354                                    ModestWindow *window)
2355 {       
2356         g_return_if_fail (MODEST_IS_WINDOW(window));
2357                 
2358         /* Mark each header as read */
2359         do_headers_action (window, headers_action_mark_as_read, NULL);
2360 }
2361
2362 void
2363 modest_ui_actions_on_mark_as_unread (GtkAction *action,
2364                                      ModestWindow *window)
2365 {       
2366         g_return_if_fail (MODEST_IS_WINDOW(window));
2367                 
2368         /* Mark each header as read */
2369         do_headers_action (window, headers_action_mark_as_unread, NULL);
2370 }
2371
2372 void
2373 modest_ui_actions_on_change_zoom (GtkRadioAction *action,
2374                                   GtkRadioAction *selected,
2375                                   ModestWindow *window)
2376 {
2377         gint value;
2378
2379         value = gtk_radio_action_get_current_value (selected);
2380         if (MODEST_IS_WINDOW (window)) {
2381                 modest_window_set_zoom (MODEST_WINDOW (window), ((gdouble)value)/100);
2382         }
2383 }
2384
2385 void     modest_ui_actions_msg_edit_on_change_priority (GtkRadioAction *action,
2386                                                         GtkRadioAction *selected,
2387                                                         ModestWindow *window)
2388 {
2389         TnyHeaderFlags flags;
2390         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2391
2392         flags = gtk_radio_action_get_current_value (selected);
2393         modest_msg_edit_window_set_priority_flags (MODEST_MSG_EDIT_WINDOW (window), flags);
2394 }
2395
2396 void     modest_ui_actions_msg_edit_on_change_file_format (GtkRadioAction *action,
2397                                                            GtkRadioAction *selected,
2398                                                            ModestWindow *window)
2399 {
2400         gint file_format;
2401
2402         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2403
2404         file_format = gtk_radio_action_get_current_value (selected);
2405         modest_msg_edit_window_set_file_format (MODEST_MSG_EDIT_WINDOW (window), file_format);
2406 }
2407
2408
2409 void     
2410 modest_ui_actions_on_zoom_plus (GtkAction *action,
2411                                 ModestWindow *window)
2412 {
2413         g_return_if_fail (MODEST_IS_WINDOW (window));
2414
2415         modest_window_zoom_plus (MODEST_WINDOW (window));
2416 }
2417
2418 void     
2419 modest_ui_actions_on_zoom_minus (GtkAction *action,
2420                                  ModestWindow *window)
2421 {
2422         g_return_if_fail (MODEST_IS_WINDOW (window));
2423
2424         modest_window_zoom_minus (MODEST_WINDOW (window));
2425 }
2426
2427 void     
2428 modest_ui_actions_on_toggle_fullscreen    (GtkToggleAction *toggle,
2429                                            ModestWindow *window)
2430 {
2431         ModestWindowMgr *mgr;
2432         gboolean fullscreen, active;
2433         g_return_if_fail (MODEST_IS_WINDOW (window));
2434
2435         mgr = modest_runtime_get_window_mgr ();
2436
2437         active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle)))?1:0;
2438         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
2439
2440         if (active != fullscreen) {
2441                 modest_window_mgr_set_fullscreen_mode (mgr, active);
2442                 gtk_window_present (GTK_WINDOW (window));
2443         }
2444 }
2445
2446 void
2447 modest_ui_actions_on_change_fullscreen (GtkAction *action,
2448                                         ModestWindow *window)
2449 {
2450         ModestWindowMgr *mgr;
2451         gboolean fullscreen;
2452
2453         g_return_if_fail (MODEST_IS_WINDOW (window));
2454
2455         mgr = modest_runtime_get_window_mgr ();
2456         fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
2457         modest_window_mgr_set_fullscreen_mode (mgr, !fullscreen);
2458
2459         gtk_window_present (GTK_WINDOW (window));
2460 }
2461
2462 /* 
2463  * Used by modest_ui_actions_on_details to call do_headers_action 
2464  */
2465 static void
2466 headers_action_show_details (TnyHeader *header, 
2467                              ModestWindow *window,
2468                              gpointer user_data)
2469
2470 {
2471         GtkWidget *dialog;
2472         
2473         /* Create dialog */
2474         dialog = modest_details_dialog_new_with_header (GTK_WINDOW (window), header);
2475
2476         /* Run dialog */
2477         gtk_widget_show_all (dialog);
2478         gtk_dialog_run (GTK_DIALOG (dialog));
2479
2480         gtk_widget_destroy (dialog);
2481 }
2482
2483 /*
2484  * Show the folder details in a ModestDetailsDialog widget
2485  */
2486 static void
2487 show_folder_details (TnyFolder *folder, 
2488                      GtkWindow *window)
2489 {
2490         GtkWidget *dialog;
2491         
2492         /* Create dialog */
2493         dialog = modest_details_dialog_new_with_folder (window, folder);
2494
2495         /* Run dialog */
2496         gtk_widget_show_all (dialog);
2497         gtk_dialog_run (GTK_DIALOG (dialog));
2498
2499         gtk_widget_destroy (dialog);
2500 }
2501
2502 /*
2503  * Show the header details in a ModestDetailsDialog widget
2504  */
2505 void     
2506 modest_ui_actions_on_details (GtkAction *action, 
2507                               ModestWindow *win)
2508 {
2509         TnyList * headers_list;
2510         TnyIterator *iter;
2511         TnyHeader *header;              
2512
2513         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2514                 TnyMsg *msg;
2515
2516                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2517                 if (!msg)
2518                         return;
2519                 g_object_unref (msg);           
2520
2521                 headers_list = get_selected_headers (win);
2522                 if (!headers_list)
2523                         return;
2524
2525                 iter = tny_list_create_iterator (headers_list);
2526
2527                 header = TNY_HEADER (tny_iterator_get_current (iter));
2528                 headers_action_show_details (header, win, NULL);
2529                 g_object_unref (header);
2530
2531                 g_object_unref (iter);
2532                 g_object_unref (headers_list);
2533
2534         } else if (MODEST_IS_MAIN_WINDOW (win)) {
2535                 GtkWidget *folder_view, *header_view;
2536
2537                 /* Check which widget has the focus */
2538                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2539                                                                     MODEST_WIDGET_TYPE_FOLDER_VIEW);
2540                 if (gtk_widget_is_focus (folder_view)) {
2541                         TnyFolderStore *folder_store
2542                                 = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2543                         if (!folder_store) {
2544                                 g_warning ("%s: No item was selected.\n", __FUNCTION__);
2545                                 return; 
2546                         }
2547                         /* Show only when it's a folder */
2548                         /* This function should not be called for account items, 
2549                          * because we dim the menu item for them. */
2550                         if (TNY_IS_FOLDER (folder_store)) {
2551                                 show_folder_details (TNY_FOLDER (folder_store), GTK_WINDOW (win));
2552                         }
2553
2554                         g_object_unref (folder_store);
2555
2556                 } else {
2557                         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2558                                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
2559                         /* Show details of each header */
2560                         do_headers_action (win, headers_action_show_details, header_view);
2561                 }
2562         }
2563 }
2564
2565 void     
2566 modest_ui_actions_on_toggle_show_cc (GtkToggleAction *toggle,
2567                                      ModestMsgEditWindow *window)
2568 {
2569         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2570
2571         modest_msg_edit_window_show_cc (window, gtk_toggle_action_get_active (toggle));
2572 }
2573
2574 void     
2575 modest_ui_actions_on_toggle_show_bcc (GtkToggleAction *toggle,
2576                                       ModestMsgEditWindow *window)
2577 {
2578         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2579
2580         modest_msg_edit_window_show_bcc (window, gtk_toggle_action_get_active (toggle));
2581 }
2582
2583 void
2584 modest_ui_actions_toggle_folders_view (GtkAction *action, 
2585                                        ModestMainWindow *main_window)
2586 {
2587         ModestConf *conf;
2588         
2589         g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
2590
2591         conf = modest_runtime_get_conf ();
2592         
2593         if (modest_main_window_get_style (main_window) == MODEST_MAIN_WINDOW_STYLE_SPLIT)
2594                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SIMPLE);
2595         else
2596                 modest_main_window_set_style (main_window, MODEST_MAIN_WINDOW_STYLE_SPLIT);
2597 }
2598
2599 void 
2600 modest_ui_actions_on_toggle_toolbar (GtkToggleAction *toggle, 
2601                                      ModestWindow *window)
2602 {
2603         gboolean active, fullscreen = FALSE;
2604         ModestWindowMgr *mgr;
2605
2606         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (toggle));
2607
2608         /* Check if we want to toggle the toolbar vuew in fullscreen
2609            or normal mode */
2610         if (!strcmp (gtk_action_get_name (GTK_ACTION (toggle)), 
2611                      "ViewShowToolbarFullScreen")) {
2612                 fullscreen = TRUE;
2613         }
2614
2615         /* Toggle toolbar */
2616         mgr = modest_runtime_get_window_mgr ();
2617         modest_window_mgr_show_toolbars (mgr, active, fullscreen);
2618 }
2619
2620 void     
2621 modest_ui_actions_msg_edit_on_select_font (GtkAction *action,
2622                                            ModestMsgEditWindow *window)
2623 {
2624         modest_msg_edit_window_select_font (window);
2625 }
2626
2627 void
2628 modest_ui_actions_on_folder_display_name_changed (ModestFolderView *folder_view,
2629                                                   const gchar *display_name,
2630                                                   GtkWindow *window)
2631 {
2632         /* Do not change the application name if the widget has not
2633            the focus. This callback could be called even if the folder
2634            view has not the focus, because the handled signal could be
2635            emitted when the folder view is redrawn */
2636         if (gtk_widget_is_focus (GTK_WIDGET (folder_view))) {
2637                 if (display_name)
2638                         gtk_window_set_title (window, display_name);
2639                 else
2640                         gtk_window_set_title (window, " ");
2641         }
2642 }
2643
2644 void
2645 modest_ui_actions_on_select_contacts (GtkAction *action, ModestMsgEditWindow *window)
2646 {
2647         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2648         modest_msg_edit_window_select_contacts (window);
2649 }
2650
2651 void
2652 modest_ui_actions_on_check_names (GtkAction *action, ModestMsgEditWindow *window)
2653 {
2654         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2655         modest_msg_edit_window_check_names (window);
2656 }
2657
2658
2659 static GtkWidget*
2660 create_move_to_dialog (ModestWindow *win,
2661                        GtkWidget *folder_view,
2662                        GtkWidget **tree_view)
2663 {
2664         GtkWidget *dialog, *scroll;
2665
2666         dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
2667                                               GTK_WINDOW (win),
2668                                               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT,
2669                                               GTK_STOCK_OK,
2670                                               GTK_RESPONSE_ACCEPT,
2671                                               GTK_STOCK_CANCEL,
2672                                               GTK_RESPONSE_REJECT,
2673                                               NULL);
2674
2675         /* Create scrolled window */
2676         scroll = gtk_scrolled_window_new (NULL, NULL);
2677         gtk_scrolled_window_set_policy  (GTK_SCROLLED_WINDOW (scroll),
2678                                          GTK_POLICY_AUTOMATIC,
2679                                          GTK_POLICY_AUTOMATIC);
2680
2681         /* Create folder view */
2682         *tree_view = modest_platform_create_folder_view (NULL);
2683
2684         /* It could happen that we're trying to move a message from a
2685            window (msg window for example) after the main window was
2686            closed, so we can not just get the model of the folder
2687            view */
2688         if (MODEST_IS_FOLDER_VIEW (folder_view))
2689                 gtk_tree_view_set_model (GTK_TREE_VIEW (*tree_view),
2690                                          gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view)));
2691         else
2692                 modest_folder_view_update_model (MODEST_FOLDER_VIEW (*tree_view), 
2693                                                  TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
2694
2695         gtk_container_add (GTK_CONTAINER (scroll), *tree_view);
2696
2697         /* Add scroll to dialog */
2698         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
2699                             scroll, FALSE, FALSE, 0);
2700
2701         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
2702
2703         /* Select INBOX or local account */
2704         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (*tree_view));
2705
2706         return dialog;
2707 }
2708
2709 /*
2710  * Returns TRUE if at least one of the headers of the list belongs to
2711  * a message that has been fully retrieved.
2712  */
2713 static gboolean
2714 has_retrieved_msgs (TnyList *list)
2715 {
2716         TnyIterator *iter;
2717         gboolean found = FALSE;
2718
2719         iter = tny_list_create_iterator (list);
2720         while (tny_iterator_is_done (iter) && !found) {
2721                 TnyHeader *header;
2722                 TnyHeaderFlags flags;
2723
2724                 header = TNY_HEADER (tny_iterator_get_current (iter));
2725                 flags = tny_header_get_flags (header);
2726                 if (!(flags & TNY_HEADER_FLAG_PARTIAL))
2727                         found = TRUE;
2728
2729                 if (!found)
2730                         tny_iterator_next (iter);
2731         }
2732         g_object_unref (iter);
2733
2734         return found;
2735 }
2736
2737 /*
2738  * Shows a confirmation dialog to the user when we're moving messages
2739  * from a remote server to the local storage. Returns the dialog
2740  * response. If it's other kind of movement the it always returns
2741  * GTK_RESPONSE_OK
2742  */
2743 static gint
2744 msgs_move_to_confirmation (GtkWindow *win,
2745                            TnyFolder *dest_folder,
2746                            TnyList *headers)
2747 {
2748         gint response = GTK_RESPONSE_OK;
2749
2750         /* If the destination is a local folder */
2751         if (modest_tny_folder_is_local_folder (dest_folder)) {
2752                 TnyFolder *src_folder;
2753                 TnyIterator *iter;
2754                 TnyHeader *header;
2755
2756                 /* Get source folder */
2757                 iter = tny_list_create_iterator (headers);
2758                 header = TNY_HEADER (tny_iterator_get_current (iter));
2759                 src_folder = tny_header_get_folder (header);
2760                 g_object_unref (header);
2761                 g_object_unref (iter);
2762
2763                 /* if no src_folder, message may be an attahcment */
2764                 if (src_folder == NULL) 
2765                         return GTK_RESPONSE_CANCEL;
2766
2767                 /* If the source is a remote folder */
2768                 if (!modest_tny_folder_is_local_folder (src_folder)) {
2769                         const gchar *message;
2770                         
2771                         if (has_retrieved_msgs (headers))
2772                                 message = ngettext ("mcen_nc_move_retrieve", "mcen_nc_move_retrieves",
2773                                                     tny_list_get_length (headers));
2774                         else 
2775                                 message = ngettext ("mcen_nc_move_header", "mcen_nc_move_headers",
2776                                                     tny_list_get_length (headers));
2777
2778                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
2779                                                                             (const gchar *) message);
2780                 }
2781                 g_object_unref (src_folder);
2782         }
2783         return response;
2784 }
2785
2786
2787 static void
2788 transfer_msgs_from_viewer_cb (const GObject *object, gpointer user_data)
2789 {
2790         ModestMsgViewWindow *self = NULL;
2791
2792         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (object));
2793         self = MODEST_MSG_VIEW_WINDOW (object);
2794
2795         /* If there are not more messages don't do anything. The
2796            viewer will show the same message */
2797         if (!modest_msg_view_window_select_first_message (self))
2798             return;
2799 }
2800
2801 void
2802 modest_ui_actions_move_folder_error_handler (ModestMailOperation *mail_op, 
2803                                              gpointer user_data)
2804 {
2805         GObject *win = modest_mail_operation_get_source (mail_op);
2806
2807         /* TODO: show error message */
2808         modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
2809                                                 _("mail_in_ui_folder_move_target_error"));
2810         g_object_unref (win);
2811 }
2812
2813 /*
2814  * UI handler for the "Move to" action when invoked from the
2815  * ModestMainWindow
2816  */
2817 static void 
2818 modest_ui_actions_on_main_window_move_to (GtkAction *action, 
2819                                           ModestMainWindow *win)
2820 {
2821         GtkWidget *dialog = NULL, *folder_view = NULL, *tree_view = NULL;
2822         GtkWidget *header_view = NULL;
2823         gint result;
2824         TnyFolderStore *folder_store = NULL;
2825         ModestMailOperation *mail_op = NULL;
2826
2827         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
2828
2829         /* Get the folder view */
2830         folder_view = modest_main_window_get_child_widget (win,
2831                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
2832
2833         /* Get header view */
2834         header_view = modest_main_window_get_child_widget (win,
2835                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
2836
2837         /* Create and run the dialog */
2838         dialog = create_move_to_dialog (MODEST_WINDOW (win), folder_view, &tree_view);
2839         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (tree_view));
2840         result = gtk_dialog_run (GTK_DIALOG(dialog));
2841         g_object_ref (tree_view);
2842
2843         /* We do this to save an indentation level ;-) */
2844         if (result != GTK_RESPONSE_ACCEPT)
2845                 goto end;
2846
2847         folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
2848
2849         if (TNY_IS_ACCOUNT (folder_store))
2850                 goto end;
2851
2852         /* Get folder or messages to transfer */
2853         if (gtk_widget_is_focus (folder_view)) {
2854                 TnyFolderStore *src_folder;
2855                 src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2856
2857                 /* Clean folder on header view before moving it */
2858                 modest_header_view_clear (MODEST_HEADER_VIEW (header_view)); 
2859
2860                 if (TNY_IS_FOLDER (src_folder)) {
2861                         mail_op = 
2862                                 modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
2863                                                                                G_OBJECT(win),
2864                                                                                modest_ui_actions_move_folder_error_handler,
2865                                                                                NULL);
2866                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
2867
2868                         modest_mail_operation_xfer_folder (mail_op, 
2869                                                            TNY_FOLDER (src_folder),
2870                                                            folder_store,
2871                                                            TRUE);
2872                         /* Unref mail operation */
2873                         g_object_unref (G_OBJECT (mail_op));
2874                 }
2875
2876                 /* Frees */
2877                 g_object_unref (G_OBJECT (src_folder));
2878         } else {
2879                 if (gtk_widget_is_focus (header_view)) {
2880                         TnyList *headers;
2881                         gint response;
2882
2883                         headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
2884
2885                         /* Ask for user confirmation */
2886                         response = msgs_move_to_confirmation (GTK_WINDOW (win), 
2887                                                               TNY_FOLDER (folder_store), 
2888                                                               headers);
2889
2890                         /* Transfer messages */
2891                         if (response == GTK_RESPONSE_OK) {
2892                                 mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(win));
2893                                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
2894                                                                  mail_op);
2895
2896                                 modest_mail_operation_xfer_msgs (mail_op, 
2897                                                                  headers,
2898                                                                  TNY_FOLDER (folder_store),
2899                                                                  TRUE,
2900                                                                  NULL,
2901                                                                  NULL);
2902
2903                                 g_object_unref (G_OBJECT (mail_op));
2904                         }
2905                         g_object_unref (headers);
2906                 }
2907         }
2908  end:
2909         if (folder_store != NULL)
2910                 g_object_unref (folder_store);
2911         gtk_widget_destroy (dialog);
2912 }
2913
2914
2915 /*
2916  * UI handler for the "Move to" action when invoked from the
2917  * ModestMsgViewWindow
2918  */
2919 static void 
2920 modest_ui_actions_on_msg_view_window_move_to (GtkAction *action, 
2921                                               ModestMsgViewWindow *win)
2922 {
2923         GtkWidget *dialog, *folder_view, *tree_view = NULL;
2924         gint result;
2925         ModestMainWindow *main_window;
2926         TnyHeader *header;
2927         TnyList *headers;
2928
2929         /* Get the folder view */
2930         main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
2931         if (main_window)
2932                 folder_view = modest_main_window_get_child_widget (main_window,
2933                                                                    MODEST_WIDGET_TYPE_FOLDER_VIEW);
2934         else
2935                 folder_view = NULL;
2936
2937         /* Create and run the dialog */
2938         dialog = create_move_to_dialog (MODEST_WINDOW (win), folder_view, &tree_view);  
2939         result = gtk_dialog_run (GTK_DIALOG(dialog));
2940         g_object_ref (tree_view);
2941
2942         if (result == GTK_RESPONSE_ACCEPT) {
2943                 TnyFolderStore *folder_store;
2944                 gint response;
2945
2946                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
2947
2948                 /* Create header list */
2949                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));              
2950                         
2951                 headers = tny_simple_list_new ();
2952                 tny_list_prepend (headers, G_OBJECT (header));
2953                 g_object_unref (header);
2954
2955                 /* Ask user for confirmation. MSG-NOT404 */
2956                 response = msgs_move_to_confirmation (GTK_WINDOW (win), 
2957                                                       TNY_FOLDER (folder_store), 
2958                                                       headers);
2959
2960                 /* Transfer current msg */
2961                 if (response == GTK_RESPONSE_OK) {
2962                         ModestMailOperation *mail_op;
2963
2964                         /* Create mail op */
2965                         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(win));
2966                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
2967                                                          mail_op);
2968                         
2969                         /* Transfer messages */
2970                         modest_mail_operation_xfer_msgs (mail_op, 
2971                                                          headers,
2972                                                          TNY_FOLDER (folder_store),
2973                                                          TRUE,
2974                                                          transfer_msgs_from_viewer_cb,
2975                                                          NULL);
2976                         g_object_unref (G_OBJECT (mail_op));
2977                 }
2978                 g_object_unref (headers);
2979                 g_object_unref (folder_store);
2980         }
2981         gtk_widget_destroy (dialog);
2982 }
2983
2984 void 
2985 modest_ui_actions_on_move_to (GtkAction *action, 
2986                               ModestWindow *win)
2987 {
2988         g_return_if_fail (MODEST_IS_MAIN_WINDOW (win) ||
2989                           MODEST_IS_MSG_VIEW_WINDOW (win));
2990
2991         if (MODEST_IS_MAIN_WINDOW (win)) 
2992                 modest_ui_actions_on_main_window_move_to (action, 
2993                                                           MODEST_MAIN_WINDOW (win));
2994         else
2995                 modest_ui_actions_on_msg_view_window_move_to (action, 
2996                                                               MODEST_MSG_VIEW_WINDOW (win));
2997 }
2998
2999 /*
3000  * Calls #HeadersFunc for each header already selected in the main
3001  * window or the message currently being shown in the msg view window
3002  */
3003 static void
3004 do_headers_action (ModestWindow *win, 
3005                    HeadersFunc func,
3006                    gpointer user_data)
3007 {
3008         TnyList *headers_list;
3009         TnyIterator *iter;
3010         TnyHeader *header;
3011         TnyFolder *folder;
3012
3013         /* Get headers */
3014         headers_list = get_selected_headers (win);
3015         if (!headers_list)
3016                 return;
3017
3018         /* Get the folder */
3019         iter = tny_list_create_iterator (headers_list);
3020         header = TNY_HEADER (tny_iterator_get_current (iter));
3021         folder = tny_header_get_folder (header);
3022         g_object_unref (header);
3023
3024         /* Call the function for each header */
3025         while (!tny_iterator_is_done (iter)) {
3026                 header = TNY_HEADER (tny_iterator_get_current (iter));
3027                 func (header, win, user_data);
3028                 g_object_unref (header);
3029                 tny_iterator_next (iter);
3030         }
3031
3032         /* Trick: do a poke status in order to speed up the signaling
3033            of observers */
3034         tny_folder_poke_status (folder);
3035
3036         /* Frees */
3037         g_object_unref (folder);
3038         g_object_unref (iter);
3039         g_object_unref (headers_list);
3040 }
3041
3042 void 
3043 modest_ui_actions_view_attachment (GtkAction *action,
3044                                    ModestWindow *window)
3045 {
3046         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
3047                 modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (window), NULL);
3048         } else {
3049                 /* not supported window for this action */
3050                 g_return_if_reached ();
3051         }
3052 }
3053
3054 void
3055 modest_ui_actions_save_attachments (GtkAction *action,
3056                                     ModestWindow *window)
3057 {
3058         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
3059                 modest_msg_view_window_save_attachments (MODEST_MSG_VIEW_WINDOW (window), NULL);
3060         } else {
3061                 /* not supported window for this action */
3062                 g_return_if_reached ();
3063         }
3064 }
3065
3066 void
3067 modest_ui_actions_remove_attachments (GtkAction *action,
3068                                       ModestWindow *window)
3069 {
3070         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
3071                 modest_msg_view_window_remove_attachments (MODEST_MSG_VIEW_WINDOW (window));
3072         } else {
3073                 /* not supported window for this action */
3074                 g_return_if_reached ();
3075         }
3076 }
3077
3078 void 
3079 modest_ui_actions_on_settings (GtkAction *action, 
3080                                ModestWindow *win)
3081 {
3082         GtkWidget *dialog;
3083
3084         dialog = modest_platform_get_global_settings_dialog ();
3085         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (win));
3086         gtk_widget_show_all (dialog);
3087
3088         gtk_dialog_run (GTK_DIALOG (dialog));
3089
3090         gtk_widget_destroy (dialog);
3091 }
3092
3093 void 
3094 modest_ui_actions_on_help (GtkAction *action, 
3095                            ModestWindow *win)
3096 {
3097         const gchar *help_id = NULL;
3098
3099         if (MODEST_IS_MAIN_WINDOW (win)) {
3100                 const gchar *action_name;
3101                 action_name = gtk_action_get_name (action);
3102
3103                 if (!strcmp (action_name, "FolderViewCSMHelp") ||
3104                     !strcmp (action_name, "HeaderViewCSMHelp")) {
3105                         GtkWidget *folder_view;
3106                         TnyFolderStore *folder_store;
3107                         /* Get selected folder */
3108                         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
3109                                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
3110                         folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
3111
3112                         /* Switch help_id */
3113                         if (TNY_IS_FOLDER (folder_store)) {
3114                                 switch (tny_folder_get_folder_type (TNY_FOLDER (folder_store))) {
3115                                 case TNY_FOLDER_TYPE_NORMAL:
3116                                         help_id = "applications_email_userfolder";
3117                                         break;
3118                                 case TNY_FOLDER_TYPE_INBOX:
3119                                         help_id = "applications_email_inbox";
3120                                         break;
3121                                 case TNY_FOLDER_TYPE_OUTBOX:
3122                                         help_id = "applications_email_outbox";
3123                                         break;
3124                                 case TNY_FOLDER_TYPE_SENT:
3125                                         help_id = "applications_email_sent";
3126                                         break;
3127                                 case TNY_FOLDER_TYPE_DRAFTS:
3128                                         help_id = "applications_email_drafts";
3129                                         break;
3130                                 case TNY_FOLDER_TYPE_ARCHIVE:
3131                                         help_id = "applications_email_archive";
3132                                         break;
3133                                 default:
3134                                         help_id = NULL;
3135                                 }
3136                         }
3137                         g_object_unref (folder_store);
3138                 } else {
3139                         help_id = "applications_email_mainview";        
3140                 }
3141         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
3142                 help_id = "applications_email_viewer";
3143         } else if (MODEST_IS_MSG_EDIT_WINDOW (win))
3144                 help_id = "applications_email_editor";
3145
3146         modest_platform_show_help (GTK_WINDOW (win), help_id);
3147 }
3148
3149 void 
3150 modest_ui_actions_on_retrieve_msg_contents (GtkAction *action,
3151                                             ModestWindow *window)
3152 {
3153         ModestMailOperation *mail_op;
3154         TnyList *headers;
3155
3156         /* Get headers */
3157         headers = get_selected_headers (window);
3158         if (!headers)
3159                 return;
3160
3161         /* Create mail operation */
3162         mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
3163                                                                  G_OBJECT (window),
3164                                                                  modest_ui_actions_get_msgs_full_error_handler, 
3165                                                                  NULL);
3166         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
3167         modest_mail_operation_get_msgs_full (mail_op, headers, NULL, NULL, NULL);
3168
3169         /* Frees */
3170         g_object_unref (headers);
3171         g_object_unref (mail_op);
3172 }
3173
3174 void
3175 modest_ui_actions_on_email_menu_activated (GtkAction *action,
3176                                           ModestWindow *window)
3177 {
3178         g_return_if_fail (MODEST_IS_WINDOW (window));
3179
3180         /* Update dimmed */     
3181         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3182 }
3183
3184 void
3185 modest_ui_actions_on_edit_menu_activated (GtkAction *action,
3186                                           ModestWindow *window)
3187 {
3188         g_return_if_fail (MODEST_IS_WINDOW (window));
3189
3190         /* Update dimmed */     
3191         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3192 }
3193
3194 void
3195 modest_ui_actions_on_view_menu_activated (GtkAction *action,
3196                                           ModestWindow *window)
3197 {
3198         g_return_if_fail (MODEST_IS_WINDOW (window));
3199
3200         /* Update dimmed */     
3201         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3202 }
3203
3204 void
3205 modest_ui_actions_on_tools_menu_activated (GtkAction *action,
3206                                           ModestWindow *window)
3207 {
3208         g_return_if_fail (MODEST_IS_WINDOW (window));
3209
3210         /* Update dimmed */     
3211         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3212 }
3213
3214 void
3215 modest_ui_actions_on_attachment_menu_activated (GtkAction *action,
3216                                           ModestWindow *window)
3217 {
3218         g_return_if_fail (MODEST_IS_WINDOW (window));
3219
3220         /* Update dimmed */     
3221         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3222 }
3223
3224 void
3225 modest_ui_actions_on_toolbar_csm_menu_activated (GtkAction *action,
3226                                                  ModestWindow *window)
3227 {
3228         g_return_if_fail (MODEST_IS_WINDOW (window));
3229
3230         /* Update dimmed */     
3231         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3232 }
3233
3234 void
3235 modest_ui_actions_on_folder_view_csm_menu_activated (GtkAction *action,
3236                                                      ModestWindow *window)
3237 {
3238         g_return_if_fail (MODEST_IS_WINDOW (window));
3239
3240         /* Update dimmed */     
3241         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3242 }
3243
3244 void
3245 modest_ui_actions_on_header_view_csm_menu_activated (GtkAction *action,
3246                                                      ModestWindow *window)
3247 {
3248         g_return_if_fail (MODEST_IS_WINDOW (window));
3249
3250         /* Update dimmed */     
3251         modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
3252 }
3253
3254 void
3255 modest_ui_actions_check_toolbar_dimming_rules (ModestWindow *window)
3256 {
3257         g_return_if_fail (MODEST_IS_WINDOW (window));
3258
3259         /* Update dimmed */     
3260         modest_window_check_dimming_rules_group (window, "ModestToolbarDimmingRules");  
3261 }
3262
3263 void
3264 modest_ui_actions_on_search_messages (GtkAction *action, ModestWindow *window)
3265 {
3266         g_return_if_fail (MODEST_IS_WINDOW (window));
3267
3268         modest_platform_show_search_messages (GTK_WINDOW (window));
3269 }
3270
3271 void     
3272 modest_ui_actions_on_open_addressbook (GtkAction *action, ModestWindow *win)
3273 {
3274         g_return_if_fail (MODEST_IS_WINDOW (win));
3275         modest_platform_show_addressbook (GTK_WINDOW (win));
3276 }
3277
3278
3279 void
3280 modest_ui_actions_on_toggle_find_in_page (GtkToggleAction *action,
3281                                           ModestWindow *window)
3282 {
3283         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3284
3285         modest_msg_edit_window_toggle_find_toolbar (MODEST_MSG_EDIT_WINDOW (window), gtk_toggle_action_get_active (action));
3286 }
3287
3288 static void 
3289 _on_send_receive_progress_changed (ModestMailOperation  *mail_op, 
3290                                    ModestMailOperationState *state,
3291                                    gpointer user_data)
3292 {
3293         g_return_if_fail (MODEST_IS_MAIN_WINDOW(user_data));
3294
3295         /* Set send/receive operation finished */       
3296         if (state->status != MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS)
3297                 modest_main_window_notify_send_receive_completed (MODEST_MAIN_WINDOW(user_data));
3298         
3299 }
3300
3301