comparison src/plugins/plugin_misc_file.py @ 1574:babd97d80049

plugins XEP-0234, file: moved file request dialog to file plugin
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 18:19:47 +0100
parents 1f7a34d499e0
children 833bdb227b16
comparison
equal deleted inserted replaced
1573:6338677f3a89 1574:babd97d80049
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _, D_
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 from sat.tools import xml_tools
25 from twisted.internet import defer
24 import os 26 import os
25 from twisted.internet import defer
26 import uuid 27 import uuid
27 28
28 29
29 PLUGIN_INFO = { 30 PLUGIN_INFO = {
30 "name": "File Tansfer", 31 "name": "File Tansfer",
33 "main": "FilePlugin", 34 "main": "FilePlugin",
34 "handler": "no", 35 "handler": "no",
35 "description": _("""File Tansfer Management: 36 "description": _("""File Tansfer Management:
36 This plugin manage the various ways of sending a file, and choose the best one.""") 37 This plugin manage the various ways of sending a file, and choose the best one.""")
37 } 38 }
39
40
41 CONFIRM = D_(u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?')
42 CONFIRM_TITLE = D_(u'Confirm file transfer')
43 CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?')
44 CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
38 45
39 46
40 class SatFile(object): 47 class SatFile(object):
41 """A file-like object to have high level files manipulation""" 48 """A file-like object to have high level files manipulation"""
42 # TODO: manage "with" statement 49 # TODO: manage "with" statement
92 99
93 def __init__(self, host): 100 def __init__(self, host):
94 log.info(_("plugin File initialization")) 101 log.info(_("plugin File initialization"))
95 self.host = host 102 self.host = host
96 103
104 # Dialogs with user
105 # the overwrite check is done here
97 106
107 def _openFileWrite(self, file_path, transfer_data, file_data, profile):
108 assert 'file_obj' not in transfer_data
109 transfer_data['file_obj'] = SatFile(
110 self.host,
111 file_path,
112 'w',
113 size=file_data['size'],
114 profile=profile,
115 )
116
117 def _gotConfirmation(self, data, peer_jid, transfer_data, file_data, profile):
118 """Called when the permission and dest path have been received
119
120 @param peer_jid(jid.JID): jid of the file sender
121 @param transfer_data(dict): same as for [self.getDestDir]
122 @param file_data(dict): same as for [self.getDestDir]
123 @param profile: %(doc_profile)s
124 return (bool): True if copy is wanted and OK
125 False if user wants to cancel
126 if file exists ask confirmation and call again self._getDestDir if needed
127 """
128 if data.get('cancelled', False):
129 return False
130 path = data['path']
131 file_data['file_path'] = file_path = os.path.join(path, file_data['name'])
132 log.debug(u'destination file path set to {}'.format(file_path))
133
134 # we manage case where file already exists
135 if os.path.exists(file_path):
136 def check_overwrite(overwrite):
137 if overwrite:
138 self._openFileWrite(file_path, transfer_data, file_data, profile)
139 return True
140 else:
141 return self.getDestDir(peer_jid, transfer_data, file_data, profile)
142
143 exists_d = xml_tools.deferConfirm(
144 self.host,
145 _(CONFIRM_OVERWRITE).format(file_path),
146 _(CONFIRM_OVERWRITE_TITLE),
147 profile=profile)
148 exists_d.addCallback(check_overwrite)
149 return exists_d
150
151 self._openFileWrite(file_path, transfer_data, file_data, profile)
152 return True
153
154 def getDestDir(self, peer_jid, transfer_data, file_data, profile):
155 """Request confirmation and destination dir to user
156
157 Overwrite confirmation is managed.
158 if transfer is confirmed, 'file_obj' is added to transfer_data
159 @param peer_jid(jid.JID): jid of the file sender
160 @param filename(unicode): name of the file
161 @param transfer_data(dict): data of the transfer session,
162 it will be only used to store the file_obj.
163 "file_obj" key *MUST NOT* exist before using getDestDir
164 @param file_data(dict): information about the file to be transfered
165 It MUST contain the following keys:
166 - peer_jid (jid.JID): other peer jid
167 - name (unicode): name of the file to trasnsfer
168 the name must not be empty or contain a "/" character
169 - size (int): size of the file
170 It may content the key used in CONFIRM constant
171 It *MUST NOT* contain the "peer" key
172 "file_path" will be added to this dict once destination selected
173 @param profile: %(doc_profile)s
174 return (defer.Deferred): True if transfer is accepted
175 """
176 filename = file_data['name']
177 assert filename and not '/' in filename
178 d = xml_tools.deferDialog(self.host,
179 _(CONFIRM).format(peer=peer_jid.full(), **file_data),
180 _(CONFIRM_TITLE),
181 type_=C.XMLUI_DIALOG_FILE,
182 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
183 profile=profile)
184 d.addCallback(self._gotConfirmation, peer_jid, transfer_data, file_data, profile)
185 return d