summaryrefslogtreecommitdiff
path: root/src/background-monitor.c
blob: 687c27fb6eff9156f6208d694835e4beae8d4c69 (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
/* background-monitor.c
 *
 * Copyright 2022 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "background-monitor.h"

#define BACKGROUND_MONITOR_BUS_NAME "org.freedesktop.background.Monitor"
#define BACKGROUND_MONITOR_OBJECT_PATH "/org/freedesktop/background/monitor"

struct _BackgroundMonitor
{
  XdpDbusBackgroundMonitorSkeleton parent_instance;

  GDBusConnection *connection;
};

static void g_initable_iface_init (GInitableIface *iface);

G_DEFINE_TYPE_WITH_CODE (BackgroundMonitor,
                         background_monitor,
                         XDP_DBUS_BACKGROUND_TYPE_MONITOR_SKELETON,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, g_initable_iface_init))

static gboolean
request_freedesktop_background_name (BackgroundMonitor  *self,
                                     GCancellable       *cancellable,
                                     GError            **error)
{
  g_autoptr(GVariant) reply = NULL;
  GBusNameOwnerFlags flags;
  guint32 result;

  flags = G_BUS_NAME_OWNER_FLAGS_REPLACE;
#if GLIB_CHECK_VERSION(2,54,0)
  flags |= G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE;
#endif

  reply = g_dbus_connection_call_sync (self->connection,
                                       "org.freedesktop.DBus",
                                       "/org/freedesktop/DBus",
                                       "org.freedesktop.DBus",
                                       "RequestName",
                                       g_variant_new ("(su)", BACKGROUND_MONITOR_BUS_NAME, flags),
                                       G_VARIANT_TYPE ("(u)"),
                                       0, -1,
                                       cancellable,
                                       error);

  if (!reply)
    return FALSE;

  g_variant_get (reply, "(u)", &result);
  if (result != 1)
    {
      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
                   "Failed to own background monitor D-Bus name");
      return FALSE;
    }

  return TRUE;
}

static gboolean
background_monitor_initable_init (GInitable     *initable,
                                  GCancellable  *cancellable,
                                  GError       **error)
{
  BackgroundMonitor *self = BACKGROUND_MONITOR (initable);
  g_autofree char *address = NULL;

  address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
  if (!address)
    return FALSE;

  self->connection = g_initable_new (G_TYPE_DBUS_CONNECTION,
                                     cancellable, error,
                                     "address", address,
                                     "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
#if GLIB_CHECK_VERSION(2,74,0)
                                              G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE |
#endif
                                              G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
                                     "exit-on-close", TRUE,
                                     NULL);

  if (!self->connection)
    return FALSE;

  g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (initable),
                                       G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);

  /* TODO: dos it need to listen to 'g-authorize-method'? */

  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (initable),
                                         self->connection,
                                         BACKGROUND_MONITOR_OBJECT_PATH,
                                         error))
    {
      return FALSE;
    }

  if (!request_freedesktop_background_name (self, cancellable, error))
    return FALSE;

  return TRUE;
}

static void
g_initable_iface_init (GInitableIface *iface)
{
  iface->init = background_monitor_initable_init;
}

static void
background_monitor_finalize (GObject *object)
{
  BackgroundMonitor *self = (BackgroundMonitor *)object;

  if (self->connection)
    g_dbus_connection_flush_sync (self->connection, NULL, NULL);

  g_clear_object (&self->connection);

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

static void
background_monitor_class_init (BackgroundMonitorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = background_monitor_finalize;
}

static void
background_monitor_init (BackgroundMonitor *self)
{
  xdp_dbus_background_monitor_set_version (XDP_DBUS_BACKGROUND_MONITOR (self), 1);
}

BackgroundMonitor *
background_monitor_new (GCancellable  *cancellable,
                        GError       **error)
{
  return g_initable_new (BACKGROUND_TYPE_MONITOR,
                         cancellable,
                         error,
                         NULL);
}