1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """\
22 For MIME box jobs there are currently three handling actions available:
23 L{X2goMIMEboxActionOPEN}, L{X2goMIMEboxActionOPENWITH} and L{X2goMIMEboxActionSAVEAS}.
24
25 """
26 __NAME__ = 'x2gomimeboxactions-pylib'
27
28
29 import os
30 import sys
31 import shutil
32 import copy
33 import types
34 import threading
35 import time
36
37 from defaults import X2GOCLIENT_OS as _X2GOCLIENT_OS
38 if _X2GOCLIENT_OS in ("Windows"):
39 import subprocess
40 import win32api
41 else:
42 import gevent_subprocess as subprocess
43
44
45 import log
46 import defaults
47
48 import utils
49 import x2go_exceptions
50
51 _MIMEBOX_ENV = os.environ.copy()
55
56 __name__ = 'NAME'
57 __description__ = 'DESCRIPTION'
58
60 """\
61 This is a meta class and has no functionality as such. It is used as parent
62 class by »real« X2go MIME box actions.
63
64 @param client_instance: the underlying L{X2goClient} instance
65 @type client_instance: C{instance}
66 @param logger: you can pass an L{X2goLogger} object to the
67 L{X2goMIMEboxAction} constructor
68 @type logger: C{instance}
69 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
70 constructed with the given loglevel
71 @type loglevel: C{int}
72
73 """
74 if logger is None:
75 self.logger = log.X2goLogger(loglevel=loglevel)
76 else:
77 self.logger = copy.deepcopy(logger)
78 self.logger.tag = __NAME__
79
80
81 self.profile_name = 'UNKNOWN'
82 self.session_name = 'UNKNOWN'
83
84 self.client_instance = client_instance
85
86 @property
88 """\
89 Return the X2go MIME box action's name.
90
91 """
92 return self.__name__
93
94 @property
96 """\
97 Return the X2go MIME box action's description text.
98
99 """
100 return self.__description__
101
102 - def do_process(self, mimebox_file, mimebox_dir, ):
103 """\
104 Perform the defined MIME box action (doing nothing in L{X2goMIMEboxAction} parent class).
105
106 @param mimebox_file: file name as placed in to the X2go MIME box directory
107 @type mimebox_file: C{str}
108 @param mimebox_dir: location of the X2go sessions's MIME box directory
109 @type mimebox_dir: C{str}
110
111 """
112 pass
113
116 """\
117 MIME box action that opens incoming files in the system's default application.
118
119 """
120 __name__= 'OPEN'
121 __decription__= 'Open incoming file with local system\'s default application.'
122
124 """\
125 @param client_instance: the underlying L{X2goClient} instance
126 @type client_instance: C{instance}
127 @param logger: you can pass an L{X2goLogger} object to the
128 L{X2goMIMEboxActionOPEN} constructor
129 @type logger: C{instance}
130 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
131 constructed with the given loglevel
132 @type loglevel: C{int}
133
134 """
135 self.client_instance = client_instance
136 X2goMIMEboxAction.__init__(self, logger=logger, loglevel=loglevel)
137
138 - def do_process(self, mimebox_file, mimebox_dir, ):
139 """\
140 Open an incoming MIME box file in the system's default application.
141
142 @param mimebox_file: file name as placed in to the MIME box directory
143 @type mimebox_file: C{str}
144 @param mimebox_dir: location of the X2go session's MIME box directory
145 @type mimebox_dir: C{str}
146
147 """
148 if _X2GOCLIENT_OS == "Windows":
149 self.logger('opening incoming MIME box file with Python\'s os.startfile() command: %s' % mimebox_file, loglevel=log.loglevel_DEBUG)
150 try:
151 os.startfile(os.path.join(mimebox_dir, mimebox_file))
152 except WindowsError, win_err:
153 if self.client_instance:
154 self.client_instance.HOOK_mimeboxaction_error(mimebox_file,
155 profile_name=self.profile_name,
156 session_name=self.session_name,
157 err_msg=str(win_err)
158 )
159 else:
160 self.logger('Encountered WindowsError: %s' % str(win_err), loglevel=log.loglevel_ERROR)
161 time.sleep(20)
162 else:
163 cmd_line = [ 'xdg-open', os.path.join(mimebox_dir, mimebox_file), ]
164 self.logger('opening MIME box file with command: %s' % ' '.join(cmd_line), loglevel=log.loglevel_DEBUG)
165 p = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_MIMEBOX_ENV)
166 time.sleep(20)
167
170 """\
171 MIME box action that calls the system's ,,Open with...'' dialog on incoming files. Currently only
172 properly implementable on Windows platforms.
173
174 """
175 __name__= 'OPENWITH'
176 __decription__= 'Evoke ,,Open with...\'\' dialog on incoming MIME box files.'
177
179 """\
180 @param client_instance: the underlying L{X2goClient} instance
181 @type client_instance: C{instance}
182 @param logger: you can pass an L{X2goLogger} object to the
183 L{X2goMIMEboxActionOPENWITH} constructor
184 @type logger: C{instance}
185 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
186 constructed with the given loglevel
187 @type loglevel: C{int}
188
189 """
190 self.client_instance = client_instance
191 X2goMIMEboxAction.__init__(self, logger=logger, loglevel=loglevel)
192
193 - def do_process(self, mimebox_file, mimebox_dir, ):
194 """\
195 Open an incoming MIME box file in the system's default application.
196
197 @param mimebox_file: file name as placed in to the MIME box directory
198 @type mimebox_file: C{str}
199 @param mimebox_dir: location of the X2go session's MIME box directory
200 @type mimebox_dir: C{str}
201
202 """
203 if _X2GOCLIENT_OS == "Windows":
204 self.logger('evoking Open-with dialog on incoming MIME box file: %s' % mimebox_file, loglevel=log.loglevel_DEBUG)
205 win32api.ShellExecute (
206 0,
207 "open",
208 "rundll32.exe",
209 "shell32.dll,OpenAs_RunDLL %s" % os.path.join(mimebox_dir, mimebox_file),
210 None,
211 0,
212 )
213 time.sleep(20)
214 else:
215 self.logger('the evocation of the Open-with dialog box is currently not available on Linux, falling back to MIME box action OPEN', loglevel=log.loglevel_WARN)
216 cmd_line = [ 'xdg-open', os.path.join(mimebox_dir, mimebox_file), ]
217 self.logger('opening MIME box file with command: %s' % ' '.join(cmd_line), loglevel=log.loglevel_DEBUG)
218 p = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_MIMEBOX_ENV)
219 time.sleep(20)
220
223 """\
224 MIME box action that allows saving incoming MIME box files to a local folder. What this
225 MIME box actually does is calling a hook method in the L{X2goClient} instance that
226 can be hi-jacked by one of your application's methods which then can handle the ,,Save as...''
227 request.
228
229 """
230 __name__ = 'SAVEAS'
231 __decription__= 'Save incoming file as...'
232
234 """\
235 @param client_instance: an L{X2goClient} instance, within your customized L{X2goClient} make sure
236 you have a C{HOOK_open_mimebox_saveas_dialog(filename=<str>)} method defined that will actually
237 handle the incoming mimebox file.
238 @type client_instance: C{instance}
239 @param logger: you can pass an L{X2goLogger} object to the
240 L{X2goMIMEboxActionSAVEAS} constructor
241 @type logger: C{instance}
242 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
243 constructed with the given loglevel
244 @type loglevel: C{int}
245
246 """
247 if client_instance is None:
248 raise x2go_exceptions.X2goMIMEboxActionException('the SAVEAS MIME box action needs to know the X2goClient instance (client=<instance>)')
249 X2goMIMEboxAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
250
252 """\
253 Call an L{X2goClient} hook method (C{HOOK_open_mimebox_saveas_dialog}) that
254 can handle the MIME box's SAVEAS action.
255
256 @param mimebox_file: file name as placed in to the MIME box directory
257 @type mimebox_file: C{str}
258 @param mimebox_dir: location of the X2go session's MIME box directory
259 @type mimebox_dir: C{str}
260 @param mimebox_file: PDF file name as placed in to the X2go spool directory
261
262 """
263 self.logger('Session %s (%s) is calling X2goClient class hook method <client_instance>.HOOK_open_mimebox_saveas_dialog(%s)' % (self.session_name, self.profile_name, mimebox_file), loglevel=log.loglevel_NOTICE)
264 self.client_instance.HOOK_open_mimebox_saveas_dialog(os.path.join(mimebox_dir, mimebox_file), profile_name=self.profile_name, session_name=self.session_name)
265 time.sleep(60)
266