X-Git-Url: https://git.phdru.name/?p=xsetbg.git;a=blobdiff_plain;f=xsetbg.py;h=68828cf567b5db4c43875df9e5c36b737c02362d;hp=8f4f6c8b96029aa52da78431c73bb14e910d9e7e;hb=HEAD;hpb=2f99c9bc2e47a031db73de534ff56fec6788854e diff --git a/xsetbg.py b/xsetbg.py index 8f4f6c8..825b20e 100644 --- a/xsetbg.py +++ b/xsetbg.py @@ -6,12 +6,6 @@ using xli program. """ -__author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2000-2015 PhiloSoft Design" -__license__ = "GNU GPL" - -__all__ = ['change'] - from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB from datetime import timedelta import os @@ -24,6 +18,8 @@ from time import time from xsetbg_conf import xsetbg_dir, xsetbg_conf from xsetbg_db import xsetbg_db +__all__ = ['change'] + def error(error_str): sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str)) @@ -54,11 +50,11 @@ if xsetbg_conf.has_option("xsetbg", "min_delay"): min_delay = xsetbg_conf.get("xsetbg", "min_delay") # Borrowed from http://stackoverflow.com/a/2765366 - td_re = re.compile('(?:(?P\d+)y)?' - '(?:(?P\d+)m)?' - '(?:(?P\d+)d)?' - '(?:T(?:(?P\d+)h)?' - '(?:(?P\d+)m)?(?:(?P\d+)s)?)?') + td_re = re.compile('(?:(?P\\d+)y)?' + '(?:(?P\\d+)m)?' + '(?:(?P\\d+)d)?' + '(?:T(?:(?P\\d+)h)?' + '(?:(?P\\d+)m)?(?:(?P\\d+)s)?)?') td_dict = td_re.match(min_delay).groupdict(0) delta = timedelta(days=int(td_dict['days']) + (int(td_dict['months']) * 30) + @@ -76,11 +72,14 @@ else: min_delay = 3600*24 # 24 hours -if xsetbg_db.select('last_shown IS NULL OR last_shown < %d' % - (time() - min_delay)).count() == 0: +if xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NULL OR last_shown < %d)' % + (time() - min_delay)).count() == 0: error("No unshown images found. Run rescan_fs.py " "or decrease min_delay. Abort.") +fs_encoding = xsetbg_conf.get("images", "fs_encoding") + def change(force=False): # Use the program's file as the lock file: @@ -94,8 +93,9 @@ def change(force=False): return try: - timestamp = xsetbg_db.select('last_shown IS NOT NULL', - orderBy='-last_shown')[0].last_shown + timestamp = xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NOT NULL)', + orderBy='-last_shown')[0].last_shown current_time = time() if not force and timestamp is not None and \ @@ -104,24 +104,36 @@ def change(force=False): return # Select a random image that has never been shown - not_shown_select = xsetbg_db.select('last_shown IS NULL') + not_shown_select = xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NULL)') not_shown_count = not_shown_select.count() if not_shown_count: row = not_shown_select[random.randint(0, not_shown_count - 1)] else: old_shown_select = xsetbg_db.select( - 'last_shown IS NOT NULL AND last_shown < %d' % + '(is_image = 1) AND ' + '(last_shown IS NOT NULL AND last_shown < %d)' % current_time - min_delay) old_shown_count = old_shown_select.count() if old_shown_count: - row = old_shown_select[random.randint(0, not_shown_count - 1)] + row = old_shown_select[random.randint(0, old_shown_count - 1)] else: error("No images to show found. Run rescan_fs.py " "or decrease min_delay. Abort.") program_options = ["xli", "-border", random.choice(borders), "-center", "-onroot", "-quiet", "-zoom", "auto", - row.full_name] + row.full_name.encode(fs_encoding)] + + ext = os.path.splitext(row.full_name)[1].lower() + if ext in ('tif', 'tiff'): + program_options[0] = 'xsetbg' + + if ext == 'webp': + program_options = ['display', '-backdrop', + '-background', random.choice(borders), + '-window', 'root', + row.full_name.encode(fs_encoding)] rc = subprocess.call(program_options) if rc: