-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktop_environments.py
104 lines (80 loc) · 3.04 KB
/
desktop_environments.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
from __future__ import unicode_literals
import os
import abc
import sqlite3 as sqlite
import subprocess
import ctypes
import six
from util import error
@six.add_metaclass(abc.ABCMeta)
class DesktopEnvironment(object):
@abc.abstractmethod
def get_current_wallpaper(self):
pass
@abc.abstractmethod
def set_wallpaper(self, img_path):
pass
class OSXDesktop(DesktopEnvironment):
WALLPAPER_SETTINGS = "~/Library/Application Support/Dock/desktoppicture.db"
def __init__(self):
if not os.path.isfile(os.path.expanduser(self.WALLPAPER_SETTINGS)):
error("BUG: unable to find OS X wallpaper settings")
def _get_db(self):
try:
conn = sqlite.connect(os.path.expanduser(self.WALLPAPER_SETTINGS))
cursor = conn.cursor()
return conn, cursor
except sqlite.OperationalError:
error("Unable to open OS X wallpaper settings.")
def get_current_wallpaper(self):
conn, cursor = self._get_db()
query = "SELECT value FROM data"
try:
cursor.execute(query)
paths = cursor.fetchall()
if not len(paths) > 0:
error("Unable to get current wallpaper.")
# we expect all rows to have the same content
if not paths.count(paths[0]) == len(paths):
return "You seem to have different wallpapers."
return paths[0][0]
except sqlite.DatabaseError as db_error:
error("Unable to get current wallpaper: {}".format(db_error))
finally:
conn.close()
return ""
def set_wallpaper(self, img_path):
def _restart_dock():
subprocess.call(["/usr/bin/killall", "Dock"])
conn, cursor = self._get_db()
query = "UPDATE data SET value = (?)"
try:
cursor.execute(query, (img_path,))
conn.commit()
except sqlite.DatabaseError as db_error:
error("Unable to save new wallpaper: {}".format(db_error))
finally:
conn.close()
_restart_dock()
class WindowsDesktop(DesktopEnvironment):
SPI_GETDESKWALLPAPER = 0x73
SPI_SETDESKWALLPAPER = 0x14
SPIF_UPDATEINIFILE = 0x01
SPIF_SENDWININICHANGE = 0x02
BUFFER_SIZE = 256
def get_current_wallpaper(self):
wallpaper = ctypes.create_unicode_buffer(self.BUFFER_SIZE)
res = ctypes.windll.user32.SystemParametersInfoW(
self.SPI_GETDESKWALLPAPER, self.BUFFER_SIZE, wallpaper, 0)
if not res:
# call Windows's GetLastError() and print error message
raise ctypes.WinError()
if not wallpaper.value:
return "Unable to get current wallpaper."
return wallpaper.value
def set_wallpaper(self, img_path):
res = ctypes.windll.user32.SystemParametersInfoW(
self.SPI_SETDESKWALLPAPER, 0, img_path, self.SPIF_UPDATEINIFILE | self.SPIF_SENDWININICHANGE)
if not res:
# call Windows's GetLastError() and print error message
raise ctypes.WinError()