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

Source Code for Module x2go.mimeboxactions

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