annotate cagou/core/common.py @ 141:8a0019ce3632

core (android): service is now launched using pyjnius, as recommended in doc
author Goffi <goffi@goffi.org>
date Fri, 13 Apr 2018 18:58:14 +0200
parents 091e288838e1
children 654b00fa3fdc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr//bin/env python2
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
3
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
126
cd99f70ea592 global file reorganisation:
Goffi <goffi@goffi.org>
parents: 107
diff changeset
5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org)
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
6
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
11
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
16
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """common widgets, which can be reused everywhere"""
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
21
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
22 from sat.core.i18n import _
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.uix.image import Image
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
24 from kivy.uix.label import Label
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.uix.behaviors import ButtonBehavior
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
26 from kivy.uix.boxlayout import BoxLayout
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
27 from kivy import properties
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
28 from cagou import G
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
29 import json
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
30 from sat.core import log as logging
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
31
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
32 log = logging.getLogger(__name__)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
33
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
34 UNKNOWN_SYMBOL = u'Unknown symbol name'
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
35
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
36
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
37 class IconButton(ButtonBehavior, Image):
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
38 pass
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
39
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
40
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
41 class JidWidget(ButtonBehavior, BoxLayout):
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
42
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
43 def __init__(self, jid, profile, **kwargs):
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
44 self.jid = jid
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
45 self.profile = profile
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
46 self.nick = kwargs.get('nick')
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
47 super(JidWidget, self).__init__(**kwargs)
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
48
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
49 def getImage(self, wid):
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
50 host = G.host
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
51 if host.contact_lists[self.profile].isRoom(self.jid.bare):
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
52 wid.opacity = 0
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
53 return ""
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
54 else:
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
55 return host.getAvatar(self.jid, profile=self.profile) or host.getDefaultAvatar(self.jid)
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
56
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
57
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
58 class Symbol(Label):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
59 symbol_map = None
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
60 symbol = properties.StringProperty()
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
61 margin = properties.NumericProperty(0)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
62
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
63 def __init__(self, **kwargs):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
64 if self.symbol_map is None:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
65 with open(G.host.app.expand('{media}/fonts/fontello/config.json')) as f:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
66 fontello_conf = json.load(f)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
67 Symbol.symbol_map = {g['css']:g['code'] for g in fontello_conf['glyphs']}
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
68
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
69 super(Symbol, self).__init__(**kwargs)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
70
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
71 def on_symbol(self, instance, symbol):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
72 try:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
73 code = self.symbol_map[symbol]
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
74 except KeyError:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
75 log.warning(_(u"Invalid symbol {symbol}").format(symbol=symbol))
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
76 else:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
77 self.text = unichr(code)
135
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
78
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
79
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
80 class SymbolButton(ButtonBehavior, Symbol):
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
81 pass