source: proiecte/PPPP/gdm/gui/simple-greeter/gdm-layout-option-widget.c @ 134

Last change on this file since 134 was 134, checked in by (none), 14 years ago

gdm sources with the modifications for webcam

File size: 10.6 KB
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008 Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Written by: Matthias Clasen <mclasen@redhat.com>
20 *
21 */
22
23#include "config.h"
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <string.h>
29#include <errno.h>
30#include <dirent.h>
31#include <sys/stat.h>
32
33#include <glib.h>
34#include <glib/gi18n.h>
35#include <glib/gstdio.h>
36#include <gtk/gtk.h>
37
38#include "gdm-profile.h"
39#include "gdm-layouts.h"
40#include "gdm-layout-option-widget.h"
41#include "gdm-recent-option-widget.h"
42#include "gdm-layout-chooser-dialog.h"
43
44#define GDM_LAYOUT_OPTION_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_LAYOUT_OPTION_WIDGET, GdmLayoutOptionWidgetPrivate))
45
46struct GdmLayoutOptionWidgetPrivate
47{
48        GtkWidget *dialog;
49};
50
51enum {
52        LAYOUT_ACTIVATED,
53        DIALOG_HIDDEN,
54        NUMBER_OF_SIGNALS
55};
56
57static guint signals [NUMBER_OF_SIGNALS] = { 0, };
58
59static void     gdm_layout_option_widget_class_init  (GdmLayoutOptionWidgetClass *klass);
60static void     gdm_layout_option_widget_init        (GdmLayoutOptionWidget      *layout_option_widget);
61static void     gdm_layout_option_widget_finalize    (GObject                     *object);
62static void     gdm_layout_option_widget_hide_dialog (GdmLayoutOptionWidget       *widget);
63
64G_DEFINE_TYPE (GdmLayoutOptionWidget, gdm_layout_option_widget, GDM_TYPE_RECENT_OPTION_WIDGET)
65
66static void
67gdm_layout_option_widget_set_layout_from_dialog (GdmLayoutOptionWidget *widget)
68{
69        char *layout_name;
70
71        layout_name = gdm_layout_chooser_dialog_get_current_layout_name (GDM_LAYOUT_CHOOSER_DIALOG (widget->priv->dialog));
72        g_debug ("GdmLayoutOptionWidget: Setting layout from dialog: '%s'", layout_name);
73
74        gdm_layout_option_widget_set_current_layout_name (widget, layout_name);
75        g_free (layout_name);
76}
77
78static void
79on_dialog_response (GtkDialog             *dialog,
80                    int                    response_id,
81                    GdmLayoutOptionWidget *widget)
82{
83        g_debug ("GdmLayoutOptionWidget: Got response from dialog: '%d'", response_id);
84
85        switch (response_id) {
86                case GTK_RESPONSE_OK:
87                        gdm_layout_option_widget_set_layout_from_dialog (widget);
88                        break;
89                default:
90                        break;
91        }
92
93        gdm_layout_option_widget_hide_dialog (widget);
94}
95
96static void
97gdm_layout_option_widget_hide_dialog (GdmLayoutOptionWidget *widget)
98{
99        gtk_widget_destroy (widget->priv->dialog);
100        widget->priv->dialog = NULL;
101        g_signal_emit (G_OBJECT (widget), signals [DIALOG_HIDDEN], 0);
102}
103
104static void
105create_dialog (GdmLayoutOptionWidget *widget)
106{
107        gdm_profile_start (NULL);
108
109        g_assert (widget->priv->dialog == NULL);
110
111        widget->priv->dialog = gdm_layout_chooser_dialog_new ();
112
113        gdm_profile_end (NULL);
114}
115
116static void
117gdm_layout_option_widget_show_dialog (GdmLayoutOptionWidget *widget,
118                                      const char            *active_item_id)
119{
120        if (widget->priv->dialog == NULL) {
121                create_dialog (widget);
122        }
123
124        g_signal_connect (GTK_DIALOG (widget->priv->dialog),
125                          "response",
126                          G_CALLBACK (on_dialog_response),
127                          widget);
128
129        gtk_widget_show_all (GTK_WIDGET (widget->priv->dialog));
130
131        gdm_layout_chooser_dialog_set_current_layout_name (GDM_LAYOUT_CHOOSER_DIALOG (GDM_LAYOUT_OPTION_WIDGET (widget)->priv->dialog),
132                                                           active_item_id);
133}
134
135static void
136gdm_layout_option_widget_activated (GdmOptionWidget *widget)
137{
138        char *active_item_id;
139
140        active_item_id = gdm_option_widget_get_active_item (GDM_OPTION_WIDGET (widget));
141        if (active_item_id == NULL) {
142                return;
143        }
144
145        if (strcmp (active_item_id, "__other") == 0) {
146                g_free (active_item_id);
147
148                active_item_id = gdm_option_widget_get_default_item (widget);
149                gdm_layout_option_widget_set_current_layout_name (GDM_LAYOUT_OPTION_WIDGET (widget), active_item_id);
150                gdm_layout_option_widget_show_dialog (GDM_LAYOUT_OPTION_WIDGET (widget), active_item_id);
151        }
152
153        g_signal_emit (G_OBJECT (widget), signals[LAYOUT_ACTIVATED], 0);
154
155        g_free (active_item_id);
156}
157
158static void
159gdm_layout_option_widget_class_init (GdmLayoutOptionWidgetClass *klass)
160{
161        GObjectClass         *object_class = G_OBJECT_CLASS (klass);
162        GdmOptionWidgetClass *option_widget_class = GDM_OPTION_WIDGET_CLASS (klass);
163
164        object_class->finalize = gdm_layout_option_widget_finalize;
165
166        option_widget_class->activated = gdm_layout_option_widget_activated;
167
168        signals[LAYOUT_ACTIVATED] = g_signal_new ("layout-activated",
169                                                  G_TYPE_FROM_CLASS (object_class),
170                                                  G_SIGNAL_RUN_FIRST,
171                                                  G_STRUCT_OFFSET (GdmLayoutOptionWidgetClass, layout_activated),
172                                                  NULL,
173                                                  NULL,
174                                                  g_cclosure_marshal_VOID__VOID,
175                                                  G_TYPE_NONE,
176                                                  0);
177
178        signals[DIALOG_HIDDEN] = g_signal_new ("dialog-hidden",
179                                               G_TYPE_FROM_CLASS (object_class),
180                                               G_SIGNAL_RUN_FIRST,
181                                               G_STRUCT_OFFSET (GdmLayoutOptionWidgetClass, dialog_hidden),
182                                               NULL,
183                                               NULL,
184                                               g_cclosure_marshal_VOID__VOID,
185                                               G_TYPE_NONE,
186                                               0);
187
188        g_type_class_add_private (klass, sizeof (GdmLayoutOptionWidgetPrivate));
189}
190
191static char *
192gdm_layout_option_widget_lookup_item (GdmRecentOptionWidget *widget,
193                                      const char            *key,
194                                      char                 **name,
195                                      char                 **comment)
196{
197        char *layout;
198
199        layout = gdm_get_layout_from_name (key);
200
201        if (layout == NULL) {
202                return NULL;
203        }
204
205        *name = layout;
206        *comment = NULL;
207
208        return g_strdup (key);
209}
210
211static void
212gdm_layout_option_widget_init (GdmLayoutOptionWidget *widget)
213{
214        GError *error;
215
216        widget->priv = GDM_LAYOUT_OPTION_WIDGET_GET_PRIVATE (widget);
217
218        error = NULL;
219        gdm_recent_option_widget_set_gconf_key (GDM_RECENT_OPTION_WIDGET (widget),
220                                                "/apps/gdm/simple-greeter/recent-layouts",
221                                                gdm_layout_option_widget_lookup_item,
222                                                &error);
223
224        if (error != NULL) {
225                g_warning ("Could not read recent layouts from gconf: %s",
226                           error->message);
227                g_error_free (error);
228        }
229
230        gdm_option_widget_add_item (GDM_OPTION_WIDGET (widget),
231                                    "__other",
232                                    /* translators: This brings up a dialog of
233                                     * available keyboard layouts
234                                     */
235                                    C_("keyboard", "Other..."),
236                                    _("Choose a keyboard layout from the "
237                                      "full list of available layouts."),
238                                    GDM_OPTION_WIDGET_POSITION_BOTTOM);
239}
240
241static void
242gdm_layout_option_widget_finalize (GObject *object)
243{
244        GdmLayoutOptionWidget *layout_option_widget;
245
246        g_return_if_fail (object != NULL);
247        g_return_if_fail (GDM_IS_LAYOUT_OPTION_WIDGET (object));
248
249        layout_option_widget = GDM_LAYOUT_OPTION_WIDGET (object);
250
251        g_return_if_fail (layout_option_widget->priv != NULL);
252
253        if (layout_option_widget->priv->dialog != NULL) {
254                gtk_widget_destroy (layout_option_widget->priv->dialog);
255        }
256
257        G_OBJECT_CLASS (gdm_layout_option_widget_parent_class)->finalize (object);
258}
259
260GtkWidget *
261gdm_layout_option_widget_new (void)
262{
263        GObject *object;
264
265        object = g_object_new (GDM_TYPE_LAYOUT_OPTION_WIDGET,
266                               "label-text", _("_Keyboard:"),
267                               "icon-name", "preferences-desktop-keyboard",
268                               "max-item-count", 8,
269                               NULL);
270
271        return GTK_WIDGET (object);
272}
273
274char *
275gdm_layout_option_widget_get_current_layout_name (GdmLayoutOptionWidget *widget)
276{
277        char *active_item_id;
278
279        active_item_id = gdm_option_widget_get_active_item (GDM_OPTION_WIDGET (widget));
280        if (active_item_id == NULL) {
281                return NULL;
282        }
283
284        if (strcmp (active_item_id, "__other") == 0) {
285                g_free (active_item_id);
286                return NULL;
287        }
288
289        return active_item_id;
290}
291
292void
293gdm_layout_option_widget_set_current_layout_name (GdmLayoutOptionWidget *widget,
294                                                  const char            *id)
295{
296        g_return_if_fail (GDM_IS_LAYOUT_OPTION_WIDGET (widget));
297
298        if (id != NULL &&
299            !gdm_option_widget_lookup_item (GDM_OPTION_WIDGET (widget),
300                                            id, NULL, NULL, NULL)) {
301                gdm_recent_option_widget_add_item (GDM_RECENT_OPTION_WIDGET (widget),
302                                                   id);
303        }
304        g_debug ("GdmLayoutOptionWidget: Setting active item: '%s'", id);
305        gdm_option_widget_set_active_item (GDM_OPTION_WIDGET (widget), id);
306}
Note: See TracBrowser for help on using the repository browser.