annotate cagou/plugins/plugin_wid_file_sharing.py @ 192:62198e00a2b7

plugin file sharing: first draft: new file sharing plugin, which allow to share local file or retrieve remote ones.
author Goffi <goffi@goffi.org>
date Tue, 22 May 2018 19:25:23 +0200
parents
children a68c9baa6694
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
192
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # 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
8 # 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
9 # the Free Software Foundation, either version 3 of the License, or
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # 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
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # 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
18 # 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
19
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import log as logging
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = logging.getLogger(__name__)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.tools.common import files_utils
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat_frontends.quick_frontend import quick_widgets
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat_frontends.tools import jid
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from cagou.core.constants import Const as C
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from cagou.core import cagou_widget
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from cagou import G
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from kivy import properties
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from kivy.uix.label import Label
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from kivy.uix.button import Button
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from kivy.uix.boxlayout import BoxLayout
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from kivy.garden import modernmenu
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from kivy.clock import Clock
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from kivy.metrics import dp
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from kivy.animation import Animation
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from functools import partial
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 import os.path
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import json
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 = {
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "name": _(u"file sharing"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "main": "FileSharing",
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "description": _(u"share/transfer files between devices"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "icon_symbol": u"exchange",
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 }
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 MODE_VIEW = u"view"
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 MODE_LOCAL = u"local"
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 dist = modernmenu.dist
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class ModeBtn(Button):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, parent, **kwargs):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 super(ModeBtn, self).__init__(**kwargs)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 parent.bind(mode=self.on_mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.on_mode(parent, parent.mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def on_mode(self, parent, new_mode):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if new_mode == MODE_VIEW:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.text = _(u"view shared files")
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 elif new_mode == MODE_LOCAL:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.text = _(u"share local files")
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 exceptions.InternalError(u"Unknown mode: {mode}".format(mode=new_mode))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
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 class Identities(object):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def __init__(self, entity_ids):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 identities = {}
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 for cat, type_, name in entity_ids:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 identities.setdefault(cat, {}).setdefault(type_, []).append(name)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.identities = identities
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 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def name(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return self.identities.values()[0].values()[0][0]
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 class ItemWidget(BoxLayout):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 click_timeout = properties.NumericProperty(0.4)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 base_width = properties.NumericProperty(dp(100))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def __init__(self, sharing_wid, name):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.sharing_wid = sharing_wid
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.name = name
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 super(ItemWidget, self).__init__()
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 on_touch_down(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if not self.collide_point(*touch.pos):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 t = partial(self.open_menu, touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 touch.ud['menu_timeout'] = t
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 Clock.schedule_once(t, self.click_timeout)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return super(ItemWidget, self).on_touch_down(touch)
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 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 pass
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 def on_touch_up(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 if touch.ud.get('menu_timeout'):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 Clock.unschedule(touch.ud['menu_timeout'])
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 if self.collide_point(*touch.pos) and self.sharing_wid.menu is None:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self.do_item_action(touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return super(ItemWidget, self).on_touch_up(touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def open_menu(self, touch, dt):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.sharing_wid.open_menu(self, touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 del touch.ud['menu_timeout']
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def getMenuChoices(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 """return choice adapted to selected item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 @return (list[dict]): choices ad expected by ModernMenu
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 return []
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 class PathWidget(ItemWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 def __init__(self, sharing_wid, filepath):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 name = os.path.basename(filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 self.filepath = os.path.normpath(filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if self.filepath == u'.':
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 self.filepath = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 super(PathWidget, self).__init__(sharing_wid, name)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 raise NotImplementedError
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if self.is_dir:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 self.sharing_wid.current_dir = self.filepath
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 def open_menu(self, touch, dt):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 log.debug(_(u"opening menu for {path}").format(path=self.filepath))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 super(PathWidget, self).open_menu(touch, dt)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 class LocalPathWidget(PathWidget):
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 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 return os.path.isdir(self.filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 def getMenuChoices(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 choices = []
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if self.shared:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 choices.append(dict(text=_(u'unshare'),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 index=len(choices)+1,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 callback=self.sharing_wid.unshare))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 choices.append(dict(text=_(u'share'),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 index=len(choices)+1,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 callback=self.sharing_wid.share))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 return choices
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 class RemotePathWidget(PathWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 def __init__(self, sharing_wid, filepath, type_):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self.type_ = type_
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 super(RemotePathWidget, self).__init__(sharing_wid, filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 def is_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 return self.type_ == C.FILE_TYPE_DIRECTORY
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 if self.is_dir:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 if self.filepath == u'..':
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self.sharing_wid.remote_entity = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 super(RemotePathWidget, self).do_item_action(touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 self.sharing_wid.request_item(self)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 return True
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 class DeviceWidget(ItemWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def __init__(self, sharing_wid, entity_jid, identities):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 self.entity_jid = entity_jid
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 self.identities = identities
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.own_device = entity_jid.bare == next(G.host.profiles.itervalues()).whoami
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 name = self.identities.name if self.own_device else self.entity_jid.node
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 super(DeviceWidget, self).__init__(sharing_wid, name)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 def do_item_action(self, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 self.sharing_wid.remote_entity = self.entity_jid
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self.sharing_wid.remote_dir = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 class CategorySeparator(Label):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 pass
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 class Menu(modernmenu.ModernMenu):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 pass
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
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 SINGLE=False
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 float_layout = properties.ObjectProperty()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 layout = properties.ObjectProperty()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 mode = properties.OptionProperty(MODE_LOCAL, options=[MODE_VIEW, MODE_LOCAL])
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 local_dir = properties.StringProperty(os.path.expanduser(u'~'))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 remote_dir = properties.StringProperty(u'')
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 remote_entity = properties.StringProperty(u'')
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 shared_paths = properties.ListProperty()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 signals_registered = False
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def __init__(self, host, target, profiles):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 self._filter_last = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 self._filter_anim = Animation(width = 0,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 height = 0,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 opacity = 0,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 d = 0.5)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 quick_widgets.QuickWidget.__init__(self, host, target, profiles)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 cagou_widget.CagouWidget.__init__(self)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 self.mode_btn = ModeBtn(self)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 self.mode_btn.bind(on_release=self.change_mode)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 self.headerInputAddExtra(self.mode_btn)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 self.bind(local_dir=self.update_view,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 remote_dir=self.update_view,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 remote_entity=self.update_view)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 self.update_view()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 self.menu = None
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 self.menu_item = None
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 self.float_layout.bind(children=self.clean_fl_children)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 if not FileSharing.signals_registered:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 # 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
240 # as there is currently no unregisterSignal available in bridges
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 G.host.registerSignal("FISSharedPathNew", handler=FileSharing.shared_path_new, iface="plugin")
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 G.host.registerSignal("FISSharedPathRemoved", handler=FileSharing.shared_path_removed, iface="plugin")
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 FileSharing.signals_registered = True
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 G.host.bridge.FISLocalSharesGet(self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 callback=self.fill_paths,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 errback=G.host.errback)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 @property
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 def current_dir(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 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
251
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 @current_dir.setter
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 def current_dir(self, new_dir):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 if self.mode == MODE_LOCAL:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 self.local_dir = new_dir
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 self.remote_dir = new_dir
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
258
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 def fill_paths(self, shared_paths):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 self.shared_paths.extend(shared_paths)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 def change_mode(self, mode_btn):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 opt = self.__class__.mode.options
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 new_idx = (opt.index(self.mode)+1) % len(opt)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 self.mode = opt[new_idx]
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 def on_mode(self, instance, new_mode):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 print(instance)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 self.update_view(None, self.local_dir)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
271
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 def onHeaderInputComplete(self, wid, text):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 """we filter items when text is entered in input box"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 text = text.strip().lower()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 filtering = len(text)>len(self._filter_last)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 self._filter_last = text
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 for child in self.layout.children:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 if not isinstance(child, ItemWidget):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 continue
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 if child.name == u'..':
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 continue
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 if text in child.name.lower():
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 self._filter_anim.cancel(child)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 child.width = child.base_width
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 child.height = child.minimum_height
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 child.opacity = 1
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 elif (filtering
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 and child.opacity > 0
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 and not self._filter_anim.have_properties_to_animate(child)):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 self._filter_anim.start(child)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 ## remote sharing callback ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 def _discoFindByFeaturesCb(self, data):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 entities_services, entities_own, entities_roster = data
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 for entities_map, title in ((entities_services,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 _(u'services')),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 (entities_own,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 _(u'your devices')),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 (entities_roster,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 _(u'your contacts devices'))):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 if entities_map:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 self.layout.add_widget(CategorySeparator(text=title))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 for entity_str, entity_ids in entities_map.iteritems():
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 entity_jid = jid.JID(entity_str)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 item = DeviceWidget(self,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 entity_jid,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 Identities(entity_ids))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 self.layout.add_widget(item)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 def discover_devices(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 """Looks for devices handling file "File Information Sharing" and display them"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 try:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 namespace = self.host.ns_map['fis']
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 except KeyError:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 msg = _(u"can't find file information sharing namespace, is the plugin running?")
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 log.warning(msg)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 G.host.addNote(_(u"missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 return
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 self.host.bridge.discoFindByFeatures(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 [namespace], [], False, True, True, True, self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 callback=self._discoFindByFeaturesCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 errback=partial(G.host.errback,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 title=_(u"shared folder error"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 message=_(u"can't check sharing devices: {msg}")))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
326
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 def FISListCb(self, files_data):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 for file_data in files_data:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 filepath = os.path.join(self.current_dir, file_data[u'name'])
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 item = RemotePathWidget(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 self,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 filepath=filepath,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 type_=file_data[u'type'])
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 self.layout.add_widget(item)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 def FISListEb(self, failure_):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 self.remote_dir = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 G.host.addNote(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 _(u"shared folder error"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 _(u"can't list files for {remote_entity}: {msg}").format(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 remote_entity=self.remote_entity,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 msg=failure_),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 level=C.XMLUI_DATA_LVL_WARNING)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
344
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 ## view generation ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
346
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 def update_view(self, *args):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 """update items according to current mode, entity and dir"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 log.debug(u'updating {}, {}'.format(self.current_dir, args))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 self.layout.clear_widgets()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 self.header_input.text = u''
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 if self.mode == MODE_LOCAL:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 filepath = os.path.join(self.local_dir, u'..')
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 self.layout.add_widget(LocalPathWidget(sharing_wid=self, filepath=filepath))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 files = sorted(os.listdir(self.local_dir))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 for f in files:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 filepath = os.path.join(self.local_dir, f)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 self.layout.add_widget(LocalPathWidget(sharing_wid=self, filepath=filepath))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 elif self.mode == MODE_VIEW:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 if not self.remote_entity:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 self.discover_devices()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 # we always a way to go back
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 # so user can return to previous list even in case of error
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 parent_path = os.path.join(self.remote_dir, u'..')
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 item = RemotePathWidget(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 self,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 filepath = parent_path,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 type_ = C.FILE_TYPE_DIRECTORY)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 self.layout.add_widget(item)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 self.host.bridge.FISList(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 self.remote_entity,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 self.remote_dir,
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 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 callback=self.FISListCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 errback=self.FISListEb)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
378
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 ## menu methods ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
380
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 def clean_fl_children(self, layout, children):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 """insure that self.menu and self.menu_item are None when menu is dimissed"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 if self.menu is not None and self.menu not in children:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 self.menu = self.menu_item = None
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
385
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 def clear_menu(self):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 """remove menu if there is one"""
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 if self.menu is not None:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 self.menu.dismiss()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 self.menu = None
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 self.menu_item = None
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
392
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 def open_menu(self, item, touch):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 """open menu for item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
395
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 @param item(PathWidget): item when the menu has been requested
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 @param touch(kivy.input.MotionEvent): touch data
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 """
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 if self.menu_item == item:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 return
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 pos = self.to_widget(*touch.pos)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 choices = item.getMenuChoices()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 if not choices:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 return
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 self.menu = Menu(choices=choices,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 center=pos,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 size_hint=(None, None))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 self.float_layout.add_widget(self.menu)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 self.menu.start_display(touch)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 self.menu_item = item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 ## Share methods ##
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
414
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 def share(self, menu):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 item = self.menu_item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 G.host.bridge.FISSharePath(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 item.name,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 item.filepath,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 json.dumps({}, ensure_ascii=False),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 callback=lambda name: G.host.addNote(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 _(u"sharing folder"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 _(u"{name} is now shared").format(name=name)),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 errback=partial(G.host.errback,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 title=_(u"sharing folder"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 message=_(u"can't share folder: {msg}")))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
429
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 def unshare(self, menu):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 item = self.menu_item
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 self.clear_menu()
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 G.host.bridge.FISUnsharePath(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 item.filepath,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 callback=lambda: G.host.addNote(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 _(u"sharing folder"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 _(u"{name} is not shared anymore").format(name=item.name)),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 errback=partial(G.host.errback,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 title=_(u"sharing folder"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 message=_(u"can't unshare folder: {msg}")))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
442
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 def fileJingleRequestCb(self, progress_id, item, dest_path):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 G.host.addNote(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 _(u"file request"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 _(u"{name} download started at {dest_path}").format(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 name = item.name,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 dest_path = dest_path))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
449
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 def request_item(self, item):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 """Retrieve an item from remote entity
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 @param item(RemotePathWidget): item to retrieve
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 """
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 path, name = os.path.split(item.filepath)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 assert name
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 assert self.remote_entity
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 extra = {'path': path}
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 dest_path = files_utils.get_unique_name(os.path.join(G.host.downloads_dir, name))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 G.host.bridge.fileJingleRequest(self.remote_entity,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 dest_path,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 name,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 u'',
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 u'',
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 extra,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 self.profile,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 callback=partial(self.fileJingleRequestCb,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 item=item,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 dest_path=dest_path),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 errback=partial(G.host.errback,
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 title = _(u"file request error"),
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 message = _(u"can't request file: {msg}")))
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
473
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 @classmethod
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 def shared_path_new(cls, shared_path, name, profile):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 for wid in G.host.getVisibleList(cls):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 if shared_path not in wid.shared_paths:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 wid.shared_paths.append(shared_path)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
479
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 @classmethod
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 def shared_path_removed(cls, shared_path, profile):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 for wid in G.host.getVisibleList(cls):
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 if shared_path in wid.shared_paths:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 wid.shared_paths.remove(shared_path)
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 else:
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 log.warning(_(u"shared path {path} not found in {widget}".format(
62198e00a2b7 plugin file sharing: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 path = shared_path, widget = wid)))