Modify License to LGPL
[platform/upstream/evolution-data-server.git] / camel / camel.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *           Bertrand Guiheneuf <bertrand@helixcode.com>
5  *
6  *  Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
21  *
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <signal.h>
30
31 #ifdef HAVE_NSS
32 #include <nspr.h>
33 #include <prthread.h>
34 #include "nss.h"      /* Don't use <> here or it will include the system nss.h instead */
35 #include <ssl.h>
36 #endif /* HAVE_NSS */
37
38 #include "camel.h"
39 #include "camel-certdb.h"
40 #include "camel-i18n.h"
41 #include "camel-mime-utils.h"
42 #include "camel-provider.h"
43 #include "camel-debug.h"
44
45 static int initialised = FALSE;
46
47 static void
48 camel_shutdown (void)
49 {
50         CamelCertDB *certdb;
51         
52         if (!initialised)
53                 return;
54         
55 #ifdef HAVE_NSS
56         NSS_Shutdown ();
57         
58         PR_Cleanup ();
59 #endif /* HAVE_NSS */
60         
61         certdb = camel_certdb_get_default ();
62         if (certdb) {
63                 camel_certdb_save (certdb);
64                 camel_object_unref (certdb);
65         }
66         
67         initialised = FALSE;
68 }
69
70 int
71 camel_init (const char *configdir, gboolean nss_init)
72 {
73         CamelCertDB *certdb;
74         char *path;
75         
76         if (initialised)
77                 return 0;
78
79         bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
80         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
81
82         camel_debug_init();
83
84         /* initialise global camel_object_type */
85         camel_object_get_type();
86
87 #ifdef HAVE_NSS
88         if (nss_init) {
89                 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
90                 
91                 if (NSS_InitReadWrite (configdir) == SECFailure) {
92                         /* fall back on using volatile dbs? */
93                         if (NSS_NoDB_Init (configdir) == SECFailure) {
94                                 g_warning ("Failed to initialize NSS");
95                                 return -1;
96                         }
97                 }
98                 
99                 NSS_SetDomesticPolicy ();
100                 
101                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
102                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
103                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
104                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
105         }
106 #endif /* HAVE_NSS */
107         
108         path = g_strdup_printf ("%s/camel-cert.db", configdir);
109         certdb = camel_certdb_new ();
110         camel_certdb_set_filename (certdb, path);
111         g_free (path);
112         
113         /* if we fail to load, who cares? it'll just be a volatile certdb */
114         camel_certdb_load (certdb);
115         
116         /* set this certdb as the default db */
117         camel_certdb_set_default (certdb);
118         
119         camel_object_unref (certdb);
120         
121 #ifndef G_OS_WIN32
122         g_atexit (camel_shutdown);
123 #else
124         /* In GLib (<= 2.8.0 at least, might get fixed later),
125          * g_atexit() is a function in the GLib DLL that calls the
126          * atexit() in the C runtime DLL. atexit() is implemented so
127          * that registered function will be called when the DLL
128          * containing the calling function is being detached from a
129          * process (not when exit() is called).
130          *
131          * We want to run camel_shutdown when the the process exits,
132          * or at least when the camel DLL is being detached, not when
133          * the GLib DLL is being detached.
134          */
135         atexit (camel_shutdown);
136 #endif
137         
138         initialised = TRUE;
139         
140         return 0;
141 }