109
by robert.ancell at gmail
Add language information |
1 |
/*
|
2 |
* Copyright (C) 2010 Robert Ancell.
|
|
3 |
* Author: Robert Ancell <robert.ancell@canonical.com>
|
|
4 |
*
|
|
190
by robert.ancell at gmail
Change libldmgreeter library from GPL to LGPL |
5 |
* This library is free software; you can redistribute it and/or modify it under
|
6 |
* the terms of the GNU Lesser General Public License as published by the Free
|
|
7 |
* Software Foundation; either version 3 of the License, or (at your option) any
|
|
8 |
* later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
|
|
109
by robert.ancell at gmail
Add language information |
9 |
* license.
|
10 |
*/
|
|
11 |
||
12 |
#include <locale.h> |
|
13 |
#include <langinfo.h> |
|
14 |
||
421
by Robert Ancell
Move public liblightdm-gobject-0 headers into subdirectory |
15 |
#include "lightdm/language.h" |
109
by robert.ancell at gmail
Add language information |
16 |
|
17 |
enum { |
|
18 |
PROP_0, |
|
19 |
PROP_CODE, |
|
20 |
PROP_NAME, |
|
21 |
PROP_TERRITORY
|
|
22 |
};
|
|
23 |
||
24 |
struct _LdmLanguagePrivate |
|
25 |
{
|
|
26 |
gchar *code; |
|
27 |
gchar *name; |
|
28 |
gchar *territory; |
|
29 |
};
|
|
30 |
||
31 |
G_DEFINE_TYPE (LdmLanguage, ldm_language, G_TYPE_OBJECT); |
|
32 |
||
33 |
/**
|
|
34 |
* ldm_language_new:
|
|
35 |
*
|
|
36 |
* Create a new language.
|
|
37 |
* @code: The language code
|
|
38 |
*
|
|
39 |
* Return value: the new #LdmLanguage
|
|
40 |
**/
|
|
41 |
LdmLanguage * |
|
42 |
ldm_language_new (const gchar *code) |
|
43 |
{
|
|
44 |
return g_object_new (LDM_TYPE_LANGUAGE, "code", code, NULL); |
|
45 |
}
|
|
46 |
||
47 |
/**
|
|
48 |
* ldm_language_get_code:
|
|
49 |
* @language: A #LdmLanguage
|
|
50 |
*
|
|
51 |
* Get the code of a language.
|
|
52 |
*
|
|
53 |
* Return value: The code of the language
|
|
54 |
**/
|
|
55 |
const gchar * |
|
56 |
ldm_language_get_code (LdmLanguage *language) |
|
57 |
{
|
|
411
by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods |
58 |
g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL); |
109
by robert.ancell at gmail
Add language information |
59 |
return language->priv->code; |
60 |
}
|
|
61 |
||
62 |
/**
|
|
63 |
* ldm_language_get_name:
|
|
64 |
* @language: A #LdmLanguage
|
|
65 |
*
|
|
66 |
* Get the name of a language.
|
|
67 |
*
|
|
68 |
* Return value: The name of the language
|
|
69 |
**/
|
|
70 |
const gchar * |
|
71 |
ldm_language_get_name (LdmLanguage *language) |
|
72 |
{
|
|
411
by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods |
73 |
g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL); |
74 |
||
109
by robert.ancell at gmail
Add language information |
75 |
if (!language->priv->name) |
76 |
{
|
|
77 |
char *current = setlocale(LC_ALL, NULL); |
|
78 |
setlocale(LC_ALL, language->priv->code); |
|
488
by Fneufneu
Allow to compile on systems that don't have nl_langinfo (e.g. BSD) |
79 |
#ifdef _NL_IDENTIFICATION_LANGUAGE
|
109
by robert.ancell at gmail
Add language information |
80 |
language->priv->name = g_strdup (nl_langinfo (_NL_IDENTIFICATION_LANGUAGE)); |
488
by Fneufneu
Allow to compile on systems that don't have nl_langinfo (e.g. BSD) |
81 |
#else
|
82 |
language->priv->name = g_strdup ("Unknown"); |
|
83 |
#endif
|
|
109
by robert.ancell at gmail
Add language information |
84 |
setlocale(LC_ALL, current); |
85 |
}
|
|
86 |
||
87 |
return language->priv->name; |
|
88 |
}
|
|
89 |
||
90 |
/**
|
|
91 |
* ldm_language_get_territory:
|
|
92 |
* @language: A #LdmLanguage
|
|
93 |
*
|
|
94 |
* Get the territory the language is used in.
|
|
95 |
*
|
|
96 |
* Return value: The territory the language is used in.
|
|
97 |
**/
|
|
98 |
const gchar * |
|
99 |
ldm_language_get_territory (LdmLanguage *language) |
|
100 |
{
|
|
411
by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods |
101 |
g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL); |
102 |
||
109
by robert.ancell at gmail
Add language information |
103 |
if (!language->priv->territory) |
104 |
{
|
|
105 |
char *current = setlocale(LC_ALL, NULL); |
|
106 |
setlocale(LC_ALL, language->priv->code); |
|
488
by Fneufneu
Allow to compile on systems that don't have nl_langinfo (e.g. BSD) |
107 |
#ifdef _NL_IDENTIFICATION_TERRITORY
|
109
by robert.ancell at gmail
Add language information |
108 |
language->priv->territory = g_strdup (nl_langinfo (_NL_IDENTIFICATION_TERRITORY)); |
488
by Fneufneu
Allow to compile on systems that don't have nl_langinfo (e.g. BSD) |
109 |
#else
|
110 |
language->priv->territory = g_strdup ("Unknown"); |
|
111 |
#endif
|
|
109
by robert.ancell at gmail
Add language information |
112 |
setlocale(LC_ALL, current); |
113 |
}
|
|
114 |
||
115 |
return language->priv->territory; |
|
116 |
}
|
|
117 |
||
345
by robert.ancell at canonical
Correctly pass LANG and LANGUAGE through to the session |
118 |
static gboolean |
119 |
is_utf8 (const gchar *code) |
|
120 |
{
|
|
414
by Robert Ancell
Handle default language when LANG is not defined |
121 |
return g_str_has_suffix (code, ".utf8") || g_str_has_suffix (code, ".UTF-8"); |
345
by robert.ancell at canonical
Correctly pass LANG and LANGUAGE through to the session |
122 |
}
|
123 |
||
124 |
/**
|
|
125 |
* ldm_language_matches:
|
|
126 |
* @language: A #LdmLanguage
|
|
127 |
* @code: A language code
|
|
128 |
*
|
|
129 |
* Check if a language code matches this language.
|
|
130 |
*
|
|
131 |
* Return value: TRUE if the code matches this language.
|
|
132 |
**/
|
|
133 |
gboolean
|
|
134 |
ldm_language_matches (LdmLanguage *language, const gchar *code) |
|
135 |
{
|
|
411
by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods |
136 |
g_return_val_if_fail (LDM_IS_LANGUAGE (language), FALSE); |
137 |
g_return_val_if_fail (code != NULL, FALSE); |
|
138 |
||
345
by robert.ancell at canonical
Correctly pass LANG and LANGUAGE through to the session |
139 |
/* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
|
140 |
if (is_utf8 (language->priv->code) && is_utf8 (code)) |
|
141 |
{
|
|
142 |
/* Match the characters before the '.' */
|
|
143 |
int i; |
|
144 |
for (i = 0; language->priv->code[i] && code[i] && language->priv->code[i] == code[i] && code[i] != '.' ; i++); |
|
145 |
return language->priv->code[i] == '.' && code[i] == '.'; |
|
146 |
}
|
|
147 |
||
148 |
return g_str_equal (language->priv->code, code); |
|
149 |
}
|
|
150 |
||
109
by robert.ancell at gmail
Add language information |
151 |
static void |
152 |
ldm_language_init (LdmLanguage *language) |
|
153 |
{
|
|
154 |
language->priv = G_TYPE_INSTANCE_GET_PRIVATE (language, LDM_TYPE_LANGUAGE, LdmLanguagePrivate); |
|
155 |
}
|
|
156 |
||
157 |
static void |
|
213
by Robert Ancell
whitespace |
158 |
ldm_language_set_property (GObject *object, |
159 |
guint prop_id, |
|
160 |
const GValue *value, |
|
161 |
GParamSpec *pspec) |
|
109
by robert.ancell at gmail
Add language information |
162 |
{
|
163 |
LdmLanguage *self; |
|
164 |
||
165 |
self = LDM_LANGUAGE (object); |
|
166 |
||
167 |
switch (prop_id) { |
|
168 |
case PROP_CODE: |
|
169 |
g_free (self->priv->name); |
|
170 |
self->priv->code = g_strdup (g_value_get_string (value)); |
|
171 |
break; |
|
172 |
default: |
|
173 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
174 |
break; |
|
175 |
}
|
|
176 |
}
|
|
177 |
||
178 |
static void |
|
213
by Robert Ancell
whitespace |
179 |
ldm_language_get_property (GObject *object, |
180 |
guint prop_id, |
|
181 |
GValue *value, |
|
182 |
GParamSpec *pspec) |
|
109
by robert.ancell at gmail
Add language information |
183 |
{
|
184 |
LdmLanguage *self; |
|
185 |
||
186 |
self = LDM_LANGUAGE (object); |
|
187 |
||
188 |
switch (prop_id) { |
|
189 |
case PROP_CODE: |
|
190 |
g_value_set_string (value, ldm_language_get_code (self)); |
|
191 |
break; |
|
192 |
case PROP_NAME: |
|
193 |
g_value_set_string (value, ldm_language_get_name (self)); |
|
194 |
break; |
|
195 |
case PROP_TERRITORY: |
|
196 |
g_value_set_string (value, ldm_language_get_territory (self)); |
|
197 |
break; |
|
198 |
default: |
|
199 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
200 |
break; |
|
201 |
}
|
|
202 |
}
|
|
203 |
||
204 |
static void |
|
205 |
ldm_language_class_init (LdmLanguageClass *klass) |
|
206 |
{
|
|
207 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
|
208 |
||
209 |
g_type_class_add_private (klass, sizeof (LdmLanguagePrivate)); |
|
210 |
||
211 |
object_class->set_property = ldm_language_set_property; |
|
212 |
object_class->get_property = ldm_language_get_property; |
|
213 |
||
214 |
g_object_class_install_property(object_class, |
|
215 |
PROP_CODE, |
|
216 |
g_param_spec_string("code", |
|
217 |
"code", |
|
218 |
"Language code", |
|
219 |
NULL, |
|
220 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
|
221 |
g_object_class_install_property(object_class, |
|
222 |
PROP_NAME, |
|
223 |
g_param_spec_string("name", |
|
224 |
"name", |
|
225 |
"Name of the language", |
|
226 |
NULL, |
|
227 |
G_PARAM_READABLE)); |
|
228 |
g_object_class_install_property(object_class, |
|
229 |
PROP_TERRITORY, |
|
230 |
g_param_spec_string("territory", |
|
231 |
"territory", |
|
232 |
"Territory the language is from", |
|
233 |
NULL, |
|
234 |
G_PARAM_READABLE)); |
|
235 |
}
|