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-2011 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 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 General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU 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 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  # Python X2go modules 
 45  import log 
 46  import defaults 
 47  # we hide the default values from epydoc (that's why we transform them to _UNDERSCORE variables) 
 48  import utils 
 49  import x2go_exceptions 
 50   
 51  _MIMEBOX_ENV = os.environ.copy() 
52 53 54 -class X2goMIMEboxAction(object):
55 56 __name__ = 'NAME' 57 __description__ = 'DESCRIPTION' 58
59 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
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 # these get set from within the X2goMIMEboxQueue class 81 self.profile_name = 'UNKNOWN' 82 self.session_name = 'UNKNOWN' 83 84 self.client_instance = client_instance
85 86 @property
87 - def name():
88 """\ 89 Return the X2go MIME box action's name. 90 91 """ 92 return self.__name__
93 94 @property
95 - def description():
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
114 115 -class X2goMIMEboxActionOPEN(X2goMIMEboxAction):
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
123 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
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
168 169 -class X2goMIMEboxActionOPENWITH(X2goMIMEboxAction):
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
178 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
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
221 222 -class X2goMIMEboxActionSAVEAS(X2goMIMEboxAction):
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
233 - def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
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
251 - def do_process(self, mimebox_file, mimebox_dir):
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