X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/64f2a363dab6a9abad19f5227d9aa119264f8e23..0736e53c6d6e9f192c8cbf68602f4a18c0264501:/wallchanger.py diff --git a/wallchanger.py b/wallchanger.py index 7d6cd6a..b5cf6a1 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -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)) @@ -63,21 +71,16 @@ class BaseChanger(object): self.permanent = permanent self.convert = convert - try: + try: + def _exec_cmd(self, cmd): 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() @@ -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): + 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 - import PIL, PIL.Image 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)) - return output_name, self._runProgram(cmd) + return output_name, self._exec_cmd(cmd) 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)) - return not self._runProgram(cmd) + return not self._exec_cmd(cmd) 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) - 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" @@ -261,7 +274,7 @@ class KDEChanger(BaseChanger): 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