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