Mercurial > pyarq-presupuestos
comparison 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 |
comparison
equal
deleted
inserted
replaced
22:7bd4ca56607d | 23:65e7ae0d0e63 |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 ## File openwith.py | 3 ## File openwith.py |
4 ## This file is part of pyArq-Presupuestos. | 4 ## This file is part of pyArq-Presupuestos. |
5 ## | 5 ## |
6 ## Copyright (C) 2010-2013 Miguel Ángel Bárcena Rodríguez | 6 ## Copyright (C) 2010-2019 Miguel Ángel Bárcena Rodríguez |
7 ## <miguelangel@obraencurso.es> | 7 ## <miguelangel@obraencurso.es> |
8 ## | 8 ## |
9 ## This file is based in gtkgui_helpers.py and common/helpers.py from gajim | 9 ## This file is based in common/helpers.py from gajim |
10 ## | 10 ## |
11 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org> | 11 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org> |
12 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com> | 12 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com> |
13 ## Nikos Kouremenos <kourem AT gmail.com> | 13 ## Nikos Kouremenos <kourem AT gmail.com> |
14 ## Copyright (C) 2006 Alex Mauer <hawke AT hawkesnest.net> | 14 ## Copyright (C) 2006 Alex Mauer <hawke AT hawkesnest.net> |
31 ## | 31 ## |
32 ## You should have received a copy of the GNU General Public License | 32 ## You should have received a copy of the GNU General Public License |
33 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. | 33 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. |
34 ## | 34 ## |
35 # Modules | 35 # Modules |
36 | |
37 # python 2/3 compatibility | |
38 from __future__ import absolute_import, division, print_function, unicode_literals | |
39 | |
36 import subprocess | 40 import subprocess |
37 import os | 41 import os |
42 import platform | |
43 import sys | |
38 | 44 |
39 # pyArq-Presupuestos modules | 45 # pyArq-Presupuestos modules |
40 import globalVars | 46 from Generic import globalVars |
41 | 47 |
42 # from gtkgui_helpers.py | |
43 def autodetect_desktop(): | 48 def autodetect_desktop(): |
44 # recognize the environment and sets it in globalVars | 49 """ |
45 if os.name == 'nt': | 50 recognize the environment and sets it in globalVars |
51 os.name: | |
52 python2 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'. | |
53 python3 'posix', 'nt', 'java'. | |
54 sys.platform | |
55 unix: return by uname | |
56 Linux 'linux', 'linux2' | |
57 Windows 'win32' | |
58 Windows/Cygwin 'cygwin' | |
59 Mac OS X 'darwin' | |
60 """ | |
61 if os.name == "nt": | |
46 globalVars.desktop["desktop"] = "windows" | 62 globalVars.desktop["desktop"] = "windows" |
63 elif sys.platform == "darwin": | |
64 globalVars.desktop["desktop"] = "macosx" | |
65 elif sys.platform.startswith('linux'): | |
66 globalVars.desktop["desktop"] = "linux" | |
47 else: | 67 else: |
48 _processes = get_running_processes() | 68 globalVars.desktop["desktop"] = sys.platform |
49 if 'gnome-session' in _processes: | |
50 globalVars.desktop["desktop"] = "gnome" | |
51 elif 'startkde' in _processes: | |
52 globalVars.desktop["desktop"] = "kde" | |
53 elif 'startxfce4' in _processes or 'xfce4-session' in _processes: | |
54 globalVars.desktop["desktop"] = "xfce" | |
55 elif 'startlxde' in _processes or 'lxsession' in _processes: | |
56 globalVars.desktop["desktop"] = "lxde" | |
57 elif 'awesome' in _processes: | |
58 globalVars.desktop["desktop"] = "awesome" | |
59 elif 'dwm' in _processes: | |
60 globalVars.desktop["desktop"] = "dwm" | |
61 elif 'startfluxbox' in _processes: | |
62 globalVars.desktop["desktop"] = "fluxbox" | |
63 elif 'fvwm2' in _processes: | |
64 globalVars.desktop["desktop"] = "fvwm" | |
65 else: | |
66 globalVars.desktop["desktop"] = "" | |
67 | |
68 def get_running_processes(): | |
69 '''returns running processes or None (if not /proc exists)''' | |
70 if os.path.isdir('/proc'): | |
71 # under Linux: checking if 'gnome-session' or | |
72 # 'startkde' programs were run before gajim, by | |
73 # checking /proc (if it exists) | |
74 # | |
75 # if something is unclear, read `man proc`; | |
76 # if /proc exists, directories that have only numbers | |
77 # in their names contain data about processes. | |
78 # /proc/[xxx]/exe is a symlink to executable started | |
79 # as process number [xxx]. | |
80 # filter out everything that we are not interested in: | |
81 files = os.listdir('/proc') | |
82 | |
83 # files that doesn't have only digits in names... | |
84 files = filter(str.isdigit, files) | |
85 | |
86 # files that aren't directories... | |
87 files = [f for f in files if os.path.isdir('/proc/' + f)] | |
88 | |
89 # processes owned by somebody not running gajim... | |
90 # (we check if we have access to that file) | |
91 files = [f for f in files if os.access('/proc/' + f +'/exe', os.F_OK)] | |
92 | |
93 # be sure that /proc/[number]/exe is really a symlink | |
94 # to avoid TBs in incorrectly configured systems | |
95 files = [f for f in files if os.path.islink('/proc/' + f + '/exe')] | |
96 | |
97 # list of processes | |
98 processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f in files] | |
99 | |
100 return processes | |
101 return [] | |
102 | 69 |
103 # from common/helpers.py | 70 # from common/helpers.py |
104 | 71 |
105 def exec_command(command): | 72 def exec_command(command): |
106 subprocess.Popen('%s &' % command, shell=True).wait() | 73 subprocess.Popen('%s &' % command, shell=True).wait() |
111 parameter = parameter.replace('"', '\\"') # but first escape " | 78 parameter = parameter.replace('"', '\\"') # but first escape " |
112 command = '%s "%s"' % (executable, parameter) | 79 command = '%s "%s"' % (executable, parameter) |
113 return command | 80 return command |
114 | 81 |
115 def launch_file(kind, uri): | 82 def launch_file(kind, uri): |
116 # kind = "url" ,"mail", "image", "dxf" | 83 """ |
84 kind = "url" ,"mail", "image", "dxf", "wmf", "pdf", video", | |
85 "office-document", "office-presentation", "office-spreadsheet", | |
86 "html", "rtf", "txt", "" | |
87 Linux: xdg-open supports: | |
88 Xfce (exo-open "$1"), | |
89 Gnome (gvfs-open "$1", gnome-open "$1" with fallback to generic open), | |
90 KDE (kde-open "$1" with fallback to kfmclient exec "$1"), | |
91 Mate (gvfs-open "$1", mate-open $1), | |
92 LXDE (pcmanfm $1 with fallback to generic open), | |
93 Enlightenment (enlightenment_open $1), | |
94 Cinnamon (open_gnome3 $1, which is gvfs-open "$1" with fallback to generic open), | |
95 Cygwin (cygstart "$1"), | |
96 Darwin (open "$1"). | |
97 | |
98 | |
99 """ | |
100 # TODO: test in Mac os X | |
101 # TODO: test in Windows | |
102 # TODO: configured apps in windows? | |
103 | |
104 if kind == "mail" and not uri.startswith("mailto:"): | |
105 uri = "mailto:" + uri | |
106 | |
117 _desktop = globalVars.desktop["desktop"] | 107 _desktop = globalVars.desktop["desktop"] |
108 | |
118 if _desktop == "windows": | 109 if _desktop == "windows": |
119 try: | 110 try: |
120 os.startfile(uri) # if pywin32 is installed we open | 111 os.startfile(uri) # if pywin32 is installed we open |
121 except Exception: | 112 except Exception: |
122 pass | 113 pass |
123 else: | 114 else: |
124 if kind == 'mail' and not uri.startswith('mailto:'): | 115 # if app is configured in globarVars use it |
125 uri = 'mailto:' + uri | 116 if kind in ("url", "html") and globalVars.desktop["browser"] != "": |
126 if _desktop == "gnome": | 117 command = globalVars.desktop["browser"] |
127 command = 'gnome-open' | 118 elif kind == "mail" and globalVars.desktop["mailapp"] != "": |
128 elif _desktop == "kde": | 119 command = globalVars.desktop["mailapp"] |
129 command = 'kfmclient exec' | 120 elif kind == "image" and globalVars.desktop["imageapp"] != "": |
130 elif _desktop == "xfce": | 121 command = globalVars.desktop["imageapp"] |
131 command = 'exo-open' | 122 elif kind == "wmf" and globalVars.desktop["wmfapp"] != "": |
123 command = globalVars.desktop["wmfapp"] | |
124 elif kind == "dxf" and globalVars.desktop["cadapp"] != "": | |
125 command = globalVars.desktop["cadapp"] | |
126 elif kind == "video" and globalVars.desktop["videoapp"] != "": | |
127 command = globalVars.desktop["videoapp"] | |
128 elif kind in ("office-document", "office-presentation", | |
129 "office-spreadsheet") and \ | |
130 globalVars.desktop["officeapp"] != "": | |
131 command = globalVars.desktop["officeapp"] | |
132 elif kind == "txt" and globalVars.desktop["txtapp"] != "": | |
133 command = globalVars.desktop["txtapp"] | |
134 elif kind == "rtf" and globalVars.desktop["rtfapp"] != "": | |
135 command = globalVars.desktop["rtfapp"] | |
136 # if no app is configured | |
137 elif _desktop == "macosx": | |
138 command = "open" | |
139 elif _desktop == "linux": | |
140 command = "xdg-open" | |
132 else: | 141 else: |
133 if kind == 'url': | 142 # if no desktop is detected |
134 command = globalVars.desktop["browser"] | 143 # try xdg-open and cross your fingers |
135 elif kind == 'mail': | 144 command = "xdg-open" |
136 command = globalVars.desktop["mailapp"] | 145 |
137 elif kind == 'image': | |
138 command = globalVars.desktop["imageapp"] | |
139 elif kind == 'dxf': | |
140 command = globalVars.desktop["cadapp"] | |
141 else: # if no app is configured | |
142 return | |
143 command = build_command(command, uri) | 146 command = build_command(command, uri) |
144 try: | 147 try: |
145 exec_command(command) | 148 exec_command(command) |
146 except Exception: | 149 except Exception: |
147 pass | 150 pass |