1 /*
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10  * for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, see <http://www.gnu.org/licenses/>.
14  *
15  *
16  * Authors:
17  *		Tor Lillqvist <tml@novell.com>
18  *
19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20  *
21  */
22 
23 #include "evolution-config.h"
24 
25 #include <windows.h>
26 #include <string.h>
27 
28 #include <glib.h>
29 
30 /* localedir uses system codepage as it is passed to the non-UTF8ified
31  * gettext library
32  */
33 static const gchar *localedir = NULL;
34 
35 /* The others are in UTF-8 */
36 static const gchar *bindir;
37 static const gchar *datadir;
38 static const gchar *ecpsdir;
39 static const gchar *etspecdir;
40 static const gchar *galviewsdir;
41 static const gchar *helpdir;
42 static const gchar *icondir;
43 static const gchar *imagesdir;
44 static const gchar *libdir;
45 static const gchar *libexecdir;
46 static const gchar *moduledir;
47 static const gchar *plugindir;
48 static const gchar *prefix;
49 static const gchar *privdatadir;
50 static const gchar *ruledir;
51 static const gchar *sounddir;
52 static const gchar *sysconfdir;
53 static const gchar *toolsdir;
54 static const gchar *uidir;
55 static const gchar *webkitdatadir;
56 
57 static HMODULE hmodule;
58 G_LOCK_DEFINE_STATIC (mutex);
59 
60 /* Silence gcc with a prototype. Yes, this is silly. */
61 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
62 		     DWORD     fdwReason,
63 		     LPVOID    lpvReserved);
64 
65 /* Minimal DllMain that just tucks away the DLL's HMODULE */
66 BOOL WINAPI
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)67 DllMain (HINSTANCE hinstDLL,
68 	 DWORD     fdwReason,
69 	 LPVOID    lpvReserved)
70 {
71         switch (fdwReason) {
72         case DLL_PROCESS_ATTACH:
73                 hmodule = hinstDLL;
74                 break;
75         }
76         return TRUE;
77 }
78 
79 static gchar *
replace_prefix(const gchar * runtime_prefix,const gchar * configure_time_path)80 replace_prefix (const gchar *runtime_prefix,
81                 const gchar *configure_time_path)
82 {
83         if (runtime_prefix &&
84             strncmp (configure_time_path, EVOLUTION_PREFIX "/",
85                      strlen (EVOLUTION_PREFIX) + 1) == 0) {
86                 return g_strconcat (runtime_prefix,
87                                     configure_time_path + strlen (EVOLUTION_PREFIX),
88                                     NULL);
89         } else
90                 return g_strdup (configure_time_path);
91 }
92 
93 static void
setup(void)94 setup (void)
95 {
96 	gchar *full_prefix;
97 	gchar *cp_prefix;
98 
99         G_LOCK (mutex);
100         if (localedir != NULL) {
101                 G_UNLOCK (mutex);
102                 return;
103         }
104 
105 	/* This requires that the libeutil DLL is installed in $bindir */
106         full_prefix = g_win32_get_package_installation_directory_of_module(hmodule);
107         cp_prefix = g_win32_locale_filename_from_utf8(full_prefix);
108 
109         localedir = replace_prefix (cp_prefix, EVOLUTION_LOCALEDIR);
110 
111         g_free (cp_prefix);
112 
113 	prefix = g_strdup (full_prefix);
114 
115 	/* It makes sense to have some of the paths overridable with
116 	 * environment variables.
117 	 */
118 	bindir = replace_prefix (full_prefix, EVOLUTION_BINDIR);
119 	datadir = replace_prefix (full_prefix, EVOLUTION_DATADIR);
120 	ecpsdir = replace_prefix (full_prefix, EVOLUTION_ECPSDIR);
121 	etspecdir = replace_prefix (full_prefix, EVOLUTION_ETSPECDIR);
122 	galviewsdir = replace_prefix (full_prefix, EVOLUTION_GALVIEWSDIR);
123 	helpdir = replace_prefix (full_prefix, EVOLUTION_HELPDIR);
124 	if (g_getenv ("EVOLUTION_ICONDIR") &&
125 	    g_file_test (g_getenv ("EVOLUTION_ICONDIR"), G_FILE_TEST_IS_DIR))
126 		icondir = g_getenv ("EVOLUTION_ICONDIR");
127 	else
128 		icondir = replace_prefix (full_prefix, EVOLUTION_ICONDIR);
129 	if (g_getenv ("EVOLUTION_IMAGESDIR") &&
130 	    g_file_test (g_getenv ("EVOLUTION_IMAGESDIR"), G_FILE_TEST_IS_DIR))
131 		imagesdir = g_getenv ("EVOLUTION_IMAGESDIR");
132 	else
133 		imagesdir = replace_prefix (full_prefix, EVOLUTION_IMAGESDIR);
134 	if (g_getenv ("EVOLUTION_WEBKITDATADIR") &&
135 	    g_file_test (g_getenv ("EVOLUTION_WEBKITDATADIR"), G_FILE_TEST_IS_DIR))
136 		webkitdatadir = g_getenv ("EVOLUTION_WEBKITDATADIR");
137 	else
138 		webkitdatadir = replace_prefix (full_prefix, EVOLUTION_WEBKITDATADIR);
139 	libdir = replace_prefix (full_prefix, EVOLUTION_LIBDIR);
140 	libexecdir = replace_prefix (full_prefix, EVOLUTION_LIBEXECDIR);
141 	moduledir = replace_prefix (full_prefix, EVOLUTION_MODULEDIR);
142 	plugindir = replace_prefix (full_prefix, EVOLUTION_PLUGINDIR);
143 	privdatadir = replace_prefix (full_prefix, EVOLUTION_PRIVDATADIR);
144 	ruledir = replace_prefix (full_prefix, EVOLUTION_RULEDIR);
145 	sounddir = replace_prefix (full_prefix, EVOLUTION_SOUNDDIR);
146 	sysconfdir = replace_prefix (full_prefix, EVOLUTION_SYSCONFDIR);
147 	toolsdir = replace_prefix (full_prefix, EVOLUTION_TOOLSDIR);
148 	uidir = replace_prefix (full_prefix, EVOLUTION_UIDIR);
149 
150         g_free (full_prefix);
151 
152 	G_UNLOCK (mutex);
153 }
154 
155 #include "e-util-private.h"	/* For prototypes */
156 
157 #define GETTER(varbl)				\
158 const gchar *					\
159 _e_get_##varbl (void)				\
160 {						\
161         setup ();				\
162         return varbl;				\
163 }
164 
165 GETTER(bindir)
GETTER(datadir)166 GETTER(datadir)
167 GETTER(ecpsdir)
168 GETTER(etspecdir)
169 GETTER(galviewsdir)
170 GETTER(helpdir)
171 GETTER(icondir)
172 GETTER(imagesdir)
173 GETTER(libdir)
174 GETTER(libexecdir)
175 GETTER(localedir)
176 GETTER(moduledir)
177 GETTER(plugindir)
178 GETTER(prefix)
179 GETTER(privdatadir)
180 GETTER(ruledir)
181 GETTER(sounddir)
182 GETTER(sysconfdir)
183 GETTER(toolsdir)
184 GETTER(uidir)
185 GETTER(webkitdatadir)
186 
187 gpointer _e_get_dll_hmodule (void)
188 {
189 	return hmodule;
190 }
191