Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-xdp-background"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <glib/gi18n.h>
9 : : #include <gtk/gtk.h>
10 : : #include <libportal/portal.h>
11 : : #include <valent.h>
12 : :
13 : : #include "valent-xdp-background.h"
14 : : #include "valent-xdp-utils.h"
15 : :
16 : :
17 : : struct _ValentXdpBackground
18 : : {
19 : : ValentApplicationPlugin parent;
20 : :
21 : : GSettings *settings;
22 : : unsigned int autostart : 1;
23 : : unsigned long active_id;
24 : : };
25 : :
26 : 0 : G_DEFINE_FINAL_TYPE (ValentXdpBackground, valent_xdp_background, VALENT_TYPE_APPLICATION_PLUGIN)
27 : :
28 : :
29 : : static void
30 : 0 : xdp_portal_request_background_cb (GObject *object,
31 : : GAsyncResult *result,
32 : : gpointer user_data)
33 : : {
34 : 0 : XdpPortal *portal = XDP_PORTAL (object);
35 : 0 : g_autoptr (GError) error = NULL;
36 : :
37 : 0 : g_assert (XDP_IS_PORTAL (portal));
38 : :
39 : 0 : if (!xdp_portal_request_background_finish (portal, result, &error))
40 : : {
41 : 0 : if (error != NULL)
42 : 0 : g_warning ("ValentXdpPlugin: %s", error->message);
43 : : else
44 : 0 : g_debug ("ValentXdpPlugin: permission denied");
45 : : }
46 : : else
47 : : {
48 : 0 : xdp_portal_set_background_status (portal,
49 : 0 : _("Syncing Devices"),
50 : : g_task_get_cancellable (G_TASK (result)),
51 : : NULL,
52 : : NULL);
53 : : }
54 : 0 : }
55 : :
56 : : static void
57 : 0 : valent_xdp_background_request (ValentXdpBackground *self)
58 : : {
59 : 0 : g_autoptr (GPtrArray) command_line = NULL;
60 : 0 : g_autoptr (XdpParent) parent = NULL;
61 : 0 : g_autoptr (GCancellable) destroy = NULL;
62 : 0 : XdpBackgroundFlags flags = XDP_BACKGROUND_FLAG_NONE;
63 : :
64 : 0 : g_assert (VALENT_IS_XDP_BACKGROUND (self));
65 : :
66 : 0 : if (self->autostart)
67 : : {
68 : 0 : command_line = g_ptr_array_new_with_free_func (g_free);
69 : 0 : g_ptr_array_add (command_line, g_strdup ("valent"));
70 : 0 : g_ptr_array_add (command_line, g_strdup ("--gapplication-service"));
71 : :
72 : 0 : flags |= XDP_BACKGROUND_FLAG_AUTOSTART;
73 : : }
74 : :
75 : 0 : parent = valent_xdp_get_parent ();
76 : 0 : destroy = valent_object_ref_cancellable (VALENT_OBJECT (self));
77 : 0 : xdp_portal_request_background (valent_xdp_get_default (),
78 : : parent,
79 : : _("Valent wants to run as a service"),
80 : : command_line,
81 : : flags,
82 : : destroy,
83 : : xdp_portal_request_background_cb,
84 : : NULL);
85 : 0 : }
86 : :
87 : : static void
88 : 0 : on_window_is_active (GtkWindow *window,
89 : : GParamSpec *pspec,
90 : : ValentXdpBackground *self)
91 : : {
92 : 0 : GListModel *windows = NULL;
93 : 0 : unsigned int n_windows = 0;
94 : :
95 : 0 : if (!gtk_window_is_active (window))
96 : : return;
97 : :
98 : 0 : windows = gtk_window_get_toplevels ();
99 : 0 : n_windows = g_list_model_get_n_items (windows);
100 : :
101 : 0 : for (unsigned int i = 0; i < n_windows; i++)
102 : : {
103 : 0 : g_autoptr (GtkWindow) item = g_list_model_get_item (windows, i);
104 : :
105 : 0 : g_signal_handlers_disconnect_by_func (item, on_window_is_active, self);
106 : : }
107 : :
108 : 0 : g_clear_signal_handler (&self->active_id, windows);
109 : 0 : valent_xdp_background_request (self);
110 : : }
111 : :
112 : : static void
113 : 0 : on_windows_changed (GListModel *list,
114 : : unsigned int position,
115 : : unsigned int removed,
116 : : unsigned int added,
117 : : ValentXdpBackground *self)
118 : : {
119 : 0 : for (unsigned int i = 0; i < added; i++)
120 : : {
121 : 0 : g_autoptr (GtkWindow) window = g_list_model_get_item (list, position + i);
122 : :
123 : : // If the new window is active, we can bail now
124 : 0 : on_window_is_active (window, NULL, self);
125 : 0 : if (self->active_id == 0)
126 : 0 : return;
127 : :
128 : 0 : g_signal_connect_object (window,
129 : : "notify::is-active",
130 : : G_CALLBACK (on_window_is_active),
131 : : self, 0);
132 : : }
133 : : }
134 : :
135 : : static void
136 : 0 : on_autostart_changed (GSettings *settings,
137 : : const char *key,
138 : : ValentXdpBackground *self)
139 : : {
140 : 0 : g_assert (VALENT_IS_XDP_BACKGROUND (self));
141 : :
142 : 0 : self->autostart = g_settings_get_boolean (self->settings, "autostart");
143 : :
144 : : /* Already waiting for an active window */
145 : 0 : if (self->active_id > 0)
146 : : return;
147 : :
148 : : /* If there is no window or Valent is not the focused application, defer the
149 : : * request until that changes. */
150 : 0 : if (!valent_xdp_has_parent ())
151 : : {
152 : 0 : GListModel *windows = gtk_window_get_toplevels ();
153 : :
154 : 0 : self->active_id = g_signal_connect_object (windows,
155 : : "items-changed",
156 : : G_CALLBACK (on_windows_changed),
157 : : self, 0);
158 : 0 : on_windows_changed (windows, 0, 0, g_list_model_get_n_items (windows), self);
159 : 0 : return;
160 : : }
161 : :
162 : 0 : valent_xdp_background_request (self);
163 : : }
164 : :
165 : : /*
166 : : * ValentObject
167 : : */
168 : : static void
169 : 0 : valent_xdp_background_destroy (ValentObject *object)
170 : : {
171 : 0 : ValentXdpBackground *self = VALENT_XDP_BACKGROUND (object);
172 : 0 : GListModel *windows = gtk_window_get_toplevels ();
173 : :
174 : 0 : g_clear_signal_handler (&self->active_id, windows);
175 : 0 : g_clear_object (&self->settings);
176 : :
177 : : /* If the extension is being disabled during application shutdown, the main
178 : : * window is already closed and this will be skipped. If the user has disabled
179 : : * the extension, then the window must be active and it will succeed */
180 : 0 : if (valent_xdp_has_parent ())
181 : : {
182 : 0 : self->autostart = FALSE;
183 : 0 : valent_xdp_background_request (self);
184 : : }
185 : :
186 : 0 : VALENT_OBJECT_CLASS (valent_xdp_background_parent_class)->destroy (object);
187 : 0 : }
188 : :
189 : : /*
190 : : * GObject
191 : : */
192 : : static void
193 : 0 : valent_xdp_background_constructed (GObject *object)
194 : : {
195 : 0 : ValentXdpBackground *self = VALENT_XDP_BACKGROUND (object);
196 : :
197 : 0 : self->settings = g_settings_new ("ca.andyholmes.Valent.Plugin.xdp");
198 : 0 : g_signal_connect_object (self->settings,
199 : : "changed::autostart",
200 : : G_CALLBACK (on_autostart_changed),
201 : : self, 0);
202 : :
203 : 0 : on_autostart_changed (self->settings, "autostart", self);
204 : :
205 : 0 : G_OBJECT_CLASS (valent_xdp_background_parent_class)->constructed (object);
206 : 0 : }
207 : :
208 : : static void
209 : 0 : valent_xdp_background_class_init (ValentXdpBackgroundClass *klass)
210 : : {
211 : 0 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 : 0 : ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
213 : :
214 : 0 : object_class->constructed = valent_xdp_background_constructed;
215 : :
216 : 0 : vobject_class->destroy = valent_xdp_background_destroy;
217 : : }
218 : :
219 : : static void
220 : 0 : valent_xdp_background_init (ValentXdpBackground *self)
221 : : {
222 : 0 : }
223 : :
|