def idle(): if self.current != directory: # Modified from another thread. return model = self.model view = self.tree if cursor_file: cursor_uri = cursor_file.get_uri() cursor_row = -1 model.clear() row = 0 for sortname, name, f in subdirs: model.append((f, self.directory, name, '', True)) uri = f.get_uri() if ( cursor_file and cursor_row == -1 and (cursor_uri == uri or cursor_uri.startswith(uri + '/')) ): cursor_row = row row += 1 for sortname, name, f in subfiles: size = ( f.query_info( 'standard::size', Gio.FileQueryInfoFlags.NONE, None ).get_size() // 1000 ) # locale.format_string does not support unicode objects # correctly, so we call it with an str and convert the # locale-dependent output to unicode. size = locale.format_string('%d', size, True) # TRANSLATORS: File size (1 kB = 1000 bytes) size = _('%s kB') % unicode(size, locale.getpreferredencoding()) model.append((f, self.track, name, size, False)) if cursor_file and cursor_row == -1 and cursor_uri == f.get_uri(): cursor_row = row row += 1 if cursor_file and cursor_row != -1: view.set_cursor((cursor_row,)) else: view.set_cursor((0,)) if view.get_realized(): view.scroll_to_point(0, 0) self.entry.set_text(directory.get_parse_name()) if history: self.back.set_sensitive(True) self.history[self.i + 1 :] = [self.current] self.i = len(self.history) - 1 self.forward.set_sensitive(False) self.up.set_sensitive(bool(directory.get_parent()))
def _setup_widgets(self): """ Sets up the widgets for the files panel """ self.directory = icons.MANAGER.pixbuf_from_icon_name( 'folder', Gtk.IconSize.SMALL_TOOLBAR ) self.track = icons.MANAGER.pixbuf_from_icon_name( 'audio-x-generic', Gtk.IconSize.SMALL_TOOLBAR ) self.back = self.builder.get_object('files_back_button') self.back.connect('clicked', self.go_back) self.forward = self.builder.get_object('files_forward_button') self.forward.connect('clicked', self.go_forward) self.up = self.builder.get_object('files_up_button') self.up.connect('clicked', self.go_up) self.builder.get_object('files_refresh_button').connect('clicked', self.refresh) self.builder.get_object('files_home_button').connect('clicked', self.go_home) # Set up the location bar self.location_bar = self.builder.get_object('files_entry') self.location_bar.connect('changed', self.on_location_bar_changed) event.add_ui_callback( self.fill_libraries_location, 'libraries_modified', self.collection ) self.fill_libraries_location() self.entry = self.location_bar.get_children()[0] self.entry.connect('activate', self.entry_activate) # Set up the search entry self.filter = guiutil.SearchEntry(self.builder.get_object('files_search_entry')) self.filter.connect( 'activate', lambda *e: self.load_directory( self.current, history=False, keyword=unicode(self.filter.get_text(), 'utf-8'), ), )
def load_directory(self, directory, history=True, keyword=None, cursor_file=None): """ Load a directory into the files view. :param history: whether to record in history :param keyword: filter string :param cursor_file: file to (attempt to) put the cursor on. Will put the cursor on a subdirectory if the file is under it. """ self.current = directory try: infos = directory.enumerate_children( 'standard::is-hidden,' 'standard::name,standard::display-name,standard::type', Gio.FileQueryInfoFlags.NONE, None) except GLib.Error as e: logger.exception(e) if directory.get_path( ) != xdg.homedir: # Avoid infinite recursion. self.load_directory( Gio.File.new_for_commandline_arg(xdg.homedir), history, keyword, cursor_file) return if self.current != directory: # Modified from another thread. return settings.set_option('gui/files_panel_dir', directory.get_uri()) subdirs = [] subfiles = [] for info in infos: if info.get_is_hidden(): # Ignore hidden files. They can still be accessed manually from # the location bar. continue name = unicode(info.get_display_name(), 'utf-8') low_name = name.lower() if keyword and keyword.lower() not in low_name: continue f = directory.get_child(info.get_name()) ftype = info.get_file_type() sortname = xl.unicode.strxfrm(name) if ftype == Gio.FileType.DIRECTORY: subdirs.append((sortname, name, f)) elif any(low_name.endswith('.' + ext) for ext in metadata.formats): subfiles.append((sortname, name, f)) subdirs.sort() subfiles.sort() def idle(): if self.current != directory: # Modified from another thread. return model = self.model view = self.tree if cursor_file: cursor_uri = cursor_file.get_uri() cursor_row = -1 model.clear() row = 0 for sortname, name, f in subdirs: model.append((f, self.directory, name, '', True)) uri = f.get_uri() if cursor_file and cursor_row == -1 and \ (cursor_uri == uri or cursor_uri.startswith(uri + '/')): cursor_row = row row += 1 for sortname, name, f in subfiles: size = f.query_info('standard::size', Gio.FileQueryInfoFlags.NONE, None).get_size() // 1000 # locale.format_string does not support unicode objects # correctly, so we call it with an str and convert the # locale-dependent output to unicode. size = locale.format_string('%d', size, True) # TRANSLATORS: File size (1 kB = 1000 bytes) size = _('%s kB') % unicode(size, locale.getpreferredencoding()) model.append((f, self.track, name, size, False)) if cursor_file and cursor_row == -1 and cursor_uri == f.get_uri( ): cursor_row = row row += 1 if cursor_file and cursor_row != -1: view.set_cursor((cursor_row, )) else: view.set_cursor((0, )) if view.get_realized(): view.scroll_to_point(0, 0) self.entry.set_text(directory.get_parse_name()) if history: self.back.set_sensitive(True) self.history[self.i + 1:] = [self.current] self.i = len(self.history) - 1 self.forward.set_sensitive(False) self.up.set_sensitive(bool(directory.get_parent())) GLib.idle_add(idle)
def load_directory(self, directory, history=True, keyword=None, cursor_file=None): """ Load a directory into the files view. :param history: whether to record in history :param keyword: filter string :param cursor_file: file to (attempt to) put the cursor on. Will put the cursor on a subdirectory if the file is under it. """ self.current = directory try: infos = gfile_enumerate_children( directory, 'standard::display-name,standard::is-hidden,standard::name,standard::type', ) except GLib.Error as e: logger.exception(e) if directory.get_path() != xdg.homedir: # Avoid infinite recursion. self.load_directory( Gio.File.new_for_commandline_arg(xdg.homedir), history, keyword, cursor_file, ) return if self.current != directory: # Modified from another thread. return settings.set_option('gui/files_panel_dir', directory.get_uri()) subdirs = [] subfiles = [] for info in infos: if info.get_is_hidden(): # Ignore hidden files. They can still be accessed manually from # the location bar. continue name = unicode(info.get_display_name(), 'utf-8') low_name = name.lower() if keyword and keyword.lower() not in low_name: continue f = directory.get_child(info.get_name()) ftype = info.get_file_type() sortname = xl.unicode.strxfrm(name) if ftype == Gio.FileType.DIRECTORY: subdirs.append((sortname, name, f)) elif any(low_name.endswith('.' + ext) for ext in metadata.formats): subfiles.append((sortname, name, f)) subdirs.sort() subfiles.sort() def idle(): if self.current != directory: # Modified from another thread. return model = self.model view = self.tree if cursor_file: cursor_uri = cursor_file.get_uri() cursor_row = -1 model.clear() row = 0 for sortname, name, f in subdirs: model.append((f, self.directory, name, '', True)) uri = f.get_uri() if ( cursor_file and cursor_row == -1 and (cursor_uri == uri or cursor_uri.startswith(uri + '/')) ): cursor_row = row row += 1 for sortname, name, f in subfiles: size = ( f.query_info( 'standard::size', Gio.FileQueryInfoFlags.NONE, None ).get_size() // 1000 ) # locale.format_string does not support unicode objects # correctly, so we call it with an str and convert the # locale-dependent output to unicode. size = locale.format_string('%d', size, True) # TRANSLATORS: File size (1 kB = 1000 bytes) size = _('%s kB') % unicode(size, locale.getpreferredencoding()) model.append((f, self.track, name, size, False)) if cursor_file and cursor_row == -1 and cursor_uri == f.get_uri(): cursor_row = row row += 1 if cursor_file and cursor_row != -1: view.set_cursor((cursor_row,)) else: view.set_cursor((0,)) if view.get_realized(): view.scroll_to_point(0, 0) self.entry.set_text(directory.get_parse_name()) if history: self.back.set_sensitive(True) self.history[self.i + 1 :] = [self.current] self.i = len(self.history) - 1 self.forward.set_sensitive(False) self.up.set_sensitive(bool(directory.get_parent())) GLib.idle_add(idle)