def _add_cb(self, button, *args): ''' Called when a new watch list entry should be added. ''' filechooser = Gtk.FileChooserDialog( action=Gtk.FileChooserAction.SELECT_FOLDER) filechooser.set_transient_for(self) filechooser.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT, Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT) result = filechooser.run() if filechooser.get_filename() is not None: directory = filechooser.get_filename() else: directory = u'' filechooser.destroy() directory = tools.relpath2root( directory, abs_fallback=prefs['portable allow abspath']) if not directory: # directory is None, means running in portable mode # and currect path is out of same mount point # so do not add directory to watchlist return if result == Gtk.ResponseType.ACCEPT \ and os.path.isdir(directory): self.library.backend.watchlist.add_directory(directory) self._fill_model(self._treeview.get_model()) self._changed = True
def write_fileinfo_file(self): '''Write current open file information.''' if self.file_loaded: path = self._window.imagehandler.get_real_path() if not path: # no file is loaded return path = tools.relpath2root( path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect image is out of same mount point # so do not create bookmarks return page_index = self._window.imagehandler.get_current_page() - 1 current_file_info = [path, page_index] with open(constants.FILEINFO_JSON_PATH, mode='wt', encoding='utf8') as config: json.dump(current_file_info, config, ensure_ascii=False, indent=2)
def load_style(self, path=None): if not path: return self.reset_style() # load userstyle from path provider = None try: csspath = tools.relpath2root(path) if not csspath: raise Exception('userstyle out of mount point is not allowed.') provider = Gtk.CssProvider.new() provider.load_from_path(csspath) except Exception as e: provider = None text = _('Failed to load userstyle: "{}", {}').format(path, e) log.warning(text) self.osd.show(text) finally: # always reset before setting userstyle self.reset_style() if not provider: # failed to load userstyle, stop return self.styleprovider = provider Gtk.StyleContext.add_provider_for_screen( self.get_screen(), self.styleprovider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
def add_book(self, path, collection=None): '''Add the archive at <path> to the library. If <collection> is not None, it is the collection that the books should be put in. Return True if the book was successfully added (or was already added). ''' path = tools.relpath2root(path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect path is out of same mount point # so do not add book to library return name = os.path.basename(path) info = archive_tools.get_archive_info(path) if info is None: return False format, pages, size = info # Thumbnail for the newly added book will be generated once it # is actually needed with get_book_thumbnail(). old = self._con.execute( '''select id from Book where path = ?''', (path, )).fetchone() try: cursor = self._con.cursor() if old is not None: cursor.execute( '''update Book set name = ?, pages = ?, format = ?, size = ? where path = ?''', (name, pages, format, size, path)) book_id = old else: cursor.execute( '''insert into Book (name, path, pages, format, size) values (?, ?, ?, ?, ?)''', (name, path, pages, format, size)) book_id = cursor.lastrowid book = backend_types._Book(book_id, name, path, pages, format, size, datetime.datetime.now().isoformat()) self.book_added(book) cursor.close() if collection is not None: self.add_book_to_collection(book_id, collection) return True except sqlite3.Error: log.error(_('! Could not add book "%s" to the library'), path) return False
def add_current_to_bookmarks(self): '''Add the currently viewed page to the list.''' name = self._image_handler.get_pretty_current_filename() path = self._image_handler.get_real_path() path = tools.relpath2root(path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect image is out of same mount point # so do not create bookmarks return page = self._image_handler.get_current_page() numpages = self._image_handler.get_number_of_pages() archive_type = self._file_handler.archive_type date_added = datetime.datetime.now() same_file_bookmarks = [] for bookmark in self._bookmarks: if bookmark.same_path(path): if bookmark.same_page(page): # Do not create identical bookmarks return else: same_file_bookmarks.append(bookmark) # If the same file was already bookmarked, ask to replace # the existing bookmarks before deleting them. if len(same_file_bookmarks) > 0: response = self.show_replace_bookmark_dialog( same_file_bookmarks, page) # Delete old bookmarks if response == Gtk.ResponseType.YES: for bookmark in same_file_bookmarks: self.remove_bookmark(bookmark) # Perform no action elif response not in (Gtk.ResponseType.YES, Gtk.ResponseType.NO): return self.add_bookmark_by_values(name, path, page, numpages, archive_type, date_added)
def terminate_program(self): '''Run clean-up tasks and exit the program.''' self.hide() if Gtk.main_level() > 0: Gtk.main_quit() if prefs['auto load last file'] and self.filehandler.file_loaded: path = self.imagehandler.get_real_path() path = tools.relpath2root( path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect image is out of same mount point # so do not save as last file prefs['path to last file'] = '' prefs['page of last file'] = 1 else: prefs['path to last file'] = self.imagehandler.get_real_path() prefs[ 'page of last file'] = self.imagehandler.get_current_page( ) else: prefs['path to last file'] = '' prefs['page of last file'] = 1 if prefs['hide all'] and self.hide_all_forced and self.fullscreen: prefs['hide all'] = False self.write_config_files() self.filehandler.close_file() if main_dialog._dialog is not None: main_dialog._dialog.close() backend.LibraryBackend().close() self.slideshow.stop()
def get_book_by_path(self, path): ''' Retrieves a book from the library, specified by C{path}. If the book doesn't exist, None is returned. Otherwise, a L{backend_types._Book} instance is returned. ''' path = tools.relpath2root(path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect path is out of same mount point # so book should not exist in library return cur = self.execute( '''select id, name, path, pages, format, size, added from book where path = ?''', (path, )) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None