]> code.delx.au - bg-scripts/blobdiff - wallchanger.py
Added support for Unity
[bg-scripts] / wallchanger.py
index 7386293abb83d9d502a652f22f8ff68fd662abf7..4ee3b08f9c5cccf6989ee8e80ab235bf1d2b9483 100755 (executable)
@@ -6,6 +6,10 @@
 
 import commands, sys, os, os.path, time
 import logging
+try:
+       import PIL, PIL.Image
+except ImportError:
+       PIL = None
 
 __all__ = ("init", "set_image")
 
@@ -43,6 +47,10 @@ def init(*args, **kwargs):
        if commands.getstatusoutput("xwininfo -name 'KDE Desktop'")[0] == 0:
                changers.append(KDEChanger(*args, **kwargs))
 
+       logging.debug("Testing for Unity")
+       if commands.getstatusoutput("xlsclients | grep -qi unity")[0] == 0:
+               changers.append(UnityChanger(*args, **kwargs))
+
        logging.debug("Testing for Gnome")
        if commands.getstatusoutput("xwininfo -name 'gnome-settings-daemon'")[0] == 0:
                changers.append(GnomeChanger(*args, **kwargs))
@@ -82,6 +90,27 @@ class BaseChanger(object):
        def set_image(self, filename):
                raise NotImplementedError()
 
+       def convert_image_format(self, filename, format='BMP', allowAlpha=False, extension='.bmp'):
+               """Convert the image to another format, and store it in a local place"""
+               if not os.path.exists(filename):
+                       logger.warn('The input file "%s" does not exist, so it will not be converted', filename)
+                       return filename, False
+               if PIL is None:
+                       logger.warn('PIL could not be found, not converting image format')
+                       return filename, False
+
+               self.remove_old_image_cache()
+               output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension))
+               img = PIL.Image.open(filename)
+
+               # Remove the alpha channel if the user doens't want it
+               if not allowAlpha and img.mode == 'RGBA':
+                       img = img.convert('RGB')
+               img.save(output_name, format)
+
+               return output_name, True
+
+
 class WMakerChanger(BaseChanger):
        name = "WindowMaker"
        _ConvertedWallpaperLocation = '/tmp/wallpapers_wmaker/'
@@ -145,10 +174,7 @@ class OSXChanger(BaseChanger):
                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
+                       return super(OSXChanger, self).convert_image_format(file, format='PNG', extension='.png')
                except ImportError:
                        logging.debug('Could not load PIL, going to try just copying the image')
                        import shutil
@@ -201,18 +227,6 @@ class WIN32Changer(BaseChanger):
                        for dirname in dirnames:
                                os.unlink(os.path.join(fullpath, dirname))
 
-       def convert_image_format(self, file):
-               """Convert the image to a bmp, and store it in a local place"""
-               self.remove_old_image_cache()
-               output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.bmp' % time.time())
-               import PIL, PIL.Image
-               img = PIL.Image.open(file)
-               if img.mode == 'RGBA':
-                       img = img.convert('RGB')
-               img.save(output_name, 'BMP')
-
-               return output_name, True
-
        def set_image(self, filename):
                import ctypes
                user32 = ctypes.windll.user32
@@ -244,6 +258,13 @@ class GnomeChanger(BaseChanger):
                logging.debug(cmd)
                return not self._runProgram(cmd)
 
+class UnityChanger(BaseChanger):
+       name = "Unity"
+       def set_image(self, file):
+               cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+file]
+               logging.debug(cmd)
+               return not self._runProgram(cmd)
+
 class KDEChanger(BaseChanger):
        name = "KDE"
        def set_image(self, file):