Statistics
| Revision:

root / libsylph / sylmain.c @ 2164

History | View | Annotate | Download (6.86 KB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2009 Hiroyuki Yamamoto
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, 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

    
26
#include <glib.h>
27
#include <glib/gi18n.h>
28

    
29
#ifdef G_OS_UNIX
30
#  include <signal.h>
31
#endif
32

    
33
#if HAVE_LOCALE_H
34
#  include <locale.h>
35
#endif
36

    
37
#include "sylmain.h"
38
#include "syl-marshal.h"
39
#include "prefs_common.h"
40
#include "account.h"
41
#include "filter.h"
42
#include "folder.h"
43
#include "socket.h"
44
#include "codeconv.h"
45
#include "utils.h"
46

    
47
#if USE_SSL
48
#  include "ssl.h"
49
#endif
50

    
51
#ifndef PACKAGE
52
#  define PACKAGE        GETTEXT_PACKAGE
53
#endif
54

    
55
G_DEFINE_TYPE(SylApp, syl_app, G_TYPE_OBJECT);
56

    
57
enum {
58
        INIT_DONE,
59
        APP_EXIT,
60
        ADD_MSG,
61
        REMOVE_MSG,
62
        REMOVE_ALL_MSG,
63
        REMOVE_FOLDER,
64
        MOVE_FOLDER,
65
        FOLDERLIST_UPDATED,
66
        LAST_SIGNAL
67
};
68

    
69
static guint app_signals[LAST_SIGNAL] = { 0 };
70

    
71
static GObject *app = NULL;
72

    
73

    
74
static void syl_app_init(SylApp *self)
75
{
76
}
77

    
78
static void syl_app_class_init(SylAppClass *klass)
79
{
80
        GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
81

    
82
        app_signals[INIT_DONE] =
83
                g_signal_new("init-done",
84
                             G_TYPE_FROM_CLASS(gobject_class),
85
                             G_SIGNAL_RUN_FIRST,
86
                             0,
87
                             NULL, NULL,
88
                             syl_marshal_VOID__VOID,
89
                             G_TYPE_NONE,
90
                             0);
91
        app_signals[APP_EXIT] =
92
                g_signal_new("app-exit",
93
                             G_TYPE_FROM_CLASS(gobject_class),
94
                             G_SIGNAL_RUN_FIRST,
95
                             0,
96
                             NULL, NULL,
97
                             syl_marshal_VOID__VOID,
98
                             G_TYPE_NONE,
99
                             0);
100
        app_signals[ADD_MSG] =
101
                g_signal_new("add-msg",
102
                             G_TYPE_FROM_CLASS(gobject_class),
103
                             G_SIGNAL_RUN_FIRST,
104
                             0,
105
                             NULL, NULL,
106
                             syl_marshal_VOID__POINTER_STRING_UINT,
107
                             G_TYPE_NONE,
108
                             3,
109
                             G_TYPE_POINTER,
110
                             G_TYPE_STRING,
111
                             G_TYPE_UINT);
112
        app_signals[REMOVE_MSG] =
113
                g_signal_new("remove-msg",
114
                             G_TYPE_FROM_CLASS(gobject_class),
115
                             G_SIGNAL_RUN_FIRST,
116
                             0,
117
                             NULL, NULL,
118
                             syl_marshal_VOID__POINTER_STRING_UINT,
119
                             G_TYPE_NONE,
120
                             3,
121
                             G_TYPE_POINTER,
122
                             G_TYPE_STRING,
123
                             G_TYPE_UINT);
124
        app_signals[REMOVE_ALL_MSG] =
125
                g_signal_new("remove-all-msg",
126
                             G_TYPE_FROM_CLASS(gobject_class),
127
                             G_SIGNAL_RUN_FIRST,
128
                             0,
129
                             NULL, NULL,
130
                             syl_marshal_VOID__POINTER,
131
                             G_TYPE_NONE,
132
                             1,
133
                             G_TYPE_POINTER);
134
        app_signals[REMOVE_FOLDER] =
135
                g_signal_new("remove-folder",
136
                             G_TYPE_FROM_CLASS(gobject_class),
137
                             G_SIGNAL_RUN_FIRST,
138
                             0,
139
                             NULL, NULL,
140
                             syl_marshal_VOID__POINTER,
141
                             G_TYPE_NONE,
142
                             1,
143
                             G_TYPE_POINTER);
144
        app_signals[MOVE_FOLDER] =
145
                g_signal_new("move-folder",
146
                             G_TYPE_FROM_CLASS(gobject_class),
147
                             G_SIGNAL_RUN_FIRST,
148
                             0,
149
                             NULL, NULL,
150
                             syl_marshal_VOID__POINTER_STRING_STRING,
151
                             G_TYPE_NONE,
152
                             3,
153
                             G_TYPE_POINTER,
154
                             G_TYPE_STRING,
155
                             G_TYPE_STRING);
156
        app_signals[FOLDERLIST_UPDATED] =
157
                g_signal_new("folderlist-updated",
158
                             G_TYPE_FROM_CLASS(gobject_class),
159
                             G_SIGNAL_RUN_FIRST,
160
                             0,
161
                             NULL, NULL,
162
                             syl_marshal_VOID__VOID,
163
                             G_TYPE_NONE,
164
                             0);
165
}
166

    
167
GObject *syl_app_create(void)
168
{
169
        if (!app)
170
                app = g_object_new(SYL_TYPE_APP, NULL);
171
        return app;
172
}
173

    
174
GObject *syl_app_get(void)
175
{
176
        return app;
177
}
178

    
179
void syl_init(void)
180
{
181
#ifdef G_OS_WIN32
182
        gchar *newpath;
183
        const gchar *lang_env;
184

    
185
        /* disable locale variable such as "LANG=1041" */
186

    
187
#define DISABLE_DIGIT_LOCALE(envstr)                        \
188
{                                                        \
189
        lang_env = g_getenv(envstr);                        \
190
        if (lang_env && g_ascii_isdigit(lang_env[0]))        \
191
                g_unsetenv(envstr);                        \
192
}
193

    
194
        DISABLE_DIGIT_LOCALE("LC_ALL");
195
        DISABLE_DIGIT_LOCALE("LANG");
196
        DISABLE_DIGIT_LOCALE("LC_CTYPE");
197
        DISABLE_DIGIT_LOCALE("LC_MESSAGES");
198

    
199
#undef DISABLE_DIGIT_LOCALE
200

    
201
        g_unsetenv("LANGUAGE");
202
#endif /* G_OS_WIN32 */
203

    
204
#ifdef HAVE_LOCALE_H
205
        setlocale(LC_ALL, "");
206
#endif
207

    
208
        set_startup_dir();
209

    
210
#ifdef G_OS_WIN32
211
        /* include startup directory into %PATH% for GSpawn */
212
        newpath = g_strconcat(get_startup_dir(), ";", g_getenv("PATH"), NULL);
213
        g_setenv("PATH", newpath, TRUE);
214
        g_free(newpath);
215
#endif
216

    
217
#ifdef ENABLE_NLS
218
        syl_init_gettext(PACKAGE, LOCALEDIR);
219
        textdomain(PACKAGE);
220
#endif
221

    
222
        sock_init();
223
#if USE_SSL
224
        ssl_init();
225
#endif
226

    
227
#ifdef G_OS_UNIX
228
        /* ignore SIGPIPE signal for preventing sudden death of program */
229
        signal(SIGPIPE, SIG_IGN);
230
#endif
231
}
232

    
233
#define MAKE_DIR_IF_NOT_EXIST(dir)                                        \
234
{                                                                        \
235
        if (!is_dir_exist(dir)) {                                        \
236
                if (is_file_exist(dir)) {                                \
237
                        g_warning("File '%s' already exists. "                \
238
                                  "Can't create folder.", dir);                \
239
                        return -1;                                        \
240
                }                                                        \
241
                if (make_dir(dir) < 0)                                        \
242
                        return -1;                                        \
243
        }                                                                \
244
}
245

    
246
void syl_init_gettext(const gchar *package, const gchar *dirname)
247
{
248
#ifdef ENABLE_NLS
249
        if (g_path_is_absolute(dirname))
250
                bindtextdomain(package, dirname);
251
        else {
252
                gchar *locale_dir;
253

    
254
                locale_dir = g_strconcat(get_startup_dir(), G_DIR_SEPARATOR_S,
255
                                         dirname, NULL);
256
#ifdef G_OS_WIN32
257
                {
258
                        gchar *locale_dir_;
259

    
260
                        locale_dir_ = g_locale_from_utf8(locale_dir, -1,
261
                                                         NULL, NULL, NULL);
262
                        if (locale_dir_) {
263
                                g_free(locale_dir);
264
                                locale_dir = locale_dir_;
265
                        }
266
                }
267
#endif /* G_OS_WIN32 */
268
                bindtextdomain(package, locale_dir);
269
                g_free(locale_dir);
270
        }
271

    
272
        bind_textdomain_codeset(package, CS_UTF_8);
273
#endif /* ENABLE_NLS */
274
}
275

    
276
gint syl_setup_rc_dir(void)
277
{
278
        if (!is_dir_exist(get_rc_dir())) {
279
                if (make_dir_hier(get_rc_dir()) < 0)
280
                        return -1;
281
        }
282

    
283
        MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
284

    
285
        CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), -1);
286

    
287
        MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
288
        MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
289
        MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
290
        MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
291
        MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
292

    
293
        /* remove temporary files */
294
        remove_all_files(get_tmp_dir());
295
        remove_all_files(get_mime_tmp_dir());
296

    
297
        return 0;
298
}
299

    
300
void syl_save_all_state(void)
301
{
302
        folder_write_list();
303
        prefs_common_write_config();
304
        filter_write_config();
305
        account_write_config_all();
306
}
307

    
308
void syl_cleanup(void)
309
{
310
        /* remove temporary files */
311
        remove_all_files(get_tmp_dir());
312
        remove_all_files(get_mime_tmp_dir());
313
#if GLIB_CHECK_VERSION(2, 6, 0)
314
        g_log_set_default_handler(g_log_default_handler, NULL);
315
#endif
316
        close_log_file();
317

    
318
#if USE_SSL
319
        ssl_done();
320
#endif
321
        sock_cleanup();
322

    
323
        if (app) {
324
                g_object_unref(app);
325
                app = NULL;
326
        }
327
}