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