2004-11-26 [colin] 0.9.12cvs173.1
[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
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
28 #if HAVE_LOCALE_H
29 #  include <locale.h>
30 #endif
31
32 #include "sylpheed.h"
33 #include "intl.h"
34 #include "utils.h"
35 #include "ssl.h"
36 #include "version.h"
37 #include "plugin.h"
38
39 static gboolean sylpheed_initialized = FALSE;
40 static gchar *startup_dir;
41 static void (*sylpheed_idle_function)(void) = NULL;
42
43 /**
44  * Parse program parameters and remove all parameters
45  * that have been processed. Arguments are pointers to
46  * original passed programm arguments and these will
47  * be modified leaving only unknown parameters for
48  * further processing
49  *
50  * \param argc pointer to number of parameters
51  * \param argv pointer to array of parameter strings
52  */
53 static void parse_parameter(int *argc, char ***argv)
54 {
55         gint i, j, k;
56
57         g_return_if_fail(argc != NULL);
58         g_return_if_fail(argv != NULL);
59
60         for (i = 1; i < *argc;) {
61                 if (strcmp("--debug", (*argv)[i]) == 0) {
62                         debug_set_mode(TRUE);
63
64                         (*argv)[i] = NULL;
65                 }
66
67                 i += 1;
68         }
69
70         /* Remove NULL args from argv[] for further processing */
71         for (i = 1; i < *argc; i++) {
72                 for (k = i; k < *argc; k++)
73                         if ((*argv)[k] != NULL)
74                                 break;
75
76                 if (k > i) {
77                         k -= i;
78                         for (j = i + k; j < *argc; j++)
79                                 (*argv)[j - k] = (*argv)[j];
80                         *argc -= k;
81                 }
82         }
83 }
84
85 gboolean sylpheed_init(int *argc, char ***argv)
86 {
87         if (sylpheed_initialized)
88                 return TRUE;
89
90         startup_dir = g_get_current_dir();
91
92         parse_parameter(argc, argv);
93
94         debug_print("Starting sylpheed version %08x\n", VERSION_NUMERIC);
95
96         setlocale(LC_ALL, "");
97         bindtextdomain(PACKAGE, LOCALEDIR);
98         bind_textdomain_codeset (PACKAGE, "UTF-8");
99         textdomain(PACKAGE);
100         putenv("G_BROKEN_FILENAMES=1");
101
102         /* backup if old rc file exists */
103         if (is_file_exist(RC_DIR)) {
104                 if (rename(RC_DIR, RC_DIR ".bak") < 0) {
105                         FILE_OP_ERROR(RC_DIR, "rename");
106                         return FALSE;
107                 }
108         }
109
110         srand((gint) time(NULL));
111
112 #if USE_OPENSSL
113         ssl_init();
114 #endif
115
116         plugin_load_all("Common");
117
118         sylpheed_initialized = TRUE;
119
120         return TRUE;
121 }
122
123 void sylpheed_done(void)
124 {
125         plugin_unload_all("Common");
126
127 #if USE_OPENSSL
128         ssl_done();
129 #endif
130 }
131
132 const gchar *sylpheed_get_startup_dir(void)
133 {
134         return startup_dir;
135 }
136
137 guint sylpheed_get_version(void)
138 {
139         return VERSION_NUMERIC;
140 }
141
142 void sylpheed_register_idle_function    (void (*idle_func)(void))
143 {
144         sylpheed_idle_function = idle_func;
145 }
146
147 void sylpheed_do_idle(void)
148 {
149         if (sylpheed_idle_function != NULL)
150                 sylpheed_idle_function();
151 }