aboutsummaryrefslogtreecommitdiff
path: root/src/backend-dbus/utils.c
blob: 25ac7c3aae1e1b52848e71f6577d18ffb388fe18 (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
/*
 * 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 "utils.h"

/***
**** indicator_session_util_get_session_proxies()
***/

struct session_proxy_data
{
  Login1Manager * login1_manager;
  Login1Seat * login1_seat;
  DisplayManagerSeat * dm_seat;
  Accounts * account_manager;

  GCancellable * cancellable;
  int pending;

  indicator_session_util_session_proxies_func callback;
  gpointer user_data;
};


static void
on_proxy_ready_impl (struct session_proxy_data * data,
                     gsize                       member_offset,
                     GError                    * err,
                     gpointer                    proxy)
{
  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);
    }
  else
    {
      *((gpointer*)G_STRUCT_MEMBER_P(data, member_offset)) = proxy;
    }

  if (!--data->pending)
    {
      data->callback (data->login1_manager,
                      data->login1_seat,
                      data->dm_seat,
                      data->account_manager,
                      data->cancellable,
                      data->user_data);

      g_clear_object (&data->login1_manager);
      g_clear_object (&data->login1_seat);
      g_clear_object (&data->dm_seat);
      g_clear_object (&data->account_manager);
      g_clear_object (&data->cancellable);
      g_free (data);
    }
}
    
static void
on_display_manager_seat_proxy_ready (GObject      * o G_GNUC_UNUSED,
                                     GAsyncResult * res,
                                     gpointer       gdata)
{
  gsize offset = G_STRUCT_OFFSET (struct session_proxy_data, dm_seat);
  GError * err = NULL;
  gpointer proxy = display_manager_seat_proxy_new_for_bus_finish (res, &err);
  on_proxy_ready_impl (gdata, offset, err, proxy);
}

static void
on_login1_seat_ready (GObject      * o G_GNUC_UNUSED,
                      GAsyncResult * res,
                      gpointer       gdata)
{
  gsize offset = G_STRUCT_OFFSET (struct session_proxy_data, login1_seat);
  GError * err = NULL;
  gpointer proxy = login1_seat_proxy_new_for_bus_finish (res,  &err);
  on_proxy_ready_impl (gdata, offset, err, proxy);
}

static void
on_login1_manager_ready (GObject      * o G_GNUC_UNUSED,
                         GAsyncResult * res,
                         gpointer       gdata)
{
  gsize offset = G_STRUCT_OFFSET (struct session_proxy_data, login1_manager);
  GError * err = NULL;
  gpointer proxy = login1_manager_proxy_new_for_bus_finish (res, &err);
  on_proxy_ready_impl (gdata, offset, err, proxy);
}

static void
on_accounts_proxy_ready (GObject      * o G_GNUC_UNUSED,
                         GAsyncResult * res,
                         gpointer       gdata)
{
  gsize offset = G_STRUCT_OFFSET (struct session_proxy_data, account_manager);
  GError * err = NULL;
  gpointer proxy = accounts_proxy_new_for_bus_finish (res, &err);
  on_proxy_ready_impl (gdata, offset, err, proxy);
}

/* helper utility to get the dbus proxies used by the backend-dbus classes */
void
indicator_session_util_get_session_proxies (
                     indicator_session_util_session_proxies_func   func,
                     GCancellable                                * cancellable,
                     gpointer                                      user_data)
{
  struct session_proxy_data * data;
  char * seat_path;

  data = g_new0 (struct session_proxy_data, 1);
  data->callback = func;
  data->user_data = user_data;
  data->cancellable = g_object_ref (cancellable);

  data->pending++;
  login1_manager_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                    G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                                    "org.freedesktop.login1",
                                    "/org/freedesktop/login1",
                                    data->cancellable,
                                    on_login1_manager_ready, data);

  data->pending++;
  seat_path = g_strconcat ("/org/freedesktop/login1/seat/", g_getenv("XDG_SEAT"), NULL);
  login1_seat_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                 G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                                 "org.freedesktop.login1",
                                 seat_path,
                                 data->cancellable,
                                 on_login1_seat_ready,
                                 data);
  data->pending++;
  accounts_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                              G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                              "org.freedesktop.Accounts",
                              "/org/freedesktop/Accounts",
                              data->cancellable,
                              on_accounts_proxy_ready, data);

  data->pending++;
  display_manager_seat_proxy_new_for_bus (
                               G_BUS_TYPE_SYSTEM,
                               G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                               "org.freedesktop.DisplayManager",
                               g_getenv ("XDG_SEAT_PATH"),
                               data->cancellable,
                               on_display_manager_seat_proxy_ready, data);

  g_free (seat_path);
}