summaryrefslogtreecommitdiff
path: root/gconf/dllmain.c
blob: f78ede035263e28e14bfb9e665f977d7b3879d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* GConf - dllmain.c
 * Copyright (C) 2005 Novell, Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <windows.h>
#include <string.h>
#include <mbstring.h>
#include <glib.h>

/* locale_dir uses system codepage as it is passed to the non-UTF8ified
 * gettext library
 */
static const char *locale_dir;

/* The others are in UTF-8 */
static const char *runtime_prefix;
static const char *confdir;
static const char *etcdir;
static const char *serverdir;
static const char *backend_dir;

static HMODULE hmodule;
G_LOCK_DEFINE_STATIC (mutex);

char *
_gconf_win32_replace_prefix (const char *configure_time_path)
{
  if (strncmp (configure_time_path, PREFIX "/", strlen (PREFIX) + 1) == 0)
    {
      return g_strconcat (runtime_prefix,
			  configure_time_path + strlen (PREFIX),
			  NULL);
    }
  else
    return g_strdup (configure_time_path);
}

/* Minimal DllMain used to only tuck away the libgconf DLL's HMODULE */
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
	 DWORD     fdwReason,
	 LPVOID    lpvReserved)
{
  switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      hmodule = hinstDLL;
      break;
    }
  return TRUE;
}

static void
setup (void)
{
  char *full_prefix = NULL;
  char *cp_prefix; 

  wchar_t wcbfr[1000];
  
  G_LOCK (mutex);
  if (locale_dir != NULL)
    {
      G_UNLOCK (mutex);
      return;
    }

  if (GetModuleFileNameW (hmodule, wcbfr, G_N_ELEMENTS (wcbfr)))
    {
      full_prefix = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);
      if (GetShortPathNameW (wcbfr, wcbfr, G_N_ELEMENTS (wcbfr)))
	cp_prefix = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);
      else if (full_prefix)
	cp_prefix = g_locale_from_utf8 (full_prefix, -1, NULL, NULL, NULL);
    }

  if (full_prefix != NULL)
    {
      gchar *p = strrchr (full_prefix, '\\');
      if (p != NULL)
	*p = '\0';

      p = strrchr (full_prefix, '\\');
      if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
	*p = '\0';
		  
      /* Replace backslashes with forward slashes to avoid
       * problems for instance in makefiles that use
       * gconftool-2 --get-default-source.
       */
      while ((p = strchr (full_prefix, '\\')) != NULL)
	*p = '/';

      runtime_prefix = full_prefix;
    }
  else
    {
      runtime_prefix = g_strdup ("");
    }

  if (cp_prefix != NULL)
    {
      gchar *p = _mbsrchr (cp_prefix, '\\');
      if (p != NULL)
	*p = '\0';
      
      p = _mbsrchr (cp_prefix, '\\');
      if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
	*p = '\0';
    }
  else
    {
      cp_prefix = g_strdup ("");
    }

  locale_dir = g_strconcat (cp_prefix, GCONF_LOCALE_DIR + strlen (PREFIX), NULL);

  confdir = _gconf_win32_replace_prefix (GCONF_CONFDIR);
  etcdir = _gconf_win32_replace_prefix (GCONF_ETCDIR);
  serverdir = _gconf_win32_replace_prefix (GCONF_SERVERDIR);
  backend_dir = _gconf_win32_replace_prefix (GCONF_BACKEND_DIR);

  G_UNLOCK (mutex);
}

#define GETTER(varbl)				\
const char *					\
_gconf_win32_get_##varbl (void)		\
{						\
	setup ();				\
        return varbl;				\
}

GETTER (locale_dir)
GETTER (confdir)
GETTER (etcdir)
GETTER (serverdir)
GETTER (backend_dir)