* Change save_settings call of main and viewer windows to
[modest] / src / maemo / modest-msg-view-window.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 #include <glib/gi18n.h>
30 #include <string.h>
31 #include <tny-account-store.h>
32 #include <tny-simple-list.h>
33 #include <tny-msg.h>
34 #include <tny-mime-part.h>
35 #include <tny-vfs-stream.h>
36 #include "modest-platform.h"
37 #include <modest-maemo-utils.h>
38 #include <modest-tny-msg.h>
39 #include <modest-msg-view-window.h>
40 #include <modest-attachments-view.h>
41 #include <modest-main-window-ui.h>
42 #include "modest-msg-view-window-ui-dimming.h"
43 #include <modest-widget-memory.h>
44 #include <modest-runtime.h>
45 #include <modest-window-priv.h>
46 #include <modest-tny-folder.h>
47 #include <modest-text-utils.h>
48 #include <modest-account-mgr-helpers.h>
49 #include "modest-progress-bar-widget.h"
50 #include "modest-defs.h"
51 #include "modest-hildon-includes.h"
52 #include <gtkhtml/gtkhtml-search.h>
53 #include "modest-ui-dimming-manager.h"
54 #include <gdk/gdkkeysyms.h>
55
56 #define DEFAULT_FOLDER "MyDocs/.documents"
57
58 static void  modest_msg_view_window_class_init   (ModestMsgViewWindowClass *klass);
59 static void  modest_msg_view_window_init         (ModestMsgViewWindow *obj);
60 static void  modest_msg_view_window_finalize     (GObject *obj);
61 static void  modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *obj,
62                                                          gpointer data);
63 static void  modest_msg_view_window_find_toolbar_close (GtkWidget *widget,
64                                                         ModestMsgViewWindow *obj);
65 static void  modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
66                                                         ModestMsgViewWindow *obj);
67
68 static void  modest_msg_view_window_set_zoom (ModestWindow *window,
69                                               gdouble zoom);
70 static gdouble modest_msg_view_window_get_zoom (ModestWindow *window);
71 static gboolean modest_msg_view_window_zoom_minus (ModestWindow *window);
72 static gboolean modest_msg_view_window_zoom_plus (ModestWindow *window);
73 static gboolean modest_msg_view_window_key_release_event (GtkWidget *window,
74                                                           GdkEventKey *event,
75                                                           gpointer userdata);
76 static gboolean modest_msg_view_window_window_state_event (GtkWidget *widget, 
77                                                            GdkEventWindowState *event, 
78                                                            gpointer userdata);
79 static void modest_msg_view_window_scroll_up (ModestWindow *window);
80 static void modest_msg_view_window_scroll_down (ModestWindow *window);
81 static void modest_msg_view_window_update_priority (ModestMsgViewWindow *window);
82
83 static void modest_msg_view_window_show_toolbar   (ModestWindow *window,
84                                                    gboolean show_toolbar);
85
86 static void modest_msg_view_window_clipboard_owner_change (GtkClipboard *clipboard,
87                                                            GdkEvent *event,
88                                                            ModestMsgViewWindow *window);
89
90 static void cancel_progressbar (GtkToolButton *toolbutton,
91                                 ModestMsgViewWindow *self);
92
93 static void         on_queue_changed                     (ModestMailOperationQueue *queue,
94                                                           ModestMailOperation *mail_op,
95                                                           ModestMailOperationQueueNotification type,
96                                                           ModestMsgViewWindow *self);
97
98 static void view_msg_cb (ModestMailOperation *mail_op, TnyHeader *header, TnyMsg *msg, gpointer user_data);
99
100 static void set_toolbar_mode (ModestMsgViewWindow *self, 
101                               ModestToolBarModes mode);
102
103 static gboolean set_toolbar_transfer_mode     (ModestMsgViewWindow *self); 
104
105 static void update_window_title (ModestMsgViewWindow *window);
106
107
108 /* list my signals */
109 enum {
110         /* MY_SIGNAL_1, */
111         /* MY_SIGNAL_2, */
112         LAST_SIGNAL
113 };
114
115 static const GtkToggleActionEntry msg_view_toggle_action_entries [] = {
116         { "FindInMessage",    GTK_STOCK_FIND,    N_("qgn_toolb_gene_find"), NULL, NULL, G_CALLBACK (modest_msg_view_window_toggle_find_toolbar), FALSE },
117         { "ToolsFindInMessage", NULL, N_("mcen_me_viewer_find"), NULL, NULL, G_CALLBACK (modest_msg_view_window_toggle_find_toolbar), FALSE },
118 };
119
120 static const GtkRadioActionEntry msg_view_zoom_action_entries [] = {
121         { "Zoom50", NULL, N_("mcen_me_viewer_50"), NULL, NULL, 50 },
122         { "Zoom80", NULL, N_("mcen_me_viewer_80"), NULL, NULL, 80 },
123         { "Zoom100", NULL, N_("mcen_me_viewer_100"), NULL, NULL, 100 },
124         { "Zoom120", NULL, N_("mcen_me_viewer_120"), NULL, NULL, 120 },
125         { "Zoom150", NULL, N_("mcen_me_viewer_150"), NULL, NULL, 150 },
126         { "Zoom200", NULL, N_("mcen_me_viewer_200"), NULL, NULL, 200 }
127 };
128
129 typedef struct _ModestMsgViewWindowPrivate ModestMsgViewWindowPrivate;
130 struct _ModestMsgViewWindowPrivate {
131
132         GtkWidget   *msg_view;
133         GtkWidget   *main_scroll;
134         GtkWidget   *find_toolbar;
135         gchar       *last_search;
136
137         /* Progress observers */
138         GtkWidget        *progress_bar;
139         GSList           *progress_widgets;
140
141         /* Tollbar items */
142         GtkWidget   *progress_toolitem;
143         GtkWidget   *cancel_toolitem;
144         GtkWidget   *prev_toolitem;
145         GtkWidget   *next_toolitem;
146         ModestToolBarModes current_toolbar_mode;
147
148         /* Optimized view enabled */
149         gboolean optimized_view;
150
151         GtkTreeModel *header_model;
152         GtkTreeRowReference *row_reference;
153
154         guint clipboard_change_handler;
155         guint queue_change_handler;
156
157         guint progress_bar_timeout;
158
159         gchar *msg_uid;
160 };
161
162 #define MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
163                                                     MODEST_TYPE_MSG_VIEW_WINDOW, \
164                                                     ModestMsgViewWindowPrivate))
165 /* globals */
166 static GtkWindowClass *parent_class = NULL;
167
168 /* uncomment the following if you have defined any signals */
169 /* static guint signals[LAST_SIGNAL] = {0}; */
170
171 GType
172 modest_msg_view_window_get_type (void)
173 {
174         static GType my_type = 0;
175         if (!my_type) {
176                 static const GTypeInfo my_info = {
177                         sizeof(ModestMsgViewWindowClass),
178                         NULL,           /* base init */
179                         NULL,           /* base finalize */
180                         (GClassInitFunc) modest_msg_view_window_class_init,
181                         NULL,           /* class finalize */
182                         NULL,           /* class data */
183                         sizeof(ModestMsgViewWindow),
184                         1,              /* n_preallocs */
185                         (GInstanceInitFunc) modest_msg_view_window_init,
186                         NULL
187                 };
188                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
189                                                   "ModestMsgViewWindow",
190                                                   &my_info, 0);
191         }
192         return my_type;
193 }
194
195 static void
196 save_state (ModestWindow *self)
197 {
198         modest_widget_memory_save (modest_runtime_get_conf (),
199                                    G_OBJECT(self), 
200                                    MODEST_CONF_MSG_VIEW_WINDOW_KEY);
201 }
202
203
204 static void
205 restore_settings (ModestMsgViewWindow *self)
206 {
207         modest_widget_memory_restore (modest_runtime_get_conf (),
208                                       G_OBJECT(self), 
209                                       MODEST_CONF_MSG_VIEW_WINDOW_KEY);
210 }
211
212 static void
213 modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass)
214 {
215         GObjectClass *gobject_class;
216         ModestWindowClass *modest_window_class;
217         gobject_class = (GObjectClass*) klass;
218         modest_window_class = (ModestWindowClass *) klass;
219
220         parent_class            = g_type_class_peek_parent (klass);
221         gobject_class->finalize = modest_msg_view_window_finalize;
222
223         modest_window_class->set_zoom_func = modest_msg_view_window_set_zoom;
224         modest_window_class->get_zoom_func = modest_msg_view_window_get_zoom;
225         modest_window_class->zoom_minus_func = modest_msg_view_window_zoom_minus;
226         modest_window_class->zoom_plus_func = modest_msg_view_window_zoom_plus;
227         modest_window_class->show_toolbar_func = modest_msg_view_window_show_toolbar;
228
229         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewWindowPrivate));
230
231         modest_window_class->save_state_func = save_state;
232 }
233
234 static void
235 modest_msg_view_window_init (ModestMsgViewWindow *obj)
236 {
237         ModestMsgViewWindowPrivate *priv;
238         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
239
240         priv->msg_view      = NULL;
241         priv->header_model  = NULL;
242         priv->clipboard_change_handler = 0;
243         priv->current_toolbar_mode = TOOLBAR_MODE_NORMAL;
244
245         priv->optimized_view  = FALSE;
246         priv->progress_bar_timeout = 0;
247         priv->msg_uid = NULL;
248 }
249
250
251 static gboolean
252 set_toolbar_transfer_mode (ModestMsgViewWindow *self)
253 {
254         ModestMsgViewWindowPrivate *priv = NULL;
255         
256         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
257
258         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
259
260         set_toolbar_mode (self, TOOLBAR_MODE_TRANSFER);
261         
262         if (priv->progress_bar_timeout > 0) {
263                 g_source_remove (priv->progress_bar_timeout);
264                 priv->progress_bar_timeout = 0;
265         }
266         
267         return FALSE;
268 }
269
270 static void 
271 set_toolbar_mode (ModestMsgViewWindow *self, 
272                   ModestToolBarModes mode)
273 {
274         ModestWindowPrivate *parent_priv;
275         ModestMsgViewWindowPrivate *priv;
276 /*      GtkWidget *widget = NULL; */
277
278         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
279
280         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
281         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
282                         
283         /* Sets current toolbar mode */
284         priv->current_toolbar_mode = mode;
285
286         /* Update toolbar dimming state */
287         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (self));
288
289         switch (mode) {
290         case TOOLBAR_MODE_NORMAL:               
291 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarMessageReply"); */
292 /*              gtk_action_set_sensitive (widget, TRUE); */
293 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarDeleteMessage"); */
294 /*              gtk_action_set_sensitive (widget, TRUE); */
295 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarMessageMoveTo"); */
296 /*              gtk_action_set_sensitive (widget, TRUE); */
297 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/FindInMessage"); */
298 /*              gtk_action_set_sensitive (widget, TRUE); */
299
300                 if (priv->prev_toolitem)
301                         gtk_widget_show (priv->prev_toolitem);
302                 
303                 if (priv->next_toolitem)
304                         gtk_widget_show (priv->next_toolitem);
305                         
306                 if (priv->progress_toolitem)
307                         gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), FALSE);
308                 if (priv->progress_bar)
309                         gtk_widget_hide (priv->progress_bar);
310                         
311                 if (priv->cancel_toolitem)
312                         gtk_widget_hide (priv->cancel_toolitem);
313
314                 /* Hide toolbar if optimized view is enabled */
315                 if (priv->optimized_view) {
316                         gtk_widget_set_no_show_all (parent_priv->toolbar, TRUE);
317                         gtk_widget_hide (GTK_WIDGET(parent_priv->toolbar));
318                 }
319
320                 break;
321         case TOOLBAR_MODE_TRANSFER:
322 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarMessageReply"); */
323 /*              gtk_action_set_sensitive (widget, FALSE); */
324 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarDeleteMessage"); */
325 /*              gtk_action_set_sensitive (widget, FALSE); */
326 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarMessageMoveTo"); */
327 /*              gtk_action_set_sensitive (widget, FALSE); */
328 /*              widget = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/FindInMessage"); */
329 /*              gtk_action_set_sensitive (widget, FALSE); */
330
331                 if (priv->prev_toolitem)
332                         gtk_widget_hide (priv->prev_toolitem);
333                 
334                 if (priv->next_toolitem)
335                         gtk_widget_hide (priv->next_toolitem);
336                 
337                 if (priv->progress_toolitem)
338                         gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
339                 if (priv->progress_bar)
340                         gtk_widget_show (priv->progress_bar);
341                         
342                 if (priv->cancel_toolitem)
343                         gtk_widget_show (priv->cancel_toolitem);
344
345                 /* Show toolbar if it's hiden (optimized view ) */
346                 if (priv->optimized_view) {
347                         gtk_widget_set_no_show_all (parent_priv->toolbar, FALSE);
348                         gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
349                 }
350
351                 break;
352         default:
353                 g_return_if_reached ();
354         }
355
356 }
357
358
359 static GtkWidget *
360 menubar_to_menu (GtkUIManager *ui_manager)
361 {
362         GtkWidget *main_menu;
363         GtkWidget *menubar;
364         GList *iter;
365
366         /* Create new main menu */
367         main_menu = gtk_menu_new();
368
369         /* Get the menubar from the UI manager */
370         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
371
372         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
373         while (iter) {
374                 GtkWidget *menu;
375
376                 menu = GTK_WIDGET (iter->data);
377                 gtk_widget_reparent(menu, main_menu);
378
379                 iter = g_list_next (iter);
380         }
381         return main_menu;
382 }
383
384 static void
385 init_window (ModestMsgViewWindow *obj, TnyMsg *msg)
386 {
387         GtkWidget *main_vbox;
388         ModestMsgViewWindowPrivate *priv;
389         ModestWindowPrivate *parent_priv;
390         
391         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
392         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
393
394         priv->msg_view = modest_msg_view_new (msg);
395         modest_msg_view_set_shadow_type (MODEST_MSG_VIEW (priv->msg_view), GTK_SHADOW_NONE);
396         main_vbox = gtk_vbox_new  (FALSE, 6);
397
398         /* Menubar */
399         parent_priv->menubar = menubar_to_menu (parent_priv->ui_manager);
400         gtk_widget_show_all (GTK_WIDGET(parent_priv->menubar));
401         hildon_window_set_menu    (HILDON_WINDOW(obj), GTK_MENU(parent_priv->menubar));
402
403         priv->main_scroll = gtk_scrolled_window_new (NULL, NULL);
404         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->main_scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
405         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->main_scroll), GTK_SHADOW_NONE);
406
407         gtk_container_add (GTK_CONTAINER (priv->main_scroll), priv->msg_view);
408         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_scroll, TRUE, TRUE, 0);
409         gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
410
411         priv->find_toolbar = hildon_find_toolbar_new (NULL);
412         gtk_widget_set_no_show_all (priv->find_toolbar, TRUE);
413         g_signal_connect (G_OBJECT (priv->find_toolbar), "close", G_CALLBACK (modest_msg_view_window_find_toolbar_close), obj);
414         g_signal_connect (G_OBJECT (priv->find_toolbar), "search", G_CALLBACK (modest_msg_view_window_find_toolbar_search), obj);
415         
416         priv->clipboard_change_handler = g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_PRIMARY)), "owner-change", G_CALLBACK (modest_msg_view_window_clipboard_owner_change), obj);
417         gtk_widget_show_all (GTK_WIDGET(main_vbox));
418         gtk_box_pack_end (GTK_BOX (main_vbox), priv->find_toolbar, FALSE, FALSE, 0);
419
420 }       
421
422
423 static void
424 modest_msg_view_window_finalize (GObject *obj)
425 {
426         ModestMsgViewWindowPrivate *priv;
427
428         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (obj);
429         if (priv->clipboard_change_handler > 0) {
430                 g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), priv->clipboard_change_handler);
431                 priv->clipboard_change_handler = 0;
432         }
433         if (priv->queue_change_handler > 0) {
434                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), priv->queue_change_handler);
435                 priv->queue_change_handler = 0;
436         }
437         if (priv->header_model != NULL) {
438                 g_object_unref (priv->header_model);
439                 priv->header_model = NULL;
440         }
441
442         /* disconnet operations queue observer */
443         
444         if (priv->progress_bar_timeout > 0) {
445                 g_source_remove (priv->progress_bar_timeout);
446                 priv->progress_bar_timeout = 0;
447         }
448
449         if (priv->row_reference) {
450                 gtk_tree_row_reference_free (priv->row_reference);
451                 priv->row_reference = NULL;
452         }
453
454         if (priv->msg_uid) {
455                 g_free (priv->msg_uid);
456                 priv->msg_uid = NULL;
457         }
458
459         G_OBJECT_CLASS(parent_class)->finalize (obj);
460 }
461
462
463
464 /* static gboolean */
465 /* on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self) */
466 /* { */
467 /*      modest_window_save_state (MODEST_WINDOW (self)); */
468 /*      return FALSE; */
469 /* } */
470
471 ModestWindow *
472 modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
473                                               const gchar *account_name,
474                                               const gchar *msg_uid,
475                                               GtkTreeModel *model, 
476                                               GtkTreeRowReference *row_reference)
477 {
478         ModestMsgViewWindow *window = NULL;
479         ModestMsgViewWindowPrivate *priv = NULL;
480
481         window = MODEST_MSG_VIEW_WINDOW(modest_msg_view_window_new (msg, account_name, msg_uid));
482         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), NULL);
483
484         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
485
486         g_object_ref (model);
487         priv->header_model = model;
488         priv->row_reference = gtk_tree_row_reference_copy (row_reference);
489
490         modest_msg_view_window_update_priority (window);
491
492         /* Check toolbar dimming rules */
493         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
494
495         return MODEST_WINDOW(window);
496 }
497
498
499 ModestWindow *
500 modest_msg_view_window_new (TnyMsg *msg, 
501                             const gchar *account_name,
502                             const gchar *msg_uid)
503 {
504         ModestMsgViewWindow *self = NULL;
505         GObject *obj = NULL;
506         ModestMsgViewWindowPrivate *priv = NULL;
507         ModestWindowPrivate *parent_priv = NULL;
508         ModestDimmingRulesGroup *menu_rules_group = NULL;
509         ModestDimmingRulesGroup *toolbar_rules_group = NULL;
510         GtkActionGroup *action_group = NULL;
511         GError *error = NULL;
512
513         g_return_val_if_fail (msg, NULL);
514         
515         obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
516         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
517         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
518         self = MODEST_MSG_VIEW_WINDOW (obj);
519
520         priv->msg_uid = g_strdup (msg_uid);
521
522         parent_priv->ui_manager = gtk_ui_manager_new();
523         parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
524
525         action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
526         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
527
528         menu_rules_group = modest_dimming_rules_group_new ("ModestMenuDimmingRules");
529         toolbar_rules_group = modest_dimming_rules_group_new ("ModestToolbarDimmingRules");
530
531         /* Add common actions */
532         gtk_action_group_add_actions (action_group,
533                                       modest_action_entries,
534                                       G_N_ELEMENTS (modest_action_entries),
535                                       obj);
536         gtk_action_group_add_toggle_actions (action_group,
537                                              modest_toggle_action_entries,
538                                              G_N_ELEMENTS (modest_toggle_action_entries),
539                                              obj);
540         gtk_action_group_add_toggle_actions (action_group,
541                                              msg_view_toggle_action_entries,
542                                              G_N_ELEMENTS (msg_view_toggle_action_entries),
543                                              obj);
544         gtk_action_group_add_radio_actions (action_group,
545                                             msg_view_zoom_action_entries,
546                                             G_N_ELEMENTS (msg_view_zoom_action_entries),
547                                             100,
548                                             G_CALLBACK (modest_ui_actions_on_change_zoom),
549                                             obj);
550
551         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
552         g_object_unref (action_group);
553
554         /* Load the UI definition */
555         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-msg-view-window-ui.xml",
556                                          &error);
557         if (error) {
558                 g_printerr ("modest: could not merge modest-msg-view-window-ui.xml: %s\n", error->message);
559                 g_error_free (error);
560                 error = NULL;
561         }
562         /* ****** */
563
564         /* Add common dimming rules */
565         modest_dimming_rules_group_add_rules (menu_rules_group, 
566                                               modest_msg_view_menu_dimming_entries,
567                                               G_N_ELEMENTS (modest_msg_view_menu_dimming_entries),
568                                               self);
569         modest_dimming_rules_group_add_rules (toolbar_rules_group, 
570                                               modest_msg_view_toolbar_dimming_entries,
571                                               G_N_ELEMENTS (modest_msg_view_toolbar_dimming_entries),
572                                               self);
573
574         /* Insert dimming rules group for this window */
575         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, menu_rules_group);
576         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, toolbar_rules_group);
577         g_object_unref (menu_rules_group);
578         g_object_unref (toolbar_rules_group);
579
580         /* Add accelerators */
581         gtk_window_add_accel_group (GTK_WINDOW (obj), 
582                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
583         
584         /* Init window */
585         init_window (MODEST_MSG_VIEW_WINDOW(obj), msg);
586         restore_settings (MODEST_MSG_VIEW_WINDOW(obj));
587         
588 /*      g_signal_connect (G_OBJECT(obj), "delete-event", G_CALLBACK(on_delete_event), obj); */
589
590         g_signal_connect (G_OBJECT(priv->msg_view), "link_clicked",
591                           G_CALLBACK (modest_ui_actions_on_msg_link_clicked), obj);
592         g_signal_connect (G_OBJECT(priv->msg_view), "link_hover",
593                           G_CALLBACK (modest_ui_actions_on_msg_link_hover), obj);
594         g_signal_connect (G_OBJECT(priv->msg_view), "attachment_clicked",
595                           G_CALLBACK (modest_ui_actions_on_msg_attachment_clicked), obj);
596         g_signal_connect (G_OBJECT(priv->msg_view), "recpt_activated",
597                           G_CALLBACK (modest_ui_actions_on_msg_recpt_activated), obj);
598         g_signal_connect (G_OBJECT(priv->msg_view), "link_contextual",
599                           G_CALLBACK (modest_ui_actions_on_msg_link_contextual), obj);
600
601         g_signal_connect (G_OBJECT (obj), "key-release-event",
602                           G_CALLBACK (modest_msg_view_window_key_release_event),
603                           NULL);
604
605         g_signal_connect (G_OBJECT (obj), "window-state-event",
606                           G_CALLBACK (modest_msg_view_window_window_state_event),
607                           NULL);
608
609         /* Mail Operation Queue */
610         priv->queue_change_handler = g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
611                                                        "queue-changed",
612                                                        G_CALLBACK (on_queue_changed),
613                                                        obj);
614
615         modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
616
617         priv->last_search = NULL;
618
619         /* Init the clipboard actions dim status */
620         modest_msg_view_grab_focus(MODEST_MSG_VIEW (priv->msg_view));
621
622         update_window_title (MODEST_MSG_VIEW_WINDOW (obj));
623
624         /* Check toolbar dimming rules */
625         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (obj));
626
627         return MODEST_WINDOW(obj);
628 }
629
630
631 gboolean 
632 modest_msg_view_window_toolbar_on_transfer_mode     (ModestMsgViewWindow *self)
633 {
634         ModestMsgViewWindowPrivate *priv= NULL; 
635
636         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
637         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
638
639         return priv->current_toolbar_mode == TOOLBAR_MODE_TRANSFER;
640 }
641
642 TnyHeader*
643 modest_msg_view_window_get_header (ModestMsgViewWindow *self)
644 {
645         ModestMsgViewWindowPrivate *priv= NULL; 
646         TnyMsg *msg = NULL;
647         TnyHeader *header = NULL;
648         GtkTreePath *path = NULL;
649         GtkTreeIter iter;
650
651         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), NULL);
652         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
653
654         /* Message is not obtained from a treemodel (Attachment ?) */
655         if (priv->header_model == NULL) {
656                 msg = modest_msg_view_window_get_message (self);
657                 header = tny_msg_get_header (msg);
658                 g_object_unref (msg);
659                 return header;
660         }
661
662         /* Get current message iter */
663         path = gtk_tree_row_reference_get_path (priv->row_reference);
664         g_return_val_if_fail (path != NULL, NULL);
665         gtk_tree_model_get_iter (priv->header_model, 
666                                  &iter, 
667                                  path);
668
669         /* Get current message header */
670         gtk_tree_model_get (priv->header_model, &iter, 
671                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
672                             &header, -1);
673
674         gtk_tree_path_free (path);
675         return header;
676 }
677
678 TnyMsg*
679 modest_msg_view_window_get_message (ModestMsgViewWindow *self)
680 {
681         ModestMsgView *msg_view;
682         ModestMsgViewWindowPrivate *priv;
683
684         g_return_val_if_fail (self, NULL);
685
686         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
687
688         msg_view = MODEST_MSG_VIEW (priv->msg_view);
689
690         return modest_msg_view_get_message (msg_view);
691 }
692
693 const gchar*
694 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
695 {
696         ModestMsgViewWindowPrivate *priv;
697
698         g_return_val_if_fail (self, NULL);
699         
700         priv  = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
701
702         return (const gchar*) priv->msg_uid;
703 }
704
705 static void
706 toggle_action_set_active_block_notify (GtkToggleAction *action,
707                                        gboolean value)
708 {
709         GSList *proxies = NULL;
710
711         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
712              proxies != NULL; proxies = g_slist_next (proxies)) {
713                 GtkWidget *widget = (GtkWidget *) proxies->data;
714                 gtk_action_block_activate_from (GTK_ACTION (action), widget);
715         }
716
717         gtk_toggle_action_set_active (action, value);
718
719         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
720              proxies != NULL; proxies = g_slist_next (proxies)) {
721                 GtkWidget *widget = (GtkWidget *) proxies->data;
722                 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
723         }
724 }
725
726
727 static void 
728 modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *toggle,
729                                             gpointer data)
730 {
731         ModestMsgViewWindow *window = MODEST_MSG_VIEW_WINDOW (data);
732         ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
733         ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
734         gboolean is_active;
735         GtkAction *action;
736
737         is_active = gtk_toggle_action_get_active (toggle);
738
739         if (is_active) {
740                 gtk_widget_show (priv->find_toolbar);
741                 hildon_find_toolbar_highlight_entry (HILDON_FIND_TOOLBAR (priv->find_toolbar), TRUE);
742         } else {
743                 gtk_widget_hide (priv->find_toolbar);
744         }
745
746         /* update the toggle buttons status */
747         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/FindInMessage");
748         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
749         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsFindInMessageMenu");
750         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), is_active);
751         
752 }
753
754 static void
755 modest_msg_view_window_find_toolbar_close (GtkWidget *widget,
756                                            ModestMsgViewWindow *obj)
757 {
758         GtkToggleAction *toggle;
759         ModestWindowPrivate *parent_priv;
760         parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
761         
762         toggle = GTK_TOGGLE_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/FindInMessage"));
763         gtk_toggle_action_set_active (toggle, FALSE);
764 }
765
766 static void
767 modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
768                                            ModestMsgViewWindow *obj)
769 {
770         gchar *current_search;
771         ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (obj);
772
773         g_object_get (G_OBJECT (widget), "prefix", &current_search, NULL);
774
775         if ((current_search == NULL) || (strcmp (current_search, "") == 0)) {
776                 g_free (current_search);
777                 hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ecdg_ib_find_rep_enter_text"));
778                 return;
779         }
780
781         if ((priv->last_search == NULL) || (strcmp (priv->last_search, current_search) != 0)) {
782                 gboolean result;
783                 g_free (priv->last_search);
784                 priv->last_search = g_strdup (current_search);
785                 result = modest_msg_view_search (MODEST_MSG_VIEW (priv->msg_view),
786                                                  priv->last_search);
787                 if (!result) {
788                         hildon_banner_show_information (NULL, NULL, dgettext("hildon-libs", "ckct_ib_find_no_matches"));
789                 } 
790         } else {
791                 if (!modest_msg_view_search_next (MODEST_MSG_VIEW (priv->msg_view))) {
792                         hildon_banner_show_information (NULL, NULL, dgettext("hildon-libs", "ckct_ib_find_search_complete"));
793                         g_free (priv->last_search);
794                         priv->last_search = NULL;
795                 }
796         }
797         
798         g_free (current_search);
799                 
800 }
801
802 static void
803 modest_msg_view_window_set_zoom (ModestWindow *window,
804                                  gdouble zoom)
805 {
806         ModestMsgViewWindowPrivate *priv;
807      
808         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
809
810         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
811         modest_msg_view_set_zoom (MODEST_MSG_VIEW (priv->msg_view), zoom);
812 }
813
814 static gdouble
815 modest_msg_view_window_get_zoom (ModestWindow *window)
816 {
817         ModestMsgViewWindowPrivate *priv;
818      
819         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), 1.0);
820
821         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
822         return modest_msg_view_get_zoom (MODEST_MSG_VIEW (priv->msg_view));
823 }
824
825 static gboolean
826 modest_msg_view_window_zoom_plus (ModestWindow *window)
827 {
828         ModestWindowPrivate *parent_priv;
829         GtkRadioAction *zoom_radio_action;
830         GSList *group, *node;
831
832         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
833         zoom_radio_action = GTK_RADIO_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, 
834                                                                          "/MenuBar/ViewMenu/ZoomMenu/Zoom50Menu"));
835
836         group = gtk_radio_action_get_group (zoom_radio_action);
837
838         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (group->data))) {
839                 hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_max_zoom_level_reached"));
840                 return FALSE;
841         }
842
843         for (node = group; node != NULL; node = g_slist_next (node)) {
844                 if ((node->next != NULL) && gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (node->next->data))) {
845                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (node->data), TRUE);
846                         return TRUE;
847                 }
848         }
849         return FALSE;
850 }
851
852 static gboolean
853 modest_msg_view_window_zoom_minus (ModestWindow *window)
854 {
855         ModestWindowPrivate *parent_priv;
856         GtkRadioAction *zoom_radio_action;
857         GSList *group, *node;
858
859         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
860         zoom_radio_action = GTK_RADIO_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, 
861                                                                          "/MenuBar/ViewMenu/ZoomMenu/Zoom50Menu"));
862
863         group = gtk_radio_action_get_group (zoom_radio_action);
864
865         for (node = group; node != NULL; node = g_slist_next (node)) {
866                 if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (node->data))) {
867                         if (node->next != NULL) {
868                                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (node->next->data), TRUE);
869                                 return TRUE;
870                         } else {
871                           hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_min_zoom_level_reached"));
872                                 return FALSE;
873                         }
874                         break;
875                 }
876         }
877         return FALSE;
878 }
879
880 static gboolean
881 modest_msg_view_window_key_release_event (GtkWidget *window,
882                                           GdkEventKey *event,
883                                           gpointer userdata)
884 {
885         if (event->type == GDK_KEY_RELEASE) {
886                 switch (event->keyval) {
887                 case GDK_Up:
888                         modest_msg_view_window_scroll_up (MODEST_WINDOW (window));
889                         return TRUE;
890                         break;
891                 case GDK_Down:
892                         modest_msg_view_window_scroll_down (MODEST_WINDOW (window));
893                         return TRUE;
894                         break;
895                 default:
896                         return FALSE;
897                         break;
898                 };
899         } else {
900                 return FALSE;
901         }
902 }
903
904 static void
905 modest_msg_view_window_scroll_up (ModestWindow *window)
906 {
907         ModestMsgViewWindowPrivate *priv;
908         gboolean return_value;
909
910         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
911         g_signal_emit_by_name (G_OBJECT (priv->main_scroll), "scroll-child", GTK_SCROLL_STEP_UP, FALSE, &return_value);
912 }
913
914 static void
915 modest_msg_view_window_scroll_down (ModestWindow *window)
916 {
917         ModestMsgViewWindowPrivate *priv;
918         gboolean return_value;
919
920         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
921         g_signal_emit_by_name (G_OBJECT (priv->main_scroll), "scroll-child", GTK_SCROLL_STEP_DOWN, FALSE, &return_value);
922 }
923
924 gboolean
925 modest_msg_view_window_last_message_selected (ModestMsgViewWindow *window)
926 {
927         GtkTreePath *path;
928         ModestMsgViewWindowPrivate *priv;
929         GtkTreeIter tmp_iter;
930         gboolean has_next = FALSE;
931
932         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), TRUE);
933         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
934
935         if (priv->header_model) {
936                 path = gtk_tree_row_reference_get_path (priv->row_reference);
937                 if (path == NULL) return FALSE;
938                 while (!has_next) {
939                         TnyHeader *header;
940                         gtk_tree_path_next (path);
941                         if (!gtk_tree_model_get_iter (priv->header_model, &tmp_iter, path))
942                                 break;
943                         gtk_tree_model_get (priv->header_model, &tmp_iter, TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
944                                             &header, -1);
945                         if (!(tny_header_get_flags(header)&TNY_HEADER_FLAG_DELETED)) {
946                                 has_next = TRUE;
947                                 break;
948                         }       
949                 }
950                 gtk_tree_path_free (path);
951                 return !has_next;
952         } else {
953                 return TRUE;
954         }
955         
956 }
957
958 gboolean
959 modest_msg_view_window_has_headers_model (ModestMsgViewWindow *window)
960 {
961         ModestMsgViewWindowPrivate *priv;
962
963         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), TRUE);
964         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
965
966         return priv->header_model != NULL;
967 }
968
969 gboolean
970 modest_msg_view_window_first_message_selected (ModestMsgViewWindow *window)
971 {
972         GtkTreePath *path;
973         ModestMsgViewWindowPrivate *priv;
974         gboolean result;
975         GtkTreeIter tmp_iter;
976
977         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), TRUE);
978         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
979
980         if (priv->header_model) {
981                 gchar * path_string;
982                 path = gtk_tree_row_reference_get_path (priv->row_reference);
983                 if (!path)
984                         return TRUE;
985
986                 path_string = gtk_tree_path_to_string (path);
987                 result = (strcmp (path_string, "0")==0);
988                 if (result) {
989                         g_free (path_string);
990                         gtk_tree_path_free (path);
991                         return result;
992                 }
993
994                 while (result) {
995                         TnyHeader *header;
996
997                         gtk_tree_path_prev (path);
998                         
999                         if (!gtk_tree_model_get_iter (priv->header_model, &tmp_iter, path))
1000                                 break;
1001                         gtk_tree_model_get (priv->header_model, &tmp_iter, TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1002                                             &header, -1);
1003                         if (!(tny_header_get_flags(header)&TNY_HEADER_FLAG_DELETED)) {
1004                                 result = FALSE;
1005                                 break;
1006                         }
1007
1008                         path_string = gtk_tree_path_to_string (path);
1009                         if (strcmp(path_string, "0")==0) {
1010                                 g_free (path_string);
1011                                 break;
1012                         }
1013                         g_free (path_string);
1014                 }
1015                 gtk_tree_path_free (path);
1016                 return result;
1017         } else {
1018                 return TRUE;
1019         }
1020         
1021 }
1022
1023 /**
1024  * Reads the message whose summary item is @header. It takes care of
1025  * several things, among others:
1026  *
1027  * If the message was not previously downloaded then ask the user
1028  * before downloading. If there is no connection launch the connection
1029  * dialog. Update toolbar dimming rules.
1030  *
1031  * Returns: TRUE if the mail operation was started, otherwise if the
1032  * user do not want to download the message, or if the user do not
1033  * want to connect, then the operation is not issued
1034  **/
1035 static gboolean
1036 message_reader (ModestMsgViewWindow *window,
1037                 ModestMsgViewWindowPrivate *priv,
1038                 TnyHeader *header,
1039                 GtkTreeIter iter)
1040 {
1041         ModestMailOperation *mail_op = NULL;
1042         GtkTreePath *path = NULL;
1043         ModestMailOperationTypeOperation op_type;
1044
1045         /* Msg download completed */
1046         if (tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED) {
1047                 op_type = MODEST_MAIL_OPERATION_TYPE_OPEN;
1048         } else {
1049                 TnyFolder *folder;
1050                 GtkResponseType response;
1051
1052                 op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
1053
1054                 /* Ask the user if he wants to download the message */
1055                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
1056                                                                     _("mcen_nc_get_msg"));
1057                 if (response == GTK_RESPONSE_CANCEL)
1058                         return FALSE;
1059                 
1060                 /* Offer the connection dialog if necessary */
1061                 /* FIXME: should this stuff go directly to the mail
1062                    operation instead of spread it all over the
1063                    code? */
1064                 folder = tny_header_get_folder (header);
1065                 if (folder) {
1066                         if (!modest_platform_connect_and_wait_if_network_folderstore (NULL, TNY_FOLDER_STORE (folder))) {
1067                                 g_object_unref (folder);
1068                                 return FALSE;
1069                         }
1070                         g_object_unref (folder);
1071                 }
1072         }
1073
1074         /* Get the path, will be freed by the callback */
1075         path = gtk_tree_model_get_path (priv->header_model, &iter);
1076
1077         /* New mail operation */
1078         mail_op = modest_mail_operation_new_with_error_handling (op_type, 
1079                                                                  G_OBJECT(window),
1080                                                                  modest_ui_actions_get_msgs_full_error_handler, 
1081                                                                  NULL);
1082                                 
1083         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
1084         modest_mail_operation_get_msg (mail_op, header, view_msg_cb, path);
1085         g_object_unref (mail_op);
1086
1087         /* Update toolbar dimming rules */
1088         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
1089
1090         return TRUE;
1091 }
1092
1093 gboolean        
1094 modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
1095 {
1096         ModestMsgViewWindowPrivate *priv;
1097         GtkTreePath *path= NULL;
1098         GtkTreeIter tmp_iter;
1099
1100         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), FALSE);
1101         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1102
1103         if (priv->header_model) {
1104                 path = gtk_tree_row_reference_get_path (priv->row_reference);
1105                 if (path == NULL) return FALSE;
1106
1107                 gtk_tree_model_get_iter (priv->header_model,
1108                                          &tmp_iter,
1109                                          path);
1110                 while (gtk_tree_model_iter_next (priv->header_model, &tmp_iter)) {
1111                         TnyHeader *header;
1112
1113                         gtk_tree_model_get (priv->header_model, &tmp_iter, 
1114                                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1115                                             &header, -1);
1116                         if (!header)
1117                                 break;
1118
1119                         if (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED) {
1120                                 g_object_unref (header);
1121                                 continue;
1122                         }
1123
1124                         /* Read the message & show it */
1125                         if (!message_reader (window, priv, header, tmp_iter)) {
1126                                 g_object_unref (header);
1127                                 break;
1128                         }
1129
1130                         /* Free */
1131                         g_object_unref (header);
1132
1133                         return TRUE;
1134                 }
1135                 gtk_tree_path_free (path);
1136         }
1137         return FALSE;           
1138 }
1139
1140 gboolean 
1141 modest_msg_view_window_select_first_message (ModestMsgViewWindow *self)
1142 {
1143         ModestMsgViewWindowPrivate *priv = NULL;
1144         TnyHeader *header = NULL;
1145         GtkTreeIter iter;
1146
1147         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
1148         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
1149
1150         /* Check that the model is not empty */
1151         if (!gtk_tree_model_get_iter_first (priv->header_model, &iter))
1152                 return FALSE;
1153
1154         /* Get the header */
1155         gtk_tree_model_get (priv->header_model, 
1156                             &iter, 
1157                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1158                             &header, -1);
1159         
1160         g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
1161         if (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED) {
1162                 g_object_unref (header);
1163                 return modest_msg_view_window_select_next_message (self);
1164         }
1165         
1166         /* Read the message & show it */
1167         message_reader (self, priv, header, iter);
1168         
1169         /* Free */
1170         g_object_unref (header);
1171
1172         return TRUE;
1173 }
1174  
1175 gboolean        
1176 modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
1177 {
1178         ModestMsgViewWindowPrivate *priv = NULL;
1179         GtkTreePath *path;
1180
1181         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), FALSE);
1182         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1183
1184         /* Return inmediatly if there is no header model */
1185         if (!priv->header_model)
1186                 return FALSE;
1187
1188         path = gtk_tree_row_reference_get_path (priv->row_reference);
1189         while (gtk_tree_path_prev (path)) {
1190                 TnyHeader *header;
1191                 GtkTreeIter iter;
1192
1193                 gtk_tree_model_get_iter (priv->header_model, &iter, path);
1194                 gtk_tree_model_get (priv->header_model, &iter, 
1195                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1196                                     &header, -1);
1197                 if (!header)
1198                         break;
1199                 if (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED) {
1200                         g_object_unref (header);
1201                         continue;
1202                 }
1203
1204                 /* Read the message & show it */
1205                 if (!message_reader (window, priv, header, iter)) {
1206                         g_object_unref (header);
1207                         break;
1208                 }
1209
1210                 g_object_unref (header);
1211
1212                 return TRUE;
1213         }
1214
1215         gtk_tree_path_free (path);
1216         return FALSE;
1217 }
1218
1219 static void
1220 view_msg_cb (ModestMailOperation *mail_op, 
1221              TnyHeader *header, 
1222              TnyMsg *msg, 
1223              gpointer user_data)
1224 {
1225         ModestMsgViewWindow *self = NULL;
1226         ModestMsgViewWindowPrivate *priv = NULL;
1227         GtkTreePath *path;
1228
1229         /* If there was any error */
1230         path = (GtkTreePath *) user_data;
1231         if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
1232                 gtk_tree_path_free (path);                      
1233                 return;
1234         }
1235
1236         /* Get the window */ 
1237         self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
1238         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
1239         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
1240
1241         /* Update the row reference */
1242         gtk_tree_row_reference_free (priv->row_reference);
1243         priv->row_reference = gtk_tree_row_reference_new (priv->header_model, path);
1244         gtk_tree_path_free (path);
1245
1246         /* Mark header as read */
1247         if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN))
1248                 tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
1249
1250         /* Set new message */
1251         modest_msg_view_set_message (MODEST_MSG_VIEW (priv->msg_view), msg);
1252         modest_msg_view_window_update_priority (self);
1253         update_window_title (MODEST_MSG_VIEW_WINDOW (self));
1254         modest_msg_view_grab_focus (MODEST_MSG_VIEW (priv->msg_view));
1255
1256         /* Free new references */
1257         g_object_unref (self);
1258 }
1259
1260 TnyFolderType
1261 modest_msg_view_window_get_folder_type (ModestMsgViewWindow *window)
1262 {
1263         ModestMsgViewWindowPrivate *priv;
1264         TnyMsg *msg;
1265         TnyFolderType folder_type;
1266
1267         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1268
1269         folder_type = TNY_FOLDER_TYPE_UNKNOWN;
1270
1271         msg = modest_msg_view_get_message (MODEST_MSG_VIEW (priv->msg_view));
1272         if (msg) {
1273                 TnyFolder *folder;
1274
1275                 folder = tny_msg_get_folder (msg);
1276                 
1277                 if (folder) {
1278                         folder_type = tny_folder_get_folder_type (folder);
1279                         
1280                         if (folder_type == TNY_FOLDER_TYPE_NORMAL || folder_type == TNY_FOLDER_TYPE_UNKNOWN) {
1281                                 const gchar *fname = tny_folder_get_name (folder);
1282                                 folder_type = modest_tny_folder_guess_folder_type_from_name (fname);
1283                         }
1284
1285                         g_object_unref (folder);
1286                 }
1287                 g_object_unref (msg);
1288         }
1289
1290         return folder_type;
1291 }
1292
1293
1294 static void
1295 modest_msg_view_window_update_priority (ModestMsgViewWindow *window)
1296 {
1297         ModestMsgViewWindowPrivate *priv;
1298         TnyHeaderFlags flags = 0;
1299
1300         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1301
1302         if (priv->header_model) {
1303                 TnyHeader *header;
1304                 GtkTreeIter iter;
1305                 GtkTreePath *path = NULL;
1306
1307                 path = gtk_tree_row_reference_get_path (priv->row_reference);
1308                 g_return_if_fail (path != NULL);
1309                 gtk_tree_model_get_iter (priv->header_model, 
1310                                          &iter, 
1311                                          gtk_tree_row_reference_get_path (priv->row_reference));
1312
1313                 gtk_tree_model_get (priv->header_model, &iter, TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1314                                     &header, -1);
1315                 flags = tny_header_get_flags (header);
1316                 gtk_tree_path_free (path);
1317         }
1318
1319         modest_msg_view_set_priority (MODEST_MSG_VIEW(priv->msg_view), flags);
1320
1321 }
1322
1323 static gboolean
1324 modest_msg_view_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer userdata)
1325 {
1326         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
1327                 ModestWindowPrivate *parent_priv;
1328                 ModestWindowMgr *mgr;
1329                 gboolean is_fullscreen;
1330                 GtkAction *fs_toggle_action;
1331                 gboolean active;
1332
1333                 mgr = modest_runtime_get_window_mgr ();
1334                 is_fullscreen = (modest_window_mgr_get_fullscreen_mode (mgr))?1:0;
1335
1336                 parent_priv = MODEST_WINDOW_GET_PRIVATE (widget);
1337                 
1338                 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
1339                 active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)))?1:0;
1340                 if (is_fullscreen != active) {
1341                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), is_fullscreen);
1342                 }
1343         }
1344
1345         return FALSE;
1346
1347 }
1348
1349 void
1350 modest_msg_view_window_toggle_fullscreen (ModestMsgViewWindow *window)
1351 {
1352                 ModestWindowPrivate *parent_priv;
1353                 GtkAction *fs_toggle_action;
1354                 parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1355                 
1356                 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
1357                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action),
1358                                               !gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)));
1359 }
1360
1361 static void
1362 set_homogeneous (GtkWidget *widget,
1363                  gpointer data)
1364 {
1365         if (GTK_IS_TOOL_ITEM (widget)) {
1366                 gtk_tool_item_set_expand (GTK_TOOL_ITEM (widget), TRUE);
1367                 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), TRUE);
1368         }
1369 }
1370
1371 static void
1372 modest_msg_view_window_show_toolbar (ModestWindow *self,
1373                                      gboolean show_toolbar)
1374 {
1375         ModestMsgViewWindowPrivate *priv = NULL;
1376         ModestWindowPrivate *parent_priv;
1377         GtkWidget *reply_button = NULL, *menu = NULL;
1378         GtkWidget *placeholder = NULL;
1379         gint insert_index;
1380         
1381         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
1382         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
1383
1384         /* Set optimized view status */
1385         priv->optimized_view = !show_toolbar;
1386
1387         if (!parent_priv->toolbar) {
1388                 parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
1389                                                                   "/ToolBar");
1390
1391                 /* Set homogeneous toolbar */
1392                 gtk_container_foreach (GTK_CONTAINER (parent_priv->toolbar), 
1393                                        set_homogeneous, NULL);
1394
1395                 priv->progress_toolitem = GTK_WIDGET (gtk_tool_item_new ());
1396                 priv->cancel_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarCancel");
1397                 priv->next_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarMessageNext");
1398                 priv->prev_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarMessageBack");
1399                 gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
1400                 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
1401                 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (priv->cancel_toolitem), FALSE);
1402                 gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->cancel_toolitem), FALSE);
1403
1404                 /* Add ProgressBar (Transfer toolbar) */ 
1405                 priv->progress_bar = modest_progress_bar_widget_new ();
1406                 gtk_widget_set_no_show_all (priv->progress_bar, TRUE);
1407                 placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ProgressbarView");
1408                 insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
1409                 gtk_container_add (GTK_CONTAINER (priv->progress_toolitem), priv->progress_bar);
1410                 gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (priv->progress_toolitem), insert_index);
1411                 
1412                 /* Connect cancel 'clicked' signal to abort progress mode */
1413                 g_signal_connect(priv->cancel_toolitem, "clicked",
1414                                  G_CALLBACK(cancel_progressbar),
1415                                  self);
1416                 
1417                 /* Add it to the observers list */
1418                 priv->progress_widgets = g_slist_prepend(priv->progress_widgets, priv->progress_bar);
1419
1420                 /* Add to window */
1421                 hildon_window_add_toolbar (HILDON_WINDOW (self), 
1422                                            GTK_TOOLBAR (parent_priv->toolbar));
1423
1424
1425                 /* Set reply button tap and hold menu */        
1426                 reply_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
1427                                                           "/ToolBar/ToolbarMessageReply");
1428                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
1429                                                   "/ToolbarReplyCSM");
1430                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
1431         }
1432
1433         if (show_toolbar) {
1434                 /* Quick hack: this prevents toolbar icons "dance" when progress bar show status is changed */ 
1435                 /* TODO: resize mode migth be GTK_RESIZE_QUEUE, in order to avoid unneccesary shows */
1436                 gtk_container_set_resize_mode (GTK_CONTAINER(parent_priv->toolbar), GTK_RESIZE_IMMEDIATE);
1437                 
1438                 gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
1439                 set_toolbar_mode (MODEST_MSG_VIEW_WINDOW(self), TOOLBAR_MODE_NORMAL);                   
1440                 
1441         } else {
1442                 gtk_widget_set_no_show_all (parent_priv->toolbar, TRUE);
1443                 gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
1444         }
1445 }
1446
1447 static void 
1448 modest_msg_view_window_clipboard_owner_change (GtkClipboard *clipboard,
1449                                                GdkEvent *event,
1450                                                ModestMsgViewWindow *window)
1451 {
1452         ModestWindowPrivate *parent_priv;
1453 /*      GtkAction *action; */
1454         gboolean is_address;
1455         gchar *selection;
1456         GtkWidget *focused;
1457
1458         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1459         selection = gtk_clipboard_wait_for_text (clipboard);
1460
1461         is_address = ((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
1462         
1463 /*      action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ToolsMenu/ToolsAddToContactsMenu"); */
1464 /*      gtk_action_set_sensitive (action, is_address); */
1465
1466         focused = gtk_window_get_focus (GTK_WINDOW (window));
1467
1468 /*      action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/EditCopyMenu"); */
1469 /*      gtk_action_set_sensitive (action, (selection != NULL) && (!MODEST_IS_ATTACHMENTS_VIEW (focused))); */
1470
1471 /*      action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/EditCutMenu"); */
1472 /*      gtk_action_set_sensitive (action, (selection != NULL) && (!MODEST_IS_ATTACHMENTS_VIEW (focused))); */
1473
1474         g_free (selection);
1475 /*      modest_msg_view_window_update_dimmed (window); */
1476         
1477 }
1478
1479 gboolean 
1480 modest_msg_view_window_transfer_mode_enabled (ModestMsgViewWindow *self)
1481 {
1482         ModestMsgViewWindowPrivate *priv;
1483         
1484         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE); 
1485         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
1486
1487         return priv->current_toolbar_mode == TOOLBAR_MODE_TRANSFER;
1488 }
1489
1490 static void
1491 cancel_progressbar (GtkToolButton *toolbutton,
1492                     ModestMsgViewWindow *self)
1493 {
1494         GSList *tmp;
1495         ModestMsgViewWindowPrivate *priv;
1496         
1497         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
1498
1499         /* Get operation observers and cancel its current operation */
1500         tmp = priv->progress_widgets;
1501         while (tmp) {
1502                 modest_progress_object_cancel_current_operation (MODEST_PROGRESS_OBJECT(tmp->data));
1503                 tmp=g_slist_next(tmp);
1504         }
1505 }
1506 static gboolean
1507 observers_empty (ModestMsgViewWindow *self)
1508 {
1509         GSList *tmp = NULL;
1510         ModestMsgViewWindowPrivate *priv;
1511         gboolean is_empty = TRUE;
1512         guint pending_ops = 0;
1513  
1514         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
1515         tmp = priv->progress_widgets;
1516
1517         /* Check all observers */
1518         while (tmp && is_empty)  {
1519                 pending_ops = modest_progress_object_num_pending_operations (MODEST_PROGRESS_OBJECT(tmp->data));
1520                 is_empty = pending_ops == 0;
1521                 
1522                 tmp = g_slist_next(tmp);
1523         }
1524         
1525         return is_empty;
1526 }
1527
1528
1529 static void
1530 on_queue_changed (ModestMailOperationQueue *queue,
1531                   ModestMailOperation *mail_op,
1532                   ModestMailOperationQueueNotification type,
1533                   ModestMsgViewWindow *self)
1534 {
1535         GSList *tmp;
1536         ModestMsgViewWindowPrivate *priv;
1537         ModestMailOperationTypeOperation op_type;
1538         ModestToolBarModes mode;
1539         
1540         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
1541         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
1542
1543         /* If this operations was created by another window, do nothing */
1544         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
1545             return;
1546
1547         /* Get toolbar mode from operation id*/
1548         op_type = modest_mail_operation_get_type_operation (mail_op);
1549         switch (op_type) {
1550         case MODEST_MAIL_OPERATION_TYPE_SEND:
1551         case MODEST_MAIL_OPERATION_TYPE_RECEIVE:
1552         case MODEST_MAIL_OPERATION_TYPE_OPEN:
1553                 mode = TOOLBAR_MODE_TRANSFER;
1554                 break;
1555         default:
1556                 mode = TOOLBAR_MODE_NORMAL;
1557                 
1558         }
1559                 
1560         /* Add operation observers and change toolbar if neccessary*/
1561         tmp = priv->progress_widgets;
1562         switch (type) {
1563         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
1564                 if (mode == TOOLBAR_MODE_TRANSFER) {
1565                         /* Enable transfer toolbar mode */
1566                         set_toolbar_transfer_mode(self);
1567                         while (tmp) {
1568                                 modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
1569                                                                       mail_op);
1570                                 tmp = g_slist_next (tmp);
1571                         }
1572                         
1573                 }
1574                 break;
1575         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
1576                 if (mode == TOOLBAR_MODE_TRANSFER) {
1577                         while (tmp) {
1578                                 modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
1579                                                                  mail_op);
1580                                 tmp = g_slist_next (tmp);
1581                                 
1582                         }
1583
1584                         /* If no more operations are being observed, NORMAL mode is enabled again */
1585                         if (observers_empty (self)) {
1586                                 set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
1587                         }
1588                 }
1589                 break;
1590         }
1591 }
1592
1593 GList *
1594 modest_msg_view_window_get_attachments (ModestMsgViewWindow *win) 
1595 {
1596         ModestMsgViewWindowPrivate *priv;
1597         GList *selected_attachments = NULL;
1598         
1599         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), NULL);
1600         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (win);
1601
1602         selected_attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
1603         
1604         return selected_attachments;
1605 }
1606
1607 void
1608 modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part)
1609 {
1610         ModestMsgViewWindowPrivate *priv;
1611         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
1612         g_return_if_fail (TNY_IS_MIME_PART (mime_part) || (mime_part == NULL));
1613
1614         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1615
1616         if (mime_part == NULL) {
1617                 gboolean error = FALSE;
1618                 GList *selected_attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
1619                 if (selected_attachments == NULL) {
1620                         error = TRUE;
1621                 } else if (g_list_length (selected_attachments) > 1) {
1622                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_unable_to_display_more"));
1623                         error = TRUE;
1624                 } else {
1625                         mime_part = (TnyMimePart *) selected_attachments->data;
1626                         g_object_ref (mime_part);
1627                 }
1628                 g_list_foreach (selected_attachments, (GFunc) g_object_unref, NULL);
1629                 g_list_free (selected_attachments);
1630
1631                 if (error)
1632                         return;
1633         } else {
1634                 g_object_ref (mime_part);
1635         }
1636
1637         if (tny_mime_part_is_purged (mime_part)) {
1638                 g_object_unref (mime_part);
1639                 hildon_banner_show_information (NULL, NULL, _("mail_ib_attach_not_local"));
1640                 return;
1641         }
1642
1643         if (!TNY_IS_MSG (mime_part)) {
1644                 gchar *filepath = NULL;
1645                 const gchar *att_filename = tny_mime_part_get_filename (mime_part);
1646                 gchar *extension = NULL;
1647                 TnyFsStream *temp_stream = NULL;
1648
1649                 if (att_filename) {
1650                         extension = g_strrstr (att_filename, ".");
1651                         if (extension != NULL)
1652                                 extension++;
1653                 }
1654
1655                 temp_stream = modest_maemo_utils_create_temp_stream (extension, &filepath);
1656
1657                 if (temp_stream) {
1658                         const gchar *content_type;
1659                         content_type = tny_mime_part_get_content_type (mime_part);
1660                         tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
1661                         
1662                         modest_platform_activate_file (filepath, content_type);
1663                         g_object_unref (temp_stream);
1664                         g_free (filepath);
1665                         /* TODO: delete temporary file */
1666                 }
1667         } else {
1668                 /* message attachment */
1669                 TnyHeader *header = NULL;
1670                 ModestWindowMgr *mgr;
1671                 ModestWindow *msg_win = NULL;
1672                 gboolean found;
1673                 
1674                 header = tny_msg_get_header (TNY_MSG (mime_part));
1675                 mgr = modest_runtime_get_window_mgr ();         
1676                 found = modest_window_mgr_find_registered_header (mgr, header, &msg_win);
1677
1678                 if (found) {
1679                         if (msg_win)                            /* there is already a window for this uid; top it */
1680                                 gtk_window_present (GTK_WINDOW(msg_win));
1681                         else 
1682                                 /* if it's found, but there is no msg_win, it's probably in the process of being created;
1683                                  * thus, we don't do anything */
1684                                 g_warning ("window for is already being created");
1685                 } else { 
1686                         /* it's not found, so create a new window for it */
1687                         modest_window_mgr_register_header (mgr, header); /* register the uid before building the window */
1688                         gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
1689                         if (!account)
1690                                 account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
1691                         msg_win = modest_msg_view_window_new (TNY_MSG (mime_part), account, NULL);
1692                         modest_window_mgr_register_window (mgr, msg_win);
1693                         gtk_window_set_transient_for (GTK_WINDOW (msg_win), GTK_WINDOW (window));
1694                         gtk_widget_show_all (GTK_WIDGET (msg_win));
1695                 }
1696         }
1697         g_object_unref (mime_part);
1698 }
1699
1700 static gboolean
1701 save_mime_part_to_file (const gchar *filename, TnyMimePart *mime_part)
1702 {
1703         GnomeVFSResult result;
1704         GnomeVFSHandle *handle;
1705         TnyStream *stream;
1706         
1707         result = gnome_vfs_create (&handle, filename, GNOME_VFS_OPEN_WRITE, FALSE, 0777);
1708         if (result != GNOME_VFS_OK) {
1709                 hildon_banner_show_information (NULL, NULL, _("mail_ib_file_operation_failed"));
1710                 return FALSE;
1711         }
1712         stream = tny_vfs_stream_new (handle);
1713         tny_mime_part_decode_to_stream (mime_part, stream);
1714         g_object_unref (G_OBJECT (stream));
1715         return TRUE;
1716 }
1717
1718 static gboolean
1719 save_mime_part_to_file_with_checks (GtkWindow *parent, const gchar *filename, TnyMimePart *mime_part)
1720 {
1721         if (modest_maemo_utils_file_exists (filename)) {
1722                 GtkWidget *confirm_overwrite_dialog;
1723                 confirm_overwrite_dialog = hildon_note_new_confirmation (GTK_WINDOW (parent),
1724                                                                          _("emev_nc_replace_files"));
1725                 if (gtk_dialog_run (GTK_DIALOG (confirm_overwrite_dialog)) != GTK_RESPONSE_OK) {
1726                         gtk_widget_destroy (confirm_overwrite_dialog);
1727                         return FALSE;
1728                 }
1729                 gtk_widget_destroy (confirm_overwrite_dialog);
1730         }
1731
1732         return save_mime_part_to_file (filename, mime_part);
1733 }
1734
1735 void
1736 modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, GList *mime_parts)
1737 {
1738         gboolean clean_list = FALSE;
1739         ModestMsgViewWindowPrivate *priv;
1740         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
1741
1742         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1743
1744         if (mime_parts == NULL) {
1745                 mime_parts = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
1746                 if (mime_parts == NULL)
1747                         return;
1748                 clean_list = TRUE;
1749         }
1750
1751         if (mime_parts->next == NULL) {
1752                 /* only one attachment selected */
1753                 GtkWidget *save_dialog = NULL;
1754                 TnyMimePart *mime_part = (TnyMimePart *) mime_parts->data;
1755                 if (!TNY_IS_MSG (mime_part) && tny_mime_part_is_attachment (mime_part)) {
1756                         const gchar *filename;
1757                         gchar *folder;
1758                         save_dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_SAVE);
1759                         folder = g_build_filename (g_get_home_dir (), DEFAULT_FOLDER, NULL);
1760                         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (save_dialog), folder);
1761                         g_free (folder);
1762                         filename = tny_mime_part_get_filename (mime_part);
1763                         if (filename != NULL)
1764                                 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (save_dialog), filename);
1765                         while (gtk_dialog_run (GTK_DIALOG (save_dialog)) == GTK_RESPONSE_OK) {
1766                                 gchar *filename_tmp = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (save_dialog));
1767                                 gboolean save_result;
1768                                 if (!modest_maemo_utils_folder_writable (filename_tmp)) {
1769                                         g_free (filename_tmp);
1770                                         hildon_banner_show_information (NULL, NULL, _("TODO: read only location"));
1771                                         continue;
1772                                 }
1773                                 save_result = save_mime_part_to_file_with_checks (GTK_WINDOW (save_dialog), 
1774                                                                                   filename_tmp, mime_part);
1775                                 g_free (filename_tmp);
1776                                 if (save_result)
1777                                         break;
1778                                 else
1779                                         continue;
1780                         }
1781                         gtk_widget_destroy (save_dialog);
1782                 } else {
1783                         g_warning ("Tried to save a non-file attachment");
1784                 }
1785         } else {
1786                 GtkWidget *save_dialog = NULL;
1787                 gchar *folder;
1788                 gchar *save_multiple_str = g_strdup_printf (_("FIXME: %d attachments"), 
1789                                                             g_list_length (mime_parts));
1790                 save_dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_SAVE);
1791                 g_object_set (G_OBJECT (save_dialog), "save-multiple", save_multiple_str, NULL);
1792                 
1793                 folder = g_build_filename (g_get_home_dir (), DEFAULT_FOLDER, NULL);
1794                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (save_dialog), folder);
1795                 g_free (folder);
1796                 if (gtk_dialog_run (GTK_DIALOG (save_dialog)) == GTK_RESPONSE_OK) {
1797                         gchar *foldername = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (save_dialog));
1798                         GList *node = NULL;
1799                         gboolean attachment_found = FALSE;
1800                         if (!modest_maemo_utils_folder_writable (foldername)) {
1801                                 g_free (foldername);
1802                                 hildon_banner_show_information (NULL, NULL, _("TODO: read only location"));
1803                         }
1804                         for (node = mime_parts; node != NULL; node = g_list_next (node)) {
1805                                 TnyMimePart *mime_part = (TnyMimePart *) node->data;
1806                                 if (tny_mime_part_is_attachment (mime_part)) {
1807                                         const gchar *att_filename = tny_mime_part_get_filename (mime_part);
1808                                         if (att_filename != NULL) {
1809                                                 gchar *full_filename;
1810                                                 gboolean save_result;
1811                                                 full_filename = g_build_filename (foldername, att_filename, NULL);
1812                                                 attachment_found = TRUE;
1813                                                 
1814                                                 save_result = save_mime_part_to_file_with_checks (GTK_WINDOW (save_dialog), 
1815                                                                                                   full_filename, mime_part);
1816                                                 g_free (full_filename);
1817                                                 if (!save_result)
1818                                                         break;
1819                                         }
1820                                 }
1821                         }
1822                         gtk_widget_destroy (save_dialog);
1823                 } else {
1824                         g_warning ("Tried to save a non-file attachment");
1825                 }
1826                 /* more than one attachment selected */
1827         }
1828         if (clean_list) {
1829                 g_list_foreach (mime_parts, (GFunc) g_object_unref, NULL);
1830                 g_list_free (mime_parts);
1831         }
1832 }
1833
1834 void
1835 modest_msg_view_window_remove_attachments (ModestMsgViewWindow *window, gboolean get_all)
1836 {
1837         ModestMsgViewWindowPrivate *priv;
1838         GList *mime_parts = NULL, *node;
1839         gchar *confirmation_message;
1840         gint response;
1841         gint n_attachments;
1842         TnyMsg *msg;
1843 /*      TnyFolder *folder; */
1844
1845         g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
1846         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1847
1848         if (get_all)
1849                 mime_parts = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view));
1850         else
1851                 mime_parts = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
1852                 
1853         /* Remove already purged messages from mime parts list */
1854         node = mime_parts;
1855         while (node != NULL) {
1856                 TnyMimePart *part = TNY_MIME_PART (node->data);
1857                 if (tny_mime_part_is_purged (part)) {
1858                         GList *deleted_node = node;
1859                         node = g_list_next (node);
1860                         g_object_unref (part);
1861                         mime_parts = g_list_delete_link (mime_parts, deleted_node);
1862                 } else {
1863                         node = g_list_next (node);
1864                 }
1865         }
1866
1867         if (mime_parts == NULL)
1868                 return;
1869
1870         n_attachments = g_list_length (mime_parts);
1871         if (n_attachments == 1) {
1872                 const gchar *filename;
1873
1874                 if (TNY_IS_MSG (mime_parts->data)) {
1875                         TnyHeader *header;
1876                         header = tny_msg_get_header (TNY_MSG (mime_parts->data));
1877                         filename = tny_header_get_subject (header);
1878                         g_object_unref (header);
1879                         if (filename == NULL)
1880                                 filename = _("mail_va_no_subject");
1881                 } else {
1882                         filename = tny_mime_part_get_filename (TNY_MIME_PART (mime_parts->data));
1883                 }
1884                 confirmation_message = g_strdup_printf (_("mcen_nc_purge_file_text"), filename);
1885         } else {
1886                 confirmation_message = g_strdup_printf (ngettext("mcen_nc_purge_file_text", 
1887                                                                  "mcen_nc_purge_files_text", 
1888                                                                  n_attachments), n_attachments);
1889         }
1890         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
1891                                                             confirmation_message);
1892         g_free (confirmation_message);
1893
1894         if (response != GTK_RESPONSE_OK)
1895                 return;
1896
1897 /*      folder = tny_msg_get_folder (msg); */
1898 /*      tny_msg_uncache_attachments (msg); */
1899 /*      tny_folder_refresh (folder, NULL); */
1900 /*      g_object_unref (folder); */
1901         
1902         modest_platform_information_banner (NULL, NULL, _("mcen_ib_removing_attachment"));
1903
1904         for (node = mime_parts; node != NULL; node = g_list_next (node)) {
1905                 tny_mime_part_set_purged (TNY_MIME_PART (node->data));
1906 /*              modest_msg_view_remove_attachment (MODEST_MSG_VIEW (priv->msg_view), node->data); */
1907         }
1908
1909         msg = modest_msg_view_get_message (MODEST_MSG_VIEW (priv->msg_view));
1910         modest_msg_view_set_message (MODEST_MSG_VIEW (priv->msg_view), NULL);
1911         tny_msg_rewrite_cache (msg);
1912         modest_msg_view_set_message (MODEST_MSG_VIEW (priv->msg_view), msg);
1913
1914         g_list_foreach (mime_parts, (GFunc) g_object_unref, NULL);
1915         g_list_free (mime_parts);
1916
1917
1918 }
1919
1920
1921 static void
1922 update_window_title (ModestMsgViewWindow *window)
1923 {
1924         ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
1925         TnyMsg *msg = NULL;
1926         TnyHeader *header = NULL;
1927         const gchar *subject = NULL;
1928
1929         msg = modest_msg_view_get_message (MODEST_MSG_VIEW (priv->msg_view));
1930         if (msg != NULL) {
1931                 header = tny_msg_get_header (msg);
1932                 subject = tny_header_get_subject (header);
1933                 g_object_unref (msg);
1934         }
1935
1936         if ((subject == NULL)||(subject[0] == '\0'))
1937                 subject = _("mail_va_no_subject");
1938
1939         gtk_window_set_title (GTK_WINDOW (window), subject);
1940 }