def __init__(self, widget, file, mimetype=None): '''Constructor @param widget: parent widget, needed to pop dialogs correctly @param file: a L{File} object or URL @param mimetype: the mime-type of the application, if already known. Providing this arguments prevents redundant lookups of the type (which is slow). ''' GObject.GObject.__init__(self) self._window = widget.get_toplevel() self.file = file if mimetype is None: mimetype = get_mimetype(file) self.mimetype = mimetype manager = ApplicationManager() for entry in manager.list_applications(mimetype): item = DesktopEntryMenuItem(entry) self.append(item) item.connect('activate', self.on_activate) if not self.get_children(): item = Gtk.MenuItem.new_with_mnemonic(_('No Applications Found')) # T: message when no applications in "Open With" menu item.set_sensitive(False) self.append(item) self.append(Gtk.SeparatorMenuItem()) item = Gtk.MenuItem.new_with_mnemonic(self.CUSTOMIZE) item.connect('activate', self.on_activate_customize, mimetype) self.append(item)
def open_url(widget, url): '''Open an URL (or URI) in the web browser or other relevant program. The application is determined based on the URL / URI scheme. Unkown schemes and "file://" URIs are opened with the webbrowser. @param widget: parent for new dialogs, C{Gtk.Widget} or C{None} @param url: an URL or URI as string ''' logger.debug('open_url(%s)', url) assert isinstance(url, str) if is_win32_share_re.match(url): url = normalize_win32_share(url) if os.name == 'nt': return _open_with_filebrowser(widget, url) # else consider as a x-scheme-handler/smb type URI below elif is_www_link_re.match(url): url = 'http://' + url elif not is_uri_re.match(url): raise ValueError('Not an URL: %s' % url) else: pass if url.startswith('file:/'): # Special case, force to browser (and not use open_file()) # even though the result may be the same if the browser is # dispatched through xdg-open, gnome-open, ...) _open_with_webbrowser(widget, url) elif url.startswith('outlook:') and hasattr(os, 'startfile'): # Special case for outlook folder paths on windows os.startfile(url) else: from zim.gui.applications import get_mimetype manager = ApplicationManager() type = get_mimetype( url) # Supports "x-scheme-handler/... for URL schemes" logger.debug('Got type "%s" for "%s"', type, url) entry = manager.get_default_application(type) if entry: _open_with(widget, entry, url) elif url.startswith('mailto:'): _open_with_emailclient(widget, url) else: _open_with_webbrowser(widget, url)