source: qutecom-coip/pywengophone/src/account/AccountListManagerWidget.py @ 125:d648f4cb122f

Last change on this file since 125:d648f4cb122f was 125:d648f4cb122f, checked in by laurent, 3 years ago

wengo => qutecom

File size: 6.8 KB
Line 
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 Ovidiu Ciule
23# @author Philippe Bernery
24
25from AccountListManagerWidget_ui import Ui_AccountListManagerWidget
26from EditAccountDialog import EditAccountDialog
27from NewAccountDialog import NewAccountDialog
28from config import Config
29
30from util.QtUtil import *
31from util.SafeConnect import *
32
33from PyCoIpManager import *
34
35from PyQt4.QtCore import *
36from PyQt4.QtGui import *
37
38import logging, os, sys
39
40log = logging.getLogger("PyQuteCom.AccountListManagerWidget")
41
42class AccountListManagerWidget(QDialog):
43    def __init__(self, parent, tCoIpManager):
44        """
45        Pre-condition: tCoIpManager must be initialized, otherwise Config, UserProfile would not be useful
46        """
47        # Init GUI     
48        QDialog.__init__(self, parent)
49        self._ui = Ui_AccountListManagerWidget()
50        self._ui.setupUi(self)
51
52        connect(self._ui.closeButton, SIGNAL("clicked()"), self.close)
53        self.__tCoIpManager = tCoIpManager
54
55        connect(self._ui.addButton, SIGNAL("clicked()"), self.addButtonClicked)
56        connect(self._ui.modifyButton, SIGNAL("clicked()"), self.modifyButtonClicked)
57        connect(self._ui.deleteButton, SIGNAL("clicked()"), self.deleteButtonClicked)
58        connect(self._ui.treeWidget, SIGNAL(" itemActivated (QTreeWidgetItem *,int)"), self.itemActivated)
59        connect(self._ui.treeWidget, SIGNAL(" itemChanged (QTreeWidgetItem *,int)"), self.itemChangedSlot)
60       
61        self.loadAccountList()
62 
63    def addButtonClicked(self):
64        """
65        Adds a new account
66        """
67        # Show NewAccountDialog
68        nad = NewAccountDialog(self)
69        # If Yes
70        if nad.exec_() == QDialog.Accepted:
71            account = nad.getAccount()
72            # Add the account
73            self.__tCoIpManager.getTUserProfileManager().getTAccountManager().add(account)
74            # check if account really was added; otherwise -> crash when trying to connect it
75            # Account will not be added if duplicate, or if it has incorrect data
76            accList = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getAccountList()
77            if AccountListHelper.getCopyOfAccount(accList, account.getLogin(), account.getAccountType())!=None:
78                # And connect it
79                self.__tCoIpManager.getTConnectManager().connect(account.getUUID())
80        self.loadAccountList()
81
82    def loadAccountList(self):
83        """
84        Refreshes the account list
85        """
86        self._ui.treeWidget.clear()
87        for account in self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getAccountList():
88            sl = QStringList("")
89            sl.append(EnumAccountType.toString(account.getAccountType()))
90            sl.append(account.getLogin())
91            sl.append(account.getUUID())
92            qTWI = QTreeWidgetItem(sl)
93            qTWI.setFlags(qTWI.flags() | Qt.ItemIsUserCheckable)
94            # Is the account enabled? Should we check the Enable checkBox?
95            enabled = (account.getConnectionState()!=EnumConnectionState.ConnectionStateDisconnected)
96            qTWI.setCheckState(0, boolToQtCheckState(enabled))
97            self._ui.treeWidget.addTopLevelItem(qTWI)
98
99    def deleteButtonClicked(self):
100        """
101        Deletes the currently selected account
102        """
103        account = self.getSelectedAccount()
104        if account == None:
105            return
106        self.__tCoIpManager.getTConnectManager().disconnect(account.getUUID())
107        self.__tCoIpManager.getTUserProfileManager().getTAccountManager().remove(account.getUUID())
108        log.info("Deleted: "+account.getUUID())
109        self.loadAccountList()
110
111    def modifyButtonClicked(self):
112        """
113        Edits an account
114        """
115        account = self.getSelectedAccount()
116        if account == None:
117            return
118        # Show EditAccountDialog
119        ead = EditAccountDialog(self, account)
120        # If Yes
121        if ead.exec_() == QDialog.Accepted:
122            outAccount = ead.getAccount()
123            # TODO: update can be given EnumUpdateSection, which optimizes the update
124            self.__tCoIpManager.getTUserProfileManager().getTAccountManager().update(outAccount)
125            #self.__tCoIpManager.getTConnectManager().disconnect(outAccount.getUUID())
126            #self.__tCoIpManager.getTConnectManager().connect(outAccount.getUUID())
127        self.loadAccountList()
128
129    def getSelectedAccount(self):
130        """
131        Returns the account corresponding to the currently selected item
132
133        Pre-condition:
134        Account items must be in the same order as the AccountList returned by UserProfile.getAccountList()
135        """
136        # TODO fix it so that it works regardless of the order
137        accountItem = self._ui.treeWidget.currentItem()
138        if accountItem == None:
139             return None
140        ixAccount = self._ui.treeWidget.indexOfTopLevelItem(accountItem)
141        accounts = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getAccountList()
142        return accounts[ixAccount]
143
144    def itemActivated(self, item, col):
145        """
146        Opens the double-clicked account for editing
147        """
148        self.modifyButtonClicked()
149
150    def itemChangedSlot(self, item, col):
151        """
152        Listens to enableCheckBox changes, and connects/disconnects the acount
153        """
154        if col==0:
155            enabled = qtCheckStateToBool(item.checkState(0))
156            account = self.getSelectedAccount()
157            if account==None: return
158            if enabled:
159                log.debug("itemChanged: Account: "+str(account.getLogin())+str(account.getUUID())+"/" \
160                    +str(EnumAccountType.toString(account.getAccountType())) \
161                    +" goes online")
162                self.__tCoIpManager.getTConnectManager().connect(account.getUUID())
163            else:
164                log.debug("itemChanged: Account: "+str(account.getLogin())+str(account.getUUID())+"/" \
165                    +str(EnumAccountType.toString(account.getAccountType())) \
166                    +" goes offline")
167                self.__tCoIpManager.getTConnectManager().disconnect(account.getUUID())
168           
Note: See TracBrowser for help on using the repository browser.