These changes are for reenabling build of gnome port.
[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_message) },
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 = GTK_WIDGET (tny_platform_factory_new_msg_view (modest_tny_platform_factory_get_instance ()));
183         tny_msg_view_set_msg (TNY_MSG_VIEW (priv->msg_view), msg);
184         main_vbox = gtk_vbox_new  (FALSE, 6);
185         
186         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
187         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
188         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
189         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
190                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
191         gtk_container_add (GTK_CONTAINER (scrolled_window), 
192                            priv->msg_view);
193         gtk_box_pack_start (GTK_BOX(main_vbox), scrolled_window, TRUE, TRUE, 6);
194
195         gtk_widget_show_all (GTK_WIDGET(main_vbox));
196         gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
197 }
198
199
200 static void
201 modest_msg_view_window_finalize (GObject *obj)
202 {
203         ModestMsgViewWindowPrivate *priv;
204
205         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
206
207         G_OBJECT_CLASS(parent_class)->finalize (obj);
208 }
209
210
211
212 static gboolean
213 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self)
214 {
215         modest_window_save_state (MODEST_WINDOW(self));
216         return FALSE;
217 }
218
219
220 ModestWindow *
221 modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
222                             const gchar *modest_account_name, 
223                             const gchar *msg_uid)
224 {
225         GObject *obj;
226         ModestMsgViewWindowPrivate *priv;
227         ModestWindowPrivate *parent_priv;
228         GtkActionGroup *action_group;
229         GError *error = NULL;
230         TnyHeader *header = NULL;
231         const gchar *subject = NULL;
232
233         g_return_val_if_fail (msg, NULL);
234
235         obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
236         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
237         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
238
239         priv->msg_uid = g_strdup (msg_uid);
240
241         modest_window_set_active_account (MODEST_WINDOW(obj), modest_account_name);
242         
243         parent_priv->ui_manager = gtk_ui_manager_new();
244         action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
245
246         /* Add common actions */
247         gtk_action_group_add_actions (action_group,
248                                       modest_action_entries,
249                                       G_N_ELEMENTS (modest_action_entries),
250                                       obj);
251         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
252         g_object_unref (action_group);
253
254         
255         /* Load the UI definition */
256         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
257                                          MODEST_UIDIR "modest-msg-view-window-ui.xml",
258                                          &error);
259         if (error) {
260                 g_printerr ("modest: could not merge modest-msg-view-window-ui.xml: %s\n", error->message);
261                 g_error_free (error);
262                 error = NULL;
263         }
264         /* ****** */
265
266         /* Add accelerators */
267         gtk_window_add_accel_group (GTK_WINDOW (obj), 
268                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
269
270         /* Toolbar / Menubar */
271         priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
272         priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
273
274         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
275
276         /* Init window */
277         init_window (MODEST_MSG_VIEW_WINDOW(obj), msg);
278         restore_settings (MODEST_WINDOW(obj));
279
280         header = tny_msg_get_header (msg);
281         if (header)
282                 subject = tny_header_get_subject (header);
283         
284         if (subject != NULL)
285                 gtk_window_set_title (GTK_WINDOW (obj), subject);
286         else
287                 gtk_window_set_title (GTK_WINDOW(obj), "Modest");
288
289         if (header)
290                 g_object_unref (header);
291
292         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
293
294         g_signal_connect (G_OBJECT(obj), "delete-event", G_CALLBACK(on_delete_event), obj);
295
296         return MODEST_WINDOW(obj);
297 }
298
299
300 TnyMsg*
301 modest_msg_view_window_get_message (ModestMsgViewWindow *self)
302 {
303         GtkWidget *msg_view;    
304         g_return_val_if_fail (self, NULL);
305
306         msg_view = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self)->msg_view;
307
308         return tny_msg_view_get_msg (TNY_MSG_VIEW(msg_view));
309 }
310
311 const gchar*
312 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
313 {
314         ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
315         TnyMsg *msg;
316         TnyHeader *header;
317         const gchar *retval = NULL;
318
319         msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
320         msg = modest_msg_view_window_get_message (self);
321
322         if (!msg)
323                 return NULL;
324
325         header = tny_msg_get_header (msg);
326         if (header) {
327                 retval = tny_header_get_uid (header);
328                 g_object_unref (header);
329         }
330         g_object_unref (msg);
331
332         return retval;
333 }
334
335 ModestWindow*   
336 modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
337                                               const gchar *modest_account_name, 
338                                               const gchar *msg_uid,
339                                               GtkTreeModel *model, 
340                                               GtkTreeRowReference *row_reference)
341 {
342         /* Currently we simply redirect to new constructor. It should store a
343            reference to the header list model, to enable next/prev message
344            actions */
345         g_message ("partially implemented %s", __FUNCTION__);
346
347         return modest_msg_view_window_new_for_attachment (msg, modest_account_name, msg_uid);
348 }
349
350
351 gboolean
352 modest_msg_view_window_select_next_message (ModestMsgViewWindow *window)
353 {
354         g_message ("not implemented %s", __FUNCTION__);
355         return FALSE;
356 }
357
358 gboolean
359 modest_msg_view_window_select_previous_message (ModestMsgViewWindow *window)
360 {
361         g_message ("not implemented %s", __FUNCTION__);
362         return FALSE;
363 }
364
365 void
366 modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part)
367 {
368         g_message ("not implemented %s", __FUNCTION__);
369 }
370
371 void
372 modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, GList *mime_parts)
373 {
374         g_message ("not implemented %s", __FUNCTION__);
375 }
376 void
377 modest_msg_view_window_remove_attachments (ModestMsgViewWindow *window, gboolean get_all)
378 {
379         g_message ("not implemented %s", __FUNCTION__);
380 }
381
382 TnyHeader *
383 modest_msg_view_window_get_header (ModestMsgViewWindow *self)
384 {
385         TnyMsg *msg;
386         TnyHeader *header = NULL;
387
388         msg = modest_msg_view_window_get_message (self);
389         if (msg) {
390                 header = tny_msg_get_header (msg);
391                 g_object_unref (msg);
392         }
393         return header;
394 }
395
396 TnyFolderType
397 modest_msg_view_window_get_folder_type (ModestMsgViewWindow *window)
398 {
399         ModestMsgViewWindowPrivate *priv;
400         TnyMsg *msg;
401         TnyFolderType folder_type;
402
403         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
404
405         folder_type = TNY_FOLDER_TYPE_UNKNOWN;
406
407         msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
408         if (msg) {
409                 TnyFolder *folder;
410
411                 folder = tny_msg_get_folder (msg);
412                 if (folder) {
413                         folder_type = tny_folder_get_folder_type (folder);
414                         g_object_unref (folder);
415                 }
416                 g_object_unref (msg);
417         }
418
419         return folder_type;
420 }
421
422 /* NOT IMPLEMENTED METHODS */
423
424 gboolean 
425 modest_msg_view_window_last_message_selected (ModestMsgViewWindow *window)
426 {
427         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
428         return TRUE;
429 }
430
431 gboolean 
432 modest_msg_view_window_first_message_selected (ModestMsgViewWindow *window)
433 {
434         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
435         return TRUE;
436 }
437
438 gboolean 
439 modest_msg_view_window_transfer_mode_enabled (ModestMsgViewWindow *self)
440 {
441         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
442         return FALSE;
443 }
444
445 gboolean  
446 modest_msg_view_window_toolbar_on_transfer_mode     (ModestMsgViewWindow *self)
447 {
448         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
449         return FALSE;
450 }
451
452
453 GList *         
454 modest_msg_view_window_get_attachments (ModestMsgViewWindow *win)
455 {
456         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
457         return NULL;    
458 }
459
460 gboolean 
461 modest_msg_view_window_is_search_result (ModestMsgViewWindow *window)
462 {
463         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
464         return FALSE;   
465 }
466
467 gboolean 
468 modest_msg_view_window_has_headers_model (ModestMsgViewWindow *window)
469 {
470         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
471         return FALSE;   
472 }