1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
32 #include <glib-object.h>
33 #include <glib/gstdio.h>
34 #include <modest-runtime.h>
35 #include <modest-defs.h>
36 #include <modest-singletons.h>
37 #include <widgets/modest-header-view.h>
38 #include <widgets/modest-folder-view.h>
39 #include <modest-tny-platform-factory.h>
40 #include <modest-widget-memory.h>
41 #include <modest-widget-memory-priv.h>
42 #include <modest-local-folder-info.h>
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
46 #if MODEST_PLATFORM_ID==2 /* maemo/hildon */
48 static gboolean hildon_init (void);
49 #endif /* MODEST_PLATFORM_ID==2 */
51 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
52 static gboolean init_local_folders (void);
53 static gboolean init_default_account_maybe (ModestAccountMgr *acc_mgr);
54 static void init_i18n (void);
55 static void debug_g_type_init (void);
56 static void debug_logging_init (void);
58 static ModestSingletons *_singletons = NULL;
61 * defaults for the column headers
65 ModestHeaderViewColumn col;
69 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
70 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
71 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
72 {MODEST_HEADER_VIEW_COLUMN_FROM, 80},
73 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
74 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
75 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
77 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
78 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
79 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
80 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
83 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
84 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
85 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
86 {MODEST_HEADER_VIEW_COLUMN_TO, 80},
87 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
88 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
89 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
91 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
92 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
93 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
94 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
97 static const TnyFolderType LOCAL_FOLDERS[] = {
98 TNY_FOLDER_TYPE_OUTBOX,
99 TNY_FOLDER_TYPE_DRAFTS,
100 TNY_FOLDER_TYPE_SENT,
101 TNY_FOLDER_TYPE_TRASH,
102 TNY_FOLDER_TYPE_ARCHIVE
107 modest_runtime_init (void)
109 ModestSingletons *my_singletons;
113 g_printerr ("modest: modest_runtime_init can only be called once\n");
119 debug_logging_init();
124 my_singletons = modest_singletons_new ();
125 if (!my_singletons) {
126 g_printerr ("modest: failed to initialize singletons\n");
130 #if MODEST_PLATFORM_ID==2
131 if (!hildon_init ()) {
132 modest_runtime_uninit ();
133 g_printerr ("modest: failed to initialize hildon\n");
136 #endif /* MODEST_PLATFORM_ID==2 */
138 /* based on the debug settings, we decide whether to overwrite old settings */
139 reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
140 if (!init_header_columns(modest_singletons_get_conf (my_singletons), reset)) {
141 modest_runtime_uninit ();
142 g_printerr ("modest: failed to init header columns\n");
146 if (!init_local_folders()) {
147 modest_runtime_uninit ();
148 g_printerr ("modest: failed to init local folders\n");
152 if (!init_default_account_maybe(modest_singletons_get_account_mgr (my_singletons))) {
153 modest_runtime_uninit ();
154 g_printerr ("modest: failed to init default account\n");
158 /* don't initialize _singletons before all the other init stuff
159 * is done; thus, using any of the singleton stuff before
160 * runtime is fully init'ed is avoided
162 _singletons = my_singletons;
169 modest_runtime_init_ui (gint argc, gchar** argv)
171 if (!gtk_init_check(&argc, &argv)) {
172 g_printerr ("modest: failed to initialize graphical ui\n");
180 modest_runtime_uninit (void)
183 g_printerr ("modest: modest_runtime is not initialized\n");
187 g_object_unref (G_OBJECT(_singletons));
195 modest_runtime_get_account_mgr (void)
197 g_return_val_if_fail (_singletons, NULL);
198 return modest_singletons_get_account_mgr (_singletons);
201 ModestTnyAccountStore*
202 modest_runtime_get_account_store (void)
204 g_return_val_if_fail (_singletons, NULL);
205 return modest_singletons_get_account_store (_singletons);
210 modest_runtime_get_conf (void)
212 g_return_val_if_fail (_singletons, NULL);
213 return modest_singletons_get_conf (_singletons);
218 modest_runtime_get_cache_mgr (void)
220 g_return_val_if_fail (_singletons, NULL);
221 return modest_singletons_get_cache_mgr (_singletons);
225 ModestMailOperationQueue*
226 modest_runtime_get_mail_operation_queue (void)
228 g_return_val_if_fail (_singletons, NULL);
229 return modest_singletons_get_mail_operation_queue (_singletons);
234 modest_runtime_get_widget_factory (void)
236 g_return_val_if_fail (_singletons, NULL);
237 return modest_singletons_get_widget_factory (_singletons);
242 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
243 ModestRuntimeDebugFlags
244 modest_runtime_get_debug_flags ()
246 static const GDebugKey debug_keys[] = {
247 { "abort-on-warning", MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
248 { "log-actions", MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
249 { "debug-objects", MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
250 { "debug-signals", MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
251 { "factory-settings", MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS }
254 static ModestRuntimeDebugFlags debug_flags = -1;
256 if (debug_flags != -1)
259 str = g_getenv (MODEST_DEBUG);
262 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
273 /* NOTE: the exact details of this format are important, as they
274 * are also used in modest-widget-memory. FIXME: make a shared function
275 * for this with widget-memory
278 save_header_settings (ModestConf *conf, TnyFolderType type,
279 ModestHeaderViewStyle style, const FolderCols* cols,
280 guint col_num, gboolean overwrite)
286 g_return_val_if_fail (cols, FALSE);
288 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
290 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
291 /* if we're not in overwrite mode, only write stuff it
292 * there was nothing before */
293 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
298 /* the format is necessarily the same as the one in modest-widget-memory */
299 str = g_string_new (NULL);
300 for (i = 0; i != col_num; ++i)
301 g_string_append_printf (str, "%d:%d ",
302 cols[i].col, cols[i].width);
304 modest_conf_set_string (conf, key, str->str, NULL);
306 g_string_free (str, TRUE);
312 * modest_init_header_columns:
313 * @overwrite: write the setting, even if it already exists
315 * will set defaults for the columns to show for folder,
316 * if there are no such settings yet (in ModestWidgetMemory)
318 * Returns: TRUE if succeeded, FALSE in case of error
321 init_header_columns (ModestConf *conf, gboolean overwrite)
325 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
326 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
328 switch (folder_type) {
329 case TNY_FOLDER_TYPE_OUTBOX:
330 case TNY_FOLDER_TYPE_SENT:
331 case TNY_FOLDER_TYPE_DRAFTS:
332 save_header_settings (conf, folder_type,
333 MODEST_HEADER_VIEW_STYLE_DETAILS,
334 OUTBOX_COLUMNS_DETAILS,
335 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
337 save_header_settings (conf, folder_type,
338 MODEST_HEADER_VIEW_STYLE_TWOLINES,
339 OUTBOX_COLUMNS_TWOLINES,
340 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
345 save_header_settings (conf, folder_type,
346 MODEST_HEADER_VIEW_STYLE_DETAILS,
347 INBOX_COLUMNS_DETAILS,
348 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
350 save_header_settings (conf, folder_type,
351 MODEST_HEADER_VIEW_STYLE_TWOLINES,
352 INBOX_COLUMNS_TWOLINES,
353 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
361 * init_local_folders:
363 * create the Local Folders folder under cache, if they
366 * Returns: TRUE if the folder were already there, or
367 * they were created, FALSE otherwise
370 init_local_folders (void)
374 static const gchar* maildirs[] = {
378 maildir_path = modest_local_folder_info_get_maildir_path ();
380 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
382 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
384 dir = g_build_filename (maildir_path,
385 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
388 if (g_mkdir_with_parents (dir, 0755) < 0) {
389 g_printerr ("modest: failed to create %s\n", dir);
391 g_free (maildir_path);
398 g_free (maildir_path);
405 free_element (gpointer data, gpointer user_data)
413 * init_default_account_maybe:
415 * if there are accounts defined, but there is no default account,
416 * it will be defined.
418 * Returns: TRUE if there was a default account already,
419 * or one has been created or there are no accounts yet,
420 * returns FALSE in case of error
423 init_default_account_maybe (ModestAccountMgr *acc_mgr)
425 GSList *all_accounts = NULL;
426 gchar *default_account;
427 gboolean retval = TRUE;
429 all_accounts = modest_account_mgr_account_names (acc_mgr, NULL);
430 if (all_accounts) { /* if there are any accounts, there should be a default one */
432 modest_account_mgr_get_default_account (acc_mgr);
433 if (!default_account) {
434 gchar *first_account;
435 g_printerr ("modest: no default account defined\n");
436 first_account = (gchar*)all_accounts->data;
437 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
438 g_printerr ("modest: set '%s' as the default account\n",
441 g_printerr ("modest: failed to set '%s' as the default account\n",
443 g_free (default_account);
445 g_slist_foreach (all_accounts, free_element, NULL);
446 g_slist_free (all_accounts);
454 debug_g_type_init (void)
456 GTypeDebugFlags gflags;
457 ModestRuntimeDebugFlags mflags;
460 mflags = modest_runtime_get_debug_flags ();
462 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
463 gflags |= G_TYPE_DEBUG_OBJECTS;
464 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
465 gflags |= G_TYPE_DEBUG_SIGNALS;
467 g_type_init_with_debug_flags (gflags);
471 debug_logging_init (void)
473 ModestRuntimeDebugFlags mflags;
474 mflags = modest_runtime_get_debug_flags ();
476 if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
477 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
478 G_LOG_LEVEL_CRITICAL |
479 G_LOG_LEVEL_WARNING);
486 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
487 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
488 textdomain (GETTEXT_PACKAGE);
493 #if MODEST_PLATFORM_ID==2
497 osso_context_t *osso_context =
498 osso_initialize(PACKAGE, PACKAGE_VERSION,
501 g_printerr ("modest: failed to acquire osso context\n");
505 #endif /* MODEST_PLATFORM_ID==2 */