a71756bed4bab25cf1e699b953291063cfcf0e9f
[claws.git] / src / common / sylpheed.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "sylpheed.h"
39 #include "utils.h"
40 #include "ssl.h"
41 #include "version.h"
42 #include "plugin.h"
43
44 static gboolean sylpheed_initialized = FALSE;
45 static gchar *startup_dir;
46 static void (*sylpheed_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         g_return_if_fail(argc != NULL);
63         g_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 sylpheed_init(int *argc, char ***argv)
91 {
92         if (sylpheed_initialized)
93                 return TRUE;
94
95         startup_dir = g_get_current_dir();
96
97         parse_parameter(argc, argv);
98
99         debug_print("Starting sylpheed version %08x\n", VERSION_NUMERIC);
100
101         setlocale(LC_ALL, "");
102 #ifdef ENABLE_NLS
103         bindtextdomain(PACKAGE, LOCALEDIR);
104         bind_textdomain_codeset (PACKAGE, "UTF-8");
105         textdomain(PACKAGE);
106 #endif /*ENABLE_NLS*/
107         putenv("G_BROKEN_FILENAMES=1");
108
109         /* backup if old rc file exists */
110         if (is_file_exist(RC_DIR)) {
111                 if (rename(RC_DIR, RC_DIR ".bak") < 0) {
112                         FILE_OP_ERROR(RC_DIR, "rename");
113                         return FALSE;
114                 }
115         }
116
117         srand((gint) time(NULL));
118
119 #if USE_OPENSSL
120         ssl_init();
121 #endif
122
123         plugin_load_all("Common");
124
125         sylpheed_initialized = TRUE;
126
127         return TRUE;
128 }
129
130 void sylpheed_done(void)
131 {
132         plugin_unload_all("Common");
133
134 #if USE_OPENSSL
135         ssl_done();
136 #endif
137 }
138
139 const gchar *sylpheed_get_startup_dir(void)
140 {
141         return startup_dir;
142 }
143
144 guint sylpheed_get_version(void)
145 {
146         return VERSION_NUMERIC;
147 }
148
149 void sylpheed_register_idle_function    (void (*idle_func)(void))
150 {
151         sylpheed_idle_function = idle_func;
152 }
153
154 void sylpheed_do_idle(void)
155 {
156         if (sylpheed_idle_function != NULL)
157                 sylpheed_idle_function();
158 }