| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # QuteCom, a voice over Internet phone |
|---|
| 4 | # Copyright (C) 2010 Mbdsys |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | # |
|---|
| 20 | # Test program for PyCoIpManager |
|---|
| 21 | # |
|---|
| 22 | # @author Philippe Bernery |
|---|
| 23 | |
|---|
| 24 | from PyCoIpManager import * |
|---|
| 25 | |
|---|
| 26 | from PyQt4.QtCore import * |
|---|
| 27 | from PyQt4.QtGui import * |
|---|
| 28 | |
|---|
| 29 | import logging |
|---|
| 30 | import sys |
|---|
| 31 | |
|---|
| 32 | log = logging.getLogger("PyQuteCom.ContactListModel") |
|---|
| 33 | |
|---|
| 34 | class ContactListModel(QAbstractListModel): |
|---|
| 35 | """ ContactList implementation of QAbstractListModel. |
|---|
| 36 | """ |
|---|
| 37 | ContactRole = QVariant.UserType + 1 |
|---|
| 38 | |
|---|
| 39 | def __init__(self, tCoIpManager, parent=None): |
|---|
| 40 | QAbstractListModel.__init__(self, parent) |
|---|
| 41 | |
|---|
| 42 | self.__tCoIpManager = tCoIpManager |
|---|
| 43 | |
|---|
| 44 | ## Initializing presence icon |
|---|
| 45 | self.__presenceIconDict = dict() |
|---|
| 46 | self.__presenceIconDict[EnumPresenceState.PresenceStateOnline] = QPixmap(":/pics/status/online.png") |
|---|
| 47 | self.__presenceIconDict[EnumPresenceState.PresenceStateOffline] = QPixmap(":/pics/status/offline.png") |
|---|
| 48 | self.__presenceIconDict[EnumPresenceState.PresenceStateInvisible] = QPixmap(":/pics/status/invisible.png") |
|---|
| 49 | self.__presenceIconDict[EnumPresenceState.PresenceStateAway] = QPixmap(":/pics/status/away.png") |
|---|
| 50 | self.__presenceIconDict[EnumPresenceState.PresenceStateDoNotDisturb] = QPixmap(":/pics/status/donotdisturb.png") |
|---|
| 51 | self.__presenceIconDict[EnumPresenceState.PresenceStateUnknown] = QPixmap(":/pics/status/unknown.png") |
|---|
| 52 | self.__presenceIconDict[EnumPresenceState.PresenceStateUnavailable] = QPixmap(":/pics/status/unknown.png") |
|---|
| 53 | #### |
|---|
| 54 | |
|---|
| 55 | ## Registering to TCoIpManager signals |
|---|
| 56 | QObject.connect(self.__tCoIpManager.getTUserProfileManager().getTContactManager(), |
|---|
| 57 | SIGNAL("contactAddedSignal(std::string)"), |
|---|
| 58 | self.contactAddedSlot) |
|---|
| 59 | QObject.connect(self.__tCoIpManager.getTUserProfileManager().getTContactManager(), |
|---|
| 60 | SIGNAL("contactRemovedSignal(std::string)"), |
|---|
| 61 | self.contactRemovedSlot) |
|---|
| 62 | QObject.connect(self.__tCoIpManager.getTUserProfileManager().getTContactManager(), |
|---|
| 63 | SIGNAL("contactUpdatedSignal(std::string, EnumUpdateSection::UpdateSection)"), |
|---|
| 64 | self.contactUpdatedSlot) |
|---|
| 65 | #### |
|---|
| 66 | |
|---|
| 67 | ## Initializes data in model |
|---|
| 68 | self.__initialize() |
|---|
| 69 | #### |
|---|
| 70 | |
|---|
| 71 | def __initialize(self): |
|---|
| 72 | """ |
|---|
| 73 | Synchonizes data with CoIpManager. |
|---|
| 74 | """ |
|---|
| 75 | contactList = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getContactList() |
|---|
| 76 | self.__contactDict = dict() |
|---|
| 77 | for contact in contactList: |
|---|
| 78 | self.__contactDict[contact.getUUID()] = contact |
|---|
| 79 | |
|---|
| 80 | self.emit(SIGNAL("layoutChanged()")) |
|---|
| 81 | |
|---|
| 82 | def rowCount(self, parent): |
|---|
| 83 | return len(self.__contactDict) |
|---|
| 84 | |
|---|
| 85 | def data(self, index, role): |
|---|
| 86 | result = QVariant() |
|---|
| 87 | |
|---|
| 88 | if not index.isValid(): |
|---|
| 89 | return QVariant() |
|---|
| 90 | |
|---|
| 91 | if index.row() > (len(self.__contactDict) - 1): |
|---|
| 92 | return QVariant() |
|---|
| 93 | |
|---|
| 94 | contactList = self.__contactDict.items() |
|---|
| 95 | contact = contactList[index.row()][1] |
|---|
| 96 | if role == Qt.DisplayRole: |
|---|
| 97 | if len(contact.getAlias()) > 0: |
|---|
| 98 | result = QVariant(QString.fromUtf8(contact.getAlias())) |
|---|
| 99 | else: |
|---|
| 100 | result = QVariant(QString.fromUtf8(contact.getDisplayName())) |
|---|
| 101 | elif role == Qt.ToolTipRole: |
|---|
| 102 | result = QVariant(QString.fromUtf8(contact.getDisplayName())) |
|---|
| 103 | elif role == Qt.DecorationRole: |
|---|
| 104 | result = QVariant(self.__presenceIconDict[contact.getPresenceState()]) |
|---|
| 105 | elif role == ContactListModel.ContactRole: |
|---|
| 106 | result = contact |
|---|
| 107 | |
|---|
| 108 | return result |
|---|
| 109 | |
|---|
| 110 | def getContact(self, index): |
|---|
| 111 | return self.data(index, ContactListModel.ContactRole) |
|---|
| 112 | |
|---|
| 113 | def flags(self, index): |
|---|
| 114 | return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable |
|---|
| 115 | |
|---|
| 116 | def contactAddedSlot(self, contactId): |
|---|
| 117 | contactList = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getContactList() |
|---|
| 118 | self.__contactDict[contactId] = ContactListHelper.getCopyOfContact(contactList, contactId) |
|---|
| 119 | |
|---|
| 120 | self.emit(SIGNAL("layoutChanged()")) |
|---|
| 121 | |
|---|
| 122 | def contactRemovedSlot(self, contactId): |
|---|
| 123 | del self.__contactDict[contactId] |
|---|
| 124 | |
|---|
| 125 | self.emit(SIGNAL("layoutChanged()")) |
|---|
| 126 | |
|---|
| 127 | def contactUpdatedSlot(self, contactId, section): |
|---|
| 128 | contactList = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getContactList() |
|---|
| 129 | self.__contactDict[contactId] = ContactListHelper.getCopyOfContact(contactList, contactId) |
|---|
| 130 | |
|---|
| 131 | self.emit(SIGNAL("layoutChanged()")) |
|---|