aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/efreet/efreet.c
blob: 091899f2b2a71144093bc0fe6132a0c9670e933d (plain) (blame)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <Eet.h>
#include <Ecore.h>
#include <Ecore_File.h>

/* define macros and variable for using the eina logging system  */
#define EFREET_MODULE_LOG_DOM /* no logging in this file */

#include "Efreet.h"
#include "efreet_private.h"
#include "efreet_xml.h"

/*
 * Needs EAPI because of helper binaries
 */
EAPI int efreet_cache_update = 1;

static int _efreet_init_count = 0;
static int efreet_parsed_locale = 0;
static const char *efreet_lang = NULL;
static const char *efreet_lang_country = NULL;
static const char *efreet_lang_modifier = NULL;
static const char *efreet_language = NULL;
static void efreet_parse_locale(void);
static int efreet_parse_locale_setting(const char *env);

#ifndef _WIN32
static uid_t ruid;
static uid_t rgid;
#endif

EAPI int
efreet_init(void)
{
#ifndef _WIN32
   char *tmp;
#endif

   if (++_efreet_init_count != 1)
     return _efreet_init_count;

#ifndef _WIN32
   /* Find users real uid and gid */
   tmp = getenv("SUDO_UID");
   if (tmp)
     ruid = strtoul(tmp, NULL, 10);
   else
     ruid = getuid();

   tmp = getenv("SUDO_GID");
   if (tmp)
     rgid = strtoul(tmp, NULL, 10);
   else
     rgid = getgid();
#endif

   if (!eina_init())
     return --_efreet_init_count;
   if (!eet_init())
     goto shutdown_eina;
   if (!ecore_init())
     goto shutdown_eet;
   if (!ecore_file_init())
     goto shutdown_ecore;

   if (!efreet_base_init())
     goto shutdown_ecore_file;

   if (!efreet_cache_init())
     goto shutdown_efreet_base;

   if (!efreet_xml_init())
     goto shutdown_efreet_cache;

   if (!efreet_icon_init())
     goto shutdown_efreet_xml;

   if (!efreet_ini_init())
     goto shutdown_efreet_icon;

   if (!efreet_desktop_init())
     goto shutdown_efreet_ini;

   if (!efreet_menu_init())
     goto shutdown_efreet_desktop;

   if (!efreet_util_init())
     goto shutdown_efreet_menu;

#ifdef ENABLE_NLS
   bindtextdomain(PACKAGE, LOCALE_DIR);
   bind_textdomain_codeset(PACKAGE, "UTF-8");
#endif

   return _efreet_init_count;

shutdown_efreet_menu:
   efreet_menu_shutdown();
shutdown_efreet_desktop:
   efreet_desktop_shutdown();
shutdown_efreet_ini:
   efreet_ini_shutdown();
shutdown_efreet_icon:
   efreet_icon_shutdown();
shutdown_efreet_xml:
   efreet_xml_shutdown();
shutdown_efreet_cache:
   efreet_cache_shutdown();
shutdown_efreet_base:
   efreet_base_shutdown();
shutdown_ecore_file:
   ecore_file_shutdown();
shutdown_ecore:
   ecore_shutdown();
shutdown_eet:
   eet_shutdown();
shutdown_eina:
   eina_shutdown();

   return --_efreet_init_count;
}

EAPI int
efreet_shutdown(void)
{
   if (_efreet_init_count <= 0)
     {
        EINA_LOG_ERR("Init count not greater than 0 in shutdown.");
        return 0;
     }
   if (--_efreet_init_count != 0)
     return _efreet_init_count;

   efreet_util_shutdown();
   efreet_menu_shutdown();
   efreet_desktop_shutdown();
   efreet_ini_shutdown();
   efreet_icon_shutdown();
   efreet_xml_shutdown();
   efreet_cache_shutdown();
   efreet_base_shutdown();

   IF_RELEASE(efreet_lang);
   IF_RELEASE(efreet_lang_country);
   IF_RELEASE(efreet_lang_modifier);
   IF_RELEASE(efreet_language);
   efreet_parsed_locale = 0;  /* reset this in case they init efreet again */

   ecore_file_shutdown();
   ecore_shutdown();
   eet_shutdown();
   eina_shutdown();

   return _efreet_init_count;
}

EAPI void
efreet_lang_reset(void)
{
   IF_RELEASE(efreet_lang);
   IF_RELEASE(efreet_lang_country);
   IF_RELEASE(efreet_lang_modifier);
   IF_RELEASE(efreet_language);
   efreet_parsed_locale = 0;  /* reset this in case they init efreet again */

   efreet_dirs_reset();
   efreet_parse_locale();
   efreet_cache_desktop_close();
   efreet_cache_desktop_build();
}

/**
 * @internal
 * @return Returns the current users language setting or NULL if none set
 * @brief Retrieves the current language setting
 */
const char *
efreet_lang_get(void)
{
   if (efreet_parsed_locale) return efreet_lang;

   efreet_parse_locale();
   return efreet_lang;
}

/**
 * @internal
 * @return Returns the current language country setting or NULL if none set
 * @brief Retrieves the current country setting for the current language or
 */
const char *
efreet_lang_country_get(void)
{
   if (efreet_parsed_locale) return efreet_lang_country;

   efreet_parse_locale();
   return efreet_lang_country;
}

/**
 * @internal
 * @return Returns the current language modifier setting or NULL if none
 * set.
 * @brief Retrieves the modifier setting for the language.
 */
const char *
efreet_lang_modifier_get(void)
{
   if (efreet_parsed_locale) return efreet_lang_modifier;

   efreet_parse_locale();
   return efreet_lang_modifier;
}

EAPI const char *
efreet_language_get(void)
{
   if (efreet_parsed_locale) return efreet_language;

   efreet_parse_locale();
   return efreet_language;
}

/**
 * @internal
 * @return Returns no value
 * @brief Parses out the language, country and modifer setting from the
 * LC_MESSAGES environment variable on UNIX. On Windows, retrieve them from
 * the system.
 */
static void
efreet_parse_locale(void)
{
   efreet_parsed_locale = 1;

   if (efreet_parse_locale_setting("LANG"))
     return;

   if (efreet_parse_locale_setting("LC_ALL"))
     return;

   if (efreet_parse_locale_setting("LC_MESSAGES"))
     return;

   efreet_language = eina_stringshare_add("C");
}

/**
 * @internal
 * @param env The environment variable to grab
 * @return Returns 1 if we parsed something of @a env, 0 otherwise
 * @brief Tries to parse the lang settings out of the given environment
 * variable
 *
 * @note @a env is not used on Windows.
 */
static int
efreet_parse_locale_setting(const char *env)
{
#ifdef _WIN32
   char buf_lang[18];
   char buf[9];
   int l1;
   int l2;

   l1 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME,
                      buf, sizeof(buf));
   if (!l1)
     return 0;

   efreet_lang = eina_stringshare_add(buf);
   memcpy(buf_lang, buf, l1 - 1);
   buf_lang[l1 - 1] = '_';

   l2 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME,
                      buf, sizeof(buf));
   if (!l2)
     return 0;

   efreet_lang_country = eina_stringshare_add(buf);
   memcpy(buf_lang + l1, buf, l2);

   efreet_language = eina_stringshare_add(buf_lang);

   return 1;

   (void)env;
#else
   int found = 0;
   char *setting;
   char *p;
   size_t len;

   p = getenv(env);
   if (!p) return 0;
   len = strlen(p) + 1;
   setting = alloca(len);
   memcpy(setting, p, len);

   /* pull the modifier off the end */
   p = strrchr(setting, '@');
   if (p)
     {
        *p = '\0';
        efreet_lang_modifier = eina_stringshare_add(p + 1);
        found = 1;
     }

   /* if there is an encoding we ignore it */
   p = strrchr(setting, '.');
   if (p) *p = '\0';

   /* get the country if available */
   p = strrchr(setting, '_');
   if (p)
     {
        *p = '\0';
        efreet_lang_country = eina_stringshare_add(p + 1);
        found = 1;
     }

   if (*setting != '\0')
     {
        efreet_lang = eina_stringshare_add(setting);
        found = 1;
     }

   if (found)
     efreet_language = eina_stringshare_add(getenv(env));
   return found;
#endif
}

/**
 * @internal
 * @param buffer The destination buffer
 * @param size The destination buffer size
 * @param strs The strings to concatenate together
 * @return Returns the size of the string in @a buffer
 * @brief Concatenates the strings in @a strs into the given @a buffer not
 * exceeding the given @a size.
 */
size_t
efreet_array_cat(char *buffer, size_t size, const char *strs[])
{
   int i;
   size_t n;
   for (i = 0, n = 0; n < size && strs[i]; i++)
     {
        n += eina_strlcpy(buffer + n, strs[i], size - n);
     }
   return n;
}

#ifndef _WIN32
EAPI void
efreet_fsetowner(int fd)
{
   struct stat st;

   if (fd < 0) return;
   if (fstat(fd, &st) < 0) return;
   if (st.st_uid == ruid) return;

   if (fchown(fd, ruid, rgid) != 0) return;
}
#else
EAPI void
efreet_fsetowner(int fd EINA_UNUSED)
{
}
#endif

#ifndef _WIN32
EAPI void
efreet_setowner(const char *path)
{
   EINA_SAFETY_ON_NULL_RETURN(path);

   int fd;

   fd = open(path, O_RDONLY);
   if (fd < 0) return;
   efreet_fsetowner(fd);
   close(fd);
}
#else
EAPI void
efreet_setowner(const char *path EINA_UNUSED)
{
}
#endif