annotate browser_side/tools.py @ 279:2d6bd975a72d

browser_side: set your own presence status and display those of others
author souliane <souliane@mailoo.org>
date Sat, 23 Nov 2013 14:46:03 +0100
parents aebb96bfa8d1
children 0eba1c4f9c6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
3
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
165
9763dec220ed dates update
Goffi <goffi@goffi.org>
parents: 131
diff changeset
6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
7
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
12
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
17
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
21
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
22 from pyjamas.ui.DragWidget import DragWidget
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
23 import re
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
24 from nativedom import NativeDOM
276
aebb96bfa8d1 frontends tools: moved src/tools/frontends to frontends/src/tools
souliane <souliane@mailoo.org>
parents: 246
diff changeset
25 from sat_frontends.tools import xml
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
26
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
27 dom = NativeDOM()
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
28
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
29
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def html_sanitize(html):
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """Naive sanitization of HTML"""
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
32 return html.replace('<', '&lt;').replace('>', '&gt;')
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
33
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
34
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
35 def inlineRoot(xhtml):
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
36 """ make root element inline """
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
37 doc = dom.parseString(xhtml)
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
38 return xml.inlineRoot(doc)
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
39
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
40
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
41 def addURLToText(string):
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
42 """Check a text for what looks like an URL and make it clickable. Regexp
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
43 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
44
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
45 def repl(match):
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
46 url = match.group(0)
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
47 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
48 url = "http://" + url
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
49 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
50 pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))"""
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
51 return re.sub(pattern, repl, string)
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
52
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
53
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
54 def setPresenceStyle(item, state, base_style="contact"):
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
55 """
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
56 @item: any UI element
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
57 @state: a value from ("", "chat", "away", "dnd", "xa")
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
58 """
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
59 if not hasattr(item, 'presence_style'):
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
60 item.presence_style = None
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
61 style = '%s-%s' % (base_style, state or 'connected')
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
62 if style == item.presence_style:
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
63 return
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
64 if item.presence_style is not None:
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
65 item.removeStyleName(item.presence_style)
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
66 item.addStyleName(style)
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
67 item.presence_style = style
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
68
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
69
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
70 class DragLabel(DragWidget):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
71
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
72 def __init__(self, text, _type):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
73 DragWidget.__init__(self)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
74 self._text = text
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
75 self._type = _type
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
76
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
77 def onDragStart(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
78 dt = event.dataTransfer
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
79 dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
80 dt.setDragImage(self.getElement(), 15, 15)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
81
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
82
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
83 class LiberviaDragWidget(DragLabel):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
84 """ A DragLabel which keep the widget being dragged as class value """
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
85 current = None # widget currently dragged
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
86
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
87 def __init__(self, text, _type, widget):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
88 DragLabel.__init__(self, text, _type)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
89 self.widget = widget
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
90
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
91 def onDragStart(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
92 LiberviaDragWidget.current = self.widget
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
93 DragLabel.onDragStart(self, event)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
94
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
95 def onDragEnd(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
96 LiberviaDragWidget.current = None