Mercurial > pyarq-presupuestos
comparison Generic/openwith.py @ 1:2ac1551ad2ab version 0.0.0
add code
author | Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es> |
---|---|
date | Sun, 31 Oct 2010 20:07:33 +0100 |
parents | |
children | 2fc6b47dbe70 |
comparison
equal
deleted
inserted
replaced
0:a1703c4f2990 | 1:2ac1551ad2ab |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 ## File openwith.py | |
4 ## This file is part of pyArq-Presupuestos. | |
5 ## | |
6 ## Copyright (C) 2010 Miguel Ángel Bárcena Rodríguez | |
7 ## <miguelangel@obraencurso.es> | |
8 ## | |
9 ## This file is based in gtkgui_helpers.py and common/helpers.py from gajim | |
10 ## | |
11 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org> | |
12 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com> | |
13 ## Nikos Kouremenos <kourem AT gmail.com> | |
14 ## Copyright (C) 2006 Alex Mauer <hawke AT hawkesnest.net> | |
15 ## Copyright (C) 2006-2007 Travis Shirk <travis AT pobox.com> | |
16 ## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org> | |
17 ## Copyright (C) 2007 Lukas Petrovicky <lukas AT petrovicky.net> | |
18 ## James Newton <redshodan AT gmail.com> | |
19 ## Julien Pivotto <roidelapluie AT gmail.com> | |
20 ## Copyright (C) 2007-2008 Stephan Erb <steve-e AT h3c.de> | |
21 ## Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com> | |
22 ## Jonathan Schleifer <js-gajim AT webkeks.org> | |
23 ## This program is free software; you can redistribute it and/or modify | |
24 ## it under the terms of the GNU General Public License as published | |
25 ## by the Free Software Foundation; version 3 only. | |
26 ## | |
27 ## This program is distributed in the hope that it will be useful, | |
28 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 ## GNU General Public License for more details. | |
31 ## | |
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/>. | |
34 ## | |
35 # Modules | |
36 import subprocess | |
37 import os | |
38 | |
39 # pyArq-Presupuestos modules | |
40 import globals | |
41 | |
42 # from gtkgui_helpers.py | |
43 def autodetect_desktop(): | |
44 # recognize the environment and sets it in globals | |
45 if os.name == 'nt': | |
46 globals.desktop["desktop"] = "windows" | |
47 else: | |
48 _processes = get_running_processes() | |
49 if 'gnome-session' in _processes: | |
50 globals.desktop["desktop"] = "gnome" | |
51 elif 'startkde' in _processes: | |
52 globals.desktop["desktop"] = "kde" | |
53 elif 'startxfce4' in _processes or 'xfce4-session' in _processes: | |
54 globals.desktop["desktop"] = "xfce" | |
55 else: | |
56 globals.desktop["desktop"] = "" | |
57 | |
58 def get_running_processes(): | |
59 '''returns running processes or None (if not /proc exists)''' | |
60 if os.path.isdir('/proc'): | |
61 # under Linux: checking if 'gnome-session' or | |
62 # 'startkde' programs were run before gajim, by | |
63 # checking /proc (if it exists) | |
64 # | |
65 # if something is unclear, read `man proc`; | |
66 # if /proc exists, directories that have only numbers | |
67 # in their names contain data about processes. | |
68 # /proc/[xxx]/exe is a symlink to executable started | |
69 # as process number [xxx]. | |
70 # filter out everything that we are not interested in: | |
71 files = os.listdir('/proc') | |
72 | |
73 # files that doesn't have only digits in names... | |
74 files = filter(str.isdigit, files) | |
75 | |
76 # files that aren't directories... | |
77 files = [f for f in files if os.path.isdir('/proc/' + f)] | |
78 | |
79 # processes owned by somebody not running gajim... | |
80 # (we check if we have access to that file) | |
81 files = [f for f in files if os.access('/proc/' + f +'/exe', os.F_OK)] | |
82 | |
83 # be sure that /proc/[number]/exe is really a symlink | |
84 # to avoid TBs in incorrectly configured systems | |
85 files = [f for f in files if os.path.islink('/proc/' + f + '/exe')] | |
86 | |
87 # list of processes | |
88 processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f in files] | |
89 | |
90 return processes | |
91 return [] | |
92 | |
93 # from common/helpers.py | |
94 | |
95 def exec_command(command): | |
96 subprocess.Popen('%s &' % command, shell=True).wait() | |
97 | |
98 def build_command(executable, parameter): | |
99 # we add to the parameter (can hold path with spaces) | |
100 # "" so we have good parsing from shell | |
101 parameter = parameter.replace('"', '\\"') # but first escape " | |
102 command = '%s "%s"' % (executable, parameter) | |
103 return command | |
104 | |
105 def launch_file(kind, uri): | |
106 # kind = "url" ,"mail", "image", "dxf" | |
107 _desktop = globals.desktop["desktop"] | |
108 if _desktop == "windows": | |
109 try: | |
110 os.startfile(uri) # if pywin32 is installed we open | |
111 except Exception: | |
112 pass | |
113 else: | |
114 if kind == 'mail' and not uri.startswith('mailto:'): | |
115 uri = 'mailto:' + uri | |
116 if _desktop == "gnome": | |
117 command = 'gnome-open' | |
118 elif _desktop == "kde": | |
119 command = 'kfmclient exec' | |
120 elif _desktop == "xfce": | |
121 command = 'exo-open' | |
122 else: | |
123 if kind == 'url': | |
124 command = globals.desktop["browser"] | |
125 elif kind == 'mail': | |
126 command = globals.desktop["mailapp"] | |
127 elif kind == 'image': | |
128 command = globals.desktop["imageapp"] | |
129 elif kind == 'dxf': | |
130 command = globals.desktop["cadapp"] | |
131 else: # if no app is configured | |
132 return | |
133 command = build_command(command, uri) | |
134 try: | |
135 exec_command(command) | |
136 except Exception: | |
137 pass |