aboutsummaryrefslogtreecommitdiff
path: root/src/backend-dbus/guest.c
blob: a1358c522574578086fff3cbb7418730fbc916f6 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include "guest.h"

struct _IndicatorSessionGuestDbusPrivate
{
  GCancellable * cancellable;

  Login1Manager * login1_manager;
  Login1Seat * login1_seat;
  DisplayManagerSeat * dm_seat;

  gboolean guest_is_active;
  gboolean guest_is_logged_in;
  gboolean guest_is_allowed;
};

typedef IndicatorSessionGuestDbusPrivate priv_t;

G_DEFINE_TYPE_WITH_PRIVATE (IndicatorSessionGuestDbus, indicator_session_guest_dbus, INDICATOR_TYPE_SESSION_GUEST)

/***
****
***/

static void
set_guest_is_allowed_flag (IndicatorSessionGuestDbus * self, gboolean b)
{
  priv_t * p = self->priv;

  if (p->guest_is_allowed != b)
    {
      p->guest_is_allowed = b;

      indicator_session_guest_notify_allowed (INDICATOR_SESSION_GUEST (self));
    }
}

static void
set_guest_is_logged_in_flag (IndicatorSessionGuestDbus * self, gboolean b)
{
  priv_t * p = self->priv;

  if (p->guest_is_logged_in != b)
    {
      p->guest_is_logged_in = b;

      indicator_session_guest_notify_logged_in (INDICATOR_SESSION_GUEST (self));
    }
}

static void
set_guest_is_active_flag (IndicatorSessionGuestDbus * self, gboolean b)
{
  priv_t * p = self->priv;

  if (p->guest_is_active != b)
    {
      p->guest_is_active = b;

      indicator_session_guest_notify_active (INDICATOR_SESSION_GUEST(self));
    }
}

/***
****
***/

/* walk the sessions to see if guest is logged in or active */
static void
on_login1_manager_session_list_ready (GObject      * o,
                                      GAsyncResult * res,
                                      gpointer       gself)
{
  GVariant * sessions;
  GError * err;

  sessions = NULL;
  err = NULL;
  login1_manager_call_list_sessions_finish (LOGIN1_MANAGER(o),
                                            &sessions,
                                            res,
                                            &err);
  if (err != NULL)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s: %s", G_STRFUNC, err->message);

      g_error_free (err);
    }
  else
    {
      const gchar * const current_seat_id = g_getenv ("XDG_SEAT");
      const gchar * const current_session_id = g_getenv ("XDG_SESSION_ID");
      gboolean is_logged_in = FALSE;
      gboolean is_active = FALSE;
      const gchar * session_id = NULL;
      guint32 uid = 0;
      const gchar * user_name = NULL;
      const gchar * seat_id = NULL;
      GVariantIter iter;

      g_variant_iter_init (&iter, sessions);
      while (g_variant_iter_loop (&iter, "(&su&s&so)", &session_id,
                                                        &uid,
                                                        &user_name,
                                                        &seat_id,
                                                        NULL))
        {
          gboolean is_current_session;
          gboolean is_guest;

          is_current_session = !g_strcmp0 (current_seat_id, seat_id)
                            && !g_strcmp0 (current_session_id, session_id);

          is_guest = g_str_has_prefix (user_name, "guest-")
                  && (uid < 1000);

          if (is_guest)
            {
              is_logged_in = TRUE;

              is_active = is_current_session;
            }
        }

      set_guest_is_logged_in_flag (gself, is_logged_in);
      set_guest_is_active_flag (gself, is_active);

      g_variant_unref (sessions);
    }
}

static void
update_session_list (IndicatorSessionGuestDbus * self)
{
  priv_t * p = self->priv;

  if (p->login1_manager != NULL)
    {
      login1_manager_call_list_sessions (p->login1_manager,
                                         p->cancellable,
                                         on_login1_manager_session_list_ready,
                                         self);
    }
}

static void
set_login1_manager (IndicatorSessionGuestDbus * self,
                    Login1Manager             * login1_manager)
{
  priv_t * p = self->priv;

  if (p->login1_manager != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->login1_manager, self);

      g_clear_object (&p->login1_manager);
    }

  if (login1_manager != NULL)
    {
      p->login1_manager = g_object_ref (login1_manager);

      g_signal_connect_swapped (login1_manager, "session-new",
                                G_CALLBACK(update_session_list), self);
      g_signal_connect_swapped (login1_manager, "session-removed",
                                G_CALLBACK(update_session_list), self);
      update_session_list (self);
    }
}

static void
set_login1_seat (IndicatorSessionGuestDbus * self,
                 Login1Seat                * login1_seat)
{
  priv_t * p = self->priv;

  if (p->login1_seat != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->login1_seat, self);
      g_clear_object (&p->login1_seat);
    }

  if (login1_seat != NULL)
    {
      p->login1_seat = g_object_ref (login1_seat);

      g_signal_connect_swapped (login1_seat, "notify::active-session",
                                G_CALLBACK(update_session_list), self);
      update_session_list (self);
    }
}

/***
****
***/

static void
on_switch_to_guest_done (GObject      * o,
                         GAsyncResult * res,
                         gpointer       unused G_GNUC_UNUSED)
{
  GError * err;

  err = NULL;
  display_manager_seat_call_switch_to_guest_finish (DISPLAY_MANAGER_SEAT(o),
                                                    res,
                                                    &err);
  if (err != NULL)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, err->message);

      g_error_free (err);
    }
}

static void
my_switch_to_guest (IndicatorSessionGuest * guest)
{
  IndicatorSessionGuestDbus * self = INDICATOR_SESSION_GUEST_DBUS(guest);

  g_return_if_fail (self != NULL);
  g_return_if_fail (self->priv->dm_seat != NULL);

  display_manager_seat_call_switch_to_guest (self->priv->dm_seat,
                                             "",
                                             self->priv->cancellable,
                                             on_switch_to_guest_done,
                                             self);
}

static void
on_notify_has_guest_account (DisplayManagerSeat        * dm_seat,
                             GParamSpec                * pspec G_GNUC_UNUSED,
                             IndicatorSessionGuestDbus * self)
{
  gboolean guest_exists = display_manager_seat_get_has_guest_account (dm_seat);
  set_guest_is_allowed_flag (self, guest_exists);
}

static void
set_display_manager_seat (IndicatorSessionGuestDbus * self,
                          DisplayManagerSeat        * dm_seat)
{
  priv_t * p = self->priv;

  if (p->dm_seat != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->dm_seat, self);

      g_clear_object (&p->dm_seat);
    }

  if (dm_seat != NULL)
    {
      p->dm_seat = g_object_ref (dm_seat);

      g_signal_connect (dm_seat, "notify::has-guest-account",
                        G_CALLBACK(on_notify_has_guest_account), self);
      on_notify_has_guest_account (dm_seat, NULL, self);
    }
}

/***
****  IndiatorSessionGuest Virtual Functions
***/

static gboolean
my_is_allowed (IndicatorSessionGuest * self)
{
  g_return_val_if_fail (INDICATOR_IS_SESSION_GUEST_DBUS(self), FALSE);

  return INDICATOR_SESSION_GUEST_DBUS(self)->priv->guest_is_allowed;
}

static gboolean
my_is_logged_in (IndicatorSessionGuest * self)
{
  g_return_val_if_fail (INDICATOR_IS_SESSION_GUEST_DBUS(self), FALSE);

  return INDICATOR_SESSION_GUEST_DBUS(self)->priv->guest_is_logged_in;
}

static gboolean
my_is_active (IndicatorSessionGuest * self)
{
  g_return_val_if_fail (INDICATOR_IS_SESSION_GUEST_DBUS(self), FALSE);

  return INDICATOR_SESSION_GUEST_DBUS(self)->priv->guest_is_active;
}

/***
****  GObject Virtual Functions
***/

static void
my_dispose (GObject * o)
{
  IndicatorSessionGuestDbus * self = INDICATOR_SESSION_GUEST_DBUS (o);

  if (self->priv->cancellable != NULL)
    {
      g_cancellable_cancel (self->priv->cancellable);
      g_clear_object (&self->priv->cancellable);
    }

  set_login1_seat (self, NULL);
  set_login1_manager (self, NULL);
  set_display_manager_seat (self, NULL);

  G_OBJECT_CLASS (indicator_session_guest_dbus_parent_class)->dispose (o);
}

/***
****  Instantiation
***/

static void
indicator_session_guest_dbus_class_init (IndicatorSessionGuestDbusClass * klass)
{
  GObjectClass * object_class;
  IndicatorSessionGuestClass * guest_class;

  object_class = G_OBJECT_CLASS (klass);
  object_class->dispose = my_dispose;

  guest_class = INDICATOR_SESSION_GUEST_CLASS (klass);
  guest_class->is_allowed = my_is_allowed;
  guest_class->is_logged_in = my_is_logged_in;
  guest_class->is_active = my_is_active;
  guest_class->switch_to_guest = my_switch_to_guest;
}

static void
indicator_session_guest_dbus_init (IndicatorSessionGuestDbus * self)
{
  priv_t * p;

  p = indicator_session_guest_dbus_get_instance_private (self);
  p->cancellable = g_cancellable_new ();
  self->priv = p;
}

/***
****  Public
***/

IndicatorSessionGuest *
indicator_session_guest_dbus_new (void)
{
  gpointer o = g_object_new (INDICATOR_TYPE_SESSION_GUEST_DBUS, NULL);

  return INDICATOR_SESSION_GUEST (o);
}

void
indicator_session_guest_dbus_set_proxies (IndicatorSessionGuestDbus * self,
                                          Login1Manager             * login1_manager,
                                          Login1Seat                * login1_seat,
                                          DisplayManagerSeat        * dm_seat)
{
  g_return_if_fail (INDICATOR_IS_SESSION_GUEST_DBUS(self));

  set_login1_manager (self, login1_manager);
  set_login1_seat (self, login1_seat);
  set_display_manager_seat (self, dm_seat);
}