| 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 PyCoIpManager import * |
|---|
| 26 | |
|---|
| 27 | from PyQt4 import QtCore |
|---|
| 28 | |
|---|
| 29 | from util.SafeConnect import * |
|---|
| 30 | |
|---|
| 31 | from ConfigParser import SafeConfigParser |
|---|
| 32 | import os, platform, sys, logging |
|---|
| 33 | |
|---|
| 34 | log = logging.getLogger("PyQuteCom.Config") |
|---|
| 35 | |
|---|
| 36 | GENERAL_SECTION = "general" |
|---|
| 37 | LASTUSEDUSERPROFILE_OPTION = "lastuseduserprofile" |
|---|
| 38 | FILETRANSFER_SECTION = "filetransfer" |
|---|
| 39 | FILETRANSFERDOWNLOADFOLDER_OPTION = "filetransferdownloadfolder" |
|---|
| 40 | FILETRANSFERLASTUPLOADEDFILEFOLDER_OPTION = "filetransferlastuploadedfilefolder" |
|---|
| 41 | |
|---|
| 42 | class Config: |
|---|
| 43 | __configFilename = "PyQuteCom.conf" |
|---|
| 44 | |
|---|
| 45 | __currentInstance = None |
|---|
| 46 | def getInstance(): |
|---|
| 47 | if Config.__currentInstance == None: |
|---|
| 48 | Config.__currentInstance = Config() |
|---|
| 49 | return Config.__currentInstance |
|---|
| 50 | getInstance = staticmethod(getInstance) |
|---|
| 51 | |
|---|
| 52 | __slots__ = [ |
|---|
| 53 | "lastUsedUserProfile", |
|---|
| 54 | "resourcesFolder", |
|---|
| 55 | "fileTransferDownloadFolder", |
|---|
| 56 | "fileTransferLastUploadedFileFolder", |
|---|
| 57 | "configurationFolder", |
|---|
| 58 | "configurationFilename", |
|---|
| 59 | "companyWebSiteUrl" |
|---|
| 60 | ] |
|---|
| 61 | |
|---|
| 62 | def __init__(self): |
|---|
| 63 | if Config.__currentInstance != None: |
|---|
| 64 | # Should not get here, constructor should not be called directly |
|---|
| 65 | raise RuntimeError, "Constructor has been called twice. Probably \ |
|---|
| 66 | it has been called directly.\n This is a Singleton. Please \ |
|---|
| 67 | use getInstance()" |
|---|
| 68 | return |
|---|
| 69 | |
|---|
| 70 | self.__setDefaultValues() |
|---|
| 71 | self.load() |
|---|
| 72 | |
|---|
| 73 | def __getConfigurationFolder(self): |
|---|
| 74 | """ |
|---|
| 75 | Gets the configuration directory path. |
|---|
| 76 | It is usually: |
|---|
| 77 | On Windows: C:\\Documents and Settings\\username\\Application Data\\PyQuteCom\\ |
|---|
| 78 | On Mac OS X: $HOME/Library/Application Support/PyQuteCom/ |
|---|
| 79 | On Linux: $HOME/.pyqutecom |
|---|
| 80 | """ |
|---|
| 81 | dirName = ".pyqutecom" # For Linux |
|---|
| 82 | if (platform.system() == "Darwin") or (platform.system() == "Windows"): |
|---|
| 83 | dirName = "PyQuteCom" |
|---|
| 84 | return os.path.join(Path.getConfigurationDirPath(), dirName + str(os.path.sep)) |
|---|
| 85 | |
|---|
| 86 | def __getConfigurationFilename(self): |
|---|
| 87 | return os.path.join(self.configurationFolder, self.__configFilename) |
|---|
| 88 | |
|---|
| 89 | def __setDefaultValues(self): |
|---|
| 90 | """ |
|---|
| 91 | Sets default values on config. members. |
|---|
| 92 | """ |
|---|
| 93 | self.companyWebSiteUrl = "" |
|---|
| 94 | self.lastUsedUserProfile = "" |
|---|
| 95 | self.__setResourcesFolder() |
|---|
| 96 | self.fileTransferDownloadFolder = "" |
|---|
| 97 | self.fileTransferLastUploadedFileFolder = "" |
|---|
| 98 | |
|---|
| 99 | configurationFolder = property(__getConfigurationFolder) |
|---|
| 100 | configurationFilename = property(__getConfigurationFilename) |
|---|
| 101 | |
|---|
| 102 | def load(self): |
|---|
| 103 | """ |
|---|
| 104 | Load last config. file. |
|---|
| 105 | """ |
|---|
| 106 | try: |
|---|
| 107 | config = SafeConfigParser() |
|---|
| 108 | config.read([self.configurationFilename]) |
|---|
| 109 | if config.has_section(GENERAL_SECTION): |
|---|
| 110 | if config.has_option(GENERAL_SECTION, LASTUSEDUSERPROFILE_OPTION): |
|---|
| 111 | self.lastUsedUserProfile = config.get(GENERAL_SECTION, LASTUSEDUSERPROFILE_OPTION) |
|---|
| 112 | if config.has_section(FILETRANSFER_SECTION): |
|---|
| 113 | if config.has_option(FILETRANSFER_SECTION, FILETRANSFERDOWNLOADFOLDER_OPTION): |
|---|
| 114 | self.fileTransferDownloadFolder = config.get(FILETRANSFER_SECTION, FILETRANSFERDOWNLOADFOLDER_OPTION) |
|---|
| 115 | if config.has_option(FILETRANSFER_SECTION, FILETRANSFERLASTUPLOADEDFILEFOLDER_OPTION): |
|---|
| 116 | self.fileTransferLastUploadedFileFolder = config.get(FILETRANSFER_SECTION, FILETRANSFERLASTUPLOADEDFILEFOLDER_OPTION) |
|---|
| 117 | except IOError: |
|---|
| 118 | pass |
|---|
| 119 | |
|---|
| 120 | def save(self): |
|---|
| 121 | """ |
|---|
| 122 | Saves current config. file. |
|---|
| 123 | """ |
|---|
| 124 | if not os.path.exists(self.configurationFolder): |
|---|
| 125 | os.mkdir(self.configurationFolder) |
|---|
| 126 | |
|---|
| 127 | config = SafeConfigParser() |
|---|
| 128 | config.add_section(GENERAL_SECTION) |
|---|
| 129 | config.set(GENERAL_SECTION, LASTUSEDUSERPROFILE_OPTION, self.lastUsedUserProfile) |
|---|
| 130 | config.add_section(FILETRANSFER_SECTION) |
|---|
| 131 | config.set(FILETRANSFER_SECTION, FILETRANSFERDOWNLOADFOLDER_OPTION, self.fileTransferDownloadFolder) |
|---|
| 132 | config.set(FILETRANSFER_SECTION, FILETRANSFERLASTUPLOADEDFILEFOLDER_OPTION, self.fileTransferLastUploadedFileFolder) |
|---|
| 133 | config.write(open(self.configurationFilename, "w")) |
|---|
| 134 | |
|---|
| 135 | def __setResourcesFolder(self): |
|---|
| 136 | """ |
|---|
| 137 | Retrieves the Resources directory path. |
|---|
| 138 | It is usually: |
|---|
| 139 | On Windows: C:\\Program Files\\QuteCom\\ |
|---|
| 140 | On Mac OS X: $APPLICATIONS/QuteCom.app/Contents/Resources |
|---|
| 141 | On Linux: $PREFIX/share/qutecom; don't ask what that prefix is |
|---|
| 142 | This resources path can also be set on PyQuteCom command line with |
|---|
| 143 | the --resources= option. |
|---|
| 144 | """ |
|---|
| 145 | if platform.system() == "Windows": |
|---|
| 146 | log.debug("__setResourcesFolder: platform = Windows") |
|---|
| 147 | self.resourcesFolder = Path.getApplicationDirPath() |
|---|
| 148 | elif platform.system() == "Darwin": |
|---|
| 149 | log.debug("__setResourcesFolder: platform = Darwin") |
|---|
| 150 | self.resourcesFolder = Path.getApplicationResourcesDirPath() |
|---|
| 151 | elif platform.system() == "Linux": |
|---|
| 152 | log.debug("__setResourcesFolder: platform = Linux") |
|---|
| 153 | self.resourcesFolder = "/usr/local/share/qutecom" |
|---|
| 154 | else: |
|---|
| 155 | log.debug("getResourcesDirPath: platform detection failed; \ |
|---|
| 156 | propably this platform is not supported; platform.system()=" + platform.system) |
|---|
| 157 | |
|---|
| 158 | # Check if dir exist |
|---|
| 159 | # If not, resourcesFolder is set to sys.path[0] |
|---|
| 160 | if not os.path.exists(self.resourcesFolder): |
|---|
| 161 | log.debug("__setResourcesFolder: path \"" + self.resourcesFolder + "\" does not exist, using sys.path[0]") |
|---|
| 162 | self.resourcesFolder = sys.path[0] |
|---|
| 163 | log.debug("__setResourcesFolder: path=\"" + self.resourcesFolder + "\"") |
|---|