- # Save current time
- global_db[timestamp_key] = current_time
-
- # Select a random image and check if we've seen it recently;
- # loop until we can find a new image (never seen before) or old enough.
- for i in xrange(len(images)): # ensure the loop is not infinite
- image_name = random.choice(images)
- if global_db.has_key(image_name):
- image_time = global_db[image_name]
- if current_time - image_time > min_delay:
- break
- else:
- break
- global_db[image_name] = current_time
-
- # Save filename
- if global_db.has_key(filename_key):
- global_db[old_filename_key] = global_db[filename_key]
- global_db[filename_key] = image_name
-
- program_options = ["xli", "-onroot", "-quiet"] + \
- ["-center", "-border", random.choice(borders), "-zoom", "auto",
- image_name]
-
- rc = subprocess.call(program_options)
- if rc:
- error("cannot execute xli!")
-
- finally:
- # Unlock and close the lock file
- flock(lock_file, LOCK_UN)
- lock_file.close()
- # Flush and close the global persistent dictionary
- if global_db: global_db.close()
+ # Select a random image that has never been shown
+ 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(
+ '(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, 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.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:
+ error("cannot execute xli!")
+ else:
+ row.last_shown = current_time
+
+ finally:
+ # Unlock and close the lock file
+ flock(lock_file, LOCK_UN)
+ lock_file.close()