* src/modest-defs.h:
[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 #include "widgets/modest-global-settings-dialog.h"
50 #include "modest-tny-msg.h"
51 #include <libgnomevfs/gnome-vfs.h>
52 #ifdef MODEST_PLATFORM_MAEMO
53 #include "modest-hildon-includes.h"
54 #endif
55 #include <locale.h>
56
57 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
58 static gboolean init_default_account_maybe  (ModestAccountMgr *acc_mgr);
59 static void     init_i18n (void);
60 static void     init_stock_icons (void);
61 static void     init_debug_g_type (void);
62 static void     init_debug_logging (void);
63 static void     init_default_settings (ModestConf *conf);
64 static void     init_device_name (ModestConf *conf);
65 static gboolean init_ui (gint argc, gchar** argv);
66
67
68 static gboolean _is_initialized = FALSE;
69
70 /*
71  * defaults for the column headers
72  */
73 typedef struct {
74         ModestHeaderViewColumn col;
75         guint                  width;
76         gint                  sort;
77 } FolderCols;
78
79
80 static const guint MODEST_MAIN_PANED_POS_PERCENTAGE = 30;
81
82 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
83         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
84         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
85         {MODEST_HEADER_VIEW_COLUMN_FROM,    80, 0},
86         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
87         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
88         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
89 };
90
91 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
92         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
93 };
94
95 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
96         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
97         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
98         {MODEST_HEADER_VIEW_COLUMN_TO,    80, 0},
99         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
100         {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
101         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
102 };
103
104 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
105         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0}
106 //      {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
107 };
108
109 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
110         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
111 };
112
113 #ifdef MODEST_PLATFORM_MAEMO
114 static const TnyFolderType LOCAL_FOLDERS[] = {
115 /*      TNY_FOLDER_TYPE_OUTBOX, */
116         TNY_FOLDER_TYPE_DRAFTS,
117         TNY_FOLDER_TYPE_SENT
118 };
119 #else
120 static const TnyFolderType LOCAL_FOLDERS[] = {
121 /*      TNY_FOLDER_TYPE_OUTBOX, */
122         TNY_FOLDER_TYPE_DRAFTS,
123         TNY_FOLDER_TYPE_SENT,
124         TNY_FOLDER_TYPE_TRASH,
125         TNY_FOLDER_TYPE_ARCHIVE 
126 };
127 #endif /* MODEST_PLATFORM_MAEMO */
128
129 static GList*
130 new_cold_ids_gslist_from_array( const FolderCols* cols, guint col_num)
131 {
132         GList *result = NULL;
133         
134         guint i = 0;
135         for (i = 0; i < col_num; ++i) {
136                 result = g_list_append (result, GINT_TO_POINTER (cols[i].col));
137         }
138         
139         return result;
140 }
141
142 GList* 
143 modest_init_get_default_header_view_column_ids (TnyFolderType folder_type, ModestHeaderViewStyle style)
144 {
145                 GList *result = NULL;
146                 
147                 switch (folder_type) {
148                 case TNY_FOLDER_TYPE_SENT:
149                 case TNY_FOLDER_TYPE_DRAFTS:
150                         if (style == MODEST_HEADER_VIEW_STYLE_DETAILS)
151                                 result = new_cold_ids_gslist_from_array (OUTBOX_COLUMNS_DETAILS,
152                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS));
153                         else if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
154                                 result = new_cold_ids_gslist_from_array (SENT_COLUMNS_TWOLINES,
155                                       G_N_ELEMENTS(SENT_COLUMNS_TWOLINES));
156                 break;
157                 case TNY_FOLDER_TYPE_OUTBOX:
158                         if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
159                                 result = new_cold_ids_gslist_from_array (OUTBOX_COLUMNS_TWOLINES,
160                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES));
161                 break;
162
163                 default:
164                         if (style == MODEST_HEADER_VIEW_STYLE_DETAILS)
165                                 result =  new_cold_ids_gslist_from_array (INBOX_COLUMNS_DETAILS,
166                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS));
167                         else if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
168                                 result = new_cold_ids_gslist_from_array (INBOX_COLUMNS_TWOLINES,
169                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES));
170                 };
171                 
172                 if (!result) {
173                         g_warning("DEBUG: %s: No default columns IDs found for "
174                                 "folder_type=%d, style=%d\n", __FUNCTION__, folder_type, style);        
175                 }
176                 
177                 return result;
178 }
179
180
181 gboolean
182 modest_init (int argc, char *argv[])
183 {
184         gboolean reset;
185
186         if (_is_initialized) {
187                 g_printerr ("modest: modest_init_init_core may only be invoked once\n");
188                 return FALSE;
189         } 
190         
191         init_i18n();
192         init_debug_g_type();
193         init_debug_logging();
194
195         /* initialize the prng, we need it when creating random files */
196         srandom((int)getpid());
197         
198         if (!gnome_vfs_initialized()) {
199                 if (!gnome_vfs_init ()) {
200                         g_printerr ("modest: failed to init gnome-vfs\n");
201                         return FALSE;
202                 }
203         }
204         
205         if (!modest_runtime_init()) {
206                 modest_init_uninit ();
207                 g_printerr ("modest: failed to initialize the modest runtime\n");
208                 return FALSE;
209         }
210         
211         /* do an initial guess for the device name */
212         init_device_name (modest_runtime_get_conf());
213         
214         if (!modest_platform_init(argc, argv)) {
215                 modest_init_uninit ();
216                 g_printerr ("modest: failed to run platform-specific initialization\n");
217                 return FALSE;
218         }
219
220         reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
221         if (!init_header_columns(modest_runtime_get_conf(), reset)) {
222                 modest_init_uninit ();
223                 g_printerr ("modest: failed to init header columns\n");
224                 return FALSE;
225         }
226
227         init_default_settings (modest_runtime_get_conf ());
228         
229         if (!modest_init_local_folders(NULL)) {
230                 modest_init_uninit ();
231                 g_printerr ("modest: failed to init local folders\n");
232                 return FALSE;
233         }
234         
235         if (!init_default_account_maybe (modest_runtime_get_account_mgr ())) {
236                 modest_init_uninit ();
237                 g_printerr ("modest: failed to init default account\n");
238                 return FALSE;
239         }       
240         
241         if (!init_ui (argc, argv)) {
242                 modest_init_uninit ();
243                 g_printerr ("modest: failed to init ui\n");
244                 return FALSE;
245         }
246
247         return _is_initialized = TRUE;
248 }
249
250
251 static gboolean
252 init_ui (gint argc, gchar** argv)
253 {
254         /* Set application name */
255         g_set_application_name (modest_platform_get_app_name());
256         /* g_message (modest_platform_get_app_name()); */
257
258         /* Init stock icons */
259         init_stock_icons ();
260
261                 /* Init notification system */
262 #ifdef MODEST_HAVE_HILDON_NOTIFY
263         notify_init ("Basics");
264 #endif
265         return TRUE;
266 }
267
268
269 gboolean
270 modest_init_uninit (void)
271 {
272         if (!_is_initialized)
273                 return TRUE; 
274         
275         if (gnome_vfs_initialized()) /* apparently, this returns TRUE, even after a shutdown */
276                 gnome_vfs_shutdown ();
277                 
278         if (!modest_runtime_uninit())
279                 g_printerr ("modest: failed to uninit runtime\n");
280
281         if (!modest_platform_uninit())
282                 g_printerr ("modest: failed to uninit platform\n");
283         
284         _is_initialized = FALSE;
285         return TRUE;
286 }
287
288
289
290
291 /* NOTE: the exact details of this format are important, as they
292  * are also used in modest-widget-memory. FIXME: make a shared function
293  * for this with widget-memory
294  */
295 static gboolean
296 save_header_settings (ModestConf *conf, TnyFolderType type,
297                       ModestHeaderViewStyle style,  const FolderCols* cols,
298                       guint col_num, gboolean overwrite)
299 {
300         int i;
301         gchar *key;
302         gchar *sort_key;
303         gchar *sort_value;
304         GString *str;
305
306         g_return_val_if_fail (cols, FALSE);
307
308         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
309                                                                   type, style,
310                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
311         sort_key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
312                                                                        type, style,
313                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
314         /* if we're not in overwrite mode, only write stuff it
315          * there was nothing before */
316         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
317                 g_free (key);
318                 g_free (sort_key);
319                 return TRUE;
320         }
321
322         /* the format is necessarily the same as the one in modest-widget-memory */
323         str = g_string_new (NULL);
324         for (i = 0; i != col_num; ++i) 
325                 g_string_append_printf (str, "%d:%d:%d ",
326                                         cols[i].col, cols[i].width, cols[i].sort); 
327
328         modest_conf_set_string (conf, key, str->str, NULL);
329         g_free (key);
330         g_string_free (str, TRUE);
331
332         if ( col_num > 0 ) {
333                 gint sort_col_id;
334                 if (cols[0].col == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT)
335                         sort_col_id = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
336                 else
337                         sort_col_id = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
338                 sort_value = g_strdup_printf("%d:%d:%d", sort_col_id, GTK_SORT_DESCENDING, 0);
339                 modest_conf_set_string (conf, sort_key, sort_value, NULL);
340                 g_free (sort_value);
341         }
342         g_free (sort_key);
343         
344         return TRUE;
345 }
346
347 /**
348  * modest_init_header_columns:
349  * @overwrite: write the setting, even if it already exists
350  * 
351  * will set defaults for the columns to show for folder,
352  * if there are no such settings yet (in ModestWidgetMemory)
353  * 
354  * Returns: TRUE if succeeded, FALSE in case of error
355  */
356 static gboolean
357 init_header_columns (ModestConf *conf, gboolean overwrite)
358 {
359         int folder_type;
360         gchar *key;
361         
362         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
363              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
364                 
365                 switch (folder_type) {
366                 case TNY_FOLDER_TYPE_SENT:
367                 case TNY_FOLDER_TYPE_DRAFTS:
368                 save_header_settings (conf, folder_type,
369                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
370                                       OUTBOX_COLUMNS_DETAILS,
371                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
372                                       overwrite);
373                 save_header_settings (conf, folder_type,
374                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
375                                       SENT_COLUMNS_TWOLINES,
376                                       G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
377                                       overwrite);
378                 break;
379                 case TNY_FOLDER_TYPE_OUTBOX:
380                 save_header_settings (conf, folder_type,
381                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
382                                       OUTBOX_COLUMNS_TWOLINES,
383                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
384                                       overwrite);
385                 break;
386
387                 default:
388                 save_header_settings (conf, folder_type,
389                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
390                                       INBOX_COLUMNS_DETAILS,
391                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
392                                       overwrite);
393                 save_header_settings (conf, folder_type,
394                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
395                                       INBOX_COLUMNS_TWOLINES,
396                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
397                                       overwrite);
398                 };
399         }
400         
401         key = _modest_widget_memory_get_keyname (MODEST_CONF_MAIN_PANED_KEY, 
402                                                  MODEST_WIDGET_MEMORY_PARAM_POS);
403         /* if we're not in overwrite mode, only write stuff it
404          * there was nothing before */
405         if (overwrite || !modest_conf_key_exists(conf, key, NULL)) 
406                 modest_conf_set_int (conf, key, MODEST_MAIN_PANED_POS_PERCENTAGE, NULL);
407         
408         g_free (key);
409         return TRUE;
410 }
411
412 gboolean modest_init_one_local_folder (gchar *maildir_path)
413 {
414         static const gchar* maildirs[] = {
415                 "cur", "new", "tmp"
416         };
417         
418         int j;
419         for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
420                 gchar *dir = g_build_filename (maildir_path,
421                                         maildirs[j],
422                                         NULL);
423                 if (g_mkdir_with_parents (dir, 0755) < 0) {
424                         g_printerr ("modest: %s: failed to create %s\n", __FUNCTION__, dir);
425                         g_free (dir);
426                         return FALSE;
427                 }
428                 
429                 g_free (dir);
430         }
431
432         return TRUE;
433 }
434
435 /**
436  * modest_init_local_folders:
437  * 
438  * create the Local Folders folder under cache, if they
439  * do not exist yet.
440  * 
441  * Returns: TRUE if the folder were already there, or
442  * they were created, FALSE otherwise
443  */
444 gboolean
445 modest_init_local_folders (const gchar* location_filepath)
446 {       
447         gboolean retval = TRUE;
448
449         gchar *maildir_path = modest_local_folder_info_get_maildir_path (location_filepath);
450
451         if (location_filepath) {
452                 /* For instance, for memory card, just create the top-level .modest folder
453                  * and one "archive" folder (so that messages can be put somewhere):
454                  */
455
456                 gchar *dir = g_build_filename (maildir_path,
457                                                modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_ARCHIVE),
458                                                NULL);
459                 const gboolean created = modest_init_one_local_folder (dir);
460                 g_free(dir);
461                         
462                 if (!created) {
463                         retval = FALSE;
464                 }
465
466                 #if 0
467                 /* Do this if we only create the top-level dir: */
468                 if (g_mkdir_with_parents (maildir_path, 0755) < 0) {
469                         g_printerr ("modest: %s: failed to create %s\n", __FUNCTION__, location_filepath);
470                         retval = FALSE;
471                 }
472                 #endif
473         }
474         else {
475                 /* Create each of the standard on-disk folders.
476                  * Per-account outbox folders will be created when first needed. */
477                 int i;
478                 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
479                         gchar *dir = g_build_filename (maildir_path,
480                                                         modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
481                                                         NULL);                  
482                         const gboolean created = modest_init_one_local_folder (dir);
483                         g_free(dir);
484                         
485                         if (!created) {
486                                 retval = FALSE;
487                         }
488                 }
489         }
490         
491         g_free (maildir_path);
492         return retval;
493 }
494
495 /**
496  * init_default_account_maybe:
497  *
498  * if there are accounts defined, but there is no default account,
499  * it will be defined.
500  * 
501  * Returns: TRUE if there was a default account already,
502  *  or one has been created or there are no accounts yet,
503  *  returns FALSE in case of error
504  */
505 static gboolean
506 init_default_account_maybe  (ModestAccountMgr *acc_mgr)
507 {
508         gchar *default_account;
509         gboolean retval = TRUE;
510
511         default_account =  modest_account_mgr_get_default_account (acc_mgr);
512         if (!default_account)
513                 retval = modest_account_mgr_set_first_account_as_default (acc_mgr);
514         g_free (default_account);
515
516         return retval;
517 }
518
519
520
521 static void
522 init_debug_g_type (void)
523 {
524         GTypeDebugFlags gflags;
525         ModestRuntimeDebugFlags mflags;
526         
527         gflags = 0;
528         mflags = modest_runtime_get_debug_flags ();
529
530         if (mflags & MODEST_RUNTIME_DEBUG_OBJECTS)
531                 gflags |= G_TYPE_DEBUG_OBJECTS;
532         if (mflags & MODEST_RUNTIME_DEBUG_SIGNALS)
533                 gflags |= G_TYPE_DEBUG_SIGNALS;
534
535         g_type_init_with_debug_flags (gflags);
536 }
537
538 static void
539 init_debug_logging (void)
540 {
541         ModestRuntimeDebugFlags mflags;
542         mflags = modest_runtime_get_debug_flags ();
543         
544         if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
545                 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
546                                         G_LOG_LEVEL_CRITICAL |
547                                         G_LOG_LEVEL_WARNING);
548 }
549
550
551 static void
552 init_i18n (void)
553 {
554        /* little trick make en_GB the fallback language, instead
555         * of the logical IDs
556         * we need the ugly ifdefs, because modest_platform_init is
557         * too late.
558         */
559         const gchar *lc_messages = getenv ("LC_MESSAGES");
560
561         if (!lc_messages) {
562                 setenv ("LANGUAGE", "en_GB", 1);
563                 setenv ("LC_MESSAGES", "en_GB", 1);
564         } else {
565                 gchar *language = g_strdup_printf ("%s:en_GB", lc_messages);
566                setenv ("LANGUAGE", language, 1);
567                g_free (language);
568         }
569         /* end of little trick */
570
571         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
572         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
573         textdomain (GETTEXT_PACKAGE);
574 }
575
576
577 /* 
578  *  This function registers our custom toolbar icons, so they can be
579  *  themed. The idea of this function was taken from the gtk-demo
580  */
581 static void
582 init_stock_icons (void)
583 {
584         static gboolean registered = FALSE;
585   
586         if (!registered) {
587                 GtkIconTheme *current_theme;
588                 GdkPixbuf *pixbuf;
589                 GtkIconFactory *factory;
590                 gint i;
591
592                 static GtkStockItem items[] = {
593 #ifdef MODEST_PLATFORM_MAEMO
594                         { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
595                         { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
596                         { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
597 #endif /*MODEST_PLATFORM_MAEMO*/
598                         { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
599                         { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
600 /*                      { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL },  */
601                         { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
602                         { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
603                         { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
604                         { MODEST_STOCK_DELETE, "delete", 0, 0, NULL }, 
605 /*                      { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
606 /*                      { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
607 /*                      { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
608                 };
609       
610                 static gchar *items_names [] = {
611 #ifdef MODEST_PLATFORM_MAEMO
612                         MODEST_TOOLBAR_ICON_SPLIT_VIEW,
613                         MODEST_TOOLBAR_ICON_SORT,
614                         MODEST_TOOLBAR_ICON_REFRESH,
615 #endif /*MODEST_PLATFORM_MAEMO*/
616                         MODEST_TOOLBAR_ICON_MAIL_SEND,
617                         MODEST_TOOLBAR_ICON_NEW_MAIL,
618 /*                      MODEST_TOOLBAR_ICON_SEND_RECEIVE,  */
619                         MODEST_TOOLBAR_ICON_REPLY,      
620                         MODEST_TOOLBAR_ICON_REPLY_ALL,
621                         MODEST_TOOLBAR_ICON_FORWARD,
622                         MODEST_TOOLBAR_ICON_DELETE, 
623 /*                      MODEST_TOOLBAR_ICON_NEXT, */
624 /*                      MODEST_TOOLBAR_ICON_PREV, */
625 /*                      MODEST_TOOLBAR_ICON_STOP */
626 /*                      MODEST_TOOLBAR_ICON_FORMAT_BULLETS, */
627                 };
628
629                 registered = TRUE;
630
631                 /* Register our stock items */
632                 gtk_stock_add (items, G_N_ELEMENTS (items));
633       
634                 /* Add our custom icon factory to the list of defaults */
635                 factory = gtk_icon_factory_new ();
636                 gtk_icon_factory_add_default (factory);
637
638                 current_theme = gtk_icon_theme_get_default ();
639
640                 /* Register icons to accompany stock items */
641                 for (i = 0; i < G_N_ELEMENTS (items); i++) {
642
643 #ifdef MODEST_PLATFORM_MAEMO  /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */ 
644                         pixbuf = gtk_icon_theme_load_icon (current_theme,
645                                                            items_names[i],
646                                                            26,
647                                                            GTK_ICON_LOOKUP_NO_SVG,
648                                                            NULL);
649 #else
650                         pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
651 #endif
652
653                         if (pixbuf != NULL) {
654                                 GtkIconSet *icon_set;
655                                 GdkPixbuf *transparent;
656
657                                 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
658                                 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
659                                 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
660                                 gtk_icon_set_unref (icon_set);
661                                 g_object_unref (pixbuf);
662                                 g_object_unref (transparent);
663                         }
664                         else
665                                 g_warning ("Modest: %s: failed to load %s icon", __FUNCTION__, items_names[i]);
666                 }
667                 /* Drop our reference to the factory, GTK will hold a reference. */
668                 g_object_unref (factory);
669         }
670 }
671
672
673 static void
674 init_default_settings (ModestConf *conf)
675 {
676         /* Show toolbar keys */
677         if (!modest_conf_key_exists (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR, NULL))
678                 modest_conf_set_bool (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
679
680         if (!modest_conf_key_exists (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
681                 modest_conf_set_bool (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
682
683         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR, NULL))
684                 modest_conf_set_bool (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
685
686         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
687                 modest_conf_set_bool (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
688
689         if (!modest_conf_key_exists (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR, NULL))
690                 modest_conf_set_bool (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
691         
692         if (!modest_conf_key_exists (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
693                 modest_conf_set_bool (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
694
695         /* Editor keys */
696         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
697                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
698
699         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
700                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
701
702         /* Global settings */
703         if (!modest_conf_key_exists (conf, MODEST_CONF_AUTO_UPDATE, NULL))
704                 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, TRUE, NULL);
705
706         if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL))
707                 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, MODEST_CONNECTED_VIA_WLAN, NULL);
708
709         if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_INTERVAL, NULL))
710                 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, MODEST_UPDATE_INTERVAL_15_MIN, NULL);
711
712         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_SIZE_LIMIT, NULL))
713                 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, 1000, NULL);
714
715         if (!modest_conf_key_exists (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, NULL))
716                 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, FALSE, NULL);
717
718         if (!modest_conf_key_exists (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
719                 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, TRUE, NULL);
720
721         if (!modest_conf_key_exists (conf, MODEST_CONF_REPLY_TYPE, NULL))
722                 modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, MODEST_TNY_MSG_REPLY_TYPE_QUOTE, NULL);
723
724         if (!modest_conf_key_exists (conf, MODEST_CONF_FETCH_HTML_EXTERNAL_IMAGES, NULL))
725                 modest_conf_set_bool (conf, MODEST_CONF_FETCH_HTML_EXTERNAL_IMAGES, FALSE, NULL);
726 }
727
728
729 /* set the device name -- note this is an initial guess from /etc/hostname
730  * on maemo-device it will most probably be replaced with the Bluetooth device
731  * name later during starting (see maemo/modest-maemo-utils.[ch])
732  */
733 static void
734 init_device_name (ModestConf *conf)
735 {
736         gsize len = 255; /* max len */
737         gchar *devname = NULL;
738         
739         if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
740                 g_printerr ("modest: failed to read hostname\n");
741                 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
742                                         MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
743                                         NULL);
744         } else {
745                 /* remove the \n at the end */
746                 if (devname[len-1] == '\n')
747                         devname[len-1] = '\0';
748                 else
749                         devname[len] = '\0';
750
751                 GError *err = NULL;
752                 if (!modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, &err)) {
753                         g_printerr ("modest: error setting device name '%s': %s",
754                                     devname, err ? err->message: "?");
755                         g_error_free (err);
756                 }
757         }
758         
759         g_free (devname);
760 }