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