def __on_download_switch_changed(self, switch, state): if self.download_switch.get_active(): db.Book.update(offline=True).where(db.Book.id == self.book.id).execute() OfflineCache().add(self.book) else: db.Book.update(offline=False, downloaded=False).where(db.Book.id == self.book.id).execute() OfflineCache().remove(self.book) self._set_book_download_status(False)
def load_last_book(): """ Load the last played book into the player. """ global __current_track global __player last_book = cozy.db.Settings.get().last_played_book if last_book and last_book.position != 0: query = cozy.db.Track.select().where( cozy.db.Track.id == last_book.position) if query.exists(): last_track = query.get() if last_track: __player.set_state(Gst.State.NULL) if cozy.filesystem_monitor.FilesystemMonitor().is_track_online( last_track): path = last_track.file else: path = OfflineCache().get_cached_path(last_track) if not path: path = last_track.file __player.set_property("uri", "file://" + path) __player.set_state(Gst.State.PAUSED) __current_track = last_track cozy.db.Book.update(last_played=int(time.time())).where( cozy.db.Book.id == last_book.id).execute() emit_event("track-changed", last_track)
def load_file(track): """ Loads a given track into the player. :param track: track to be loaded """ global __current_track global __player if get_gst_player_state() == Gst.State.PLAYING: save_current_track_position() save_current_book_position(__current_track) __current_track = track emit_event("stop") __player.set_state(Gst.State.NULL) init() if cozy.filesystem_monitor.FilesystemMonitor().is_track_online(track): path = track.file else: path = OfflineCache().get_cached_path(track) if not path: path = track.file __player.set_property("uri", "file://" + path) __player.set_state(Gst.State.PAUSED) save_current_book_position(__current_track) cozy.db.Settings.update(last_played_book=__current_track.book).execute() cozy.db.Book.update(last_played=int(time.time())).where( cozy.db.Book.id == __current_track.book.id).execute() emit_event("track-changed", track)
def __init__(self): self.ui = cozy.ui.CozyUI() builder = self.ui.window_builder self.name_label = builder.get_object("info_book_label") self.author_label = builder.get_object("info_author_label") self.download_box = builder.get_object("info_download_box") self.download_label = builder.get_object("info_download_label") self.download_image = builder.get_object("info_download_image") self.download_switch = builder.get_object("info_download_switch") self.published_label = builder.get_object("info_published_label") self.last_played_label = builder.get_object("info_last_played_label") self.total_label = builder.get_object("info_total_label") self.remaining_label = builder.get_object("info_remaining_label") self.progress_bar = builder.get_object("book_progress_bar") self.cover_img = builder.get_object("info_cover_image") self.track_list_container = builder.get_object("track_list_container") self.published_text = builder.get_object("info_published_text") self.remaining_text = builder.get_object("info_remaining_text") self.play_book_button = builder.get_object("play_book_button") self.play_book_button.connect("clicked", self.__on_play_clicked) self.play_img = builder.get_object("play_img1") self.pause_img = builder.get_object("pause_img1") self.scroller = builder.get_object("book_overview_scroller") if Gtk.get_minor_version() > 20: self.scroller.props.propagate_natural_height = True self.ui.speed.add_listener(self.__ui_changed) player.add_player_listener(self.__player_changed) Settings().add_listener(self.__settings_changed) OfflineCache().add_listener(self.__on_offline_cache_changed)
def __on_storage_changed(event, message): """ """ global __player if event == "storage-offline": if get_current_track() and message in get_current_track().file: cached_path = OfflineCache().get_cached_path(get_current_track()) if not cached_path: stop() unload() emit_event("stop")
def update_database(ui, force=False): """ Scans the audio book directory for changes and new files. Also removes entries from the db that are no longer existent. """ paths = [] for location in cozy.db.Storage.select(): if os.path.exists(location.path): paths.append(location.path) # clean artwork cache artwork_cache.delete_artwork_cache() # are UI buttons currently blocked? player_blocked, importer_blocked = ui.get_ui_buttons_blocked() i = 0 percent_counter = 0 file_count = 0 for path in paths: file_count += sum([len(files) for r, d, files in os.walk(path)]) percent_threshold = file_count / 1000 failed = "" tracks_to_import = [] # Tracks which changed and need to be updated if they are cached tracks_cache_update = [] start = time.time() for path in paths: for directory, subdirectories, files in os.walk(path): for file in files: if file.lower().endswith( ('.mp3', '.ogg', '.flac', '.m4a', '.wav')): path = os.path.join(directory, file) imported = True try: if force: crc = None if tools.get_glib_settings().get_boolean( "use-crc32"): crc = __crc32_from_file(path) imported, ignore = import_file( file, directory, path, True, crc) tracks_cache_update.append(path) # Is the track already in the database? elif cozy.db.Track.select().where( cozy.db.Track.file == path).count() < 1: imported, track_data = import_file( file, directory, path) if track_data: tracks_to_import.append(track_data) # Has the track changed on disk? elif tools.get_glib_settings().get_boolean( "use-crc32"): crc = __crc32_from_file(path) # Is the value in the db already crc32 or is the crc changed? if (cozy.db.Track.select().where( cozy.db.Track.file == path).first().modified != crc or cozy.db.Track.select().where( cozy.db.Track.file == path).first().crc32 != True): imported, ignore = import_file( file, directory, path, True, crc) tracks_cache_update.append(path) # Has the modified date changed or is the value still a crc? elif (cozy.db.Track.select().where( cozy.db.Track.file == path).first().modified < os.path.getmtime(path) or cozy.db.Track.select().where( cozy.db.Track.file == path).first().crc32 != False): imported, ignore = import_file(file, directory, path, update=True) tracks_cache_update.append(path) if not imported: failed += path + "\n" except Exception as e: log.warning("Could not import file: " + path) log.warning(traceback.format_exc()) failed += path + "\n" i = i + 1 if len(tracks_to_import) > 100: write_tracks_to_db(tracks_to_import) tracks_to_import = [] # don't flood gui updates if percent_counter < percent_threshold: percent_counter = percent_counter + 1 else: percent_counter = 1 Gdk.threads_add_idle( GLib.PRIORITY_DEFAULT_IDLE, ui.titlebar.progress_bar.set_fraction, i / file_count) Gdk.threads_add_idle( GLib.PRIORITY_DEFAULT_IDLE, ui.titlebar.update_progress_bar.set_fraction, i / file_count) write_tracks_to_db(tracks_to_import) end = time.time() log.info("Total import time: " + str(end - start)) # remove entries from the db that are no longer existent cozy.db.remove_invalid_entries() artwork_cache.generate_artwork_cache() Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.refresh_content) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.switch_to_playing) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.check_for_tracks) if len(failed) > 0: Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.display_failed_imports, failed) OfflineCache().update_cache(tracks_cache_update) OfflineCache()._process_queue()