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