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