| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <coipmanager/CoIpManagerConfig.h> |
|---|
| 21 | |
|---|
| 22 | #include <networkdiscovery/NetworkProxyDiscovery.h> |
|---|
| 23 | |
|---|
| 24 | #include <sound/AudioDeviceManager.h> |
|---|
| 25 | |
|---|
| 26 | #include <webcam/WebcamDriver.h> |
|---|
| 27 | |
|---|
| 28 | #include <sipwrapper/EnumVideoQuality.h> |
|---|
| 29 | |
|---|
| 30 | #include <util/File.h> |
|---|
| 31 | #include <util/Path.h> |
|---|
| 32 | |
|---|
| 33 | #include <cstdlib> |
|---|
| 34 | |
|---|
| 35 | const std::string CoIpManagerConfig::CONFIG_PATH_KEY = "path.profiles"; |
|---|
| 36 | const std::string CoIpManagerConfig::COIP_PLUGINS_PATH_KEY = "path.coip-plugins"; |
|---|
| 37 | const std::string CoIpManagerConfig::AUTO_SAVE_KEY = "autosave"; |
|---|
| 38 | const std::string CoIpManagerConfig::HTTP_PROXY_AUTO_DETECT_KEY = "httpproxy.autodetect"; |
|---|
| 39 | const std::string CoIpManagerConfig::HTTP_PROXY_SERVER_KEY = "httpproxy.server"; |
|---|
| 40 | const std::string CoIpManagerConfig::HTTP_PROXY_SERVER_PORT_KEY = "httpproxy.port"; |
|---|
| 41 | const std::string CoIpManagerConfig::HTTP_PROXY_LOGIN_KEY = "httpproxy.login"; |
|---|
| 42 | const std::string CoIpManagerConfig::HTTP_PROXY_PASSWORD_KEY = "httpproxy.password"; |
|---|
| 43 | const std::string CoIpManagerConfig::HTTP_PROXY_AUTH_TYPE_KEY = "httpproxy.authtype"; |
|---|
| 44 | |
|---|
| 45 | const std::string CoIpManagerConfig::AUDIO_OUTPUT_DEVICEID_KEY = "audio.output.deviceid"; |
|---|
| 46 | const std::string CoIpManagerConfig::AUDIO_INPUT_DEVICEID_KEY = "audio.input.deviceid"; |
|---|
| 47 | const std::string CoIpManagerConfig::AUDIO_RINGER_DEVICEID_KEY = "audio.ringer.deviceid"; |
|---|
| 48 | |
|---|
| 49 | const std::string CoIpManagerConfig::VIDEO_WEBCAM_DEVICE_KEY = "video.webcam.device"; |
|---|
| 50 | const std::string CoIpManagerConfig::VIDEO_QUALITY_KEY = "video.quality"; |
|---|
| 51 | |
|---|
| 52 | #define stringize2(x) #x |
|---|
| 53 | #define stringize(x) stringize2(x) |
|---|
| 54 | |
|---|
| 55 | #define ToVar toVar |
|---|
| 56 | |
|---|
| 57 | CoIpManagerConfig::CoIpManagerConfig() { |
|---|
| 58 | _keyDefaultValueMap[CONFIG_PATH_KEY] = ToVar(Path::getHomeDirPath()); |
|---|
| 59 | |
|---|
| 60 | StringList coipPluginsPaths; |
|---|
| 61 | |
|---|
| 62 | //Get the COIPMANAGER_PLUGIN_PATH |
|---|
| 63 | char * envPath = getenv("COIPMANAGER_PLUGIN_PATH"); |
|---|
| 64 | if (envPath) { |
|---|
| 65 | coipPluginsPaths += File::normalize(std::string(envPath), true); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | #if defined(OS_MACOSX) |
|---|
| 69 | coipPluginsPaths += Path::getBundlePathFromId("com.qutecom.CoIpManager") + "Libraries/"; |
|---|
| 70 | #elif defined(OS_LINUX) |
|---|
| 71 | coipPluginsPaths += Path::getApplicationDirPath(); |
|---|
| 72 | coipPluginsPaths += "/usr/local/CoIpManager-0.1/lib/"; |
|---|
| 73 | coipPluginsPaths += stringize(COIPMANAGER_PLUGIN_PATH); //Defined in CMakeLists.txt |
|---|
| 74 | #else |
|---|
| 75 | coipPluginsPaths += Path::getApplicationDirPath(); |
|---|
| 76 | coipPluginsPaths += "C:/CoIpManager-0.1/lib/"; |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | _keyDefaultValueMap[COIP_PLUGINS_PATH_KEY] = ToVar(coipPluginsPaths); |
|---|
| 80 | |
|---|
| 81 | _keyDefaultValueMap[AUTO_SAVE_KEY] = true; |
|---|
| 82 | |
|---|
| 83 | _keyDefaultValueMap[HTTP_PROXY_AUTO_DETECT_KEY] = true; |
|---|
| 84 | _keyDefaultValueMap[HTTP_PROXY_SERVER_KEY] = ""; |
|---|
| 85 | _keyDefaultValueMap[HTTP_PROXY_SERVER_PORT_KEY] = 0; |
|---|
| 86 | _keyDefaultValueMap[HTTP_PROXY_LOGIN_KEY] = ""; |
|---|
| 87 | _keyDefaultValueMap[HTTP_PROXY_PASSWORD_KEY] = ""; |
|---|
| 88 | _keyDefaultValueMap[HTTP_PROXY_AUTH_TYPE_KEY] = ToVar(EnumProxyAuthType::toString(EnumProxyAuthType::ProxyAuthTypeUnknown)); |
|---|
| 89 | |
|---|
| 90 | _keyDefaultValueMap[AUDIO_OUTPUT_DEVICEID_KEY] = ToVar(AudioDeviceManager::getInstance().getDefaultOutputDevice().getData()); |
|---|
| 91 | _keyDefaultValueMap[AUDIO_INPUT_DEVICEID_KEY] = ToVar(AudioDeviceManager::getInstance().getDefaultInputDevice().getData()); |
|---|
| 92 | _keyDefaultValueMap[AUDIO_RINGER_DEVICEID_KEY] = ToVar(AudioDeviceManager::getInstance().getDefaultOutputDevice().getData()); |
|---|
| 93 | |
|---|
| 94 | _keyDefaultValueMap[VIDEO_WEBCAM_DEVICE_KEY] = ToVar(WebcamDriver::getInstance()->getDefaultDevice()); |
|---|
| 95 | _keyDefaultValueMap[VIDEO_QUALITY_KEY] = ToVar(EnumVideoQuality::toString(EnumVideoQuality::VideoQualityNormal)); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | CoIpManagerConfig::~CoIpManagerConfig() { |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | std::string CoIpManagerConfig::getConfigPath() const { |
|---|
| 102 | return getStringKeyValue(CONFIG_PATH_KEY); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void CoIpManagerConfig::setConfigPath(const std::string & path) { |
|---|
| 106 | set(CONFIG_PATH_KEY, path); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | StringList CoIpManagerConfig::getCoIpPluginsPath() const { |
|---|
| 110 | return getStringListKeyValue(COIP_PLUGINS_PATH_KEY); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void CoIpManagerConfig::addCoIpPluginsPath(const std::string & path) { |
|---|
| 114 | StringList list = getCoIpPluginsPath(); |
|---|
| 115 | list.push_back(File::normalize(path)); |
|---|
| 116 | set(COIP_PLUGINS_PATH_KEY, list); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | bool CoIpManagerConfig::autoSave() const { |
|---|
| 120 | return getBooleanKeyValue(AUTO_SAVE_KEY); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | bool CoIpManagerConfig::autoDetectHttpProxy() const { |
|---|
| 124 | return getBooleanKeyValue(HTTP_PROXY_AUTO_DETECT_KEY); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | std::string CoIpManagerConfig::getHttpProxyServer() const { |
|---|
| 128 | return getStringKeyValue(HTTP_PROXY_SERVER_KEY); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | int CoIpManagerConfig::getHttpProxyServerPort() const { |
|---|
| 132 | return getIntegerKeyValue(HTTP_PROXY_SERVER_PORT_KEY); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | std::string CoIpManagerConfig::getHttpProxyLogin() const { |
|---|
| 136 | return getStringKeyValue(HTTP_PROXY_LOGIN_KEY); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | std::string CoIpManagerConfig::getHttpProxyPassword() const { |
|---|
| 140 | return getStringKeyValue(HTTP_PROXY_PASSWORD_KEY); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | EnumProxyAuthType::ProxyAuthType CoIpManagerConfig::getHttpProxyAuthType() const { |
|---|
| 144 | return EnumProxyAuthType::toProxyAuthType(getStringKeyValue(HTTP_PROXY_AUTH_TYPE_KEY)); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Code factorization. |
|---|
| 149 | * |
|---|
| 150 | * @see Config::getAudioRingerDeviceId() |
|---|
| 151 | * @see Config::getAudioInputDeviceId() |
|---|
| 152 | * @see Config::getAudioOutputDeviceId() |
|---|
| 153 | * @param storedDeviceData the stored audio device |
|---|
| 154 | * @param currentdeviceList the actual audio devices list |
|---|
| 155 | * @param currentDefaultDevice the actual default device |
|---|
| 156 | */ |
|---|
| 157 | static StringList getProperAudioDeviceId( |
|---|
| 158 | const StringList &storedDeviceData, |
|---|
| 159 | const std::list<AudioDevice> ¤tdeviceList, |
|---|
| 160 | const StringList ¤tDefaultDevice) { |
|---|
| 161 | |
|---|
| 162 | StringList result; |
|---|
| 163 | |
|---|
| 164 | // if no device stored use the default one |
|---|
| 165 | if (storedDeviceData.empty()) { |
|---|
| 166 | result = currentDefaultDevice; |
|---|
| 167 | |
|---|
| 168 | // else check if the stored device is currently available |
|---|
| 169 | // do not exec this check under Linux because when devices |
|---|
| 170 | // are busy they are not in currentdeviceList |
|---|
| 171 | } else { |
|---|
| 172 | #ifndef OS_LINUX |
|---|
| 173 | bool found = false; |
|---|
| 174 | |
|---|
| 175 | std::list<AudioDevice>::const_iterator it; |
|---|
| 176 | for (it = currentdeviceList.begin(); it != currentdeviceList.end(); ++it) { |
|---|
| 177 | |
|---|
| 178 | if ((*it).getData() == storedDeviceData) { |
|---|
| 179 | found = true; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | // if the stored device has been found we use it |
|---|
| 184 | if (found) { |
|---|
| 185 | result = storedDeviceData; |
|---|
| 186 | } else { |
|---|
| 187 | result = currentDefaultDevice; |
|---|
| 188 | } |
|---|
| 189 | #else |
|---|
| 190 | result = storedDeviceData; |
|---|
| 191 | #endif |
|---|
| 192 | } |
|---|
| 193 | return result; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | StringList CoIpManagerConfig::getAudioOutputDeviceId() const { |
|---|
| 197 | return getProperAudioDeviceId( |
|---|
| 198 | getStringListKeyValue(AUDIO_OUTPUT_DEVICEID_KEY), |
|---|
| 199 | AudioDeviceManager::getInstance().getOutputDeviceList(), |
|---|
| 200 | AudioDeviceManager::getInstance().getDefaultOutputDevice().getData() |
|---|
| 201 | ); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | StringList CoIpManagerConfig::getAudioInputDeviceId() const { |
|---|
| 205 | return getProperAudioDeviceId( |
|---|
| 206 | getStringListKeyValue(AUDIO_INPUT_DEVICEID_KEY), |
|---|
| 207 | AudioDeviceManager::getInstance().getInputDeviceList(), |
|---|
| 208 | AudioDeviceManager::getInstance().getDefaultInputDevice().getData() |
|---|
| 209 | ); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | StringList CoIpManagerConfig::getAudioRingerDeviceId() const { |
|---|
| 213 | return getProperAudioDeviceId( |
|---|
| 214 | getStringListKeyValue(AUDIO_RINGER_DEVICEID_KEY), |
|---|
| 215 | AudioDeviceManager::getInstance().getOutputDeviceList(), |
|---|
| 216 | AudioDeviceManager::getInstance().getDefaultOutputDevice().getData() |
|---|
| 217 | ); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | std::string CoIpManagerConfig::getVideoWebcamDevice() const { |
|---|
| 221 | WebcamDriver *webcam = WebcamDriver::getInstance(); |
|---|
| 222 | |
|---|
| 223 | std::string deviceName = getStringKeyValue(VIDEO_WEBCAM_DEVICE_KEY); |
|---|
| 224 | |
|---|
| 225 | //FIXME see fixme DirectXWebcamDriver.h |
|---|
| 226 | //string defaultDevice = webcam->getDefaultDevice(); |
|---|
| 227 | std::string defaultDevice = deviceName; |
|---|
| 228 | |
|---|
| 229 | if (defaultDevice == WEBCAM_NULL) { |
|---|
| 230 | defaultDevice = ""; |
|---|
| 231 | } |
|---|
| 232 | StringList deviceList = webcam->getDeviceList(); |
|---|
| 233 | |
|---|
| 234 | if (deviceName.empty()) { |
|---|
| 235 | return defaultDevice; |
|---|
| 236 | } else if (!deviceList.contains(deviceName)) { |
|---|
| 237 | return defaultDevice; |
|---|
| 238 | } else { |
|---|
| 239 | return deviceName; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | std::string CoIpManagerConfig::getVideoQuality() const { |
|---|
| 244 | return getStringKeyValue(VIDEO_QUALITY_KEY); |
|---|
| 245 | } |
|---|