]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - libldmgreeter-gobject/language.c
libldmgreeter -> libldmgreeter-gobject
[sojka/lightdm.git] / libldmgreeter-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 "language.h"
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 {
58     return language->priv->code;
59 }
60
61 /**
62  * ldm_language_get_name:
63  * @language: A #LdmLanguage
64  * 
65  * Get the name of a language.
66  *
67  * Return value: The name of the language
68  **/
69 const gchar *
70 ldm_language_get_name (LdmLanguage *language)
71 {
72     if (!language->priv->name)
73     {
74         char *current = setlocale(LC_ALL, NULL);
75         setlocale(LC_ALL, language->priv->code);
76         language->priv->name = g_strdup (nl_langinfo (_NL_IDENTIFICATION_LANGUAGE));
77         setlocale(LC_ALL, current);
78     }
79
80     return language->priv->name;
81 }
82
83 /**
84  * ldm_language_get_territory:
85  * @language: A #LdmLanguage
86  * 
87  * Get the territory the language is used in.
88  * 
89  * Return value: The territory the language is used in.
90  **/
91 const gchar *
92 ldm_language_get_territory (LdmLanguage *language)
93 {
94     if (!language->priv->territory)
95     {
96         char *current = setlocale(LC_ALL, NULL);
97         setlocale(LC_ALL, language->priv->code);
98         language->priv->territory = g_strdup (nl_langinfo (_NL_IDENTIFICATION_TERRITORY));
99         setlocale(LC_ALL, current);
100     }
101
102     return language->priv->territory;
103 }
104
105 static void
106 ldm_language_init (LdmLanguage *language)
107 {
108     language->priv = G_TYPE_INSTANCE_GET_PRIVATE (language, LDM_TYPE_LANGUAGE, LdmLanguagePrivate);
109 }
110
111 static void
112 ldm_language_set_property (GObject      *object,
113                            guint         prop_id,
114                            const GValue *value,
115                            GParamSpec   *pspec)
116 {
117     LdmLanguage *self;
118     gint i, n_pages;
119
120     self = LDM_LANGUAGE (object);
121
122     switch (prop_id) {
123     case PROP_CODE:
124         g_free (self->priv->name);
125         self->priv->code = g_strdup (g_value_get_string (value));
126         break;
127     default:
128         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129         break;
130     }
131 }
132
133 static void
134 ldm_language_get_property (GObject    *object,
135                            guint       prop_id,
136                            GValue     *value,
137                            GParamSpec *pspec)
138 {
139     LdmLanguage *self;
140
141     self = LDM_LANGUAGE (object);
142
143     switch (prop_id) {
144     case PROP_CODE:
145         g_value_set_string (value, ldm_language_get_code (self));
146         break;
147     case PROP_NAME:
148         g_value_set_string (value, ldm_language_get_name (self));
149         break;
150     case PROP_TERRITORY:
151         g_value_set_string (value, ldm_language_get_territory (self));
152         break;
153     default:
154         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155         break;
156     }
157 }
158
159 static void
160 ldm_language_class_init (LdmLanguageClass *klass)
161 {
162     GObjectClass *object_class = G_OBJECT_CLASS (klass);
163   
164     g_type_class_add_private (klass, sizeof (LdmLanguagePrivate));
165
166     object_class->set_property = ldm_language_set_property;
167     object_class->get_property = ldm_language_get_property;
168
169     g_object_class_install_property(object_class,
170                                     PROP_CODE,
171                                     g_param_spec_string("code",
172                                                         "code",
173                                                         "Language code",
174                                                         NULL,
175                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
176     g_object_class_install_property(object_class,
177                                     PROP_NAME,
178                                     g_param_spec_string("name",
179                                                         "name",
180                                                         "Name of the language",
181                                                         NULL,
182                                                         G_PARAM_READABLE));
183     g_object_class_install_property(object_class,
184                                     PROP_TERRITORY,
185                                     g_param_spec_string("territory",
186                                                         "territory",
187                                                         "Territory the language is from",
188                                                         NULL,
189                                                         G_PARAM_READABLE));
190 }