]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/layout.c
Return the user face as a QIcon rather than a QPixmap
[sojka/lightdm.git] / liblightdm-gobject / layout.c
1 /* -*- Mode: C; indent-tabs-mode:nil; tab-width:4 -*-
2  *
3  * Copyright (C) 2010 Robert Ancell.
4  * Author: Robert Ancell <robert.ancell@canonical.com>
5  * 
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 3 of the License, or (at your option) any
9  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
10  * license.
11  */
12
13 #include <libxklavier/xklavier.h>
14
15 #include "lightdm/layout.h"
16
17 enum {
18     PROP_0,
19     PROP_NAME,
20     PROP_SHORT_DESCRIPTION,
21     PROP_DESCRIPTION
22 };
23
24 typedef struct
25 {
26     gchar *name;
27     gchar *short_description;
28     gchar *description;
29 } LightDMLayoutPrivate;
30
31 G_DEFINE_TYPE (LightDMLayout, lightdm_layout, G_TYPE_OBJECT);
32
33 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LAYOUT, LightDMLayoutPrivate)
34
35 static gboolean have_layouts = FALSE;
36 static Display *display = NULL;
37 static XklEngine *xkl_engine = NULL;
38 static XklConfigRec *xkl_config = NULL;
39 static GList *layouts = NULL;
40 static LightDMLayout *default_layout = NULL;
41
42 static gchar *
43 make_layout_string (const gchar *layout, const gchar *variant)
44 {
45     if (!layout || layout[0] == 0)
46         return NULL;
47     else if (!variant || variant[0] == 0)
48         return g_strdup (layout);
49     else
50         return g_strdup_printf ("%s\t%s", layout, variant);
51 }
52
53 static void
54 parse_layout_string (const gchar *name, gchar **layout, gchar **variant)
55 {
56     gchar **split;
57
58     *layout = NULL;
59     *variant = NULL;
60
61     if (!name)
62         return;
63
64     split = g_strsplit (name, "\t", 2);
65     if (split[0])
66     {
67         *layout = g_strdup (split[0]);
68         if (split[1])
69             *variant = g_strdup (split[1]);
70     }
71     g_strfreev (split);
72 }
73
74 static void
75 variant_cb (XklConfigRegistry *config,
76            const XklConfigItem *item,
77            gpointer data)
78 {
79     LightDMLayout *layout;
80     gchar *full_name;
81
82     full_name = make_layout_string (data, item->name);
83
84     layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", full_name, "short-description", item->short_description, "description", item->description, NULL);
85     layouts = g_list_append (layouts, layout);
86
87     g_free (full_name);
88 }
89
90 static void
91 layout_cb (XklConfigRegistry *config,
92            const XklConfigItem *item,
93            gpointer data)
94 {
95     LightDMLayout *layout;
96
97     layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", item->name, "short-description", item->short_description, "description", item->description, NULL);
98     layouts = g_list_append (layouts, layout);
99
100     xkl_config_registry_foreach_layout_variant (config, item->name, variant_cb, (gpointer) item->name);
101 }
102
103 /**
104  * lightdm_get_layouts:
105  *
106  * Get a list of keyboard layouts to present to the user.
107  *
108  * Return value: (element-type LightDMLayout) (transfer none): A list of #LightDMLayout that should be presented to the user.
109  **/
110 GList *
111 lightdm_get_layouts (void)
112 {
113     XklConfigRegistry *registry;
114
115     if (have_layouts)
116         return layouts;
117
118     display = XOpenDisplay (NULL);
119     xkl_engine = xkl_engine_get_instance (display);
120     xkl_config = xkl_config_rec_new ();
121     if (!xkl_config_rec_get_from_server (xkl_config, xkl_engine))
122         g_warning ("Failed to get Xkl configuration from server");
123
124     registry = xkl_config_registry_get_instance (xkl_engine);
125     xkl_config_registry_load (registry, FALSE);
126     xkl_config_registry_foreach_layout (registry, layout_cb, NULL);
127     g_object_unref (registry);
128
129     have_layouts = TRUE;
130
131     return layouts;
132 }
133
134 /**
135  * lightdm_set_layout:
136  * @layout: The layout to use
137  *
138  * Set the layout for this session.
139  **/
140 void
141 lightdm_set_layout (LightDMLayout *dmlayout)
142 {
143     XklConfigRec *config;
144     gchar *layout, *variant;
145
146     g_return_if_fail (dmlayout != NULL);
147
148     g_debug ("Setting keyboard layout to '%s'", lightdm_layout_get_name (dmlayout));
149
150     parse_layout_string (lightdm_layout_get_name (dmlayout), &layout, &variant);
151
152     config = xkl_config_rec_new ();
153     config->layouts = g_malloc (sizeof (gchar *) * 2);
154     config->variants = g_malloc (sizeof (gchar *) * 2);
155     config->model = g_strdup (xkl_config->model);
156     config->layouts[0] = layout;
157     config->layouts[1] = NULL;
158     config->variants[0] = variant;
159     config->variants[1] = NULL;
160     if (!xkl_config_rec_activate (config, xkl_engine))
161         g_warning ("Failed to activate XKL config");
162     g_object_unref (config);
163 }
164
165 /**
166  * lightdm_get_layout:
167  *
168  * Get the current keyboard layout.
169  *
170  * Return value: (transfer none): The currently active layout for this user.
171  **/
172 LightDMLayout *
173 lightdm_get_layout (void)
174 {
175     lightdm_get_layouts ();
176
177     if (layouts && xkl_config && !default_layout)
178     {
179         gchar *full_name;
180         GList *item;
181
182         full_name = make_layout_string (xkl_config->layouts ? xkl_config->layouts[0] : NULL,
183                                         xkl_config->variants ? xkl_config->variants[0] : NULL);
184
185         for (item = layouts; item; item = item->next)
186         {
187             LightDMLayout *iter_layout = (LightDMLayout *) item->data;
188             if (g_strcmp0 (lightdm_layout_get_name (iter_layout), full_name) == 0)
189             {
190                 default_layout = iter_layout;
191                 break;
192             }
193         }
194
195         g_free (full_name);
196     }
197
198     return default_layout;
199 }
200
201 /**
202  * lightdm_layout_get_name:
203  * @layout: A #LightDMLayout
204  * 
205  * Get the name of a layout.
206  * 
207  * Return value: The name of the layout
208  **/
209 const gchar *
210 lightdm_layout_get_name (LightDMLayout *layout)
211 {
212     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
213     return GET_PRIVATE (layout)->name;
214 }
215
216 /**
217  * lightdm_layout_get_short_description:
218  * @layout: A #LightDMLayout
219  * 
220  * Get the short description of a layout.
221  *
222  * Return value: A short description of the layout
223  **/
224 const gchar *
225 lightdm_layout_get_short_description (LightDMLayout *layout)
226 {
227     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
228     return GET_PRIVATE (layout)->short_description;
229 }
230
231 /**
232  * lightdm_layout_get_description:
233  * @layout: A #LightDMLayout
234  * 
235  * Get the long description of a layout.
236  * 
237  * Return value: A long description of the layout
238  **/
239 const gchar *
240 lightdm_layout_get_description (LightDMLayout *layout)
241 {
242     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
243     return GET_PRIVATE (layout)->description;
244 }
245
246 static void
247 lightdm_layout_init (LightDMLayout *layout)
248 {
249 }
250
251 static void
252 lightdm_layout_set_property (GObject      *object,
253                          guint         prop_id,
254                          const GValue *value,
255                          GParamSpec   *pspec)
256 {
257     LightDMLayout *self = LIGHTDM_LAYOUT (object);
258     LightDMLayoutPrivate *priv = GET_PRIVATE (self);
259
260     switch (prop_id) {
261     case PROP_NAME:
262         g_free (priv->name);
263         priv->name = g_strdup (g_value_get_string (value));
264         break;
265     case PROP_SHORT_DESCRIPTION:
266         g_free (priv->short_description);
267         priv->short_description = g_strdup (g_value_get_string (value));
268         break;
269     case PROP_DESCRIPTION:
270         g_free (priv->description);
271         priv->description = g_strdup (g_value_get_string (value));
272         break;
273     default:
274         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275         break;
276     }
277 }
278
279 static void
280 lightdm_layout_get_property (GObject    *object,
281                          guint       prop_id,
282                          GValue     *value,
283                          GParamSpec *pspec)
284 {
285     LightDMLayout *self;
286
287     self = LIGHTDM_LAYOUT (object);
288
289     switch (prop_id) {
290     case PROP_NAME:
291         g_value_set_string (value, lightdm_layout_get_name (self));
292         break;
293     case PROP_SHORT_DESCRIPTION:
294         g_value_set_string (value, lightdm_layout_get_short_description (self));
295         break;
296     case PROP_DESCRIPTION:
297         g_value_set_string (value, lightdm_layout_get_description (self));
298         break;
299     default:
300         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301         break;
302     }
303 }
304
305 static void
306 lightdm_layout_class_init (LightDMLayoutClass *klass)
307 {
308     GObjectClass *object_class = G_OBJECT_CLASS (klass);
309   
310     g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
311
312     object_class->set_property = lightdm_layout_set_property;
313     object_class->get_property = lightdm_layout_get_property;
314
315     g_object_class_install_property (object_class,
316                                      PROP_NAME,
317                                      g_param_spec_string ("name",
318                                                           "name",
319                                                           "Name of the layout",
320                                                           NULL,
321                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
322     g_object_class_install_property (object_class,
323                                      PROP_SHORT_DESCRIPTION,
324                                      g_param_spec_string ("short-description",
325                                                           "short-description",
326                                                           "Short description of the layout",
327                                                           NULL,
328                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
329     g_object_class_install_property (object_class,
330                                      PROP_DESCRIPTION,
331                                      g_param_spec_string ("description",
332                                                           "description",
333                                                           "Long description of the layout",
334                                                           NULL,
335                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
336 }