53c32dc99777f5fb5077089ffd5aa678158ef05c
[modest] / src / modest-init.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 <config.h>
31 #include <glib.h>
32 #include <glib-object.h>
33 #include <glib/gstdio.h>
34 #include <modest-runtime.h>
35 #include <modest-runtime-priv.h>
36 #include <modest-init.h>
37 #include <modest-defs.h>
38 #include <modest-singletons.h>
39 #include <widgets/modest-header-view.h>
40 #include <widgets/modest-folder-view.h>
41 #include <modest-tny-platform-factory.h>
42 #include <modest-platform.h>
43 #include <modest-widget-memory.h>
44 #include <modest-widget-memory-priv.h>
45 #include <modest-local-folder-info.h>
46 #include <modest-account-mgr.h>
47 #include <modest-account-mgr-helpers.h>
48 #include <modest-icon-names.h>
49
50 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
51 static gboolean init_local_folders  (void);
52 static gboolean init_default_account_maybe  (ModestAccountMgr *acc_mgr);
53 static void     init_i18n (void);
54 static void     init_stock_icons (void);
55 static void     init_debug_g_type (void);
56 static void     init_debug_logging (void);
57 static void     init_default_settings (ModestConf *conf);
58 static void     init_device_name (ModestConf *conf);
59
60 /*
61  * defaults for the column headers
62  */
63 typedef struct {
64         ModestHeaderViewColumn col;
65         guint                  width;
66         gint                  sort;
67 } FolderCols;
68
69 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
70         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
71         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
72         {MODEST_HEADER_VIEW_COLUMN_FROM,    80, 0},
73         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
74         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
75         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
76 };
77
78 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
79         {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
80         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
81         {MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE, 240, -1}
82 };
83
84 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
85         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
86         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
87         {MODEST_HEADER_VIEW_COLUMN_TO,    80, 0},
88         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
89         {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
90         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
91 };
92
93 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
94         {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
95         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
96         {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
97 };
98
99 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
100         {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
101         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
102         {MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE, 240, -1}
103 };
104
105 #ifdef MODEST_PLATFORM_MAEMO
106 static const TnyFolderType LOCAL_FOLDERS[] = {
107         TNY_FOLDER_TYPE_OUTBOX,
108         TNY_FOLDER_TYPE_DRAFTS,
109         TNY_FOLDER_TYPE_SENT
110 };
111 #else
112 static const TnyFolderType LOCAL_FOLDERS[] = {
113         TNY_FOLDER_TYPE_OUTBOX,
114         TNY_FOLDER_TYPE_DRAFTS,
115         TNY_FOLDER_TYPE_SENT,
116         TNY_FOLDER_TYPE_TRASH,
117         TNY_FOLDER_TYPE_ARCHIVE 
118 };
119 #endif /* MODEST_PLATFORM_MAEMO */
120
121
122 gboolean
123 modest_init_init_core (void)
124 {
125         gboolean reset;
126         static gboolean invoked = FALSE;
127
128         if (invoked) {
129                 g_printerr ("modest: modest_init_init_core may only be invoked once\n");
130                 g_assert (!invoked); /* abort */
131                 return FALSE;
132         } else
133                 invoked = TRUE;
134         
135         init_i18n();
136         init_debug_g_type();
137         init_debug_logging();
138
139         g_thread_init(NULL);
140         gdk_threads_init ();
141         
142         if (!modest_runtime_init()) {
143                 modest_init_uninit ();
144                 g_printerr ("modest: failed to initialize the modest runtime\n");
145                 return FALSE;
146         }
147
148
149         /* do an initial guess for the device name */
150         init_device_name (modest_runtime_get_conf());
151
152         if (!modest_platform_init()) {
153                 modest_init_uninit ();
154                 g_printerr ("modest: failed to run platform-specific initialization\n");
155                 return FALSE;
156         }
157
158         /* based on the debug settings, we decide whether to overwrite old settings */
159         reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
160         if (!init_header_columns(modest_runtime_get_conf(), reset)) {
161                 modest_init_uninit ();
162                 g_printerr ("modest: failed to init header columns\n");
163                 return FALSE;
164         }
165
166         init_default_settings (modest_runtime_get_conf ());
167         
168         if (!init_local_folders()) {
169                 modest_init_uninit ();
170                 g_printerr ("modest: failed to init local folders\n");
171                 return FALSE;
172         }
173         
174         if (!init_default_account_maybe(modest_runtime_get_account_mgr ())) {
175                 modest_init_uninit ();
176                 g_printerr ("modest: failed to init default account\n");
177                 return FALSE;
178         }
179         
180         return TRUE;
181 }
182
183
184 gboolean
185 modest_init_init_ui (gint argc, gchar** argv)
186 {
187         if (!gtk_init_check(&argc, &argv)) {
188                 g_printerr ("modest: failed to initialize graphical ui\n");
189                 return FALSE;
190         }
191
192         /* Set application name */
193         g_set_application_name (modest_platform_get_app_name());
194         g_message (modest_platform_get_app_name());
195
196         init_stock_icons ();
197         return TRUE;
198 }
199
200
201 gboolean
202 modest_init_uninit (void)
203 {
204         if (!modest_runtime_uninit())
205                 g_printerr ("modest: failed to uninit runtime\n");
206                 
207         return TRUE;
208 }
209
210
211
212
213 /* NOTE: the exact details of this format are important, as they
214  * are also used in modest-widget-memory. FIXME: make a shared function
215  * for this with widget-memory
216  */
217 static gboolean
218 save_header_settings (ModestConf *conf, TnyFolderType type,
219                       ModestHeaderViewStyle style,  const FolderCols* cols,
220                       guint col_num, gboolean overwrite)
221 {
222         int i;
223         gchar *key;
224         GString *str;
225
226         g_return_val_if_fail (cols, FALSE);
227
228         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
229                                                                   type, style,
230                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
231         /* if we're not in overwrite mode, only write stuff it
232          * there was nothing before */
233         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
234                 g_free (key);
235                 return TRUE;
236         }
237
238         /* the format is necessarily the same as the one in modest-widget-memory */
239         str = g_string_new (NULL);
240         for (i = 0; i != col_num; ++i) 
241                 g_string_append_printf (str, "%d:%d:%d ",
242                                         cols[i].col, cols[i].width, cols[i].sort); 
243
244         modest_conf_set_string (conf, key, str->str, NULL);
245         g_free (key);
246         g_string_free (str, TRUE);
247         
248         return TRUE;
249 }
250
251 /**
252  * modest_init_header_columns:
253  * @overwrite: write the setting, even if it already exists
254  * 
255  * will set defaults for the columns to show for folder,
256  * if there are no such settings yet (in ModestWidgetMemory)
257  * 
258  * Returns: TRUE if succeeded, FALSE in case of error
259  */
260 static gboolean
261 init_header_columns (ModestConf *conf, gboolean overwrite)
262 {
263         int folder_type;
264         
265         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
266              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
267                 
268                 switch (folder_type) {
269                 case TNY_FOLDER_TYPE_SENT:
270                 case TNY_FOLDER_TYPE_DRAFTS:
271                 save_header_settings (conf, folder_type,
272                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
273                                       OUTBOX_COLUMNS_DETAILS,
274                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
275                                       overwrite);
276                 save_header_settings (conf, folder_type,
277                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
278                                       SENT_COLUMNS_TWOLINES,
279                                       G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
280                                       overwrite);
281                 break;
282                 case TNY_FOLDER_TYPE_OUTBOX:
283                 save_header_settings (conf, folder_type,
284                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
285                                       OUTBOX_COLUMNS_TWOLINES,
286                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
287                                       overwrite);
288                 break;
289
290                 default:
291                 save_header_settings (conf, folder_type,
292                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
293                                       INBOX_COLUMNS_DETAILS,
294                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
295                                       overwrite);
296                 save_header_settings (conf, folder_type,
297                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
298                                       INBOX_COLUMNS_TWOLINES,
299                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
300                                       overwrite);
301                 };
302         }
303         return TRUE;
304 }
305
306 /**
307  * init_local_folders:
308  * 
309  * create the Local Folders folder under cache, if they
310  * do not exist yet.
311  * 
312  * Returns: TRUE if the folder were already there, or
313  * they were created, FALSE otherwise
314  */
315 static gboolean
316 init_local_folders  (void)
317 {
318         int i;
319         gchar *maildir_path;
320         static const gchar* maildirs[] = {
321                 "cur", "new", "tmp"
322         };
323         
324         maildir_path = modest_local_folder_info_get_maildir_path ();
325
326         for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
327                 int j;
328                 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
329                         gchar *dir;
330                         dir = g_build_filename (maildir_path,
331                                                 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
332                                                 maildirs[j],
333                                                 NULL);
334                         if (g_mkdir_with_parents (dir, 0755) < 0) {
335                                 g_printerr ("modest: failed to create %s\n", dir);
336                                 g_free (dir);
337                                 g_free (maildir_path);
338                                 return FALSE;
339                         }
340                         g_free(dir);
341                 }
342         }
343         
344         g_free (maildir_path);
345         return TRUE;
346 }
347
348
349
350 static void
351 free_element (gpointer data, gpointer user_data)
352 {
353         g_free (data);
354 }
355
356
357
358 /**
359  * init_default_account_maybe:
360  *
361  * if there are accounts defined, but there is no default account,
362  * it will be defined.
363  * 
364  * Returns: TRUE if there was a default account already,
365  *  or one has been created or there are no accounts yet,
366  *  returns FALSE in case of error
367  */
368 static gboolean
369 init_default_account_maybe  (ModestAccountMgr *acc_mgr)
370 {
371         GSList *all_accounts = NULL;
372         gchar *default_account;
373         gboolean retval = TRUE;
374         
375         all_accounts = modest_account_mgr_account_names (acc_mgr);
376         if (all_accounts) { /* if there are any accounts, there should be a default one */
377                 default_account = 
378                         modest_account_mgr_get_default_account (acc_mgr);
379                 if (!default_account) {
380                         gchar *first_account;
381                         g_printerr ("modest: no default account defined\n");
382                         first_account = (gchar*)all_accounts->data;
383                         if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
384                                 g_printerr ("modest: set '%s' as the default account\n",
385                                             first_account);
386                         else
387                                 g_printerr ("modest: failed to set '%s' as the default account\n",
388                                             first_account);
389                         g_free (default_account);
390                 }
391                 g_slist_foreach (all_accounts, free_element, NULL);
392                 g_slist_free    (all_accounts);
393         }
394         return retval;
395 }
396
397
398
399 static void
400 init_debug_g_type (void)
401 {
402         GTypeDebugFlags gflags;
403         ModestRuntimeDebugFlags mflags;
404         
405         gflags = 0;
406         mflags = modest_runtime_get_debug_flags ();
407
408         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
409                 gflags |= G_TYPE_DEBUG_OBJECTS;
410         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
411                 gflags |= G_TYPE_DEBUG_SIGNALS;
412
413         g_type_init_with_debug_flags (gflags);
414 }
415
416 static void
417 init_debug_logging (void)
418 {
419         ModestRuntimeDebugFlags mflags;
420         mflags = modest_runtime_get_debug_flags ();
421         
422         if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
423                 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
424                                         G_LOG_LEVEL_CRITICAL |
425                                         G_LOG_LEVEL_WARNING);
426 }
427
428
429 static void
430 init_i18n (void)
431 {
432         /* Setup gettext, to use our .po files: */
433         /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
434         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
435         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
436         textdomain (GETTEXT_PACKAGE);
437 }
438
439
440 /* 
441  *  This function registers our custom toolbar icons, so they can be
442  *  themed. The idea of this function was taken from the gtk-demo
443  */
444 static void
445 init_stock_icons (void)
446 {
447         static gboolean registered = FALSE;
448   
449         if (!registered) {
450                 GtkIconTheme *current_theme;
451                 GdkPixbuf *pixbuf;
452                 GtkIconFactory *factory;
453                 gint i;
454
455                 static GtkStockItem items[] = {
456 #ifdef MODEST_PLATFORM_MAEMO
457                         { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
458                         { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
459                         { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
460 #endif /*MODEST_PLATFORM_MAEMO*/
461                         { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
462                         { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
463 /*                      { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL },  */
464                         { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
465                         { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
466                         { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
467                         { MODEST_STOCK_DELETE, "delete", 0, 0, NULL }, 
468 /*                      { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
469 /*                      { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
470 /*                      { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
471                 };
472       
473                 static gchar *items_names [] = {
474 #ifdef MODEST_PLATFORM_MAEMO
475                         MODEST_TOOLBAR_ICON_SPLIT_VIEW,
476                         MODEST_TOOLBAR_ICON_SORT,
477                         MODEST_TOOLBAR_ICON_REFRESH,
478 #endif /*MODEST_PLATFORM_MAEMO*/
479                         MODEST_TOOLBAR_ICON_MAIL_SEND,
480                         MODEST_TOOLBAR_ICON_NEW_MAIL,
481 /*                      MODEST_TOOLBAR_ICON_SEND_RECEIVE,  */
482                         MODEST_TOOLBAR_ICON_REPLY,      
483                         MODEST_TOOLBAR_ICON_REPLY_ALL,
484                         MODEST_TOOLBAR_ICON_FORWARD,
485                         MODEST_TOOLBAR_ICON_DELETE, 
486 /*                      MODEST_TOOLBAR_ICON_NEXT, */
487 /*                      MODEST_TOOLBAR_ICON_PREV, */
488 /*                      MODEST_TOOLBAR_ICON_STOP */
489                         MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
490                 };
491
492                 registered = TRUE;
493
494                 /* Register our stock items */
495                 gtk_stock_add (items, G_N_ELEMENTS (items));
496       
497                 /* Add our custom icon factory to the list of defaults */
498                 factory = gtk_icon_factory_new ();
499                 gtk_icon_factory_add_default (factory);
500
501                 current_theme = gtk_icon_theme_get_default ();
502
503                 /* Register icons to accompany stock items */
504                 for (i = 0; i < G_N_ELEMENTS (items); i++) {
505
506 #ifdef MODEST_PLATFORM_MAEMO  /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */ 
507                         pixbuf = gtk_icon_theme_load_icon (current_theme,
508                                                            items_names[i],
509                                                            26,
510                                                            GTK_ICON_LOOKUP_NO_SVG,
511                                                            NULL);
512 #else
513                         pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
514 #endif
515
516                         if (pixbuf != NULL) {
517                                 GtkIconSet *icon_set;
518                                 GdkPixbuf *transparent;
519
520                                 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
521                                 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
522                                 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
523                                 gtk_icon_set_unref (icon_set);
524                                 g_object_unref (pixbuf);
525                                 g_object_unref (transparent);
526                         }
527                         else
528                                 g_warning ("failed to load %s icon", items_names[i]);
529                 }
530                 /* Drop our reference to the factory, GTK will hold a reference. */
531                 g_object_unref (factory);
532         }
533 }
534
535
536 static void
537 init_default_settings (ModestConf *conf)
538 {
539         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
540                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
541
542         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL))
543                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
544         
545         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
546                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
547
548         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
549                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
550
551         if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
552                 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
553
554 }
555
556
557 /* set the device name -- note this is an initial guess from /etc/hostname
558  * on maemo-device it will most probably be replaced with the Bluetooth device
559  * name later during starting (see maemo/modest-maemo-utils.[ch])
560  */
561 static void
562 init_device_name (ModestConf *conf)
563 {
564         int len = 255; /* max len */
565         gchar *devname = NULL;
566         
567         if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
568                 g_printerr ("modest: failed to read hostname\n");
569                 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
570                                         MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
571                                         NULL);
572         } else {
573                 /* remove the \n at the end */
574                 if (devname[len-1] == '\n')
575                         devname[len-1] = '\0';
576                 else
577                         devname[len] = '\0';
578
579                 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, NULL);
580         }
581
582         g_free (devname);
583 }