]> code.delx.au - bg-scripts/blobdiff - lib/WallChanger.py
RandomBG: More major cleanups.
[bg-scripts] / lib / WallChanger.py
index c6958a84dd958dada1418c3dd755c3968ded560b..efebd501281b4113fafff807a626c6673306c4a5 100644 (file)
@@ -37,6 +37,13 @@ def RandomBG(*args, **kwargs):
                else:
                        ret = __KDEChanger(*args, **kwargs)
 
+       debug("Testing for Gnome", DEBUG_LEVEL_LOW)
+       if commands.getstatusoutput("xwininfo -name 'gnome-session'")[0] == 0:
+               if ret is not None:
+                       ret.nextChanger = __GnomeChanger(*args, **kwargs)
+               else:
+                       ret = __GnomeChanger(*args, **kwargs)
+
        debug("Testing for WMaker", DEBUG_LEVEL_LOW)
        if commands.getstatusoutput("xlsclients | grep -qi wmaker")[0] == 0:
                if ret is not None:
@@ -50,32 +57,17 @@ def RandomBG(*args, **kwargs):
                return ret
 
 class __BaseChanger(object):
-       def __init__(self, filelist, backgroundColour='black', permanent=False):
+       def __init__(self, backgroundColour='black', permanent=False):
                debug('Determined the window manager is "%s"' % self.__class__.__name__, DEBUG_LEVEL_MEDIUM)
                self.backgroundColour = backgroundColour
                self.permanent = permanent
-               self.filelist = filelist
                # Used to 'chain' background changers
                self.nextChanger = None
-
-       def callChained(self, filename):
-               if self.nextChanger is None:
-                       return True
-               else:
-                       return self.nextChanger.changeTo(filename)
-
-       def cycleNext(self):
-               file = self.filelist.getNextRandomImage()
-               return self.changeTo(file) and self.callChained(file)
-       
-       def cyclePrev(self):
-               file = self.filelist.getPrevRandomImage()
-               return self.changeTo(file) and self.callChained(file)
        
-       def cycleReload(self):
-               file = self.filelist.getCurrentImage()
-               return self.changeTo(file) and self.callChained(file)
-
+       def setImage(self, filename):
+               self._setImage(filename)
+               if self.nextChanger is not None:
+                       self.nextChange.changeTo(filename)
 
 class __WMakerChanger(__BaseChanger):
        _ConvertedWallpaperLocation = '/tmp/wallpapers_wmaker/'
@@ -96,7 +88,8 @@ class __WMakerChanger(__BaseChanger):
                cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name]
                debug("""Convert command: '"%s"'""" % '" "'.join(cmd), DEBUG_LEVEL_DEBUG)
                return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
-       def changeTo(self, file):
+
+       def _setImage(self, file):
                file, convert_status = self._convertImageFormat(file)
                if convert_status:
                        debug('Convert failed')
@@ -116,6 +109,8 @@ class __WMakerChanger(__BaseChanger):
 
 class __OSXChanger(__BaseChanger):
        _ConvertedWallpaperLocation = '/tmp/wallpapers/'
+       _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist')
+
        def _removeOldImageCache(self):
                """Cleans up any old temp images"""
                if not os.path.isdir(self._ConvertedWallpaperLocation):
@@ -130,27 +125,51 @@ class __OSXChanger(__BaseChanger):
                """Convert the image to a png, and store it in a local place"""
                self._removeOldImageCache()
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
-               cmd = ["convert", file, output_name]
-               debug("""Convert command: '"%s"'""" % '" "'.join(cmd), DEBUG_LEVEL_DEBUG)
-               return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
-
-       def changeTo(self, file):
+               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 _fixDesktopPList(self):
+               """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor"""
+               try:
+                       import Foundation
+                       desktopPList = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation)
+                       # Remove all but the 'default' entry
+                       for k in desktopPList['Background'].keys():
+                               if k == 'default':
+                                       continue
+                               desktopPList['Background'].removeObjectForKey_(k)
+                       # Store the plist again (Make sure we write it out atomically -- Don't want to break finder)
+                       desktopPList.writeToFile_atomically_(self._DesktopPlistLocation, True)
+               except ImportError:
+                       debug('Could not import the Foundation module, you may have problems with dual screens', DEBUG_LEVEL_MEDIUM)
+
+       def _setImage(self, file):
                output_name, ret = self._convertImageFormat(file)
-               if ret: # Since 0 indicates success
-                       debug("Convert failed %s" % ret)
+               if not ret:
+                       debug("Convert failed")
                        return False
+               self._fixDesktopPList()
                cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % output_name
                debug(cmd, DEBUG_LEVEL_DEBUG)
                return not commands.getstatusoutput(cmd)[0]
 
 class __GnomeChanger(__BaseChanger):
-       def changeTo(self, file):
+       def _setImage(self, file):
                cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file]
                debug(cmd, DEBUG_LEVEL_DEBUG)
                return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
 
 class __KDEChanger(__BaseChanger):
-       def changeTo(self, file):
+       def _setImage(self, file):
                cmds = []
                for group in ('Desktop0', 'Desktop0Screen0'):
                        base = ['kwriteconfig', '--file', 'kdesktoprc', '--group', group, '--key']