Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 24:bc15b55a4114
chat: better bubble and time resizing
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 09 Aug 2016 22:06:00 +0200 |
parents | 74117b733bac |
children | d09bd16dbbe2 |
rev | line source |
---|---|
22 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from sat.core.i18n import _ | |
24 from cagou.core.constants import Const as C | |
25 from kivy.uix.boxlayout import BoxLayout | |
26 from kivy.uix.scrollview import ScrollView | |
27 from kivy.uix.textinput import TextInput | |
28 from kivy import properties | |
29 from sat_frontends.quick_frontend import quick_widgets | |
30 from sat_frontends.quick_frontend import quick_chat | |
31 from sat_frontends.tools import jid | |
32 from cagou.core import cagou_widget | |
33 from cagou import G | |
34 | |
35 | |
36 PLUGIN_INFO = { | |
37 "name": _(u"chat"), | |
38 "main": "Chat", | |
39 "description": _(u"instant messaging with one person or a group"), | |
40 } | |
41 | |
42 | |
43 class MessageWidget(BoxLayout): | |
44 mess_data = properties.ObjectProperty() | |
45 mess_label = properties.ObjectProperty(None) | |
46 | |
47 def __init__(self, **kwargs): | |
48 BoxLayout.__init__(self, orientation='vertical', **kwargs) | |
49 | |
50 @property | |
51 def message(self): | |
52 """Return currently displayed message""" | |
53 return self.mess_data.main_message | |
54 | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
55 def sizeAdjust(self): |
22 | 56 """this widget grows up with its children""" |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
57 text_width, text_height = self.mess_label.texture_size |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
58 if text_width > self.parent.width: |
22 | 59 self.mess_label.text_size = (self.parent.width - 10, None) |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
60 self.text_max = text_width |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
61 elif self.mess_label.text_size[0] is not None and text_width < self.parent.width - 10: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
62 if text_width > self.text_max: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
63 self.mess_label.text_size = (None, None) |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
64 else: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
65 self.mess_label.text_size = (self.parent.width - 10, None) |
22 | 66 |
67 | |
68 class MessageInputWidget(TextInput): | |
69 | |
70 def _key_down(self, key, repeat=False): | |
71 displayed_str, internal_str, internal_action, scale = key | |
72 if internal_action == 'enter': | |
73 self.dispatch('on_text_validate') | |
74 else: | |
75 super(MessageInputWidget, self)._key_down(key, repeat) | |
76 | |
77 | |
78 class MessagesWidget(BoxLayout): | |
79 _spacing = properties.NumericProperty(10) | |
80 _padding = properties.NumericProperty(5) | |
81 | |
82 def __init__(self, **kwargs): | |
83 kwargs['orientation'] = 'vertical' | |
84 kwargs['size_hint'] = (1, None) | |
85 super(MessagesWidget, self).__init__(**kwargs) | |
86 | |
87 def sizeAdjust(self): | |
88 self.height = sum([(c.height+self._padding*2) for c in self.children]) + self._spacing | |
89 | |
90 | |
91 class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget): | |
92 | |
93 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None): | |
94 quick_chat.QuickChat.__init__(self, host, target, type_, occupants, subject, profiles=profiles) | |
95 cagou_widget.CagouWidget.__init__(self) | |
96 self.header_input.hint_text = u"You are talking with {}".format(target) | |
97 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0) | |
98 self.messages_widget = MessagesWidget() | |
99 scroll_view.add_widget(self.messages_widget) | |
100 self.add_widget(scroll_view) | |
101 message_input = MessageInputWidget() | |
102 message_input.bind(on_text_validate=self.onSend) | |
103 self.add_widget(message_input) | |
104 self.postInit() | |
105 | |
106 @classmethod | |
107 def factory(cls, plugin_info, target, profiles): | |
108 profiles = list(profiles) | |
109 if len(profiles) > 1: | |
110 raise NotImplementedError(u"Multi-profiles is not available yet for chat") | |
111 if target is None: | |
112 target = G.host.profiles[profiles[0]].whoami | |
113 return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE, profiles=profiles) | |
114 | |
115 def messageDataConverter(self, idx, mess_id): | |
116 return {"mess_data": self.messages[mess_id]} | |
117 | |
118 def _onHistoryPrinted(self): | |
119 """Refresh or scroll down the focus after the history is printed""" | |
120 # self.adapter.data = self.messages | |
121 for mess_data in self.messages.itervalues(): | |
122 self.appendMessage(mess_data) | |
123 super(Chat, self)._onHistoryPrinted() | |
124 | |
125 def createMessage(self, message): | |
126 self.appendMessage(message) | |
127 | |
128 def appendMessage(self, mess_data): | |
129 self.messages_widget.add_widget(MessageWidget(mess_data=mess_data)) | |
130 | |
131 def onSend(self, input_widget): | |
132 G.host.messageSend( | |
133 self.target, | |
134 {'': input_widget.text}, # TODO: handle language | |
135 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, # TODO: put this in QuickChat | |
136 profile_key=self.profile | |
137 ) | |
138 input_widget.text = '' | |
139 | |
140 def onHeaderInput(self): | |
141 text = self.header_input.text.strip() | |
142 try: | |
143 if text.count(u'@') != 1 or text.count(u' '): | |
144 raise ValueError | |
145 jid_ = jid.JID(text) | |
146 except ValueError: | |
147 log.info(u"entered text is not a jid") | |
148 return | |
149 | |
150 def discoCb(disco): | |
151 # TODO: check if plugin XEP-0045 is activated | |
152 if "conference" in [i[0] for i in disco[1]]: | |
153 raise NotImplementedError(u"MUC not implemented yet") | |
154 # G.host.bridge.MUCJoin(unicode(jid_), "", "", self.profile) | |
155 else: | |
156 plugin_info = [p for p in G.host.getPluggedWidgets() if p["factory"] == self.factory][0] # FIXME: Q&D way, need a proper method in host | |
157 factory = plugin_info['factory'] | |
158 G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile])) | |
159 | |
160 def discoEb(failure): | |
161 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
162 | |
163 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
164 | |
165 | |
166 | |
167 PLUGIN_INFO["factory"] = Chat.factory | |
168 quick_widgets.register(quick_chat.QuickChat, Chat) |