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