annotate cagou/plugins/plugin_wid_file_sharing.py @ 389:442756495a96

core (common): improved flexibility of jid selector: - JidSelector can now display a custom set of jids, using the new `to_show` property, see comments for details. - a new "implicit_update" property can be set to False (it's True by default) to avoid the automatic "update" on instance initiation. If unset, use must call "update" explicitly.
author Goffi <goffi@goffi.org>
date Thu, 06 Feb 2020 21:16:21 +0100
parents d61bbbac4160
children f7476818f9fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
1 #!/usr/bin/env python3
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 328
diff changeset
4 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
383
a90f26e89a4a share widget: fixed crash when a text content is shared without `text` in data:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
20 from functools import partial
a90f26e89a4a share widget: fixed crash when a text content is shared without `text` in data:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
21 import os.path
a90f26e89a4a share widget: fixed crash when a text content is shared without `text` in data:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
22 import json
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core import log as logging
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.i18n import _
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools.common import files_utils
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat_frontends.quick_frontend import quick_widgets
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat_frontends.tools import jid
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from cagou.core.constants import Const as C
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from cagou.core import cagou_widget
237
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
31 from cagou.core.menu import EntitiesSelectorMenu, TouchMenuBehaviour
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
32 from cagou.core.utils import FilterBehavior
237
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
33 from cagou.core.common_widgets import (Identities, ItemWidget, DeviceWidget,
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
34 CategorySeparator)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from cagou import G
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from kivy import properties
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from kivy.uix.label import Label
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from kivy.uix.button import Button
205
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
39 from kivy import utils as kivy_utils
383
a90f26e89a4a share widget: fixed crash when a text content is shared without `text` in data:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
40
a90f26e89a4a share widget: fixed crash when a text content is shared without `text` in data:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
41 log = logging.getLogger(__name__)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
45 "name": _("file sharing"),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "main": "FileSharing",
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
47 "description": _("share/transfer files between devices"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
48 "icon_symbol": "exchange",
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 }
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
50 MODE_VIEW = "view"
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
51 MODE_LOCAL = "local"
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
52 SELECT_INSTRUCTIONS = _("Please select entities to share with")
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
205
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
54 if kivy_utils.platform == "android":
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
55 from jnius import autoclass
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
56 Environment = autoclass("android.os.Environment")
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
57 base_dir = Environment.getExternalStorageDirectory().getAbsolutePath()
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
58 def expanduser(path):
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
59 if path == '~' or path.startswith('~/'):
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
60 return path.replace('~', base_dir, 1)
205
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
61 return path
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
62 else:
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
63 expanduser = os.path.expanduser
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 class ModeBtn(Button):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def __init__(self, parent, **kwargs):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 super(ModeBtn, self).__init__(**kwargs)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 parent.bind(mode=self.on_mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.on_mode(parent, parent.mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def on_mode(self, parent, new_mode):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 if new_mode == MODE_VIEW:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
75 self.text = _("view shared files")
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 elif new_mode == MODE_LOCAL:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
77 self.text = _("share local files")
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 else:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
79 exceptions.InternalError("Unknown mode: {mode}".format(mode=new_mode))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 class PathWidget(ItemWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
84 def __init__(self, filepath, main_wid, **kw):
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 name = os.path.basename(filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.filepath = os.path.normpath(filepath)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
87 if self.filepath == '.':
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
88 self.filepath = ''
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
89 super(PathWidget, self).__init__(name=name, main_wid=main_wid, **kw)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 raise NotImplementedError
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if self.is_dir:
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
97 self.main_wid.current_dir = self.filepath
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def open_menu(self, touch, dt):
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
100 log.debug(_("opening menu for {path}").format(path=self.filepath))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 super(PathWidget, self).open_menu(touch, dt)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 class LocalPathWidget(PathWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return os.path.isdir(self.filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def getMenuChoices(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 choices = []
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 if self.shared:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
113 choices.append(dict(text=_('unshare'),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 index=len(choices)+1,
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
115 callback=self.main_wid.unshare))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 else:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
117 choices.append(dict(text=_('share'),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 index=len(choices)+1,
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
119 callback=self.main_wid.share))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return choices
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 class RemotePathWidget(PathWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
125 def __init__(self, main_wid, filepath, type_, **kw):
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.type_ = type_
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
127 super(RemotePathWidget, self).__init__(filepath, main_wid=main_wid, **kw)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return self.type_ == C.FILE_TYPE_DIRECTORY
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if self.is_dir:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
135 if self.filepath == '..':
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
136 self.main_wid.remote_entity = ''
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 super(RemotePathWidget, self).do_item_action(touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 else:
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
140 self.main_wid.request_item(self)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 return True
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
237
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
143 class SharingDeviceWidget(DeviceWidget):
206
890b48e41998 plugin file sharing: use appropriate symbol when discovering devices
Goffi <goffi@goffi.org>
parents: 205
diff changeset
144
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 def do_item_action(self, touch):
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
146 self.main_wid.remote_entity = self.entity_jid
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
147 self.main_wid.remote_dir = ''
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
150 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior,
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
151 TouchMenuBehaviour):
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 SINGLE=False
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 layout = properties.ObjectProperty()
254
0700293df0e6 file sharing: start in MODE_VIEW
Goffi <goffi@goffi.org>
parents: 238
diff changeset
154 mode = properties.OptionProperty(MODE_VIEW, options=[MODE_VIEW, MODE_LOCAL])
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
155 local_dir = properties.StringProperty(expanduser('~'))
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
156 remote_dir = properties.StringProperty('')
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
157 remote_entity = properties.StringProperty('')
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 shared_paths = properties.ListProperty()
387
d61bbbac4160 cagou widget: don't add header_input systematically anymore:
Goffi <goffi@goffi.org>
parents: 383
diff changeset
159 use_header_input = True
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 signals_registered = False
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 def __init__(self, host, target, profiles):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 quick_widgets.QuickWidget.__init__(self, host, target, profiles)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 cagou_widget.CagouWidget.__init__(self)
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
165 FilterBehavior.__init__(self)
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
166 TouchMenuBehaviour.__init__(self)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self.mode_btn = ModeBtn(self)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.mode_btn.bind(on_release=self.change_mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self.headerInputAddExtra(self.mode_btn)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 self.bind(local_dir=self.update_view,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 remote_dir=self.update_view,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 remote_entity=self.update_view)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.update_view()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if not FileSharing.signals_registered:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 # FIXME: we use this hack (registering the signal for the whole class) now
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 # as there is currently no unregisterSignal available in bridges
217
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
177 G.host.registerSignal("FISSharedPathNew",
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
178 handler=FileSharing.shared_path_new,
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
179 iface="plugin")
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
180 G.host.registerSignal("FISSharedPathRemoved",
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
181 handler=FileSharing.shared_path_removed,
286f14127f61 plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents: 214
diff changeset
182 iface="plugin")
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 FileSharing.signals_registered = True
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 G.host.bridge.FISLocalSharesGet(self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 callback=self.fill_paths,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 errback=G.host.errback)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def current_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 return self.local_dir if self.mode == MODE_LOCAL else self.remote_dir
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 @current_dir.setter
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 def current_dir(self, new_dir):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 if self.mode == MODE_LOCAL:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 self.local_dir = new_dir
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 self.remote_dir = new_dir
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 def fill_paths(self, shared_paths):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 self.shared_paths.extend(shared_paths)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 def change_mode(self, mode_btn):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 opt = self.__class__.mode.options
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 new_idx = (opt.index(self.mode)+1) % len(opt)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 self.mode = opt[new_idx]
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 def on_mode(self, instance, new_mode):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self.update_view(None, self.local_dir)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
194
a68c9baa6694 plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents: 192
diff changeset
211 def onHeaderInput(self):
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
212 if '/' in self.header_input.text or self.header_input.text == '~':
205
9cefc9f8efc9 plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
Goffi <goffi@goffi.org>
parents: 202
diff changeset
213 self.current_dir = expanduser(self.header_input.text)
194
a68c9baa6694 plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents: 192
diff changeset
214
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
215 def onHeaderInputComplete(self, wid, text, **kwargs):
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 """we filter items when text is entered in input box"""
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
217 if '/' in text:
194
a68c9baa6694 plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents: 192
diff changeset
218 return
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
219 self.do_filter(self.layout.children,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
220 text,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
221 lambda c: c.name,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
222 width_cb=lambda c: c.base_width,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
223 height_cb=lambda c: c.minimum_height,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
224 continue_tests=[lambda c: not isinstance(c, ItemWidget),
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
225 lambda c: c.name == '..'])
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents: 194
diff changeset
226
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 ## remote sharing callback ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 def _discoFindByFeaturesCb(self, data):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 entities_services, entities_own, entities_roster = data
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 for entities_map, title in ((entities_services,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
233 _('services')),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 (entities_own,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
235 _('your devices')),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 (entities_roster,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
237 _('your contacts devices'))):
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 if entities_map:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 self.layout.add_widget(CategorySeparator(text=title))
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
240 for entity_str, entity_ids in entities_map.items():
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 entity_jid = jid.JID(entity_str)
237
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
242 item = SharingDeviceWidget(
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 222
diff changeset
243 self, entity_jid, Identities(entity_ids))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 self.layout.add_widget(item)
214
6a98d70a3a66 file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents: 213
diff changeset
245 if not entities_services and not entities_own and not entities_roster:
6a98d70a3a66 file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents: 213
diff changeset
246 self.layout.add_widget(Label(
6a98d70a3a66 file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents: 213
diff changeset
247 size_hint=(1, 1),
6a98d70a3a66 file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents: 213
diff changeset
248 halign='center',
6a98d70a3a66 file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents: 213
diff changeset
249 text_size=self.size,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
250 text=_("No sharing device found")))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 def discover_devices(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 """Looks for devices handling file "File Information Sharing" and display them"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 try:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 namespace = self.host.ns_map['fis']
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 except KeyError:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
257 msg = _("can't find file information sharing namespace, "
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
258 "is the plugin running?")
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 log.warning(msg)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
260 G.host.addNote(_("missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR)
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 return
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self.host.bridge.discoFindByFeatures(
199
b80d275e437f plugin file sharing: use new local_device argument of discoFindByFeatures
Goffi <goffi@goffi.org>
parents: 198
diff changeset
263 [namespace], [], False, True, True, True, False, self.profile,
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 callback=self._discoFindByFeaturesCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 errback=partial(G.host.errback,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
266 title=_("shared folder error"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
267 message=_("can't check sharing devices: {msg}")))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
268
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 def FISListCb(self, files_data):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 for file_data in files_data:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
271 filepath = os.path.join(self.current_dir, file_data['name'])
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 item = RemotePathWidget(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 filepath=filepath,
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
274 main_wid=self,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
275 type_=file_data['type'])
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 self.layout.add_widget(item)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
277
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 def FISListEb(self, failure_):
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
279 self.remote_dir = ''
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 G.host.addNote(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
281 _("shared folder error"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
282 _("can't list files for {remote_entity}: {msg}").format(
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 remote_entity=self.remote_entity,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 msg=failure_),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 level=C.XMLUI_DATA_LVL_WARNING)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 ## view generation ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 def update_view(self, *args):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 """update items according to current mode, entity and dir"""
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
291 log.debug('updating {}, {}'.format(self.current_dir, args))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.layout.clear_widgets()
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
293 self.header_input.text = ''
194
a68c9baa6694 plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents: 192
diff changeset
294 self.header_input.hint_text = self.current_dir
a68c9baa6694 plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents: 192
diff changeset
295
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 if self.mode == MODE_LOCAL:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
297 filepath = os.path.join(self.local_dir, '..')
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
298 self.layout.add_widget(LocalPathWidget(filepath=filepath, main_wid=self))
213
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
299 try:
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
300 files = sorted(os.listdir(self.local_dir))
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
301 except OSError as e:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
302 msg = _("can't list files in \"{local_dir}\": {msg}").format(
213
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
303 local_dir=self.local_dir,
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
304 msg=e)
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
305 G.host.addNote(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
306 _("shared folder error"),
213
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
307 msg,
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
308 level=C.XMLUI_DATA_LVL_WARNING)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
309 self.local_dir = expanduser('~')
213
423d462ea739 file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents: 212
diff changeset
310 return
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 for f in files:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 filepath = os.path.join(self.local_dir, f)
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
313 self.layout.add_widget(LocalPathWidget(filepath=filepath,
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
314 main_wid=self))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 elif self.mode == MODE_VIEW:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 if not self.remote_entity:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 self.discover_devices()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 # we always a way to go back
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 # so user can return to previous list even in case of error
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
321 parent_path = os.path.join(self.remote_dir, '..')
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 item = RemotePathWidget(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 filepath = parent_path,
222
a676cb07c1cb core (menu): TouchMenuBehaviour:
Goffi <goffi@goffi.org>
parents: 217
diff changeset
324 main_wid=self,
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 type_ = C.FILE_TYPE_DIRECTORY)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 self.layout.add_widget(item)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 self.host.bridge.FISList(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
328 str(self.remote_entity),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 self.remote_dir,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 {},
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 callback=self.FISListCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 errback=self.FISListEb)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
334
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 ## Share methods ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
336
198
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
337 def do_share(self, entities_jids, item):
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
338 if entities_jids:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
339 access = {'read': {'type': 'whitelist',
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
340 'jids': entities_jids}}
198
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
341 else:
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
342 access = {}
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
343
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 G.host.bridge.FISSharePath(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 item.name,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 item.filepath,
198
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
347 json.dumps(access, ensure_ascii=False),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 callback=lambda name: G.host.addNote(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
350 _("sharing folder"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
351 _("{name} is now shared").format(name=name)),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 errback=partial(G.host.errback,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
353 title=_("sharing folder"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
354 message=_("can't share folder: {msg}")))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355
198
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
356 def share(self, menu):
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
357 item = self.menu_item
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
358 self.clear_menu()
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
359 EntitiesSelectorMenu(instructions=SELECT_INSTRUCTIONS,
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
360 callback=partial(self.do_share, item=item)).show()
60b63c3e63a1 plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents: 196
diff changeset
361
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 def unshare(self, menu):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 item = self.menu_item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 G.host.bridge.FISUnsharePath(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 item.filepath,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 callback=lambda: G.host.addNote(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
369 _("sharing folder"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
370 _("{name} is not shared anymore").format(name=item.name)),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 errback=partial(G.host.errback,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
372 title=_("sharing folder"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
373 message=_("can't unshare folder: {msg}")))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
374
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 def fileJingleRequestCb(self, progress_id, item, dest_path):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 G.host.addNote(
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
377 _("file request"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
378 _("{name} download started at {dest_path}").format(
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 name = item.name,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 dest_path = dest_path))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 def request_item(self, item):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 """Retrieve an item from remote entity
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
384
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 @param item(RemotePathWidget): item to retrieve
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 """
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 path, name = os.path.split(item.filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 assert name
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 assert self.remote_entity
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 extra = {'path': path}
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 dest_path = files_utils.get_unique_name(os.path.join(G.host.downloads_dir, name))
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
392 G.host.bridge.fileJingleRequest(str(self.remote_entity),
328
dddea9684a8e plugin file sharing: convert `dest_path` to string when sending to bridge:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
393 str(dest_path),
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 name,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
395 '',
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
396 '',
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 extra,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 callback=partial(self.fileJingleRequestCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 item=item,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 dest_path=dest_path),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 errback=partial(G.host.errback,
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
403 title = _("file request error"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
404 message = _("can't request file: {msg}")))
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 @classmethod
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 def shared_path_new(cls, shared_path, name, profile):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 for wid in G.host.getVisibleList(cls):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 if shared_path not in wid.shared_paths:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 wid.shared_paths.append(shared_path)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 @classmethod
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 def shared_path_removed(cls, shared_path, profile):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 for wid in G.host.getVisibleList(cls):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 if shared_path in wid.shared_paths:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 wid.shared_paths.remove(shared_path)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 else:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
418 log.warning(_("shared path {path} not found in {widget}".format(
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 path = shared_path, widget = wid)))