* Cleaned some header files, deleting unused includes
[modest] / src / gtk / modest-main-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
30 #include <glib/gi18n.h>
31 #include <gtk/gtktreeviewcolumn.h>
32
33 #include "modest-main-window.h"
34 #include "modest-widget-factory.h"
35 #include "modest-widget-memory.h"
36 #include "modest-icon-factory.h"
37 #include "modest-ui.h"
38 #include "modest-main-window-ui.h"
39 #include "modest-account-view-window.h"
40 #include "modest-account-mgr.h"
41 #include "modest-conf.h"
42 #include "modest-edit-msg-window.h"
43 #include "modest-tny-platform-factory.h"
44 #include "modest-tny-msg-actions.h"
45 #include "modest-mail-operation.h"
46 #include "modest-icon-names.h"
47
48 /* 'private'/'protected' functions */
49 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
50 static void modest_main_window_init          (ModestMainWindow *obj);
51 static void modest_main_window_finalize      (GObject *obj);
52
53 static void restore_sizes (ModestMainWindow *self);
54 static void save_sizes (ModestMainWindow *self);
55
56 /* list my signals */
57 enum {
58         /* MY_SIGNAL_1, */
59         /* MY_SIGNAL_2, */
60         LAST_SIGNAL
61 };
62
63 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
64 struct _ModestMainWindowPrivate {
65
66         GtkUIManager *ui_manager;
67         ModestWidgetFactory *widget_factory;
68         TnyPlatformFactory *factory;
69         TnyAccountStore *account_store;
70         
71         GtkWidget *toolbar;
72         GtkWidget *menubar;
73
74         GtkWidget *folder_paned;
75         GtkWidget *msg_paned;
76         GtkWidget *main_paned;
77         
78         ModestHeaderView *header_view;
79         ModestFolderView *folder_view;
80         ModestMsgView    *msg_preview;
81 };
82
83
84 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
85                                                 MODEST_TYPE_MAIN_WINDOW, \
86                                                 ModestMainWindowPrivate))
87
88 typedef struct _GetMsgAsyncHelper {
89         ModestMainWindowPrivate *main_window_private;
90         guint action;
91         ModestMailOperationReplyType reply_type;
92         ModestMailOperationForwardType forward_type;
93         gchar *from;
94         TnyIterator *iter;
95 } GetMsgAsyncHelper;
96
97 /* globals */
98 static GtkWindowClass *parent_class = NULL;
99
100 /* uncomment the following if you have defined any signals */
101 /* static guint signals[LAST_SIGNAL] = {0}; */
102
103 GType
104 modest_main_window_get_type (void)
105 {
106         static GType my_type = 0;
107         if (!my_type) {
108                 static const GTypeInfo my_info = {
109                         sizeof(ModestMainWindowClass),
110                         NULL,           /* base init */
111                         NULL,           /* base finalize */
112                         (GClassInitFunc) modest_main_window_class_init,
113                         NULL,           /* class finalize */
114                         NULL,           /* class data */
115                         sizeof(ModestMainWindow),
116                         1,              /* n_preallocs */
117                         (GInstanceInitFunc) modest_main_window_init,
118                         NULL
119                 };
120                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
121                                                   "ModestMainWindow",
122                                                   &my_info, 0);
123         }
124         return my_type;
125 }
126
127 static void
128 modest_main_window_class_init (ModestMainWindowClass *klass)
129 {
130         GObjectClass *gobject_class;
131         gobject_class = (GObjectClass*) klass;
132
133         parent_class            = g_type_class_peek_parent (klass);
134         gobject_class->finalize = modest_main_window_finalize;
135
136         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
137
138         /* signal definitions go here, e.g.: */
139 /*      signals[MY_SIGNAL_1] = */
140 /*              g_signal_new ("my_signal_1",....); */
141 /*      signals[MY_SIGNAL_2] = */
142 /*              g_signal_new ("my_signal_2",....); */
143 /*      etc. */
144 }
145
146 static void
147 modest_main_window_init (ModestMainWindow *obj)
148 {
149         ModestMainWindowPrivate *priv;
150
151         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
152         
153         priv->factory = modest_tny_platform_factory_get_instance ();
154         priv->widget_factory = NULL;
155         priv->ui_manager = NULL;
156         priv->account_store = NULL;
157 }
158
159 static void
160 modest_main_window_finalize (GObject *obj)
161 {
162         ModestMainWindowPrivate *priv;  
163         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
164         if (priv->widget_factory) {
165                 g_object_unref (G_OBJECT(priv->widget_factory));
166                 priv->widget_factory = NULL;
167         }
168         if (priv->ui_manager) {
169                 g_object_unref (G_OBJECT(priv->ui_manager));
170                 priv->ui_manager = NULL;
171         }
172         if (priv->account_store) {
173                 g_object_unref (G_OBJECT(priv->account_store));
174                 priv->account_store = NULL;
175         }
176
177         G_OBJECT_CLASS(parent_class)->finalize (obj);
178 }
179
180
181 static ModestHeaderView*
182 header_view_new (ModestMainWindow *self)
183 {
184         int i;
185         GList *columns = NULL;
186         ModestHeaderView *header_view;
187         ModestMainWindowPrivate *priv;
188         ModestHeaderViewColumn cols[] = {
189                 MODEST_HEADER_VIEW_COLUMN_MSGTYPE,
190                 MODEST_HEADER_VIEW_COLUMN_ATTACH,
191 /*              MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER, */
192                 MODEST_HEADER_VIEW_COLUMN_FROM,
193                 MODEST_HEADER_VIEW_COLUMN_SUBJECT,
194                 MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE
195         };
196         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
197         
198         for (i = 0 ; i != sizeof(cols) / sizeof(ModestHeaderViewColumn); ++i)
199                 columns = g_list_append (columns, GINT_TO_POINTER(cols[i]));
200
201         header_view = modest_widget_factory_get_header_view (priv->widget_factory);
202         modest_header_view_set_columns (header_view, columns);
203         g_list_free (columns);
204
205         return header_view;
206 }
207
208
209 static void
210 restore_sizes (ModestMainWindow *self)
211 {
212         ModestConf *conf;
213         ModestMainWindowPrivate *priv;
214         
215         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
216         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->factory);
217
218         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_paned),
219                                       "modest-folder-paned");
220         modest_widget_memory_restore (conf, G_OBJECT(priv->msg_paned),
221                                       "modest-msg-paned");
222         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
223                                       "modest-main-paned");
224         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
225                                       "header-view");
226         modest_widget_memory_restore (conf,G_OBJECT(self),
227                                       "modest-main-window");
228 }
229
230
231 static void
232 save_sizes (ModestMainWindow *self)
233 {
234         ModestMainWindowPrivate *priv;
235         ModestConf *conf;
236         
237         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
238         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->factory);
239         
240         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
241         modest_widget_memory_save (conf, G_OBJECT(priv->folder_paned),
242                                    "modest-folder-paned");
243         modest_widget_memory_save (conf, G_OBJECT(priv->msg_paned),
244                                    "modest-msg-paned");
245         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
246                                    "modest-main-paned");
247         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
248 }
249
250 static GtkWidget*
251 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
252 {
253         GtkWidget *win;
254
255         win = gtk_scrolled_window_new (NULL, NULL);
256         gtk_scrolled_window_set_policy
257                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
258                  GTK_POLICY_AUTOMATIC);
259         
260         if (needs_viewport)
261                 gtk_scrolled_window_add_with_viewport
262                         (GTK_SCROLLED_WINDOW(win), widget);
263         else
264                 gtk_container_add (GTK_CONTAINER(win),
265                                    widget);
266
267         return win;
268 }
269
270
271 static gboolean
272 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
273 {
274         save_sizes (self);
275         return FALSE;
276 }
277
278
279 ModestWindow *
280 modest_main_window_new (ModestWidgetFactory *widget_factory,
281                         TnyAccountStore *account_store)
282 {
283         GObject *obj;
284         ModestMainWindowPrivate *priv;
285         GtkWidget *main_vbox;
286         GtkWidget *status_hbox;
287         GtkWidget *header_win, *folder_win;
288         GtkActionGroup *action_group;
289         GError *error = NULL;
290         
291         g_return_val_if_fail (widget_factory, NULL);
292
293         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
294         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
295
296         priv->widget_factory = g_object_ref (widget_factory);
297         priv->account_store  = g_object_ref (account_store);
298
299         /* ***************** */
300         priv->ui_manager = gtk_ui_manager_new();
301         action_group = gtk_action_group_new ("ModestMainWindowActions");
302
303         /* Add common actions */
304         gtk_action_group_add_actions (action_group,
305                                       modest_action_entries,
306                                       G_N_ELEMENTS (modest_action_entries),
307                                       obj);
308
309         gtk_ui_manager_insert_action_group (priv->ui_manager, action_group, 0);
310         g_object_unref (action_group);
311
312         /* Load the UI definition */
313         gtk_ui_manager_add_ui_from_file (priv->ui_manager, MODEST_UIDIR "modest-ui.xml", &error);
314         if (error != NULL) {
315                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
316                 g_error_free (error);
317                 error = NULL;
318         }
319         /* *************** */
320 /*      priv->ui_manager = g_object_ref (ui_manager); */
321
322         /* Add accelerators */
323         gtk_window_add_accel_group (GTK_WINDOW (obj), 
324                                     gtk_ui_manager_get_accel_group (priv->ui_manager));
325
326
327         /* Toolbar / Menubar */
328         priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/ToolBar");
329         priv->menubar = gtk_ui_manager_get_widget (priv->ui_manager, "/MenuBar");
330
331         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
332
333         /* widgets from factory */
334         priv->folder_view = modest_widget_factory_get_folder_view (widget_factory);
335         priv->header_view = header_view_new (MODEST_MAIN_WINDOW(obj));
336         priv->msg_preview = modest_widget_factory_get_msg_preview (widget_factory);
337         
338         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view),
339                                                  FALSE);
340         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view),
341                                                  FALSE);                           
342
343         /* paned */
344         priv->folder_paned = gtk_vpaned_new ();
345         priv->msg_paned = gtk_vpaned_new ();
346         priv->main_paned = gtk_hpaned_new ();
347         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
348         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->msg_paned);
349         gtk_paned_add1 (GTK_PANED(priv->msg_paned), header_win);
350         gtk_paned_add2 (GTK_PANED(priv->msg_paned), GTK_WIDGET(priv->msg_preview));
351
352         gtk_widget_show (GTK_WIDGET(priv->header_view));
353         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
354
355         
356         /* status bar / progress */
357         status_hbox = gtk_hbox_new (FALSE, 0);
358         gtk_box_pack_start (GTK_BOX(status_hbox),
359                             modest_widget_factory_get_folder_info_label (widget_factory),
360                             FALSE,FALSE, 6);
361         gtk_box_pack_start (GTK_BOX(status_hbox),
362                             modest_widget_factory_get_status_bar(widget_factory),
363                             TRUE, TRUE, 0);
364         gtk_box_pack_start (GTK_BOX(status_hbox),
365                             modest_widget_factory_get_progress_bar(widget_factory),
366                             FALSE, FALSE, 0);
367         gtk_box_pack_start (GTK_BOX(status_hbox),
368                           modest_widget_factory_get_online_toggle(widget_factory),
369                           FALSE, FALSE, 0);
370
371         /* putting it all together... */
372         main_vbox = gtk_vbox_new (FALSE, 6);
373         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
374         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
375         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
376         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
377         
378         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
379         restore_sizes (MODEST_MAIN_WINDOW(obj));        
380
381         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
382         gtk_window_set_icon  (GTK_WINDOW(obj),
383                               modest_icon_factory_get_icon (MODEST_APP_ICON));
384         
385         gtk_widget_show_all (main_vbox);
386
387         g_signal_connect (G_OBJECT(obj), "delete-event",
388                           G_CALLBACK(on_delete_event), obj);
389         
390         return (ModestWindow *) obj;
391 }
392
393 ModestWidgetFactory *
394 modest_main_window_get_widget_factory (ModestMainWindow *main_window)
395 {
396         ModestMainWindowPrivate *priv;
397         
398         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (main_window), NULL);
399
400         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
401
402         return g_object_ref (priv->widget_factory);
403 }
404
405 TnyAccountStore * 
406 modest_main_window_get_account_store (ModestMainWindow *main_window)
407 {
408         ModestMainWindowPrivate *priv;
409         
410         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (main_window), NULL);
411
412         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
413
414         return g_object_ref (priv->account_store);
415 }