* big commit, mainly cleanups:
[modest] / src / maemo / 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 <hildon-widgets/hildon-window.h>
31
32 #include <glib/gi18n.h>
33 #include <gtk/gtktreeviewcolumn.h>
34 #include <tny-account-store-view.h>
35 #include <modest-runtime.h>
36 #include <string.h>
37
38 #include <widgets/modest-main-window.h>
39 #include <widgets/modest-msg-edit-window.h>
40 #include <widgets/modest-account-view-window.h>
41
42
43 #include "modest-widget-memory.h"
44 #include "modest-window-priv.h"
45 #include "modest-main-window-ui.h"
46 #include "modest-account-mgr.h"
47 #include "modest-conf.h"
48 #include <modest-maemo-utils.h>
49 #include "modest-tny-platform-factory.h"
50 #include "modest-tny-msg.h"
51 #include "modest-mail-operation.h"
52 #include "modest-icon-names.h"
53
54 /* 'private'/'protected' functions */
55 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
56 static void modest_main_window_init          (ModestMainWindow *obj);
57 static void modest_main_window_finalize      (GObject *obj);
58
59 static void connect_signals (ModestMainWindow *self);
60 static void restore_sizes (ModestMainWindow *self);
61 static void save_sizes (ModestMainWindow *self);
62
63 /* list my signals */
64 enum {
65         /* MY_SIGNAL_1, */
66         /* MY_SIGNAL_2, */
67         LAST_SIGNAL
68 };
69
70 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
71 struct _ModestMainWindowPrivate {
72         GtkWidget *msg_paned;
73         GtkWidget *main_paned;
74         GtkWidget *progress_bar;
75
76         ModestHeaderView *header_view;
77         ModestFolderView *folder_view;
78
79 };
80
81
82 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
83                                                 MODEST_TYPE_MAIN_WINDOW, \
84                                                 ModestMainWindowPrivate))
85
86 typedef struct _GetMsgAsyncHelper {
87         ModestMainWindowPrivate *main_window_private;
88         guint action;
89         ModestMailOperationReplyType reply_type;
90         ModestMailOperationForwardType forward_type;
91         gchar *from;
92         TnyIterator *iter;
93 } GetMsgAsyncHelper;
94
95 /* globals */
96 static GtkWindowClass *parent_class = NULL;
97
98 /* uncomment the following if you have defined any signals */
99 /* static guint signals[LAST_SIGNAL] = {0}; */
100
101 GType
102 modest_main_window_get_type (void)
103 {
104         static GType my_type = 0;
105         if (!my_type) {
106                 static const GTypeInfo my_info = {
107                         sizeof(ModestMainWindowClass),
108                         NULL,           /* base init */
109                         NULL,           /* base finalize */
110                         (GClassInitFunc) modest_main_window_class_init,
111                         NULL,           /* class finalize */
112                         NULL,           /* class data */
113                         sizeof(ModestMainWindow),
114                         1,              /* n_preallocs */
115                         (GInstanceInitFunc) modest_main_window_init,
116                         NULL
117                 };
118                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
119                                                   "ModestMainWindow",
120                                                   &my_info, 0);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_main_window_class_init (ModestMainWindowClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_main_window_finalize;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
135 }
136
137
138 static void
139 on_key_changed (ModestConf* conf, const gchar *key, ModestConfEvent event, ModestMainWindow *win)
140 {
141         TnyAccount *account;
142         
143         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME) != 0)
144                 return; /* wrong key */
145         
146         /* ok, the device name changed; thus, we have to update the
147          * local folder account name*/
148         account =
149                 modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
150                                                                      MODEST_LOCAL_FOLDERS_ACCOUNT_ID,
151                                                                      TNY_ACCOUNT_TYPE_STORE);
152         if (!account) {
153                 g_printerr ("modest: could not get account\n");
154                 return;
155         }
156
157         if (event == MODEST_CONF_EVENT_KEY_UNSET) 
158                 tny_account_set_name (account, MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
159         else {
160                 gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
161                                                              MODEST_CONF_DEVICE_NAME, NULL);
162                 tny_account_set_name (account, device_name);
163                 g_free (device_name);
164         }
165         g_object_unref (G_OBJECT(account));     
166 }
167
168 static void
169 modest_main_window_init (ModestMainWindow *obj)
170 {
171         ModestMainWindowPrivate *priv;
172
173         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
174
175         priv->msg_paned    = NULL;
176         priv->main_paned   = NULL;      
177         priv->header_view  = NULL;
178         priv->folder_view  = NULL;
179
180         /* progress bar */
181         priv->progress_bar = gtk_progress_bar_new ();
182         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar), 1.0);
183         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(priv->progress_bar),
184                                         PANGO_ELLIPSIZE_END);
185 }
186
187 static void
188 modest_main_window_finalize (GObject *obj)
189 {
190         G_OBJECT_CLASS(parent_class)->finalize (obj);
191 }
192
193 GtkWidget*
194 modest_main_window_get_child_widget (ModestMainWindow *self,
195                                      ModestWidgetType widget_type)
196 {
197         ModestMainWindowPrivate *priv;
198         GtkWidget *widget;
199         
200         g_return_val_if_fail (self, NULL);
201         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_WIDGET_TYPE_NUM,
202                               NULL);
203         
204         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
205
206         switch (widget_type) {
207         case MODEST_WIDGET_TYPE_HEADER_VIEW:
208                 widget = (GtkWidget*)priv->header_view; break;
209         case MODEST_WIDGET_TYPE_FOLDER_VIEW:
210                 widget = (GtkWidget*)priv->folder_view; break;
211         default:
212                 return NULL;
213         }
214
215         return widget ? GTK_WIDGET(widget) : NULL;
216 }
217
218
219
220 static void
221 restore_sizes (ModestMainWindow *self)
222 {
223         ModestConf *conf;
224         ModestMainWindowPrivate *priv;
225
226         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
227
228         conf = modest_runtime_get_conf ();
229         
230         modest_widget_memory_restore (conf,G_OBJECT(self),
231                                       "modest-main-window");
232         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
233                                       "modest-main-paned");
234         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
235                                       "header-view");
236 }
237
238
239 static void
240 save_sizes (ModestMainWindow *self)
241 {
242         ModestConf *conf;
243         ModestMainWindowPrivate *priv;
244                 
245         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
246         conf = modest_runtime_get_conf ();
247         
248         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
249         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
250                                    "modest-main-paned");
251         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
252 }
253
254 static GtkWidget*
255 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
256 {
257         GtkWidget *win;
258
259         win = gtk_scrolled_window_new (NULL, NULL);
260         gtk_scrolled_window_set_policy
261                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
262                  GTK_POLICY_AUTOMATIC);
263         
264         if (needs_viewport)
265                 gtk_scrolled_window_add_with_viewport
266                         (GTK_SCROLLED_WINDOW(win), widget);
267         else
268                 gtk_container_add (GTK_CONTAINER(win),
269                                    widget);
270
271         return win;
272 }
273
274
275 static gboolean
276 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
277 {
278         save_sizes (self);
279         return FALSE;
280 }
281
282
283 static GtkWidget*
284 get_toolbar (ModestMainWindow *self)
285 {
286         GtkWidget   *toolbar, *progress_box, *progress_alignment;
287         GtkToolItem *progress_item;
288         ModestWindowPrivate *parent_priv;
289         ModestMainWindowPrivate *priv;
290         GtkWidget   *stop_icon;
291         
292         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
293         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
294         
295         /* Toolbar */
296         toolbar       = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
297         progress_box        = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
298         progress_alignment  = gtk_alignment_new (0.5, 0.5, 1, 0);
299         
300         gtk_container_add  (GTK_CONTAINER(progress_alignment), priv->progress_bar);
301         gtk_box_pack_start (GTK_BOX(progress_box), progress_alignment, TRUE, TRUE, 0);
302         
303         progress_item  = gtk_tool_item_new ();
304         gtk_container_add (GTK_CONTAINER(progress_item), progress_box);
305         gtk_tool_item_set_homogeneous (progress_item, FALSE);
306         gtk_tool_item_set_expand(progress_item, TRUE);
307         
308         gtk_toolbar_insert (GTK_TOOLBAR(toolbar), progress_item,
309                             gtk_toolbar_get_n_items(GTK_TOOLBAR(toolbar)));
310
311         stop_icon = gtk_image_new_from_icon_name("qgn_toolb_gene_stop", GTK_ICON_SIZE_BUTTON);
312         gtk_toolbar_insert (GTK_TOOLBAR(toolbar), gtk_tool_button_new(stop_icon, NULL),
313                             gtk_toolbar_get_n_items(GTK_TOOLBAR(toolbar)));
314
315         gtk_widget_show_all (toolbar);
316         return toolbar;
317 }
318
319
320 static void
321 on_destroy (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
322 {
323         gtk_main_quit();
324 }
325
326 static void
327 connect_signals (ModestMainWindow *self)
328 {       
329         ModestWindowPrivate *parent_priv;
330         ModestMainWindowPrivate *priv;
331         
332         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
333         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
334         
335         /* folder view */
336         g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
337                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
338
339         /* header view */
340         g_signal_connect (G_OBJECT(priv->header_view), "status_update",
341                           G_CALLBACK(modest_ui_actions_on_header_status_update), self);
342         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
343                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
344         g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
345                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
346         g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
347                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
348
349         /* window */
350         g_signal_connect (G_OBJECT(self), "destroy", G_CALLBACK(on_destroy), NULL);
351         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
352
353         
354         /* modest_maemo_utils_get_device_name will probably change
355          * MODEST_CONF_DEVICE_NAME. If that happens, we update the local folders
356          * account name in the callback
357          */
358         g_signal_connect (G_OBJECT(modest_runtime_get_conf()), "key_changed",
359                           G_CALLBACK(on_key_changed), self);
360         
361         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
362 }
363
364
365 gboolean
366 sync_accounts_cb (ModestMainWindow *win)
367 {
368         modest_ui_actions_on_send_receive (NULL, MODEST_WINDOW(win));
369         return FALSE;
370 }
371
372
373         
374 ModestWindow*
375 modest_main_window_new (void)
376 {
377         ModestMainWindow *self; 
378         ModestMainWindowPrivate *priv;
379         ModestWindowPrivate *parent_priv;
380         GtkWidget *main_vbox;
381         GtkWidget *header_win, *folder_win;
382         GtkActionGroup *action_group;
383         GError *error = NULL;
384         TnyFolderStoreQuery     *query;
385
386         self  = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
387         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
388         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
389
390         parent_priv->ui_manager = gtk_ui_manager_new();
391         action_group = gtk_action_group_new ("ModestMainWindowActions");
392
393         /* Add common actions */
394         gtk_action_group_add_actions (action_group,
395                                       modest_action_entries,
396                                       G_N_ELEMENTS (modest_action_entries),
397                                       self);
398         
399         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
400         g_object_unref (action_group);
401
402         /* Load the UI definition */
403         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
404                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
405         if (error != NULL) {
406                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
407                 g_error_free (error);
408                 error = NULL;
409         }
410
411         /* Add accelerators */
412         gtk_window_add_accel_group (GTK_WINDOW (self), 
413                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
414
415         /* add the toolbar */
416         parent_priv->toolbar = get_toolbar(self);
417         hildon_window_add_toolbar (HILDON_WINDOW (self), GTK_TOOLBAR (parent_priv->toolbar));
418
419         /* Menubar */
420         parent_priv->menubar = modest_maemo_utils_menubar_to_menu (parent_priv->ui_manager);
421         hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
422
423         /* folder view */
424         query = tny_folder_store_query_new ();
425         tny_folder_store_query_add_item (query, NULL,
426                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
427         priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
428         if (!priv->folder_view)
429                 g_printerr ("modest: cannot instantiate folder view\n");        
430         g_object_unref (G_OBJECT (query));      
431         modest_maemo_utils_get_device_name ();
432
433         /* header view */
434         priv->header_view  =
435                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
436         if (!priv->header_view)
437                 g_printerr ("modest: cannot instantiate header view\n");
438         modest_header_view_set_style (priv->header_view, MODEST_HEADER_VIEW_STYLE_TWOLINES);
439         
440         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
441         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);                    
442         /* paned */
443         priv->main_paned = gtk_hpaned_new ();
444         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
445         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
446         gtk_widget_show (GTK_WIDGET(priv->header_view));
447         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
448
449         /* putting it all together... */
450         main_vbox = gtk_vbox_new (FALSE, 6);
451         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
452
453         gtk_container_add (GTK_CONTAINER(self), main_vbox);
454         restore_sizes (MODEST_MAIN_WINDOW(self));       
455
456         gtk_window_set_title (GTK_WINDOW(self), _("Modest"));
457         gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
458         gtk_widget_show_all (main_vbox);
459
460         /* should we hide the toolbar? */
461         if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL))
462                 gtk_widget_hide (parent_priv->toolbar);
463
464         /* Connect signals */
465         connect_signals (self);
466
467         /* Set account store */
468         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
469                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
470         g_idle_add ((GSourceFunc)sync_accounts_cb, self);
471         /* do send & receive when we are idle */
472         
473         return MODEST_WINDOW(self);
474 }
475
476
477
478
479
480