2ed3095f93aa5f33c53129330cfd08377e6dd62d
[modest] / src / gnome / 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 <string.h>
31 #include <tny-account-store.h>
32 #include <tny-simple-list.h>
33 #include <modest-conf.h>
34 #include <modest-runtime.h>
35 #include <modest-tny-msg.h>
36
37 #include <widgets/modest-window-priv.h>
38 #include <widgets/modest-msg-edit-window.h>
39 #include <widgets/modest-msg-edit-window-ui.h>
40 #include <widgets/modest-combo-box.h>
41
42 #include <modest-widget-memory.h>
43 #include <modest-account-mgr-helpers.h>
44
45 static void  modest_msg_edit_window_class_init   (ModestMsgEditWindowClass *klass);
46 static void  modest_msg_edit_window_init         (ModestMsgEditWindow *obj);
47 static void  modest_msg_edit_window_finalize     (GObject *obj);
48
49 /* list my signals */
50 enum {
51         /* MY_SIGNAL_1, */
52         /* MY_SIGNAL_2, */
53         LAST_SIGNAL
54 };
55
56 typedef struct _ModestMsgEditWindowPrivate ModestMsgEditWindowPrivate;
57 struct _ModestMsgEditWindowPrivate {
58
59         GtkWidget   *toolbar;
60         GtkWidget   *menubar;
61
62         GtkWidget   *msg_body;
63         GtkWidget   *from_field;
64         GtkWidget   *to_field;
65         GtkWidget   *cc_field;
66         GtkWidget   *bcc_field;
67         GtkWidget   *subject_field;
68 };
69
70 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
71                                                     MODEST_TYPE_MSG_EDIT_WINDOW, \
72                                                     ModestMsgEditWindowPrivate))
73 /* globals */
74 static GtkWindowClass *parent_class = NULL;
75
76 /* uncomment the following if you have defined any signals */
77 /* static guint signals[LAST_SIGNAL] = {0}; */
78
79 GType
80 modest_msg_edit_window_get_type (void)
81 {
82         static GType my_type = 0;
83         if (!my_type) {
84                 static const GTypeInfo my_info = {
85                         sizeof(ModestMsgEditWindowClass),
86                         NULL,           /* base init */
87                         NULL,           /* base finalize */
88                         (GClassInitFunc) modest_msg_edit_window_class_init,
89                         NULL,           /* class finalize */
90                         NULL,           /* class data */
91                         sizeof(ModestMsgEditWindow),
92                         1,              /* n_preallocs */
93                         (GInstanceInitFunc) modest_msg_edit_window_init,
94                         NULL
95                 };
96                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
97                                                   "ModestMsgEditWindow",
98                                                   &my_info, 0);
99         }
100         return my_type;
101 }
102
103 static void
104 modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
105 {
106         GObjectClass *gobject_class;
107         gobject_class = (GObjectClass*) klass;
108
109         parent_class            = g_type_class_peek_parent (klass);
110         gobject_class->finalize = modest_msg_edit_window_finalize;
111
112         g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
113 }
114
115 static void
116 modest_msg_edit_window_init (ModestMsgEditWindow *obj)
117 {
118         ModestMsgEditWindowPrivate *priv;
119         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
120
121         priv->toolbar       = NULL;
122         priv->menubar       = NULL;
123         priv->msg_body      = NULL;
124         priv->from_field    = NULL;
125         priv->to_field      = NULL;
126         priv->cc_field      = NULL;
127         priv->bcc_field     = NULL;
128         priv->subject_field = NULL;
129 }
130
131 static void
132 save_settings (ModestMsgEditWindow *self)
133 {
134         modest_widget_memory_save (modest_runtime_get_conf (),
135                                     G_OBJECT(self), "modest-edit-msg-window");
136 }
137
138
139 static void
140 restore_settings (ModestMsgEditWindow *self)
141 {
142         modest_widget_memory_restore (modest_runtime_get_conf (),
143                                       G_OBJECT(self), "modest-edit-msg-window");
144 }
145
146
147
148 static ModestPairList*
149 get_transports (void)
150 {
151         ModestAccountMgr *account_mgr;
152         GSList *transports = NULL;
153         GSList *cursor, *accounts;
154         
155         account_mgr = modest_runtime_get_account_mgr();
156         cursor = accounts = modest_account_mgr_account_names (account_mgr);
157         while (cursor) {
158                 gchar *account_name = (gchar*)cursor->data;
159                 gchar *from_string  = modest_account_mgr_get_from_string (account_mgr,
160                                                                           account_name);
161                 if (!from_string)  {
162                         /* something went wrong: ignore this one */
163                         g_free (account_name);
164                         cursor->data = NULL;
165                 } else {
166                         ModestPair *pair;
167                         pair = modest_pair_new ((gpointer) account_name,
168                                                 (gpointer) from_string , TRUE);
169                         transports = g_slist_prepend (transports, pair);
170                 } /* don't free account name; it's freed when the transports list is freed */
171                 cursor = cursor->next;
172         }
173         g_slist_free (accounts);
174         return transports;
175 }
176
177
178 static void
179 on_from_combo_changed (ModestComboBox *combo, ModestWindow *win)
180 {
181         modest_window_set_active_account (
182                 win, modest_combo_box_get_active_id(combo));
183 }
184
185
186
187 static void
188 init_window (ModestMsgEditWindow *obj, const gchar* account)
189 {
190         GtkWidget *to_button, *cc_button, *bcc_button; 
191         GtkWidget *header_table;
192         GtkWidget *main_vbox;
193         ModestMsgEditWindowPrivate *priv;
194         ModestWindowPrivate *parent_priv;
195         ModestPairList *protos;
196         
197         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
198         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
199
200         to_button     = gtk_button_new_with_label (_("To..."));
201         cc_button     = gtk_button_new_with_label (_("Cc..."));
202         bcc_button    = gtk_button_new_with_label (_("Bcc..."));
203         
204         protos = get_transports ();
205         priv->from_field    = modest_combo_box_new (protos, g_str_equal);
206         modest_pair_list_free (protos);
207         if (account) {
208                 modest_combo_box_set_active_id (MODEST_COMBO_BOX(priv->from_field),
209                                                 (gpointer)account);
210                 modest_window_set_active_account (MODEST_WINDOW(obj), account);
211         }
212         /* auto-update the active account */
213         g_signal_connect (G_OBJECT(priv->from_field), "changed", G_CALLBACK(on_from_combo_changed), obj);
214         
215         priv->to_field      = gtk_entry_new_with_max_length (80);
216         priv->cc_field      = gtk_entry_new_with_max_length (80);
217         priv->bcc_field     = gtk_entry_new_with_max_length (80);
218         priv->subject_field = gtk_entry_new_with_max_length (80);
219         
220         header_table = gtk_table_new (5,2, FALSE);
221         
222         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("From:")),
223                           0,1,0,1, GTK_SHRINK, 0, 0, 0);
224         gtk_table_attach (GTK_TABLE(header_table), to_button,     0,1,1,2, GTK_SHRINK, 0, 0, 0);
225         gtk_table_attach (GTK_TABLE(header_table), cc_button,     0,1,2,3, GTK_SHRINK, 0, 0, 0);
226         gtk_table_attach (GTK_TABLE(header_table), bcc_button,    0,1,3,4, GTK_SHRINK, 0, 0, 0);
227         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("Subject:")),
228                           0,1,4,5, GTK_SHRINK, 0, 0, 0);
229
230         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->from_field,   1,2,0,1);
231         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->to_field,     1,2,1,2);
232         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->cc_field,     1,2,2,3);
233         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->bcc_field,    1,2,3,4);
234         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->subject_field,1,2,4,5);
235
236         priv->msg_body = gtk_text_view_new ();
237         
238         main_vbox = gtk_vbox_new  (FALSE, 6);
239
240         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
241         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
242         gtk_box_pack_start (GTK_BOX(main_vbox), header_table, FALSE, FALSE, 6);
243         gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_body, TRUE, TRUE, 6);
244
245         gtk_widget_show_all (GTK_WIDGET(main_vbox));
246         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
247 }
248
249
250 static void
251 modest_msg_edit_window_finalize (GObject *obj)
252 {
253         G_OBJECT_CLASS(parent_class)->finalize (obj);
254 }
255
256
257
258 static gboolean
259 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgEditWindow *self)
260 {
261         save_settings (self);
262         return FALSE;
263 }
264
265
266 static void
267 set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
268 {
269         TnyHeader *header;
270         GtkTextBuffer *buf;
271         const gchar *to, *cc, *bcc, *subject;
272         ModestMsgEditWindowPrivate *priv;
273         gchar *body;
274         
275         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
276         g_return_if_fail (TNY_IS_MSG (msg));
277
278         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
279
280         header  = tny_msg_get_header (msg);
281         to      = tny_header_get_to (header);
282         cc      = tny_header_get_cc (header);
283         bcc     = tny_header_get_bcc (header);
284         subject = tny_header_get_subject (header);
285
286         if (to)
287                 gtk_entry_set_text (GTK_ENTRY(priv->to_field), to);
288         if (cc)
289                 gtk_entry_set_text (GTK_ENTRY(priv->cc_field), cc);
290         if (bcc)
291                 gtk_entry_set_text (GTK_ENTRY(priv->bcc_field),  bcc);
292         if (subject)
293                 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);
294
295         
296         buf  = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body));
297         body = modest_tny_msg_get_body (msg, FALSE);
298         if (body) 
299                 gtk_text_buffer_set_text (buf, body, -1);
300         g_free (body);
301 }
302
303
304 ModestWindow *
305 modest_msg_edit_window_new (TnyMsg *msg, const gchar *account)
306 {
307         ModestMsgEditWindow *self;
308         ModestMsgEditWindowPrivate *priv;
309         ModestWindowPrivate *parent_priv;
310         GtkActionGroup *action_group;
311         GError *error = NULL;
312
313         g_return_val_if_fail (msg, NULL);
314         
315         self = MODEST_MSG_EDIT_WINDOW(g_object_new(MODEST_TYPE_MSG_EDIT_WINDOW, NULL));
316         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(self);
317         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
318         
319         parent_priv->ui_manager = gtk_ui_manager_new();
320         action_group = gtk_action_group_new ("ModestMsgEditWindowActions");
321
322         /* Add common actions */
323         gtk_action_group_add_actions (action_group,
324                                       modest_msg_edit_action_entries,
325                                       G_N_ELEMENTS (modest_msg_edit_action_entries),
326                                       self);
327         gtk_action_group_add_toggle_actions (action_group,
328                                              modest_msg_edit_toggle_action_entries,
329                                              G_N_ELEMENTS (modest_msg_edit_toggle_action_entries),
330                                              self);
331         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
332         g_object_unref (action_group);
333
334         /* Load the UI definition */
335         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
336                                          MODEST_UIDIR "modest-msg-edit-window-ui.xml",
337                                          &error);
338         if (error) {
339                 g_printerr ("modest: could not merge modest-msg-edit-window-ui.xml: %s\n", error->message);
340                 g_error_free (error);
341                 error = NULL;
342         }
343         /* ****** */
344
345         /* Add accelerators */
346         gtk_window_add_accel_group (GTK_WINDOW (self), 
347                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
348
349         /* Toolbar / Menubar */
350         priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
351         priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
352
353         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
354
355         /* Init window */
356         init_window (MODEST_MSG_EDIT_WINDOW(self), account);
357
358         restore_settings (MODEST_MSG_EDIT_WINDOW(self));
359         
360         gtk_window_set_title (GTK_WINDOW(self), "Modest");
361         gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
362
363         g_signal_connect (G_OBJECT(self), "delete-event",
364                           G_CALLBACK(on_delete_event), self);
365         
366         set_msg (self, msg);
367         
368         return MODEST_WINDOW(self);
369 }
370
371
372 MsgData * 
373 modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
374 {
375         MsgData *data;
376         GtkTextBuffer *buf;
377         GtkTextIter b, e;
378         const gchar *account_name;
379         gchar *from_string = NULL;
380         ModestMsgEditWindowPrivate *priv;
381         
382         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
383
384         priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
385         
386         account_name = (gchar*)modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
387         if (account_name) 
388                 from_string = modest_account_mgr_get_from_string (
389                         modest_runtime_get_account_mgr(), account_name);
390         if (!from_string) {
391                 g_printerr ("modest: cannot get from string\n");
392                 return NULL;
393         }
394         
395         buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
396         gtk_text_buffer_get_bounds (buf, &b, &e);
397
398         /* don't free these (except from) */
399         data = g_slice_new0 (MsgData);
400         data->from    =  from_string, /* will be freed when data is freed */
401         data->to      =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->to_field));
402         data->cc      =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->cc_field));
403         data->bcc     =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->bcc_field));
404         data->subject =  (gchar*) gtk_entry_get_text (GTK_ENTRY(priv->subject_field));
405         data->body    =  gtk_text_buffer_get_text (buf, &b, &e, FALSE);
406
407         return data;
408 }
409
410 void 
411 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
412                                       MsgData *data)
413 {
414         g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window));
415
416         g_free (data->from);
417         g_free (data->body);
418         g_slice_free (MsgData, data);
419 }