From 7af87739bc273ad6b33d0885ce81869248cc9b00 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Tue, 19 Apr 2011 14:36:22 +0200
Subject: [PATCH] use webbrowser module to open uri instead of using popen.
 Fixes #5751

---
 src/common/helpers.py | 59 ++++++++++++++++++-------------------------
 src/config.py         |  5 +---
 src/gajim.py          |  2 +-
 src/gtkgui_helpers.py | 29 ---------------------
 src/gui_interface.py  |  3 ---
 5 files changed, 26 insertions(+), 72 deletions(-)

diff --git a/src/common/helpers.py b/src/common/helpers.py
index 752dd36c13..b95a331cba 100644
--- a/src/common/helpers.py
+++ b/src/common/helpers.py
@@ -35,6 +35,7 @@ import locale
 import os
 import subprocess
 import urllib
+import webbrowser
 import errno
 import select
 import base64
@@ -671,30 +672,20 @@ def get_contact_dict_for_account(account):
     return contacts_dict
 
 def launch_browser_mailer(kind, uri):
-    #kind = 'url' or 'mail'
-    if os.name == 'nt':
-        try:
-            os.startfile(uri) # if pywin32 is installed we open
-        except Exception:
-            pass
-
-    else:
-        if kind in ('mail', 'sth_at_sth') and not uri.startswith('mailto:'):
-            uri = 'mailto:' + uri
-
-        if kind == 'url' and uri.startswith('www.'):
-            uri = 'http://' + uri
-
-        if gajim.config.get('openwith') in ('xdg-open', 'gnome-open',
-        'kfmclient exec', 'exo-open'):
-            command = gajim.config.get('openwith')
-        elif gajim.config.get('openwith') == 'custom':
-            if kind == 'url':
-                command = gajim.config.get('custombrowser')
-            elif kind in ('mail', 'sth_at_sth'):
-                command = gajim.config.get('custommailapp')
-            if command == '': # if no app is configured
-                return
+    # kind = 'url' or 'mail'
+    if kind in ('mail', 'sth_at_sth') and not uri.startswith('mailto:'):
+        uri = 'mailto:' + uri
+
+    if kind == 'url' and uri.startswith('www.'):
+        uri = 'http://' + uri
+
+    if not gajim.config.get('autodetect_browser_mailer'):
+        if kind == 'url':
+            command = gajim.config.get('custombrowser')
+        elif kind in ('mail', 'sth_at_sth'):
+            command = gajim.config.get('custommailapp')
+        if command == '': # if no app is configured
+            return
 
         command = build_command(command, uri)
         try:
@@ -702,18 +693,14 @@ def launch_browser_mailer(kind, uri):
         except Exception:
             pass
 
-def launch_file_manager(path_to_open):
-    if os.name == 'nt':
-        try:
-            os.startfile(path_to_open) # if pywin32 is installed we open
-        except Exception:
-            pass
     else:
-        if gajim.config.get('openwith') in ('xdg-open', 'gnome-open',
-        'kfmclient exec', 'exo-open'):
-            command = gajim.config.get('openwith')
-        elif gajim.config.get('openwith') == 'custom':
-            command = gajim.config.get('custom_file_manager')
+        webbrowser.open(uri)
+
+
+def launch_file_manager(path_to_open):
+    uri = 'file://' + path_to_open
+    if not gajim.config.get('autodetect_browser_mailer'):
+        command = gajim.config.get('custom_file_manager')
         if command == '': # if no app is configured
             return
         command = build_command(command, path_to_open)
@@ -721,6 +708,8 @@ def launch_file_manager(path_to_open):
             exec_command(command)
         except Exception:
             pass
+    else:
+        webbrowser.open(uri)
 
 def play_sound(event):
     if not gajim.config.get('sounds_on'):
diff --git a/src/config.py b/src/config.py
index daef842c32..d2807ae748 100644
--- a/src/config.py
+++ b/src/config.py
@@ -504,9 +504,7 @@ class PreferencesWindow:
 
             if gajim.config.get('autodetect_browser_mailer'):
                 self.applications_combobox.set_active(0)
-            # else autodetect_browser_mailer is False.
-            # so user has 'Always Use GNOME/KDE/Xfce' or Custom
-            elif gajim.config.get('openwith') == 'custom':
+            else:
                 self.applications_combobox.set_active(1)
                 self.xml.get_object('custom_apps_frame').show()
 
@@ -1146,7 +1144,6 @@ class PreferencesWindow:
         elif widget.get_active() == 1:
             gajim.config.set('autodetect_browser_mailer', False)
             self.xml.get_object('custom_apps_frame').show()
-            gajim.config.set('openwith', 'custom')
         gajim.interface.save_config()
 
     def on_custom_browser_entry_changed(self, widget):
diff --git a/src/gajim.py b/src/gajim.py
index 941a37211a..f866e4356b 100644
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -40,7 +40,7 @@ demandimport.enable()
 demandimport.ignore += ['gobject._gobject', 'libasyncns', 'i18n',
     'logging.NullHandler', 'dbus.glib', 'dbus.service',
     'command_system.implementation.standard', 'OpenSSL.SSL', 'OpenSSL.crypto',
-    'common.sleepy', 'DLFCN', 'dl', 'xml.sax', 'xml.sax.handler']
+    'common.sleepy', 'DLFCN', 'dl', 'xml.sax', 'xml.sax.handler', 'ic']
 
 import os
 import sys
diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py
index 49e4a41f7d..5b62aca0dd 100644
--- a/src/gtkgui_helpers.py
+++ b/src/gtkgui_helpers.py
@@ -221,35 +221,6 @@ def get_default_font():
 
     return None
 
-def autodetect_browser_mailer():
-    # recognize the environment and set appropriate browser/mailer
-    if user_supports_xdg_open():
-        gajim.config.set('openwith', 'xdg-open')
-    elif user_runs_gnome():
-        gajim.config.set('openwith', 'gnome-open')
-    elif user_runs_kde():
-        gajim.config.set('openwith', 'kfmclient exec')
-    elif user_runs_xfce():
-        gajim.config.set('openwith', 'exo-open')
-    else:
-        gajim.config.set('openwith', 'custom')
-
-def user_supports_xdg_open():
-    import os.path
-    return os.path.isfile('/usr/bin/xdg-open')
-
-def user_runs_gnome():
-    return 'gnome-session' in get_running_processes()
-
-def user_runs_kde():
-    return 'startkde' in get_running_processes()
-
-def user_runs_xfce():
-    procs = get_running_processes()
-    if 'startxfce4' in procs or 'xfce4-session' in procs:
-        return True
-    return False
-
 def get_running_processes():
     """
     Return running processes or None (if /proc does not exist)
diff --git a/src/gui_interface.py b/src/gui_interface.py
index 09236cbaf0..a39be727ec 100644
--- a/src/gui_interface.py
+++ b/src/gui_interface.py
@@ -2649,9 +2649,6 @@ class Interface:
                     gajim.config.set_per('themes', theme_name, o,
                         theme[d.index(o)])
 
-        if gajim.config.get('autodetect_browser_mailer') or not cfg_was_read:
-            gtkgui_helpers.autodetect_browser_mailer()
-
         gajim.idlequeue = idlequeue.get_idlequeue()
         # resolve and keep current record of resolved hosts
         gajim.resolver = resolver.get_resolver(gajim.idlequeue)
-- 
GitLab