Fixes NB#106346, added a missing fix
[modest] / src / hildon2 / modest-msg-edit-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
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include <fcntl.h>
33 #include <glib/gstdio.h>
34 #include <string.h>
35 #include <tny-account-store.h>
36 #include <tny-fs-stream.h>
37 #include <tny-vfs-stream.h>
38 #include <tny-camel-mem-stream.h>
39
40 #include <config.h>
41
42 #include <modest-account-mgr.h>
43 #include <modest-account-mgr-helpers.h>
44
45 #include <widgets/modest-msg-edit-window.h>
46 #include <modest-selector-picker.h>
47 #include <widgets/modest-recpt-editor.h>
48 #include <widgets/modest-attachments-view.h>
49
50 #include <modest-runtime.h>
51
52 #include "modest-platform.h"
53 #include "modest-icon-names.h"
54 #include "modest-widget-memory.h"
55 #include "modest-window-priv.h"
56 #include "modest-mail-operation.h"
57 #include "modest-tny-platform-factory.h"
58 #include "modest-tny-msg.h"
59 #include "modest-tny-folder.h"
60 #include "modest-tny-account.h"
61 #include "modest-address-book.h"
62 #include "modest-text-utils.h"
63 #include <tny-simple-list.h>
64 #include <modest-wp-text-view.h>
65 #include <wptextbuffer.h>
66 #include <hildon/hildon-pannable-area.h>
67 #include <hildon/hildon-touch-selector.h>
68 #include <hildon/hildon-picker-dialog.h>
69 #include "modest-msg-edit-window-ui-dimming.h"
70
71 #include "modest-hildon-includes.h"
72 #ifdef MODEST_HAVE_HILDON0_WIDGETS
73 #include <hildon-widgets/hildon-color-chooser.h>
74 #endif
75 #include "widgets/modest-msg-edit-window-ui.h"
76 #ifdef MODEST_HAVE_HILDON0_WIDGETS
77 #include <libgnomevfs/gnome-vfs-mime-utils.h>
78 #else
79 #include <libgnomevfs/gnome-vfs-mime.h>
80 #endif
81 #include <modest-utils.h>
82 #include "modest-maemo-utils.h"
83 #include <modest-ui-constants.h>
84
85
86 #define DEFAULT_FONT_SIZE 3
87 #define DEFAULT_FONT 2
88 #define DEFAULT_SIZE_BUTTON_FONT_FAMILY "Sans"
89 #define DEFAULT_MAIN_VBOX_SPACING 6
90 #define SUBJECT_MAX_LENGTH 1000
91 #define IMAGE_MAX_WIDTH 560
92 #define DEFAULT_FONT_SCALE 1.5
93 #define ATTACHMENT_BUTTON_WIDTH 118
94
95 static gboolean is_wp_text_buffer_started = FALSE;
96
97 static void  modest_msg_edit_window_class_init   (ModestMsgEditWindowClass *klass);
98 static void  modest_msg_edit_window_init         (ModestMsgEditWindow *obj);
99 static void  modest_msg_edit_window_finalize     (GObject *obj);
100
101 static gboolean msg_body_focus (GtkWidget *focus, GdkEventFocus *event, gpointer userdata);
102 static void  body_changed (GtkTextBuffer *buffer, ModestMsgEditWindow *editor);
103 static void  recpt_field_changed (GtkTextBuffer *buffer, ModestMsgEditWindow *editor);
104
105 static void  text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window);
106 static void  text_buffer_can_undo (GtkTextBuffer *buffer, gboolean can_undo, ModestMsgEditWindow *window);
107 static void  text_buffer_can_redo (GtkTextBuffer *buffer, gboolean can_redo, ModestMsgEditWindow *window);
108 static void  text_buffer_apply_tag (GtkTextBuffer *buffer, GtkTextTag *tag, 
109                                     GtkTextIter *start, GtkTextIter *end,
110                                     gpointer userdata);
111 static void  text_buffer_delete_images_by_id (GtkTextBuffer *buffer, const gchar * image_id);
112 static void  subject_field_changed (GtkEditable *editable, ModestMsgEditWindow *window);
113 static void  subject_field_insert_text (GtkEditable *editable, 
114                                         gchar *new_text,
115                                         gint new_text_length,
116                                         gint *position,
117                                         ModestMsgEditWindow *window);
118 static void  modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
119                                                          gpointer userdata);
120 static void  modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window);
121
122 static void modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
123                                                      ModestRecptEditor *editor);
124 static void modest_msg_edit_window_add_attachment_clicked (GtkButton *button,
125                                                            ModestMsgEditWindow *window);
126
127 /* ModestWindow methods implementation */
128 static void modest_msg_edit_window_disconnect_signals (ModestWindow *window);
129 static void modest_msg_edit_window_show_toolbar   (ModestWindow *window,
130                                                    gboolean show_toolbar);
131 static void modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
132                                                            GdkEvent *event,
133                                                            ModestMsgEditWindow *window);
134 static void modest_msg_edit_window_clipboard_owner_handle_change_in_idle (ModestMsgEditWindow *window);
135 static void subject_field_move_cursor (GtkEntry *entry,
136                                        GtkMovementStep step,
137                                        gint a1,
138                                        gboolean a2,
139                                        gpointer userdata);
140 static void update_window_title (ModestMsgEditWindow *window);
141
142 /* Find toolbar */
143 static void modest_msg_edit_window_find_toolbar_search (GtkWidget *widget,
144                                                         ModestMsgEditWindow *window);
145 static void modest_msg_edit_window_find_toolbar_close (GtkWidget *widget,
146                                                        ModestMsgEditWindow *window);
147 static gboolean gtk_text_iter_forward_search_insensitive (const GtkTextIter *iter,
148                                                           const gchar *str,
149                                                           GtkTextIter *match_start,
150                                                           GtkTextIter *match_end);
151
152 static void remove_tags (WPTextBuffer *buffer);
153
154 static void on_account_removed (TnyAccountStore *account_store, 
155                                 TnyAccount *account,
156                                 gpointer user_data);
157
158 static void init_window (ModestMsgEditWindow *obj);
159
160 gboolean scroll_drag_timeout (gpointer userdata);
161 static void correct_scroll (ModestMsgEditWindow *w);
162 static void correct_scroll_without_drag_check (ModestMsgEditWindow *w, gboolean only_if_focused);
163 static void text_buffer_end_user_action (GtkTextBuffer *buffer,
164                                          ModestMsgEditWindow *userdata);
165 static void text_buffer_mark_set (GtkTextBuffer *buffer,
166                                   GtkTextIter *iter,
167                                   GtkTextMark *mark,
168                                   ModestMsgEditWindow *userdata);
169 static void on_message_settings (GtkAction *action,
170                                  ModestMsgEditWindow *window);
171 static void setup_menu (ModestMsgEditWindow *self);
172
173 static void from_field_changed (HildonPickerButton *button,
174                                 ModestMsgEditWindow *self);
175 static void font_size_clicked (GtkToolButton *button,
176                                ModestMsgEditWindow *window);
177 static void font_face_clicked (GtkToolButton *button,
178                                ModestMsgEditWindow *window);
179 static void DEBUG_BUFFER (WPTextBuffer *buffer)
180 {
181 #ifdef DEBUG
182         GtkTextIter iter;
183         g_message ("BEGIN BUFFER OF SIZE %d", gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (buffer)));
184
185         gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
186         while (!gtk_text_iter_is_end (&iter)) {
187                 GString *output = g_string_new ("");
188                 GSList *toggled_tags;
189                 GSList *node;
190
191                 toggled_tags = gtk_text_iter_get_toggled_tags (&iter, FALSE);
192                 g_string_append_printf (output, "%d: CLOSED [ ", gtk_text_iter_get_offset (&iter));
193                 for (node = toggled_tags; node != NULL; node = g_slist_next (node)) {
194                         GtkTextTag *tag = (GtkTextTag *) node->data;
195                         const gchar *name;
196                         g_object_get (G_OBJECT (tag), "name", &name, NULL);
197                         output = g_string_append (output, name);
198                         g_string_append (output, " ");
199                 }
200                 output = g_string_append (output, "] OPENED [ ");
201                 toggled_tags = gtk_text_iter_get_toggled_tags (&iter, TRUE);
202                 for (node = toggled_tags; node != NULL; node = g_slist_next (node)) {
203                         GtkTextTag *tag = (GtkTextTag *) node->data;
204                         const gchar *name;
205                         g_object_get (G_OBJECT (tag), "name", &name, NULL);
206                         output = g_string_append (output, name);
207                         g_string_append (output, " ");
208                 }
209                 output = g_string_append (output, "]\n");
210                 g_message ("%s", output->str);
211                 g_string_free (output, TRUE);
212                 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
213         }
214         g_message ("END BUFFER");
215 #endif
216 }
217
218 static const GtkActionEntry hildon2_msg_edit_action_entries [] = {
219         { "MessageSettings", NULL, N_("mcen_me_message_settings"), NULL, NULL, G_CALLBACK (on_message_settings)},
220 };
221
222
223 /* static gboolean */
224 /* on_key_pressed (GtkWidget *self, */
225 /*              GdkEventKey *event, */
226 /*              gpointer user_data); */
227
228 /* list my signals */
229 enum {
230         /* MY_SIGNAL_1, */
231         /* MY_SIGNAL_2, */
232         LAST_SIGNAL
233 };
234
235 typedef struct _ModestMsgEditWindowPrivate ModestMsgEditWindowPrivate;
236 struct _ModestMsgEditWindowPrivate {
237         GtkWidget   *msg_body;
238         GtkWidget   *header_box;
239         
240         ModestPairList *from_field_protos;
241         GtkWidget   *from_field;
242         gchar       *last_from_account;
243         gchar       *original_account_name;
244         
245         GtkWidget   *to_field;
246         GtkWidget   *cc_field;
247         GtkWidget   *bcc_field;
248         GtkWidget   *subject_field;
249         GtkWidget   *attachments_view;
250         GtkWidget   *priority_icon;
251         GtkWidget   *subject_box;
252         GtkWidget   *add_attachment_button;
253
254         GtkWidget   *cc_caption;
255         GtkWidget   *bcc_caption;
256         gboolean     update_caption_visibility;
257         GtkWidget   *attachments_caption;
258
259         GtkTextBuffer *text_buffer;
260
261         GtkWidget   *font_size_toolitem;
262         GtkWidget   *font_face_toolitem;
263         GtkWidget   *font_color_button;
264         GtkWidget   *font_color_toolitem;
265         GSList      *font_items_group;
266         GtkTreeModel *faces_model;
267         gint         current_face_index;
268         GtkWidget   *font_tool_button_label;
269         GtkTreeModel *sizes_model;
270         gint         current_size_index;
271         GtkWidget   *size_tool_button_label;
272
273         GtkWidget   *find_toolbar;
274         gchar       *last_search;
275
276         GtkWidget   *font_dialog;
277
278         GtkWidget   *pannable;
279         guint        correct_scroll_idle;
280         guint        scroll_drag_timeout_id;
281         gdouble      last_upper;
282
283         gint next_cid;
284         TnyList *attachments;
285         TnyList *images;
286         guint64 images_size;
287         gint images_count;
288
289         TnyHeaderFlags priority_flags;
290
291         gboolean    can_undo, can_redo;
292         gulong      clipboard_change_handler_id;
293         gulong      default_clipboard_change_handler_id;
294         gulong      account_removed_handler_id;
295         guint       clipboard_owner_idle;
296         gchar       *clipboard_text;
297
298         TnyMsg      *draft_msg;
299         TnyMsg      *outbox_msg;
300         gchar       *msg_uid;
301
302         gboolean    sent;
303
304         GtkWidget   *app_menu;
305         GtkWidget   *cc_button;
306         GtkWidget   *bcc_button;
307 };
308
309 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
310                                                     MODEST_TYPE_MSG_EDIT_WINDOW, \
311                                                     ModestMsgEditWindowPrivate))
312 /* globals */
313 static GtkWindowClass *parent_class = NULL;
314
315 /* uncomment the following if you have defined any signals */
316 /* static guint signals[LAST_SIGNAL] = {0}; */
317
318 GType
319 modest_msg_edit_window_get_type (void)
320 {
321         static GType my_type = 0;
322         if (!my_type) {
323                 static const GTypeInfo my_info = {
324                         sizeof(ModestMsgEditWindowClass),
325                         NULL,           /* base init */
326                         NULL,           /* base finalize */
327                         (GClassInitFunc) modest_msg_edit_window_class_init,
328                         NULL,           /* class finalize */
329                         NULL,           /* class data */
330                         sizeof(ModestMsgEditWindow),
331                         1,              /* n_preallocs */
332                         (GInstanceInitFunc) modest_msg_edit_window_init,
333                         NULL
334                 };
335                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
336                                                   "ModestMsgEditWindow",
337                                                   &my_info, 0);
338
339         }
340         return my_type;
341 }
342
343 static void
344 save_state (ModestWindow *self)
345 {
346         modest_widget_memory_save (modest_runtime_get_conf(),
347                                    G_OBJECT(self), MODEST_CONF_EDIT_WINDOW_KEY);
348 }
349
350
351 static void
352 restore_settings (ModestMsgEditWindow *self)
353 {
354         ModestConf *conf = NULL;
355
356         conf = modest_runtime_get_conf ();
357
358         /* Dim at start clipboard actions */
359         modest_widget_memory_restore (conf, G_OBJECT(self), MODEST_CONF_EDIT_WINDOW_KEY);
360 }
361
362
363 static void
364 modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
365 {
366         GObjectClass *gobject_class;
367         ModestWindowClass *modest_window_class;
368         gobject_class = (GObjectClass*) klass;
369         modest_window_class = (ModestWindowClass*) klass;
370
371         parent_class            = g_type_class_peek_parent (klass);
372         gobject_class->finalize = modest_msg_edit_window_finalize;
373
374         modest_window_class->show_toolbar_func = modest_msg_edit_window_show_toolbar;
375         modest_window_class->save_state_func = save_state;
376         modest_window_class->disconnect_signals_func = modest_msg_edit_window_disconnect_signals;
377
378         g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
379 }
380
381 static void
382 modest_msg_edit_window_init (ModestMsgEditWindow *obj)
383 {
384         ModestMsgEditWindowPrivate *priv;
385         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
386
387         priv->msg_body      = NULL;
388         priv->from_field    = NULL;
389         priv->to_field      = NULL;
390         priv->cc_field      = NULL;
391         priv->bcc_field     = NULL;
392         priv->subject_field = NULL;
393         priv->attachments   = TNY_LIST (tny_simple_list_new ());
394         priv->images        = TNY_LIST (tny_simple_list_new ());
395         priv->images_size   = 0;
396         priv->images_count  = 0;
397         priv->next_cid      = 0;
398
399         priv->cc_caption    = NULL;
400         priv->bcc_caption    = NULL;
401         priv->update_caption_visibility = FALSE;
402
403         priv->priority_flags = 0;
404
405         priv->find_toolbar = NULL;
406         priv->last_search = NULL;
407
408         priv->draft_msg = NULL;
409         priv->outbox_msg = NULL;
410         priv->msg_uid = NULL;
411
412         priv->can_undo = FALSE;
413         priv->can_redo = FALSE;
414         priv->clipboard_change_handler_id = 0;
415         priv->default_clipboard_change_handler_id = 0;
416         priv->account_removed_handler_id = 0;
417         priv->clipboard_owner_idle = 0;
418         priv->clipboard_text = NULL;
419         priv->sent = FALSE;
420
421         priv->scroll_drag_timeout_id = 0;
422         priv->correct_scroll_idle = 0;
423         priv->last_upper = 0.0;
424
425         priv->font_dialog = NULL;
426         priv->app_menu = NULL;
427
428         if (!is_wp_text_buffer_started) {
429                 is_wp_text_buffer_started = TRUE;
430                 wp_text_buffer_library_init ();
431         }
432
433         init_window (obj);
434         
435         hildon_program_add_window (hildon_program_get_instance(),
436                                    HILDON_WINDOW(obj));
437 }
438
439
440 /** 
441  * @result: A ModestPairList, which must be freed with modest_pair_list_free().
442  */
443 static ModestPairList*
444 get_transports (void)
445 {
446         GSList *transports = NULL;
447         
448         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
449         GSList *accounts = modest_account_mgr_account_names (account_mgr, 
450                                                              TRUE /* only enabled accounts. */); 
451                                                 
452         GSList *cursor = accounts;
453         while (cursor) {
454                 gchar *account_name = cursor->data;
455                 gchar *from_string  = NULL;
456                 if (account_name) {
457                         from_string = modest_account_mgr_get_from_string (account_mgr,
458                                                                           account_name);
459                 }
460                 
461                 if (from_string && account_name) {
462                         gchar *name = account_name;
463                         ModestPair *pair = modest_pair_new ((gpointer) name,
464                                                 (gpointer) from_string , TRUE);
465                         transports = g_slist_prepend (transports, pair);
466                 }
467                 
468                 cursor = cursor->next;
469         }
470         g_slist_free (accounts); /* only free the accounts, not the elements,
471                                   * because they are used in the pairlist */
472         return transports;
473 }
474
475 static void window_focus (GtkWindow *window,
476                           GtkWidget *widget,
477                           gpointer userdata)
478 {
479         modest_window_check_dimming_rules_group (MODEST_WINDOW (userdata), MODEST_DIMMING_RULES_CLIPBOARD);
480 }
481
482 gboolean
483 scroll_drag_timeout (gpointer userdata)
484 {
485         ModestMsgEditWindow *win = (ModestMsgEditWindow *) userdata;
486         ModestMsgEditWindowPrivate *priv;
487
488         /* It could happen that the window was already closed */
489         if (!GTK_WIDGET_VISIBLE (win))
490                 return FALSE;
491
492         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(win);
493
494         correct_scroll_without_drag_check (win, TRUE);
495
496         priv->scroll_drag_timeout_id = 0;
497
498         return FALSE;
499 }
500
501 static gboolean 
502 correct_scroll_without_drag_check_idle (gpointer userdata)
503 {
504         ModestMsgEditWindow *w = (ModestMsgEditWindow *) userdata;
505         ModestMsgEditWindowPrivate *priv;
506         GtkTextIter iter;
507         GdkRectangle rectangle;
508         gint offset_min, offset_max;
509         GtkTextMark *insert;
510         GtkAdjustment *vadj;
511
512         /* It could happen that the window was already closed */
513         if (!GTK_WIDGET_VISIBLE (w))
514                 return FALSE;
515
516         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(w);
517
518         insert = gtk_text_buffer_get_insert (priv->text_buffer);
519         gtk_text_buffer_get_iter_at_mark (priv->text_buffer, &iter, insert);
520
521         gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->msg_body), &iter, &rectangle);
522         offset_min = priv->msg_body->allocation.y + rectangle.y;
523         offset_max = offset_min + rectangle.height;
524
525         vadj = hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (priv->pannable));
526         offset_min = MAX (offset_min - 48, 0);
527         offset_max = MIN (offset_max + 48, vadj->upper);
528
529         gtk_adjustment_clamp_page (vadj, (gdouble) offset_min, (gdouble) offset_max);
530
531         priv->correct_scroll_idle = 0;
532         return FALSE;
533 }
534
535 static void
536 correct_scroll_without_drag_check (ModestMsgEditWindow *w, gboolean only_if_focused)
537 {
538         ModestMsgEditWindowPrivate *priv;
539
540         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(w);
541
542         if (only_if_focused && !gtk_widget_is_focus (priv->msg_body))
543                 return;
544
545         if (priv->correct_scroll_idle > 0) {
546                 return;
547         }
548
549         priv->correct_scroll_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
550                                                      (GSourceFunc) correct_scroll_without_drag_check_idle,
551                                                      g_object_ref (w),
552                                                      g_object_unref);
553 }
554
555 static void
556 correct_scroll (ModestMsgEditWindow *w)
557 {
558         ModestMsgEditWindowPrivate *priv;
559
560         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(w);
561         if (gtk_grab_get_current () == priv->msg_body) {
562                 if (priv->scroll_drag_timeout_id == 0) {
563                         priv->scroll_drag_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
564                                                                            500,
565                                                                            (GSourceFunc) scroll_drag_timeout,
566                                                                            g_object_ref (w),
567                                                                            g_object_unref);
568                 }
569                 return;
570         }
571
572         correct_scroll_without_drag_check (w, TRUE);
573 }
574
575 static void
576 text_buffer_end_user_action (GtkTextBuffer *buffer,
577                              ModestMsgEditWindow *userdata)
578 {
579
580         correct_scroll (userdata);
581 }
582
583 static void
584 text_buffer_mark_set (GtkTextBuffer *buffer,
585                       GtkTextIter *iter,
586                       GtkTextMark *mark,
587                       ModestMsgEditWindow *userdata)
588 {
589         gtk_text_buffer_begin_user_action (buffer);
590         gtk_text_buffer_end_user_action (buffer);
591 }
592
593 static void
594 cut_clipboard_check (GtkTextView *text_view,
595                      gpointer userdata)
596 {
597         GtkTextBuffer *buffer;
598         
599         buffer = gtk_text_view_get_buffer (text_view);
600         if (!modest_text_utils_buffer_selection_is_valid (buffer)) {
601                 g_signal_stop_emission_by_name ((gpointer )text_view, "cut-clipboard");
602         }
603 }
604
605 static void
606 copy_clipboard_check (GtkTextView *text_view,
607                      gpointer userdata)
608 {
609         GtkTextBuffer *buffer;
610         
611         buffer = gtk_text_view_get_buffer (text_view);
612         if (!modest_text_utils_buffer_selection_is_valid (buffer)) {
613                 g_signal_stop_emission_by_name ((gpointer )text_view, "copy-clipboard");
614         }
615 }
616
617 static void
618 attachment_deleted (ModestAttachmentsView *attachments_view,
619                     gpointer user_data)
620 {
621         modest_msg_edit_window_remove_attachments (MODEST_MSG_EDIT_WINDOW (user_data),
622                                                    NULL);
623 }
624
625 static void
626 connect_signals (ModestMsgEditWindow *obj)
627 {
628         ModestMsgEditWindowPrivate *priv;
629
630         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
631
632         g_signal_connect (G_OBJECT (priv->text_buffer), "refresh_attributes",
633                           G_CALLBACK (text_buffer_refresh_attributes), obj);
634         g_signal_connect (G_OBJECT (priv->text_buffer), "can-undo",
635                           G_CALLBACK (text_buffer_can_undo), obj);
636         g_signal_connect (G_OBJECT (priv->text_buffer), "can-redo",
637                           G_CALLBACK (text_buffer_can_redo), obj);
638         g_signal_connect (G_OBJECT (priv->text_buffer), "changed",
639                           G_CALLBACK (body_changed), obj);
640         g_signal_connect (G_OBJECT (priv->text_buffer), "modified-changed",
641                           G_CALLBACK (body_changed), obj);
642         g_signal_connect (G_OBJECT (priv->text_buffer), "end-user-action",
643                           G_CALLBACK (text_buffer_end_user_action), obj);
644         g_signal_connect (G_OBJECT (priv->text_buffer), "mark-set",
645                           G_CALLBACK (text_buffer_mark_set), obj);
646         g_signal_connect_after (G_OBJECT (priv->text_buffer), "apply-tag",
647                                 G_CALLBACK (text_buffer_apply_tag), obj);
648         g_signal_connect_swapped (G_OBJECT (priv->to_field), "open-addressbook", 
649                                   G_CALLBACK (modest_msg_edit_window_open_addressbook), obj);
650         g_signal_connect_swapped (G_OBJECT (priv->cc_field), "open-addressbook", 
651                                   G_CALLBACK (modest_msg_edit_window_open_addressbook), obj);
652         g_signal_connect_swapped (G_OBJECT (priv->bcc_field), "open-addressbook", 
653                                   G_CALLBACK (modest_msg_edit_window_open_addressbook), obj);
654
655         g_signal_connect (G_OBJECT (priv->add_attachment_button), "clicked",
656                           G_CALLBACK (modest_msg_edit_window_add_attachment_clicked), obj);
657         g_signal_connect (G_OBJECT (priv->from_field), "value-changed",
658                           G_CALLBACK (from_field_changed), obj);
659
660         g_signal_connect (G_OBJECT (priv->msg_body), "focus-in-event",
661                           G_CALLBACK (msg_body_focus), obj);
662         g_signal_connect (G_OBJECT (priv->msg_body), "focus-out-event",
663                           G_CALLBACK (msg_body_focus), obj);
664         g_signal_connect (G_OBJECT (obj), "set-focus", G_CALLBACK (window_focus), obj);
665         g_signal_connect (G_OBJECT (modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (priv->to_field))),
666                           "changed", G_CALLBACK (recpt_field_changed), obj);
667         g_signal_connect (G_OBJECT (modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (priv->cc_field))),
668                           "changed", G_CALLBACK (recpt_field_changed), obj);
669         g_signal_connect (G_OBJECT (modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (priv->bcc_field))),
670                           "changed", G_CALLBACK (recpt_field_changed), obj);
671         g_signal_connect (G_OBJECT (priv->subject_field), "changed", G_CALLBACK (subject_field_changed), obj);
672         g_signal_connect_after (G_OBJECT (priv->subject_field), "move-cursor", G_CALLBACK (subject_field_move_cursor), obj);
673         g_signal_connect (G_OBJECT (priv->subject_field), "insert-text", G_CALLBACK (subject_field_insert_text), obj);
674
675         g_signal_connect (G_OBJECT (priv->find_toolbar), "search", G_CALLBACK (modest_msg_edit_window_find_toolbar_search), obj);
676         g_signal_connect (G_OBJECT (priv->find_toolbar), "close", G_CALLBACK (modest_msg_edit_window_find_toolbar_close), obj);
677  
678         priv->clipboard_change_handler_id = 
679                 g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_PRIMARY)), "owner-change",
680                                   G_CALLBACK (modest_msg_edit_window_clipboard_owner_change), obj);
681         priv->default_clipboard_change_handler_id = 
682                 g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)), "owner-change",
683                                   G_CALLBACK (modest_msg_edit_window_clipboard_owner_change), obj);
684
685         g_signal_connect (G_OBJECT (priv->msg_body), "cut-clipboard", G_CALLBACK (cut_clipboard_check), NULL);
686         g_signal_connect (G_OBJECT (priv->msg_body), "copy-clipboard", G_CALLBACK (copy_clipboard_check), NULL);
687         g_signal_connect (G_OBJECT (priv->attachments_view), "delete", G_CALLBACK (attachment_deleted), obj);
688 }
689
690 static void
691 init_wp_text_view_style ()
692 {
693         static gboolean initialized = FALSE;
694
695         if (!initialized) {
696                 gtk_rc_parse_string ("class \"WPTextView\" style \"fremantle-textview\"");
697                 initialized = TRUE;
698         }
699 }       
700
701 static void
702 init_window (ModestMsgEditWindow *obj)
703 {
704         GtkWidget *to_caption, *subject_caption;
705         GtkWidget *main_vbox;
706         GtkWidget *add_attachment_align;
707         ModestMsgEditWindowPrivate *priv;
708         GtkActionGroup *action_group;
709         ModestWindowPrivate *parent_priv;
710         GdkPixbuf *window_icon = NULL;
711         GError *error = NULL;
712
713         GtkSizeGroup *title_size_group;
714         GtkSizeGroup *value_size_group;
715         GtkWidget *attachment_icon;
716         GtkWidget *window_box;
717         GtkWidget *window_align;
718 #if (GTK_MINOR_VERSION >= 10)
719         GdkAtom deserialize_type;
720 #endif
721
722         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
723         parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
724
725         parent_priv->ui_manager = gtk_ui_manager_new();
726         action_group = gtk_action_group_new ("ModestMsgEditWindowActions");
727         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
728
729         /* Add common actions */
730         gtk_action_group_add_actions (action_group,
731                                       modest_msg_edit_action_entries,
732                                       G_N_ELEMENTS (modest_msg_edit_action_entries),
733                                       obj);
734         gtk_action_group_add_actions (action_group,
735                                       hildon2_msg_edit_action_entries,
736                                       G_N_ELEMENTS (hildon2_msg_edit_action_entries),
737                                       obj);
738         gtk_action_group_add_toggle_actions (action_group,
739                                              modest_msg_edit_toggle_action_entries,
740                                              G_N_ELEMENTS (modest_msg_edit_toggle_action_entries),
741                                              obj);
742         gtk_action_group_add_radio_actions (action_group,
743                                             modest_msg_edit_alignment_radio_action_entries,
744                                             G_N_ELEMENTS (modest_msg_edit_alignment_radio_action_entries),
745                                             GTK_JUSTIFY_LEFT,
746                                             G_CALLBACK (modest_ui_actions_on_change_justify),
747                                             obj);
748         gtk_action_group_add_radio_actions (action_group,
749                                             modest_msg_edit_priority_action_entries,
750                                             G_N_ELEMENTS (modest_msg_edit_priority_action_entries),
751                                             0,
752                                             G_CALLBACK (modest_ui_actions_msg_edit_on_change_priority),
753                                             obj);
754         gtk_action_group_add_radio_actions (action_group,
755                                             modest_msg_edit_file_format_action_entries,
756                                             G_N_ELEMENTS (modest_msg_edit_file_format_action_entries),
757                                             modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL),
758                                             G_CALLBACK (modest_ui_actions_msg_edit_on_change_file_format),
759                                             obj);
760         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
761         g_object_unref (action_group);
762
763         /* Load the UI definition */
764         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-msg-edit-window-ui.xml",
765                                          &error);
766         if (error != NULL) {
767                 g_warning ("Could not merge modest-msg-edit-window-ui.xml: %s", error->message);
768                 g_clear_error (&error);
769         }
770
771         /* Add accelerators */
772         gtk_window_add_accel_group (GTK_WINDOW (obj), 
773                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
774
775         parent_priv->menubar = NULL;
776
777         title_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
778         value_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
779
780         /* Note: This ModestPairList* must exist for as long as the picker
781          * that uses it, because the ModestSelectorPicker uses the ID opaquely, 
782          * so it can't know how to manage its memory. */ 
783         priv->from_field    = modest_selector_picker_new (MODEST_EDITABLE_SIZE,
784                                                           HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
785                                                           NULL, g_str_equal);
786         modest_maemo_utils_set_hbutton_layout (title_size_group, value_size_group, 
787                                                _("mail_va_from"), priv->from_field);
788
789         priv->to_field      = modest_recpt_editor_new ();
790         priv->cc_field      = modest_recpt_editor_new ();
791         priv->bcc_field     = modest_recpt_editor_new ();
792         priv->subject_box = gtk_hbox_new (FALSE, MODEST_MARGIN_NONE);
793         priv->priority_icon = gtk_image_new ();
794         gtk_box_pack_start (GTK_BOX (priv->subject_box), priv->priority_icon, FALSE, FALSE, 0);
795         priv->subject_field = hildon_entry_new (MODEST_EDITABLE_SIZE);
796         gtk_entry_set_max_length (GTK_ENTRY (priv->subject_field) ,SUBJECT_MAX_LENGTH);
797         g_object_set (G_OBJECT (priv->subject_field), "truncate-multiline", TRUE, NULL);
798         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->subject_field), 
799                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
800         gtk_box_pack_start (GTK_BOX (priv->subject_box), priv->subject_field, TRUE, TRUE, 0);
801         priv->add_attachment_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
802         gtk_widget_set_size_request (priv->add_attachment_button, ATTACHMENT_BUTTON_WIDTH, -1);
803         GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (priv->add_attachment_button), GTK_CAN_FOCUS);
804         gtk_button_set_focus_on_click (GTK_BUTTON (priv->add_attachment_button), FALSE);
805         attachment_icon = gtk_image_new_from_icon_name (MODEST_HEADER_ICON_ATTACH, HILDON_ICON_SIZE_FINGER);
806         hildon_button_set_image (HILDON_BUTTON (priv->add_attachment_button), attachment_icon);
807         add_attachment_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
808         gtk_alignment_set_padding (GTK_ALIGNMENT (add_attachment_align), 0, 0, MODEST_MARGIN_DEFAULT, 0);
809         gtk_container_add (GTK_CONTAINER (add_attachment_align), priv->add_attachment_button);
810         gtk_box_pack_start (GTK_BOX (priv->subject_box), add_attachment_align, FALSE, FALSE, 0);
811         priv->attachments_view = modest_attachments_view_new (NULL);
812         modest_attachments_view_set_style (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
813                                            MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS);
814         
815         priv->header_box = gtk_vbox_new (FALSE, 0);
816         
817         to_caption = modest_maemo_utils_create_captioned_with_size_type 
818                 (title_size_group, value_size_group,
819                  _("mail_va_to"), FALSE, priv->to_field,
820                  HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
821         priv->cc_caption = modest_maemo_utils_create_captioned_with_size_type 
822                 (title_size_group, value_size_group,
823                  _("mail_va_cc"), FALSE, priv->cc_field,
824                  HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
825         priv->bcc_caption = modest_maemo_utils_create_captioned_with_size_type
826                 (title_size_group, value_size_group,
827                  _("mail_va_hotfix1"), FALSE, priv->bcc_field,
828                  HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
829         subject_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
830                                                                _("mail_va_subject"), FALSE, priv->subject_box);
831         priv->attachments_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group, value_size_group,
832                                                                                         _("mail_va_attachment"), 
833                                                                                         FALSE,
834                                                                                         priv->attachments_view,
835                                                                                         HILDON_SIZE_AUTO_WIDTH |
836                                                                                         HILDON_SIZE_AUTO_HEIGHT);
837         /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->to_field), value_size_group); */
838         /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->cc_field), value_size_group); */
839         /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->bcc_field), value_size_group); */
840         g_object_unref (title_size_group);
841         g_object_unref (value_size_group);
842
843         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->from_field, FALSE, FALSE, 0);
844         gtk_box_pack_start (GTK_BOX (priv->header_box), to_caption, FALSE, FALSE, 0);
845         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->cc_caption, FALSE, FALSE, 0);
846         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->bcc_caption, FALSE, FALSE, 0);
847         gtk_box_pack_start (GTK_BOX (priv->header_box), subject_caption, FALSE, FALSE, 0);
848         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->attachments_caption, FALSE, FALSE, 0);
849         gtk_widget_set_no_show_all (priv->attachments_caption, TRUE);
850
851         init_wp_text_view_style ();
852
853         priv->msg_body = modest_wp_text_view_new ();
854         
855
856         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->msg_body), GTK_WRAP_WORD_CHAR);
857         priv->text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
858         g_object_set (priv->text_buffer, "font_scale", DEFAULT_FONT_SCALE, NULL);
859         wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
860 #if (GTK_MINOR_VERSION >= 10)
861         gtk_text_buffer_register_serialize_tagset(GTK_TEXT_BUFFER(priv->text_buffer), NULL);
862         deserialize_type = gtk_text_buffer_register_deserialize_tagset(GTK_TEXT_BUFFER(priv->text_buffer), 
863                                                                        NULL);
864         gtk_text_buffer_deserialize_set_can_create_tags (GTK_TEXT_BUFFER (priv->text_buffer), 
865                                                          deserialize_type, TRUE);
866 #endif
867         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
868
869         priv->find_toolbar = hildon_find_toolbar_new (NULL);
870         gtk_widget_set_no_show_all (priv->find_toolbar, TRUE);
871
872 /*      g_signal_connect (G_OBJECT (obj), "key_pressed", G_CALLBACK (on_key_pressed), NULL) */
873
874         priv->pannable = hildon_pannable_area_new ();
875         
876         main_vbox = gtk_vbox_new  (FALSE, DEFAULT_MAIN_VBOX_SPACING);
877         window_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
878         gtk_alignment_set_padding (GTK_ALIGNMENT (window_align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DEFAULT);
879
880         gtk_box_pack_start (GTK_BOX(main_vbox), priv->header_box, FALSE, FALSE, 0);
881         gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_body, TRUE, TRUE, 0);
882         gtk_container_add (GTK_CONTAINER (window_align), main_vbox);
883
884         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (priv->pannable), window_align);
885         gtk_container_set_focus_vadjustment (GTK_CONTAINER (main_vbox), 
886                                              hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (priv->pannable)));
887         gtk_widget_show_all (GTK_WIDGET(priv->pannable));
888         
889         window_box = gtk_vbox_new (FALSE, 0);
890         gtk_container_add (GTK_CONTAINER(obj), window_box);
891
892         gtk_box_pack_start (GTK_BOX (window_box), priv->pannable, TRUE, TRUE, 0);
893
894         /* Set window icon */
895         window_icon = modest_platform_get_icon (MODEST_APP_MSG_EDIT_ICON, MODEST_ICON_SIZE_BIG); 
896         if (window_icon) {
897                 gtk_window_set_icon (GTK_WINDOW (obj), window_icon);
898                 g_object_unref (window_icon);
899         }       
900 }
901         
902 static void
903 modest_msg_edit_window_disconnect_signals (ModestWindow *window)
904 {
905         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
906
907         if (gtk_clipboard_get (GDK_SELECTION_PRIMARY) &&
908             g_signal_handler_is_connected (gtk_clipboard_get (GDK_SELECTION_PRIMARY), 
909                                            priv->clipboard_change_handler_id))
910                 g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), 
911                                              priv->clipboard_change_handler_id);
912         if (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD) &&
913             g_signal_handler_is_connected (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), 
914                                            priv->default_clipboard_change_handler_id))
915                 g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), 
916                                              priv->default_clipboard_change_handler_id);
917
918         if (priv->account_removed_handler_id && 
919             g_signal_handler_is_connected (modest_runtime_get_account_store (), 
920                                            priv->account_removed_handler_id))
921                 g_signal_handler_disconnect(modest_runtime_get_account_store (), 
922                                            priv->account_removed_handler_id);
923 }
924
925 static void
926 modest_msg_edit_window_finalize (GObject *obj)
927 {
928         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
929         
930         /* Sanity check: shouldn't be needed, the window mgr should
931            call this function before */
932         modest_msg_edit_window_disconnect_signals (MODEST_WINDOW (obj));
933
934         if (priv->font_dialog != NULL) {
935                 gtk_dialog_response (GTK_DIALOG (priv->font_dialog), GTK_RESPONSE_NONE);
936         }
937
938         if (priv->clipboard_text != NULL) {
939                 g_free (priv->clipboard_text);
940                 priv->clipboard_text = NULL;
941         }
942         
943         if (priv->draft_msg != NULL) {
944                 TnyHeader *header = tny_msg_get_header (priv->draft_msg);
945                 if (TNY_IS_HEADER (header)) {
946                         ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
947                         modest_window_mgr_unregister_header (mgr, header);
948                 }
949                 g_object_unref (priv->draft_msg);
950                 priv->draft_msg = NULL;
951         }
952         if (priv->outbox_msg != NULL) {
953                 TnyHeader *header = tny_msg_get_header (priv->outbox_msg);
954                 if (TNY_IS_HEADER (header)) {
955                         ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
956                         modest_window_mgr_unregister_header (mgr, header);
957                 }
958                 g_object_unref (priv->outbox_msg);
959                 priv->outbox_msg = NULL;
960         }
961         if (priv->correct_scroll_idle > 0) {
962                 g_source_remove (priv->correct_scroll_idle);
963                 priv->correct_scroll_idle = 0;
964         }
965         if (priv->scroll_drag_timeout_id > 0) {
966                 g_source_remove (priv->scroll_drag_timeout_id);
967                 priv->scroll_drag_timeout_id = 0;
968         }
969         if (priv->clipboard_owner_idle > 0) {
970                 g_source_remove (priv->clipboard_owner_idle);
971                 priv->clipboard_owner_idle = 0;
972         }
973         if (priv->original_account_name)
974                 g_free (priv->original_account_name);
975         g_free (priv->msg_uid);
976         g_free (priv->last_search);
977         g_object_unref (priv->faces_model);
978         g_object_unref (priv->sizes_model);
979         g_object_unref (priv->attachments);
980         g_object_unref (priv->images);
981
982         /* This had to stay alive for as long as the picker that used it: */
983         modest_pair_list_free (priv->from_field_protos);
984         
985         G_OBJECT_CLASS(parent_class)->finalize (obj);
986 }
987
988 static void
989 pixbuf_size_prepared (GdkPixbufLoader *loader,
990                       gint width,
991                       gint height,
992                       ModestMsgEditWindow *self)
993 {
994         gint new_height, new_width;
995         gboolean set_size;
996         
997         new_height = height;
998         new_width = width;
999         set_size = FALSE;
1000
1001         if (width > IMAGE_MAX_WIDTH) {
1002                 new_height = height * IMAGE_MAX_WIDTH / width;
1003                 new_width = IMAGE_MAX_WIDTH;
1004         }
1005
1006         gdk_pixbuf_loader_set_size (loader, new_width, new_height);
1007 }
1008
1009 static GdkPixbuf *
1010 pixbuf_from_stream (TnyStream *stream,
1011                     const gchar *mime_type,
1012                     guint64 *stream_size,
1013                     ModestMsgEditWindow *self)
1014 {
1015         GdkPixbufLoader *loader;
1016         GdkPixbuf *pixbuf;
1017         guint64 size;
1018         GError *error = NULL;
1019
1020         size = 0;
1021
1022         loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL);
1023
1024         if (loader == NULL) {
1025                 if (stream_size)
1026                         *stream_size = 0;
1027                 return NULL;
1028         }
1029         g_signal_connect (G_OBJECT (loader), "size-prepared", G_CALLBACK (pixbuf_size_prepared), self);
1030
1031         hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
1032
1033         tny_stream_reset (TNY_STREAM (stream));
1034         while (!tny_stream_is_eos (TNY_STREAM (stream))) {
1035                 unsigned char read_buffer[128];
1036                 gint readed;
1037                 readed = tny_stream_read (TNY_STREAM (stream), (char *) read_buffer, 128);
1038                 size += readed;
1039                 if (!gdk_pixbuf_loader_write (loader, read_buffer, readed, &error)) {
1040                         break;
1041                 }
1042                 /* Allow some UI responsiveness */
1043                 while (gtk_events_pending ())
1044                         gtk_main_iteration ();
1045         }
1046         hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), FALSE);
1047
1048         gdk_pixbuf_loader_close (loader, &error);
1049
1050         if (error)
1051                 g_error_free (error);
1052         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1053         if (pixbuf) 
1054                 g_object_ref (pixbuf);
1055         g_object_unref (loader);
1056
1057         if (!pixbuf)
1058                 return NULL;
1059
1060         if (gdk_pixbuf_get_width (pixbuf) > IMAGE_MAX_WIDTH) {
1061                 GdkPixbuf *new_pixbuf;
1062                 gint new_height;
1063                 new_height = (gdk_pixbuf_get_height (pixbuf) * IMAGE_MAX_WIDTH) /
1064                         gdk_pixbuf_get_width (pixbuf);
1065                 new_pixbuf = gdk_pixbuf_scale_simple (pixbuf, IMAGE_MAX_WIDTH, new_height, GDK_INTERP_BILINEAR);
1066                 g_object_unref (pixbuf);
1067                 pixbuf = new_pixbuf;
1068         }
1069
1070         if (stream_size)
1071                 *stream_size = size;
1072
1073         return pixbuf;
1074 }
1075
1076 static void
1077 replace_with_images (ModestMsgEditWindow *self, TnyList *attachments)
1078 {
1079         ModestMsgEditWindowPrivate *priv;
1080         TnyIterator *iter;
1081
1082         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1083
1084         g_object_ref (self);
1085         for (iter = tny_list_create_iterator (attachments);
1086              !tny_iterator_is_done (iter);
1087              tny_iterator_next (iter)) {
1088                 TnyMimePart *part = (TnyMimePart *) tny_iterator_get_current (iter);
1089                 const gchar *cid = tny_mime_part_get_content_id (part);
1090                 const gchar *mime_type = tny_mime_part_get_content_type (part);
1091                 if ((cid != NULL)&&(mime_type != NULL)) {
1092                         guint64 stream_size;
1093                         TnyStream *stream = tny_mime_part_get_decoded_stream (part);
1094                         GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size, self);
1095
1096
1097                         g_object_unref (stream);
1098
1099                         if (pixbuf != NULL) {
1100                                 priv->images_count ++;
1101                                 priv->images_size += stream_size;
1102                                 wp_text_buffer_replace_image (WP_TEXT_BUFFER (priv->text_buffer), cid, pixbuf);
1103                                 g_object_unref (pixbuf);
1104                         }
1105                 }
1106                 g_object_unref (part);
1107         }
1108         g_object_unref (iter);
1109         g_object_unref (self);
1110 }
1111
1112 static void
1113 get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
1114 {
1115         TnyMimePart *parent = NULL;
1116         const gchar *content_type = NULL;
1117         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1118
1119         content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
1120
1121         if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
1122                 parent = g_object_ref (msg);
1123         } else if (content_type && !g_strcasecmp (content_type, "multipart/mixed")) {
1124                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
1125                 TnyIterator *iter;
1126
1127                 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
1128                 iter = tny_list_create_iterator (parts);
1129                 while (!tny_iterator_is_done (iter)) {
1130                         TnyMimePart *part;
1131                         part = TNY_MIME_PART (tny_iterator_get_current (iter));
1132                         content_type = tny_mime_part_get_content_type (part);
1133                         if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
1134                                 parent = part;
1135                                 break;
1136                         } else {
1137                                 g_object_unref (part);
1138                         }
1139                         tny_iterator_next (iter);
1140                 }
1141                 g_object_unref (iter);
1142                 g_object_unref (parts);
1143         }
1144
1145         if (parent != NULL) {
1146                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
1147                 TnyIterator *iter;
1148
1149                 tny_mime_part_get_parts (TNY_MIME_PART (parent), parts);
1150                 iter = tny_list_create_iterator (parts);
1151                 while (!tny_iterator_is_done (iter)) {
1152                         TnyMimePart *part;
1153                         part = TNY_MIME_PART (tny_iterator_get_current (iter));
1154                         content_type = tny_mime_part_get_content_type (part);
1155                         if (content_type && g_str_has_prefix (content_type, "image/")) {
1156                                 tny_list_prepend (priv->images, (GObject *) part);
1157                         } 
1158                         g_object_unref (part);
1159                         tny_iterator_next (iter);
1160                 }
1161                 g_object_unref (iter);
1162                 g_object_unref (parts);
1163                 g_object_unref (parent);
1164         }
1165 }
1166
1167 static void
1168 update_next_cid (ModestMsgEditWindow *self, TnyList *attachments)
1169 {
1170         TnyIterator *iter;
1171         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1172
1173         for (iter = tny_list_create_iterator (attachments) ; 
1174              !tny_iterator_is_done (iter);
1175              tny_iterator_next (iter)) {
1176                 TnyMimePart *part = (TnyMimePart *) tny_iterator_get_current (iter);
1177                 const gchar *cid = tny_mime_part_get_content_id (part);
1178                 if (cid != NULL) {
1179                         char *invalid = NULL;
1180                         gint int_cid = strtol (cid, &invalid, 10);
1181                         if ((invalid != NULL) && (*invalid == '\0') && (int_cid >= priv->next_cid)) {
1182                                 priv->next_cid = int_cid + 1;
1183                         }
1184                 }
1185                 g_object_unref (part);
1186         }
1187         g_object_unref (iter);
1188 }
1189
1190 static void
1191 set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
1192 {
1193         TnyHeader *header;
1194         gchar *to, *cc, *bcc, *subject;
1195         gchar *body;
1196         ModestMsgEditWindowPrivate *priv;
1197         ModestWindowPrivate *parent_priv;
1198         GtkTextIter iter;
1199         TnyHeaderFlags priority_flags;
1200         TnyFolder *msg_folder;
1201         gboolean is_html = FALSE;
1202         gboolean field_view_set;
1203         
1204         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
1205         g_return_if_fail (TNY_IS_MSG (msg));
1206
1207         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1208         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
1209
1210         header = tny_msg_get_header (msg);
1211         to      = tny_header_dup_to (header);
1212         cc      = tny_header_dup_cc (header);
1213         bcc     = tny_header_dup_bcc (header);
1214         subject = tny_header_dup_subject (header);
1215         priority_flags = tny_header_get_priority (header);
1216
1217         if (to)
1218                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->to_field),  to);
1219
1220         field_view_set = TRUE;
1221         if (cc) {
1222                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->cc_field),  cc);
1223                 gtk_widget_set_no_show_all (priv->cc_caption, FALSE);
1224                 gtk_widget_show (priv->cc_caption);
1225         } else if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_CC, NULL)) {
1226                 gtk_widget_set_no_show_all (priv->cc_caption, TRUE);
1227                 gtk_widget_hide (priv->cc_caption);
1228                 field_view_set = FALSE;
1229         }
1230         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->cc_button), field_view_set);
1231
1232         field_view_set = TRUE;
1233         if (bcc) {
1234                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->bcc_field), bcc);
1235                 gtk_widget_set_no_show_all (priv->bcc_caption, FALSE);
1236                 gtk_widget_show (priv->bcc_caption);
1237         } else if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_BCC, NULL)) {
1238                 gtk_widget_set_no_show_all (priv->bcc_caption, TRUE);
1239                 gtk_widget_hide (priv->bcc_caption);
1240                 field_view_set = FALSE;
1241         }
1242         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->bcc_button), field_view_set);
1243
1244
1245         if (subject)
1246                 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);
1247         modest_msg_edit_window_set_priority_flags (MODEST_MSG_EDIT_WINDOW(self),
1248                                                    priority_flags);
1249
1250         update_window_title (self);
1251
1252         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
1253         body = modest_tny_msg_get_body (msg, TRUE, &is_html);
1254
1255         if ((body == NULL)||(body[0] == '\0')) {
1256                 g_free (body);
1257                 body = modest_text_utils_convert_to_html ("");
1258                 is_html = FALSE;
1259         }
1260         wp_text_buffer_load_document_begin (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
1261         wp_text_buffer_load_document_write (WP_TEXT_BUFFER (priv->text_buffer),
1262                                             (gchar *) body,
1263                                             strlen (body));
1264         wp_text_buffer_load_document_end (WP_TEXT_BUFFER (priv->text_buffer));
1265         g_free (body);
1266
1267         /* Add attachments to the view */
1268         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), msg);
1269         priv->attachments = modest_attachments_view_get_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1270         if (tny_list_get_length (priv->attachments) == 0) {
1271                 gtk_widget_hide (priv->attachments_caption);
1272         } else {
1273                 gtk_widget_set_no_show_all (priv->attachments_caption, FALSE);
1274                 gtk_widget_show_all (priv->attachments_caption);
1275         }
1276         get_related_images (self, msg);
1277         update_next_cid (self, priv->attachments);
1278         update_next_cid (self, priv->images);
1279         replace_with_images (self, priv->images);
1280
1281         if (preserve_is_rich && !is_html) {
1282                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
1283         /* Get the default format required from configuration */
1284         } else if (!preserve_is_rich && !modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL)) {
1285                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
1286         }
1287
1288         /* Set the default focus depending on having already a To: field or not */
1289         if ((!to)||(*to == '\0')) {
1290                 modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (priv->to_field));
1291         } else {
1292                 gtk_widget_grab_focus (priv->msg_body);
1293         }
1294
1295         DEBUG_BUFFER (WP_TEXT_BUFFER (priv->text_buffer));
1296
1297         gtk_text_buffer_get_start_iter (priv->text_buffer, &iter);
1298         gtk_text_buffer_place_cursor (priv->text_buffer, &iter);
1299
1300         modest_msg_edit_window_set_modified (self, FALSE);
1301
1302         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (self));
1303         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
1304         text_buffer_can_undo (priv->text_buffer, FALSE, self);
1305         text_buffer_can_redo (priv->text_buffer, FALSE, self);
1306
1307         if (priv->msg_uid) {
1308                 g_free (priv->msg_uid);
1309                 priv->msg_uid = NULL;
1310         }
1311
1312         /* we should set a reference to the incoming message if it is a draft */
1313         msg_folder = tny_msg_get_folder (msg);
1314         if (msg_folder) {               
1315                 if (modest_tny_folder_is_local_folder (msg_folder)) {
1316                         TnyFolderType type = modest_tny_folder_get_local_or_mmc_folder_type (msg_folder);
1317                         if (type == TNY_FOLDER_TYPE_INVALID)
1318                                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1319                         
1320                         if (type == TNY_FOLDER_TYPE_DRAFTS) 
1321                                 priv->draft_msg = g_object_ref(msg);
1322                         if (type == TNY_FOLDER_TYPE_OUTBOX)
1323                                 priv->outbox_msg = g_object_ref(msg);
1324                         priv->msg_uid = modest_tny_folder_get_header_unique_id (header);
1325                 }
1326                 g_object_unref (msg_folder);
1327         }
1328
1329         g_free (to);
1330         g_free (subject);
1331         g_free (cc);
1332         g_free (bcc);
1333 }
1334
1335
1336 static void
1337 modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
1338 {
1339         ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1340         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1341         GtkWidget *placeholder;
1342         GtkWidget *tool_item;
1343         gint insert_index;
1344         gchar size_text[5];
1345         gint size_index;
1346         gint font_index;
1347         GtkWidget *sizes_menu;
1348         GtkWidget *fonts_menu;
1349         gchar *markup;
1350
1351         /* Toolbar */
1352         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
1353         gtk_toolbar_set_show_arrow (GTK_TOOLBAR (parent_priv->toolbar), FALSE);
1354         gtk_toolbar_set_icon_size (GTK_TOOLBAR (parent_priv->toolbar), HILDON_ICON_SIZE_FINGER);
1355         hildon_window_add_toolbar (HILDON_WINDOW (window), GTK_TOOLBAR (parent_priv->toolbar));
1356
1357         /* Font color placeholder */
1358         placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/FontColor");
1359         insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
1360
1361         /* font color */
1362         priv->font_color_toolitem = GTK_WIDGET (gtk_tool_item_new ());
1363         priv->font_color_button = hildon_color_button_new ();
1364         gtk_widget_set_size_request (priv->font_color_button, -1, 48);
1365         GTK_WIDGET_UNSET_FLAGS (priv->font_color_toolitem, GTK_CAN_FOCUS);
1366         GTK_WIDGET_UNSET_FLAGS (priv->font_color_button, GTK_CAN_FOCUS);
1367         gtk_container_add (GTK_CONTAINER (priv->font_color_toolitem), priv->font_color_button);
1368         gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->font_color_toolitem), TRUE);
1369         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (priv->font_color_toolitem), TRUE);
1370         gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (priv->font_color_toolitem), insert_index);
1371         g_signal_connect_swapped (G_OBJECT (priv->font_color_button), 
1372                                   "notify::color", 
1373                                   G_CALLBACK (modest_msg_edit_window_color_button_change), 
1374                                   window);
1375
1376         /* Font size and face placeholder */
1377         placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/FontAttributes");
1378         insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
1379         /* font_size */
1380         tool_item = GTK_WIDGET (gtk_tool_button_new (NULL, NULL));
1381         priv->size_tool_button_label = gtk_label_new (NULL);
1382         snprintf(size_text, sizeof(size_text), "%d", wp_font_size[DEFAULT_FONT_SIZE]);
1383         markup = g_strconcat ("<span font_family='", DEFAULT_SIZE_BUTTON_FONT_FAMILY, "'>",
1384                               size_text, "</span>", NULL);
1385         gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
1386         g_free (markup);
1387         hildon_helper_set_logical_font (priv->size_tool_button_label, "LargeSystemFont");
1388         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (tool_item), priv->size_tool_button_label);
1389         sizes_menu = gtk_menu_new ();
1390         priv->sizes_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
1391         for (size_index = 0; size_index < WP_FONT_SIZE_COUNT; size_index++) {
1392                 GtkTreeIter iter;
1393
1394                 snprintf(size_text, sizeof(size_text), "%d", wp_font_size[size_index]);
1395                 gtk_list_store_append (GTK_LIST_STORE (priv->sizes_model), &iter);
1396
1397                 gtk_list_store_set (GTK_LIST_STORE (priv->sizes_model), &iter, 
1398                                     0, size_text,
1399                                     -1);
1400
1401                 if (wp_font_size[size_index] == 12)
1402                         priv->current_size_index = size_index;
1403                                         
1404         }
1405
1406         g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (font_size_clicked), window);
1407         gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
1408         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
1409         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
1410         priv->font_size_toolitem = tool_item;
1411
1412         /* font face */
1413         tool_item = GTK_WIDGET (gtk_tool_button_new (NULL, NULL));
1414         priv->font_tool_button_label = gtk_label_new (NULL);
1415         markup = g_strconcat ("<span font_family='", wp_get_font_name(DEFAULT_FONT), "'>Tt</span>", NULL);
1416         gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
1417         g_free(markup);
1418         hildon_helper_set_logical_font (priv->font_tool_button_label, "LargeSystemFont");
1419         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (tool_item), priv->font_tool_button_label);
1420         fonts_menu = gtk_menu_new ();
1421         priv->faces_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
1422         for (font_index = 0; font_index < wp_get_font_count (); font_index++) {
1423                 GtkTreeIter iter;
1424
1425                 gtk_list_store_append (GTK_LIST_STORE (priv->faces_model), &iter);
1426
1427                 gtk_list_store_set (GTK_LIST_STORE (priv->faces_model), &iter, 
1428                                     0, wp_get_font_name (font_index),
1429                                     -1);
1430
1431                 if (font_index == DEFAULT_FONT)
1432                         priv->current_face_index = font_index;
1433         }
1434         g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (font_face_clicked), window);
1435         gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
1436         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
1437         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
1438         priv->font_face_toolitem = tool_item;
1439
1440         /* Set expand and homogeneous for remaining items */
1441         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarSend");
1442         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
1443         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
1444         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsBold");
1445         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
1446         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
1447         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsItalics");
1448         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
1449         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
1450
1451         /* Explicitelly show all the toolbar (a normal gtk_widget_show
1452            will not show the tool items added to the placeholders) */
1453         gtk_widget_show_all (parent_priv->toolbar);
1454
1455         /* Set the no show all *after* showing all items. We do not
1456            want the toolbar to be shown with a show all because it
1457            could go agains the gconf setting regarding showing or not
1458            the toolbar of the editor window */
1459         gtk_widget_set_no_show_all (parent_priv->toolbar, TRUE);
1460 }
1461
1462
1463
1464 ModestWindow*
1465 modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean preserve_is_rich)
1466 {
1467         GObject *obj;
1468         ModestWindowPrivate *parent_priv;
1469         ModestMsgEditWindowPrivate *priv;
1470         ModestDimmingRulesGroup *toolbar_rules_group = NULL;
1471         ModestDimmingRulesGroup *clipboard_rules_group = NULL;
1472         ModestWindowMgr *mgr = NULL;
1473
1474         g_return_val_if_fail (msg, NULL);
1475         g_return_val_if_fail (account_name, NULL);
1476
1477         mgr = modest_runtime_get_window_mgr ();
1478         
1479         obj = G_OBJECT (modest_window_mgr_get_msg_edit_window (mgr));
1480
1481         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
1482         parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
1483
1484         /* Menubar. Update the state of some toggles */
1485         priv->from_field_protos = get_transports ();
1486         modest_selector_picker_set_pair_list (MODEST_SELECTOR_PICKER (priv->from_field), priv->from_field_protos);
1487         modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field), (gpointer) account_name);
1488         priv->last_from_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
1489         hildon_button_set_title (HILDON_BUTTON (priv->from_field),
1490                                  _("mail_va_from"));
1491         hildon_button_set_value (HILDON_BUTTON (priv->from_field), 
1492                                  hildon_touch_selector_get_current_text 
1493                                  (HILDON_TOUCH_SELECTOR (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (priv->from_field)))));
1494         modest_msg_edit_window_setup_toolbar (MODEST_MSG_EDIT_WINDOW (obj));
1495         hildon_window_add_toolbar (HILDON_WINDOW (obj), GTK_TOOLBAR (priv->find_toolbar));
1496
1497         /* Init window */
1498         connect_signals (MODEST_MSG_EDIT_WINDOW(obj));
1499
1500         restore_settings (MODEST_MSG_EDIT_WINDOW(obj));
1501                 
1502         modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
1503
1504         priv->original_account_name = (account_name) ? g_strdup (account_name) : NULL;
1505
1506         toolbar_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_TOOLBAR, TRUE);
1507         clipboard_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_CLIPBOARD, FALSE);
1508         /* Add common dimming rules */
1509         modest_dimming_rules_group_add_rules (toolbar_rules_group, 
1510                                               modest_msg_edit_window_toolbar_dimming_entries,
1511                                               G_N_ELEMENTS (modest_msg_edit_window_toolbar_dimming_entries),
1512                                               MODEST_WINDOW (obj));
1513         modest_dimming_rules_group_add_widget_rule (toolbar_rules_group, priv->font_color_toolitem,
1514                                                     G_CALLBACK (modest_ui_dimming_rules_on_set_style),
1515                                                     MODEST_WINDOW (obj));
1516         modest_dimming_rules_group_add_widget_rule (toolbar_rules_group, priv->font_size_toolitem,
1517                                                     G_CALLBACK (modest_ui_dimming_rules_on_set_style),
1518                                                     MODEST_WINDOW (obj));
1519         modest_dimming_rules_group_add_widget_rule (toolbar_rules_group, priv->font_face_toolitem,
1520                                                     G_CALLBACK (modest_ui_dimming_rules_on_set_style),
1521                                                     MODEST_WINDOW (obj));
1522         /* Insert dimming rules group for this window */
1523         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, toolbar_rules_group);
1524         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, clipboard_rules_group);
1525
1526         /* Setup app menu */
1527         setup_menu (MODEST_MSG_EDIT_WINDOW (obj));
1528
1529         /* Checks the dimming rules */
1530         g_object_unref (toolbar_rules_group);
1531         g_object_unref (clipboard_rules_group);
1532         gtk_widget_hide (priv->priority_icon);
1533         gtk_widget_queue_resize (priv->subject_box);
1534         set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg, preserve_is_rich);
1535
1536         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (obj));
1537
1538         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (obj));
1539         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (obj));
1540         modest_window_check_dimming_rules_group (MODEST_WINDOW (obj), MODEST_DIMMING_RULES_CLIPBOARD);
1541         priv->update_caption_visibility = TRUE;
1542
1543         modest_msg_edit_window_set_modified (MODEST_MSG_EDIT_WINDOW (obj), FALSE);
1544
1545         /* Track account-removed signal, this window should be closed
1546            in the case we're creating a mail associated to the account
1547            that is deleted */
1548         priv->account_removed_handler_id = 
1549                 g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
1550                                   "account_removed",
1551                                   G_CALLBACK(on_account_removed),
1552                                   obj);
1553
1554         modest_msg_edit_window_clipboard_owner_handle_change_in_idle (MODEST_MSG_EDIT_WINDOW (obj));
1555
1556         return (ModestWindow*) obj;
1557 }
1558
1559 static gint
1560 get_formatted_data_cb (const gchar *buffer, gpointer user_data)
1561 {
1562         GString **string_buffer = (GString **) user_data;
1563
1564         *string_buffer = g_string_append (*string_buffer, buffer);
1565    
1566         return 0;
1567 }
1568
1569 /**
1570  * @result: A new string which should be freed with g_free().
1571  */
1572 static gchar *
1573 get_formatted_data (ModestMsgEditWindow *edit_window)
1574 {
1575         ModestMsgEditWindowPrivate *priv;
1576         GString *string_buffer = g_string_new ("");
1577         
1578         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
1579
1580         wp_text_buffer_save_document (WP_TEXT_BUFFER(priv->text_buffer), get_formatted_data_cb, &string_buffer);
1581
1582         modest_text_utils_hyperlinkify (string_buffer);
1583
1584         gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
1585
1586         return g_string_free (string_buffer, FALSE);
1587                                                                         
1588 }
1589
1590 MsgData * 
1591 modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
1592 {
1593         MsgData *data;
1594         const gchar *account_name;
1595         ModestMsgEditWindowPrivate *priv;
1596         TnyIterator *att_iter;
1597         
1598         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
1599
1600         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
1601                                                                         
1602         account_name = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
1603         g_return_val_if_fail (account_name, NULL);
1604         
1605         
1606         /* don't free these (except from) */
1607         data = g_slice_new0 (MsgData);
1608         data->from    =  modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
1609                                                              account_name);
1610         data->account_name = g_strdup (account_name);
1611         data->to      =  g_strdup (modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR (priv->to_field)));
1612         data->cc      =  g_strdup (modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR (priv->cc_field)));
1613         data->bcc     =  g_strdup (modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR (priv->bcc_field)));
1614         data->subject =  g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->subject_field)));
1615         if (priv->draft_msg) {
1616                 data->draft_msg = g_object_ref (priv->draft_msg);
1617         } else if (priv->outbox_msg) {
1618                 data->draft_msg = g_object_ref (priv->outbox_msg);
1619         } else {
1620                 data->draft_msg = NULL;
1621         }
1622
1623         GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
1624         GtkTextIter b, e;
1625         gtk_text_buffer_get_bounds (buf, &b, &e);
1626         data->plain_body = modest_text_utils_text_buffer_get_text (priv->text_buffer); /* returns a copy */
1627
1628         if (wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer)))
1629                 data->html_body = get_formatted_data (edit_window); /* returns a copy. */
1630         else
1631                 data->html_body = NULL;
1632
1633         /* deep-copy the data */
1634         att_iter = tny_list_create_iterator (priv->attachments);
1635         data->attachments = NULL;
1636         while (!tny_iterator_is_done (att_iter)) {
1637                 TnyMimePart *part = (TnyMimePart *) tny_iterator_get_current (att_iter);
1638                 if (!(TNY_IS_MIME_PART(part))) {
1639                         g_warning ("strange data in attachment list");
1640                         g_object_unref (part);
1641                         tny_iterator_next (att_iter);
1642                         continue;
1643                 }
1644                 data->attachments = g_list_append (data->attachments,
1645                                                    part);
1646                 tny_iterator_next (att_iter);
1647         }
1648         g_object_unref (att_iter);
1649
1650         GtkTextTagTable *tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (priv->text_buffer));
1651         att_iter = tny_list_create_iterator (priv->images);
1652         data->images = NULL;
1653         while (!tny_iterator_is_done (att_iter)) {
1654                 TnyMimePart *part = (TnyMimePart *) tny_iterator_get_current (att_iter);
1655                 const gchar *cid;
1656                 if (!(TNY_IS_MIME_PART(part))) {
1657                         g_warning ("strange data in attachment list");
1658                         g_object_unref (part);
1659                         tny_iterator_next (att_iter);
1660                         continue;
1661                 }
1662                 cid = tny_mime_part_get_content_id (part);
1663                 if (cid) {                      
1664                         gchar *image_tag_id;
1665                         GtkTextTag *image_tag;
1666                         GtkTextIter iter;
1667                         image_tag_id = g_strdup_printf ("image-tag-%s", cid);
1668                         image_tag = gtk_text_tag_table_lookup (tag_table, image_tag_id);
1669                         g_free (image_tag_id);
1670                         
1671                         gtk_text_buffer_get_start_iter (priv->text_buffer, &iter);
1672                         if (image_tag && 
1673                             ((gtk_text_iter_has_tag (&iter, image_tag))||
1674                              (gtk_text_iter_forward_to_tag_toggle (&iter, image_tag))))
1675                                 data->images = g_list_append (data->images,
1676                                                               g_object_ref (part));
1677                 }
1678                 g_object_unref (part);
1679                 tny_iterator_next (att_iter);
1680         }
1681         g_object_unref (att_iter);
1682         
1683         data->priority_flags = priv->priority_flags;
1684
1685         return data;
1686 }
1687
1688
1689 static void
1690 unref_gobject (GObject *obj, gpointer data)
1691 {
1692         if (!G_IS_OBJECT(obj))
1693                 return;
1694         g_object_unref (obj);
1695 }
1696
1697 void 
1698 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
1699                                                       MsgData *data)
1700 {
1701         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window));
1702
1703         if (!data)
1704                 return;
1705
1706         g_free (data->to);
1707         g_free (data->cc);
1708         g_free (data->bcc);
1709         g_free (data->from);
1710         g_free (data->subject);
1711         g_free (data->plain_body);
1712         g_free (data->html_body);
1713         g_free (data->account_name);
1714         
1715         if (data->draft_msg != NULL) {
1716                 g_object_unref (data->draft_msg);
1717                 data->draft_msg = NULL;
1718         }
1719         
1720         g_list_foreach (data->attachments, (GFunc)unref_gobject,  NULL);
1721         g_list_free (data->attachments);
1722         g_list_foreach (data->images, (GFunc)unref_gobject,  NULL);
1723         g_list_free (data->images);
1724         
1725         g_slice_free (MsgData, data);
1726 }
1727
1728 void                    
1729 modest_msg_edit_window_get_parts_size (ModestMsgEditWindow *window,
1730                                        gint *parts_count,
1731                                        guint64 *parts_size)
1732 {
1733         ModestMsgEditWindowPrivate *priv;
1734
1735         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1736
1737         modest_attachments_view_get_sizes (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), parts_count, parts_size);
1738
1739         /* TODO: add images */
1740         *parts_size += priv->images_size;
1741         *parts_count += priv->images_count;
1742
1743 }
1744
1745 ModestMsgEditFormat
1746 modest_msg_edit_window_get_format (ModestMsgEditWindow *self)
1747 {
1748         gboolean rich_text;
1749         ModestMsgEditWindowPrivate *priv = NULL;
1750         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), MODEST_MSG_EDIT_FORMAT_HTML);
1751
1752         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1753
1754         rich_text = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer));
1755         if (rich_text)
1756                 return MODEST_MSG_EDIT_FORMAT_HTML;
1757         else
1758                 return MODEST_MSG_EDIT_FORMAT_TEXT;
1759 }
1760
1761 void
1762 modest_msg_edit_window_set_format (ModestMsgEditWindow *self,
1763                                    ModestMsgEditFormat format)
1764 {
1765         ModestMsgEditWindowPrivate *priv;
1766
1767         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
1768         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1769
1770         switch (format) {
1771         case MODEST_MSG_EDIT_FORMAT_HTML:
1772                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
1773                 break;
1774         case MODEST_MSG_EDIT_FORMAT_TEXT:
1775                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
1776                 break;
1777         default:
1778                 g_return_if_reached ();
1779         }
1780 }
1781
1782 ModestMsgEditFormatState *
1783 modest_msg_edit_window_get_format_state (ModestMsgEditWindow *self)
1784 {
1785         ModestMsgEditFormatState *format_state = NULL;
1786         ModestMsgEditWindowPrivate *priv;
1787         WPTextBufferFormat *buffer_format;
1788
1789         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), NULL);
1790
1791         buffer_format = g_new0 (WPTextBufferFormat, 1);
1792         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1793
1794         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, TRUE);
1795
1796         format_state = g_new0 (ModestMsgEditFormatState, 1);
1797         format_state->bold = buffer_format->bold&0x1;
1798         format_state->italics = buffer_format->italic&0x1;
1799         format_state->bullet = buffer_format->bullet&0x1;
1800         format_state->color = buffer_format->color;
1801         format_state->font_size = buffer_format->font_size;
1802         format_state->font_family = wp_get_font_name (buffer_format->font);
1803         format_state->justification = buffer_format->justification;
1804         g_free (buffer_format);
1805
1806         return format_state;
1807  
1808 }
1809
1810 void
1811 modest_msg_edit_window_set_format_state (ModestMsgEditWindow *self,
1812                                          const ModestMsgEditFormatState *format_state)
1813 {
1814         ModestMsgEditWindowPrivate *priv;
1815         WPTextBufferFormat *buffer_format;
1816         WPTextBufferFormat *current_format;
1817
1818         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
1819         g_return_if_fail (format_state != NULL);
1820
1821         buffer_format = g_new0 (WPTextBufferFormat, 1);
1822         current_format = g_new0 (WPTextBufferFormat, 1);
1823
1824         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
1825         gtk_widget_grab_focus (priv->msg_body);
1826         buffer_format->bold = (format_state->bold != FALSE);
1827         buffer_format->italic = (format_state->italics != FALSE);
1828         buffer_format->color = format_state->color;
1829         buffer_format->font_size = format_state->font_size;
1830         buffer_format->font = wp_get_font_index (format_state->font_family, 0);
1831         buffer_format->justification = format_state->justification;
1832         buffer_format->bullet = format_state->bullet;
1833
1834         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), current_format, TRUE);
1835
1836         buffer_format->cs.bold = ((buffer_format->bold&0x1) != (current_format->bold&0x1));
1837         buffer_format->cs.italic = ((buffer_format->italic&0x1) != (current_format->italic&0x1));
1838         buffer_format->cs.color = !gdk_color_equal(&(buffer_format->color), &(current_format->color));
1839         buffer_format->cs.font_size =  (buffer_format->font_size != current_format->font_size);
1840         buffer_format->cs.font = (buffer_format->font != current_format->font);
1841         buffer_format->cs.justification = (buffer_format->justification != current_format->justification);
1842         buffer_format->cs.bullet = (buffer_format->bullet != current_format->bullet);
1843
1844         wp_text_buffer_freeze (WP_TEXT_BUFFER (priv->text_buffer));
1845         if (buffer_format->cs.bold) {
1846                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD,
1847                                               GINT_TO_POINTER (buffer_format->bold&0x1));
1848         }
1849         if (buffer_format->cs.italic) {
1850                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_ITALIC,
1851                                               GINT_TO_POINTER (buffer_format->italic&0x1));
1852         }
1853         if (buffer_format->cs.color) {
1854                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR,
1855                                               GINT_TO_POINTER (&(buffer_format->color)));
1856         }
1857         if (buffer_format->cs.font_size) {
1858                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT_SIZE,
1859                                               GINT_TO_POINTER (buffer_format->font_size));
1860         }
1861         if (buffer_format->cs.justification) {
1862                 switch (buffer_format->justification) {
1863                 case GTK_JUSTIFY_LEFT:
1864                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_LEFT,
1865                                                       GINT_TO_POINTER(TRUE));
1866                         break;
1867                 case GTK_JUSTIFY_CENTER:
1868                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_CENTER,
1869                                                       GINT_TO_POINTER(TRUE));
1870                         break;
1871                 case GTK_JUSTIFY_RIGHT:
1872                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_RIGHT,
1873                                                       GINT_TO_POINTER(TRUE));
1874                         break;
1875                 default:
1876                         break;
1877                 }
1878                         
1879         }
1880         if (buffer_format->cs.font) {
1881                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT,
1882                                               GINT_TO_POINTER (buffer_format->font));
1883         }
1884         wp_text_buffer_thaw (WP_TEXT_BUFFER (priv->text_buffer));
1885         if (buffer_format->cs.bullet) {
1886                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BULLET,
1887                                               GINT_TO_POINTER ((buffer_format->bullet)?1:0));
1888         }
1889 /*      wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), buffer_format); */
1890         
1891         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), self);
1892         
1893         g_free (buffer_format);
1894         g_free (current_format);
1895
1896         /* Check dimming rules */
1897         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (self));
1898         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
1899 }
1900
1901 static void
1902 text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window)
1903 {
1904         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
1905         GtkAction *action;
1906         ModestWindowPrivate *parent_priv;
1907         ModestMsgEditWindowPrivate *priv;
1908         
1909         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1910         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1911
1912         if (wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer))) {
1913                 action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/FileFormatFormattedTextMenu");
1914                 if (!gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
1915                         modest_utils_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), TRUE);
1916         } else {
1917                 action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/FileFormatPlainTextMenu");
1918                 if (!gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
1919                         modest_utils_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), TRUE);
1920         }
1921
1922         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
1923
1924         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsBold");
1925         modest_utils_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bold);
1926
1927         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsItalics");
1928         modest_utils_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->italic);
1929
1930 /*      action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/BulletedListMenu"); */
1931 /*      modest_utils_toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bullet); */
1932
1933         action = NULL;
1934         switch (buffer_format->justification)
1935         {
1936         case GTK_JUSTIFY_LEFT:
1937                 action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/AlignmentMenu/AlignmentLeftMenu");
1938                 break;
1939         case GTK_JUSTIFY_CENTER:
1940                 action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/AlignmentMenu/AlignmentCenterMenu");
1941                 break;
1942         case GTK_JUSTIFY_RIGHT:
1943                 action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/AlignmentMenu/AlignmentRightMenu");
1944                 break;
1945         default:
1946                 break;
1947         }
1948         
1949         if (action != NULL)
1950                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1951         
1952         g_signal_handlers_block_by_func (G_OBJECT (priv->font_color_button), 
1953                                          G_CALLBACK (modest_msg_edit_window_color_button_change),
1954                                          window);
1955         hildon_color_button_set_color (HILDON_COLOR_BUTTON (priv->font_color_button), & (buffer_format->color));
1956         g_signal_handlers_unblock_by_func (G_OBJECT (priv->font_color_button), 
1957                                            G_CALLBACK (modest_msg_edit_window_color_button_change),
1958                                            window);
1959
1960         if (priv->current_size_index != buffer_format->font_size) {
1961                 GtkTreeIter iter;
1962                 GtkTreePath *path;
1963
1964                 path = gtk_tree_path_new_from_indices (buffer_format->font_size, -1);
1965                 if (gtk_tree_model_get_iter (priv->sizes_model, &iter, path)) {
1966                         gchar *size_text;
1967                         gchar *markup;
1968
1969                         priv->current_size_index = buffer_format->font_size;
1970
1971                         gtk_tree_model_get (priv->sizes_model, &iter, 0, &size_text, -1);
1972                         markup = g_strconcat ("<span font_family='Sans'>", 
1973                                               size_text, "</span>", NULL);
1974                         
1975                         gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
1976                         g_free (markup);
1977                         g_free (size_text);
1978                 }
1979                 gtk_tree_path_free (path);              
1980         }
1981
1982         if (priv->current_face_index != buffer_format->font) {
1983                 GtkTreeIter iter;
1984                 GtkTreePath *path;
1985
1986                 path = gtk_tree_path_new_from_indices (buffer_format->font, -1);
1987                 if (gtk_tree_model_get_iter (priv->faces_model, &iter, path)) {
1988                         gchar *face_name;
1989                         gchar *markup;
1990
1991                         priv->current_face_index = buffer_format->font;
1992                         gtk_tree_model_get (priv->faces_model, &iter, 0, &face_name, -1);
1993                         markup = g_strconcat ("<span font_family='", face_name, "'>Tt</span>", NULL);
1994                         gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
1995                         g_free (face_name);
1996                         g_free (markup);
1997                 }
1998
1999         }
2000
2001         g_free (buffer_format);
2002
2003 }
2004
2005 #ifdef MODEST_HILDON_VERSION_0
2006 void
2007 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
2008 {
2009         
2010         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
2011         ModestMsgEditWindowPrivate *priv;
2012         GtkWidget *dialog = NULL;
2013         gint response;
2014         GdkColor *new_color = NULL;
2015
2016         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2017         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
2018         
2019         dialog = hildon_color_selector_new (GTK_WINDOW (window));
2020         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), &(buffer_format->color));
2021         g_free (buffer_format);
2022
2023         if (gtk_dialog_run (GTK_DIALOG (dialog) == GTK_RESPONSE_OK)) {
2024                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
2025                 if (new_color != NULL) {
2026                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR,
2027                                                       (gpointer) new_color);
2028                 }
2029         }
2030         gtk_widget_destroy (dialog);
2031 }
2032
2033
2034 void
2035 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
2036 {
2037         
2038         ModestMsgEditWindowPrivate *priv;
2039         GtkWidget *dialog = NULL;
2040         gint response;
2041         GdkColor *old_color = NULL;
2042         const GdkColor *new_color = NULL;
2043         
2044         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2045         old_color = (GdkColor*)wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
2046         
2047         dialog = hildon_color_selector_new (GTK_WINDOW (window));
2048         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog),(GdkColor*)old_color);
2049
2050         if (gtk_dialog_run (GTK_DIALOG (dialog) == GTK_RESPONSE_OK)) {
2051                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
2052                 if (new_color != NULL)
2053                         wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), new_color);
2054         }
2055         gtk_widget_destroy (dialog);
2056
2057 }
2058
2059 #else 
2060 void
2061 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
2062 {
2063         
2064         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
2065         ModestMsgEditWindowPrivate *priv;
2066         GtkWidget *dialog = NULL;
2067
2068         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2069         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
2070                 
2071         dialog = hildon_color_chooser_new ();
2072         hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog), &(buffer_format->color));
2073         g_free (buffer_format);
2074
2075         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
2076                 GdkColor col;
2077                 hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER(dialog), &col);
2078                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR,
2079                                               (gpointer) &col);
2080         }
2081         gtk_widget_destroy (dialog);
2082 }
2083
2084
2085 void
2086 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
2087 {
2088         
2089         ModestMsgEditWindowPrivate *priv;
2090         GtkWidget *dialog = NULL;
2091         GdkColor *old_color = NULL;
2092         
2093         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2094         old_color = (GdkColor*)wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
2095         
2096         dialog = hildon_color_chooser_new ();
2097         hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog),(GdkColor*)old_color);
2098
2099         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) { 
2100                 GdkColor col;
2101                 hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER(dialog), &col);
2102                 wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), &col);
2103         }
2104         gtk_widget_destroy (dialog);
2105 }
2106
2107 #endif /*!MODEST_HILDON_VERSION_0*/
2108
2109
2110
2111 static TnyStream*
2112 create_stream_for_uri (const gchar* uri)
2113 {
2114         if (!uri)
2115                 return NULL;
2116                 
2117         TnyStream *result = NULL;
2118
2119         GnomeVFSHandle *handle = NULL;
2120         GnomeVFSResult test = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
2121         if (test == GNOME_VFS_OK) {
2122                 TnyStream *vfssstream = TNY_STREAM (tny_vfs_stream_new (handle));
2123                 /* Streams over OBEX (Bluetooth) are not seekable but
2124                  * we expect them to be (we might need to read them
2125                  * several times). So if this is a Bluetooth URI just
2126                  * read the whole file into memory (this is not a fast
2127                  * protocol so we can assume that these files are not
2128                  * going to be very big) */
2129                 if ((g_ascii_strncasecmp (uri, "obex://", 7) == 0)||
2130                     (g_ascii_strncasecmp (uri, "upnpav://", 9) == 0)) {
2131                         TnyStream *memstream = tny_camel_mem_stream_new ();
2132                         tny_stream_write_to_stream (vfssstream, memstream);
2133                         g_object_unref (vfssstream);
2134                         result = memstream;
2135                 } else {
2136                         result = vfssstream;
2137                 }
2138         }
2139         
2140         return result;
2141 }
2142
2143 void
2144 modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
2145 {
2146         
2147         ModestMsgEditWindowPrivate *priv;
2148         GtkWidget *dialog = NULL;
2149         gint response = 0;
2150         GSList *uris = NULL;
2151         GSList *uri_node = NULL;
2152
2153         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2154
2155         dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_OPEN);
2156         gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ia_select_inline_image_title"));
2157         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
2158
2159         modest_maemo_utils_setup_images_filechooser (GTK_FILE_CHOOSER (dialog));
2160
2161         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
2162                                      GTK_WINDOW (dialog), GTK_WINDOW (window));
2163
2164         response = gtk_dialog_run (GTK_DIALOG (dialog));
2165         switch (response) {
2166         case GTK_RESPONSE_OK:
2167                 uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
2168                 break;
2169         default:
2170                 break;
2171         }
2172         gtk_widget_destroy (dialog);
2173
2174         g_object_ref (window);
2175         /* The operation could take some time so allow the dialog to be closed */
2176         while (gtk_events_pending ())
2177                 gtk_main_iteration ();
2178
2179         for (uri_node = uris; uri_node != NULL; uri_node = g_slist_next (uri_node)) {
2180                 const gchar *uri;
2181                 GnomeVFSHandle *handle = NULL;
2182                 GnomeVFSResult result;
2183                 GtkTextIter position;
2184                 GtkTextMark *insert_mark;
2185
2186                 uri = (const gchar *) uri_node->data;
2187                 result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
2188                 if (result == GNOME_VFS_OK) {
2189                         GdkPixbuf *pixbuf;
2190                         GnomeVFSFileInfo *info;
2191                         gchar *filename, *basename, *escaped_filename;
2192                         TnyMimePart *mime_part;
2193                         gchar *content_id;
2194                         const gchar *mime_type = NULL;
2195                         GnomeVFSURI *vfs_uri;
2196                         guint64 stream_size;
2197
2198                         gnome_vfs_close (handle);
2199                         vfs_uri = gnome_vfs_uri_new (uri);
2200
2201                         escaped_filename = g_path_get_basename (gnome_vfs_uri_get_path (vfs_uri));
2202                         filename = gnome_vfs_unescape_string_for_display (escaped_filename);
2203                         g_free (escaped_filename);
2204                         gnome_vfs_uri_unref (vfs_uri);
2205                         info = gnome_vfs_file_info_new ();
2206
2207                         if (gnome_vfs_get_file_info (uri, info, GNOME_VFS_FILE_INFO_GET_MIME_TYPE
2208                                                      | GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE) 
2209                             == GNOME_VFS_OK)
2210                                 mime_type = gnome_vfs_file_info_get_mime_type (info);
2211
2212                         mime_part = tny_platform_factory_new_mime_part
2213                                 (modest_runtime_get_platform_factory ());
2214
2215                         TnyStream *stream = create_stream_for_uri (uri);
2216
2217                         if (stream == NULL) {
2218
2219                                 modest_platform_information_banner (NULL, NULL, 
2220                                                                     _FM("sfil_ib_opening_not_allowed"));
2221                                 g_free (filename);
2222                                 g_object_unref (mime_part);
2223                                 gnome_vfs_file_info_unref (info);
2224                                 continue;
2225                         }
2226
2227                         tny_mime_part_construct (mime_part, stream, mime_type, "base64");
2228
2229                         content_id = g_strdup_printf ("%d", priv->next_cid);
2230                         tny_mime_part_set_content_id (mime_part, content_id);
2231                         g_free (content_id);
2232                         priv->next_cid++;
2233
2234                         basename = g_path_get_basename (filename);
2235                         tny_mime_part_set_filename (mime_part, basename);
2236                         g_free (basename);
2237
2238                         pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size, window);
2239
2240                         if (pixbuf != NULL) {
2241                                 priv->images_size += stream_size;
2242                                 priv->images_count ++;
2243                                 insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
2244                                 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &position, insert_mark);
2245                                 wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, 
2246                                                              tny_mime_part_get_content_id (mime_part), pixbuf);
2247                                 g_object_unref (pixbuf);
2248
2249                                 tny_list_prepend (priv->images, (GObject *) mime_part);
2250                                 gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
2251                         } else {
2252                                 modest_platform_information_banner (NULL, NULL,
2253                                                                     _("mail_ib_file_operation_failed"));
2254                         }
2255
2256                         g_free (filename);
2257                         g_object_unref (mime_part);
2258                         gnome_vfs_file_info_unref (info);
2259
2260                 }
2261         }
2262         g_object_unref (window);
2263 }
2264
2265 static void
2266 on_attach_file_response (GtkDialog *dialog,
2267                          gint       arg1,
2268                          gpointer   user_data)
2269 {
2270         GSList *uris = NULL;
2271         GSList *uri_node;
2272         GnomeVFSFileSize total_size, allowed_size;
2273         ModestMsgEditWindow *window;
2274         ModestMsgEditWindowPrivate *priv;
2275         gint att_num;
2276         guint64 att_size;
2277
2278         switch (arg1) {
2279         case GTK_RESPONSE_OK:
2280                 uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
2281                 break;
2282         default:
2283                 break;
2284         }
2285
2286         window = MODEST_MSG_EDIT_WINDOW (user_data);
2287         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2288
2289         /* allowed size is the maximum size - what's already there */
2290         modest_attachments_view_get_sizes (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
2291                                            &att_num, &att_size);
2292         allowed_size = MODEST_MAX_ATTACHMENT_SIZE - att_size;
2293
2294         total_size = 0;
2295         for (uri_node = uris; uri_node != NULL; uri_node = g_slist_next (uri_node)) {
2296
2297                 const gchar *uri = (const gchar *) uri_node->data;
2298
2299                 total_size += 
2300                         modest_msg_edit_window_attach_file_one (window, uri, allowed_size);
2301
2302                 if (total_size > allowed_size) {
2303                         g_warning ("%s: total size: %u", 
2304                                    __FUNCTION__, (unsigned int)total_size);
2305                         break;
2306                 }
2307                 allowed_size -= total_size;
2308         }
2309         g_slist_foreach (uris, (GFunc) g_free, NULL);
2310         g_slist_free (uris);
2311
2312         gtk_widget_destroy (GTK_WIDGET (dialog));
2313 }
2314
2315 void
2316 modest_msg_edit_window_offer_attach_file (ModestMsgEditWindow *window)
2317 {
2318         GtkWidget *dialog = NULL;
2319         ModestMsgEditWindowPrivate *priv;
2320
2321         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(window));
2322
2323         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2324
2325         if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
2326                 return;
2327
2328         dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), 
2329                                                  GTK_FILE_CHOOSER_ACTION_OPEN);
2330         gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title"));
2331         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
2332         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
2333                                      GTK_WINDOW (dialog), GTK_WINDOW (window));
2334
2335         /* Connect to response & show */
2336         g_signal_connect (dialog, "response", 
2337                           G_CALLBACK (on_attach_file_response), window);
2338         gtk_widget_show (dialog);
2339 }
2340
2341
2342 GnomeVFSFileSize
2343 modest_msg_edit_window_attach_file_one (ModestMsgEditWindow *window,
2344                                         const gchar *uri, 
2345                                         GnomeVFSFileSize allowed_size)
2346
2347 {
2348         GnomeVFSHandle *handle = NULL;
2349         ModestMsgEditWindowPrivate *priv;
2350         GnomeVFSResult result;
2351         GnomeVFSFileSize size = 0;
2352         g_return_val_if_fail (window, 0);
2353         g_return_val_if_fail (uri, 0);
2354
2355         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2356
2357         result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
2358         if (result == GNOME_VFS_OK) {
2359                 TnyMimePart *mime_part;
2360                 TnyStream *stream;
2361                 const gchar *mime_type = NULL;
2362                 gchar *basename;
2363                 gchar *escaped_filename;
2364                 gchar *filename;
2365                 gchar *content_id;
2366                 GnomeVFSFileInfo *info;
2367                 GnomeVFSURI *vfs_uri;
2368
2369                 gnome_vfs_close (handle);
2370                 vfs_uri = gnome_vfs_uri_new (uri);
2371                 
2372
2373                 escaped_filename = g_path_get_basename (gnome_vfs_uri_get_path (vfs_uri));
2374                 filename = gnome_vfs_unescape_string_for_display (escaped_filename);
2375                 g_free (escaped_filename);
2376                 gnome_vfs_uri_unref (vfs_uri);
2377
2378                 info = gnome_vfs_file_info_new ();
2379                 
2380                 if (gnome_vfs_get_file_info (uri, 
2381                                              info, 
2382                                              GNOME_VFS_FILE_INFO_GET_MIME_TYPE)
2383                     == GNOME_VFS_OK)
2384                         mime_type = gnome_vfs_file_info_get_mime_type (info);
2385                 mime_part = tny_platform_factory_new_mime_part
2386                         (modest_runtime_get_platform_factory ());
2387                 
2388                 /* try to get the attachment's size; this may fail for weird
2389                  * file systems, like obex, upnp... */
2390                 if (allowed_size != 0 &&
2391                     info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) {
2392                         size = info->size;
2393                         if (size > allowed_size) {
2394                                 modest_platform_information_banner (NULL, NULL, 
2395                                                                     _FM("sfil_ib_opening_not_allowed"));
2396                                 g_free (filename);
2397                                 return 0;
2398                         }
2399                 } else
2400                         g_warning ("%s: could not get attachment size", __FUNCTION__);
2401                 
2402                 stream = create_stream_for_uri (uri);
2403                 
2404                 if (stream == NULL) {
2405
2406                         modest_platform_information_banner (NULL, NULL, _FM("sfil_ib_opening_not_allowed"));
2407
2408                         g_object_unref (mime_part);
2409                         g_free (filename);
2410                         gnome_vfs_file_info_unref (info);
2411                         return 0;
2412                 }
2413
2414                 tny_mime_part_construct (mime_part, stream, mime_type, "base64");
2415                 g_object_unref (stream);
2416                 
2417                 content_id = g_strdup_printf ("%d", priv->next_cid);
2418                 tny_mime_part_set_content_id (mime_part, content_id);
2419                 g_free (content_id);
2420                 priv->next_cid++;
2421                 
2422                 basename = g_path_get_basename (filename);
2423                 tny_mime_part_set_filename (mime_part, basename);
2424                 g_free (basename);
2425                 
2426                 tny_list_prepend (priv->attachments, (GObject *) mime_part);
2427                 modest_attachments_view_add_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
2428                                                         mime_part,
2429                                                         info->size == 0, info->size);
2430                 gtk_widget_set_no_show_all (priv->attachments_caption, FALSE);
2431                 gtk_widget_show_all (priv->attachments_caption);
2432                 gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
2433                 g_free (filename);
2434                 g_object_unref (mime_part);
2435                 gnome_vfs_file_info_unref (info);
2436         }
2437
2438         return size;
2439 }
2440
2441 void
2442 modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
2443                                            TnyList *att_list)
2444 {
2445         ModestMsgEditWindowPrivate *priv;
2446         TnyIterator *iter;
2447
2448         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2449         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2450
2451         if (att_list == NULL) {
2452                 att_list = modest_attachments_view_get_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
2453                 if (!modest_maemo_utils_select_attachments (GTK_WINDOW (window), att_list, TRUE)) {
2454                         g_object_unref (att_list);
2455                         return;
2456                 }
2457                 
2458         } else {
2459                 g_object_ref (att_list);
2460         }
2461
2462         if (tny_list_get_length (att_list) == 0) {
2463                 hildon_banner_show_information (NULL, NULL, _("TODO: no attachments selected to remove"));
2464         } else {
2465                 gboolean dialog_response;
2466                 gchar *message = NULL;
2467                 gchar *filename = NULL;
2468
2469                 if (tny_list_get_length (att_list) == 1) {
2470                         TnyMimePart *part;
2471                         iter = tny_list_create_iterator (att_list);
2472                         part = (TnyMimePart *) tny_iterator_get_current (iter);
2473                         g_object_unref (iter);
2474                         if (TNY_IS_MSG (part)) {
2475                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
2476                                 if (header) {
2477                                         filename = tny_header_dup_subject (header);
2478                                         g_object_unref (header);
2479                                 }
2480                                 if (filename == NULL) {
2481                                         filename = g_strdup (_("mail_va_no_subject"));
2482                                 }
2483                         } else {
2484                                 filename = g_strdup (tny_mime_part_get_filename (TNY_MIME_PART (part)));
2485                         }
2486                         g_object_unref (part);
2487                 } else {
2488                         filename = g_strdup ("");
2489                 }
2490                 message = g_strdup_printf (ngettext("emev_nc_delete_attachment", 
2491                                                     "emev_nc_delete_attachments",
2492                                                     tny_list_get_length (att_list)), filename);
2493                 g_free (filename);
2494
2495                 dialog_response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window), 
2496                                                                            message);
2497                 g_free (message);
2498
2499                 if (dialog_response != GTK_RESPONSE_OK) {
2500                         g_object_unref (att_list);
2501                         return;
2502                 }
2503
2504                 for (iter = tny_list_create_iterator (att_list);
2505                      !tny_iterator_is_done (iter);
2506                      tny_iterator_next (iter)) {
2507                         TnyMimePart *mime_part = (TnyMimePart *) tny_iterator_get_current (iter);
2508                         const gchar *att_id;
2509                         tny_list_remove (priv->attachments, (GObject *) mime_part);
2510
2511                         modest_attachments_view_remove_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
2512                                                                    mime_part);
2513                         if (tny_list_get_length (priv->attachments) == 0)
2514                                 gtk_widget_hide (priv->attachments_caption);
2515                         att_id = tny_mime_part_get_content_id (mime_part);
2516                         if (att_id != NULL)
2517                                 text_buffer_delete_images_by_id (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body)),
2518                                                                  att_id);
2519                         gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
2520                         g_object_unref (mime_part);
2521                 }
2522                 g_object_unref (iter);
2523         }
2524
2525         g_object_unref (att_list);
2526
2527         /* if the last attachment has been removed, focus the Subject: field */
2528         if (!modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view))) 
2529                 gtk_widget_grab_focus (priv->subject_field);
2530 }
2531
2532 static void
2533 modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
2534                                             gpointer userdata)
2535 {
2536         ModestMsgEditWindowPrivate *priv;
2537         GdkColor *new_color;
2538         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2539         
2540 #ifdef MODEST_HAVE_HILDON0_WIDGETS      
2541         new_color = hildon_color_button_get_color (HILDON_COLOR_BUTTON (priv->font_color_button));
2542 #else 
2543         GdkColor col;
2544         hildon_color_button_get_color (HILDON_COLOR_BUTTON(priv->font_color_button), &col);
2545         new_color = &col;
2546 #endif /*#ifdef MODEST_HAVE_HILDON0_WIDGETS*/
2547
2548         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
2549         
2550         gtk_window_set_focus (GTK_WINDOW (window), priv->msg_body);
2551
2552 }
2553
2554 static void
2555 font_size_clicked (GtkToolButton *button,
2556                    ModestMsgEditWindow *window)
2557 {
2558         ModestMsgEditWindowPrivate *priv;
2559         GtkWidget *selector, *dialog;
2560         
2561         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2562
2563         selector = hildon_touch_selector_new ();
2564         hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector), priv->sizes_model, TRUE);
2565         hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, priv->current_size_index);
2566
2567         dialog = hildon_picker_dialog_new (GTK_WINDOW (window));
2568         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
2569
2570         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
2571                 gint new_index;
2572                 gchar *size_text;
2573                 gchar *markup;
2574                 WPTextBufferFormat format;
2575
2576                 new_index = hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (selector), 0);
2577
2578                 memset (&format, 0, sizeof (format));
2579                 wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), &format, FALSE);
2580
2581                 format.cs.font_size = TRUE;
2582                 format.cs.text_position = TRUE;
2583                 format.cs.font = TRUE;
2584                 format.font_size = new_index;
2585 /*              wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), &format); */
2586
2587                 if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT_SIZE,
2588                                                    GINT_TO_POINTER (new_index)))
2589                         wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
2590                 
2591                 text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
2592                 size_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
2593                 markup = g_strconcat ("<span font_family='", DEFAULT_SIZE_BUTTON_FONT_FAMILY, "'>", 
2594                                       size_text, "</span>", NULL);
2595                 g_free (size_text);
2596                 gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
2597                 g_free (markup);
2598
2599         }
2600         gtk_widget_destroy (dialog);
2601
2602         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
2603
2604 }
2605
2606 static void
2607 font_face_clicked (GtkToolButton *button,
2608                    ModestMsgEditWindow *window)
2609 {
2610         ModestMsgEditWindowPrivate *priv;
2611         GtkWidget *selector, *dialog;
2612         GtkCellRenderer *renderer;
2613         
2614         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2615
2616         selector = hildon_touch_selector_new ();
2617         renderer = gtk_cell_renderer_text_new ();
2618         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), priv->faces_model, 
2619                                              renderer, "family", 0, "text", 0, NULL);
2620         hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, priv->current_face_index);
2621
2622         dialog = hildon_picker_dialog_new (GTK_WINDOW (window));
2623         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
2624
2625         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
2626                 gint new_font_index;
2627                 GtkTreePath *path;
2628                 GtkTreeIter iter;
2629
2630                 new_font_index = hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (selector), 0);
2631                 path = gtk_tree_path_new_from_indices (new_font_index, -1);
2632                 if (gtk_tree_model_get_iter (priv->faces_model, &iter, path)) {
2633                         gchar *face_name;
2634                         gchar *markup;
2635
2636                         gtk_tree_model_get (priv->faces_model, &iter, 0, &face_name, -1);
2637
2638                         if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT, 
2639                                                            GINT_TO_POINTER(new_font_index)))
2640                                 wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
2641
2642                         markup = g_strconcat ("<span font_family='", face_name, "'>Tt</span>", NULL);
2643                         gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
2644
2645                         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
2646                         g_free (face_name);
2647                         g_free (markup);
2648                 }
2649                 gtk_tree_path_free (path);
2650
2651         }
2652         gtk_widget_destroy (dialog);
2653
2654         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
2655
2656 }
2657
2658 void
2659 modest_msg_edit_window_show_cc (ModestMsgEditWindow *window, 
2660                                 gboolean show)
2661 {
2662         ModestMsgEditWindowPrivate *priv = NULL;
2663         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2664
2665         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2666         if (!priv->update_caption_visibility)
2667                 return;
2668
2669         gtk_widget_set_no_show_all (priv->cc_caption, TRUE);
2670         if (show)
2671                 gtk_widget_show (priv->cc_caption);
2672         else
2673                 gtk_widget_hide (priv->cc_caption);
2674
2675         modest_conf_set_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_CC, show, NULL);
2676 }
2677
2678 void
2679 modest_msg_edit_window_show_bcc (ModestMsgEditWindow *window, 
2680                                  gboolean show)
2681 {
2682         ModestMsgEditWindowPrivate *priv = NULL;
2683         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2684
2685         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2686         if (!priv->update_caption_visibility)
2687                 return;
2688
2689         gtk_widget_set_no_show_all (priv->bcc_caption, TRUE);
2690         if (show)
2691                 gtk_widget_show (priv->bcc_caption);
2692         else
2693                 gtk_widget_hide (priv->bcc_caption);
2694
2695         modest_conf_set_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_BCC, show, NULL);
2696 }
2697
2698 static void
2699 modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
2700                                          ModestRecptEditor *editor)
2701 {
2702         ModestMsgEditWindowPrivate *priv;
2703
2704         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2705         g_return_if_fail ((editor == NULL) || (MODEST_IS_RECPT_EDITOR (editor)));
2706         
2707         /* we check for low-mem; in that case, show a warning, and don't allow
2708          * for the addressbook
2709          */
2710         if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
2711                 return;
2712
2713         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2714
2715         if (editor == NULL) {
2716                 GtkWidget *view_focus;
2717                 view_focus = gtk_window_get_focus (GTK_WINDOW (window));
2718
2719                 /* This code should be kept in sync with ModestRecptEditor. The
2720                    textview inside the recpt editor is the one that really gets the
2721                    focus. As it's inside a scrolled window, and this one inside the
2722                    hbox recpt editor inherits from, we'll need to go up in the 
2723                    hierarchy to know if the text view is part of the recpt editor
2724                    or if it's a different text entry */
2725
2726                 if (gtk_widget_get_parent (view_focus)) {
2727                         GtkWidget *first_parent;
2728
2729                         first_parent = gtk_widget_get_parent (view_focus);
2730                         if (gtk_widget_get_parent (first_parent) && 
2731                             MODEST_IS_RECPT_EDITOR (gtk_widget_get_parent (first_parent))) {
2732                                 editor = MODEST_RECPT_EDITOR (gtk_widget_get_parent (first_parent));
2733                         }
2734                 }
2735
2736                 if (editor == NULL)
2737                         editor = MODEST_RECPT_EDITOR (priv->to_field);
2738
2739         }
2740
2741         modest_address_book_select_addresses (editor);
2742
2743 }
2744
2745 void
2746 modest_msg_edit_window_select_contacts (ModestMsgEditWindow *window)
2747 {
2748         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2749
2750         modest_msg_edit_window_open_addressbook (window, NULL);
2751 }
2752
2753 static void
2754 modest_msg_edit_window_show_toolbar (ModestWindow *self,
2755                                      gboolean show_toolbar)
2756 {
2757         ModestWindowPrivate *parent_priv;
2758
2759         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
2760         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
2761
2762         /* We can not just use the code of
2763            modest_msg_edit_window_setup_toolbar because it has a
2764            mixture of both initialization and creation code. */
2765         if (show_toolbar)
2766                 gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
2767         else
2768                 gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
2769 }
2770
2771 void
2772 modest_msg_edit_window_set_priority_flags (ModestMsgEditWindow *window,
2773                                            TnyHeaderFlags priority_flags)
2774 {
2775         ModestMsgEditWindowPrivate *priv;
2776         ModestWindowPrivate *parent_priv;
2777
2778         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2779
2780         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2781         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
2782
2783         if (priv->priority_flags != priority_flags) {
2784                 GtkAction *priority_action = NULL;
2785
2786                 priv->priority_flags = priority_flags;
2787
2788                 switch (priority_flags) {
2789                 case TNY_HEADER_FLAG_HIGH_PRIORITY:
2790                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon),
2791                                                       MODEST_HEADER_ICON_HIGH, 
2792                                                       HILDON_ICON_SIZE_SMALL);
2793                         gtk_widget_show (priv->priority_icon);
2794                         priority_action = gtk_ui_manager_get_action (parent_priv->ui_manager,
2795                                                                      "/MenuBar/ToolsMenu/MessagePriorityMenu/MessagePriorityHighMenu");
2796                         break;
2797                 case TNY_HEADER_FLAG_LOW_PRIORITY:
2798                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon),
2799                                                       MODEST_HEADER_ICON_LOW,
2800                                                       HILDON_ICON_SIZE_SMALL);
2801                         gtk_widget_show (priv->priority_icon);
2802                         priority_action = gtk_ui_manager_get_action (parent_priv->ui_manager,
2803                                                                      "/MenuBar/ToolsMenu/MessagePriorityMenu/MessagePriorityLowMenu");
2804                         break;
2805                 default:
2806                         gtk_widget_hide (priv->priority_icon);
2807                         priority_action = gtk_ui_manager_get_action (parent_priv->ui_manager,
2808                                                                      "/MenuBar/ToolsMenu/MessagePriorityMenu/MessagePriorityNormalMenu");
2809                         break;
2810                 }
2811                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priority_action), TRUE);
2812                 gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
2813         }
2814         gtk_widget_queue_resize (priv->subject_box);
2815 }
2816
2817 void
2818 modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
2819                                         gint file_format)
2820 {
2821         ModestMsgEditWindowPrivate *priv;
2822         ModestWindowPrivate *parent_priv;
2823         gint current_format;
2824
2825         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2826
2827         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
2828         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2829
2830         current_format = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer))
2831                 ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
2832
2833         if (current_format != file_format) {
2834                 switch (file_format) {
2835                 case MODEST_FILE_FORMAT_FORMATTED_TEXT:
2836                         wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
2837                         remove_tags (WP_TEXT_BUFFER (priv->text_buffer));
2838                         break;
2839                 case MODEST_FILE_FORMAT_PLAIN_TEXT:
2840                 {
2841                         GtkWidget *dialog;
2842                         gint response;
2843                         dialog = hildon_note_new_confirmation (NULL, _("emev_nc_formatting_lost"));
2844                         response = gtk_dialog_run (GTK_DIALOG (dialog));
2845                         gtk_widget_destroy (dialog);
2846                         if (response == GTK_RESPONSE_OK) {
2847                                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
2848                         } else {
2849                                 GtkToggleAction *action = GTK_TOGGLE_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/FileFormatFormattedTextMenu"));
2850                                 modest_utils_toggle_action_set_active_block_notify (action, TRUE);
2851                         }
2852                 }
2853                         break;
2854                 }
2855                 modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
2856                 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
2857                 text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), window);
2858         }
2859 }
2860
2861 void
2862 modest_msg_edit_window_select_font (ModestMsgEditWindow *window)
2863 {
2864         GtkWidget *dialog;
2865         ModestMsgEditWindowPrivate *priv;
2866         WPTextBufferFormat oldfmt, fmt;
2867         gint old_position = 0;
2868         gint response = 0;
2869         gint position = 0;
2870         gint font_size;
2871         GdkColor *color = NULL;
2872         gboolean bold, bold_set, italic, italic_set;
2873         gboolean underline, underline_set;
2874         gboolean strikethrough, strikethrough_set;
2875         gboolean position_set;
2876         gboolean font_size_set, font_set, color_set;
2877         gchar *font_name;
2878
2879         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
2880         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
2881         
2882         dialog = hildon_font_selection_dialog_new (GTK_WINDOW (window), NULL);
2883         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(),
2884                                      GTK_WINDOW(dialog), GTK_WINDOW (window));
2885
2886         /* First we get the currently selected font information */
2887         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), &oldfmt, TRUE);
2888
2889         switch (oldfmt.text_position) {
2890         case TEXT_POSITION_NORMAL:
2891                 old_position = 0;
2892                 break;
2893         case TEXT_POSITION_SUPERSCRIPT:
2894                 old_position = 1;
2895                 break;
2896         default:
2897                 old_position = -1;
2898                 break;
2899         }
2900
2901         g_object_set (G_OBJECT (dialog),
2902                       "bold", oldfmt.bold != FALSE,
2903                       "bold-set", !oldfmt.cs.bold,
2904                       "underline", oldfmt.underline != FALSE,
2905                       "underline-set", !oldfmt.cs.underline,
2906                       "italic", oldfmt.italic != FALSE,
2907                       "italic-set", !oldfmt.cs.italic,
2908                       "strikethrough", oldfmt.strikethrough != FALSE,
2909                       "strikethrough-set", !oldfmt.cs.strikethrough,
2910                       "color", &oldfmt.color,
2911                       "color-set", !oldfmt.cs.color,
2912                       "size", wp_font_size[oldfmt.font_size],
2913                       "size-set", !oldfmt.cs.font_size,
2914                       "position", old_position,
2915                       "position-set", !oldfmt.cs.text_position,
2916                       "family", wp_get_font_name (oldfmt.font),
2917                       "family-set", !oldfmt.cs.font,
2918                       NULL);
2919
2920         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
2921                                      GTK_WINDOW (dialog), GTK_WINDOW (window));
2922         gtk_widget_show_all (dialog);
2923         priv->font_dialog = dialog;
2924         response = gtk_dialog_run (GTK_DIALOG (dialog));
2925         priv->font_dialog = NULL;
2926         if (response == GTK_RESPONSE_OK) {
2927
2928                 g_object_get( dialog,
2929                               "bold", &bold,
2930                               "bold-set", &bold_set,
2931                               "underline", &underline,
2932                               "underline-set", &underline_set,
2933                               "italic", &italic,
2934                               "italic-set", &italic_set,
2935                               "strikethrough", &strikethrough,
2936                               "strikethrough-set", &strikethrough_set,
2937                               "color", &color,
2938                               "color-set", &color_set,
2939                               "size", &font_size,
2940                               "size-set", &font_size_set,
2941                               "family", &font_name,
2942                               "family-set", &font_set,
2943                               "position", &position,
2944                               "position-set", &position_set,
2945                               NULL );
2946                 
2947         }       
2948
2949         if (response == GTK_RESPONSE_OK) {
2950                 memset(&fmt, 0, sizeof(fmt));
2951                 if (bold_set) {
2952                         fmt.bold = bold;
2953                         fmt.cs.bold = TRUE;
2954                 }
2955                 if (italic_set) {
2956                         fmt.italic = italic;
2957                         fmt.cs.italic = TRUE;
2958                 }
2959                 if (underline_set) {
2960                         fmt.underline = underline;
2961                         fmt.cs.underline = TRUE;
2962                 }
2963                 if (strikethrough_set) {
2964                         fmt.strikethrough = strikethrough;
2965                         fmt.cs.strikethrough = TRUE;
2966                 }
2967                 if (position_set) {
2968                         fmt.text_position =
2969                                 ( position == 0 )
2970                                 ? TEXT_POSITION_NORMAL
2971                                 : ( ( position == 1 )
2972                                     ? TEXT_POSITION_SUPERSCRIPT
2973                                     : TEXT_POSITION_SUBSCRIPT );
2974                         fmt.cs.text_position = TRUE;
2975                         fmt.font_size = oldfmt.font_size;
2976                 }
2977                 if (color_set) {
2978                         fmt.color = *color;
2979                         fmt.cs.color = TRUE;
2980                 }
2981                 if (font_set) {
2982                         fmt.font = wp_get_font_index(font_name,
2983                                                      DEFAULT_FONT);
2984                         fmt.cs.font = TRUE;
2985                 }
2986                 g_free(font_name);
2987                 if (font_size_set) {
2988                         fmt.cs.font_size = TRUE;
2989                         fmt.font_size = wp_get_font_size_index(font_size, DEFAULT_FONT_SIZE);
2990                 }
2991                 wp_text_buffer_set_format(WP_TEXT_BUFFER(priv->text_buffer), &fmt);
2992                 text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), window);
2993         }
2994         gtk_widget_destroy (dialog);
2995         
2996         gtk_widget_grab_focus(GTK_WIDGET(priv->msg_body));
2997 }
2998
2999 void
3000 modest_msg_edit_window_undo (ModestMsgEditWindow *window)
3001 {
3002         ModestMsgEditWindowPrivate *priv;
3003
3004         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3005         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3006         
3007         wp_text_buffer_undo (WP_TEXT_BUFFER (priv->text_buffer));
3008
3009         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
3010         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
3011 }
3012
3013 void
3014 modest_msg_edit_window_redo (ModestMsgEditWindow *window)
3015 {
3016         ModestMsgEditWindowPrivate *priv;
3017
3018         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
3019         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3020         
3021         wp_text_buffer_redo (WP_TEXT_BUFFER (priv->text_buffer));
3022
3023         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
3024         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
3025
3026 }
3027
3028 static void  
3029 text_buffer_can_undo (GtkTextBuffer *buffer, gboolean can_undo, ModestMsgEditWindow *window)
3030 {
3031         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3032
3033         priv->can_undo = can_undo;
3034 }
3035
3036 static void  
3037 text_buffer_can_redo (GtkTextBuffer *buffer, gboolean can_redo, ModestMsgEditWindow *window)
3038 {
3039         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3040
3041         priv->can_redo = can_redo;
3042 }
3043
3044 gboolean            
3045 modest_msg_edit_window_can_undo (ModestMsgEditWindow *window)
3046 {
3047         ModestMsgEditWindowPrivate *priv;
3048         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
3049         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3050
3051         return priv->can_undo;
3052 }
3053
3054 gboolean            
3055 modest_msg_edit_window_can_redo (ModestMsgEditWindow *window)
3056 {
3057         ModestMsgEditWindowPrivate *priv;
3058         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
3059         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3060
3061         return priv->can_redo;
3062 }
3063
3064
3065 static void
3066 text_buffer_delete_images_by_id (GtkTextBuffer *buffer, const gchar * image_id)
3067 {
3068         GtkTextIter iter;
3069         GtkTextIter match_start, match_end;
3070
3071         if (image_id == NULL)
3072                 return;
3073
3074         gtk_text_buffer_get_start_iter (buffer, &iter);
3075
3076         while (gtk_text_iter_forward_search (&iter, "\xef\xbf\xbc", 0, &match_start, &match_end, NULL)) {
3077                 GSList *tags = gtk_text_iter_get_tags (&match_start);
3078                 GSList *node;
3079                 for (node = tags; node != NULL; node = g_slist_next (node)) {
3080                         GtkTextTag *tag = (GtkTextTag *) node->data;
3081                         if (g_object_get_data (G_OBJECT (tag), "image-set") != NULL) {
3082                                 gchar *cur_image_id = g_object_get_data (G_OBJECT (tag), "image-index");
3083                                 if ((cur_image_id != NULL) && (strcmp (image_id, cur_image_id)==0)) {
3084                                         gint offset;
3085                                         offset = gtk_text_iter_get_offset (&match_start);
3086                                         gtk_text_buffer_delete (buffer, &match_start, &match_end);
3087                                         gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
3088                                 }
3089                         }
3090                 }
3091                 gtk_text_iter_forward_char (&iter);
3092         }
3093 }
3094
3095 gboolean
3096 message_is_empty (ModestMsgEditWindow *window)
3097 {
3098         ModestMsgEditWindowPrivate *priv = NULL;
3099
3100         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
3101         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
3102
3103         /** TODO: Add wpeditor API to tell us if there is any _visible_ text,
3104          * so we can ignore markup.
3105          */
3106         GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
3107         gint count = 0;
3108         if (buf)
3109                 count = gtk_text_buffer_get_char_count (buf);
3110
3111         return count == 0;
3112 }
3113
3114 static gboolean
3115 msg_body_focus (GtkWidget *focus,
3116                 GdkEventFocus *event,
3117                 gpointer userdata)
3118 {
3119         
3120         modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (userdata));
3121         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (userdata));
3122         modest_window_check_dimming_rules_group (MODEST_WINDOW (userdata), MODEST_DIMMING_RULES_CLIPBOARD);
3123         return FALSE;
3124 }
3125
3126 static void
3127 recpt_field_changed (GtkTextBuffer *buffer,
3128                   ModestMsgEditWindow *editor)