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
|
/*
* Copyright (c) 2008 Stephan Arts <stephan@xfce.org>
* Copyright (c) 2008-2009 Mike Massonnet <mmassonnet@xfce.org>
* Copyright (c) 2009 Jannis Pohlmann <jannis@xfce.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gio/gio.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <glib/gi18n-lib.h>
#include <xfconf/xfconf.h>
#include "twp-provider.h"
#define XFDESKTOP_SELECTION_FMT "XFDESKTOP_SELECTION_%d"
#define NAUTILUS_SELECTION_FMT "NAUTILUS_DESKTOP_WINDOW_ID"
static void twp_menu_provider_init (ThunarxMenuProviderIface *iface);
static void twp_provider_finalize (GObject *object);
static GList *twp_provider_get_file_menu_items (ThunarxMenuProvider *menu_provider,
GtkWidget *window,
GList *files);
static void twp_action_set_wallpaper (ThunarxMenuItem *item,
gpointer user_data);
static gint twp_get_active_workspace_number (GdkScreen *screen);
static gboolean _has_gsettings = FALSE;
static GtkWidget *main_window = NULL;
struct _TwpProviderClass
{
GObjectClass __parent__;
};
struct _TwpProvider
{
GObject __parent__;
gchar *child_watch_path;
gint child_watch_id;
};
THUNARX_DEFINE_TYPE_WITH_CODE (TwpProvider, twp_provider, G_TYPE_OBJECT,
THUNARX_IMPLEMENT_INTERFACE (THUNARX_TYPE_MENU_PROVIDER,
twp_menu_provider_init));
static void
twp_menu_provider_init (ThunarxMenuProviderIface *iface)
{
iface->get_file_menu_items = twp_provider_get_file_menu_items;
}
static void
twp_provider_class_init (TwpProviderClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = twp_provider_finalize;
}
static void
twp_provider_init (TwpProvider *twp_provider)
{
gchar *program;
program = g_find_program_in_path ("gsettings");
if (G_LIKELY (program != NULL))
{
_has_gsettings = TRUE;
g_free (program);
}
}
static void
twp_provider_finalize (GObject *object)
{
G_OBJECT_CLASS (twp_provider_parent_class)->finalize (object);
}
static GList*
twp_provider_get_file_menu_items (ThunarxMenuProvider *menu_provider,
GtkWidget *window,
GList *files)
{
ThunarxMenuItem *item = NULL;
GFile *location;
GList *items = NULL;
gchar *mime_type;
main_window = window;
/* we can only set a single wallpaper */
if (files->next == NULL)
{
/* get the location of the file */
location = thunarx_file_info_get_location (files->data);
/* unable to handle non-local files */
if (G_UNLIKELY (!g_file_has_uri_scheme (location, "file")))
{
g_object_unref (location);
return NULL;
}
/* release the location */
g_object_unref (location);
if (!thunarx_file_info_is_directory (files->data))
{
/* get the mime type of the file */
mime_type = thunarx_file_info_get_mime_type (files->data);
if (g_str_has_prefix (mime_type, "image/")
&& (thunarx_file_info_has_mime_type (files->data, "image/jpeg")
|| thunarx_file_info_has_mime_type (files->data, "image/png")
|| thunarx_file_info_has_mime_type (files->data, "image/svg+xml")
|| thunarx_file_info_has_mime_type (files->data, "image/svg+xml-compressed")))
{
item = thunarx_menu_item_new ("Twp::setwallpaper", _("Set as wallpaper"), NULL, "preferences-desktop-wallpaper");
g_signal_connect (item, "activate", G_CALLBACK (twp_action_set_wallpaper), files->data);
items = g_list_append (items, item);
}
g_free(mime_type);
}
}
return items;
}
static void
twp_action_set_wallpaper (ThunarxMenuItem *item,
gpointer user_data)
{
ThunarxFileInfo *file_info = user_data;
GdkDisplay *display = gdk_display_get_default();
gint screen_nr = 0;
gint n_monitors;
gint monitor_nr = 0;
gint workspace;
GdkScreen *screen;
GdkMonitor *monitor;
gchar *image_path_prop;
gchar *image_show_prop;
gchar *image_style_prop;
const char *monitor_name;
gchar *file_uri;
gchar *escaped_file_name;
gchar *file_name = NULL;
gchar *hostname = NULL;
gchar *command;
XfconfChannel *channel;
gboolean is_single_workspace;
gint current_image_style;
const gchar *desktop_type = NULL;
screen = gdk_display_get_default_screen (display);
desktop_type = g_getenv ("XDG_CURRENT_DESKTOP");
if (desktop_type == NULL)
{
g_warning ("Failed to set wallpaper: $XDG_CURRENT_DESKTOP is not defined");
return;
}
file_uri = thunarx_file_info_get_uri (file_info);
file_name = g_filename_from_uri (file_uri, &hostname, NULL);
if (hostname != NULL)
{
g_free (hostname);
g_free (file_uri);
g_free (file_name);
return;
}
workspace = twp_get_active_workspace_number (screen);
n_monitors = gdk_display_get_n_monitors (display);
if (n_monitors > 1 && main_window)
{
monitor = gdk_display_get_monitor_at_window (display, gtk_widget_get_window (main_window));
}
else
{
monitor = gdk_display_get_monitor (display, monitor_nr);
}
monitor_name = gdk_monitor_get_model (monitor);
escaped_file_name = g_shell_quote (file_name);
if (g_strcmp0 (desktop_type, "XFCE") == 0)
{
g_debug ("set on xfce");
channel = xfconf_channel_get ("xfce4-desktop");
/* This is the format for xfdesktop before 4.11 */
image_path_prop = g_strdup_printf("/backdrop/screen%d/monitor%d/image-path", screen_nr, monitor_nr);
image_show_prop = g_strdup_printf("/backdrop/screen%d/monitor%d/image-show", screen_nr, monitor_nr);
image_style_prop = g_strdup_printf("/backdrop/screen%d/monitor%d/image-style", screen_nr, monitor_nr);
/* Set the wallpaper and ensure that it's set to show */
xfconf_channel_set_string (channel, image_path_prop, file_name);
xfconf_channel_set_bool (channel, image_show_prop, TRUE);
/* If there isn't a wallpaper style set, then set one */
current_image_style = xfconf_channel_get_int (channel, image_style_prop, -1);
if (current_image_style == -1)
{
xfconf_channel_set_int (channel, image_style_prop, 0);
}
g_free(image_path_prop);
g_free(image_show_prop);
g_free(image_style_prop);
/* Xfdesktop 4.11+ has a concept of a single-workspace-mode where
* the same workspace is used for everything but additionally allows
* the user to use any current workspace as the single active
* workspace, we'll need to check if it is enabled (which by default
* it is) and use that. */
is_single_workspace = xfconf_channel_get_bool (channel, "/backdrop/single-workspace-mode", TRUE);
if (is_single_workspace)
{
workspace = xfconf_channel_get_int (channel, "/backdrop/single-workspace-number", 0);
}
/* This is the format for xfdesktop post 4.11. A workspace number is
* added and the monitor is referred to name. We set both formats so
* that it works as the user expects. */
if (monitor_name)
{
image_path_prop = g_strdup_printf("/backdrop/screen%d/monitor%s/workspace%d/last-image", screen_nr, monitor_name, workspace);
image_style_prop = g_strdup_printf("/backdrop/screen%d/monitor%s/workspace%d/image-style", screen_nr, monitor_name, workspace);
}
else
{
/* gdk_screen_get_monitor_plug_name can return NULL, in those
* instances we fallback to monitor number but still include the
* workspace number */
image_path_prop = g_strdup_printf("/backdrop/screen%d/monitor%d/workspace%d/last-image", screen_nr, monitor_nr, workspace);
image_style_prop = g_strdup_printf("/backdrop/screen%d/monitor%d/workspace%d/image-style", screen_nr, monitor_nr, workspace);
}
xfconf_channel_set_string (channel, image_path_prop, file_name);
/* If there isn't a wallpaper style set, then set one */
current_image_style = xfconf_channel_get_int (channel, image_style_prop, -1);
if (current_image_style == -1)
{
xfconf_channel_set_int (channel, image_style_prop, 5);
}
g_free(image_path_prop);
g_free(image_style_prop);
}
else if (g_strcmp0 (desktop_type, "GNOME") == 0)
{
if (_has_gsettings)
{
g_debug ("set on gnome");
command = g_strdup_printf ("gsettings set "
"org.gnome.desktop.background picture-uri "
"'%s'", file_uri);
g_spawn_command_line_async (command, NULL);
g_free (command);
}
else
{
g_warning ("Failed to set wallpaper: Missing executable 'gsettings'");
}
}
else
{
g_warning (("Failed to set wallpaper: $XDG_CURRENT_DESKTOP Desktop type '%s' not supported by thunar wallpaper plugin."), desktop_type);
}
g_free (escaped_file_name);
g_free (file_name);
g_free (file_uri);
}
/* Taken from xfce_spawn_get_active_workspace_number in xfce-spawn.c apart of
* the libxfce4ui library.
* https://git.xfce.org/xfce/libxfce4ui/tree/libxfce4ui/xfce-spawn.c#n193
*/
static gint
twp_get_active_workspace_number (GdkScreen *screen)
{
GdkWindow *root;
gulong bytes_after_ret = 0;
gulong nitems_ret = 0;
guint *prop_ret = NULL;
Atom _NET_CURRENT_DESKTOP;
Atom _WIN_WORKSPACE;
Atom type_ret = None;
gint format_ret;
gint ws_num = 0;
gdk_x11_display_error_trap_push (gdk_display_get_default ());
root = gdk_screen_get_root_window (screen);
/* determine the X atom values */
_NET_CURRENT_DESKTOP = XInternAtom (GDK_WINDOW_XDISPLAY (root), "_NET_CURRENT_DESKTOP", False);
_WIN_WORKSPACE = XInternAtom (GDK_WINDOW_XDISPLAY (root), "_WIN_WORKSPACE", False);
if (XGetWindowProperty (GDK_WINDOW_XDISPLAY (root),
gdk_x11_get_default_root_xwindow(),
_NET_CURRENT_DESKTOP, 0, 32, False, XA_CARDINAL,
&type_ret, &format_ret, &nitems_ret, &bytes_after_ret,
(gpointer) &prop_ret) != Success)
{
if (XGetWindowProperty (GDK_WINDOW_XDISPLAY (root),
gdk_x11_get_default_root_xwindow(),
_WIN_WORKSPACE, 0, 32, False, XA_CARDINAL,
&type_ret, &format_ret, &nitems_ret, &bytes_after_ret,
(gpointer) &prop_ret) != Success)
{
if (G_UNLIKELY (prop_ret != NULL))
{
XFree (prop_ret);
prop_ret = NULL;
}
}
}
if (G_LIKELY (prop_ret != NULL))
{
if (G_LIKELY (type_ret != None && format_ret != 0))
ws_num = *prop_ret;
XFree (prop_ret);
}
gdk_x11_display_error_trap_pop_ignored (gdk_display_get_default ());
return ws_num;
}
|