281e65a0169be3c6fde449735fdf9da8d8be849f
[claws.git] / src / common / claws.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto and 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 #endif
23
24 #include "defs.h"
25 #include <stdlib.h>
26 #include <glib.h>
27 #ifdef ENABLE_NLS
28 #include <glib/gi18n.h>
29 #else
30 #define _(a) (a)
31 #define N_(a) (a)
32 #endif
33
34 #if HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37
38 #include "claws.h"
39 #include "utils.h"
40 #include "ssl.h"
41 #include "version.h"
42
43 static gboolean claws_initialized = FALSE;
44 static gchar *startup_dir;
45 static void (*claws_idle_function)(void) = NULL;
46
47 /**
48  * Parse program parameters and remove all parameters
49  * that have been processed. Arguments are pointers to
50  * original passed programm arguments and these will
51  * be modified leaving only unknown parameters for
52  * further processing
53  *
54  * \param argc pointer to number of parameters
55  * \param argv pointer to array of parameter strings
56  */
57 static void parse_parameter(int *argc, char ***argv)
58 {
59         gint i, j, k;
60
61         g_return_if_fail(argc != NULL);
62         g_return_if_fail(argv != NULL);
63
64         for (i = 1; i < *argc;) {
65                 if (strcmp("--debug", (*argv)[i]) == 0) {
66                         debug_set_mode(TRUE);
67
68                         (*argv)[i] = NULL;
69                 }
70
71                 i += 1;
72         }
73
74         /* Remove NULL args from argv[] for further processing */
75         for (i = 1; i < *argc; i++) {
76                 for (k = i; k < *argc; k++)
77                         if ((*argv)[k] != NULL)
78                                 break;
79
80                 if (k > i) {
81                         k -= i;
82                         for (j = i + k; j < *argc; j++)
83                                 (*argv)[j - k] = (*argv)[j];
84                         *argc -= k;
85                 }
86         }
87 }
88
89 gboolean claws_init(int *argc, char ***argv)
90 {
91         if (claws_initialized)
92                 return TRUE;
93
94         startup_dir = g_get_current_dir();
95
96         parse_parameter(argc, argv);
97
98         debug_print("Starting Claws Mail version %s\n", PROG_VERSION);
99         
100         setlocale(LC_ALL, "");
101 #ifdef ENABLE_NLS
102         bindtextdomain(PACKAGE, get_locale_dir () );
103         bind_textdomain_codeset (PACKAGE, "UTF-8");
104         textdomain(PACKAGE);
105 #endif /*ENABLE_NLS*/
106         putenv("G_BROKEN_FILENAMES=1");
107
108         /* backup if old rc file exists */
109         if (is_file_exist(RC_DIR)) {
110                 if (rename(RC_DIR, RC_DIR ".bak") < 0) {
111                         FILE_OP_ERROR(RC_DIR, "rename");
112                         return FALSE;
113                 }
114         }
115
116         srand((gint) time(NULL));
117
118 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
119         ssl_init();
120 #endif
121
122         claws_initialized = TRUE;
123
124         return TRUE;
125 }
126
127 void claws_done(void)
128 {
129
130 #if (defined(USE_OPENSSL) || defined (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 }