Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 25:d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 10 Aug 2016 01:24:37 +0200 |
parents | bc15b55a4114 |
children | 20b04c58868f |
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) | |
99 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0) | |
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 | |
142 def onHeaderInput(self): | |
143 text = self.header_input.text.strip() | |
144 try: | |
145 if text.count(u'@') != 1 or text.count(u' '): | |
146 raise ValueError | |
147 jid_ = jid.JID(text) | |
148 except ValueError: | |
149 log.info(u"entered text is not a jid") | |
150 return | |
151 | |
152 def discoCb(disco): | |
153 # TODO: check if plugin XEP-0045 is activated | |
154 if "conference" in [i[0] for i in disco[1]]: | |
155 raise NotImplementedError(u"MUC not implemented yet") | |
156 # G.host.bridge.MUCJoin(unicode(jid_), "", "", self.profile) | |
157 else: | |
158 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 | |
159 factory = plugin_info['factory'] | |
160 G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile])) | |
161 | |
162 def discoEb(failure): | |
163 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
164 | |
165 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
166 | |
167 | |
168 | |
169 PLUGIN_INFO["factory"] = Chat.factory | |
170 quick_widgets.register(quick_chat.QuickChat, Chat) |