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