def get_url_name_item(self, uri, name=None): if not uri: return file = vfs.File.for_uri(uri) if file.is_native(): if not file.exists(): return icon = self.get_first_existing_icon(file.get_icon_names()) display_uri = uri[7:] else: icon = "folder-remote" display_uri = uri display_uri = unquote(display_uri) if name is None: match = url_pattern.match(uri) if match is not None: name = "/%s on %s" % (match.group(2), match.group(1)) else: if file.is_native(): filename = os.path.abspath(file.props.path) name = glib.filename_display_basename(filename) else: name = uri name = unquote(str(name)) item = self.create_menu_item(name, icon, _("Open '%s'") % display_uri) item.connect("activate", self.open_folder_cb, uri) return item
def parse_kfcom_file(filepath): """Extract the serialized command inside @filepath The file must be executable (comparable to a shell script) >>> parse_kfcom_file(__file__) # doctest: +ELLIPSIS Traceback (most recent call last): ... ExecutionError: ... (not executable) Return commands triple """ fobj = open(filepath, "rb") if not os.access(filepath, os.X_OK): raise ExecutionError(_('No permission to run "%s" (not executable)') % glib.filename_display_basename(filepath)) # strip shebang away data = fobj.read() if data.startswith("#!") and "\n" in data: shebang, data = data.split("\n", 1) try: id_ = conspickle.BasicUnpickler.loads(data) command_object = puid.resolve_unique_id(id_) except pickle.UnpicklingError, err: raise ExecutionError("Could not parse: %s" % unicode(err))
def add_folders_and_bookmarks(self): self.applet.icons.destroy_all() # Destroy all current local bookmark monitors for monitor in self.__monitors: monitor.cancel() self.__monitors = [] self.add_folder_icon(_("Home Folder"), "user-home", "file://%s" % user_path) # Add Desktop desktop_path = glib.get_user_special_dir(glib.USER_DIRECTORY_DESKTOP) if desktop_path != user_path: self.add_folder_icon(glib.filename_display_basename(desktop_path), "user-desktop", "file://%s" % desktop_path) # Add XDG user special folders for uri in xdg_user_uris: self.add_url_name(uri) # Add bookmarks if os.path.isfile(bookmarks_file): with open(bookmarks_file) as f: for url_name in (i.rstrip().split(" ", 1) for i in f): if url_name[0] not in xdg_user_uris: self.add_url_name(*url_name)
def get_name(self): """Get name as UTF-8 string (if possible).""" if self.infos['display-name']: ret = self.infos['display-name'] elif self.path: ret = glib.filename_display_basename(self.path) ret = ret.encode('utf8') elif self.infos['name']: ret = self.infos['name'] else: ret = self.gfile.get_basename() return ret
def add_url_name(self, uri, name=None): if not uri: return file = vfs.File.for_uri(uri) if file.is_native(): monitor = gio.File(uri).monitor_file() self.__monitors.append(monitor) monitor.connect("changed", self.file_changed_cb) if not file.exists(): return icon = None # Use a custom icon if it has been set info = gio.File(uri).query_info("metadata::custom-icon", gio.FILE_QUERY_INFO_NONE) custom_icon_uri = info.get_attribute_string("metadata::custom-icon") if custom_icon_uri is not None: icon = vfs.File.for_uri(custom_icon_uri) if icon is None or not icon.exists(): existing_icons = filter(self.icon_theme.has_icon, file.get_icon_names()) icon = existing_icons[0] if len(existing_icons) > 0 else "image-missing" else: icon = "folder-remote" if name is None: match = url_pattern.match(uri) if match is not None: name = "/%s on %s" % (match.group(2), match.group(1)) else: if file.is_native(): filename = os.path.abspath(file.props.path) name = glib.filename_display_basename(filename) else: name = uri name = unquote(str(name)) self.add_folder_icon(name, icon, uri)
def create_places_submenu(self, parent_menu): item = self.append_menu_item(parent_menu, _("Places"), "folder", None) menu = gtk.Menu() item.set_submenu(menu) user_path = os.path.expanduser("~/") home_item = self.append_menu_item(menu, _("Home Folder"), "user-home", _("Open your personal folder")) home_item.connect("activate", self.open_folder_cb, "file://%s" % user_path) # Add Desktop desktop_path = glib.get_user_special_dir(glib.USER_DIRECTORY_DESKTOP) if desktop_path != user_path: label = glib.filename_display_basename(desktop_path) desktop_item = self.append_menu_item(menu, label, "user-desktop", _("Open the contents of your desktop in a folder")) desktop_item.connect("activate", self.open_folder_cb, "file://%s" % desktop_path) """ Bookmarks """ self.places_menu = menu self.bookmarks_items = [] self.append_bookmarks() # Monitor bookmarks file for changes bookmarks_file = os.path.expanduser("~/.gtk-bookmarks") self.__bookmarks_monitor = gio.File(bookmarks_file).monitor_file() # keep a reference to avoid getting it garbage collected def bookmarks_changed_cb(monitor, file, other_file, event): if event == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT: with self.__rebuild_lock: self.append_bookmarks() # Refresh menu to re-initialize the widget self.places_menu.show_all() self.__bookmarks_monitor.connect("changed", bookmarks_changed_cb) menu.append(gtk.SeparatorMenuItem()) """ Devices """ added = False added |= self.append_awn_desktop(menu, "nautilus-computer") added |= self.append_awn_desktop(menu, "nautilus-cd-burner") # Set up volumes and mounts monitor self.__volumes_mounts_monitor = gio.volume_monitor_get() self.__volumes_index = len(self.places_menu) - len(self.bookmarks_items) self.volume_items = [] self.append_volumes() # TODO if added is False and no volumes, then their are two separators if added: menu.append(gtk.SeparatorMenuItem()) added = False added |= self.append_awn_desktop(menu, "network-scheme") self.__mounts_index = len(self.places_menu) - len(self.volume_items) - len(self.bookmarks_items) self.mount_items = [] self.append_mounts() # TODO if added is False and no mounts, then there are two separators # Connect signals after having initialized volumes and mounts for signal in ("volume-added", "volume-changed", "volume-removed", "mount-added", "mount-changed", "mount-removed"): self.__volumes_mounts_monitor.connect(signal, self.refresh_volumes_mounts_cb) ncs_exists = os.path.exists(commands.getoutput("which nautilus-connect-server")) if ncs_exists: connect_item = self.append_menu_item(menu, _("Connect to Server..."), "applications-internet", _("Connect to a remote computer or shared disk")) connect_item.connect("activate", lambda w: subprocess.Popen("nautilus-connect-server")) added |= ncs_exists if added: menu.append(gtk.SeparatorMenuItem()) self.append_awn_desktop(menu, "gnome-search-tool") """ Recent Documents """ self.create_documents_submenu(menu)
def create_places_submenu(self, parent_menu): item = self.append_menu_item(parent_menu, _("Places"), "folder", None) menu = gtk.Menu() item.set_submenu(menu) user_path = os.path.expanduser("~/") home_item = self.append_menu_item(menu, _("Home Folder"), "user-home", _("Open your personal folder")) home_item.connect("activate", self.open_folder_cb, "file://%s" % user_path) # Add Desktop desktop_path = glib.get_user_special_dir(glib.USER_DIRECTORY_DESKTOP) if desktop_path != user_path: label = glib.filename_display_basename(desktop_path) desktop_item = self.append_menu_item(menu, label, "user-desktop", _("Open the contents of your desktop in a folder")) desktop_item.connect("activate", self.open_folder_cb, "file://%s" % desktop_path) """ Bookmarks """ self.places_menu = menu self.bookmarks_items = [] self.append_bookmarks() # Monitor bookmarks file for changes bookmarks_file = os.path.expanduser("~/.gtk-bookmarks") self.__bookmarks_monitor = gio.File(bookmarks_file).monitor_file() # keep a reference to avoid getting it garbage collected def bookmarks_changed_cb(monitor, file, other_file, event): if event == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT: with self.__rebuild_lock: self.append_bookmarks() # Refresh menu to re-initialize the widget self.places_menu.show_all() self.__bookmarks_monitor.connect("changed", bookmarks_changed_cb) menu.append(gtk.SeparatorMenuItem()) """ Devices """ added = False added |= self.append_awn_desktop(menu, "nautilus-computer") added |= self.append_awn_desktop(menu, "nautilus-cd-burner") # Set up volumes and mounts monitor self.__volumes_mounts_monitor = gio.volume_monitor_get() self.__volumes_index = len(self.places_menu) - len(self.bookmarks_items) self.volume_items = [] self.append_volumes() # TODO if added is False and no volumes, then their are two separators if added: menu.append(gtk.SeparatorMenuItem()) added = False added |= self.append_awn_desktop(menu, "network-scheme") self.__mounts_index = len(self.places_menu) - len(self.volume_items) - len(self.bookmarks_items) self.mount_items = [] self.append_mounts() # TODO if added is False and no mounts, then there are two separators # Connect signals after having initialized volumes and mounts for signal in ("volume-added", "volume-changed", "volume-removed", "mount-added", "mount-changed", "mount-removed"): self.__volumes_mounts_monitor.connect(signal, self.refresh_volumes_mounts_cb) ncs_exists = os.path.exists(commands.getoutput("which nautilus-connect-server")) if ncs_exists: connect_item = self.append_menu_item(menu, _("Connect to Server..."), "applications-internet", _("Connect to a remote computer or shared disk")) connect_item.connect("activate", lambda w: subprocess.Popen("nautilus-connect-server")) added |= ncs_exists if added: menu.append(gtk.SeparatorMenuItem()) self.append_awn_desktop(menu, "mate-search-tool") """ Recent Documents """ self.create_documents_submenu(menu)
def __init__(self, path): basename = glib.filename_display_basename(path) nameroot, ext = os.path.splitext(basename) FileLeaf.__init__(self, path, _("%s template") % nameroot)
# strip shebang away data = fobj.read() if data.startswith("#!") and "\n" in data: shebang, data = data.split("\n", 1) try: id_ = conspickle.BasicUnpickler.loads(data) command_object = puid.resolve_unique_id(id_) except pickle.UnpicklingError, err: raise ExecutionError("Could not parse: %s" % unicode(err)) except Exception: raise ExecutionError('"%s" is not a saved command' % os.path.basename(filepath)) if command_object is None: raise ExecutionError(_('Command in "%s" is not available') % glib.filename_display_basename(filepath)) try: return tuple(command_object.object) except (AttributeError, TypeError): raise ExecutionError('"%s" is not a saved command' % os.path.basename(filepath)) finally: glib.idle_add(update_icon, command_object, filepath) def save_to_file(command_leaf, filename): fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o777) wfile = os.fdopen(fd, "wb") try: wfile.write(KUPFER_COMMAND_SHEBANG) pickle.dump(puid.get_unique_id(command_leaf), wfile, 0)