summaryrefslogtreecommitdiff
path: root/endless/eosinit.c
blob: 057e391301150016a79eb11f62ab1f705f8b2dc7 (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
/* Copyright 2013 Endless Mobile, Inc. */

#include <config.h>
#include <glib.h>
#include <glib/gi18n-lib.h>

#include "eosinit-private.h"

/* Constructors supported since GCC 2.7; I have this on GLib's authority. This
should also work on Clang. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)

#define _EOS_CONSTRUCTOR(func) static void __attribute__((constructor)) func (void);
#define _EOS_DESTRUCTOR(func) static void __atrribute__((destructor)) func (void);

#else

#error "We do not currently support constructors for your compiler."

#endif /* compiler version */

static gboolean _eos_initialized = FALSE;

static char *eos_system_personality;

/*
 * _eos_init:
 *
 * This function initializes the library. It is called automatically when the
 * library is loaded.
 */
_EOS_CONSTRUCTOR(_eos_init);
static void
_eos_init (void)
{
  if (G_UNLIKELY (!_eos_initialized))
    {
      /* Initialize Gettext */
      bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
      bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

      _eos_initialized = TRUE;
    }
}

/*
 * eos_is_inited:
 *
 * For testing purposes.
 */
gboolean
eos_is_inited (void)
{
  return _eos_initialized;
}

/**
 * eos_get_system_personality:
 *
 * Retrieves the "personality" of the system.
 *
 * The personality is a unique string that identifies the installation
 * of EndlessOS for a specific country or audience. The availability of
 * certain applications, or their content, is determined by this value.
 *
 * Return value: (transfer none): a string, owned by the Endless SDK,
 *   with the name of the personality. You should never free or modify
 *   the returned string.
 */
const char *
eos_get_system_personality (void)
{
  static char *personality;

  if (g_once_init_enter (&personality))
    {
      char *tmp;

      tmp = g_strdup (g_getenv ("ENDLESS_OS_PERSONALITY"));
      if (tmp == NULL || *tmp == '\0')
        {
          char *path = g_build_filename (DATADIR,
                                         "EndlessOS",
                                         "personality.txt",
                                         NULL);

          GError *error = NULL;
          g_file_get_contents (path, &tmp, NULL, &error);
          if (error != NULL)
            {
              g_critical ("No personality defined: %s", error->message);
              g_error_free (error);
              tmp = NULL;
            }
        }

      if (tmp == NULL)
        tmp = g_strdup ("Default");

      g_once_init_leave (&personality, tmp);
    }

  return personality;
}