forked from bharadwaj-raju/WeatherDesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Desktop.py
321 lines (184 loc) · 9.54 KB
/
Desktop.py
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
#!/usr/bin/env python3
import os
import sys
import subprocess
import shutil
import re
import textwrap
# Library to set wallpaper and find desktop - Cross-platform
# set_wallpaper() and get_config_dir()
# taken (and modified a bit) from http://stackoverflow.com/a/21213504/5413945
# by stackoverflow user Martin Hansen
# get_desktop_environment() and is_running()
# taken (and modified a bit) from http://stackoverflow.com/a/21213358/5413945
# by stackoverflow user Martin Hansen
def get_desktop_environment():
if sys.platform in ['win32', 'cygwin']: return 'windows'
elif sys.platform == 'darwin': return 'mac'
else:
desktop_session = os.environ.get('DESKTOP_SESSION')
if desktop_session is not None:
desktop_session = desktop_session.lower()
if desktop_session in ['gnome','unity', 'cinnamon', 'mate', 'xfce4', 'lxde', 'fluxbox',
'blackbox', 'openbox', 'icewm', 'jwm', 'afterstep','trinity', 'kde', 'pantheon']:
return desktop_session
#-- Special cases --#
# Canonical sets $DESKTOP_SESSION to Lubuntu rather than LXDE if using LXDE.
# There is no guarantee that they will not do the same with the other desktop environments.
elif 'xfce' in desktop_session or desktop_session.startswith('xubuntu'): return 'xfce4'
elif desktop_session.startswith('ubuntu'): return 'unity'
elif desktop_session.startswith('lubuntu'): return 'lxde'
elif desktop_session.startswith('kubuntu'): return 'kde'
elif desktop_session.startswith('razor'): return 'razor-qt'
elif desktop_session.startswith('wmaker'): return 'windowmaker'
if os.environ.get('KDE_FULL_SESSION') == 'true':
return 'kde'
elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
if not 'deprecated' in os.environ.get('GNOME_DESKTOP_SESSION_ID'):
return 'gnome2'
elif is_running('xfce-mcs-manage'): return 'xfce4'
elif is_running('ksmserver'): return 'kde'
return 'unknown'
def is_running(process):
try: # Linux/Unix
s = subprocess.Popen(['ps', 'axw'],stdout=subprocess.PIPE)
except: # Windows
s = subprocess.Popen(['tasklist', '/v'],stdout=subprocess.PIPE)
for x in s.stdout:
if re.search(process, x):
return True
return False
def set_wallpaper(image):
desktop_env = get_desktop_environment()
try:
if desktop_env in ['gnome', 'unity', 'cinnamon', 'pantheon']:
uri = 'file://%s' % image
try:
SCHEMA = 'org.gnome.desktop.background'
KEY = 'picture-uri'
gsettings = Gio.Settings.new(SCHEMA)
gsettings.set_string(KEY, uri)
except:
args = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', uri]
subprocess.Popen(args)
elif desktop_env == 'mate':
try: # MATE >= 1.6
args = ['gsettings', 'set', 'org.mate.background', 'picture-filename', '%s' % image]
subprocess.Popen(args)
except: # MATE < 1.6
args = ['mateconftool-2','-t','string','--set','/desktop/mate/background/picture_filename','%s' % image]
subprocess.Popen(args)
elif desktop_env == 'gnome2':
args = ['gconftool-2','-t','string','--set','/desktop/gnome/background/picture_filename', '%s' % image]
subprocess.Popen(args)
elif desktop_env == 'kde': pass
# The KDE 4+ method of changing *anything* in the CLI is either
# non-existent or deprecated or horribly convoluted.
# There have been long-open bugs (5 yrs and counting) but no fix.
# There was *one* way (in KDE 4) - the file
# ~/.kde/share/config/plasma-desktop-appletsrc
# That too, is gone in KDE 5.
# The only way seems to be to *make* the user set a file as
# wallpaper and keep overwriting that file. KDE will, apparently,
# notice the change and update automatically.
# Update: That, too, is gone. KDE users will have to set a
# peridically updating slideshow in a folder.
# Update: Even *that* does not work. Sorry, KDE.
elif desktop_env in ['kde3', 'trinity']:
args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % image
subprocess.Popen(args,shell=True)
elif desktop_env=='xfce4':
# XFCE4's image property is not image-path but last-image (What?)
# Only GNOME seems to have a sane wallpaper interface
# Update: the monitor id thing seems to be changed in
# XFCE 4.12 to just monitor0 instead of monitorVGA1 or something
# So now we need to do both.
XFCE_SCRIPT = r'''
monitor_port=$(xrandr | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor"$monitor_port"/workspace0/last-image -s ''' + image + '''\nxfdesktop --reload
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s ''' + image + '''\nxfdesktop --reload'''
xfce_script_file = open(os.path.expanduser('~/.weatherdesk_script.sh'), 'w')
xfce_script_file.truncate()
xfce_script_file.write(XFCE_SCRIPT)
xfce_script_file.close()
subprocess.Popen(['sh', os.path.abspath(os.path.expanduser('~/.weatherdesk_script.sh'))])
elif desktop_env=='razor-qt':
desktop_conf = configparser.ConfigParser()
# Development version
desktop_conf_file = os.path.join(get_config_dir('razor'),'desktop.conf')
if os.path.isfile(desktop_conf_file):
config_option = r'screens\1\desktops\1\wallpaper'
else:
desktop_conf_file = os.path.join(os.path.expanduser('~'),'.razor/desktop.conf')
config_option = r'desktops\1\wallpaper'
desktop_conf.read(os.path.join(desktop_conf_file))
try:
if desktop_conf.has_option('razor',config_option): # only replacing a value
desktop_conf.set('razor',config_option,image)
with codecs.open(desktop_conf_file, 'w', encoding='utf-8', errors='replace') as f:
desktop_conf.write(f)
except: pass
elif desktop_env in ['fluxbox','jwm','openbox','afterstep']:
try:
args = ['feh','--bg-scale', image]
subprocess.Popen(args)
except:
sys.stderr.write('Error: Failed to set wallpaper with feh!')
sys.stderr.write('Please make sre that You have feh installed.')
elif desktop_env == 'icewm':
args = ['icewmbg', image]
subprocess.Popen(args)
elif desktop_env == 'blackbox':
args = ['bsetbg', '-full', image]
subprocess.Popen(args)
elif desktop_env == 'lxde':
args = 'pcmanfm --set-wallpaper %s --wallpaper-mode=scaled' % image
subprocess.Popen(args,shell=True)
elif desktop_env == 'windowmaker':
args = 'wmsetbg -s -u %s' % image
subprocess.Popen(args,shell=True)
elif desktop_env == 'enlightenment':
args = 'enlightenment_remote -desktop-bg-add 0 0 0 0 %s' % image
subprocess.Popen(args,shell=True)
elif desktop_env == 'windows':
WIN_SCRIPT = '''reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %s /f
rundll32.exe user32.dll,UpdatePerUserSystemParameters
''' % image
win_script_file = open(os.path.abspath(os.path.expanduser('~/.weatherdesk_script.bat')), 'w')
win_script_file.write(WIN_SCRIPT)
win_script_file.close()
subprocess.Popen([os.path.abspath(os.path.expanduser('~/.weatherdesk_script.bat'))], shell=True)
elif desktop_env == 'mac':
try:
from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File(image))
except ImportError:
OSX_SCRIPT = '''tell application 'Finder'
set desktop picture to POSIX file "%s"
end tell
''' % image
osx_script_file = open(os.path.expanduser('~/.weatherdesk_script.AppleScript'), 'w')
osx_script_file.truncate()
osx_script_file.write(OSX_SCRIPT)
osx_script_file.close()
subprocess.Popen(['/usr/bin/osascript', os.path.abspath(os.path.expanduser('~/.weatherdesk_script.sh'))])
else:
sys.stderr.write('Error: Failed to set wallpaper. (Desktop not supported)')
return False
return True
except:
sys.stderr.write('Error: Failed to set wallpaper.')
return False
def get_config_dir(app_name):
if 'XDG_CONFIG_HOME' in os.environ:
confighome = os.environ['XDG_CONFIG_HOME']
elif 'APPDATA' in os.environ: # On Windows
confighome = os.environ['APPDATA']
else:
try:
from xdg import BaseDirectory
confighome = BaseDirectory.xdg_config_home
except ImportError: # Most likely a Linux/Unix system anyway
confighome = os.path.join(os.path.expanduser('~'),'.config')
configdir = os.path.join(confighome,app_name)
return configdir