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