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