]> code.delx.au - bg-scripts/blobdiff - wallchanger.py
rename runProgram
[bg-scripts] / wallchanger.py
index 7d6cd6a9d97682575790049f1e2abd7405a645b2..b5cf6a14ef8e37e8675df27bb3657aea036d1222 100755 (executable)
@@ -6,6 +6,10 @@
 
 import commands, sys, os, os.path, time
 import logging
 
 import commands, sys, os, os.path, time
 import logging
+try:
+       import PIL, PIL.Image
+except ImportError:
+       PIL = None
 
 __all__ = ("init", "set_image")
 
 
 __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))
 
        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))
        logging.debug("Testing for Gnome")
        if commands.getstatusoutput("xwininfo -name 'gnome-settings-daemon'")[0] == 0:
                changers.append(GnomeChanger(*args, **kwargs))
@@ -63,21 +71,16 @@ class BaseChanger(object):
                self.permanent = permanent
                self.convert = convert
 
                self.permanent = permanent
                self.convert = convert
 
-               try:
+       try:
+               def _exec_cmd(self, cmd):
                        import subprocess
                        import subprocess
-               except ImportError:
-                       self._runProgram = self._runProgram_command
-               else:
-                       self._runProgram = self._runProgram_subprocess
-
-       def _runProgram_subprocess(self, cmd):
-               import subprocess
-               return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+                       return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
 
 
-       # A simple implementation of subprocess for python2.4
-       def _runProgram_command(self, cmd):
-               """Runs a program given in cmd"""
-               return os.spawnvp(os.P_WAIT, cmd[0], cmd)
+       except ImportError:
+               # A simple implementation of subprocess for python2.4
+               def _exec_cmd(self, cmd):
+                       """Runs a program given in cmd"""
+                       return os.spawnvp(os.P_WAIT, cmd[0], cmd)
 
        def set_image(self, filename):
                raise NotImplementedError()
 
        def set_image(self, filename):
                raise NotImplementedError()
@@ -85,8 +88,11 @@ class BaseChanger(object):
        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):
        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
                        return filename, False
-               import PIL, PIL.Image
 
                self.remove_old_image_cache()
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension))
 
                self.remove_old_image_cache()
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension))
@@ -119,7 +125,7 @@ class WMakerChanger(BaseChanger):
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
                cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name]
                logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd))
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
                cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name]
                logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd))
-               return output_name, self._runProgram(cmd)
+               return output_name, self._exec_cmd(cmd)
 
        def set_image(self, file):
                if self.convert:
 
        def set_image(self, file):
                if self.convert:
@@ -138,7 +144,7 @@ class WMakerChanger(BaseChanger):
                        cmd += ["-u"] # update the wmaker database
                cmd += [file]
                logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd))
                        cmd += ["-u"] # update the wmaker database
                cmd += [file]
                logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd))
-               return not self._runProgram(cmd)
+               return not self._exec_cmd(cmd)
 
 class OSXChanger(BaseChanger):
        name = "Mac OS X"
 
 class OSXChanger(BaseChanger):
        name = "Mac OS X"
@@ -245,7 +251,14 @@ class GnomeChanger(BaseChanger):
        def set_image(self, file):
                cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file]
                logging.debug(cmd)
        def set_image(self, file):
                cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file]
                logging.debug(cmd)
-               return not self._runProgram(cmd)
+               return not self._exec_cmd(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._exec_cmd(cmd)
 
 class KDEChanger(BaseChanger):
        name = "KDE"
 
 class KDEChanger(BaseChanger):
        name = "KDE"
@@ -261,7 +274,7 @@ class KDEChanger(BaseChanger):
                cmds.append(['dcop', 'kdesktop', 'KBackgroundIface', 'configure'])
                for cmd in cmds:
                        logging.debug(cmd)
                cmds.append(['dcop', 'kdesktop', 'KBackgroundIface', 'configure'])
                for cmd in cmds:
                        logging.debug(cmd)
-                       if self._runProgram(cmd) != 0:
+                       if self._exec_cmd(cmd) != 0:
                                return False
 
                return True
                                return False
 
                return True