Package x2go :: Module mimeboxactions
[frames] | no frames]

Source Code for Module x2go.mimeboxactions

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2010-2012 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> 
  5  # 
  6  # Python X2Go is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU Affero General Public License as published by 
  8  # the Free Software Foundation; either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # Python X2Go is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU Affero General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU Affero General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 
 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  # modules 
 29  import os 
 30  import copy 
 31  import time 
 32   
 33  from defaults import X2GOCLIENT_OS as _X2GOCLIENT_OS 
 34  if _X2GOCLIENT_OS in ("Windows"): 
 35      import subprocess 
 36      import win32api 
 37  else: 
 38      import gevent_subprocess as subprocess 
 39      import x2go_exceptions 
 40      WindowsErrror = x2go_exceptions.WindowsError 
 41   
 42  # Python X2Go modules 
 43  import log 
 44  import x2go_exceptions 
 45   
 46  _MIMEBOX_ENV = os.environ.copy() 
47 48 49 -class X2goMIMEboxAction(object):
50 51 __name__ = 'NAME' 52 __description__ = 'DESCRIPTION' 53
54 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
55 """\ 56 This is a meta class and has no functionality as such. It is used as parent 57 class by »real« X2Go MIME box actions. 58 59 @param client_instance: the underlying L{X2goClient} instance 60 @type client_instance: C{obj} 61 @param logger: you can pass an L{X2goLogger} object to the 62 L{X2goMIMEboxAction} constructor 63 @type logger: C{obj} 64 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 65 constructed with the given loglevel 66 @type loglevel: C{int} 67 68 """ 69 if logger is None: 70 self.logger = log.X2goLogger(loglevel=loglevel) 71 else: 72 self.logger = copy.deepcopy(logger) 73 self.logger.tag = __NAME__ 74 75 # these get set from within the X2goMIMEboxQueue class 76 self.profile_name = 'UNKNOWN' 77 self.session_name = 'UNKNOWN' 78 79 self.client_instance = client_instance
80 81 @property
82 - def name(self):
83 """\ 84 Return the X2Go MIME box action's name. 85 86 """ 87 return self.__name__
88 89 @property
90 - def description(self):
91 """\ 92 Return the X2Go MIME box action's description text. 93 94 """ 95 return self.__description__
96
97 - def _do_process(self, mimebox_file, mimebox_dir, ):
98 """\ 99 Perform the defined MIME box action (doing nothing in L{X2goMIMEboxAction} parent class). 100 101 @param mimebox_file: file name as placed in to the X2Go MIME box directory 102 @type mimebox_file: C{str} 103 @param mimebox_dir: location of the X2Go session's MIME box directory 104 @type mimebox_dir: C{str} 105 106 """ 107 pass
108
109 - def do_process(self, mimebox_file, mimebox_dir, ):
110 """\ 111 Wrapper method for the actual processing of MIME 112 box actions. 113 114 @param mimebox_file: file name as placed in to the X2Go MIME box directory 115 @type mimebox_file: C{str} 116 @param mimebox_dir: location of the X2Go session's MIME box directory 117 @type mimebox_dir: C{str} 118 119 """ 120 mimebox_file = os.path.normpath(mimebox_file) 121 mimebox_dir = os.path.normpath(mimebox_dir) 122 123 self._do_process(mimebox_file, mimebox_dir)
124
125 126 -class X2goMIMEboxActionOPEN(X2goMIMEboxAction):
127 """\ 128 MIME box action that opens incoming files in the system's default application. 129 130 """ 131 __name__= 'OPEN' 132 __decription__= 'Open incoming file with local system\'s default application.' 133
134 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
135 """\ 136 @param client_instance: the underlying L{X2goClient} instance 137 @type client_instance: C{obj} 138 @param logger: you can pass an L{X2goLogger} object to the 139 L{X2goMIMEboxActionOPEN} constructor 140 @type logger: C{obj} 141 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 142 constructed with the given loglevel 143 @type loglevel: C{int} 144 145 """ 146 self.client_instance = client_instance 147 X2goMIMEboxAction.__init__(self, logger=logger, loglevel=loglevel)
148
149 - def _do_process(self, mimebox_file, mimebox_dir, ):
150 """\ 151 Open an incoming MIME box file in the system's default application. 152 153 @param mimebox_file: file name as placed in to the MIME box directory 154 @type mimebox_file: C{str} 155 @param mimebox_dir: location of the X2Go session's MIME box directory 156 @type mimebox_dir: C{str} 157 158 """ 159 mimebox_file = os.path.normpath(mimebox_file) 160 mimebox_dir = os.path.normpath(mimebox_dir) 161 162 if _X2GOCLIENT_OS == "Windows": 163 self.logger('opening incoming MIME box file with Python\'s os.startfile() command: %s' % mimebox_file, loglevel=log.loglevel_DEBUG) 164 try: 165 os.startfile(os.path.join(mimebox_dir, mimebox_file)) 166 except WindowsError, win_err: 167 if self.client_instance: 168 self.client_instance.HOOK_mimeboxaction_error(mimebox_file, 169 profile_name=self.profile_name, 170 session_name=self.session_name, 171 err_msg=str(win_err) 172 ) 173 else: 174 self.logger('Encountered WindowsError: %s' % str(win_err), loglevel=log.loglevel_ERROR) 175 time.sleep(20) 176 else: 177 cmd_line = [ 'xdg-open', os.path.join(mimebox_dir, mimebox_file), ] 178 self.logger('opening MIME box file with command: %s' % ' '.join(cmd_line), loglevel=log.loglevel_DEBUG) 179 subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_MIMEBOX_ENV) 180 time.sleep(20)
181
182 183 -class X2goMIMEboxActionOPENWITH(X2goMIMEboxAction):
184 """\ 185 MIME box action that calls the system's ,,Open with...'' dialog on incoming files. Currently only 186 properly implementable on Windows platforms. 187 188 """ 189 __name__= 'OPENWITH' 190 __decription__= 'Evoke ,,Open with...\'\' dialog on incoming MIME box files.' 191
192 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
193 """\ 194 @param client_instance: the underlying L{X2goClient} instance 195 @type client_instance: C{obj} 196 @param logger: you can pass an L{X2goLogger} object to the 197 L{X2goMIMEboxActionOPENWITH} constructor 198 @type logger: C{obj} 199 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 200 constructed with the given loglevel 201 @type loglevel: C{int} 202 203 """ 204 self.client_instance = client_instance 205 X2goMIMEboxAction.__init__(self, logger=logger, loglevel=loglevel)
206
207 - def _do_process(self, mimebox_file, mimebox_dir, ):
208 """\ 209 Open an incoming MIME box file in the system's default application. 210 211 @param mimebox_file: file name as placed in to the MIME box directory 212 @type mimebox_file: C{str} 213 @param mimebox_dir: location of the X2Go session's MIME box directory 214 @type mimebox_dir: C{str} 215 216 """ 217 mimebox_file = os.path.normpath(mimebox_file) 218 mimebox_dir = os.path.normpath(mimebox_dir) 219 220 if _X2GOCLIENT_OS == "Windows": 221 self.logger('evoking Open-with dialog on incoming MIME box file: %s' % mimebox_file, loglevel=log.loglevel_DEBUG) 222 win32api.ShellExecute ( 223 0, 224 "open", 225 "rundll32.exe", 226 "shell32.dll,OpenAs_RunDLL %s" % os.path.join(mimebox_dir, mimebox_file), 227 None, 228 0, 229 ) 230 time.sleep(20) 231 else: 232 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) 233 cmd_line = [ 'xdg-open', os.path.join(mimebox_dir, mimebox_file), ] 234 self.logger('opening MIME box file with command: %s' % ' '.join(cmd_line), loglevel=log.loglevel_DEBUG) 235 subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_MIMEBOX_ENV) 236 time.sleep(20)
237
238 239 -class X2goMIMEboxActionSAVEAS(X2goMIMEboxAction):
240 """\ 241 MIME box action that allows saving incoming MIME box files to a local folder. What this 242 MIME box actually does is calling a hook method in the L{X2goClient} instance that 243 can be hi-jacked by one of your application's methods which then can handle the ,,Save as...'' 244 request. 245 246 """ 247 __name__ = 'SAVEAS' 248 __decription__= 'Save incoming file as...' 249
250 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
251 """\ 252 @param client_instance: an L{X2goClient} instance, within your customized L{X2goClient} make sure 253 you have a C{HOOK_open_mimebox_saveas_dialog(filename=<str>)} method defined that will actually 254 handle the incoming mimebox file. 255 @type client_instance: C{obj} 256 @param logger: you can pass an L{X2goLogger} object to the 257 L{X2goMIMEboxActionSAVEAS} constructor 258 @type logger: C{obj} 259 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 260 constructed with the given loglevel 261 @type loglevel: C{int} 262 263 @raise X2goMIMEboxActionException: if the client_instance has not been passed to the SAVEAS MIME box action 264 265 """ 266 if client_instance is None: 267 raise x2go_exceptions.X2goMIMEboxActionException('the SAVEAS MIME box action needs to know the X2goClient instance (client=<instance>)') 268 X2goMIMEboxAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
269
270 - def _do_process(self, mimebox_file, mimebox_dir):
271 """\ 272 Call an L{X2goClient} hook method (C{HOOK_open_mimebox_saveas_dialog}) that 273 can handle the MIME box's SAVEAS action. 274 275 @param mimebox_file: file name as placed in to the MIME box directory 276 @type mimebox_file: C{str} 277 @param mimebox_dir: location of the X2Go session's MIME box directory 278 @type mimebox_dir: C{str} 279 @param mimebox_file: PDF file name as placed in to the X2Go spool directory 280 281 """ 282 mimebox_file = os.path.normpath(mimebox_file) 283 mimebox_dir = os.path.normpath(mimebox_dir) 284 285 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) 286 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) 287 time.sleep(60)
288