df143c272ae1edf12704b52f9ffa5672dd318c35
[modest] / src / gtk / 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
35 #include <widgets/modest-msg-view-window.h>
36 #include "modest-main-window-ui.h"
37 #include <widgets/modest-window-priv.h>
38
39 #include <modest-widget-memory.h>
40 #include <modest-runtime.h>
41
42 static void  modest_msg_view_window_class_init   (ModestMsgViewWindowClass *klass);
43 static void  modest_msg_view_window_init         (ModestMsgViewWindow *obj);
44 static void  modest_msg_view_window_finalize     (GObject *obj);
45
46 /* list my signals */
47 enum {
48         /* MY_SIGNAL_1, */
49         /* MY_SIGNAL_2, */
50         LAST_SIGNAL
51 };
52
53 typedef struct _ModestMsgViewWindowPrivate ModestMsgViewWindowPrivate;
54 struct _ModestMsgViewWindowPrivate {
55         GtkWidget   *toolbar;
56         GtkWidget   *menubar;
57         GtkWidget   *msg_view;
58 };
59
60 #define MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
61                                                     MODEST_TYPE_MSG_VIEW_WINDOW, \
62                                                     ModestMsgViewWindowPrivate))
63 /* globals */
64 static GtkWindowClass *parent_class = NULL;
65
66 /* uncomment the following if you have defined any signals */
67 /* static guint signals[LAST_SIGNAL] = {0}; */
68
69 GType
70 modest_msg_view_window_get_type (void)
71 {
72         static GType my_type = 0;
73         if (!my_type) {
74                 static const GTypeInfo my_info = {
75                         sizeof(ModestMsgViewWindowClass),
76                         NULL,           /* base init */
77                         NULL,           /* base finalize */
78                         (GClassInitFunc) modest_msg_view_window_class_init,
79                         NULL,           /* class finalize */
80                         NULL,           /* class data */
81                         sizeof(ModestMsgViewWindow),
82                         1,              /* n_preallocs */
83                         (GInstanceInitFunc) modest_msg_view_window_init,
84                         NULL
85                 };
86                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
87                                                   "ModestMsgViewWindow",
88                                                   &my_info, 0);
89         }
90         return my_type;
91 }
92
93 static void
94 modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass)
95 {
96         GObjectClass *gobject_class;
97         gobject_class = (GObjectClass*) klass;
98
99         parent_class            = g_type_class_peek_parent (klass);
100         gobject_class->finalize = modest_msg_view_window_finalize;
101
102         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewWindowPrivate));
103 }
104
105 static void
106 modest_msg_view_window_init (ModestMsgViewWindow *obj)
107 {
108         ModestMsgViewWindowPrivate *priv;
109         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
110
111         priv->toolbar       = NULL;
112         priv->menubar       = NULL;
113         priv->msg_view      = NULL;
114 }
115
116 static void
117 save_settings (ModestMsgViewWindow *self)
118 {
119         modest_widget_memory_save (modest_runtime_get_conf (),
120                                     G_OBJECT(self), "modest-msg-view-window");
121 }
122
123
124 static void
125 restore_settings (ModestMsgViewWindow *self)
126 {
127         modest_widget_memory_restore (modest_runtime_get_conf (),
128                                       G_OBJECT(self), "modest-msg-view-window");
129 }
130
131
132 static void
133 init_window (ModestMsgViewWindow *obj, TnyMsg *msg)
134 {
135         GtkWidget *main_vbox;
136         ModestMsgViewWindowPrivate *priv;
137         ModestWindowPrivate *parent_priv;
138         
139         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
140         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
141
142         priv->msg_view = modest_msg_view_new (msg);
143         main_vbox = gtk_vbox_new  (FALSE, 6);
144         
145         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
146         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
147         gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_view, TRUE, TRUE, 6);
148
149         gtk_widget_show_all (GTK_WIDGET(main_vbox));
150         gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
151 }
152
153
154 static void
155 modest_msg_view_window_finalize (GObject *obj)
156 {       
157         G_OBJECT_CLASS(parent_class)->finalize (obj);
158 }
159
160
161
162 static gboolean
163 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgViewWindow *self)
164 {
165         save_settings (self);
166         return FALSE;
167 }
168
169
170 ModestWindow *
171 modest_msg_view_window_new (TnyMsg *msg, const gchar *account)
172 {
173         GObject *obj;
174         ModestMsgViewWindowPrivate *priv;
175         ModestWindowPrivate *parent_priv;
176         GtkActionGroup *action_group;
177         GError *error = NULL;
178
179         g_return_val_if_fail (msg, NULL);
180         g_return_val_if_fail (msg, NULL);
181         
182         obj = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
183         priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj);
184         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
185
186         modest_window_set_active_account (MODEST_WINDOW(obj), account);
187         
188         parent_priv->ui_manager = gtk_ui_manager_new();
189         action_group = gtk_action_group_new ("ModestMsgViewWindowActions");
190
191         /* Add common actions */
192         gtk_action_group_add_actions (action_group,
193                                       modest_action_entries,
194                                       G_N_ELEMENTS (modest_action_entries),
195                                       obj);
196         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
197         g_object_unref (action_group);
198
199         
200         /* Load the UI definition */
201         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
202                                          MODEST_UIDIR "modest-msg-view-window-ui.xml",
203                                          &error);
204         if (error) {
205                 g_printerr ("modest: could not merge modest-msg-view-window-ui.xml: %s\n", error->message);
206                 g_error_free (error);
207                 error = NULL;
208         }
209         /* ****** */
210
211         /* Add accelerators */
212         gtk_window_add_accel_group (GTK_WINDOW (obj), 
213                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
214
215         /* Toolbar / Menubar */
216         priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
217         priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
218
219         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
220
221         /* Init window */
222         init_window (MODEST_MSG_VIEW_WINDOW(obj), msg);
223         restore_settings (MODEST_MSG_VIEW_WINDOW(obj));
224         
225         gtk_window_set_title (GTK_WINDOW(obj), "Modest");
226         gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
227
228         g_signal_connect (G_OBJECT(obj), "delete-event", G_CALLBACK(on_delete_event), obj);
229
230         return MODEST_WINDOW(obj);
231 }