changeset 1966:d727aab9a80e

primitivus: basic handling of filter, only language can filtered for now
author Goffi <goffi@goffi.org>
date Mon, 20 Jun 2016 19:19:16 +0200
parents 4c5d8cd35690
children de6faf9be715
files frontends/src/primitivus/chat.py frontends/src/primitivus/primitivus
diffstat 2 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Mon Jun 20 18:44:27 2016 +0200
+++ b/frontends/src/primitivus/chat.py	Mon Jun 20 19:19:16 2016 +0200
@@ -189,6 +189,7 @@
 
     def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None):
         quick_chat.QuickChat.__init__(self, host, target, type_, occupants, subject, profiles=profiles)
+        self.filters = []  # list of filter callbacks to apply
         self.mess_walker = urwid.SimpleListWalker([])
         self.mess_widgets = urwid.ListBox(self.mess_walker)
         self.chat_widget = urwid.Frame(self.mess_widgets)
@@ -268,6 +269,24 @@
             self.host.addMenus(menu, C.MENU_SINGLE, {'jid': full_jid})
         return menu
 
+    def setFilter(self, args):
+        """set filtering of messages
+
+        @param args(list[unicode]): filters following syntax "[filter]=[value]"
+            empty list to clear all filters
+            only lang=XX is handled for now
+        """
+        if args:
+            if args[0].startswith("lang="):
+                lang = args[0][5:].strip()
+                self.filters.append(lambda mess_data: lang in mess_data.message)
+        else:
+            del self.filters[:]
+
+        del self.mess_walker[:]
+        for message in self.messages.itervalues():
+            self.appendMessage(message)
+
     def presenceListener(self, entity, show, priority, statuses, profile):
         """Update entity's presence status
 
@@ -307,6 +326,9 @@
         Can merge messages together is desirable (e.g.: multiple joined/leave)
         @param message(quick_chat.Message): message to add
         """
+        if self.filters:
+            if not all([f(message) for f in self.filters]):
+                return
         if self._user_moved(message):
             for wid in reversed(self.mess_walker):
                 # we merge in/out messages if no message was sent meanwhile
--- a/frontends/src/primitivus/primitivus	Mon Jun 20 18:44:27 2016 +0200
+++ b/frontends/src/primitivus/primitivus	Mon Jun 20 19:19:16 2016 +0200
@@ -148,6 +148,13 @@
                     self.host.notif_bar.addMessage(D_("Please specify the globbing pattern to search for"))
                 else:
                     widget.updateHistory(size=C.HISTORY_LIMIT_NONE, search=pattern, profile=widget.profile)
+        elif command == 'filter':
+            # FIXME: filter is now only for current widget,
+            #        need to be able to set it globally or per widget
+            widget = self.host.selected_widget
+            # FIXME: Q&D way, need to be more generic
+            if isinstance(widget, quick_chat.QuickChat):
+                widget.setFilter(args)
         else:
             return
         self.set_edit_text('')