annotate src/plugins/plugin_misc_file.py @ 1593:791c45ed8659

jp: added param/set command
author Goffi <goffi@goffi.org>
date Sat, 14 Nov 2015 19:18:10 +0100
parents 846a39900fa6
children b144babc2658
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
20 from sat.core.i18n import _, D_
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
24 from sat.core import exceptions
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
25 from sat.tools import xml_tools
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
26 from twisted.internet import defer
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
27 from twisted.words.protocols.jabber import jid
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
29 import os.path
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import uuid
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 "name": "File Tansfer",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "import_name": "FILE",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "type": C.PLUG_TYPE_MISC,
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "main": "FilePlugin",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "handler": "no",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "description": _("""File Tansfer Management:
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 This plugin manage the various ways of sending a file, and choose the best one.""")
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
44 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 ?')
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
45 CONFIRM_TITLE = D_(u'Confirm file transfer')
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
46 CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?')
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
47 CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
48
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
49 PROGRESS_ID_KEY = 'progress_id'
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
50
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
51
1553
ebf97c1ac14a fixed bad inheritance of SatFile
Goffi <goffi@goffi.org>
parents: 1525
diff changeset
52 class SatFile(object):
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """A file-like object to have high level files manipulation"""
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 # TODO: manage "with" statement
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 @param host: %(doc_host)s
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 @param path(str): path of the file to get
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 @param mode(str): same as for built-in "open" function
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @param uid(unicode, None): unique id identifing this progressing element
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
62 This uid will be used with self.host.progressGet
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 will be automaticaly generated if None
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @param size(None, int): size of the file
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host = host
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.uid = uid or unicode(uuid.uuid4())
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self._file = open(path, mode)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.size = None
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.profile = profile
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.eof = defer.Deferred()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host.registerProgressCb(self.uid, self.getProgress, profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host.bridge.progressStarted(self.uid, self.profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def close(self):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self._file.close()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.host.removeProgressCb(self.uid, self.profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
1568
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
81 def flush(self):
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
82 self._file.flush()
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
83
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
84 def write(self, buf):
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
85 self._file.write(buf)
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
86
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def read(self, size=-1):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 read = self._file.read(size)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if not read:
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.eof.callback(None)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return read
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def seek(self, offset, whence=os.SEEK_SET):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 self._file.seek(offset, whence)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def tell(self):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return self._file.tell()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def getProgress(self, progress_id, data, profile):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 return {'position': self._file.tell(), 'size': self.size or 0}
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 class FilePlugin(object):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 File=SatFile
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def __init__(self, host):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 log.info(_("plugin File initialization"))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.host = host
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
109 host.bridge.addMethod("fileSend", ".plugin", in_sign='sssss', out_sign='a{ss}', method=self._fileSend, async=True)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
110 self._file_callbacks = []
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
111
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
112 def _fileSend(self, peer_jid_s, filepath, name="", file_desc="", profile=C.PROF_KEY_NONE):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
113 return self.fileSend(jid.JID(peer_jid_s), filepath, name or None, file_desc or None, profile)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
114
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
115 @defer.inlineCallbacks
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
116 def fileSend(self, peer_jid, filepath, filename=None, file_desc=None, profile=C.PROF_KEY_NONE):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
117 """Send a file using best available method
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
118
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
119 @param peer_jid(jid.JID): jid of the destinee
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
120 @param filepath(str): absolute path to the file
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
121 @param filename(unicode, None): name to use, or None to find it from filepath
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
122 @param file_desc(unicode, None): description of the file
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
123 @param profile: %(doc_profile)s
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
124 @return (dict): action dictionary, with progress id in case of success, else xmlui message
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
125 """
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
126 if not os.path.isfile(filepath):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
127 raise exceptions.DataError(u"The given path doesn't link to a file")
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
128 if not filename:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
129 filename = os.path.basename(filepath) or '_'
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
130 for namespace, callback, priority, method_name in self._file_callbacks:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
131 has_feature = yield self.host.hasFeature(namespace, peer_jid, profile)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
132 if has_feature:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
133 log.info(u"{name} method will be used to send the file".format(name=method_name))
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
134 progress_id = yield defer.maybeDeferred(callback, peer_jid, filepath, filename, file_desc, profile)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
135 defer.returnValue({'progress': progress_id})
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
136 msg = u"Can't find any method to send file to {jid}".format(jid=peer_jid.full())
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
137 log.warning(msg)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
138 defer.returnValue({'xmlui': xml_tools.note(u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING).toXml()})
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
139
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
140 def register(self, namespace, callback, priority=0, method_name=None):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
141 """Register a fileSending method
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
142
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
143 @param namespace(unicode): XEP namespace
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
144 @param callback(callable): method to call (must have the same signature as [fileSend])
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
145 @param priority(int): pririoty of this method, the higher available will be used
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
146 @param method_name(unicode): short name for the method, namespace will be used if None
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
147 """
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
148 for data in self._file_callbacks:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
149 if namespace == data[0]:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
150 raise exceptions.ConflictError(u'A method with this namespace is already registered')
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
151 self._file_callbacks.append((namespace, callback, priority, method_name or namespace))
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
152 self._file_callbacks.sort(key=lambda data: data[2], reverse=True)
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
153
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
154 def unregister(self, namespace):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
155 for idx, data in enumerate(self._file_callbacks):
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
156 if data[0] == namespace:
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
157 del [idx]
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
158 return
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
159 raise exceptions.NotFound(u"The namespace to unregister doesn't exist")
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
161 # Dialogs with user
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
162 # the overwrite check is done here
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
164 def _openFileWrite(self, file_path, transfer_data, file_data, profile):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
165 assert 'file_obj' not in transfer_data
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
166 transfer_data['file_obj'] = SatFile(
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
167 self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
168 file_path,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
169 'w',
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
170 uid=file_data[PROGRESS_ID_KEY],
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
171 size=file_data['size'],
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
172 profile=profile,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
173 )
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
174
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
175 def _gotConfirmation(self, data, peer_jid, transfer_data, file_data, profile):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
176 """Called when the permission and dest path have been received
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
177
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
178 @param peer_jid(jid.JID): jid of the file sender
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
179 @param transfer_data(dict): same as for [self.getDestDir]
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
180 @param file_data(dict): same as for [self.getDestDir]
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
181 @param profile: %(doc_profile)s
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
182 return (bool): True if copy is wanted and OK
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
183 False if user wants to cancel
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
184 if file exists ask confirmation and call again self._getDestDir if needed
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
185 """
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
186 if data.get('cancelled', False):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
187 return False
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
188 path = data['path']
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
189 file_data['file_path'] = file_path = os.path.join(path, file_data['name'])
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
190 log.debug(u'destination file path set to {}'.format(file_path))
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
191
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
192 # we manage case where file already exists
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
193 if os.path.exists(file_path):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
194 def check_overwrite(overwrite):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
195 if overwrite:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
196 self._openFileWrite(file_path, transfer_data, file_data, profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
197 return True
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
198 else:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
199 return self.getDestDir(peer_jid, transfer_data, file_data, profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
200
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
201 exists_d = xml_tools.deferConfirm(
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
202 self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
203 _(CONFIRM_OVERWRITE).format(file_path),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
204 _(CONFIRM_OVERWRITE_TITLE),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
205 profile=profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
206 exists_d.addCallback(check_overwrite)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
207 return exists_d
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
208
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
209 self._openFileWrite(file_path, transfer_data, file_data, profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
210 return True
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
211
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
212 def getDestDir(self, peer_jid, transfer_data, file_data, profile):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
213 """Request confirmation and destination dir to user
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
214
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
215 Overwrite confirmation is managed.
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
216 if transfer is confirmed, 'file_obj' is added to transfer_data
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
217 @param peer_jid(jid.JID): jid of the file sender
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
218 @param filename(unicode): name of the file
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
219 @param transfer_data(dict): data of the transfer session,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
220 it will be only used to store the file_obj.
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
221 "file_obj" key *MUST NOT* exist before using getDestDir
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
222 @param file_data(dict): information about the file to be transfered
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
223 It MUST contain the following keys:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
224 - peer_jid (jid.JID): other peer jid
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
225 - name (unicode): name of the file to trasnsfer
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
226 the name must not be empty or contain a "/" character
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
227 - size (int): size of the file
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
228 - progress_id (unicode): id to use for progression
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
229 It may content the key used in CONFIRM constant
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
230 It *MUST NOT* contain the "peer" key
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
231 "file_path" will be added to this dict once destination selected
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
232 "size_human" will also be added with human readable file size
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
233 @param profile: %(doc_profile)s
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
234 return (defer.Deferred): True if transfer is accepted
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
235 """
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
236 filename = file_data['name']
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
237 assert filename and not '/' in filename
1585
846a39900fa6 plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
238 assert PROGRESS_ID_KEY in file_data
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
239 # human readable size
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
240 file_data['size_human'] = u'{:.6n} Mio'.format(float(file_data['size'])/(1024**2))
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
241 d = xml_tools.deferDialog(self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
242 _(CONFIRM).format(peer=peer_jid.full(), **file_data),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
243 _(CONFIRM_TITLE),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
244 type_=C.XMLUI_DIALOG_FILE,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
245 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
246 profile=profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
247 d.addCallback(self._gotConfirmation, peer_jid, transfer_data, file_data, profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
248 return d