import pprint
import ConfigParser
import StringIO
+import locale
+
+from pathlib import *
from wand.image import Image
from wand.color import Color
font = '/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf'
opacity = 0.6
+KDE_WALLPAPER_ROOT_DIR = '/usr/share/wallpapers'
+
DEFAULT_MIN_WIDTH = 800
DEFAULT_MIN_HEIGTH = 600
+re_lang_compl = re.compile(r'^([a-z_\-\d]+)', re.IGNORECASE)
+re_lang = re.compile(r'^([a-z]+)', re.IGNORECASE)
+
+locale.setlocale(locale.LC_ALL, '')
+
#-------------------------------------------------------------------------
def init_logging():
"""
#-------------------------------------------------------------------------
def get_max_monitor_geometry():
+ """
+ Returns the geometry of the monitor with the maximum HEIGHT.
+ """
max_width = DEFAULT_MIN_WIDTH
max_height = DEFAULT_MIN_HEIGTH
for m in range(nmons):
mg = screen.get_monitor_geometry(m)
log.debug("Monitor geometry %d: %d x %d", m, mg.width, mg.height)
- if mg.width > max_width:
- max_width = mg.width
if mg.height > max_height:
+ max_width = mg.width
max_height = mg.height
return (max_width, max_height)
return None
+#-------------------------------------------------------------------------
+def get_kde_wallpaper(img_dir, screen_width, screen_height):
+
+ if not img_dir.exists():
+ log.error("%r doesn't exists.", str(img_dir))
+ return None
+
+ if not img_dir.is_dir():
+ log.error("%r is not a dirctory.", str(img_dir))
+ return None
+
+ contents_dir = img_dir / 'contents'
+ images_dir = contents_dir / 'images'
+ meta_file = img_dir / 'metadata.desktop'
+
+ title = img_dir.name
+
+ if not images_dir.exists():
+ log.error("%r doesn't exists.", str(images_dir))
+ return None
+
+ if not images_dir.is_dir():
+ log.error("%r is not a dirctory.", str(images_dir))
+ return None
+
+ images = sorted(images_dir.glob('*'))
+ if not images:
+ log.error("No images in %r found.", str(images_dir))
+ return None
+
+ image = None
+ search_pattern = "%dx%d" % (screen_width, screen_height)
+ for img in images:
+ if img.stem == search_pattern:
+ image = img
+ log.debug("Found appropriate image %r.", str(image))
+ break
+
+ if not image:
+ image = images[-1]
+ log.debug("Took the biggest image %r.", str(image))
+
+ if meta_file.exists():
+
+ config = ConfigParser.RawConfigParser()
+ config.read(str(meta_file))
+ section = 'Desktop Entry'
+
+ if config.has_section(section):
+
+ cur_locale = locale.getlocale(locale.LC_MESSAGES)
+ log.debug("Current locale: %r", cur_locale)
+ lang_compl = None
+ lang = None
+
+ if cur_locale and cur_locale[0] is not None:
+ lang_compl = cur_locale[0]
+ match = re_lang.search(lang_compl)
+ if match:
+ lang = match.group(1)
+
+ meta_title = None
+ # Searching for Name[de_DE] ..
+ if lang_compl:
+ key = 'Name[%s]' % (lang_compl)
+ if config.has_option(section, key):
+ meta_title = config.get(section, key)
+ if meta_title:
+ meta_title = meta_title.strip()
+ if meta_title:
+ log.debug("Found title %r in metadata for language %r.",
+ meta_title, lang_compl)
+ else:
+ meta_title = None
+
+ # Searching for Name[de] ..
+ if not meta_title and lang:
+ key = 'Name[%s]' % (lang)
+ if config.has_option(section, key):
+ meta_title = config.get(section, key)
+ if meta_title:
+ meta_title = meta_title.strip()
+ if meta_title:
+ log.debug("Found title %r in metadata for language %r.",
+ meta_title, lang)
+ else:
+ meta_title = None
+
+ # Searching for Name ..
+ if not meta_title:
+ key = 'Name'
+ if config.has_option(section, key):
+ meta_title = config.get(section, key)
+ if meta_title:
+ meta_title = meta_title.strip()
+ if meta_title:
+ log.debug("Found title %r in metadata for language %r.",
+ meta_title, 'C')
+ else:
+ meta_title = None
+
+ if meta_title:
+ title = meta_title
+
+ else:
+ log.error("%r doesn't exists.", str(meta_file))
+
+ return (image, title)
+
#-------------------------------------------------------------------------
bg_dirs = [
#'/home/fbrehm/Bilder/Wallpaper',
#log.debug("Setting Wallpaper to: %r.", wpaper)
#if os.path.exists(wpaper) and os.path.isfile(wpaper):
# gsettings.set_string(KEY, wpaper)
+
log.debug("Setting Wallpaper to: %r.", new_img_fname)
gsettings.set_string(KEY, new_img_fname)
+kde_wallpaper_root_dir = Path(KDE_WALLPAPER_ROOT_DIR)
+
+if kde_wallpaper_root_dir.exists() and kde_wallpaper_root_dir.is_dir():
+ dirs = sorted(kde_wallpaper_root_dir.glob('*'))
+ kde_img_dir = random.choice(dirs)
+ loop = 0
+ while not kde_img_dir.is_dir() and loop < 100:
+ loop += 1
+ kde_img_dir = random.choice(dirs)
+
+ if kde_img_dir.is_dir():
+ (image, img_title) = get_kde_wallpaper(kde_img_dir,
+ screen_width, screen_height)
+ log.debug("Got a KDE wallpaper:\n %r\n %r.",
+ str(image), img_title)
+
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4