* big commit, mainly cleanups:
[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
62 #define MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
63                                                     MODEST_TYPE_MSG_VIEW_WINDOW, \
64                                                     ModestMsgViewWindowPrivate))
65 /* globals */
66 static GtkWindowClass *parent_class = NULL;
67
68 /* Action entries */
69 static const GtkActionEntry modest_action_entries [] = {
70
71         /* Toplevel menus */
72         { "Edit", NULL, N_("_Edit") },
73         { "Actions", NULL, N_("_Actions") },
74         { "Help", NULL, N_("_Help") },
75
76         /* EDIT */
77         { "EditUndo",        GTK_STOCK_UNDO,   N_("_Undo"), "<CTRL>Z",        N_("Undo last action"),  NULL },
78         { "EditRedo",        GTK_STOCK_REDO,   N_("_Redo"), "<shift><CTRL>Z", N_("Redo previous action"),  NULL },
79         { "EditCut",         GTK_STOCK_CUT,    N_("Cut"),   "<CTRL>X",        N_("_Cut"), NULL   },
80         { "EditCopy",        GTK_STOCK_COPY,   N_("Copy"),  "<CTRL>C",        N_("Copy"), NULL },
81         { "EditPaste",       GTK_STOCK_PASTE,  N_("Paste"), "<CTRL>V",        N_("Paste"), NULL },
82         { "EditDelete",      GTK_STOCK_DELETE, N_("_Delete"),      "<CTRL>Q",         N_("Delete"), NULL },
83         { "EditSelectAll",   NULL,             N_("Select all"),   "<CTRL>A",         N_("Select all"), NULL },
84         { "EditDeselectAll", NULL,             N_("Deselect all"), "<Shift><CTRL>A",  N_("Deselect all"), NULL },
85
86         /* ACTIONS */
87         { "ActionsReply",       MODEST_STOCK_REPLY, N_("_Reply"),         NULL, N_("Reply to a message"), G_CALLBACK (modest_ui_actions_on_reply) },
88         { "ActionsReplyAll",    MODEST_STOCK_REPLY_ALL, N_("Reply to all"),   NULL, N_("Reply to all"), G_CALLBACK (modest_ui_actions_on_reply_all) },
89         { "ActionsForward",     MODEST_STOCK_FORWARD, N_("_Forward"),       NULL, N_("Forward a message"), G_CALLBACK (modest_ui_actions_on_forward) },
90         { "ActionsBounce",      NULL, N_("_Bounce"),        NULL, N_("Bounce a message"),          NULL },
91         { "ActionsSendReceive", GTK_STOCK_REFRESH, N_("Send/Receive"),   NULL, N_("Send and receive messages"), NULL },
92         { "ActionsDelete",      MODEST_STOCK_DELETE, N_("Delete message"), NULL, N_("Delete messages"), G_CALLBACK (modest_ui_actions_on_delete) },
93
94         /* HELP */
95         { "HelpAbout", GTK_STOCK_ABOUT, N_("About"), NULL, N_("About Modest"), G_CALLBACK (modest_ui_actions_on_about) },
96 };
97
98
99 GType
100 modest_msg_view_window_get_type (void)
101 {
102         static GType my_type = 0;
103         if (!my_type) {
104                 static const GTypeInfo my_info = {
105                         sizeof(ModestMsgViewWindowClass),
106                         NULL,           /* base init */
107                         NULL,           /* base finalize */
108                         (GClassInitFunc) modest_msg_view_window_class_init,
109                         NULL,           /* class finalize */
110                         NULL,           /* class data */
111                         sizeof(ModestMsgViewWindow),
112                         1,              /* n_preallocs */
113                         (GInstanceInitFunc) modest_msg_view_window_init,
114                         NULL
115                 };
116                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
117                                                   "ModestMsgViewWindow",
118                                                   &my_info, 0);
119         }
120         return my_type;
121 }
122
123 static void
124 modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass)
125 {
126         GObjectClass *gobject_class;
127         gobject_class = (GObjectClass*) klass;
128
129         parent_class            = g_type_class_peek_parent (klass);
130         gobject_class->finalize = modest_msg_view_window_finalize;
131
132         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewWindowPrivate));
133 }
134
135 static void
136 modest_msg_view_window_init (ModestMsgViewWindow *obj)
137 {
138         ModestMsgViewWindowPrivate *priv;
139         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
140
141         priv->toolbar       = NULL;
142         priv->menubar       = NULL;
143         priv->msg_view      = NULL;
144 }
145
146 static void
147 save_settings (ModestMsgViewWindow *self)
148 {
149         modest_widget_memory_save (modest_runtime_get_conf (),
150                                     G_OBJECT(self), "modest-msg-view-window");
151 }
152
153
154 static void
155 restore_settings (ModestMsgViewWindow *self)
156 {
157         modest_widget_memory_restore (modest_runtime_get_conf (),
158                                       G_OBJECT(self), "modest-msg-view-window");
159 }
160
161
162 static void
163 init_window (ModestMsgViewWindow *obj, TnyMsg *msg)
164 {
165         GtkWidget *main_vbox;
166         ModestMsgViewWindowPrivate *priv;
167         ModestWindowPrivate *parent_priv;
168         
169         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
170         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
171
172         priv->msg_view = modest_msg_view_new (msg);
173         main_vbox = gtk_vbox_new  (FALSE, 6);
174         
175         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
176         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
177         gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_view, TRUE, TRUE, 6);
178
179         gtk_widget_show_all (GTK_WIDGET(main_vbox));
180         gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
181 }
182
183
184 static void
185 modest_msg_view_window_finalize (GObject *obj)
186 {       
187         G_OBJECT_CLASS(parent_class)->finalize (obj);
188 }
189
190
191
192 static gboolean
193 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self)
194 {
195         save_settings (self);
196         return FALSE;
197 }
198
199
200 ModestWindow *
201 modest_msg_view_window_new (TnyMsg *msg, const gchar *account)
202 {
203         GObject *obj;
204         ModestMsgViewWindowPrivate *priv;
205         ModestWindowPrivate *parent_priv;
206         GtkActionGroup *action_group;
207         GError *error = NULL;
208
209         g_return_val_if_fail (msg, NULL);
210
211         obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
212         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
213         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
214
215         modest_window_set_active_account (MODEST_WINDOW(obj), account);
216         
217         parent_priv->ui_manager = gtk_ui_manager_new();
218         action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
219
220         /* Add common actions */
221         gtk_action_group_add_actions (action_group,
222                                       modest_action_entries,
223                                       G_N_ELEMENTS (modest_action_entries),
224                                       obj);
225         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
226         g_object_unref (action_group);
227
228         
229         /* Load the UI definition */
230         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
231                                          MODEST_UIDIR "modest-msg-view-window-ui.xml",
232                                          &error);
233         if (error) {
234                 g_printerr ("modest: could not merge modest-msg-view-window-ui.xml: %s\n", error->message);
235                 g_error_free (error);
236                 error = NULL;
237         }
238         /* ****** */
239
240         /* Add accelerators */
241         gtk_window_add_accel_group (GTK_WINDOW (obj), 
242                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
243
244         /* Toolbar / Menubar */
245         priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
246         priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
247
248         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
249
250         /* Init window */
251         init_window (MODEST_MSG_VIEW_WINDOW(obj), msg);
252         restore_settings (MODEST_MSG_VIEW_WINDOW(obj));
253         
254         gtk_window_set_title (GTK_WINDOW(obj), "Modest");
255         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
256
257         g_signal_connect (G_OBJECT(obj), "delete-event", G_CALLBACK(on_delete_event), obj);
258
259         return MODEST_WINDOW(obj);
260 }
261
262
263 TnyMsg*
264 modest_msg_view_window_get_message (ModestMsgViewWindow *self)
265 {
266         GtkWidget *msg_view;    
267         g_return_val_if_fail (self, NULL);
268
269         msg_view = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self)->msg_view;
270
271         return modest_msg_view_get_message (MODEST_MSG_VIEW(msg_view));
272 }