2012-07-07 [colin] 3.8.1cvs7
[claws.git] / src / common / claws.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012  the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26 #include <stdlib.h>
27 #include <glib.h>
28 #ifdef ENABLE_NLS
29 #include <glib/gi18n.h>
30 #else
31 #define _(a) (a)
32 #define N_(a) (a)
33 #endif
34
35 #if HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38
39 #include "claws.h"
40 #include "utils.h"
41 #include "ssl.h"
42 #include "version.h"
43
44 static gboolean claws_initialized = FALSE;
45 static gchar *startup_dir;
46 static void (*claws_idle_function)(void) = NULL;
47
48 /**
49  * Parse program parameters and remove all parameters
50  * that have been processed. Arguments are pointers to
51  * original passed programm arguments and these will
52  * be modified leaving only unknown parameters for
53  * further processing
54  *
55  * \param argc pointer to number of parameters
56  * \param argv pointer to array of parameter strings
57  */
58 static void parse_parameter(int *argc, char ***argv)
59 {
60         gint i, j, k;
61
62         cm_return_if_fail(argc != NULL);
63         cm_return_if_fail(argv != NULL);
64
65         for (i = 1; i < *argc;) {
66                 if (strcmp("--debug", (*argv)[i]) == 0) {
67                         debug_set_mode(TRUE);
68
69                         (*argv)[i] = NULL;
70                 }
71
72                 i += 1;
73         }
74
75         /* Remove NULL args from argv[] for further processing */
76         for (i = 1; i < *argc; i++) {
77                 for (k = i; k < *argc; k++)
78                         if ((*argv)[k] != NULL)
79                                 break;
80
81                 if (k > i) {
82                         k -= i;
83                         for (j = i + k; j < *argc; j++)
84                                 (*argv)[j - k] = (*argv)[j];
85                         *argc -= k;
86                 }
87         }
88 }
89
90 gboolean claws_init(int *argc, char ***argv)
91 {
92         if (claws_initialized)
93                 return TRUE;
94
95 #ifdef USE_GNUTLS
96         ssl_init();
97 #endif
98         startup_dir = g_get_current_dir();
99
100         parse_parameter(argc, argv);
101
102         debug_print("Starting Claws Mail version %s\n", PROG_VERSION);
103         
104         setlocale(LC_ALL, "");
105 #ifdef ENABLE_NLS
106         bindtextdomain(PACKAGE, get_locale_dir () );
107         bind_textdomain_codeset (PACKAGE, "UTF-8");
108         textdomain(PACKAGE);
109 #endif /*ENABLE_NLS*/
110         putenv("G_BROKEN_FILENAMES=1");
111
112         /* backup if old rc file exists */
113         if (is_file_exist(RC_DIR)) {
114                 if (g_rename(RC_DIR, RC_DIR ".bak") < 0) {
115                         FILE_OP_ERROR(RC_DIR, "rename");
116                         return FALSE;
117                 }
118         }
119
120         srand((gint) time(NULL));
121
122         claws_initialized = TRUE;
123
124         return TRUE;
125 }
126
127 void claws_done(void)
128 {
129
130 #ifdef USE_GNUTLS
131         ssl_done();
132 #endif
133 }
134
135 const gchar *claws_get_startup_dir(void)
136 {
137         return startup_dir;
138 }
139
140 guint claws_get_version(void)
141 {
142         return VERSION_NUMERIC;
143 }
144
145 void claws_register_idle_function       (void (*idle_func)(void))
146 {
147         claws_idle_function = idle_func;
148 }
149
150 void claws_do_idle(void)
151 {
152         if (claws_idle_function != NULL)
153                 claws_idle_function();
154 }