changeset 370:30d03d9f07e4

browser_side: refactorization of the file tools.py: - tools.py renamed to html_tools.py - method setPresenceStyle moved to contact.py - classes DragLabel and LiberviaDragWidget moved to base_widget.py - class FilterFileUpload moved to file_tools.py
author souliane <souliane@mailoo.org>
date Sun, 23 Feb 2014 17:01:03 +0100
parents 678d1739bbf2
children d7870ab9d1ff
files browser_side/base_panels.py browser_side/base_widget.py browser_side/contact.py browser_side/file_tools.py browser_side/html_tools.py browser_side/menu.py browser_side/panels.py browser_side/radiocol.py browser_side/tools.py
diffstat 9 files changed, 164 insertions(+), 141 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_panels.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/base_panels.py	Sun Feb 23 17:01:03 2014 +0100
@@ -35,7 +35,7 @@
 from datetime import datetime
 from time import time
 
-from tools import html_sanitize, html_strip, inlineRoot
+from html_tools import html_sanitize, html_strip, inlineRoot
 
 from sat_frontends.tools.strings import addURLToText, addURLToImage
 from sat.core.i18n import _
--- a/browser_side/base_widget.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/base_widget.py	Sun Feb 23 17:01:03 2014 +0100
@@ -30,6 +30,7 @@
 from pyjamas.ui.Button import Button
 from pyjamas.ui.Image import Image
 from pyjamas.ui.Widget import Widget
+from pyjamas.ui.DragWidget import DragWidget
 from pyjamas.ui.DropWidget import DropWidget
 from pyjamas.ui.ClickListener import ClickHandler
 from pyjamas.ui import HasAlignment
@@ -38,7 +39,35 @@
 from __pyjamas__ import doc
 
 import dialog
-from tools import LiberviaDragWidget
+
+
+class DragLabel(DragWidget):
+
+    def __init__(self, text, _type):
+        DragWidget.__init__(self)
+        self._text = text
+        self._type = _type
+
+    def onDragStart(self, event):
+        dt = event.dataTransfer
+        dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
+        dt.setDragImage(self.getElement(), 15, 15)
+
+
+class LiberviaDragWidget(DragLabel):
+    """ A DragLabel which keep the widget being dragged as class value """
+    current = None  # widget currently dragged
+
+    def __init__(self, text, _type, widget):
+        DragLabel.__init__(self, text, _type)
+        self.widget = widget
+
+    def onDragStart(self, event):
+        LiberviaDragWidget.current = self.widget
+        DragLabel.onDragStart(self, event)
+
+    def onDragEnd(self, event):
+        LiberviaDragWidget.current = None
 
 
 class DropCell(DropWidget):
--- a/browser_side/contact.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/contact.py	Sun Feb 23 17:01:03 2014 +0100
@@ -29,11 +29,30 @@
 from pyjamas import DOM
 
 from browser_side.base_panels import PopupMenuPanel
+from browser_side.base_widget import DragLabel
 from browser_side.panels import ChatPanel, MicroblogPanel, WebPanel, UniBoxPanel
-from browser_side.tools import DragLabel, html_sanitize, setPresenceStyle
+from browser_side.tools import html_sanitize
 from __pyjamas__ import doc
 
 
+def setPresenceStyle(element, presence, base_style="contact"):
+    """
+    Set the CSS style of a contact's element according to its presence.
+    @param item: the UI element of the contact
+    @param presence: a value in ("", "chat", "away", "dnd", "xa").
+    @param base_style: the base name of the style to apply
+    """
+    if not hasattr(element, 'presence_style'):
+        element.presence_style = None
+    style = '%s-%s' % (base_style, presence or 'connected')
+    if style == element.presence_style:
+        return
+    if element.presence_style is not None:
+        element.removeStyleName(element.presence_style)
+    element.addStyleName(style)
+    element.presence_style = style
+
+
 class GroupLabel(DragLabel, Label, ClickHandler):
     def __init__(self, host, group):
         self.group = group
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/browser_side/file_tools.py	Sun Feb 23 17:01:03 2014 +0100
@@ -0,0 +1,61 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Libervia: a Salut à Toi frontend
+# Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from pyjamas.ui.FileUpload import FileUpload
+from pyjamas import Window
+from pyjamas import DOM
+
+
+class FilterFileUpload(FileUpload):
+
+    def __init__(self, name, max_size, types=None):
+        """
+        @param name: the input element name
+        @param max_size: maximum file size in MB
+        @param types: allowed types as a list of couples (x, y, z):
+        - x: MIME content type e.g. "audio/ogg"
+        - y: file extension e.g. "*.ogg"
+        - z: description for the user e.g. "Ogg Vorbis Audio"
+        If types is None, all file format are accepted
+        """
+        FileUpload.__init__(self)
+        self.setName(name)
+        while DOM.getElementById(name):
+            name = "%s_" % name
+        self.setID(name)
+        self._id = name
+        self.max_size = max_size
+        self.types = types
+
+    def getFileInfo(self):
+        from __pyjamas__ import JS
+        JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]")
+
+    def check(self):
+        if self.getFilename() == "":
+            return False
+        (size, filetype) = self.getFileInfo()
+        if self.types and filetype not in [x for (x, y, z) in self.types]:
+            types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types]
+            Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types))
+            return False
+        if size > self.max_size * pow(2, 20):
+            Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size)
+            return False
+        return True
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/browser_side/html_tools.py	Sun Feb 23 17:01:03 2014 +0100
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Libervia: a Salut à Toi frontend
+# Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from nativedom import NativeDOM
+from sat_frontends.tools import xmltools
+import re
+
+dom = NativeDOM()
+
+
+def html_sanitize(html):
+    """Naive sanitization of HTML"""
+    return html.replace('<', '&lt;').replace('>', '&gt;')
+
+
+def html_strip(html):
+    """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
+    cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html)
+    cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned)
+    return cleaned
+
+
+def inlineRoot(xhtml):
+    """ make root element inline """
+    doc = dom.parseString(xhtml)
+    return xmltools.inlineRoot(doc)
--- a/browser_side/menu.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/menu.py	Sun Feb 23 17:01:03 2014 +0100
@@ -34,8 +34,8 @@
 from pyjamas.ui.Frame import Frame
 from pyjamas import Window
 from jid import JID
-from tools import html_sanitize
-from tools import FilterFileUpload
+from html_tools import html_sanitize
+from file_tools import FilterFileUpload
 from xmlui import XMLUI
 import panels
 import dialog
--- a/browser_side/panels.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/panels.py	Sun Feb 23 17:01:03 2014 +0100
@@ -39,17 +39,19 @@
 from pyjamas import Window
 from __pyjamas__ import doc
 
-from tools import html_sanitize, setPresenceStyle
-from base_panels import ChatText, OccupantsList, PopupMenuPanel, BaseTextEditor, LightTextEditor
 from datetime import datetime
 from time import time
+from jid import JID
+
+from html_tools import html_sanitize
+from base_panels import ChatText, OccupantsList, PopupMenuPanel, BaseTextEditor, LightTextEditor
 from card_game import CardPanel
 from radiocol import RadioColPanel
 from menu import Menu
-from jid import JID
 import dialog
 import base_widget
 import richtext
+import contact
 
 from constants import Const
 from plugin_xep_0085 import ChatStateMachine
@@ -970,7 +972,7 @@
 
     def setPresence(self, presence):
         self._presence = presence
-        setPresenceStyle(self.presence_button, self._presence)
+        contact.setPresenceStyle(self.presence_button, self._presence)
 
     def setStatus(self, status):
         self.status_panel.setContent({'text': status})
--- a/browser_side/radiocol.py	Sun Feb 23 16:20:03 2014 +0100
+++ b/browser_side/radiocol.py	Sun Feb 23 17:01:03 2014 +0100
@@ -33,8 +33,8 @@
 from pyjamas.Timer import Timer
 from __pyjamas__ import JS
 
-from tools import html_sanitize
-from tools import FilterFileUpload
+from html_tools import html_sanitize
+from file_tools import FilterFileUpload
 from sat_frontends.tools.misc import DEFAULT_MUC
 
 
--- a/browser_side/tools.py	Sun Feb 23 16:20:03 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-# Libervia: a Salut à Toi frontend
-# Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-from pyjamas.ui.DragWidget import DragWidget
-from pyjamas.ui.FileUpload import FileUpload
-from pyjamas import Window
-from nativedom import NativeDOM
-from sat_frontends.tools import xmltools
-import re
-
-dom = NativeDOM()
-
-
-def html_sanitize(html):
-    """Naive sanitization of HTML"""
-    return html.replace('<', '&lt;').replace('>', '&gt;')
-
-
-def html_strip(html):
-    """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
-    cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html)
-    cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned)
-    return cleaned
-
-
-def inlineRoot(xhtml):
-    """ make root element inline """
-    doc = dom.parseString(xhtml)
-    return xmltools.inlineRoot(doc)
-
-
-def setPresenceStyle(item, state, base_style="contact"):
-    """
-    @item: any UI element
-    @state: a value from ("", "chat", "away", "dnd", "xa")
-    """
-    if not hasattr(item, 'presence_style'):
-        item.presence_style = None
-    style = '%s-%s' % (base_style, state or 'connected')
-    if style == item.presence_style:
-        return
-    if item.presence_style is not None:
-        item.removeStyleName(item.presence_style)
-    item.addStyleName(style)
-    item.presence_style = style
-
-
-class DragLabel(DragWidget):
-
-    def __init__(self, text, _type):
-        DragWidget.__init__(self)
-        self._text = text
-        self._type = _type
-
-    def onDragStart(self, event):
-        dt = event.dataTransfer
-        dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
-        dt.setDragImage(self.getElement(), 15, 15)
-
-
-class LiberviaDragWidget(DragLabel):
-    """ A DragLabel which keep the widget being dragged as class value """
-    current = None  # widget currently dragged
-
-    def __init__(self, text, _type, widget):
-        DragLabel.__init__(self, text, _type)
-        self.widget = widget
-
-    def onDragStart(self, event):
-        LiberviaDragWidget.current = self.widget
-        DragLabel.onDragStart(self, event)
-
-    def onDragEnd(self, event):
-        LiberviaDragWidget.current = None
-
-
-class FilterFileUpload(FileUpload):
-
-    def __init__(self, name, max_size, types=None):
-        """
-        @param name: the input element name
-        @param max_size: maximum file size in MB
-        @param types: allowed types as a list of couples (x, y, z):
-        - x: MIME content type e.g. "audio/ogg"
-        - y: file extension e.g. "*.ogg"
-        - z: description for the user e.g. "Ogg Vorbis Audio"
-        If types is None, all file format are accepted
-        """
-        FileUpload.__init__(self)
-        self.setName(name)
-        from pyjamas import DOM
-        while DOM.getElementById(name):
-            name = "%s_" % name
-        self.setID(name)
-        self._id = name
-        self.max_size = max_size
-        self.types = types
-
-    def getFileInfo(self):
-        from __pyjamas__ import JS
-        JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]")
-
-    def check(self):
-        if self.getFilename() == "":
-            return False
-        (size, filetype) = self.getFileInfo()
-        if self.types and filetype not in [x for (x, y, z) in self.types]:
-            types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types]
-            Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types))
-            return False
-        if size > self.max_size * pow(2, 20):
-            Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size)
-            return False
-        return True