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