Changeset 712

Show
Ignore:
Timestamp:
07/09/08 10:27:39 (5 months ago)
Author:
sylvain
Message:

Solified roster/presence management between contacts

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • oss/jlib/examples/basic/client.py

    r710 r712  
    5656        chatUi = jlibChatWidget(str(contact.jid), parent=widget) 
    5757        self.connection.emit(QtCore.SIGNAL("registerLinkages(PyQt_PyObject)"), chatUi.qobj) 
    58         self.tabWidget.addTab(widget, QtCore.QString(str(contact.jid))) 
     58        tab = self.tabWidget.addTab(widget, QtCore.QString(str(contact.jid))) 
     59        self.tabWidget.setCurrentIndex(tab) 
    5960 
    6061        
  • oss/jlib/jlib/core/contact.py

    r711 r712  
    2626        QtCore.QObject.connect(self, QtCore.SIGNAL("removeContact(PyQt_PyObject)"), 
    2727                               self.removeContact)  
     28        QtCore.QObject.connect(self, QtCore.SIGNAL("addContact(PyQt_PyObject)"), 
     29                               self.addContact)   
     30        QtCore.QObject.connect(self, QtCore.SIGNAL("addContact(PyQt_PyObject, PyQt_PyObject)"), 
     31                               self.addContactWithGroups)  
    2832 
    2933    def setComponent(self, component): 
     
    3236        self.emit(QtCore.SIGNAL("setupOneMapping(PyQt_PyObject)"), 'pushed') 
    3337        self.emit(QtCore.SIGNAL("setupOneMapping(PyQt_PyObject)"), 'remove') 
     38        self.emit(QtCore.SIGNAL("setupOneMapping(PyQt_PyObject)"), 'add') 
    3439 
    3540    def getLinkages(self): 
     
    4550        self._deliver(contact_jid, 'remove') 
    4651 
     52    def addContact(self, contact_jid): 
     53        self._deliver((contact_jid, []), 'add') 
     54         
     55    def addContactWithGroups(self, contact_jid, groups): 
     56        self._deliver((contact_jid, groups), 'add') 
     57 
    4758class jlibContactComponent(component):     
    4859    Inboxes = {"inbox"        : "headstock.api.contact.Roster instance", 
     
    5061               "pushed"       : "roster stanzas pushed by the server", 
    5162               "remove"       : "", 
     63               "add"          : "", 
    5264               "jid"          : "headstock.api.jid.JID instance received from the server", 
    5365               "ask-activity" : "request activity status to the server for each roster contact"} 
     
    93105                for nodeid in roster.items: 
    94106                    contact = roster.items[nodeid] 
    95                     if contact.subscription == 'remove'
     107                    if contact.subscription in ['remove', 'none']
    96108                        self.qobj.emit(QtCore.SIGNAL("removedContact(PyQt_PyObject)"), contact) 
    97                     elif contact.subscription == 'from'
     109                    elif contact.subscription in ['both', 'from']
    98110                        self.qobj.emit(QtCore.SIGNAL("addedContact(PyQt_PyObject)"), contact) 
    99111                    self.send(Roster(from_jid=self.from_jid, to_jid=nodeid, 
    100112                                     type=u'result', stanza_id=generate_unique()), 'result') 
     113 
     114                    if contact.subscription == 'none': 
     115                        r = Roster(from_jid=self.from_jid, to_jid=nodeid, 
     116                           type=u'set', stanza_id=generate_unique()) 
     117                        i = Item(nodeid) 
     118                        i.subscription = u'remove' 
     119                        r.items[nodeid] = i 
     120                        self.send(r, 'result') 
    101121                     
    102122            if self.dataReady('remove'): 
     
    110130                self.send(r, 'result') 
    111131 
     132            if self.dataReady('add'): 
     133                contact_jid, groups = self.recv('add') 
     134                contact_jid = str(contact_jid) 
     135                r = Roster(from_jid=self.from_jid, to_jid=contact_jid, 
     136                           type=u'set', stanza_id=generate_unique()) 
     137                i = Item(contact_jid) 
     138                i.groups = groups[:] 
     139                r.items[contact_jid] = i 
     140                self.send(r, 'result') 
     141 
    112142            if self.dataReady("inbox"): 
    113143                roster = self.recv("inbox") 
     
    138168        QtGui.QStandardItemModel.__init__(self, parent) 
    139169        self.roster = None 
     170        self.groups = {} 
    140171 
    141172    def setRoster(self, roster): 
     
    143174        for nodeid in roster.items: 
    144175            contact = roster.items[nodeid] 
    145             print contact.groups 
    146             self.addContact(contact) 
     176            self.setContact(contact) 
    147177 
    148178    def getContact(self, index): 
    149179        return self.roster.items.values()[index.row()] 
    150180 
    151     def getIndex(self, contact): 
    152         for nodeid in self.roster.items: 
    153             if str(contact.jid) == nodeid: 
    154                 return self.index(self.roster.items.keys().index(nodeid), 0) 
     181    def updateContact(self, contact): 
     182        for contact in self.roster.items: 
     183            if unicode(contact.jid) == unicode(contact_jid): 
     184                self.roster.items 
     185 
     186    def getContactByJID(self, contact_jid): 
     187        return self.roster.items.get(str(contact_jid), None) 
     188     
     189    def hasContact(self, contact_jid): 
     190        return str(contact_jid) in self.roster.items 
     191     
     192    def getContactIndex(self, contact): 
     193        if self.hasContact(contact.jid): 
     194            row = self.roster.items.keys().index(str(contact.jid)) 
     195            return self.index(row, 0) 
    155196 
    156197        return QtCore.QModelIndex() 
     198 
     199    def getGroupIndex(self, group): 
     200        if group in self.groups: 
     201            return self.groups[group] 
    157202 
    158203    def deleteContact(self, contact): 
     
    167212            self.removeRow(index.row(), index.parent()) 
    168213         
    169     def addContact(self, contact): 
    170         item = QtGui.QStandardItem() 
    171         self.appendRow(item) 
     214    def setContact(self, contact): 
     215        item = self.itemFromIndex(self.getContactIndex(contact)) 
     216        if not item: 
     217            item = QtGui.QStandardItem() 
     218            if contact.groups: 
     219                for group in contact.groups: 
     220                    groupItem = self.setGroup(group) 
     221                    groupItem.appendRow(item) 
     222            else: 
     223                self.appendRow(item) 
    172224        nodejid = str(contact.jid) 
    173225        self.setData(self.index(item.row(), 0),  
    174226                     QtCore.QVariant(QtCore.QString(nodejid))) 
    175         if nodejid not in self.roster.items: 
    176             self.roster.items[nodejid] = contact 
     227        self.roster.items[nodejid] = contact 
     228        return item 
     229 
     230    def setGroup(self, group): 
     231        index = self.getGroupIndex(group) 
     232        item = self.itemFromIndex(index) 
     233        if not item: 
     234            item = QtGui.QStandardItem(QtCore.QString(group)) 
     235            self.appendRow(item) 
     236        self.groups[group] = index 
     237        return item 
    177238 
    178239class jlibPresence(QAxonObject): 
     
    184245                               self.rejectSubscription)    
    185246        QtCore.QObject.connect(self, QtCore.SIGNAL("requestSubscription(PyQt_PyObject)"), 
    186                                self.requestSubscription)  
     247                               self.requestSubscription)  
    187248        QtCore.QObject.connect(self, QtCore.SIGNAL("requestUnsubscription(PyQt_PyObject)"), 
    188249                               self.requestUnsubscription) 
  • oss/jlib/jlib/gui/contact.py

    r711 r712  
    55from jlib.core.contact import jlibContact, jlibContactComponent, \ 
    66    jlibContactModel, jlibPresence, jlibPresenceComponent 
     7from jlib.gui.subscriptiondialog import jlibSubscriptionDialog 
    78 
    89__all__ = ['jlibContactWidget'] 
     
    5556    def addedContact(self, contact): 
    5657        model = self.contacts.model() 
    57         model.addContact(contact) 
     58        model.setContact(contact) 
    5859 
    5960    def clicked(self, index): 
     
    8384                self.contact.emit(QtCore.SIGNAL("removeContact(PyQt_PyObject)"), contact.jid) 
    8485 
     86    def subscribeToContact(self): 
     87        ui = jlibSubscriptionDialog() 
     88        ui.show() 
     89        if ui.exec_() == QtGui.QDialog.Accepted: 
     90            contact_jid = unicode(ui.contactJid.text()) 
     91            group = [] 
     92            if not ui.group.text().isEmpty(): 
     93                group.append(unicode(ui.group.text())) 
     94            self.contact.emit(QtCore.SIGNAL("addContact(PyQt_PyObject, PyQt_PyObject)"), 
     95                              contact_jid, group) 
     96            self.presence.emit(QtCore.SIGNAL("requestSubscription(PyQt_PyObject)"), contact_jid) 
     97 
    8598    def contextMenuEvent(self, event): 
    8699        menu = QtGui.QMenu(self.parent()) 
     
    88101        QtCore.QObject.connect(actionUnsub, QtCore.SIGNAL("triggered()"), self.unsubscribeFromContact) 
    89102        menu.addAction(actionUnsub) 
     103        actionSub = QtGui.QAction(QtCore.QString("Subscribe to contact..."), self.parent()) 
     104        QtCore.QObject.connect(actionSub, QtCore.SIGNAL("triggered()"), self.subscribeToContact) 
     105        menu.addAction(actionSub) 
    90106        menu.exec_(event.globalPos()) 
    91107