- name = "Mac OS X"
- _ConvertedWallpaperLocation = '/tmp/wallpapers/'
- _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist')
-
- def __init__(self, *args, **kwargs):
- BaseChanger.__init__(self, *args, **kwargs)
- self.fix_desktop_plist()
-
- def remove_old_image_cache(self):
- """Cleans up any old temp images"""
- if not os.path.isdir(self._ConvertedWallpaperLocation):
- os.mkdir(self._ConvertedWallpaperLocation)
- for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False):
- for filename in filenames:
- os.unlink(os.path.join(fullpath, filename))
- for dirname in dirnames:
- os.unlink(os.path.join(fullpath, dirname))
-
- def convert_image_format(self, file):
- """Convert the image to a png, and store it in a local place"""
- self.remove_old_image_cache()
- output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
- try:
- import PIL, PIL.Image
- img = PIL.Image.open(file)
- img.save(output_name, "PNG")
- return output_name, True
- except ImportError:
- debug('Could not load PIL, going to try just copying the image')
- import shutil
- output_name = os.path.join(self._ConvertedWallpaperLocation, os.path.basename(file))
- shutil.copyfile(file, output_name)
- return output_name, True
-
- def fix_desktop_plist(self):
- """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor"""
- try:
- import Foundation
- desktop_plist = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation)
- # Remove all but the 'default' entry
- for k in desktop_plist['Background'].keys():
- if k == 'default':
- continue
- desktop_plist['Background'].removeObjectForKey_(k)
- # Store the plist again (Make sure we write it out atomically -- Don't want to break finder)
- desktop_plist.writeToFile_atomically_(self._DesktopPlistLocation, True)
- except ImportError:
- debug('Could not import the Foundation module, you may have problems with dual screens')
-
- def set_image(self, filename):
- if self.convert:
- filename, ret = self.convert_image_format(filename)
- if not ret:
- debug("Convert failed")
- return False
- cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % filename
- debug(cmd)
- return not commands.getstatusoutput(cmd)[0]
-
-class GnomeChanger(BaseChanger):
- name = "Gnome"
- def set_image(self, file):
- cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file]
- debug(cmd)
- return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
-
-class KDEChanger(BaseChanger):
- name = "KDE"
- def set_image(self, file):
- cmds = []
- for group in ('Desktop0', 'Desktop0Screen0'):
- base = ['kwriteconfig', '--file', 'kdesktoprc', '--group', group, '--key']
- cmds.append(base + ['Wallpaper', file])
- cmds.append(base + ['UseSHM', '--type', 'bool', 'true'])
- cmds.append(base + ['WallpaperMode', 'ScaleAndCrop'])
- cmds.append(base + ['MultiWallpaperMode', 'NoMulti'])
-
- cmds.append(['dcop', 'kdesktop', 'KBackgroundIface', 'configure'])
- for cmd in cmds:
- debug(cmd)
- if subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() != 0:
- return False
-
- return True
-
+ name = "Mac OS X"
+ _ConvertedWallpaperLocation = '/tmp/wallpapers/'
+ _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist')
+
+ def __init__(self, *args, **kwargs):
+ BaseChanger.__init__(self, *args, **kwargs)
+
+ def remove_old_image_cache(self):
+ """Cleans up any old temp images"""
+ if not os.path.isdir(self._ConvertedWallpaperLocation):
+ os.mkdir(self._ConvertedWallpaperLocation)
+ for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False):
+ for filename in filenames:
+ os.unlink(os.path.join(fullpath, filename))
+ for dirname in dirnames:
+ os.unlink(os.path.join(fullpath, dirname))
+
+ def convert_image_format(self, filename):
+ """Convert the image to a png, and store it in a local place"""
+ self.remove_old_image_cache()
+ output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
+ try:
+ return super(OSXChanger, self).convert_image_format(filename, format='PNG', extension='.png')
+ except ImportError:
+ logging.debug('Could not load PIL, going to try just copying the image')
+ import shutil
+ output_name = os.path.join(self._ConvertedWallpaperLocation, os.path.basename(filename))
+ shutil.copyfile(filename, output_name)
+ return output_name, True
+
+ def fix_desktop_plist(self):
+ """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor"""
+ try:
+ import Foundation
+ desktop_plist = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation)
+ # Remove all but the 'default' entry
+ for k in desktop_plist['Background'].keys():
+ if k == 'default':
+ continue
+ desktop_plist['Background'].removeObjectForKey_(k)
+ # Store the plist again (Make sure we write it out atomically -- Don't want to break finder)
+ desktop_plist.writeToFile_atomically_(self._DesktopPlistLocation, True)
+ except ImportError:
+ logging.debug('Could not import the Foundation module, you may have problems with dual screens')
+
+ def set_image(self, filename):
+ self.fix_desktop_plist()
+ if self.convert:
+ filename, ret = self.convert_image_format(filename)
+ if not ret:
+ logging.debug("Convert failed")
+ return False
+ cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % filename
+ logging.debug(cmd)
+ return not subprocess.getstatusoutput(cmd)[0]
+
+class WIN32Changer(BaseChanger):
+ name = "Windows"
+ _ConvertedWallpaperLocation = os.path.join(os.environ.get('APPDATA', os.path.expanduser('~')), 'wallchanger')
+
+ def __init__(self, *args, **kwargs):
+ BaseChanger.__init__(self, *args, **kwargs)
+ if not self.convert:
+ logging.warn('Running on windows, but convert is not set')
+
+ def remove_old_image_cache(self):
+ """Cleans up any old temp images"""
+ if not os.path.isdir(self._ConvertedWallpaperLocation):
+ os.mkdir(self._ConvertedWallpaperLocation)
+ for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False):
+ for filename in filenames:
+ os.unlink(os.path.join(fullpath, filename))
+ for dirname in dirnames:
+ os.unlink(os.path.join(fullpath, dirname))
+
+ def set_image(self, filename):
+ import ctypes
+ user32 = ctypes.windll.user32
+
+ # Taken from the Platform SDK
+ SPI_SETDESKWALLPAPER = 20
+ SPIF_SENDWININICHANGE = 2
+
+ if self.convert:
+ filename, ret = self.convert_image_format(filename)
+ if not ret:
+ logging.debug("Convert failed")
+ return False
+
+ # Parameters for SystemParametersInfoA are:
+ # (UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni)
+ user32.SystemParametersInfoA(
+ SPI_SETDESKWALLPAPER,
+ 0,
+ filename,
+ SPIF_SENDWININICHANGE,
+ )
+ return True
+
+class Gnome2Changer(BaseChanger):
+ name = "Gnome"
+ def set_image(self, filename):
+ cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', filename]
+ logging.debug(cmd)
+ return subprocess.call(cmd) == 0
+
+class Gnome3Changer(BaseChanger):
+ name = "Gnome3"
+ def set_image(self, filename):
+ cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+filename]
+ logging.debug(cmd)
+ return subprocess.call(cmd) == 0
+
+class Xfce4Changer(BaseChanger):
+ name = "XFCE4"
+ def set_image(self, filename):
+ for display in get_displays():
+ cmd = [
+ "xfconf-query",
+ "-c", "xfce4-desktop",
+ "-p", "/backdrop/screen0/monitor%s/workspace0/last-image" % display,
+ "-s", filename,
+ ]
+ logging.debug(cmd)
+ if subprocess.call(cmd) != 0:
+ return False
+ return True