Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 42:286865bc013a
chat: joining MUC using header input is now working:
in case of error, a simple log warning is used for now.
TODO: need to display a proper notification
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 28 Aug 2016 16:49:28 +0200 |
parents | 7cac2d1a6f20 |
children | 7819e9efa250 |
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"), | |
25
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
40 "icon_small": u"{media}/icons/muchoslava/png/chat_rouge_32.png", |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
41 "icon_medium": u"{media}/icons/muchoslava/png/chat_rouge_44.png" |
22 | 42 } |
43 | |
44 | |
45 class MessageWidget(BoxLayout): | |
46 mess_data = properties.ObjectProperty() | |
47 mess_label = properties.ObjectProperty(None) | |
48 | |
49 def __init__(self, **kwargs): | |
50 BoxLayout.__init__(self, orientation='vertical', **kwargs) | |
51 | |
52 @property | |
53 def message(self): | |
54 """Return currently displayed message""" | |
55 return self.mess_data.main_message | |
56 | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
57 def sizeAdjust(self): |
22 | 58 """this widget grows up with its children""" |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
59 text_width, text_height = self.mess_label.texture_size |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
60 if text_width > self.parent.width: |
22 | 61 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
|
62 self.text_max = text_width |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
63 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
|
64 if text_width > self.text_max: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
65 self.mess_label.text_size = (None, None) |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
66 else: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
67 self.mess_label.text_size = (self.parent.width - 10, None) |
22 | 68 |
69 | |
70 class MessageInputWidget(TextInput): | |
71 | |
72 def _key_down(self, key, repeat=False): | |
73 displayed_str, internal_str, internal_action, scale = key | |
74 if internal_action == 'enter': | |
75 self.dispatch('on_text_validate') | |
76 else: | |
77 super(MessageInputWidget, self)._key_down(key, repeat) | |
78 | |
79 | |
80 class MessagesWidget(BoxLayout): | |
81 _spacing = properties.NumericProperty(10) | |
82 _padding = properties.NumericProperty(5) | |
83 | |
84 def __init__(self, **kwargs): | |
85 kwargs['orientation'] = 'vertical' | |
86 kwargs['size_hint'] = (1, None) | |
87 super(MessagesWidget, self).__init__(**kwargs) | |
88 | |
89 def sizeAdjust(self): | |
90 self.height = sum([(c.height+self._padding*2) for c in self.children]) + self._spacing | |
91 | |
92 | |
93 class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget): | |
94 | |
95 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None): | |
96 quick_chat.QuickChat.__init__(self, host, target, type_, occupants, subject, profiles=profiles) | |
97 cagou_widget.CagouWidget.__init__(self) | |
98 self.header_input.hint_text = u"You are talking with {}".format(target) | |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
99 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0, do_scroll_x=False) |
22 | 100 self.messages_widget = MessagesWidget() |
101 scroll_view.add_widget(self.messages_widget) | |
102 self.add_widget(scroll_view) | |
103 message_input = MessageInputWidget() | |
104 message_input.bind(on_text_validate=self.onSend) | |
105 self.add_widget(message_input) | |
106 self.postInit() | |
107 | |
108 @classmethod | |
109 def factory(cls, plugin_info, target, profiles): | |
110 profiles = list(profiles) | |
111 if len(profiles) > 1: | |
112 raise NotImplementedError(u"Multi-profiles is not available yet for chat") | |
113 if target is None: | |
114 target = G.host.profiles[profiles[0]].whoami | |
115 return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE, profiles=profiles) | |
116 | |
117 def messageDataConverter(self, idx, mess_id): | |
118 return {"mess_data": self.messages[mess_id]} | |
119 | |
120 def _onHistoryPrinted(self): | |
121 """Refresh or scroll down the focus after the history is printed""" | |
122 # self.adapter.data = self.messages | |
123 for mess_data in self.messages.itervalues(): | |
124 self.appendMessage(mess_data) | |
125 super(Chat, self)._onHistoryPrinted() | |
126 | |
127 def createMessage(self, message): | |
128 self.appendMessage(message) | |
129 | |
130 def appendMessage(self, mess_data): | |
131 self.messages_widget.add_widget(MessageWidget(mess_data=mess_data)) | |
132 | |
133 def onSend(self, input_widget): | |
134 G.host.messageSend( | |
135 self.target, | |
136 {'': input_widget.text}, # TODO: handle language | |
137 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, # TODO: put this in QuickChat | |
138 profile_key=self.profile | |
139 ) | |
140 input_widget.text = '' | |
141 | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
142 def _mucJoinCb(self, bare): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
143 jid_ = jid.JID(bare) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
144 self.changeWidget(jid_) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
145 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
146 def _mucJoinEb(self, failure): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
147 log.warning(u"Can't join room: {}".format(failure)) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
148 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
149 def changeWidget(self, jid_): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
150 """change current widget for a new one with given jid |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
151 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
152 @param jid_(jid.JID): jid of the widget to create |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
153 """ |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
154 plugin_info = G.host.getPluginInfo(main=Chat) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
155 factory = plugin_info['factory'] |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
156 G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile])) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
157 self.header_input.text = '' |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
158 |
22 | 159 def onHeaderInput(self): |
160 text = self.header_input.text.strip() | |
161 try: | |
162 if text.count(u'@') != 1 or text.count(u' '): | |
163 raise ValueError | |
164 jid_ = jid.JID(text) | |
165 except ValueError: | |
166 log.info(u"entered text is not a jid") | |
167 return | |
168 | |
169 def discoCb(disco): | |
170 # TODO: check if plugin XEP-0045 is activated | |
171 if "conference" in [i[0] for i in disco[1]]: | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
172 G.host.bridge.mucJoin(unicode(jid_), "", "", self.profile, callback=self._mucJoinCb, errback=self._mucJoinEb) |
22 | 173 else: |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
174 self.changeWidget(jid_) |
22 | 175 |
176 def discoEb(failure): | |
177 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
178 | |
179 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
180 | |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
181 def onDelete(self, force=False): |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
182 if force==True: |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
183 return True |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
184 if len(list(G.host.widgets.getWidgets(self.__class__, self.target, profiles=self.profiles))) > 1: |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
185 # we don't keep duplicate widgets |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
186 return True |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
187 return False |
22 | 188 |
189 | |
190 PLUGIN_INFO["factory"] = Chat.factory | |
191 quick_widgets.register(quick_chat.QuickChat, Chat) |