def _convert_uri(uri): path = uri[len("file://") :] if uri.startswith("file://") else uri # Check if relative file exists and convert it if it does. if not any( uri.startswith(proto) for proto in filesystem.REMOTE_PROTOCOLS ) and not os.path.isabs(path): path = os.path.join(data_dir, path) assert os.path.isabs(path), path if os.path.exists(path): uri = urls.get_local_url(path) return uri
def on_insert_file(self, sel_text): dirs = self.main_window.journal.dirs file_chooser = self.main_window.builder.get_object("file_chooser") file_chooser.set_current_folder(dirs.last_file_dir) response = file_chooser.run() file_chooser.hide() if response == Gtk.ResponseType.OK: folder = file_chooser.get_current_folder() # Folder is None if the file was chosen from the "recently used" section. if folder: dirs.last_file_dir = folder filename = file_chooser.get_filename() filename = urls.get_local_url(filename) sel_text = self.main_window.day_text_field.get_selected_text() _, tail = os.path.split(filename) # It is always safer to add the "file://" protocol and the ""s return '[{} ""{}""]'.format(sel_text or tail, filename)
def on_insert_pic(self, sel_text): dirs = self.main_window.journal.dirs picture_chooser = self.main_window.builder.get_object( "picture_chooser") picture_chooser.set_current_folder(dirs.last_pic_dir) # if no text is selected, we can support inserting multiple images picture_chooser.set_select_multiple(not sel_text) file_filter = Gtk.FileFilter() file_filter.set_name("Images") file_filter.add_mime_type("image/bmp") file_filter.add_mime_type("image/gif") file_filter.add_mime_type("image/jpeg") file_filter.add_mime_type("image/png") # SVG images aren't found by MIME type on Windows. file_filter.add_pattern("*.svg") # File filter hides all files on MacOS. if not filesystem.IS_MAC: picture_chooser.add_filter(file_filter) # Add box for inserting image width. box = Gtk.HBox() box.set_spacing(2) label = Gtk.Label(label=_("Width (optional):")) width_entry = Gtk.Entry() width_entry.set_max_length(6) width_entry.set_width_chars(6) box.pack_start(label, False, False, 0) box.pack_start(width_entry, False, False, 0) box.pack_start(Gtk.Label(_("pixels")), True, True, 0) box.show_all() picture_chooser.set_extra_widget(box) response = picture_chooser.run() picture_chooser.hide() if response == Gtk.ResponseType.OK: folder = picture_chooser.get_current_folder() # Folder is None if the file was chosen from the "recently used" section. if folder: dirs.last_pic_dir = folder # get requested width of image width_text = "" width = width_entry.get_text() if width: try: width = int(width) except ValueError: self.main_window.journal.show_message( _("Width must be an integer."), error=True) return width_text = "?%d" % width if sel_text: sel_text += " " # iterate through all selected images lines = [] for filename in picture_chooser.get_filenames(): base, ext = os.path.splitext(filename) # On windows firefox accepts absolute filenames only # with the file:// prefix base = urls.get_local_url(base) lines.append('[{}""{}""{}{}]'.format(sel_text, base, ext, width_text)) return "\n".join(lines)