diff Generic/openwith.py @ 23:65e7ae0d0e63

GTK2 to GTK3
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Thu, 02 May 2019 16:31:17 +0200
parents a7b9f7e7dfa4
children
line wrap: on
line diff
--- a/Generic/openwith.py	Tue Sep 30 17:16:50 2014 +0200
+++ b/Generic/openwith.py	Thu May 02 16:31:17 2019 +0200
@@ -3,10 +3,10 @@
 ## File openwith.py
 ## This file is part of pyArq-Presupuestos.
 ##
-## Copyright (C) 2010-2013 Miguel Ángel Bárcena Rodríguez
+## Copyright (C) 2010-2019 Miguel Ángel Bárcena Rodríguez
 ##                    <miguelangel@obraencurso.es>
 ##
-## This file is based in gtkgui_helpers.py and common/helpers.py from gajim
+## This file is based in common/helpers.py from gajim
 ##
 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org>
 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
@@ -33,72 +33,39 @@
 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
 ##
 # Modules
+
+# python 2/3 compatibility
+from __future__ import absolute_import, division, print_function, unicode_literals
+
 import subprocess
 import os
+import platform
+import sys
 
 # pyArq-Presupuestos modules
-import globalVars
-
-# from gtkgui_helpers.py 
-def autodetect_desktop():
-    # recognize the environment and sets it in globalVars
-    if os.name == 'nt':
-        globalVars.desktop["desktop"] = "windows"
-    else:
-        _processes = get_running_processes()
-        if 'gnome-session' in _processes:
-            globalVars.desktop["desktop"] = "gnome"
-        elif 'startkde' in _processes:
-            globalVars.desktop["desktop"] = "kde"
-        elif 'startxfce4' in _processes or 'xfce4-session' in _processes:
-            globalVars.desktop["desktop"] = "xfce"
-        elif 'startlxde' in _processes or 'lxsession' in _processes:
-            globalVars.desktop["desktop"] = "lxde"
-        elif 'awesome' in _processes:
-            globalVars.desktop["desktop"] = "awesome"
-        elif 'dwm' in _processes:
-            globalVars.desktop["desktop"] = "dwm"
-        elif 'startfluxbox' in _processes:
-            globalVars.desktop["desktop"] = "fluxbox"
-        elif 'fvwm2' in _processes:
-            globalVars.desktop["desktop"] = "fvwm"
-        else:
-            globalVars.desktop["desktop"] = ""
+from Generic import globalVars
 
-def get_running_processes():
-    '''returns running processes or None (if not /proc exists)'''
-    if os.path.isdir('/proc'):
-        # under Linux: checking if 'gnome-session' or
-        # 'startkde' programs were run before gajim, by
-        # checking /proc (if it exists)
-        #
-        # if something is unclear, read `man proc`;
-        # if /proc exists, directories that have only numbers
-        # in their names contain data about processes.
-        # /proc/[xxx]/exe is a symlink to executable started
-        # as process number [xxx].
-        # filter out everything that we are not interested in:
-        files = os.listdir('/proc')
-
-        # files that doesn't have only digits in names...
-        files = filter(str.isdigit, files)
-
-        # files that aren't directories...
-        files = [f for f in files if os.path.isdir('/proc/' + f)]
-
-        # processes owned by somebody not running gajim...
-        # (we check if we have access to that file)
-        files = [f for f in files if os.access('/proc/' + f +'/exe', os.F_OK)]
-
-        # be sure that /proc/[number]/exe is really a symlink
-        # to avoid TBs in incorrectly configured systems
-        files = [f for f in files if os.path.islink('/proc/' + f + '/exe')]
-
-        # list of processes
-        processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f in files]
-
-        return processes
-    return []
+def autodetect_desktop():
+    """
+    recognize the environment and sets it in globalVars
+    os.name:
+             python2  'posix', 'nt', 'os2', 'ce', 'java', 'riscos'.
+             python3  'posix', 'nt', 'java'.
+    sys.platform
+              unix: return by uname 
+              Linux          'linux', 'linux2'
+              Windows        'win32'
+              Windows/Cygwin 'cygwin'
+              Mac OS X       'darwin'
+    """
+    if os.name == "nt":
+        globalVars.desktop["desktop"] = "windows"
+    elif sys.platform == "darwin":
+        globalVars.desktop["desktop"] = "macosx"
+    elif sys.platform.startswith('linux'):
+        globalVars.desktop["desktop"] = "linux"
+    else:
+        globalVars.desktop["desktop"] = sys.platform
 
 # from common/helpers.py
 
@@ -113,33 +80,69 @@
     return command
 
 def launch_file(kind, uri):
-    # kind = "url" ,"mail", "image", "dxf"
+    """
+    kind = "url" ,"mail", "image", "dxf", "wmf", "pdf",  video", 
+           "office-document", "office-presentation", "office-spreadsheet",
+           "html", "rtf", "txt", ""
+    Linux: xdg-open supports:
+        Xfce (exo-open "$1"),
+        Gnome (gvfs-open "$1", gnome-open "$1" with fallback to generic open),
+        KDE (kde-open "$1" with fallback to kfmclient exec "$1"),
+        Mate (gvfs-open "$1", mate-open $1),
+        LXDE (pcmanfm $1 with fallback to generic open),
+        Enlightenment (enlightenment_open $1),
+        Cinnamon (open_gnome3 $1, which is gvfs-open "$1" with fallback to generic open),
+        Cygwin (cygstart "$1"),
+        Darwin (open "$1").
+        
+
+    """
+    # TODO: test in Mac os X
+    # TODO: test in Windows
+    # TODO: configured apps in windows?
+    
+    if kind == "mail" and not uri.startswith("mailto:"):
+        uri = "mailto:" + uri
+    
     _desktop = globalVars.desktop["desktop"]
+    
     if _desktop == "windows":
         try:
             os.startfile(uri) # if pywin32 is installed we open
         except Exception:
             pass
     else:
-        if kind == 'mail' and not uri.startswith('mailto:'):
-            uri = 'mailto:' + uri
-        if _desktop == "gnome":
-            command = 'gnome-open'
-        elif _desktop == "kde":
-            command = 'kfmclient exec'
-        elif _desktop == "xfce":
-            command = 'exo-open'
+        # if app is configured in globarVars use it
+        if kind in ("url", "html") and globalVars.desktop["browser"] != "":
+            command = globalVars.desktop["browser"]
+        elif kind == "mail" and globalVars.desktop["mailapp"] != "":
+            command = globalVars.desktop["mailapp"]
+        elif kind == "image" and globalVars.desktop["imageapp"] != "":
+            command = globalVars.desktop["imageapp"]
+        elif kind == "wmf" and globalVars.desktop["wmfapp"] != "":
+            command = globalVars.desktop["wmfapp"]
+        elif kind == "dxf" and globalVars.desktop["cadapp"]  != "":
+            command = globalVars.desktop["cadapp"]
+        elif kind == "video" and globalVars.desktop["videoapp"]  != "":
+            command = globalVars.desktop["videoapp"]
+        elif kind in ("office-document", "office-presentation",
+                      "office-spreadsheet") and \
+             globalVars.desktop["officeapp"]  != "":
+            command = globalVars.desktop["officeapp"]
+        elif kind == "txt" and globalVars.desktop["txtapp"] != "":
+            command = globalVars.desktop["txtapp"]
+        elif kind == "rtf" and globalVars.desktop["rtfapp"] != "":
+            command = globalVars.desktop["rtfapp"]
+        # if no app is configured
+        elif _desktop == "macosx":
+            command = "open"
+        elif _desktop == "linux":
+            command = "xdg-open"
         else:
-            if kind == 'url':
-                command = globalVars.desktop["browser"]
-            elif kind == 'mail':
-                command = globalVars.desktop["mailapp"]
-            elif kind == 'image':
-                command = globalVars.desktop["imageapp"]
-            elif kind == 'dxf':
-                command = globalVars.desktop["cadapp"]
-            else:  # if no app is configured
-                return
+            # if no desktop is detected
+            # try xdg-open and cross your fingers 
+            command = "xdg-open"
+
         command = build_command(command, uri)
         try:
             exec_command(command)