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