/usr/share/cinnamon/cinnamon-slideshow/cinnamon-slideshow.py is in cinnamon-common 3.6.7-8ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | #! /usr/bin/python2
from __future__ import print_function
from gi.repository import Gio, GLib
import dbus
import dbus.service
import dbus.glib
from dbus.mainloop.glib import DBusGMainLoop
import random
import os
import locale
from xml.etree import ElementTree
SLIDESHOW_DBUS_NAME = "org.Cinnamon.Slideshow"
SLIDESHOW_DBUS_PATH = "/org/Cinnamon/Slideshow"
BACKGROUND_COLLECTION_TYPE_DIRECTORY = "directory"
BACKGROUND_COLLECTION_TYPE_XML = "xml"
class CinnamonSlideshow(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName(SLIDESHOW_DBUS_NAME, bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, SLIDESHOW_DBUS_PATH)
self.slideshow_settings = Gio.Settings(schema="org.cinnamon.desktop.background.slideshow")
self.background_settings = Gio.Settings(schema="org.cinnamon.desktop.background")
if self.slideshow_settings.get_boolean("slideshow-paused"):
self.slideshow_settings.set_boolean("slideshow-paused", False)
self.image_playlist = []
self.used_image_playlist = []
self.images_ready = False
self.update_in_progress = False
self.current_image = self.background_settings.get_string("picture-uri")
self.update_id = 0
self.loop_counter = self.slideshow_settings.get_int("delay")
self.folder_monitor = None
self.folder_monitor_id = 0
@dbus.service.method(SLIDESHOW_DBUS_NAME, in_signature='', out_signature='')
def begin(self):
self.setup_slideshow()
@dbus.service.method(SLIDESHOW_DBUS_NAME, in_signature='', out_signature='')
def end(self):
if self.update_id > 0:
GLib.source_remove(self.update_id)
self.update_id = 0
ml.quit()
@dbus.service.method(SLIDESHOW_DBUS_NAME, in_signature='', out_signature='')
def getNextImage(self):
if self.update_id > 0:
GLib.source_remove(self.update_id)
self.update_id = 0
self.loop_counter = self.slideshow_settings.get_int("delay")
self.start_mainloop()
def setup_slideshow(self):
self.load_settings()
self.connect_signals()
self.gather_images()
if self.collection_type == BACKGROUND_COLLECTION_TYPE_DIRECTORY:
self.connect_folder_monitor()
self.start_mainloop()
def format_source(self, type, path):
# returns 'type://path'
return ("%s://%s" % (type, path))
def load_settings(self):
self.random_order = self.slideshow_settings.get_boolean("random-order")
self.collection = self.slideshow_settings.get_string("image-source")
self.collection_path = ""
self.collection_type = None
if self.collection != "" and "://" in self.collection:
(self.collection_type, self.collection_path) = self.collection.split("://")
self.collection_path = os.path.expanduser(self.collection_path)
def connect_signals(self):
self.slideshow_settings.connect("changed::image-source", self.on_slideshow_source_changed)
self.slideshow_settings.connect("changed::random-order", self.on_random_order_changed)
self.background_settings.connect("changed::picture-uri", self.on_picture_uri_changed)
def connect_folder_monitor(self):
folder_path = Gio.file_new_for_path(self.collection_path)
self.folder_monitor = folder_path.monitor_directory(0, None)
self.folder_monitor_id = self.folder_monitor.connect("changed", self.on_monitored_folder_changed)
def disconnect_folder_monitor(self):
if self.folder_monitor_id > 0:
self.folder_monitor.disconnect(self.folder_monitor_id)
self.folder_monitor_id = 0
def gather_images(self):
if self.collection_type == BACKGROUND_COLLECTION_TYPE_DIRECTORY:
folder_at_path = Gio.file_new_for_path(self.collection_path)
if folder_at_path.query_exists(None):
folder_at_path.enumerate_children_async("standard::name,standard::type,standard::content-type",
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_LOW,
None,
self.gather_images_cb,
None)
elif self.collection_type == BACKGROUND_COLLECTION_TYPE_XML:
pictures = self.parse_xml_backgrounds_list(self.collection_path)
for picture in pictures:
filename = picture["filename"]
self.add_image_to_playlist(filename)
def gather_images_cb(self, obj, res, user_data):
all_files = []
enumerator = obj.enumerate_children_finish(res)
def on_next_file_complete(obj, res, user_data=all_files):
files = obj.next_files_finish(res)
file_list = all_files
if len(files) is not 0:
file_list = file_list.extend(files)
enumerator.next_files_async(100, GLib.PRIORITY_LOW, None, on_next_file_complete, None)
else:
enumerator.close(None)
self.ensure_file_is_image(file_list)
enumerator.next_files_async(100, GLib.PRIORITY_LOW, None, on_next_file_complete, all_files)
def ensure_file_is_image(self, file_list):
for item in file_list:
file_type = item.get_file_type()
if file_type is not Gio.FileType.DIRECTORY:
file_contents = item.get_content_type()
if file_contents.startswith("image"):
self.add_image_to_playlist(self.collection_path + "/" + item.get_name())
def add_image_to_playlist(self, file_path):
image = Gio.file_new_for_path(file_path)
image_uri = image.get_uri()
self.image_playlist.append(image_uri)
if self.collection_type == BACKGROUND_COLLECTION_TYPE_DIRECTORY:
self.image_playlist.sort()
self.images_ready = True
def on_slideshow_source_changed(self, settings, key):
if self.update_id > 0:
GLib.source_remove(self.update_id)
self.update_id = 0
self.disconnect_folder_monitor()
self.image_playlist = []
self.used_image_playlist = []
self.images_ready = False
self.collection = self.slideshow_settings.get_string("image-source")
self.collection_path = ""
self.collection_type = None
if self.collection != "" and "://" in self.collection:
(self.collection_type, self.collection_path) = self.collection.split("://")
self.collection_path = os.path.expanduser(self.collection_path)
if self.collection_type == BACKGROUND_COLLECTION_TYPE_DIRECTORY:
self.connect_folder_monitor()
self.gather_images()
self.loop_counter = self.slideshow_settings.get_int("delay")
self.start_mainloop()
def on_monitored_folder_changed(self, monitor, file1, file2, event_type):
try:
if event_type == Gio.FileMonitorEvent.DELETED:
file_uri = file1.get_uri()
if self.image_playlist.count(file_uri) > 0:
index_to_remove = self.image_playlist.index(file_uri)
del self.image_playlist[index_to_remove]
elif self.used_image_playlist.count(file_uri) > 0:
index_to_remove = self.used_image_playlist.index(file_uri)
del self.used_image_playlist[index_to_remove]
if event_type == Gio.FileMonitorEvent.CREATED:
file_path = file1.get_path()
file_info = file1.query_info("standard::type,standard::content-type", Gio.FileQueryInfoFlags.NONE, None)
file_type = file_info.get_file_type()
if file_type is not Gio.FileType.DIRECTORY:
file_contents = file_info.get_content_type()
if file_contents.startswith("image"):
self.add_image_to_playlist(file_path)
except Exception:
pass
def on_random_order_changed(self, settings, key):
self.random_order = self.slideshow_settings.get_boolean("random-order")
def on_picture_uri_changed(self, settings, key):
if self.update_in_progress:
return
else:
if self.background_settings.get_string("picture-uri") != self.current_image:
self.slideshow_settings.set_boolean("slideshow-enabled", False)
def start_mainloop(self):
if self.update_id > 0:
GLib.source_remove(self.update_id)
self.update_id = 0
if not self.images_ready:
self.update_id = GLib.timeout_add_seconds(1, self.start_mainloop)
else:
if self.loop_counter >= self.slideshow_settings.get_int("delay") and not self.slideshow_settings.get_boolean("slideshow-paused"):
self.loop_counter = 1
self.update_background()
self.update_id = GLib.timeout_add_seconds(60, self.start_mainloop)
else:
self.loop_counter = self.loop_counter + 1
self.update_id = GLib.timeout_add_seconds(60, self.start_mainloop)
def update_background(self):
if self.update_in_progress:
return
self.update_in_progress = True
if len(self.image_playlist) == 0:
self.move_used_images_to_original_playlist()
next_image = self.get_next_image_from_list()
if next_image is not None:
self.background_settings.set_string("picture-uri", next_image)
self.current_image = next_image
self.update_in_progress = False
def get_next_image_from_list(self):
if self.random_order:
index = random.randint(0, len(self.image_playlist) - 1)
image = self.image_playlist[index]
else:
index = 0
image = self.image_playlist[index]
self.move_image_to_used_playlist(index, image)
return image
def move_image_to_used_playlist(self, index, image):
self.image_playlist.pop(index)
self.used_image_playlist.append(image)
def move_used_images_to_original_playlist(self):
self.image_playlist = self.used_image_playlist
if self.collection_type == BACKGROUND_COLLECTION_TYPE_DIRECTORY:
self.image_playlist.sort()
self.used_image_playlist = []
# TAKEN FROM CS_BACKGROUND
def splitLocaleCode(self, localeCode):
loc = localeCode.partition("_")
loc = (loc[0], loc[2])
return loc
def getLocalWallpaperName(self, names, loc):
result = ""
mainLocFound = False
for wp in names:
wpLoc = wp[0]
wpName = wp[1]
if wpLoc == ("", ""):
if not mainLocFound:
result = wpName
elif wpLoc[0] == loc[0]:
if wpLoc[1] == loc[1]:
return wpName
elif wpLoc[1] == "":
result = wpName
mainLocFound = True
return result
def parse_xml_backgrounds_list(self, filename):
try:
locAttrName = "{http://www.w3.org/XML/1998/namespace}lang"
loc = self.splitLocaleCode(locale.getdefaultlocale()[0])
res = []
f = open(filename)
rootNode = ElementTree.fromstring(f.read())
f.close()
if rootNode.tag == "wallpapers":
for wallpaperNode in rootNode:
if wallpaperNode.tag == "wallpaper" and wallpaperNode.get("deleted") != "true":
wallpaperData = {"metadataFile": filename}
names = []
for prop in wallpaperNode:
if type(prop.tag) == str:
if prop.tag != "name":
wallpaperData[prop.tag] = prop.text
else:
propAttr = prop.attrib
wpName = prop.text
locName = self.splitLocaleCode(propAttr.get(locAttrName)) if locAttrName in propAttr else ("", "")
names.append((locName, wpName))
wallpaperData["name"] = self.getLocalWallpaperName(names, loc)
if "filename" in wallpaperData and wallpaperData["filename"] != "" and os.path.exists(wallpaperData["filename"]) and os.access(wallpaperData["filename"], os.R_OK):
if wallpaperData["name"] == "":
wallpaperData["name"] = os.path.basename(wallpaperData["filename"])
res.append(wallpaperData)
return res
except Exception as detail:
print(detail)
return []
###############
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
sessionBus = dbus.SessionBus()
request = sessionBus.request_name(SLIDESHOW_DBUS_NAME, dbus.bus.NAME_FLAG_DO_NOT_QUEUE)
if request != dbus.bus.REQUEST_NAME_REPLY_EXISTS:
slideshow = CinnamonSlideshow()
else:
print("cinnamon-slideshow already running.")
quit()
ml = GLib.MainLoop.new(None, True)
ml.run()
|