]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/language.c
Improve liblightdm-gobject documentation
[sojka/lightdm.git] / liblightdm-gobject / language.c
1 /*
2  * Copyright (C) 2010 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
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
9  * license.
10  */
11
12 #include <locale.h>
13 #include <langinfo.h>
14
15 #include "lightdm/language.h"
16
17 enum {
18     PROP_0,
19     PROP_CODE,
20     PROP_NAME,
21     PROP_TERRITORY
22 };
23
24 typedef struct
25 {
26     gchar *code;
27     gchar *name;
28     gchar *territory;
29 } LdmLanguagePrivate;
30
31 G_DEFINE_TYPE (LdmLanguage, ldm_language, G_TYPE_OBJECT);
32
33 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LDM_TYPE_LANGUAGE, LdmLanguagePrivate)
34
35 /**
36  * ldm_language_new:
37  * 
38  * Create a new language.
39  * @code: The language code
40  * 
41  * Return value: the new #LdmLanguage
42  **/
43 LdmLanguage *
44 ldm_language_new (const gchar *code)
45 {
46     return g_object_new (LDM_TYPE_LANGUAGE, "code", code, NULL);
47 }
48
49 /**
50  * ldm_language_get_code:
51  * @language: A #LdmLanguage
52  * 
53  * Get the code of a language.
54  * 
55  * Return value: The code of the language
56  **/
57 const gchar *
58 ldm_language_get_code (LdmLanguage *language)
59 {
60     g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);
61     return GET_PRIVATE (language)->code;
62 }
63
64 /**
65  * ldm_language_get_name:
66  * @language: A #LdmLanguage
67  * 
68  * Get the name of a language.
69  *
70  * Return value: The name of the language
71  **/
72 const gchar *
73 ldm_language_get_name (LdmLanguage *language)
74 {
75     LdmLanguagePrivate *priv;
76
77     g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);
78
79     priv = GET_PRIVATE (language);
80
81     if (!priv->name)
82     {
83         char *current = setlocale(LC_ALL, NULL);
84         setlocale(LC_ALL, priv->code);
85 #ifdef _NL_IDENTIFICATION_LANGUAGE
86         priv->name = g_strdup (nl_langinfo (_NL_IDENTIFICATION_LANGUAGE));
87 #else
88         priv->name = g_strdup ("Unknown");
89 #endif
90         setlocale(LC_ALL, current);
91     }
92
93     return priv->name;
94 }
95
96 /**
97  * ldm_language_get_territory:
98  * @language: A #LdmLanguage
99  * 
100  * Get the territory the language is used in.
101  * 
102  * Return value: The territory the language is used in.
103  **/
104 const gchar *
105 ldm_language_get_territory (LdmLanguage *language)
106 {
107     LdmLanguagePrivate *priv;
108
109     g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);
110
111     priv = GET_PRIVATE (language);
112
113     if (!priv->territory)
114     {
115         char *current = setlocale(LC_ALL, NULL);
116         setlocale(LC_ALL, priv->code);
117 #ifdef _NL_IDENTIFICATION_TERRITORY
118         priv->territory = g_strdup (nl_langinfo (_NL_IDENTIFICATION_TERRITORY));
119 #else
120         priv->territory = g_strdup ("Unknown");
121 #endif
122         setlocale(LC_ALL, current);
123     }
124
125     return priv->territory;
126 }
127
128 static gboolean
129 is_utf8 (const gchar *code)
130 {
131     return g_str_has_suffix (code, ".utf8") || g_str_has_suffix (code, ".UTF-8");
132 }
133
134 /**
135  * ldm_language_matches:
136  * @language: A #LdmLanguage
137  * @code: A language code
138  * 
139  * Check if a language code matches this language.
140  * 
141  * Return value: #TRUE if the code matches this language.
142  **/
143 gboolean
144 ldm_language_matches (LdmLanguage *language, const gchar *code)
145 {
146     LdmLanguagePrivate *priv;
147
148     g_return_val_if_fail (LDM_IS_LANGUAGE (language), FALSE);
149     g_return_val_if_fail (code != NULL, FALSE);
150
151     priv = GET_PRIVATE (language);
152
153     /* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
154     if (is_utf8 (priv->code) && is_utf8 (code))
155     {
156         /* Match the characters before the '.' */
157         int i;
158         for (i = 0; priv->code[i] && code[i] && priv->code[i] == code[i] && code[i] != '.' ; i++);
159         return priv->code[i] == '.' && code[i] == '.';
160     }
161
162     return g_str_equal (priv->code, code);
163 }
164
165 static void
166 ldm_language_init (LdmLanguage *language)
167 {
168 }
169
170 static void
171 ldm_language_set_property (GObject      *object,
172                            guint         prop_id,
173                            const GValue *value,
174                            GParamSpec   *pspec)
175 {
176     LdmLanguage *self = LDM_LANGUAGE (object);
177     LdmLanguagePrivate *priv = GET_PRIVATE (self);
178
179     switch (prop_id) {
180     case PROP_CODE:
181         g_free (priv->name);
182         priv->code = g_strdup (g_value_get_string (value));
183         break;
184     default:
185         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186         break;
187     }
188 }
189
190 static void
191 ldm_language_get_property (GObject    *object,
192                            guint       prop_id,
193                            GValue     *value,
194                            GParamSpec *pspec)
195 {
196     LdmLanguage *self;
197
198     self = LDM_LANGUAGE (object);
199
200     switch (prop_id) {
201     case PROP_CODE:
202         g_value_set_string (value, ldm_language_get_code (self));
203         break;
204     case PROP_NAME:
205         g_value_set_string (value, ldm_language_get_name (self));
206         break;
207     case PROP_TERRITORY:
208         g_value_set_string (value, ldm_language_get_territory (self));
209         break;
210     default:
211         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212         break;
213     }
214 }
215
216 static void
217 ldm_language_class_init (LdmLanguageClass *klass)
218 {
219     GObjectClass *object_class = G_OBJECT_CLASS (klass);
220   
221     g_type_class_add_private (klass, sizeof (LdmLanguagePrivate));
222
223     object_class->set_property = ldm_language_set_property;
224     object_class->get_property = ldm_language_get_property;
225
226     g_object_class_install_property(object_class,
227                                     PROP_CODE,
228                                     g_param_spec_string("code",
229                                                         "code",
230                                                         "Language code",
231                                                         NULL,
232                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
233     g_object_class_install_property(object_class,
234                                     PROP_NAME,
235                                     g_param_spec_string("name",
236                                                         "name",
237                                                         "Name of the language",
238                                                         NULL,
239                                                         G_PARAM_READABLE));
240     g_object_class_install_property(object_class,
241                                     PROP_TERRITORY,
242                                     g_param_spec_string("territory",
243                                                         "territory",
244                                                         "Territory the language is from",
245                                                         NULL,
246                                                         G_PARAM_READABLE));
247 }