comparison cagou/plugins/plugin_wid_chat.py @ 409:2caea63ae2ab

chat: show attachments as clickable icons
author Goffi <goffi@goffi.org>
date Wed, 19 Feb 2020 09:49:21 +0100
parents 03554ad70846
children b018386653c2
comparison
equal deleted inserted replaced
408:355326a3501c 409:2caea63ae2ab
26 from kivy.uix import screenmanager 26 from kivy.uix import screenmanager
27 from kivy.uix.behaviors import ButtonBehavior 27 from kivy.uix.behaviors import ButtonBehavior
28 from kivy.metrics import sp, dp 28 from kivy.metrics import sp, dp
29 from kivy.clock import Clock 29 from kivy.clock import Clock
30 from kivy import properties 30 from kivy import properties
31 from kivy.uix.scrollview import ScrollView
31 from kivy.uix.dropdown import DropDown 32 from kivy.uix.dropdown import DropDown
32 from kivy.core.window import Window 33 from kivy.core.window import Window
33 from sat.core import log as logging 34 from sat.core import log as logging
34 from sat.core.i18n import _ 35 from sat.core.i18n import _
35 from sat.core import exceptions 36 from sat.core import exceptions
74 75
75 # below this limit, new messages will be prepended 76 # below this limit, new messages will be prepended
76 INFINITE_SCROLL_LIMIT = dp(600) 77 INFINITE_SCROLL_LIMIT = dp(600)
77 78
78 79
80 class AttachmentsLayout(ScrollView):
81 attachments = properties.ObjectProperty()
82
83
84 class AttachmentItem(BoxLayout):
85 data = properties.DictProperty()
86
87 def get_symbol(self, data):
88 media_type = data.get('media_type', '')
89 main_type = media_type.split('/', 1)[0]
90 if main_type == 'image':
91 return "file-image"
92 elif main_type == 'video':
93 return "file-video"
94 elif main_type == 'audio':
95 return "file-audio"
96 else:
97 return "doc"
98
99 def on_press(self):
100 url = self.data.get('url')
101 if url:
102 G.local_platform.open_url(url, self)
103 else:
104 log.warning("can't find URL in {self.data}")
105
106
79 class MessAvatar(ButtonBehavior, Image): 107 class MessAvatar(ButtonBehavior, Image):
80 pass 108 pass
81 109
82 110
83 class MessageWidget(quick_chat.MessageWidget, BoxLayout): 111 class MessageWidget(quick_chat.MessageWidget, BoxLayout):
88 delivery = properties.ObjectProperty() 116 delivery = properties.ObjectProperty()
89 font_size = properties.NumericProperty(sp(12)) 117 font_size = properties.NumericProperty(sp(12))
90 right_part = properties.ObjectProperty() 118 right_part = properties.ObjectProperty()
91 header_box = properties.ObjectProperty() 119 header_box = properties.ObjectProperty()
92 120
93 def on_mess_data(self, wid, mess_data): 121 def on_kv_post(self, __):
94 mess_data.widgets.add(self) 122 if not self.mess_data:
123 raise exceptions.InternalError(
124 "mess_data must always be set in MessageWidget")
125
126 self.mess_data.widgets.add(self)
127 self.add_attachments()
95 128
96 @property 129 @property
97 def chat(self): 130 def chat(self):
98 """return parent Chat instance""" 131 """return parent Chat instance"""
99 return self.mess_data.parent 132 return self.mess_data.parent
142 if 'avatar' in update_dict: 175 if 'avatar' in update_dict:
143 self.avatar.source = update_dict['avatar'] 176 self.avatar.source = update_dict['avatar']
144 if 'status' in update_dict: 177 if 'status' in update_dict:
145 status = update_dict['status'] 178 status = update_dict['status']
146 self.delivery.text = '\u2714' if status == 'delivered' else '' 179 self.delivery.text = '\u2714' if status == 'delivered' else ''
180
181 def add_attachments(self):
182 """Add attachments layout + attachments item"""
183 attachments = self.mess_data.attachments
184 if not attachments:
185 return
186 root_layout = AttachmentsLayout()
187 self.right_part.add_widget(root_layout)
188 layout = root_layout.attachments
189 for attachment in attachments:
190 item = AttachmentItem(data=attachment)
191 layout.add_widget(item)
147 192
148 193
149 class SendButton(SymbolButton): 194 class SendButton(SymbolButton):
150 message_input_box = properties.ObjectProperty() 195 message_input_box = properties.ObjectProperty()
151 196
853 898
854 def _backHistoryGetCb(self, history): 899 def _backHistoryGetCb(self, history):
855 # TODO: factorise with QuickChat._historyGetCb 900 # TODO: factorise with QuickChat._historyGetCb
856 scroll_start_height = self.messages_widget.height * self.history_scroll.scroll_y 901 scroll_start_height = self.messages_widget.height * self.history_scroll.scroll_y
857 for data in reversed(history): 902 for data in reversed(history):
858 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data 903 uid, timestamp, from_jid, to_jid, message, subject, type_, extra_s = data
859 from_jid = jid.JID(from_jid) 904 from_jid = jid.JID(from_jid)
860 to_jid = jid.JID(to_jid) 905 to_jid = jid.JID(to_jid)
906 extra = data_format.deserialise(extra_s)
861 extra["history"] = True 907 extra["history"] = True
862 self.messages[uid] = message = quick_chat.Message( 908 self.messages[uid] = message = quick_chat.Message(
863 self, 909 self,
864 uid, 910 uid,
865 timestamp, 911 timestamp,