comparison cagou/plugins/plugin_wid_file_sharing.py @ 237:059c5b39032d

plugin file sharing: moved common discovery widgets to new core.common_widgets module
author Goffi <goffi@goffi.org>
date Fri, 31 Aug 2018 16:59:38 +0200
parents a676cb07c1cb
children 7918a5668304
comparison
equal deleted inserted replaced
236:ca86954b3788 237:059c5b39032d
25 from sat.tools.common import files_utils 25 from sat.tools.common import files_utils
26 from sat_frontends.quick_frontend import quick_widgets 26 from sat_frontends.quick_frontend import quick_widgets
27 from sat_frontends.tools import jid 27 from sat_frontends.tools import jid
28 from cagou.core.constants import Const as C 28 from cagou.core.constants import Const as C
29 from cagou.core import cagou_widget 29 from cagou.core import cagou_widget
30 from cagou.core.menu import (EntitiesSelectorMenu, TouchMenuBehaviour, 30 from cagou.core.menu import EntitiesSelectorMenu, TouchMenuBehaviour
31 TouchMenuItemBehaviour)
32 from cagou.core.utils import FilterBehavior 31 from cagou.core.utils import FilterBehavior
32 from cagou.core.common_widgets import (Identities, ItemWidget, DeviceWidget,
33 CategorySeparator)
33 from cagou import G 34 from cagou import G
34 from kivy import properties 35 from kivy import properties
35 from kivy.uix.label import Label 36 from kivy.uix.label import Label
36 from kivy.uix.button import Button 37 from kivy.uix.button import Button
37 from kivy.uix.boxlayout import BoxLayout
38 from kivy.metrics import dp
39 from kivy import utils as kivy_utils 38 from kivy import utils as kivy_utils
40 from functools import partial 39 from functools import partial
41 import os.path 40 import os.path
42 import json 41 import json
43 42
76 self.text = _(u"view shared files") 75 self.text = _(u"view shared files")
77 elif new_mode == MODE_LOCAL: 76 elif new_mode == MODE_LOCAL:
78 self.text = _(u"share local files") 77 self.text = _(u"share local files")
79 else: 78 else:
80 exceptions.InternalError(u"Unknown mode: {mode}".format(mode=new_mode)) 79 exceptions.InternalError(u"Unknown mode: {mode}".format(mode=new_mode))
81
82
83 class Identities(object):
84
85 def __init__(self, entity_ids):
86 identities = {}
87 for cat, type_, name in entity_ids:
88 identities.setdefault(cat, {}).setdefault(type_, []).append(name)
89 client = identities.get('client', {})
90 if 'pc' in client:
91 self.type = 'desktop'
92 elif 'phone' in client:
93 self.type = 'phone'
94 elif 'web' in client:
95 self.type = 'web'
96 elif 'console' in client:
97 self.type = 'console'
98 else:
99 self.type = 'desktop'
100
101 self.identities = identities
102
103 @property
104 def name(self):
105 return self.identities.values()[0].values()[0][0]
106
107
108 class ItemWidget(TouchMenuItemBehaviour, BoxLayout):
109 name = properties.StringProperty()
110 base_width = properties.NumericProperty(dp(100))
111 80
112 81
113 class PathWidget(ItemWidget): 82 class PathWidget(ItemWidget):
114 83
115 def __init__(self, filepath, main_wid, **kw): 84 def __init__(self, filepath, main_wid, **kw):
169 super(RemotePathWidget, self).do_item_action(touch) 138 super(RemotePathWidget, self).do_item_action(touch)
170 else: 139 else:
171 self.main_wid.request_item(self) 140 self.main_wid.request_item(self)
172 return True 141 return True
173 142
174 143 class SharingDeviceWidget(DeviceWidget):
175 class DeviceWidget(ItemWidget):
176
177 def __init__(self, main_wid, entity_jid, identities, **kw):
178 self.entity_jid = entity_jid
179 self.identities = identities
180 own_jid = next(G.host.profiles.itervalues()).whoami
181 self.own_device = entity_jid.bare == own_jid
182 if self.own_device:
183 name = self.identities.name
184 elif self.entity_jid.node:
185 name = self.entity_jid.node
186 elif self.entity_jid.domain.endswith(own_jid.domain):
187 name = _(u"your server")
188 else:
189 name = _(u"sharing component")
190
191 super(DeviceWidget, self).__init__(name=name, main_wid=main_wid, **kw)
192
193 def getSymbol(self):
194 if self.identities.type == 'desktop':
195 return 'desktop'
196 elif self.identities.type == 'phone':
197 return 'mobile'
198 elif self.identities.type == 'web':
199 return 'globe'
200 elif self.identities.type == 'console':
201 return 'terminal'
202 else:
203 return 'desktop'
204 144
205 def do_item_action(self, touch): 145 def do_item_action(self, touch):
206 self.main_wid.remote_entity = self.entity_jid 146 self.main_wid.remote_entity = self.entity_jid
207 self.main_wid.remote_dir = u'' 147 self.main_wid.remote_dir = u''
208
209
210 class CategorySeparator(Label):
211 pass
212 148
213 149
214 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior, 150 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior,
215 TouchMenuBehaviour): 151 TouchMenuBehaviour):
216 SINGLE=False 152 SINGLE=False
300 _(u'your contacts devices'))): 236 _(u'your contacts devices'))):
301 if entities_map: 237 if entities_map:
302 self.layout.add_widget(CategorySeparator(text=title)) 238 self.layout.add_widget(CategorySeparator(text=title))
303 for entity_str, entity_ids in entities_map.iteritems(): 239 for entity_str, entity_ids in entities_map.iteritems():
304 entity_jid = jid.JID(entity_str) 240 entity_jid = jid.JID(entity_str)
305 item = DeviceWidget(self, 241 item = SharingDeviceWidget(
306 entity_jid, 242 self, entity_jid, Identities(entity_ids))
307 Identities(entity_ids))
308 self.layout.add_widget(item) 243 self.layout.add_widget(item)
309 if not entities_services and not entities_own and not entities_roster: 244 if not entities_services and not entities_own and not entities_roster:
310 self.layout.add_widget(Label( 245 self.layout.add_widget(Label(
311 size_hint=(1, 1), 246 size_hint=(1, 1),
312 halign='center', 247 halign='center',