| 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 | |
|---|
| 25 | from config import Config |
|---|
| 26 | from NewProfileDialog import NewProfileDialog |
|---|
| 27 | from UserProfileListManagerWidget_ui import Ui_UserProfileListManagerWidget |
|---|
| 28 | |
|---|
| 29 | from util.SafeConnect import * |
|---|
| 30 | |
|---|
| 31 | from PyCoIpManager import * |
|---|
| 32 | |
|---|
| 33 | from PyQt4.QtCore import * |
|---|
| 34 | from PyQt4.QtGui import * |
|---|
| 35 | |
|---|
| 36 | import logging, os, sys |
|---|
| 37 | |
|---|
| 38 | log = logging.getLogger("PyQuteCom.UserProfileListManagerWidget") |
|---|
| 39 | |
|---|
| 40 | class UserProfileListManagerWidget(QDialog): |
|---|
| 41 | def __init__(self, parent, tCoIpManager): |
|---|
| 42 | """ |
|---|
| 43 | Pre-condition: tCoIpManager must be initialized, otherwise Config would not be useful |
|---|
| 44 | """ |
|---|
| 45 | self.__tCoIpManager = tCoIpManager |
|---|
| 46 | |
|---|
| 47 | # Init GUI |
|---|
| 48 | QDialog.__init__(self, parent) |
|---|
| 49 | self._ui = Ui_UserProfileListManagerWidget() |
|---|
| 50 | self._ui.setupUi(self) |
|---|
| 51 | self.setAttribute(Qt.WA_DeleteOnClose) |
|---|
| 52 | |
|---|
| 53 | connect(self._ui.addButton, SIGNAL("clicked()"), self.addButtonClicked) |
|---|
| 54 | connect(self._ui.loadButton, SIGNAL("clicked()"), self.loadButtonClicked) |
|---|
| 55 | connect(self._ui.modifyButton, SIGNAL("clicked()"), self.editButtonClicked) |
|---|
| 56 | connect(self._ui.closeButton, SIGNAL("clicked()"), self.close) |
|---|
| 57 | connect(self.__tCoIpManager.getTUserProfileManager(), |
|---|
| 58 | SIGNAL("userProfileSetSignal(UserProfile)"), |
|---|
| 59 | self.userProfileSetUpdatedSlot) |
|---|
| 60 | connect(self.__tCoIpManager.getTUserProfileManager(), |
|---|
| 61 | SIGNAL("userProfileUpdatedSignal(UserProfile)"), |
|---|
| 62 | self.userProfileSetUpdatedSlot) |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | self._ui.deleteButton.setEnabled(False) |
|---|
| 66 | self.loadUserProfileList() |
|---|
| 67 | |
|---|
| 68 | def addButtonClicked(self): |
|---|
| 69 | # Show NewProfileDialog |
|---|
| 70 | npw = NewProfileDialog(self) |
|---|
| 71 | if npw.exec_() == QDialog.Accepted: |
|---|
| 72 | up = UserProfile() |
|---|
| 73 | up.setName(str(npw.getUserProfileName())) |
|---|
| 74 | self.__tCoIpManager.getTUserProfileManager().setUserProfile(up) |
|---|
| 75 | log.info("Profile: " + up.getName() + " id: " + up.getUUID()) |
|---|
| 76 | |
|---|
| 77 | def loadUserProfileList(self): |
|---|
| 78 | """ |
|---|
| 79 | Loads the list of existing profiles into the Widget |
|---|
| 80 | For each profiles, also appends its Accounts as children |
|---|
| 81 | Pre-condition: a current UP is already set |
|---|
| 82 | """ |
|---|
| 83 | self._ui.treeWidget.clear() |
|---|
| 84 | |
|---|
| 85 | fileStorage = UserProfileFileStorage(Config.getInstance().configurationFolder) |
|---|
| 86 | idList = fileStorage.getUserProfileIds() |
|---|
| 87 | currentUP = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile() |
|---|
| 88 | currentId = currentUP.getUUID() |
|---|
| 89 | ## Is the current UP among the stored profiles? If not, add it; this could happen if the |
|---|
| 90 | ## current UP is newly created, and has not been saved yet |
|---|
| 91 | if not currentId in idList: |
|---|
| 92 | idList.append(currentId) |
|---|
| 93 | for id in idList: |
|---|
| 94 | sl = QStringList() |
|---|
| 95 | up = None |
|---|
| 96 | if currentId==id: |
|---|
| 97 | sl.append("*") |
|---|
| 98 | up = currentUP |
|---|
| 99 | profileName = up.getName() |
|---|
| 100 | else: |
|---|
| 101 | sl.append("") |
|---|
| 102 | up = UserProfile() |
|---|
| 103 | fileStorage.load(id, up) |
|---|
| 104 | profileName = fileStorage.getNameOfUserProfile(id) |
|---|
| 105 | if profileName==id or len(profileName)==0: |
|---|
| 106 | sl.append("<No Name>") |
|---|
| 107 | else: |
|---|
| 108 | sl.append(fileStorage.getNameOfUserProfile(id)) |
|---|
| 109 | sl.append(id) |
|---|
| 110 | qTWI = QTreeWidgetItem(sl) |
|---|
| 111 | self._ui.treeWidget.addTopLevelItem(qTWI) |
|---|
| 112 | # Append the accounts |
|---|
| 113 | for account in up.getAccountList(): |
|---|
| 114 | sl = QStringList("") |
|---|
| 115 | login = account.getLogin() |
|---|
| 116 | if len(login)==0: |
|---|
| 117 | sl.append("<No Name>") |
|---|
| 118 | else: |
|---|
| 119 | sl.append(login) |
|---|
| 120 | sl.append(account.getUUID()) |
|---|
| 121 | qTWI.addChild(QTreeWidgetItem(sl)) |
|---|
| 122 | |
|---|
| 123 | def loadButtonClicked(self): |
|---|
| 124 | profileId = self.getSelectedUserProfileId() |
|---|
| 125 | userProfile = UserProfile() |
|---|
| 126 | fileStorage = UserProfileFileStorage(Config.getInstance().configurationFolder) |
|---|
| 127 | fileStorage.load(profileId, userProfile) |
|---|
| 128 | self.__tCoIpManager.getTUserProfileManager().setUserProfile(userProfile) |
|---|
| 129 | log.info("Loaded profile: " +userProfile.getUUID()) |
|---|
| 130 | |
|---|
| 131 | def editButtonClicked(self): |
|---|
| 132 | profileId = self.getSelectedUserProfileId() |
|---|
| 133 | fileStorage = UserProfileFileStorage(Config.getInstance().configurationFolder) |
|---|
| 134 | # Check if the profile to be modified is the current profile |
|---|
| 135 | if profileId == self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getUUID(): |
|---|
| 136 | # If yes, just get it |
|---|
| 137 | userProfile = self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile() |
|---|
| 138 | else: |
|---|
| 139 | # If no, load it |
|---|
| 140 | userProfile = UserProfile() |
|---|
| 141 | fileStorage.load(profileId, userProfile) |
|---|
| 142 | log.info("Modify profile: "+ userProfile.getUUID()) |
|---|
| 143 | |
|---|
| 144 | # Show NewProfileDialog, we'll use it as an 'edit profile' though |
|---|
| 145 | npw = NewProfileDialog(self) |
|---|
| 146 | npw.setWindowTitle("Edit User Profile") |
|---|
| 147 | npw._ui.profileNameLineEdit.setText(userProfile.getName()) |
|---|
| 148 | if npw.exec_() == QDialog.Accepted: |
|---|
| 149 | # Here we really modify the profile |
|---|
| 150 | userProfile.setName(str(npw.getUserProfileName())) |
|---|
| 151 | # Save the modified profile |
|---|
| 152 | # Check if the profile to be modified is the current profile |
|---|
| 153 | if userProfile.getUUID() == self.__tCoIpManager.getTUserProfileManager().getCopyOfUserProfile().getUUID(): |
|---|
| 154 | self.__tCoIpManager.getTUserProfileManager().updateUserProfile(userProfile) |
|---|
| 155 | else: |
|---|
| 156 | fileStorage.save(userProfile) |
|---|
| 157 | self.loadUserProfileList() |
|---|
| 158 | |
|---|
| 159 | def getSelectedUserProfileId(self): |
|---|
| 160 | """ |
|---|
| 161 | Returns the profile id corresponding to the currently selected item |
|---|
| 162 | |
|---|
| 163 | Pre-condition: |
|---|
| 164 | Profile items must be in the same order as the AccountList returned by UserProfileFileStorage.getUserProfileIds() |
|---|
| 165 | """ |
|---|
| 166 | # TODO fix it so that it works regardless of the order |
|---|
| 167 | profileItem = self._ui.treeWidget.currentItem() |
|---|
| 168 | ixProfile = self._ui.treeWidget.indexOfTopLevelItem(profileItem) |
|---|
| 169 | fileStorage = UserProfileFileStorage(Config.getInstance().configurationFolder) |
|---|
| 170 | profileId = fileStorage.getUserProfileIds()[ixProfile] |
|---|
| 171 | return profileId |
|---|
| 172 | |
|---|
| 173 | def userProfileSetUpdatedSlot(self, userProfile): |
|---|
| 174 | self.loadUserProfileList() |
|---|