* src/modest-ui-actions.[ch]:
[modest] / src / maemo / 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
38 #include <config.h>
39
40 #include <modest-account-mgr.h>
41 #include <modest-account-mgr-helpers.h>
42
43 #include <widgets/modest-msg-edit-window.h>
44 #include <widgets/modest-combo-box.h>
45 #include <widgets/modest-recpt-editor.h>
46 #include <widgets/modest-attachments-view.h>
47
48 #include <modest-runtime.h>
49
50 #include "modest-platform.h"
51 #include "modest-icon-names.h"
52 #include "modest-widget-memory.h"
53 #include "modest-window-priv.h"
54 #include "modest-mail-operation.h"
55 #include "modest-tny-platform-factory.h"
56 #include "modest-tny-msg.h"
57 #include "modest-address-book.h"
58 #include <tny-simple-list.h>
59 #include <wptextview.h>
60 #include <wptextbuffer.h>
61 #include <hildon-widgets/hildon-color-selector.h>
62 #include <hildon-widgets/hildon-color-button.h>
63 #include <hildon-widgets/hildon-banner.h>
64 #include <hildon-widgets/hildon-caption.h>
65 #include <hildon-widgets/hildon-note.h>
66 #include <hildon-widgets/hildon-scroll-area.h>
67 #include <hildon-widgets/hildon-font-selection-dialog.h>
68 #include "widgets/modest-msg-edit-window-ui.h"
69
70 #ifdef MODEST_HILDON_VERSION_0
71 #include <hildon-widgets/hildon-file-chooser-dialog.h>
72 #else
73 #include <hildon/hildon-file-chooser-dialog.h>
74
75 #endif /*MODEST_HILDON_VERSION_0 */
76
77
78
79 #define DEFAULT_FONT_SIZE 3
80 #define DEFAULT_FONT 2
81 #define DEFAULT_SIZE_BUTTON_FONT_FAMILY "Sans"
82 #define DEFAULT_SIZE_COMBOBOX_WIDTH 80
83 #define DEFAULT_MAIN_VBOX_SPACING 6
84 #define SUBJECT_MAX_LENGTH 1000
85
86 static void  modest_msg_edit_window_class_init   (ModestMsgEditWindowClass *klass);
87 static void  modest_msg_edit_window_init         (ModestMsgEditWindow *obj);
88 static void  modest_msg_edit_window_finalize     (GObject *obj);
89
90 static void  text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window);
91 static void  text_buffer_mark_set (GtkTextBuffer *buffer, GtkTextIter *location, GtkTextMark *mark, gpointer userdata);
92 static void  modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
93                                                          gpointer userdata);
94 static void  modest_msg_edit_window_size_change (GtkCheckMenuItem *menu_item,
95                                                  gpointer userdata);
96 static void  modest_msg_edit_window_font_change (GtkCheckMenuItem *menu_item,
97                                                  gpointer userdata);
98 static void  modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window);
99 static gboolean modest_msg_edit_window_window_state_event (GtkWidget *widget, 
100                                                            GdkEventWindowState *event, 
101                                                            gpointer userdata);
102 static void modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
103                                                      ModestRecptEditor *editor);
104
105 /* ModestWindow methods implementation */
106 static void  modest_msg_edit_window_set_zoom (ModestWindow *window, gdouble zoom);
107 static gdouble modest_msg_edit_window_get_zoom (ModestWindow *window);
108 static gboolean modest_msg_edit_window_zoom_minus (ModestWindow *window);
109 static gboolean modest_msg_edit_window_zoom_plus (ModestWindow *window);
110 static void modest_msg_edit_window_show_toolbar   (ModestWindow *window,
111                                                    gboolean show_toolbar);
112
113
114 /* list my signals */
115 enum {
116         /* MY_SIGNAL_1, */
117         /* MY_SIGNAL_2, */
118         LAST_SIGNAL
119 };
120
121 typedef struct _ModestMsgEditWindowPrivate ModestMsgEditWindowPrivate;
122 struct _ModestMsgEditWindowPrivate {
123         GtkWidget   *msg_body;
124         GtkWidget   *header_box;
125         GtkWidget   *from_field;
126         GtkWidget   *to_field;
127         GtkWidget   *cc_field;
128         GtkWidget   *bcc_field;
129         GtkWidget   *subject_field;
130         GtkWidget   *attachments_view;
131         GtkWidget   *priority_icon;
132         GtkWidget   *add_attachment_button;
133
134         GtkWidget   *cc_caption;
135         GtkWidget   *bcc_caption;
136         GtkWidget   *attachments_caption;
137
138         GtkTextBuffer *text_buffer;
139
140         GtkWidget   *font_color_button;
141         GSList      *font_items_group;
142         GtkWidget   *font_tool_button_label;
143         GSList      *size_items_group;
144         GtkWidget   *size_tool_button_label;
145
146         GtkWidget   *scroll;
147
148         gint last_cid;
149         GList *attachments;
150
151         TnyHeaderFlags priority_flags;
152
153         gdouble zoom_level;
154 };
155
156 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
157                                                     MODEST_TYPE_MSG_EDIT_WINDOW, \
158                                                     ModestMsgEditWindowPrivate))
159 /* globals */
160 static GtkWindowClass *parent_class = NULL;
161
162 /* uncomment the following if you have defined any signals */
163 /* static guint signals[LAST_SIGNAL] = {0}; */
164
165 GType
166 modest_msg_edit_window_get_type (void)
167 {
168         static GType my_type = 0;
169         if (!my_type) {
170                 static const GTypeInfo my_info = {
171                         sizeof(ModestMsgEditWindowClass),
172                         NULL,           /* base init */
173                         NULL,           /* base finalize */
174                         (GClassInitFunc) modest_msg_edit_window_class_init,
175                         NULL,           /* class finalize */
176                         NULL,           /* class data */
177                         sizeof(ModestMsgEditWindow),
178                         1,              /* n_preallocs */
179                         (GInstanceInitFunc) modest_msg_edit_window_init,
180                         NULL
181                 };
182                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
183                                                   "ModestMsgEditWindow",
184                                                   &my_info, 0);
185
186                 wp_text_buffer_library_init ();
187         }
188         return my_type;
189 }
190
191 static void
192 modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
193 {
194         GObjectClass *gobject_class;
195         ModestWindowClass *modest_window_class;
196         gobject_class = (GObjectClass*) klass;
197         modest_window_class = (ModestWindowClass*) klass;
198
199         parent_class            = g_type_class_peek_parent (klass);
200         gobject_class->finalize = modest_msg_edit_window_finalize;
201
202         modest_window_class->set_zoom_func = modest_msg_edit_window_set_zoom;
203         modest_window_class->get_zoom_func = modest_msg_edit_window_get_zoom;
204         modest_window_class->zoom_plus_func = modest_msg_edit_window_zoom_plus;
205         modest_window_class->zoom_minus_func = modest_msg_edit_window_zoom_minus;
206         modest_window_class->show_toolbar_func = modest_msg_edit_window_show_toolbar;
207
208         g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
209 }
210
211 static void
212 modest_msg_edit_window_init (ModestMsgEditWindow *obj)
213 {
214         ModestMsgEditWindowPrivate *priv;
215         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
216
217         priv->msg_body      = NULL;
218         priv->from_field    = NULL;
219         priv->to_field      = NULL;
220         priv->cc_field      = NULL;
221         priv->bcc_field     = NULL;
222         priv->subject_field = NULL;
223         priv->attachments   = NULL;
224         priv->last_cid      = 0;
225         priv->zoom_level    = 1.0;
226
227         priv->cc_caption    = NULL;
228         priv->bcc_caption    = NULL;
229
230         priv->priority_flags = 0;
231 }
232
233
234
235 static void
236 save_settings (ModestMsgEditWindow *self)
237 {
238         modest_widget_memory_save (modest_runtime_get_conf(),
239                                    G_OBJECT(self), "modest-edit-msg-window");
240 }
241
242
243 static void
244 restore_settings (ModestMsgEditWindow *self)
245 {
246         modest_widget_memory_restore (modest_runtime_get_conf(),
247                                       G_OBJECT(self), "modest-edit-msg-window");
248 }
249
250
251 /* FIXME: this is a dup from the one in gtk/ */
252 static ModestPairList*
253 get_transports (void)
254 {
255         ModestAccountMgr *account_mgr;
256         GSList *transports = NULL;
257         GSList *cursor, *accounts;
258         
259         account_mgr = modest_runtime_get_account_mgr();
260         cursor = accounts = modest_account_mgr_account_names (account_mgr); 
261         while (cursor) {
262                 gchar *account_name = (gchar*)cursor->data;
263                 gchar *from_string  = modest_account_mgr_get_from_string (account_mgr,
264                                                                           account_name);
265                 if (!from_string)  {
266                         /* something went wrong: ignore this one */
267                         g_free (account_name);
268                         cursor->data = NULL;
269                 } else {
270                         ModestPair *pair;
271                         pair = modest_pair_new ((gpointer) account_name,
272                                                 (gpointer) from_string , TRUE);
273                         transports = g_slist_prepend (transports, pair);
274                 } /* don't free account name; it's freed when the transports list is freed */
275                 cursor = cursor->next;
276         }
277         g_slist_free (accounts);
278         return transports;
279 }
280
281
282 static void
283 text_buffer_mark_set (GtkTextBuffer *buffer, GtkTextIter *iter, GtkTextMark *mark, gpointer userdata)
284 {
285         ModestMsgEditWindow *window;
286         ModestMsgEditWindowPrivate *priv;
287         GdkRectangle location;
288         gint v_scroll_min_value = 0;
289         gint v_scroll_max_value = 0;
290         gint v_scroll_visible;
291         GtkAdjustment *vadj;
292         GtkTextMark *insert_mark;
293         GtkTextIter insert_iter;
294         
295         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (userdata));
296         g_return_if_fail (GTK_IS_TEXT_MARK (mark));
297         window = MODEST_MSG_EDIT_WINDOW (userdata);
298         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
299                 
300         insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
301         gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &insert_iter, insert_mark);
302         gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->msg_body), &insert_iter, &location);
303         
304         if (priv->header_box)
305                 v_scroll_min_value += priv->header_box->allocation.height + DEFAULT_MAIN_VBOX_SPACING;
306         v_scroll_min_value += location.y;
307         v_scroll_max_value = v_scroll_min_value + location.height;
308         
309         v_scroll_visible = GTK_WIDGET (window)->allocation.height;
310         
311         vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll));
312         
313         if (((gdouble) v_scroll_min_value) < vadj->value)
314                 gtk_adjustment_set_value (vadj, v_scroll_min_value);
315         else if (((gdouble) v_scroll_max_value) > (vadj->value + vadj->page_size))
316                 gtk_adjustment_set_value (vadj, ((gdouble)v_scroll_max_value) - vadj->page_size);
317 }
318
319 static void
320 init_window (ModestMsgEditWindow *obj)
321 {
322         GtkWidget *from_caption, *to_caption, *subject_caption;
323         GtkWidget *main_vbox;
324         ModestMsgEditWindowPrivate *priv;
325         ModestPairList *protos;
326         GtkSizeGroup *size_group;
327         GtkWidget *frame;
328         GtkWidget *scroll_area;
329         GtkWidget *subject_box;
330         GtkWidget *attachment_icon;
331
332         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
333
334         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
335
336         protos = get_transports ();
337         priv->from_field    = modest_combo_box_new (protos, g_str_equal);
338         modest_pair_list_free (protos);
339
340         priv->to_field      = modest_recpt_editor_new ();
341         priv->cc_field      = modest_recpt_editor_new ();
342         priv->bcc_field     = modest_recpt_editor_new ();
343         subject_box = gtk_hbox_new (FALSE, 0);
344         priv->priority_icon = gtk_image_new ();
345         gtk_box_pack_start (GTK_BOX (subject_box), priv->priority_icon, FALSE, FALSE, 0);
346         priv->subject_field = gtk_entry_new_with_max_length (SUBJECT_MAX_LENGTH);
347         g_object_set (G_OBJECT (priv->subject_field), "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
348         gtk_box_pack_start (GTK_BOX (subject_box), priv->subject_field, TRUE, TRUE, 0);
349         priv->add_attachment_button = gtk_button_new ();
350         gtk_button_set_relief (GTK_BUTTON (priv->add_attachment_button), GTK_RELIEF_NONE);
351         gtk_button_set_focus_on_click (GTK_BUTTON (priv->add_attachment_button), FALSE);
352         gtk_button_set_alignment (GTK_BUTTON (priv->add_attachment_button), 1.0, 1.0);
353         attachment_icon = gtk_image_new_from_icon_name (MODEST_HEADER_ICON_ATTACH, GTK_ICON_SIZE_BUTTON);
354         gtk_container_add (GTK_CONTAINER (priv->add_attachment_button), attachment_icon);
355         gtk_box_pack_start (GTK_BOX (subject_box), priv->add_attachment_button, FALSE, FALSE, 0);
356         priv->attachments_view = modest_attachments_view_new (NULL);
357         
358         priv->header_box = gtk_vbox_new (FALSE, 0);
359         
360         from_caption = hildon_caption_new (size_group, _("mail_va_from"), priv->from_field, NULL, 0);
361         to_caption = hildon_caption_new (size_group, _("mail_va_to"), priv->to_field, NULL, 0);
362         priv->cc_caption = hildon_caption_new (size_group, _("mail_va_cc"), priv->cc_field, NULL, 0);
363         priv->bcc_caption = hildon_caption_new (size_group, _("mail_va_hotfix1"), priv->bcc_field, NULL, 0);
364         subject_caption = hildon_caption_new (size_group, _("mail_va_subject"), subject_box, NULL, 0);
365         priv->attachments_caption = hildon_caption_new (size_group, _("mail_va_attachment"), priv->attachments_view, NULL, 0);
366         g_object_unref (size_group);
367
368         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
369         modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->to_field), size_group);
370         modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->cc_field), size_group);
371         modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->bcc_field), size_group);
372         gtk_size_group_add_widget (size_group, priv->subject_field);
373         gtk_size_group_add_widget (size_group, priv->attachments_view);
374         g_object_unref (size_group);
375
376         gtk_box_pack_start (GTK_BOX (priv->header_box), from_caption, FALSE, FALSE, 0);
377         gtk_box_pack_start (GTK_BOX (priv->header_box), to_caption, FALSE, FALSE, 0);
378         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->cc_caption, FALSE, FALSE, 0);
379         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->bcc_caption, FALSE, FALSE, 0);
380         gtk_box_pack_start (GTK_BOX (priv->header_box), subject_caption, FALSE, FALSE, 0);
381         gtk_box_pack_start (GTK_BOX (priv->header_box), priv->attachments_caption, FALSE, FALSE, 0);
382         gtk_widget_set_no_show_all (priv->attachments_caption, TRUE);
383
384
385         priv->msg_body = wp_text_view_new ();
386         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->msg_body), GTK_WRAP_WORD_CHAR);
387         priv->text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
388         g_object_set (priv->text_buffer, "font_scale", 1.0, NULL);
389         wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
390 /*      gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
391         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
392
393         g_signal_connect (G_OBJECT (priv->text_buffer), "refresh_attributes",
394                           G_CALLBACK (text_buffer_refresh_attributes), obj);
395         g_signal_connect (G_OBJECT (priv->text_buffer), "mark-set",
396                           G_CALLBACK (text_buffer_mark_set), obj);
397         g_signal_connect (G_OBJECT (obj), "window-state-event",
398                           G_CALLBACK (modest_msg_edit_window_window_state_event),
399                           NULL);
400         g_signal_connect_swapped (G_OBJECT (priv->to_field), "open-addressbook", 
401                                   G_CALLBACK (modest_msg_edit_window_open_addressbook), obj);
402
403         priv->scroll = gtk_scrolled_window_new (NULL, NULL);
404         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
405         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scroll), GTK_SHADOW_NONE);
406         
407         main_vbox = gtk_vbox_new  (FALSE, DEFAULT_MAIN_VBOX_SPACING);
408
409         gtk_box_pack_start (GTK_BOX(main_vbox), priv->header_box, FALSE, FALSE, 0);
410         frame = gtk_frame_new (NULL);
411         gtk_box_pack_start (GTK_BOX(main_vbox), frame, TRUE, TRUE, 0);
412
413         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (priv->scroll), main_vbox);
414         gtk_container_set_focus_vadjustment (GTK_CONTAINER (main_vbox), gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll)));
415         gtk_widget_show_all (GTK_WIDGET(priv->scroll));
416         
417         if (!modest_conf_get_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_CC, NULL))
418                 gtk_widget_hide (priv->cc_field);
419         if (!modest_conf_get_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_BCC, NULL))
420                 gtk_widget_hide (priv->bcc_field);
421
422         gtk_container_add (GTK_CONTAINER(obj), priv->scroll);
423         scroll_area = hildon_scroll_area_new (priv->scroll, priv->msg_body);
424         gtk_container_add (GTK_CONTAINER (frame), scroll_area);
425         gtk_container_set_focus_vadjustment (GTK_CONTAINER (scroll_area), 
426                                              gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll)));
427 }
428         
429
430
431 static void
432 modest_msg_edit_window_finalize (GObject *obj)
433 {
434         G_OBJECT_CLASS(parent_class)->finalize (obj);
435 }
436
437
438
439 static gboolean
440 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgEditWindow *self)
441 {
442         save_settings (self);
443         return FALSE;
444 }
445
446 static GtkWidget *
447 menubar_to_menu (GtkUIManager *ui_manager)
448 {
449         GtkWidget *main_menu;
450         GtkWidget *menubar;
451         GList *iter;
452
453         /* Create new main menu */
454         main_menu = gtk_menu_new();
455
456         /* Get the menubar from the UI manager */
457         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
458
459         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
460         while (iter) {
461                 GtkWidget *menu;
462
463                 menu = GTK_WIDGET (iter->data);
464                 gtk_widget_reparent(menu, main_menu);
465
466                 iter = g_list_next (iter);
467         }
468         return main_menu;
469 }
470
471
472 static void
473 set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
474 {
475         TnyHeader *header;
476         const gchar *to, *cc, *bcc, *subject, *body;
477         ModestMsgEditWindowPrivate *priv;
478         
479         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
480         g_return_if_fail (TNY_IS_MSG (msg));
481
482         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
483
484         header = tny_msg_get_header (msg);
485         to      = tny_header_get_to (header);
486         cc      = tny_header_get_cc (header);
487         bcc     = tny_header_get_bcc (header);
488         subject = tny_header_get_subject (header);
489
490         if (to)
491                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->to_field),  to);
492         if (cc)
493                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->cc_field),  cc);
494         if (bcc)
495                 modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->bcc_field), bcc);
496         if (subject)
497                 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);   
498
499 /*      gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
500         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
501         body = modest_tny_msg_get_body (msg, FALSE);
502         if ((body!=NULL) && (body[0] != '\0')) {
503                 wp_text_buffer_load_document_begin (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
504                 wp_text_buffer_load_document_write (WP_TEXT_BUFFER (priv->text_buffer),
505                                                     (gchar *) body,
506                                                     -1);
507                 wp_text_buffer_load_document_end (WP_TEXT_BUFFER (priv->text_buffer));
508         } else {
509                 WPTextBufferFormat fmt = {0};
510
511                 fmt.font_size = DEFAULT_FONT_SIZE;
512                 fmt.font = DEFAULT_FONT;
513                 fmt.rich_text = 1;
514                 fmt.text_position = TEXT_POSITION_NORMAL;
515                 fmt.justification = 0;
516                 fmt.cs.font_size = 1;
517                 fmt.cs.font = 1;
518                 fmt.cs.text_position = 1;
519                 fmt.cs.justification = 1;
520                 wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), &fmt);
521         }
522
523         /* Set the default focus depending on having already a To: field or not */
524         if ((!to)||(*to == '\0')) {
525                 gtk_widget_grab_focus (priv->to_field);
526         } else {
527                 gtk_widget_grab_focus (priv->msg_body);
528         }
529
530         /* TODO: lower priority, select in the From: combo to the
531            value that comes from msg <- not sure, should it be
532            allowed? */
533         
534         /* Add attachments to the view */
535         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), msg);
536         if (priv->attachments == NULL)
537                 gtk_widget_hide_all (priv->attachments_caption);
538 }
539
540 static void
541 menu_tool_button_clicked_popup (GtkMenuToolButton *item,
542                                 gpointer data)
543 {
544         GList *item_children, *node;
545         GtkWidget *bin_child;
546
547         bin_child = gtk_bin_get_child (GTK_BIN(item));
548
549         item_children = gtk_container_get_children (GTK_CONTAINER (bin_child));
550         
551         for (node = item_children; node != NULL; node = g_list_next (node)) {
552                 if (GTK_IS_TOGGLE_BUTTON (node->data)) {
553                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (node->data), TRUE);
554                 }
555         }
556         g_list_free (item_children);
557 }
558
559 static void
560 menu_tool_button_dont_expand (GtkMenuToolButton *item)
561 {
562         GtkWidget *box;
563         GList *item_children, *node;
564
565         box = gtk_bin_get_child (GTK_BIN (item));
566         gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
567         item_children = gtk_container_get_children (GTK_CONTAINER (box));
568         
569         for (node = item_children; node != NULL; node = g_list_next (node)) {
570                 gtk_box_set_child_packing (GTK_BOX (box), GTK_WIDGET (node->data), TRUE, TRUE, 0, GTK_PACK_START);
571                 if (GTK_IS_TOGGLE_BUTTON (node->data))
572                         gtk_button_set_alignment (GTK_BUTTON (node->data), 0.0, 0.5);
573                 else if (GTK_IS_BUTTON (node->data))
574                         gtk_button_set_alignment (GTK_BUTTON (node->data), 1.0, 0.5);
575         }
576         g_list_free (item_children);
577 }
578
579
580 static void
581 modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
582 {
583         ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
584         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
585         GtkWidget *placeholder;
586         GtkWidget *tool_item;
587         gint insert_index;
588         gchar size_text[5];
589         gint size_index;
590         gint font_index;
591         GtkWidget *sizes_menu;
592         GtkWidget *fonts_menu;
593         GSList *radio_group = NULL;
594         gchar *markup;
595
596         /* Toolbar */
597         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
598         hildon_window_add_toolbar (HILDON_WINDOW (window), GTK_TOOLBAR (parent_priv->toolbar));
599
600         /* should we hide the toolbar? */
601         if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL))
602                 gtk_widget_hide (parent_priv->toolbar);
603
604         /* Font color placeholder */
605         placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/FontColor");
606         insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
607
608         /* font color */
609         tool_item = GTK_WIDGET (gtk_tool_item_new ());
610         priv->font_color_button = hildon_color_button_new ();
611         gtk_container_add (GTK_CONTAINER (tool_item), priv->font_color_button);
612         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
613         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
614         gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
615         g_signal_connect_swapped (G_OBJECT (priv->font_color_button), "notify::color", G_CALLBACK (modest_msg_edit_window_color_button_change), window);
616
617         /* Font size and face placeholder */
618         placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/FontAttributes");
619         insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
620         /* font_size */
621         tool_item = GTK_WIDGET (gtk_menu_tool_button_new (NULL, NULL));
622         priv->size_tool_button_label = gtk_label_new (NULL);
623         snprintf(size_text, sizeof(size_text), "%d", wp_font_size[DEFAULT_FONT_SIZE]);
624         markup = g_strconcat ("<span font_family='", DEFAULT_SIZE_BUTTON_FONT_FAMILY, "'>",
625                               size_text,"</span>", NULL);
626         gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
627         g_free (markup);
628         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (tool_item), priv->size_tool_button_label);
629         sizes_menu = gtk_menu_new ();
630         priv->size_items_group = NULL;
631         radio_group = NULL;
632         for (size_index = 0; size_index < WP_FONT_SIZE_COUNT; size_index++) {
633                 GtkWidget *size_menu_item;
634
635                 snprintf(size_text, sizeof(size_text), "%d", wp_font_size[size_index]);
636                 size_menu_item = gtk_radio_menu_item_new_with_label (radio_group, size_text);
637                 radio_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (size_menu_item));
638                 gtk_menu_shell_append (GTK_MENU_SHELL (sizes_menu), size_menu_item);
639                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (size_menu_item), (wp_font_size[size_index] == 12));
640                 gtk_widget_show (size_menu_item);
641
642                 priv->size_items_group = g_slist_prepend (priv->size_items_group, size_menu_item);
643                         
644                 g_signal_connect (G_OBJECT (size_menu_item), "toggled", G_CALLBACK (modest_msg_edit_window_size_change),
645                                   window);
646         }
647         priv->size_items_group = g_slist_reverse (priv->size_items_group);
648         gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (tool_item), sizes_menu);
649         g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (menu_tool_button_clicked_popup), NULL);
650         gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
651         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
652         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
653         menu_tool_button_dont_expand (GTK_MENU_TOOL_BUTTON (tool_item));
654
655         /* font face */
656         tool_item = GTK_WIDGET (gtk_menu_tool_button_new (NULL, NULL));
657         priv->font_tool_button_label = gtk_label_new (NULL);
658         markup = g_strconcat ("<span font_family='", wp_get_font_name(DEFAULT_FONT), "'>Tt</span>", NULL);
659         gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
660         g_free(markup);
661         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (tool_item), priv->font_tool_button_label);
662         fonts_menu = gtk_menu_new ();
663         priv->font_items_group = NULL;
664         radio_group = NULL;
665         for (font_index = 0; font_index < wp_get_font_count (); font_index++) {
666                 GtkWidget *font_menu_item;
667                 GtkWidget *child_label;
668
669                 font_menu_item = gtk_radio_menu_item_new_with_label (radio_group, "");
670                 child_label = gtk_bin_get_child (GTK_BIN (font_menu_item));
671                 markup = g_strconcat ("<span font_family='", wp_get_font_name (font_index),"'>", 
672                                       wp_get_font_name (font_index), "</span>", NULL);
673                 gtk_label_set_markup (GTK_LABEL (child_label), markup);
674                 g_free (markup);
675                 
676                 radio_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (font_menu_item));
677                 gtk_menu_shell_append (GTK_MENU_SHELL (fonts_menu), font_menu_item);
678                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (font_menu_item), (font_index == DEFAULT_FONT));
679                 gtk_widget_show (font_menu_item);
680
681                 priv->font_items_group = g_slist_prepend (priv->font_items_group, font_menu_item);
682                         
683                 g_signal_connect (G_OBJECT (font_menu_item), "toggled", G_CALLBACK (modest_msg_edit_window_font_change),
684                                   window);
685         }
686         priv->font_items_group = g_slist_reverse (priv->font_items_group);
687         gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (tool_item), fonts_menu);
688         g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (menu_tool_button_clicked_popup), NULL);
689         gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
690         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
691         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
692         menu_tool_button_dont_expand (GTK_MENU_TOOL_BUTTON (tool_item));
693
694         /* Set expand and homogeneous for remaining items */
695         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsSend");
696         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
697         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
698         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsBold");
699         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
700         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
701         tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsItalics");
702         gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
703         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
704
705
706 }
707
708
709
710 ModestWindow*
711 modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
712 {
713         GObject *obj;
714         ModestWindowPrivate *parent_priv;
715         ModestMsgEditWindowPrivate *priv;
716         GtkActionGroup *action_group;
717         GError *error = NULL;
718         GdkPixbuf *window_icon = NULL;
719
720         g_return_val_if_fail (msg, NULL);
721         
722         obj = g_object_new(MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
723
724         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
725         parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
726
727         parent_priv->ui_manager = gtk_ui_manager_new();
728         action_group = gtk_action_group_new ("ModestMsgEditWindowActions");
729         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
730
731         /* Add common actions */
732         gtk_action_group_add_actions (action_group,
733                                       modest_msg_edit_action_entries,
734                                       G_N_ELEMENTS (modest_msg_edit_action_entries),
735                                       obj);
736         gtk_action_group_add_toggle_actions (action_group,
737                                              modest_msg_edit_toggle_action_entries,
738                                              G_N_ELEMENTS (modest_msg_edit_toggle_action_entries),
739                                              obj);
740         gtk_action_group_add_radio_actions (action_group,
741                                             modest_msg_edit_alignment_radio_action_entries,
742                                             G_N_ELEMENTS (modest_msg_edit_alignment_radio_action_entries),
743                                             GTK_JUSTIFY_LEFT,
744                                             G_CALLBACK (modest_ui_actions_on_change_justify),
745                                             obj);
746         gtk_action_group_add_radio_actions (action_group,
747                                             modest_msg_edit_zoom_action_entries,
748                                             G_N_ELEMENTS (modest_msg_edit_zoom_action_entries),
749                                             100,
750                                             G_CALLBACK (modest_ui_actions_on_change_zoom),
751                                             obj);
752         gtk_action_group_add_radio_actions (action_group,
753                                             modest_msg_edit_priority_action_entries,
754                                             G_N_ELEMENTS (modest_msg_edit_priority_action_entries),
755                                             0,
756                                             G_CALLBACK (modest_ui_actions_msg_edit_on_change_priority),
757                                             obj);
758         gtk_action_group_add_radio_actions (action_group,
759                                             modest_msg_edit_file_format_action_entries,
760                                             G_N_ELEMENTS (modest_msg_edit_file_format_action_entries),
761                                             MODEST_FILE_FORMAT_FORMATTED_TEXT,
762                                             G_CALLBACK (modest_ui_actions_msg_edit_on_change_file_format),
763                                             obj);
764         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
765         g_object_unref (action_group);
766
767         /* Load the UI definition */
768         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-msg-edit-window-ui.xml", &error);
769         if (error != NULL) {
770                 g_warning ("Could not merge modest-msg-edit-window-ui.xml: %s", error->message);
771                 g_error_free (error);
772                 error = NULL;
773         }
774
775         /* Add accelerators */
776         gtk_window_add_accel_group (GTK_WINDOW (obj), 
777                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
778
779         /* Menubar */
780         parent_priv->menubar = menubar_to_menu (parent_priv->ui_manager);
781         hildon_window_set_menu (HILDON_WINDOW (obj), GTK_MENU (parent_priv->menubar));
782
783         /* Init window */
784         init_window (MODEST_MSG_EDIT_WINDOW(obj));
785
786         restore_settings (MODEST_MSG_EDIT_WINDOW(obj));
787                 
788         gtk_window_set_title (GTK_WINDOW(obj), "Modest");
789         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
790
791         g_signal_connect (G_OBJECT(obj), "delete-event",
792                           G_CALLBACK(on_delete_event), obj);
793
794         modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
795
796         modest_msg_edit_window_setup_toolbar (MODEST_MSG_EDIT_WINDOW (obj));
797
798         set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg);
799
800         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (obj));
801
802         /* Set window icon */
803         window_icon = modest_platform_get_icon (MODEST_APP_MSG_EDIT_ICON);
804         gtk_window_set_icon (GTK_WINDOW (obj), window_icon);
805         
806         return (ModestWindow*)obj;
807 }
808
809 static gint
810 get_formatted_data_cb (const gchar *buffer, gpointer user_data)
811 {
812         GString **string_buffer = (GString **) user_data;
813
814         *string_buffer = g_string_append (*string_buffer, buffer);
815    
816         return 0;
817 }
818
819 static gchar *
820 get_formatted_data (ModestMsgEditWindow *edit_window)
821 {
822         ModestMsgEditWindowPrivate *priv;
823         GString *string_buffer = g_string_new ("");
824         
825         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
826
827         wp_text_buffer_save_document (WP_TEXT_BUFFER(priv->text_buffer), get_formatted_data_cb, &string_buffer);
828
829         return g_string_free (string_buffer, FALSE);
830                                                                         
831 }
832
833 MsgData * 
834 modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
835 {
836         MsgData *data;
837         const gchar *account_name;
838         GtkTextBuffer *buf;
839         GtkTextIter b, e;
840         ModestMsgEditWindowPrivate *priv;
841         
842         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
843
844         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
845                                                                         
846         account_name = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
847         g_return_val_if_fail (account_name, NULL);
848         
849         buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));        
850         gtk_text_buffer_get_bounds (buf, &b, &e);
851         
852         /* don't free these (except from) */
853         data = g_slice_new0 (MsgData);
854         data->from    =  modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
855                                                              account_name);
856         data->to      =  (gchar*) modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR(priv->to_field));
857         data->cc      =  (gchar*) modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR(priv->cc_field));
858         data->bcc     =  (gchar*) modest_recpt_editor_get_recipients (MODEST_RECPT_EDITOR(priv->bcc_field));
859         data->subject =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->subject_field));  
860         data->plain_body =  (gchar *) gtk_text_buffer_get_text (priv->text_buffer, &b, &e, FALSE);
861         if (wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer)))
862                 data->html_body = get_formatted_data (edit_window);
863         else
864                 data->html_body = NULL;
865         data->attachments = priv->attachments;
866         data->priority_flags = priv->priority_flags;
867
868         return data;
869 }
870
871 void 
872 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
873                                                       MsgData *data)
874 {
875         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window));
876
877         g_free (data->from);
878         g_free (data->html_body);
879         g_free (data->plain_body);
880         g_slice_free (MsgData, data);
881 }
882
883 ModestMsgEditFormat
884 modest_msg_edit_window_get_format (ModestMsgEditWindow *self)
885 {
886         gboolean rich_text;
887         ModestMsgEditWindowPrivate *priv = NULL;
888         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), MODEST_MSG_EDIT_FORMAT_HTML);
889
890         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
891
892         rich_text = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer));
893         if (rich_text)
894                 return MODEST_MSG_EDIT_FORMAT_HTML;
895         else
896                 return MODEST_MSG_EDIT_FORMAT_TEXT;
897 }
898
899 void
900 modest_msg_edit_window_set_format (ModestMsgEditWindow *self,
901                                    ModestMsgEditFormat format)
902 {
903         ModestMsgEditWindowPrivate *priv;
904
905         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
906         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
907
908         switch (format) {
909         case MODEST_MSG_EDIT_FORMAT_HTML:
910                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
911                 break;
912         case MODEST_MSG_EDIT_FORMAT_TEXT:
913                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
914                 break;
915         default:
916                 g_return_if_reached ();
917         }
918 }
919
920 ModestMsgEditFormatState *
921 modest_msg_edit_window_get_format_state (ModestMsgEditWindow *self)
922 {
923         ModestMsgEditFormatState *format_state = NULL;
924         ModestMsgEditWindowPrivate *priv;
925         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
926
927         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), NULL);
928         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
929
930         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, TRUE);
931
932         format_state = g_new0 (ModestMsgEditFormatState, 1);
933         format_state->bold = buffer_format->bold&0x1;
934         format_state->italics = buffer_format->italic&0x1;
935         format_state->bullet = buffer_format->bullet&0x1;
936         format_state->color = buffer_format->color;
937         format_state->font_size = buffer_format->font_size;
938         format_state->font_family = wp_get_font_name (buffer_format->font);
939         format_state->justification = buffer_format->justification;
940         g_free (buffer_format);
941
942         return format_state;
943  
944 }
945
946 void
947 modest_msg_edit_window_set_format_state (ModestMsgEditWindow *self,
948                                          const ModestMsgEditFormatState *format_state)
949 {
950         ModestMsgEditWindowPrivate *priv;
951         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
952         WPTextBufferFormat *current_format = g_new0 (WPTextBufferFormat, 1);
953         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
954         g_return_if_fail (format_state != NULL);
955
956         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
957         gtk_widget_grab_focus (priv->msg_body);
958         buffer_format->bold = (format_state->bold != FALSE);
959         buffer_format->italic = (format_state->italics != FALSE);
960         buffer_format->color = format_state->color;
961         buffer_format->font_size = format_state->font_size;
962         buffer_format->font = wp_get_font_index (format_state->font_family, 0);
963         buffer_format->justification = format_state->justification;
964         buffer_format->bullet = format_state->bullet;
965
966         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), current_format, TRUE);
967
968         buffer_format->cs.bold = ((buffer_format->bold&0x1) != (current_format->bold&0x1));
969         buffer_format->cs.italic = ((buffer_format->italic&0x1) != (current_format->italic&0x1));
970         buffer_format->cs.color = gdk_color_equal(&(buffer_format->color), &(current_format->color));
971         buffer_format->cs.font_size =  (buffer_format->font_size != current_format->font_size);
972         buffer_format->cs.font = (buffer_format->font != current_format->font);
973         buffer_format->cs.justification = (buffer_format->justification != current_format->justification);
974         buffer_format->cs.bullet = (buffer_format->bullet != current_format->bullet);
975
976         wp_text_buffer_freeze (WP_TEXT_BUFFER (priv->text_buffer));
977         if (buffer_format->cs.bold) {
978                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->bold&0x1));
979         }
980         if (buffer_format->cs.italic) {
981                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_ITALIC, (gpointer) (buffer_format->italic&0x1));
982         }
983         if (buffer_format->cs.color) {
984                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) (&(buffer_format->color)));
985         }
986         if (buffer_format->cs.font_size) {
987                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->font_size));
988         }
989         if (buffer_format->cs.justification) {
990                 switch (buffer_format->justification) {
991                 case GTK_JUSTIFY_LEFT:
992                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_LEFT, (gpointer) TRUE);
993                         break;
994                 case GTK_JUSTIFY_CENTER:
995                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_CENTER, (gpointer) TRUE);
996                         break;
997                 case GTK_JUSTIFY_RIGHT:
998                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_RIGHT, (gpointer) TRUE);
999                         break;
1000                 default:
1001                         break;
1002                 }
1003                         
1004         }
1005         if (buffer_format->cs.font) {
1006                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->font));
1007         }
1008         if (buffer_format->cs.bullet) {
1009                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BULLET, (gpointer) (buffer_format->bullet));
1010         }
1011 /*      wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), buffer_format); */
1012         wp_text_buffer_thaw (WP_TEXT_BUFFER (priv->text_buffer));
1013
1014         g_free (current_format);
1015
1016 }
1017
1018 static void
1019 toggle_action_set_active_block_notify (GtkToggleAction *action,
1020                                        gboolean value)
1021 {
1022         GSList *proxies = NULL;
1023
1024         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
1025              proxies != NULL; proxies = g_slist_next (proxies)) {
1026                 GtkWidget *widget = (GtkWidget *) proxies->data;
1027                 gtk_action_block_activate_from (GTK_ACTION (action), widget);
1028         }
1029
1030         gtk_toggle_action_set_active (action, value);
1031
1032         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
1033              proxies != NULL; proxies = g_slist_next (proxies)) {
1034                 GtkWidget *widget = (GtkWidget *) proxies->data;
1035                 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
1036         }
1037 }
1038
1039 static void
1040 text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window)
1041 {
1042         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
1043         GtkAction *action;
1044         ModestWindowPrivate *parent_priv;
1045         ModestMsgEditWindowPrivate *priv;
1046         GtkWidget *new_size_menuitem;
1047         GtkWidget *new_font_menuitem;
1048         
1049         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1050         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1051
1052         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
1053         
1054         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsBold");
1055         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bold);
1056
1057         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsItalics");
1058         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->italic);
1059
1060         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/BulletedListMenu");
1061         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bullet);
1062
1063         g_signal_handlers_block_by_func (G_OBJECT (priv->font_color_button), 
1064                                          G_CALLBACK (modest_msg_edit_window_color_button_change),
1065                                          window);
1066         hildon_color_button_set_color (HILDON_COLOR_BUTTON (priv->font_color_button), & (buffer_format->color));
1067         g_signal_handlers_unblock_by_func (G_OBJECT (priv->font_color_button), 
1068                                            G_CALLBACK (modest_msg_edit_window_color_button_change),
1069                                            window);
1070
1071         new_size_menuitem = GTK_WIDGET ((g_slist_nth (priv->size_items_group, 
1072                                                       buffer_format->font_size))->data);
1073         if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (new_size_menuitem))) {
1074                 GtkWidget *label;
1075                 gchar *markup;
1076
1077                 label = gtk_bin_get_child (GTK_BIN (new_size_menuitem));
1078                 markup = g_strconcat ("<span font_family='Serif'>", gtk_label_get_text (GTK_LABEL (label)), "</span>", NULL);
1079                 gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
1080                 g_free (markup);
1081                 g_signal_handlers_block_by_func (G_OBJECT (new_size_menuitem),
1082                                                  G_CALLBACK (modest_msg_edit_window_size_change),
1083                                                  window);
1084                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (new_size_menuitem), TRUE);
1085                 g_signal_handlers_unblock_by_func (G_OBJECT (new_size_menuitem),
1086                                                    G_CALLBACK (modest_msg_edit_window_size_change),
1087                                                    window);
1088         }
1089
1090         new_font_menuitem = GTK_WIDGET ((g_slist_nth (priv->font_items_group, 
1091                                                       buffer_format->font))->data);
1092         if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (new_font_menuitem))) {
1093                 GtkWidget *label;
1094                 gchar *markup;
1095
1096                 label = gtk_bin_get_child (GTK_BIN (new_font_menuitem));
1097                 markup = g_strconcat ("<span font_family='", gtk_label_get_text (GTK_LABEL (label)),"'>Tt</span>", NULL);
1098                 gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
1099                 g_free (markup);
1100                 g_signal_handlers_block_by_func (G_OBJECT (new_font_menuitem),
1101                                                  G_CALLBACK (modest_msg_edit_window_font_change),
1102                                                  window);
1103                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (new_font_menuitem), TRUE);
1104                 g_signal_handlers_unblock_by_func (G_OBJECT (new_font_menuitem),
1105                                                    G_CALLBACK (modest_msg_edit_window_font_change),
1106                                                    window);
1107         }
1108
1109         g_free (buffer_format);
1110
1111 }
1112
1113 void
1114 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
1115 {
1116         
1117         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
1118         ModestMsgEditWindowPrivate *priv;
1119         GtkWidget *dialog = NULL;
1120         gint response;
1121         const GdkColor *new_color = NULL;
1122         
1123         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1124         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
1125         
1126         dialog = hildon_color_selector_new (GTK_WINDOW (window));
1127         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), & (buffer_format->color));
1128         g_free (buffer_format);
1129
1130         response = gtk_dialog_run (GTK_DIALOG (dialog));
1131         switch (response) {
1132         case GTK_RESPONSE_OK:
1133                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
1134                 break;
1135         default:
1136                 break;
1137         }
1138         gtk_widget_destroy (dialog);
1139
1140         if (new_color != NULL)
1141                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
1142
1143 }
1144
1145 void
1146 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
1147 {
1148         
1149         ModestMsgEditWindowPrivate *priv;
1150         GtkWidget *dialog = NULL;
1151         gint response;
1152         const GdkColor *old_color = NULL;
1153         const GdkColor *new_color = NULL;
1154         
1155         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1156         old_color = wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
1157         
1158         dialog = hildon_color_selector_new (GTK_WINDOW (window));
1159         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), (GdkColor *) old_color);
1160
1161         response = gtk_dialog_run (GTK_DIALOG (dialog));
1162         switch (response) {
1163         case GTK_RESPONSE_OK:
1164                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
1165                 break;
1166         default:
1167                 break;
1168         }
1169         gtk_widget_destroy (dialog);
1170
1171         if (new_color != NULL)
1172                 wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), new_color);
1173
1174 }
1175
1176 void
1177 modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
1178 {
1179         
1180         ModestMsgEditWindowPrivate *priv;
1181         GtkWidget *dialog = NULL;
1182         gint response = 0;
1183         gchar *filename = NULL;
1184         
1185         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1186         
1187         dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_OPEN);
1188
1189         response = gtk_dialog_run (GTK_DIALOG (dialog));
1190         switch (response) {
1191         case GTK_RESPONSE_OK:
1192                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1193                 break;
1194         default:
1195                 break;
1196         }
1197         gtk_widget_destroy (dialog);
1198
1199         if (filename) {
1200                 GdkPixbuf *pixbuf = NULL;
1201                 GtkTextIter position;
1202                 GtkTextMark *insert_mark;
1203
1204                 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
1205                 if (pixbuf) {
1206                         gint image_file_id;
1207                         GdkPixbufFormat *pixbuf_format;
1208
1209                         image_file_id = g_open (filename, O_RDONLY, 0);
1210                         pixbuf_format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
1211                         if ((image_file_id != -1)&&(pixbuf_format != NULL)) {
1212                                 TnyMimePart *image_part;
1213                                 TnyStream *image_stream;
1214                                 gchar **mime_types;
1215                                 gchar *mime_type;
1216                                 gchar *basename;
1217                                 gchar *content_id;
1218
1219                                 mime_types = gdk_pixbuf_format_get_mime_types (pixbuf_format);
1220                                 if ((mime_types != NULL) && (mime_types[0] != NULL)) {
1221                                         mime_type = mime_types[0];
1222                                 } else {
1223                                         mime_type = "image/unknown";
1224                                 }
1225                                 image_part = tny_platform_factory_new_mime_part
1226                                         (modest_runtime_get_platform_factory ());
1227                                 image_stream = TNY_STREAM (tny_fs_stream_new (image_file_id));
1228
1229                                 tny_mime_part_construct_from_stream (image_part, image_stream, mime_type);
1230                                 g_strfreev (mime_types);
1231
1232                                 content_id = g_strdup_printf ("%d", priv->last_cid);
1233                                 tny_mime_part_set_content_id (image_part, content_id);
1234                                 g_free (content_id);
1235                                 priv->last_cid++;
1236
1237                                 basename = g_path_get_basename (filename);
1238                                 tny_mime_part_set_filename (image_part, basename);
1239                                 g_free (basename);
1240                                 
1241                                 insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
1242                                 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &position, insert_mark);
1243                                 wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, g_strdup (tny_mime_part_get_content_id (image_part)), pixbuf);
1244                                 priv->attachments = g_list_prepend (priv->attachments, image_part);
1245                                 modest_attachments_view_add_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
1246                                                                         image_part);
1247                                 gtk_widget_set_no_show_all (priv->attachments_caption, FALSE);
1248                                 gtk_widget_show_all (priv->attachments_caption);
1249                         } else if (image_file_id == -1) {
1250                                 close (image_file_id);
1251                         }
1252                 }
1253         }
1254
1255
1256 }
1257
1258 static void
1259 modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
1260                                             gpointer userdata)
1261 {
1262         ModestMsgEditWindowPrivate *priv;
1263         GdkColor *new_color;
1264
1265         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1266         new_color = hildon_color_button_get_color (HILDON_COLOR_BUTTON (priv->font_color_button));
1267
1268         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
1269         gtk_window_set_focus (GTK_WINDOW (window), priv->msg_body);
1270
1271 }
1272
1273 static void
1274 modest_msg_edit_window_size_change (GtkCheckMenuItem *menu_item,
1275                                     gpointer userdata)
1276 {
1277         ModestMsgEditWindowPrivate *priv;
1278         gint new_size_index;
1279         ModestMsgEditWindow *window;
1280         GtkWidget *label;
1281         
1282         window = MODEST_MSG_EDIT_WINDOW (userdata);
1283         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1284         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
1285
1286         if (gtk_check_menu_item_get_active (menu_item)) {
1287                 gchar *markup;
1288
1289                 label = gtk_bin_get_child (GTK_BIN (menu_item));
1290                 
1291                 new_size_index = atoi (gtk_label_get_text (GTK_LABEL (label)));
1292
1293                 if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT_SIZE, 
1294                                                    (gpointer) wp_get_font_size_index (new_size_index, 12)))
1295                         wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
1296                 
1297                 text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
1298                 markup = g_strconcat ("<span font_family='Serif'>", gtk_label_get_text (GTK_LABEL (label)), "</span>", NULL);
1299                 gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
1300                 g_free (markup);
1301         }
1302 }
1303
1304 static void
1305 modest_msg_edit_window_font_change (GtkCheckMenuItem *menu_item,
1306                                     gpointer userdata)
1307 {
1308         ModestMsgEditWindowPrivate *priv;
1309         gint new_font_index;
1310         ModestMsgEditWindow *window;
1311         GtkWidget *label;
1312         
1313         window = MODEST_MSG_EDIT_WINDOW (userdata);
1314         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1315         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
1316
1317         if (gtk_check_menu_item_get_active (menu_item)) {
1318                 gchar *markup;
1319
1320                 label = gtk_bin_get_child (GTK_BIN (menu_item));
1321                 
1322                 new_font_index = wp_get_font_index (gtk_label_get_text (GTK_LABEL (label)), DEFAULT_FONT);
1323
1324                 if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT, 
1325                                                    (gpointer) new_font_index))
1326                         wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
1327                 
1328                 text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
1329                     markup = g_strconcat ("<span font_family='",gtk_label_get_text (GTK_LABEL (label)),"'>Tt</span>", NULL);
1330                 gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
1331                 g_free (markup);
1332         }
1333 }
1334
1335 static void
1336 modest_msg_edit_window_set_zoom (ModestWindow *window,
1337                                  gdouble zoom)
1338 {
1339         ModestMsgEditWindowPrivate *priv;
1340      
1341         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1342
1343         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1344         priv->zoom_level = zoom;
1345         wp_text_buffer_set_font_scaling_factor (WP_TEXT_BUFFER (priv->text_buffer), zoom);
1346 }
1347
1348 static gdouble
1349 modest_msg_edit_window_get_zoom (ModestWindow *window)
1350 {
1351         ModestMsgEditWindowPrivate *priv;
1352      
1353         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), 1.0);
1354
1355         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1356         return priv->zoom_level;
1357 }
1358
1359 static gboolean
1360 modest_msg_edit_window_zoom_plus (ModestWindow *window)
1361 {
1362         ModestWindowPrivate *parent_priv;
1363         GtkRadioAction *zoom_radio_action;
1364         GSList *group, *node;
1365
1366         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1367         zoom_radio_action = GTK_RADIO_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, 
1368                                                                          "/MenuBar/ViewMenu/ZoomMenu/Zoom50Menu"));
1369
1370         group = gtk_radio_action_get_group (zoom_radio_action);
1371
1372         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (group->data))) {
1373                 hildon_banner_show_information (NULL, NULL, _("mcen_ib_max_zoom_level"));
1374                 return FALSE;
1375         }
1376
1377         for (node = group; node != NULL; node = g_slist_next (node)) {
1378                 if ((node->next != NULL) && gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (node->next->data))) {
1379                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (node->data), TRUE);
1380                         return TRUE;
1381                 }
1382         }
1383         return FALSE;
1384 }
1385
1386 static gboolean
1387 modest_msg_edit_window_zoom_minus (ModestWindow *window)
1388 {
1389         ModestWindowPrivate *parent_priv;
1390         GtkRadioAction *zoom_radio_action;
1391         GSList *group, *node;
1392
1393         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1394         zoom_radio_action = GTK_RADIO_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, 
1395                                                                          "/MenuBar/ViewMenu/ZoomMenu/Zoom50Menu"));
1396
1397         group = gtk_radio_action_get_group (zoom_radio_action);
1398
1399         for (node = group; node != NULL; node = g_slist_next (node)) {
1400                 if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (node->data))) {
1401                         if (node->next != NULL) {
1402                                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (node->next->data), TRUE);
1403                                 return TRUE;
1404                         } else
1405                                 hildon_banner_show_information (NULL, NULL, _("mcen_ib_min_zoom_level"));
1406                         break;
1407                 }
1408         }
1409         return FALSE;
1410 }
1411
1412 static gboolean
1413 modest_msg_edit_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer userdata)
1414 {
1415         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
1416                 ModestWindowPrivate *parent_priv;
1417                 ModestWindowMgr *mgr;
1418                 gboolean is_fullscreen;
1419                 GtkAction *fs_toggle_action;
1420                 gboolean active;
1421
1422                 mgr = modest_runtime_get_window_mgr ();
1423                 is_fullscreen = (modest_window_mgr_get_fullscreen_mode (mgr))?1:0;
1424
1425                 parent_priv = MODEST_WINDOW_GET_PRIVATE (widget);
1426                 
1427                 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
1428                 active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)))?1:0;
1429                 if (is_fullscreen != active)
1430                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), is_fullscreen);
1431         }
1432
1433         return FALSE;
1434
1435 }
1436
1437 void
1438 modest_msg_edit_window_toggle_fullscreen (ModestMsgEditWindow *window)
1439 {
1440         ModestWindowPrivate *parent_priv;
1441         GtkAction *fs_toggle_action;
1442         gboolean active;
1443
1444         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
1445
1446         fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
1447         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action));
1448         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), !active);
1449 }
1450
1451 void
1452 modest_msg_edit_window_show_cc (ModestMsgEditWindow *window, 
1453                                 gboolean show)
1454 {
1455         ModestMsgEditWindowPrivate *priv = NULL;
1456         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1457
1458         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1459         if (show)
1460                 gtk_widget_show (priv->cc_caption);
1461         else
1462                 gtk_widget_hide (priv->cc_caption);
1463 }
1464
1465 void
1466 modest_msg_edit_window_show_bcc (ModestMsgEditWindow *window, 
1467                                  gboolean show)
1468 {
1469         ModestMsgEditWindowPrivate *priv = NULL;
1470         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1471
1472         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1473         if (show)
1474                 gtk_widget_show (priv->bcc_caption);
1475         else
1476                 gtk_widget_hide (priv->bcc_caption);
1477 }
1478
1479 static void
1480 modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
1481                                          ModestRecptEditor *editor)
1482 {
1483         ModestMsgEditWindowPrivate *priv;
1484
1485         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1486         g_return_if_fail ((editor == NULL) || (MODEST_IS_RECPT_EDITOR (editor)));
1487         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1488
1489         if (editor == NULL) {
1490                 GtkWidget *view_focus;
1491                 view_focus = gtk_window_get_focus (GTK_WINDOW (window));
1492
1493                 if (gtk_widget_get_parent (view_focus) && 
1494                     MODEST_IS_RECPT_EDITOR (gtk_widget_get_parent (view_focus))) {
1495                         editor = MODEST_RECPT_EDITOR (gtk_widget_get_parent (view_focus));
1496                 } else {
1497                         editor = MODEST_RECPT_EDITOR (priv->to_field);
1498                 }
1499         }
1500
1501         modest_address_book_select_addresses (editor);
1502
1503 }
1504 static void
1505 modest_msg_edit_window_show_toolbar (ModestWindow *self,
1506                                      gboolean show_toolbar)
1507 {
1508         ModestWindowPrivate *parent_priv;
1509         
1510         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
1511
1512         /* FIXME: we can not just use the code of
1513            modest_msg_edit_window_setup_toolbar because it has a
1514            mixture of both initialization and creation code. */
1515
1516         if (show_toolbar)
1517                 gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
1518         else
1519                 gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
1520 }
1521
1522 void
1523 modest_msg_edit_window_set_priority_flags (ModestMsgEditWindow *window,
1524                                            TnyHeaderFlags priority_flags)
1525 {
1526         ModestMsgEditWindowPrivate *priv;
1527
1528         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1529
1530         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1531         priority_flags = priority_flags & (TNY_HEADER_FLAG_HIGH_PRIORITY);
1532
1533         if (priv->priority_flags != priority_flags) {
1534
1535                 priv->priority_flags = priority_flags;
1536
1537                 switch (priority_flags) {
1538                 case TNY_HEADER_FLAG_HIGH_PRIORITY:
1539                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon), "qgn_list_messaging_high", GTK_ICON_SIZE_MENU);
1540                         gtk_widget_show (priv->priority_icon);
1541                         break;
1542                 case TNY_HEADER_FLAG_LOW_PRIORITY:
1543                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon), "qgn_list_messaging_low", GTK_ICON_SIZE_MENU);
1544                         gtk_widget_show (priv->priority_icon);
1545                         break;
1546                 default:
1547                         gtk_widget_hide (priv->priority_icon);
1548                         break;
1549                 }
1550         }
1551 }
1552
1553 void
1554 modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
1555                                         gint file_format)
1556 {
1557         ModestMsgEditWindowPrivate *priv;
1558         gint current_format;
1559
1560         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1561
1562         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1563
1564         current_format = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer))
1565                 ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
1566
1567         if (current_format != file_format) {
1568                 switch (file_format) {
1569                 case MODEST_FILE_FORMAT_FORMATTED_TEXT:
1570                         wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
1571                         break;
1572                 case MODEST_FILE_FORMAT_PLAIN_TEXT:
1573                 {
1574                         GtkWidget *dialog;
1575                         gint response;
1576                         dialog = hildon_note_new_confirmation (NULL, _("emev_nc_formatting_lost"));
1577                         response = gtk_dialog_run (GTK_DIALOG (dialog));
1578                         gtk_widget_destroy (dialog);
1579                         if (response == GTK_RESPONSE_OK)
1580                                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
1581                 }
1582                         break;
1583                 }
1584         }
1585 }
1586
1587 void
1588 modest_msg_edit_window_select_font (ModestMsgEditWindow *window)
1589 {
1590         GtkWidget *dialog;
1591         ModestMsgEditWindowPrivate *priv;
1592         WPTextBufferFormat oldfmt, fmt;
1593         gint old_position = 0;
1594         gint response = 0;
1595         gint position = 0;
1596         gint font_size;
1597         GdkColor *color = NULL;
1598         gboolean bold, bold_set, italic, italic_set;
1599         gboolean underline, underline_set;
1600         gboolean strikethrough, strikethrough_set;
1601         gboolean position_set;
1602         gboolean font_size_set, font_set, color_set;
1603         gchar *font_name;
1604
1605         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1606         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1607         
1608         dialog = hildon_font_selection_dialog_new (NULL, NULL);
1609
1610         /* First we get the currently selected font information */
1611         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), &oldfmt, TRUE);
1612         g_object_set (G_OBJECT (dialog), "font-scaling", priv->zoom_level, NULL);
1613
1614         switch (oldfmt.text_position) {
1615         case TEXT_POSITION_NORMAL:
1616                 old_position = 0;
1617                 break;
1618         case TEXT_POSITION_SUPERSCRIPT:
1619                 old_position = 1;
1620                 break;
1621         default:
1622                 old_position = -1;
1623                 break;
1624         }
1625
1626         g_object_set (G_OBJECT (dialog),
1627                       "bold", oldfmt.bold != FALSE,
1628                       "bold-set", !oldfmt.cs.bold,
1629                       "underline", oldfmt.underline != FALSE,
1630                       "underline-set", !oldfmt.cs.underline,
1631                       "italic", oldfmt.italic != FALSE,
1632                       "italic-set", !oldfmt.cs.italic,
1633                       "strikethrough", oldfmt.strikethrough != FALSE,
1634                       "strikethrough-set", !oldfmt.cs.strikethrough,
1635                       "color", &oldfmt.color,
1636                       "color-set", !oldfmt.cs.color,
1637                       "size", wp_font_size[oldfmt.font_size],
1638                       "size-set", !oldfmt.cs.font_size,
1639                       "position", old_position,
1640                       "position-set", !oldfmt.cs.text_position,
1641                       "family", wp_get_font_name (oldfmt.font),
1642                       "family-set", !oldfmt.cs.font,
1643                       NULL);
1644
1645         gtk_widget_show_all (dialog);
1646         response = gtk_dialog_run (GTK_DIALOG (dialog));
1647         if (response == GTK_RESPONSE_OK) {
1648
1649                 g_object_get( dialog,
1650                               "bold", &bold,
1651                               "bold-set", &bold_set,
1652                               "underline", &underline,
1653                               "underline-set", &underline_set,
1654                               "italic", &italic,
1655                               "italic-set", &italic_set,
1656                               "strikethrough", &strikethrough,
1657                               "strikethrough-set", &strikethrough_set,
1658                               "color", &color,
1659                               "color-set", &color_set,
1660                               "size", &font_size,
1661                               "size-set", &font_size_set,
1662                               "family", &font_name,
1663                               "family-set", &font_set,
1664                               "position", &position,
1665                               "position-set", &position_set,
1666                               NULL );
1667                 
1668         }       
1669
1670         gtk_widget_destroy (dialog);
1671         
1672         if (response == GTK_RESPONSE_OK) {
1673                 memset(&fmt, 0, sizeof(fmt));
1674                 if (bold_set) {
1675                         fmt.bold = bold;
1676                         fmt.cs.bold = TRUE;
1677                 }
1678                 if (italic_set) {
1679                         fmt.italic = italic;
1680                         fmt.cs.italic = TRUE;
1681                 }
1682                 if (underline_set) {
1683                         fmt.underline = underline;
1684                         fmt.cs.underline = TRUE;
1685                 }
1686                 if (strikethrough_set) {
1687                         fmt.strikethrough = strikethrough;
1688                         fmt.cs.strikethrough = TRUE;
1689                 }
1690                 if (position_set) {
1691                         fmt.text_position =
1692                                 ( position == 0 )
1693                                 ? TEXT_POSITION_NORMAL
1694                                 : ( ( position == 1 )
1695                                     ? TEXT_POSITION_SUPERSCRIPT
1696                                     : TEXT_POSITION_SUBSCRIPT );
1697                         fmt.cs.text_position = TRUE;
1698                 }
1699                 if (color_set) {
1700                         fmt.color = *color;
1701                         fmt.cs.color = TRUE;
1702                 }
1703                 gdk_color_free(color);
1704                 if (font_set) {
1705                         fmt.font = wp_get_font_index(font_name,
1706                                                      DEFAULT_FONT);
1707                         fmt.cs.font = TRUE;
1708                 }
1709                 g_free(font_name);
1710                 if (font_size_set) {
1711                         fmt.font_size = wp_get_font_size_index(
1712                                 font_size, DEFAULT_FONT_SIZE);
1713                         fmt.cs.font_size = TRUE;
1714                 }
1715                 gtk_widget_grab_focus(GTK_WIDGET(priv->msg_body));
1716                 wp_text_buffer_set_format(WP_TEXT_BUFFER(priv->text_buffer), &fmt);
1717         }
1718
1719 }
1720
1721 void
1722 modest_msg_edit_window_undo               (ModestMsgEditWindow *window)
1723 {
1724         ModestMsgEditWindowPrivate *priv;
1725
1726         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
1727         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
1728         
1729         wp_text_buffer_undo (WP_TEXT_BUFFER (priv->text_buffer));
1730
1731 }