2007-08-16 Murray Cumming <murrayc@murrayc.com>
[modest] / src / gnome / modest-msg-view-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-tny-msg.h>
34 #include "modest-icon-names.h"
35 #include "modest-ui-actions.h"
36 #include <modest-widget-memory.h>
37 #include <modest-runtime.h>
38
39 #include <widgets/modest-msg-view-window.h>
40 #include <widgets/modest-window-priv.h>
41 #include "widgets/modest-msg-view.h"
42
43
44 static void  modest_msg_view_window_class_init   (ModestMsgViewWindowClass *klass);
45 static void  modest_msg_view_window_init         (ModestMsgViewWindow *obj);
46 static void  modest_msg_view_window_finalize     (GObject *obj);
47
48 /* list my signals */
49 enum {
50         /* MY_SIGNAL_1, */
51         /* MY_SIGNAL_2, */
52         LAST_SIGNAL
53 };
54
55 typedef struct _ModestMsgViewWindowPrivate ModestMsgViewWindowPrivate;
56 struct _ModestMsgViewWindowPrivate {
57         GtkWidget   *toolbar;
58         GtkWidget   *menubar;
59         GtkWidget   *msg_view;
60
61         gchar *msg_uid;
62 };
63
64 #define MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
65                                                     MODEST_TYPE_MSG_VIEW_WINDOW, \
66                                                     ModestMsgViewWindowPrivate))
67 /* globals */
68 static GtkWindowClass *parent_class = NULL;
69
70 /* Action entries */
71 static const GtkActionEntry modest_action_entries [] = {
72
73         /* Toplevel menus */
74         { "Edit", NULL, N_("_Edit") },
75         { "Actions", NULL, N_("_Actions") },
76         { "Help", NULL, N_("_Help") },
77         { "Email", NULL, N_("E_mail") },
78
79         /* EDIT */
80         { "EditUndo",        GTK_STOCK_UNDO,   N_("_Undo"), "<CTRL>Z",        N_("Undo last action"),  NULL },
81         { "EditRedo",        GTK_STOCK_REDO,   N_("_Redo"), "<shift><CTRL>Z", N_("Redo previous action"),  NULL },
82         { "Cut",         GTK_STOCK_CUT,    N_("Cut"),   "<CTRL>X",        N_("_Cut"), G_CALLBACK (modest_ui_actions_on_cut)   },
83         { "Copy",        GTK_STOCK_COPY,   N_("Copy"),  "<CTRL>C",        N_("Copy"), G_CALLBACK (modest_ui_actions_on_copy) },
84         { "Paste",       GTK_STOCK_PASTE,  N_("Paste"), "<CTRL>V",        N_("Paste"), G_CALLBACK (modest_ui_actions_on_paste) },
85         { "EditDelete",      GTK_STOCK_DELETE, N_("_Delete"),      "<CTRL>Q",         N_("Delete"), NULL },
86         { "SelectAll",   NULL,         N_("Select all"),   "<CTRL>A",         N_("Select all"), G_CALLBACK (modest_ui_actions_on_select_all) },
87         { "EditDeselectAll", NULL,             N_("Deselect all"), "<Shift><CTRL>A",  N_("Deselect all"), NULL },
88
89         /* ACTIONS */
90         { "ActionsNewMessage",  MODEST_STOCK_NEW_MAIL, N_("_New"), "<CTRL>N", N_("Compose new message"), G_CALLBACK (modest_ui_actions_on_new_msg) },
91         { "ActionsReply",       MODEST_STOCK_REPLY, N_("_Reply"),         NULL, N_("Reply to a message"), G_CALLBACK (modest_ui_actions_on_reply) },
92         { "ActionsReplyAll",    MODEST_STOCK_REPLY_ALL, N_("Reply to all"),   NULL, N_("Reply to all"), G_CALLBACK (modest_ui_actions_on_reply_all) },
93         { "ActionsForward",     MODEST_STOCK_FORWARD, N_("_Forward"),       NULL, N_("Forward a message"), G_CALLBACK (modest_ui_actions_on_forward) },
94         { "ActionsBounce",      NULL, N_("_Bounce"),        NULL, N_("Bounce a message"),          NULL },
95         { "ActionsSendReceive", GTK_STOCK_REFRESH, N_("Send/Receive"),   NULL, N_("Send and receive messages"), NULL },
96         { "ActionsDelete",      MODEST_STOCK_DELETE, N_("Delete message"), NULL, N_("Delete messages"), G_CALLBACK (modest_ui_actions_on_delete) },
97
98         /* HELP */
99         { "HelpAbout", GTK_STOCK_ABOUT, N_("About"), NULL, N_("About Modest"), G_CALLBACK (modest_ui_actions_on_about) },
100 };
101
102
103 GType
104 modest_msg_view_window_get_type (void)
105 {
106         static GType my_type = 0;
107         if (!my_type) {
108                 static const GTypeInfo my_info = {
109                         sizeof(ModestMsgViewWindowClass),
110                         NULL,           /* base init */
111                         NULL,           /* base finalize */
112                         (GClassInitFunc) modest_msg_view_window_class_init,
113                         NULL,           /* class finalize */
114                         NULL,           /* class data */
115                         sizeof(ModestMsgViewWindow),
116                         1,              /* n_preallocs */
117                         (GInstanceInitFunc) modest_msg_view_window_init,
118                         NULL
119                 };
120                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
121                                                   "ModestMsgViewWindow",
122                                                   &my_info, 0);
123         }
124         return my_type;
125 }
126
127 static void
128 save_state (ModestWindow *self)
129 {
130         modest_widget_memory_save (modest_runtime_get_conf (),
131                                    G_OBJECT(self), 
132                                    MODEST_CONF_MSG_VIEW_WINDOW_KEY);
133 }
134
135
136 static void
137 restore_settings (ModestWindow *self)
138 {
139         modest_widget_memory_restore (modest_runtime_get_conf (),
140                                       G_OBJECT(self), 
141                                       MODEST_CONF_MSG_VIEW_WINDOW_KEY);
142 }
143
144 static void
145 modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass)
146 {
147         GObjectClass *gobject_class;
148         gobject_class = (GObjectClass*) klass;
149
150         parent_class            = g_type_class_peek_parent (klass);
151         gobject_class->finalize = modest_msg_view_window_finalize;
152
153         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewWindowPrivate));
154
155         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
156         modest_window_class->save_state_func = save_state;
157 }
158
159 static void
160 modest_msg_view_window_init (ModestMsgViewWindow *obj)
161 {
162         ModestMsgViewWindowPrivate *priv;
163         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
164
165         priv->toolbar  = NULL;
166         priv->menubar  = NULL;
167         priv->msg_view = NULL;
168         priv->msg_uid  = NULL;
169 }
170
171
172 static void
173 init_window (ModestMsgViewWindow *obj, TnyMsg *msg)
174 {
175         GtkWidget *main_vbox, *scrolled_window;
176         ModestMsgViewWindowPrivate *priv;
177         ModestWindowPrivate *parent_priv;
178         
179         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
180         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
181
182         priv->msg_view = modest_msg_view_new (msg);
183         main_vbox = gtk_vbox_new  (FALSE, 6);
184         
185         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
186         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
187         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
188         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
189                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
190         gtk_container_add (GTK_CONTAINER (scrolled_window), 
191                            priv->msg_view);
192         gtk_box_pack_start (GTK_BOX(main_vbox), scrolled_window, TRUE, TRUE, 6);
193
194         gtk_widget_show_all (GTK_WIDGET(main_vbox));
195         gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
196 }
197
198
199 static void
200 modest_msg_view_window_finalize (GObject *obj)
201 {
202         ModestMsgViewWindowPrivate *priv;
203
204         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
205
206         if (priv->msg_uid) {
207                 g_free (priv->msg_uid);
208                 msg_uid = NULL;
209         }
210
211         G_OBJECT_CLASS(parent_class)->finalize (obj);
212 }
213
214
215
216 static gboolean
217 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self)
218 {
219         modest_window_save_state (MODEST_WINDOW(self));
220         return FALSE;
221 }
222
223
224 ModestWindow *
225 modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
226                             const gchar *modest_account_name, 
227                             const gchar *msg_uid)
228 {
229         GObject *obj;
230         ModestMsgViewWindowPrivate *priv;
231         ModestWindowPrivate *parent_priv;
232         GtkActionGroup *action_group;
233         GError *error = NULL;
234         TnyHeader *header = NULL;
235         const gchar *subject = NULL;
236
237         g_return_val_if_fail (msg, NULL);
238
239         obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
240         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
241         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
242
243         priv->msg_uid = g_strdup (msg_uid);
244
245         modest_window_set_active_account (MODEST_WINDOW(obj), account);
246         
247         parent_priv->ui_manager = gtk_ui_manager_new();
248         action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
249
250         /* Add common actions */
251         gtk_action_group_add_actions (action_group,
252                                       modest_action_entries,
253                                       G_N_ELEMENTS (modest_action_entries),
254                                       obj);
255         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
256         g_object_unref (action_group);
257
258         
259         /* Load the UI definition */
260         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
261                                          MODEST_UIDIR "modest-msg-view-window-ui.xml",
262                                          &error);
263         if (error) {
264                 g_printerr ("modest: could not merge modest-msg-view-window-ui.xml: %s\n", error->message);
265                 g_error_free (error);
266                 error = NULL;
267         }
268         /* ****** */
269
270         /* Add accelerators */
271         gtk_window_add_accel_group (GTK_WINDOW (obj), 
272                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
273
274         /* Toolbar / Menubar */
275         priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
276         priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
277
278         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
279
280         /* Init window */
281         init_window (MODEST_MSG_VIEW_WINDOW(obj), msg);
282         restore_settings (MODEST_WINDOW(obj));
283
284         header = tny_msg_get_header (msg);
285         if (header)
286                 subject = tny_header_get_subject (header);
287         
288         if (subject != NULL)
289                 gtk_window_set_title (GTK_WINDOW (obj), subject);
290         else
291                 gtk_window_set_title (GTK_WINDOW(obj), "Modest");
292
293         if (header)
294                 g_object_unref (header);
295
296         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
297
298         g_signal_connect (G_OBJECT(obj), "delete-event", G_CALLBACK(on_delete_event), obj);
299
300         return MODEST_WINDOW(obj);
301 }
302
303
304 TnyMsg*
305 modest_msg_view_window_get_message (ModestMsgViewWindow *self)
306 {
307         GtkWidget *msg_view;    
308         g_return_val_if_fail (self, NULL);
309
310         msg_view = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self)->msg_view;
311
312         return modest_msg_view_get_message (MODEST_MSG_VIEW(msg_view));
313 }
314
315 const gchar*
316 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
317 {
318         TnyMsg *msg;
319         TnyHeader *header;
320         const gchar *retval = NULL;
321
322         msg = modest_msg_view_window_get_message (self);
323
324         if (!msg)
325                 return NULL;
326
327         header = tny_msg_get_header (msg);
328         if (header) {
329                 retval = tny_header_get_uid (header);
330                 g_object_unref (header);
331         }
332         g_object_unref (msg);
333
334         return retval;
335 }
336
337 ModestWindow*   
338 modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
339                                               const gchar *modest_account_name, 
340                                               const gchar *msg_uid,
341                                               GtkTreeModel *model, 
342                                               GtkTreeRowReference *row_reference)
343 {
344         /* Currently we simply redirect to new constructor. It should store a
345            reference to the header list model, to enable next/prev message
346            actions */
347         g_message ("partially implemented %s", __FUNCTION__);
348
349         return modest_msg_view_window_new_for_attachment (msg, account);
350 }
351
352
353 gboolean
354 modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
355 {
356         g_message ("not implemented %s", __FUNCTION__);
357         return FALSE;
358 }
359
360 gboolean
361 modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
362 {
363         g_message ("not implemented %s", __FUNCTION__);
364         return FALSE;
365 }
366
367 void
368 modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part)
369 {
370         g_message ("not implemented %s", __FUNCTION__);
371 }
372
373 void
374 modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, GList *mime_parts)
375 {
376         g_message ("not implemented %s", __FUNCTION__);
377 }
378 void
379 modest_msg_view_window_remove_attachments (ModestMsgViewWindow *window, GList *mime_parts)
380 {
381         g_message ("not implemented %s", __FUNCTION__);
382 }