Cédric Krier pushed to branch branch/default at Tryton / Tryton


Commits:
7af047d8 by Cédric Krier at 2022-12-22T15:12:25+01:00
Use Gio.AppInfo.launch_default_for_uri instead of calling xdg-open

Also we try first this method instead of using the python webbrowser module.

Closes #11942
- - - - -


1 changed file:

- tryton/tryton/common/common.py


Changes:

=====================================
tryton/tryton/common/common.py
=====================================
@@ -45,7 +45,7 @@
 import zipfile
 from threading import Lock
 
-from gi.repository import Gdk, GdkPixbuf, GLib, GObject, Gtk
+from gi.repository import Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk
 
 from tryton import __version__
 from tryton.exceptions import TrytonError, TrytonServerError
@@ -542,10 +542,8 @@
                     file_open(zfilename, type=ztype, print_p=True)
         return
 
-    if os.name == 'nt':
-        operation = 'open'
-        if print_p:
-            operation = 'print'
+    if hasattr(os, 'startfile'):
+        operation = 'print' if print_p else 'open'
         try:
             os.startfile(os.path.normpath(filename), operation)
         except WindowsError:
@@ -570,4 +568,5 @@
         except OSError:
             save()
     else:
+        uri = GLib.filename_to_uri(filename)
         try:
@@ -573,6 +572,6 @@
         try:
-            subprocess.Popen(['xdg-open', filename])
-        except OSError:
+            Gio.AppInfo.launch_default_for_uri(uri)
+        except GLib.Error:
             save()
 
 
@@ -576,6 +575,13 @@
             save()
 
 
+def webbrowser_open(url):
+    try:
+        Gio.AppInfo.launch_default_for_uri(url)
+    except GLib.Error:
+        webbrowser.open(url)
+
+
 def url_open(uri):
     try:
         return urllib.request.urlopen(uri)
@@ -636,7 +642,7 @@
         url += "&body=" + urllib.parse.quote(body, "")
     if attachment:
         url += "&attachment=" + urllib.parse.quote(attachment, "")
-    webbrowser.open(url, new=1)
+    webbrowser_open(url, new=1)
 
 
 class UniqueDialog(object):
@@ -878,7 +884,7 @@
             CONFIG['bug.url'], _("Report Bug"))
         button_roundup.get_child().set_halign(Gtk.Align.START)
         button_roundup.connect('activate-link',
-            lambda widget: webbrowser.open(CONFIG['bug.url'], new=2))
+            lambda widget: webbrowser_open(CONFIG['bug.url'], new=2))
         dialog.vbox.pack_start(
             button_roundup, expand=False, fill=False, padding=0)
 
@@ -899,7 +905,7 @@
 def check_version(box, version=__version__):
     def info_bar_response(info_bar, response, box, url):
         if response == Gtk.ResponseType.ACCEPT:
-            webbrowser.open(url)
+            webbrowser_open(url)
         box.remove(info_bar)
 
     class HeadRequest(urllib.request.Request):
@@ -951,7 +957,7 @@
         version = 'latest'
     else:
         version = '.'.join(version)
-    webbrowser.open(CONFIG['doc.url'] % {
+    webbrowser_open(CONFIG['doc.url'] % {
             'lang': CONFIG['client.lang'],
             'version': version,
             })
@@ -1045,7 +1051,7 @@
             query['renew'] = user_id
         url_parts[4] = urlencode(query)
         url = urlunparse(url_parts)
-        webbrowser.open(url)
+        webbrowser_open(url)
 
         class RequestHandler(BaseHTTPRequestHandler):
             def do_GET(self):



View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/7af047d859c49cfe7854a711996245a428591e41

-- 
View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/7af047d859c49cfe7854a711996245a428591e41
You're receiving this email because of your account on foss.heptapod.net.


Reply via email to