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