annotate src/plugins/plugin_misc_file.py @ 1705:3765e10ec52f

core (sqlite storage): do explicit delete to profile linked tables for deleteProfile
author Goffi <goffi@goffi.org>
date Mon, 30 Nov 2015 20:42:14 +0100
parents 518a42587600
children d17772b0fe22
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
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
44 SENDING = D_(u'Please select a file to send to {peer}')
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
45 SENDING_TITLE = D_(u'File sending')
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
46 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
47 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
48 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
49 CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
1647
31b96ac3eec2 core (param), plugin file: set security_limit for acount changes, backend (dis)connection, and put a higher one for file send
Goffi <goffi@goffi.org>
parents: 1646
diff changeset
50 SECURITY_LIMIT = 30
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
51
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
52 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
53
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
54
1553
ebf97c1ac14a fixed bad inheritance of SatFile
Goffi <goffi@goffi.org>
parents: 1525
diff changeset
55 class SatFile(object):
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 """A file-like object to have high level files manipulation"""
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 # TODO: manage "with" statement
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
59 def __init__(self, host, path, mode='rb', uid=None, size=None, data_cb=None, auto_end_signals=True, profile=C.PROF_KEY_NONE):
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @param host: %(doc_host)s
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 @param path(str): path of the file to get
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 @param mode(str): same as for built-in "open" function
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @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
65 This uid will be used with self.host.progressGet
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 will be automaticaly generated if None
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @param size(None, int): size of the file
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
68 @param data_cb(None, callable): method to call on each data read/write
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
69 mainly useful to do things like calculating hash
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
70 @param auto_end_signals(bool): if True, progressFinished and progressError signals are automatically sent
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
71 if False, you'll have to call self.progressFinished and self.progressError yourself
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
72 progressStarted signal is always sent automatically
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.host = host
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.uid = uid or unicode(uuid.uuid4())
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self._file = open(path, mode)
1598
b144babc2658 core, plugin file: fixed progress id + data is now returned by getProgress, instead of being an argument to fill
Goffi <goffi@goffi.org>
parents: 1585
diff changeset
77 self.size = size
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
78 self.data_cb = data_cb
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.profile = profile
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
80 self.auto_end_signals = auto_end_signals
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
81 metadata = self.getProgressMetadata()
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
82 self.host.registerProgressCb(self.uid, self.getProgress, metadata, profile=profile)
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
83 self.host.bridge.progressStarted(self.uid, metadata, self.profile)
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
85 def checkSize(self):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
86 """Check that current size correspond to given size
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
87
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
88 must be used when the transfer is supposed to be finished
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
89 @return (bool): True if the position is the same as given size
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
90 @raise exceptions.NotFound: size has not be specified
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
91 """
1607
4741e2f5eed2 plugin file: progressFinished and progressError are now sent on SatFile.close. eof Deferred attribute is removed, as it is not used and bring unnecessary complication
Goffi <goffi@goffi.org>
parents: 1602
diff changeset
92 position = self._file.tell()
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
93 if self.size is None:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
94 raise exceptions.NotFound
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
95 return position == self.size
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
96
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
97
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
98 def close(self, progress_metadata=None, error=None):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
99 """Close the current file
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
100
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
101 @param progress_metadata(None, dict): metadata to send with _onProgressFinished message
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
102 @param error(None, unicode): set to an error message if progress was not successful
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
103 mutually exclusive with progress_metadata
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
104 error can happen even if error is None, if current size differ from given size
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
105 """
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
106 if self._file.closed:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
107 return # avoid double close (which is allowed) error
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
108 if error is None:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
109 try:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
110 size_ok = self.checkSize()
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
111 except exceptions.NotFound:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
112 size_ok = True
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
113 if not size_ok:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
114 error = u'declared and actual size mismatch'
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
115 log.warning(error)
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
116 progress_metadata = None
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
117
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self._file.close()
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
119
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
120 if self.auto_end_signals:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
121 if error is None:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
122 self.progressFinished(progress_metadata)
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
123 else:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
124 assert progress_metadata is None
1674
518a42587600 plugin file: fixed bad method call
Goffi <goffi@goffi.org>
parents: 1647
diff changeset
125 self.progressError(error)
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
126
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.host.removeProgressCb(self.uid, self.profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
1639
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
129 def progressFinished(self, metadata=None):
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
130 if metadata is None:
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
131 metadata = {}
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
132 self.host.bridge.progressFinished(self.uid, metadata, self.profile)
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
133
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
134 def progressError(self, error):
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
135 self.host.bridge.progressError(self.uid, error, self.profile)
baac2e120600 plugin file[SatFile]: auto_end_signals flag can be False if the progressFinished and progressError signals are managed by caller.
Goffi <goffi@goffi.org>
parents: 1635
diff changeset
136
1568
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
137 def flush(self):
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
138 self._file.flush()
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
139
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
140 def write(self, buf):
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
141 self._file.write(buf)
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
142 if self.data_cb is not None:
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
143 return self.data_cb(buf)
1568
1f7a34d499e0 plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents: 1553
diff changeset
144
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 def read(self, size=-1):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 read = self._file.read(size)
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
147 if self.data_cb is not None and read:
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
148 self.data_cb(read)
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return read
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def seek(self, offset, whence=os.SEEK_SET):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self._file.seek(offset, whence)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def tell(self):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return self._file.tell()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
1626
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
157 def mode(self):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
158 return self._file.mode()
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
159
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
160 def getProgressMetadata(self):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
161 """Return progression metadata as given to progressStarted
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
162
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
163 @return (dict): metadata (check bridge for documentation)
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
164 """
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
165 metadata = {'type': C.META_TYPE_FILE}
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
166
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
167 mode = self._file.mode
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
168 if '+' in mode:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
169 pass # we have no direction in read/write modes
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
170 elif mode in ('r', 'rb'):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
171 metadata['direction'] = 'out'
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
172 elif mode in ('w', 'wb'):
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
173 metadata['direction'] = 'in'
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
174 elif 'U' in mode:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
175 metadata['direction'] = 'out'
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
176 else:
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
177 raise exceptions.InternalError
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
178
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
179 metadata['name'] = self._file.name
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
180
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
181 return metadata
63cef4dbf2a4 core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents: 1619
diff changeset
182
1598
b144babc2658 core, plugin file: fixed progress id + data is now returned by getProgress, instead of being an argument to fill
Goffi <goffi@goffi.org>
parents: 1585
diff changeset
183 def getProgress(self, progress_id, profile):
1602
33728a2f17bf plugin file: 'size' key is not added anymore in progress'data if size is 0 or not specified in SatFile
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
184 ret = {'position': self._file.tell()}
33728a2f17bf plugin file: 'size' key is not added anymore in progress'data if size is 0 or not specified in SatFile
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
185 if self.size:
33728a2f17bf plugin file: 'size' key is not added anymore in progress'data if size is 0 or not specified in SatFile
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
186 ret['size'] = self.size
33728a2f17bf plugin file: 'size' key is not added anymore in progress'data if size is 0 or not specified in SatFile
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
187 return ret
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 class FilePlugin(object):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 File=SatFile
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 def __init__(self, host):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 log.info(_("plugin File initialization"))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 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
196 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
197 self._file_callbacks = []
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
198 host.importMenu((D_("Action"), D_("send file")), self._fileSendMenu, security_limit=10, help_string=D_("Send a file"), type_=C.MENU_SINGLE)
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
199
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
200 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
201 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
202
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
203 @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
204 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
205 """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
206
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
207 @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
208 @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
209 @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
210 @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
211 @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
212 @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
213 """
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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
228 def _onFileChoosed(self, peer_jid, data, profile):
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
229 cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE))
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
230 if cancelled:
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
231 return
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
232 path=data['path']
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
233 return self.fileSend(peer_jid, path, profile=profile)
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
234
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
235 def _fileSendMenu(self, data, profile):
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
236 """ XMLUI activated by menu: return file sending UI
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
237
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
238 @param profile: %(doc_profile)s
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
239 """
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
240 try:
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
241 jid_ = jid.JID(data['jid'])
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
242 except RuntimeError:
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
243 raise exceptions.DataError(_("Invalid JID"))
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
244
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
245 file_choosed_id = self.host.registerCallback(lambda data, profile: self._onFileChoosed(jid_, data, profile), with_data=True, one_shot=True)
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
246 xml_ui = xml_tools.XMLUI(
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
247 C.XMLUI_DIALOG,
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
248 dialog_opt = {
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
249 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_FILE,
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
250 C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full())},
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
251 title = _(SENDING_TITLE),
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
252 submit_id = file_choosed_id)
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
253
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
254 return {'xmlui': xml_ui.toXml()}
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
255
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
256 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
257 """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
258
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
259 @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
260 @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
261 @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
262 @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
263 """
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
264 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
265 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
266 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
267 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
268 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
269
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
270 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
271 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
272 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
273 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
274 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
275 raise exceptions.NotFound(u"The namespace to unregister doesn't exist")
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
277 # Dialogs with user
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
278 # the overwrite check is done here
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
280 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
281 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
282 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
283 self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
284 file_path,
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
285 'wb',
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
286 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
287 size=file_data['size'],
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
288 data_cb = file_data.get('data_cb'),
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
289 profile=profile,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
290 )
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
291
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
292 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
293 """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
294
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
295 @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
296 @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
297 @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
298 @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
299 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
300 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
301 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
302 """
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
303 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
304 return False
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
305 path = data['path']
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
306 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
307 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
308
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
309 # 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
310 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
311 def check_overwrite(overwrite):
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
312 if overwrite:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
313 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
314 return True
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
315 else:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
316 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
317
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
318 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
319 self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
320 _(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
321 _(CONFIRM_OVERWRITE_TITLE),
1601
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
322 action_extra={'meta_from_jid': peer_jid.full(),
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
323 'meta_type': C.META_TYPE_OVERWRITE,
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
324 'meta_progress_id': file_data[PROGRESS_ID_KEY]
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
325 },
1646
7f0c8856e4e1 plugin file: deferred XMLUI now use security limit
Goffi <goffi@goffi.org>
parents: 1639
diff changeset
326 security_limit=SECURITY_LIMIT,
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
327 profile=profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
328 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
329 return exists_d
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
330
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
331 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
332 return True
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
333
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
334 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
335 """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
336
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
337 Overwrite confirmation is managed.
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
338 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
339 @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
340 @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
341 @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
342 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
343 "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
344 @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
345 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
346 - 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
347 - 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
348 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
349 - size (int): size of the file
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
350 - desc (unicode): description 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
351 - 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
352 It *MUST NOT* contain the "peer" key
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
353 It may contain:
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
354 - data_cb (callable): method called on each data read/write
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
355 "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
356 "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
357 @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
358 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
359 """
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
360 filename = file_data['name']
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
361 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
362 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
363 # human readable size
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
364 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
365 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
366 _(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
367 _(CONFIRM_TITLE),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
368 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
369 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
1601
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
370 action_extra={'meta_from_jid': peer_jid.full(),
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
371 'meta_type': C.META_TYPE_FILE,
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
372 'meta_progress_id': file_data[PROGRESS_ID_KEY]
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
373 },
1646
7f0c8856e4e1 plugin file: deferred XMLUI now use security limit
Goffi <goffi@goffi.org>
parents: 1639
diff changeset
374 security_limit=SECURITY_LIMIT,
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
375 profile=profile)
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
376 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
377 return d