changeset 128:2240f34f6452

Primitivus: misc fixes + menubar first draft - Menu bar: first draft of class - Password widget fixed - change some str to unicode, notably for JID
author Goffi <goffi@goffi.org>
date Tue, 13 Jul 2010 02:24:59 +0800
parents 55d3ef84f01f
children 542682d67282
files frontends/primitivus/contact_list.py frontends/primitivus/custom_widgets.py frontends/primitivus/primitivus frontends/primitivus/profile_manager.py sat_bridge/DBus.py tools/jid.py
diffstat 6 files changed, 81 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/primitivus/contact_list.py	Mon Jul 12 18:55:27 2010 +0800
+++ b/frontends/primitivus/contact_list.py	Tue Jul 13 02:24:59 2010 +0800
@@ -150,9 +150,10 @@
         """mark a contact disconnected"""
         self.remove(jid.short)
     
-    def remove(self, jid):
+    def remove(self, param_jid):
         """remove a contact from the list"""
         groups_to_remove = []
+        jid = JID(param_jid)
         for group in self.groups:
             contacts = self.groups[group][1]
             if jid.short in contacts:
--- a/frontends/primitivus/custom_widgets.py	Mon Jul 12 18:55:27 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Tue Jul 13 02:24:59 2010 +0800
@@ -23,6 +23,7 @@
 from urwid.escape import utf8decode
 
 class Password(urwid.Edit):
+    toto=0
     """Edit box which doesn't show what is entered (show '*' or other char instead)"""
 
     def __init__(self, *args, **kwargs):
@@ -41,6 +42,18 @@
     def get_edit_text(self):
         return self.__real_text
 
+    def insert_text(self, text):
+        self._edit_text = self.__real_text
+        super(Password,self).insert_text(text)
+
+    def render(self, size, focus=False):
+        Password.toto+=1
+        if Password.toto==30:
+            import os,pdb
+            os.system('reset')
+            pdb.set_trace()
+        return super(Password, self).render(size, focus)
+
 class AdvancedEdit(urwid.Edit):
     """Edit box with some custom improvments
     new chars:
@@ -327,6 +340,49 @@
         height = min(list_size,self.max_height) or 1 
         return urwid.BoxAdapter(self.genericList, height)
 
+## MISC ##
+
+class Menu(urwid.FlowWidget):
+
+    def __init__(self):
+        super(Menu, self).__init__()
+        self.menu_keys = ['test']
+        self.menu = {'test':[('top',None)]}
+        self.shortcuts = {} #keyboard shortcuts
+
+    def checkShortcuts(self, key):
+        for shortcut in self.shortcuts.keys():
+            if key == shortcut:
+                category, item, callback = self.shortcuts[shortcuts]
+                callback((category, item))
+        return key
+
+    def addMenu(self, category, item, callback, shortcut=None):
+        """Add a menu item, create the category if new
+        @param category: category of the menu (e.g. File/Edit)
+        @param item: menu item (e.g. new/close/about)
+        @callback: method to call when item is selected"""
+        if not category in self.menu.keys():
+            self.menu_keys.append(category)
+            self.menu[category] = []
+        self.menu[category].append[(item, callback)]
+        if shortcut:
+            assert(shortcut not in self.shortcuts.keys())
+            self.shortcuts[shortcut] = (category, item, callback)
+
+    def rows(self,size,focus=False):
+        return self.display_widget(size, focus).rows(size, focus)
+
+    def render(self, size, focus=False):
+        return self.display_widget(size, focus).render(size, focus)
+
+    def display_widget(self, size, focus):
+        render_txt = []
+        for menu in self.menu_keys:
+            render_txt.append('[ %s ] ' % menu) 
+        return urwid.AttrMap(urwid.Text(render_txt), 'menubar')
+
+
 ## DIALOGS ##
 
 class GenericDialog(urwid.WidgetWrap):
--- a/frontends/primitivus/primitivus	Mon Jul 12 18:55:27 2010 +0800
+++ b/frontends/primitivus/primitivus	Tue Jul 13 02:24:59 2010 +0800
@@ -28,7 +28,7 @@
 from profile_manager import ProfileManager
 from contact_list import ContactList
 from chat import Chat
-from custom_widgets import AdvancedEdit,FocusFrame,InputDialog,Alert
+import custom_widgets
 import pdb
 """from window import Window
 from editbox import EditBox
@@ -65,6 +65,7 @@
                  ('date', 'light gray', 'default'),
                  ('my_nick', 'dark red,bold', 'default'),
                  ('other_nick', 'dark cyan,bold', 'default'),
+                 ('menubar', 'light gray,bold', 'dark red'),
                  ]
             
 class ChatList(QuickChatList):
@@ -118,10 +119,18 @@
     def keyHandler(self, input):
         if input in ('q', 'Q') or input == 'ctrl x':
             raise urwid.ExitMainLoop()
+        elif input == 'meta m':
+            try:
+                if self.main_widget.header == None:
+                    self.main_widget.header = self.menu
+                else:
+                    self.main_widget.header = None
+            except AttributeError:
+                pass
         elif input == 'ctrl d' and 'D' in self.bridge.getVersion(): #Debug only for dev versions
             self.debug()
         elif input == 'meta j': #user wants to join a room
-            pop_up_widget = InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt = 'test@conference.necton2.int', cancel_cb=self.removePopUp, ok_cb=self.onJoinRoom)
+            pop_up_widget = custom_widgets.InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt = 'test@conference.necton2.int', cancel_cb=self.removePopUp, ok_cb=self.onJoinRoom)
             self.showPopUp(pop_up_widget)
         elif input == 'f2': #user wants to (un)hide the contact_list
             try:
@@ -136,14 +145,19 @@
             except AttributeError:
                 #The main widget is not built (probably in Profile Manager)
                 pass
+        try:
+            return self.menu.checkShortcuts(input)
+        except AttributeError:
+            return input
 
     def __buildMainWidget(self):
         self.contactList = ContactList(self, self.CM, on_click = self.contactSelected, on_change=lambda w: self.redraw())
         #self.center_part = urwid.Columns([('weight',2,self.contactList),('weight',8,Chat('',self))])
         self.center_part = urwid.Columns([('weight',2,self.contactList), ('weight',8,urwid.Filler(urwid.Text('')))])
-        editBar = AdvancedEdit('> ')
+        editBar = custom_widgets.AdvancedEdit('> ')
         urwid.connect_signal(editBar,'click',self.onTextEntered)
-        self.main_widget = FocusFrame(self.center_part, footer=editBar, focus_part='footer')
+        self.menu = custom_widgets.Menu()
+        self.main_widget = custom_widgets.FocusFrame(self.center_part, header=self.menu, footer=editBar, focus_part='footer')
         return self.main_widget
 
     def plug_profile(self, profile_key='@DEFAULT@'):
@@ -192,7 +206,7 @@
         else:
             message = _("'%s' is an invalid JID !") % room_jid
             error (message)
-            Alert(_("Error"), message, ok_cb=self.removePopUp)        
+            custom_widgets.Alert(_("Error"), message, ok_cb=self.removePopUp)        
 
 sat = PrimitivusApp()
 sat.start()
--- a/frontends/primitivus/profile_manager.py	Mon Jul 12 18:55:27 2010 +0800
+++ b/frontends/primitivus/profile_manager.py	Tue Jul 13 02:24:59 2010 +0800
@@ -21,6 +21,7 @@
 
 import urwid
 from custom_widgets import Password,List,InputDialog,ConfirmDialog,Alert,FocusFrame
+from tools.jid import JID
 
 
 class ProfileManager(urwid.WidgetWrap):
--- a/sat_bridge/DBus.py	Mon Jul 12 18:55:27 2010 +0800
+++ b/sat_bridge/DBus.py	Tue Jul 13 02:24:59 2010 +0800
@@ -196,7 +196,7 @@
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
                          in_signature='ssss', out_signature='')
     def setParam(self, name, value, category, profile_key='@DEFAULT@'):
-        self.cb["setParam"](str(name), str(value), str(category), profile_key)
+        self.cb["setParam"](unicode(name), unicode(value), unicode(category), profile_key)
         
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
                          in_signature='sss', out_signature='s')
--- a/tools/jid.py	Mon Jul 12 18:55:27 2010 +0800
+++ b/tools/jid.py	Tue Jul 13 02:24:59 2010 +0800
@@ -21,11 +21,11 @@
 
 
 
-class JID(str):
+class JID(unicode):
     """This class help manage JID (Node@Domaine/Resource)"""
 
     def __new__(cls, jid):
-        self = str.__new__(cls, jid)
+        self = unicode.__new__(cls, jid)
         self.__parse()
         return self