summaryrefslogtreecommitdiff
path: root/gui/simple-greeter/gdm-session-option-widget.c
blob: 961ac943968f70694155553713074b4e70176f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Written by: William Jon McCann <mccann@jhu.edu>
 *             Ray Strode <rstrode@redhat.com>
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>

#include "gdm-session-option-widget.h"
#include "gdm-sessions.h"

#define GDM_SESSION_OPTION_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_SESSION_OPTION_WIDGET, GdmSessionOptionWidgetPrivate))

#define GDM_SESSION_OPTION_WIDGET_LAST_SESSION "__previous"

struct GdmSessionOptionWidgetPrivate
{
        gpointer dummy;
};

enum {
        PROP_0,
};

enum {
        SESSION_ACTIVATED,
        NUMBER_OF_SIGNALS
};

static guint signals [NUMBER_OF_SIGNALS] = { 0, };

static void     gdm_session_option_widget_class_init  (GdmSessionOptionWidgetClass *klass);
static void     gdm_session_option_widget_init        (GdmSessionOptionWidget      *session_option_widget);
static void     gdm_session_option_widget_finalize    (GObject                     *object);

G_DEFINE_TYPE (GdmSessionOptionWidget, gdm_session_option_widget, GDM_TYPE_OPTION_WIDGET)
static void
gdm_session_option_widget_activated (GdmOptionWidget *widget)
{
        char *active_item_id;

        active_item_id = gdm_option_widget_get_active_item (GDM_OPTION_WIDGET (widget));
        if (active_item_id == NULL) {
                return;
        }

        g_signal_emit (G_OBJECT (widget), signals[SESSION_ACTIVATED], 0);
}


static void
gdm_session_option_widget_class_init (GdmSessionOptionWidgetClass *klass)
{
        GObjectClass *object_class;
        GdmOptionWidgetClass *option_widget_class;

        object_class = G_OBJECT_CLASS (klass);
        option_widget_class = GDM_OPTION_WIDGET_CLASS (klass);

        object_class->finalize = gdm_session_option_widget_finalize;
        option_widget_class->activated = gdm_session_option_widget_activated;

        signals[SESSION_ACTIVATED] = g_signal_new ("session-activated",
                                                   G_TYPE_FROM_CLASS (object_class),
                                                   G_SIGNAL_RUN_FIRST,
                                                   G_STRUCT_OFFSET (GdmSessionOptionWidgetClass, session_activated),
                                                   NULL,
                                                   NULL,
                                                   g_cclosure_marshal_VOID__VOID,
                                                   G_TYPE_NONE,
                                                   0);

        g_type_class_add_private (klass, sizeof (GdmSessionOptionWidgetPrivate));
}

static void
add_available_sessions (GdmSessionOptionWidget *widget)
{
        char     **session_ids;
        int        i;

        session_ids = gdm_get_all_sessions ();

        for (i = 0; session_ids[i] != NULL; i++) {
                char *name;
                char *comment;

                if (!gdm_get_details_for_session (session_ids[i],
                                                  &name, &comment)) {
                        continue;
                }

                gdm_option_widget_add_item (GDM_OPTION_WIDGET (widget),
                                            session_ids[i], name, comment,
                                            GDM_OPTION_WIDGET_POSITION_MIDDLE);
                g_free (name);
                g_free (comment);
        }

        g_strfreev (session_ids);
}

static void
gdm_session_option_widget_init (GdmSessionOptionWidget *widget)
{
        widget->priv = GDM_SESSION_OPTION_WIDGET_GET_PRIVATE (widget);

        add_available_sessions (widget);
}

static void
gdm_session_option_widget_finalize (GObject *object)
{
        GdmSessionOptionWidget *session_option_widget;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GDM_IS_SESSION_OPTION_WIDGET (object));

        session_option_widget = GDM_SESSION_OPTION_WIDGET (object);

        g_return_if_fail (session_option_widget->priv != NULL);

        G_OBJECT_CLASS (gdm_session_option_widget_parent_class)->finalize (object);
}

GtkWidget *
gdm_session_option_widget_new (void)
{
        GObject *object;

        object = g_object_new (GDM_TYPE_SESSION_OPTION_WIDGET,
                               "label-text", _("_Sessions:"),
                               "icon-name", "session-properties",
                               NULL);

        return GTK_WIDGET (object);
}

char *
gdm_session_option_widget_get_current_session (GdmSessionOptionWidget *widget)
{
        char *active_item_id;

        active_item_id = gdm_option_widget_get_active_item (GDM_OPTION_WIDGET (widget));
        if (active_item_id == NULL) {
                return NULL;
        }

        return active_item_id;
}

void
gdm_session_option_widget_set_current_session (GdmSessionOptionWidget *widget,
                                               const char             *session)
{
        if (session == NULL) {
                gdm_option_widget_set_active_item (GDM_OPTION_WIDGET (widget),
                                                   GDM_SESSION_OPTION_WIDGET_LAST_SESSION);
        } else if (gdm_option_widget_lookup_item (GDM_OPTION_WIDGET (widget), session,
                                            NULL, NULL, NULL)) {
                gdm_option_widget_set_active_item (GDM_OPTION_WIDGET (widget), session);
        }
}