7910a87e9b89a1e587a8416b4e34f1dc52e58efc
[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 <tny-account-store.h>
35 #include <tny-fs-stream.h>
36
37 #include <config.h>
38
39 #include <modest-account-mgr.h>
40 #include <modest-account-mgr-helpers.h>
41
42 #include <widgets/modest-msg-edit-window.h>
43 #include <widgets/modest-combo-box.h>
44
45 #include <modest-runtime.h>
46
47 #include <widgets/modest-msg-edit-window-ui.h>
48 #include "modest-icon-names.h"
49 #include "modest-widget-memory.h"
50 #include "modest-window-priv.h"
51 #include "modest-mail-operation.h"
52 #include "modest-tny-platform-factory.h"
53 #include "modest-tny-msg.h"
54 #include <tny-simple-list.h>
55 #include <wptextview.h>
56 #include <wptextbuffer.h>
57 #include <hildon-widgets/hildon-color-selector.h>
58 #include <hildon-widgets/hildon-color-button.h>
59
60 #ifdef MODEST_HILDON_VERSION_0
61 #include <hildon-widgets/hildon-file-chooser-dialog.h>
62 #else
63 #include <hildon/hildon-file-chooser-dialog.h>
64 #endif /*MODEST_HILDON_VERSION_0 */
65
66
67 #define DEFAULT_FONT_SIZE 3
68 #define DEFAULT_FONT 2
69 #define DEFAULT_SIZE_COMBOBOX_WIDTH 80
70
71 static void  modest_msg_edit_window_class_init   (ModestMsgEditWindowClass *klass);
72 static void  modest_msg_edit_window_init         (ModestMsgEditWindow *obj);
73 static void  modest_msg_edit_window_finalize     (GObject *obj);
74
75 static void  text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window);
76 static void  modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
77                                                          gpointer userdata);
78 static void  modest_msg_edit_window_size_combobox_change (ModestMsgEditWindow *window,
79                                                           gpointer userdata);
80 static void  modest_msg_edit_window_font_combobox_change (ModestMsgEditWindow *window,
81                                                           gpointer userdata);
82 static void  modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window);
83
84 /* list my signals */
85 enum {
86         /* MY_SIGNAL_1, */
87         /* MY_SIGNAL_2, */
88         LAST_SIGNAL
89 };
90
91 typedef struct _ModestMsgEditWindowPrivate ModestMsgEditWindowPrivate;
92 struct _ModestMsgEditWindowPrivate {
93         GtkWidget   *msg_body;
94         GtkWidget   *from_field;
95         GtkWidget   *to_field;
96         GtkWidget   *cc_field;
97         GtkWidget   *bcc_field;
98         GtkWidget   *subject_field;
99
100         GtkTextBuffer *text_buffer;
101
102         GtkWidget   *font_color_button;
103         GtkWidget   *size_combobox;
104         GtkWidget   *font_combobox;
105
106         gint last_cid;
107         GList *attachments;
108 };
109
110 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
111                                                     MODEST_TYPE_MSG_EDIT_WINDOW, \
112                                                     ModestMsgEditWindowPrivate))
113 /* globals */
114 static GtkWindowClass *parent_class = NULL;
115
116 /* uncomment the following if you have defined any signals */
117 /* static guint signals[LAST_SIGNAL] = {0}; */
118
119 GType
120 modest_msg_edit_window_get_type (void)
121 {
122         static GType my_type = 0;
123         if (!my_type) {
124                 static const GTypeInfo my_info = {
125                         sizeof(ModestMsgEditWindowClass),
126                         NULL,           /* base init */
127                         NULL,           /* base finalize */
128                         (GClassInitFunc) modest_msg_edit_window_class_init,
129                         NULL,           /* class finalize */
130                         NULL,           /* class data */
131                         sizeof(ModestMsgEditWindow),
132                         1,              /* n_preallocs */
133                         (GInstanceInitFunc) modest_msg_edit_window_init,
134                         NULL
135                 };
136                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
137                                                   "ModestMsgEditWindow",
138                                                   &my_info, 0);
139
140                 wp_text_buffer_library_init ();
141         }
142         return my_type;
143 }
144
145 static void
146 modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
147 {
148         GObjectClass *gobject_class;
149         gobject_class = (GObjectClass*) klass;
150
151         parent_class            = g_type_class_peek_parent (klass);
152         gobject_class->finalize = modest_msg_edit_window_finalize;
153
154         g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
155 }
156
157 static void
158 modest_msg_edit_window_init (ModestMsgEditWindow *obj)
159 {
160         ModestMsgEditWindowPrivate *priv;
161         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
162
163         priv->msg_body      = NULL;
164         priv->from_field    = NULL;
165         priv->to_field      = NULL;
166         priv->cc_field      = NULL;
167         priv->bcc_field     = NULL;
168         priv->subject_field = NULL;
169         priv->attachments = NULL;
170         priv->last_cid = 0;
171 }
172
173
174
175 static void
176 save_settings (ModestMsgEditWindow *self)
177 {
178         modest_widget_memory_save (modest_runtime_get_conf(),
179                                    G_OBJECT(self), "modest-edit-msg-window");
180 }
181
182
183 static void
184 restore_settings (ModestMsgEditWindow *self)
185 {
186         modest_widget_memory_restore (modest_runtime_get_conf(),
187                                       G_OBJECT(self), "modest-edit-msg-window");
188 }
189
190
191 /* FIXME: this is a dup from the one in gtk/ */
192 static ModestPairList*
193 get_transports (void)
194 {
195         ModestAccountMgr *account_mgr;
196         GSList *transports = NULL;
197         GSList *cursor, *accounts;
198         
199         account_mgr = modest_runtime_get_account_mgr();
200         cursor = accounts = modest_account_mgr_account_names (account_mgr); 
201         while (cursor) {
202                 gchar *account_name = (gchar*)cursor->data;
203                 gchar *from_string  = modest_account_mgr_get_from_string (account_mgr,
204                                                                           account_name);
205                 if (!from_string)  {
206                         /* something went wrong: ignore this one */
207                         g_free (account_name);
208                         cursor->data = NULL;
209                 } else {
210                         ModestPair *pair;
211                         pair = modest_pair_new ((gpointer) account_name,
212                                                 (gpointer) from_string , TRUE);
213                         transports = g_slist_prepend (transports, pair);
214                 } /* don't free account name; it's freed when the transports list is freed */
215                 cursor = cursor->next;
216         }
217         g_slist_free (accounts);
218         return transports;
219 }
220
221
222
223 static void
224 init_window (ModestMsgEditWindow *obj)
225 {
226         GtkWidget *to_button, *cc_button, *bcc_button; 
227         GtkWidget *header_table;
228         GtkWidget *main_vbox;
229         GtkWidget *body_scroll;
230         ModestMsgEditWindowPrivate *priv;
231         ModestPairList *protos;
232
233         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
234
235         to_button     = gtk_button_new_with_label (_("To..."));
236         cc_button     = gtk_button_new_with_label (_("Cc..."));
237         bcc_button    = gtk_button_new_with_label (_("Bcc..."));
238                 
239         protos = get_transports ();
240         priv->from_field    = modest_combo_box_new (protos, g_str_equal);
241         modest_pair_list_free (protos);
242
243         priv->to_field      = gtk_entry_new_with_max_length (80);
244         priv->cc_field      = gtk_entry_new_with_max_length (80);
245         priv->bcc_field     = gtk_entry_new_with_max_length (80);
246         priv->subject_field = gtk_entry_new_with_max_length (80);
247         
248         header_table = gtk_table_new (5,2, FALSE);
249         
250         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("From:")),
251                           0,1,0,1, GTK_SHRINK, 0, 0, 0);
252         gtk_table_attach (GTK_TABLE(header_table), to_button,     0,1,1,2, GTK_SHRINK, 0, 0, 0);
253         gtk_table_attach (GTK_TABLE(header_table), cc_button,     0,1,2,3, GTK_SHRINK, 0, 0, 0);
254         gtk_table_attach (GTK_TABLE(header_table), bcc_button,    0,1,3,4, GTK_SHRINK, 0, 0, 0);
255         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("Subject:")),
256                           0,1,4,5, GTK_SHRINK, 0, 0, 0);
257
258         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->from_field,   1,2,0,1);
259         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->to_field,     1,2,1,2);
260         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->cc_field,     1,2,2,3);
261         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->bcc_field,    1,2,3,4);
262         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->subject_field,1,2,4,5);
263
264         priv->msg_body = wp_text_view_new ();
265         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->msg_body), GTK_WRAP_WORD_CHAR);
266         priv->text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
267         g_object_set (priv->text_buffer, "font_scale", 1.0, NULL);
268         wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
269 /*      gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
270         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
271         g_signal_connect (G_OBJECT (priv->text_buffer), "refresh_attributes",
272                           G_CALLBACK (text_buffer_refresh_attributes), obj);
273
274         body_scroll = gtk_scrolled_window_new (NULL, NULL);
275         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (body_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
276         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (body_scroll), GTK_SHADOW_IN);
277         gtk_container_add (GTK_CONTAINER (body_scroll), priv->msg_body);
278         
279         main_vbox = gtk_vbox_new  (FALSE, 6);
280
281         gtk_box_pack_start (GTK_BOX(main_vbox), header_table, FALSE, FALSE, 6);
282         gtk_box_pack_start (GTK_BOX(main_vbox), body_scroll, TRUE, TRUE, 6);
283
284         gtk_widget_show_all (GTK_WIDGET(main_vbox));
285         
286         if (!modest_conf_get_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_CC, NULL))
287                 gtk_widget_hide (priv->cc_field);
288         if (!modest_conf_get_bool(modest_runtime_get_conf(), MODEST_CONF_SHOW_BCC, NULL))
289                 gtk_widget_hide (priv->bcc_field);
290
291         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
292 }
293         
294
295
296 static void
297 modest_msg_edit_window_finalize (GObject *obj)
298 {
299         G_OBJECT_CLASS(parent_class)->finalize (obj);
300 }
301
302
303
304 static gboolean
305 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgEditWindow *self)
306 {
307         save_settings (self);
308         return FALSE;
309 }
310
311 static GtkWidget *
312 menubar_to_menu (GtkUIManager *ui_manager)
313 {
314         GtkWidget *main_menu;
315         GtkWidget *menubar;
316         GList *iter;
317
318         /* Create new main menu */
319         main_menu = gtk_menu_new();
320
321         /* Get the menubar from the UI manager */
322         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
323
324         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
325         while (iter) {
326                 GtkWidget *menu;
327
328                 menu = GTK_WIDGET (iter->data);
329                 gtk_widget_reparent(menu, main_menu);
330
331                 iter = g_list_next (iter);
332         }
333         return main_menu;
334 }
335
336
337 static void
338 set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
339 {
340         TnyHeader *header;
341         const gchar *to, *cc, *bcc, *subject, *body;
342         ModestMsgEditWindowPrivate *priv;
343         
344         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
345         g_return_if_fail (TNY_IS_MSG (msg));
346
347         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
348
349         header = tny_msg_get_header (msg);
350         to      = tny_header_get_to (header);
351         cc      = tny_header_get_cc (header);
352         bcc     = tny_header_get_bcc (header);
353         subject = tny_header_get_subject (header);
354
355         if (to)
356                 gtk_entry_set_text (GTK_ENTRY(priv->to_field),  to);
357         if (cc)
358                 gtk_entry_set_text (GTK_ENTRY(priv->cc_field),  cc);
359         if (bcc)
360                 gtk_entry_set_text (GTK_ENTRY(priv->bcc_field), bcc);
361         if (subject)
362                 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);   
363         
364 /*      gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
365         wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
366         body = modest_tny_msg_get_body (msg, FALSE);
367         if ((body!=NULL) && (body[0] != '\0')) {
368                 wp_text_buffer_load_document_begin (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
369                 wp_text_buffer_load_document_write (WP_TEXT_BUFFER (priv->text_buffer),
370                                                     (gchar *) body,
371                                                     -1);
372                 wp_text_buffer_load_document_end (WP_TEXT_BUFFER (priv->text_buffer));
373         } else {
374                 WPTextBufferFormat fmt = {0};
375
376                 fmt.font_size = DEFAULT_FONT_SIZE;
377                 fmt.font = DEFAULT_FONT;
378                 fmt.rich_text = 1;
379                 fmt.text_position = TEXT_POSITION_NORMAL;
380                 fmt.justification = 0;
381                 fmt.cs.font_size = 1;
382                 fmt.cs.font = 1;
383                 fmt.cs.text_position = 1;
384                 fmt.cs.justification = 1;
385                 wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), &fmt);
386         }
387
388         /* TODO: lower priority, select in the From: combo to the
389            value that comes from msg <- not sure, should it be
390            allowed? */
391         
392         /* TODO: set attachments */
393 }
394
395 static void
396 modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
397 {
398         ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
399         ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
400         GtkWidget *font_placeholder;
401         GtkWidget *tool_item;
402         gint insert_index;
403         gint size_index;
404         gint font_index;
405
406         /* Toolbar */
407         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
408         hildon_window_add_toolbar (HILDON_WINDOW (window), GTK_TOOLBAR (parent_priv->toolbar));
409
410         /* should we hide the toolbar? */
411         if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL))
412                 gtk_widget_hide (parent_priv->toolbar);
413
414         /* Font management toolbar elements */
415         font_placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/FontAttributes");
416         insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(font_placeholder));
417
418         /* font color */
419         tool_item = GTK_WIDGET (gtk_tool_item_new ());
420         priv->font_color_button = hildon_color_button_new ();
421         gtk_container_add (GTK_CONTAINER (tool_item), priv->font_color_button);
422         gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
423         g_signal_connect_swapped (G_OBJECT (priv->font_color_button), "notify::color", G_CALLBACK (modest_msg_edit_window_color_button_change), window);
424
425         /* font_size */
426         priv->size_combobox = gtk_combo_box_new_text ();
427         gtk_widget_set_size_request (priv->size_combobox, DEFAULT_SIZE_COMBOBOX_WIDTH, -1);
428         for (size_index = 0; size_index < WP_FONT_SIZE_COUNT; size_index++) {
429                 gchar size_text[5];
430                 snprintf(size_text, sizeof(size_text), "%d", wp_font_size[size_index]);
431                 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->size_combobox), size_text);
432         }
433         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->size_combobox), wp_get_font_size_index(12, 4));
434         tool_item = GTK_WIDGET (gtk_tool_item_new ());
435         gtk_container_add (GTK_CONTAINER (tool_item), priv->size_combobox);
436         gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
437         g_signal_connect_swapped (G_OBJECT (priv->size_combobox), "changed", G_CALLBACK (modest_msg_edit_window_size_combobox_change), window);
438
439         priv->font_combobox = gtk_combo_box_new_text ();
440         for (font_index = 0; font_index < wp_get_font_count (); font_index++) {
441                 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->font_combobox), wp_get_font_name (font_index));
442         }
443         tool_item = GTK_WIDGET (gtk_tool_item_new ());
444         gtk_container_add (GTK_CONTAINER (tool_item), priv->font_combobox);
445         gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
446         g_signal_connect_swapped (G_OBJECT (priv->font_combobox), "changed", G_CALLBACK (modest_msg_edit_window_font_combobox_change), window);
447 }
448
449
450
451 ModestWindow*
452 modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
453 {
454         GObject *obj;
455         ModestWindowPrivate *parent_priv;
456         ModestMsgEditWindowPrivate *priv;
457         GtkActionGroup *action_group;
458         GError *error = NULL;
459
460         g_return_val_if_fail (msg, NULL);
461         
462         obj = g_object_new(MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
463
464         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
465         parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
466
467         parent_priv->ui_manager = gtk_ui_manager_new();
468         action_group = gtk_action_group_new ("ModestMsgEditWindowActions");
469
470         /* Add common actions */
471         gtk_action_group_add_actions (action_group,
472                                       modest_msg_edit_action_entries,
473                                       G_N_ELEMENTS (modest_msg_edit_action_entries),
474                                       obj);
475         gtk_action_group_add_toggle_actions (action_group,
476                                              modest_msg_edit_toggle_action_entries,
477                                              G_N_ELEMENTS (modest_msg_edit_toggle_action_entries),
478                                              obj);
479         gtk_action_group_add_radio_actions (action_group,
480                                             modest_msg_edit_alignment_radio_action_entries,
481                                             G_N_ELEMENTS (modest_msg_edit_alignment_radio_action_entries),
482                                             GTK_JUSTIFY_LEFT,
483                                             G_CALLBACK (modest_ui_actions_on_change_justify),
484                                             obj);
485         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
486         g_object_unref (action_group);
487
488         /* Load the UI definition */
489         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-msg-edit-window-ui.xml", &error);
490         if (error != NULL) {
491                 g_warning ("Could not merge modest-msg-edit-window-ui.xml: %s", error->message);
492                 g_error_free (error);
493                 error = NULL;
494         }
495
496         /* Add accelerators */
497         gtk_window_add_accel_group (GTK_WINDOW (obj), 
498                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
499
500
501         
502         /* Menubar */
503         parent_priv->menubar = menubar_to_menu (parent_priv->ui_manager);
504         hildon_window_set_menu (HILDON_WINDOW (obj), GTK_MENU (parent_priv->menubar));
505
506         /* Init window */
507         init_window (MODEST_MSG_EDIT_WINDOW(obj));
508
509         restore_settings (MODEST_MSG_EDIT_WINDOW(obj));
510                 
511         gtk_window_set_title (GTK_WINDOW(obj), "Modest");
512         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
513
514         g_signal_connect (G_OBJECT(obj), "delete-event",
515                           G_CALLBACK(on_delete_event), obj);
516
517         modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
518
519         modest_msg_edit_window_setup_toolbar (MODEST_MSG_EDIT_WINDOW (obj));
520
521         set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg);
522
523         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (obj));
524         
525         return (ModestWindow*)obj;
526 }
527
528 static gint
529 get_formatted_data_cb (const gchar *buffer, gpointer user_data)
530 {
531         GString **string_buffer = (GString **) user_data;
532
533         *string_buffer = g_string_append (*string_buffer, buffer);
534    
535         return 0;
536 }
537
538 static gchar *
539 get_formatted_data (ModestMsgEditWindow *edit_window)
540 {
541         ModestMsgEditWindowPrivate *priv;
542         GString *string_buffer = g_string_new ("");
543         
544         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
545
546         wp_text_buffer_save_document (WP_TEXT_BUFFER(priv->text_buffer), get_formatted_data_cb, &string_buffer);
547
548         return g_string_free (string_buffer, FALSE);
549                                                                         
550 }
551
552 MsgData * 
553 modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
554 {
555         MsgData *data;
556         const gchar *account_name;
557         GtkTextBuffer *buf;
558         GtkTextIter b, e;
559         ModestMsgEditWindowPrivate *priv;
560         
561         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
562
563         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
564                                                                         
565         account_name = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
566         g_return_val_if_fail (account_name, NULL);
567         
568         buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));        
569         gtk_text_buffer_get_bounds (buf, &b, &e);
570         
571         /* don't free these (except from) */
572         data = g_slice_new0 (MsgData);
573         data->from    =  modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
574                                                              account_name);
575         data->to      =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->to_field));
576         data->cc      =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->cc_field));
577         data->bcc     =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->bcc_field));
578         data->subject =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->subject_field));  
579         data->plain_body =  (gchar *) gtk_text_buffer_get_text (priv->text_buffer, &b, &e, FALSE);
580         data->html_body  =  get_formatted_data (edit_window);
581         data->attachments = priv->attachments;
582
583         return data;
584 }
585
586 void 
587 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
588                                                       MsgData *data)
589 {
590         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window));
591
592         g_free (data->from);
593         g_free (data->html_body);
594         g_free (data->plain_body);
595         g_slice_free (MsgData, data);
596 }
597
598 ModestMsgEditFormat
599 modest_msg_edit_window_get_format (ModestMsgEditWindow *self)
600 {
601         gboolean rich_text;
602         ModestMsgEditWindowPrivate *priv = NULL;
603         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), MODEST_MSG_EDIT_FORMAT_HTML);
604
605         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
606
607         rich_text = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer));
608         if (rich_text)
609                 return MODEST_MSG_EDIT_FORMAT_HTML;
610         else
611                 return MODEST_MSG_EDIT_FORMAT_TEXT;
612 }
613
614 void
615 modest_msg_edit_window_set_format (ModestMsgEditWindow *self,
616                                    ModestMsgEditFormat format)
617 {
618         ModestMsgEditWindowPrivate *priv;
619
620         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
621         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
622
623         switch (format) {
624         case MODEST_MSG_EDIT_FORMAT_HTML:
625                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
626                 break;
627         case MODEST_MSG_EDIT_FORMAT_TEXT:
628                 wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
629                 break;
630         default:
631                 g_return_if_reached ();
632         }
633 }
634
635 ModestMsgEditFormatState *
636 modest_msg_edit_window_get_format_state (ModestMsgEditWindow *self)
637 {
638         ModestMsgEditFormatState *format_state = NULL;
639         ModestMsgEditWindowPrivate *priv;
640         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
641
642         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), NULL);
643         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
644
645         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, TRUE);
646
647         format_state = g_new0 (ModestMsgEditFormatState, 1);
648         format_state->bold = buffer_format->bold&0x1;
649         format_state->italics = buffer_format->italic&0x1;
650         format_state->bullet = buffer_format->bullet&0x1;
651         format_state->color = buffer_format->color;
652         format_state->font_size = buffer_format->font_size;
653         format_state->font_family = wp_get_font_name (buffer_format->font);
654         format_state->justification = buffer_format->justification;
655         g_free (buffer_format);
656
657         return format_state;
658  
659 }
660
661 void
662 modest_msg_edit_window_set_format_state (ModestMsgEditWindow *self,
663                                          const ModestMsgEditFormatState *format_state)
664 {
665         ModestMsgEditWindowPrivate *priv;
666         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
667         WPTextBufferFormat *current_format = g_new0 (WPTextBufferFormat, 1);
668         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
669         g_return_if_fail (format_state != NULL);
670
671         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
672         gtk_widget_grab_focus (priv->msg_body);
673         buffer_format->bold = (format_state->bold != FALSE);
674         buffer_format->italic = (format_state->italics != FALSE);
675         buffer_format->color = format_state->color;
676         buffer_format->font_size = format_state->font_size;
677         buffer_format->font = wp_get_font_index (format_state->font_family, 0);
678         buffer_format->justification = format_state->justification;
679         buffer_format->bullet = format_state->bullet;
680
681         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), current_format, TRUE);
682
683         buffer_format->cs.bold = ((buffer_format->bold&0x1) != (current_format->bold&0x1));
684         buffer_format->cs.italic = ((buffer_format->italic&0x1) != (current_format->italic&0x1));
685         buffer_format->cs.color = gdk_color_equal(&(buffer_format->color), &(current_format->color));
686         buffer_format->cs.font_size =  (buffer_format->font_size != current_format->font_size);
687         buffer_format->cs.font = (buffer_format->font != current_format->font);
688         buffer_format->cs.justification = (buffer_format->justification != current_format->justification);
689         buffer_format->cs.bullet = (buffer_format->bullet != current_format->bullet);
690
691         wp_text_buffer_freeze (WP_TEXT_BUFFER (priv->text_buffer));
692         if (buffer_format->cs.bold) {
693                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->bold&0x1));
694         }
695         if (buffer_format->cs.italic) {
696                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_ITALIC, (gpointer) (buffer_format->italic&0x1));
697         }
698         if (buffer_format->cs.color) {
699                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) (&(buffer_format->color)));
700         }
701         if (buffer_format->cs.font_size) {
702                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->font_size));
703         }
704         if (buffer_format->cs.justification) {
705                 switch (buffer_format->justification) {
706                 case GTK_JUSTIFY_LEFT:
707                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_LEFT, (gpointer) TRUE);
708                         break;
709                 case GTK_JUSTIFY_CENTER:
710                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_CENTER, (gpointer) TRUE);
711                         break;
712                 case GTK_JUSTIFY_RIGHT:
713                         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_RIGHT, (gpointer) TRUE);
714                         break;
715                 default:
716                         break;
717                 }
718                         
719         }
720         if (buffer_format->cs.font) {
721                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BOLD, (gpointer) (buffer_format->font));
722         }
723         if (buffer_format->cs.bullet) {
724                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_BULLET, (gpointer) (buffer_format->bullet));
725         }
726 /*      wp_text_buffer_set_format (WP_TEXT_BUFFER (priv->text_buffer), buffer_format); */
727         wp_text_buffer_thaw (WP_TEXT_BUFFER (priv->text_buffer));
728
729         g_free (current_format);
730
731 }
732
733 static void
734 toggle_action_set_active_block_notify (GtkToggleAction *action,
735                                        gboolean value)
736 {
737         GSList *proxies = NULL;
738
739         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
740              proxies != NULL; proxies = g_slist_next (proxies)) {
741                 GtkWidget *widget = (GtkWidget *) proxies->data;
742                 gtk_action_block_activate_from (GTK_ACTION (action), widget);
743         }
744
745         gtk_toggle_action_set_active (action, value);
746
747         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
748              proxies != NULL; proxies = g_slist_next (proxies)) {
749                 GtkWidget *widget = (GtkWidget *) proxies->data;
750                 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
751         }
752 }
753
754 static void
755 text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window)
756 {
757         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
758         GtkAction *action;
759         ModestWindowPrivate *parent_priv;
760         ModestMsgEditWindowPrivate *priv;
761         
762         parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
763         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
764
765         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
766         
767         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsBold");
768         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bold);
769
770         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsItalics");
771         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->italic);
772
773         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ActionsBulletedList");
774         toggle_action_set_active_block_notify (GTK_TOGGLE_ACTION (action), buffer_format->bullet);
775
776         g_signal_handlers_block_by_func (G_OBJECT (priv->font_color_button), 
777                                          G_CALLBACK (modest_msg_edit_window_color_button_change),
778                                          window);
779         hildon_color_button_set_color (HILDON_COLOR_BUTTON (priv->font_color_button), & (buffer_format->color));
780         g_signal_handlers_unblock_by_func (G_OBJECT (priv->font_color_button), 
781                                            G_CALLBACK (modest_msg_edit_window_color_button_change),
782                                            window);
783
784         g_signal_handlers_block_by_func (G_OBJECT (priv->size_combobox), 
785                                          G_CALLBACK (modest_msg_edit_window_size_combobox_change),
786                                          window);
787         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->size_combobox), buffer_format->font_size);
788         g_signal_handlers_unblock_by_func (G_OBJECT (priv->size_combobox), 
789                                            G_CALLBACK (modest_msg_edit_window_size_combobox_change),
790                                            window);
791
792         g_signal_handlers_block_by_func (G_OBJECT (priv->font_combobox), 
793                                          G_CALLBACK (modest_msg_edit_window_font_combobox_change),
794                                          window);
795         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->font_combobox), buffer_format->font);
796         g_signal_handlers_unblock_by_func (G_OBJECT (priv->font_combobox), 
797                                            G_CALLBACK (modest_msg_edit_window_font_combobox_change),
798                                            window);
799
800         g_free (buffer_format);
801
802 }
803
804 void
805 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
806 {
807         
808         WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
809         ModestMsgEditWindowPrivate *priv;
810         GtkWidget *dialog = NULL;
811         gint response;
812         const GdkColor *new_color = NULL;
813         
814         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
815         wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
816         
817         dialog = hildon_color_selector_new (GTK_WINDOW (window));
818         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), & (buffer_format->color));
819         g_free (buffer_format);
820
821         response = gtk_dialog_run (GTK_DIALOG (dialog));
822         switch (response) {
823         case GTK_RESPONSE_OK:
824                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
825                 break;
826         default:
827                 break;
828         }
829         gtk_widget_destroy (dialog);
830
831         if (new_color != NULL)
832                 wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
833
834 }
835
836 void
837 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
838 {
839         
840         ModestMsgEditWindowPrivate *priv;
841         GtkWidget *dialog = NULL;
842         gint response;
843         const GdkColor *old_color = NULL;
844         const GdkColor *new_color = NULL;
845         
846         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
847         old_color = wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
848         
849         dialog = hildon_color_selector_new (GTK_WINDOW (window));
850         hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), (GdkColor *) old_color);
851
852         response = gtk_dialog_run (GTK_DIALOG (dialog));
853         switch (response) {
854         case GTK_RESPONSE_OK:
855                 new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
856                 break;
857         default:
858                 break;
859         }
860         gtk_widget_destroy (dialog);
861
862         if (new_color != NULL)
863                 wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), new_color);
864
865 }
866
867 void
868 modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
869 {
870         
871         ModestMsgEditWindowPrivate *priv;
872         GtkWidget *dialog = NULL;
873         gint response = 0;
874         gchar *filename = NULL;
875         
876         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
877         
878         dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_OPEN);
879
880         response = gtk_dialog_run (GTK_DIALOG (dialog));
881         switch (response) {
882         case GTK_RESPONSE_OK:
883                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
884                 break;
885         default:
886                 break;
887         }
888         gtk_widget_destroy (dialog);
889
890         if (filename) {
891                 GdkPixbuf *pixbuf = NULL;
892                 GtkTextIter position;
893                 GtkTextMark *insert_mark;
894
895                 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
896                 if (pixbuf) {
897                         gint image_file_id;
898                         GdkPixbufFormat *pixbuf_format;
899
900                         image_file_id = g_open (filename, O_RDONLY, 0);
901                         pixbuf_format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
902                         if ((image_file_id != -1)&&(pixbuf_format != NULL)) {
903                                 TnyMimePart *image_part;
904                                 TnyStream *image_stream;
905                                 gchar **mime_types;
906                                 gchar *mime_type;
907                                 gchar *basename;
908                                 gchar *content_id;
909
910                                 mime_types = gdk_pixbuf_format_get_mime_types (pixbuf_format);
911                                 if ((mime_types != NULL) && (mime_types[0] != NULL)) {
912                                         mime_type = mime_types[0];
913                                 } else {
914                                         mime_type = "image/unknown";
915                                 }
916                                 image_part = tny_platform_factory_new_mime_part
917                                         (modest_runtime_get_platform_factory ());
918                                 image_stream = TNY_STREAM (tny_fs_stream_new (image_file_id));
919
920                                 tny_mime_part_construct_from_stream (image_part, image_stream, mime_type);
921                                 g_strfreev (mime_types);
922
923                                 content_id = g_strdup_printf ("%d", priv->last_cid);
924                                 tny_mime_part_set_content_id (image_part, content_id);
925                                 g_free (content_id);
926                                 priv->last_cid++;
927
928                                 basename = g_path_get_basename (filename);
929                                 tny_mime_part_set_filename (image_part, basename);
930                                 g_free (basename);
931                                 
932                                 insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
933                                 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &position, insert_mark);
934                                 wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, g_strdup (tny_mime_part_get_content_id (image_part)), pixbuf);
935                                 priv->attachments = g_list_prepend (priv->attachments, image_part);
936                         } else if (image_file_id == -1) {
937                                 close (image_file_id);
938                         }
939                 }
940         }
941
942
943 }
944
945 static void
946 modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
947                                             gpointer userdata)
948 {
949         ModestMsgEditWindowPrivate *priv;
950         GdkColor *new_color;
951
952         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
953         new_color = hildon_color_button_get_color (HILDON_COLOR_BUTTON (priv->font_color_button));
954
955         wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
956         gtk_window_set_focus (GTK_WINDOW (window), priv->msg_body);
957
958 }
959
960 static void
961 modest_msg_edit_window_size_combobox_change (ModestMsgEditWindow *window,
962                                              gpointer userdata)
963 {
964         ModestMsgEditWindowPrivate *priv;
965         gint new_size_index;
966
967         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
968         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
969
970         new_size_index = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->size_combobox));
971
972         if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT_SIZE, (gpointer) new_size_index))
973                 wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
974
975         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));;
976 }
977
978 static void
979 modest_msg_edit_window_font_combobox_change (ModestMsgEditWindow *window,
980                                              gpointer userdata)
981 {
982         ModestMsgEditWindowPrivate *priv;
983         gint new_font_index;
984
985         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
986         gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
987
988         new_font_index = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->font_combobox));
989
990         if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT, (gpointer) new_font_index))
991                 wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
992
993         text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
994 }