configurationmanager.cpp 13.6 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
 *  Author: Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
6
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
7
8
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 3 of the License, or
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
10
 *  (at your option) any later version.
11
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
12
13
14
15
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
16
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
17
18
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20
21
22
23
24
25
26
27
28
29
30
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
31
 */
32

33
#ifdef HAVE_CONFIG_H
Rafaël Carré's avatar
Rafaël Carré committed
34
#include "config.h"
35
#endif
Rafaël Carré's avatar
Rafaël Carré committed
36

37
#include <cerrno>
38
#include <sstream>
39
40
41
42

#include "configurationmanager.h"
#include "account_schema.h"
#include "manager.h"
43
#include "sip/sipvoiplink.h"
44
#include "sip/siptransport.h"
45
#include "logger.h"
46
#include "fileutils.h"
47
#include "sip/sipaccount.h"
48
49
#include "history/historynamecache.h"
#include "audio/audiolayer.h"
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
50

51
52
53
namespace {
    const char* SERVER_PATH = "/org/sflphone/SFLphone/ConfigurationManager";
}
Julien Bonjean's avatar
Julien Bonjean committed
54

55
ConfigurationManager::ConfigurationManager(DBus::Connection& connection) :
56
    DBus::ObjectAdaptor(connection, SERVER_PATH)
57
{}
58

59
std::map<std::string, std::string> ConfigurationManager::getIp2IpDetails()
Julien Bonjean's avatar
Julien Bonjean committed
60
{
61
    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
62

Julien Bonjean's avatar
Julien Bonjean committed
63
    if (!sipaccount) {
64
        ERROR("Could not find IP2IP account");
65
        return std::map<std::string, std::string>();
66
    } else
67
        return sipaccount->getIp2IpDetails();
68
69
}

Julien Bonjean's avatar
Julien Bonjean committed
70

71
std::map<std::string, std::string> ConfigurationManager::getAccountDetails(
72
73
    const std::string& accountID)
{
74
    return Manager::instance().getAccountDetails(accountID);
75
}
Julien Bonjean's avatar
Julien Bonjean committed
76

77
std::map<std::string, std::string>
78
ConfigurationManager::getTlsSettingsDefault()
79
80
81
{
    std::stringstream portstr;
    portstr << DEFAULT_SIP_TLS_PORT;
Julien Bonjean's avatar
Julien Bonjean committed
82

83
    std::map<std::string, std::string> tlsSettingsDefault;
84
85
86
87
88
89
90
91
92
93
94
95
96
    tlsSettingsDefault[CONFIG_TLS_LISTENER_PORT] = portstr.str();
    tlsSettingsDefault[CONFIG_TLS_CA_LIST_FILE] = "";
    tlsSettingsDefault[CONFIG_TLS_CERTIFICATE_FILE] = "";
    tlsSettingsDefault[CONFIG_TLS_PRIVATE_KEY_FILE] = "";
    tlsSettingsDefault[CONFIG_TLS_PASSWORD] = "";
    tlsSettingsDefault[CONFIG_TLS_METHOD] = "TLSv1";
    tlsSettingsDefault[CONFIG_TLS_CIPHERS] = "";
    tlsSettingsDefault[CONFIG_TLS_SERVER_NAME] = "";
    tlsSettingsDefault[CONFIG_TLS_VERIFY_SERVER] = "true";
    tlsSettingsDefault[CONFIG_TLS_VERIFY_CLIENT] = "true";
    tlsSettingsDefault[CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE] = "true";
    tlsSettingsDefault[CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC] = "2";
    tlsSettingsDefault[CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC] = "0";
97

98
    return tlsSettingsDefault;
99
100
}

Julien Bonjean's avatar
Julien Bonjean committed
101
102
103
std::map<std::string, std::string> ConfigurationManager::getTlsSettings()
{
    std::map<std::string, std::string> tlsSettings;
104

105
    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
106

Julien Bonjean's avatar
Julien Bonjean committed
107
108
    if (!sipaccount)
        return tlsSettings;
109

110
    return sipaccount->getTlsSettings();
111
112
}

113
void ConfigurationManager::setTlsSettings(const std::map<std::string, std::string>& details)
Julien Bonjean's avatar
Julien Bonjean committed
114
{
115
    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
116

Julien Bonjean's avatar
Julien Bonjean committed
117
    if (!sipaccount) {
118
        DEBUG("No valid account in set TLS settings");
Julien Bonjean's avatar
Julien Bonjean committed
119
120
121
        return;
    }

122
    sipaccount->setTlsSettings(details);
Julien Bonjean's avatar
Julien Bonjean committed
123
124
125
126
127

    Manager::instance().saveConfig();

    // Update account details to the client side
    accountsChanged();
128
129
}

130

131
void ConfigurationManager::setAccountDetails(const std::string& accountID, const std::map<std::string, std::string>& details)
Julien Bonjean's avatar
Julien Bonjean committed
132
{
133
    Manager::instance().setAccountDetails(accountID, details);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
134
135
}

136
void ConfigurationManager::sendRegister(const std::string& accountID, const bool& enable)
Julien Bonjean's avatar
Julien Bonjean committed
137
{
138
    Manager::instance().sendRegister(accountID, enable);
139
140
}

141
142
143
144
145
void ConfigurationManager::registerAllAccounts()
{
    Manager::instance().registerAllAccounts();
}

146
147
148
149
///This function is used as a base for new accounts for clients that support it
std::map<std::string, std::string> ConfigurationManager::getAccountTemplate()
{
   std::map<std::string, std::string> accTemplate;
150
151
   accTemplate[ CONFIG_LOCAL_PORT                  ] = CONFIG_DEFAULT_LOCAL_PORT            ;
   accTemplate[ CONFIG_PUBLISHED_PORT              ] = CONFIG_DEFAULT_PUBLISHED_PORT        ;
152
   accTemplate[ CONFIG_PUBLISHED_SAMEAS_LOCAL      ] = CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL;
153
154
155
156
   accTemplate[ CONFIG_INTERFACE                   ] = CONFIG_DEFAULT_INTERFACE             ;
   accTemplate[ CONFIG_ACCOUNT_REGISTRATION_EXPIRE ] = CONFIG_DEFAULT_REGISTRATION_EXPIRE   ;
   accTemplate[ CONFIG_RINGTONE_ENABLED            ] = CONFIG_DEFAULT_RINGTONE_ENABLED      ;
   accTemplate[ CONFIG_PRESENCE_ENABLED            ] = CONFIG_DEFAULT_PRESENCE_ENABLED      ;
157
158
159
   return accTemplate;
}

160
std::string ConfigurationManager::addAccount(const std::map<std::string, std::string>& details)
Julien Bonjean's avatar
Julien Bonjean committed
161
{
162
    return Manager::instance().addAccount(details);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
163
164
}

165
void ConfigurationManager::removeAccount(const std::string& accoundID)
Julien Bonjean's avatar
Julien Bonjean committed
166
{
167
    return Manager::instance().removeAccount(accoundID);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
168
169
}

Julien Bonjean's avatar
Julien Bonjean committed
170
171
172
std::vector<std::string> ConfigurationManager::getAccountList()
{
    return Manager::instance().getAccountList();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
173
174
}

Emmanuel Milou's avatar
Emmanuel Milou committed
175
176
/**
 * Send the list of all codecs loaded to the client through DBus.
177
 * Can stay global, as only the active codecs will be set per accounts
Emmanuel Milou's avatar
Emmanuel Milou committed
178
 */
179
std::vector<int32_t> ConfigurationManager::getAudioCodecList()
Julien Bonjean's avatar
Julien Bonjean committed
180
{
181
    std::vector<int32_t> list(Manager::instance().audioCodecFactory.getCodecList());
Emmanuel Milou's avatar
Emmanuel Milou committed
182

183
184
185
    if (list.empty())
        errorAlert(CODECS_NOT_LOADED);

Julien Bonjean's avatar
Julien Bonjean committed
186
    return list;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
187
188
}

189
std::vector<std::string> ConfigurationManager::getSupportedTlsMethod()
Julien Bonjean's avatar
Julien Bonjean committed
190
191
{
    std::vector<std::string> method;
192
193
194
195
    method.push_back("Default");
    method.push_back("TLSv1");
    method.push_back("SSLv3");
    method.push_back("SSLv23");
Julien Bonjean's avatar
Julien Bonjean committed
196
    return method;
197
198
}

199
std::vector<std::string> ConfigurationManager::getAudioCodecDetails(const int32_t& payload)
Julien Bonjean's avatar
Julien Bonjean committed
200
{
201
    std::vector<std::string> result(Manager::instance().audioCodecFactory.getCodecSpecifications(payload));
202

203
204
    if (result.empty())
        errorAlert(CODECS_NOT_LOADED);
205

206
    return result;
207
208
}

209
std::vector<int32_t> ConfigurationManager::getActiveAudioCodecList(const std::string& accountID)
Julien Bonjean's avatar
Julien Bonjean committed
210
{
211
212
    Account *acc = Manager::instance().getAccount(accountID);

Rafaël Carré's avatar
Cleanup    
Rafaël Carré committed
213
    if (acc)
214
215
        return acc->getActiveAudioCodecs();
    else {
216
217
        ERROR("Could not find account %s, returning default", accountID.c_str());
        return Account::getDefaultAudioCodecs();
218
    }
219
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
220

221
void ConfigurationManager::setActiveAudioCodecList(const std::vector<std::string>& list, const std::string& accountID)
Julien Bonjean's avatar
Julien Bonjean committed
222
{
223
224
    Account *acc = Manager::instance().getAccount(accountID);

Rafaël Carré's avatar
Rafaël Carré committed
225
    if (acc) {
226
        acc->setActiveAudioCodecs(list);
Rafaël Carré's avatar
Rafaël Carré committed
227
        Manager::instance().saveConfig();
228
229
    } else {
        ERROR("Could not find account %s", accountID.c_str());
Julien Bonjean's avatar
Julien Bonjean committed
230
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
231
232
}

Julien Bonjean's avatar
Julien Bonjean committed
233
234
235
std::vector<std::string> ConfigurationManager::getAudioPluginList()
{
    std::vector<std::string> v;
236

237
238
    v.push_back(PCM_DEFAULT);
    v.push_back(PCM_DMIX_DSNOOP);
239

Julien Bonjean's avatar
Julien Bonjean committed
240
    return v;
241
}
242

243
void ConfigurationManager::setAudioPlugin(const std::string& audioPlugin)
Julien Bonjean's avatar
Julien Bonjean committed
244
{
245
    return Manager::instance().setAudioPlugin(audioPlugin);
246
247
}

Julien Bonjean's avatar
Julien Bonjean committed
248
249
250
std::vector<std::string> ConfigurationManager::getAudioOutputDeviceList()
{
    return Manager::instance().getAudioOutputDeviceList();
251
}
252

Julien Bonjean's avatar
Julien Bonjean committed
253
254
255
std::vector<std::string> ConfigurationManager::getAudioInputDeviceList()
{
    return Manager::instance().getAudioInputDeviceList();
256
}
257

258
void ConfigurationManager::setAudioOutputDevice(const int32_t& index)
Julien Bonjean's avatar
Julien Bonjean committed
259
{
260
    return Manager::instance().setAudioDevice(index, AudioLayer::SFL_PCM_PLAYBACK);
261
262
}

263
void ConfigurationManager::setAudioInputDevice(const int32_t& index)
Julien Bonjean's avatar
Julien Bonjean committed
264
{
265
    return Manager::instance().setAudioDevice(index, AudioLayer::SFL_PCM_CAPTURE);
266
267
}

268
void ConfigurationManager::setAudioRingtoneDevice(const int32_t& index)
Julien Bonjean's avatar
Julien Bonjean committed
269
{
270
    return Manager::instance().setAudioDevice(index, AudioLayer::SFL_PCM_RINGTONE);
271
}
272

Julien Bonjean's avatar
Julien Bonjean committed
273
274
275
std::vector<std::string> ConfigurationManager::getCurrentAudioDevicesIndex()
{
    return Manager::instance().getCurrentAudioDevicesIndex();
276
}
277

278
int32_t ConfigurationManager::getAudioDeviceIndex(const std::string& name)
Julien Bonjean's avatar
Julien Bonjean committed
279
{
280
    return Manager::instance().getAudioDeviceIndex(name);
281
282
}

283
std::string ConfigurationManager::getCurrentAudioOutputPlugin()
Julien Bonjean's avatar
Julien Bonjean committed
284
{
285
    DEBUG("Get audio plugin %s", Manager::instance().getCurrentAudioOutputPlugin().c_str());
286

Julien Bonjean's avatar
Julien Bonjean committed
287
    return Manager::instance().getCurrentAudioOutputPlugin();
288
289
}

290
std::string ConfigurationManager::getNoiseSuppressState()
Julien Bonjean's avatar
Julien Bonjean committed
291
292
{
    return Manager::instance().getNoiseSuppressState();
293
294
}

295
void ConfigurationManager::setNoiseSuppressState(const std::string& state)
Julien Bonjean's avatar
Julien Bonjean committed
296
{
297
    Manager::instance().setNoiseSuppressState(state);
298
299
}

300
std::string ConfigurationManager::getEchoCancelState()
301
{
302
    return Manager::instance().getEchoCancelState() ? "enabled" : "disabled";
303
304
}

305
306
307
std::map<std::string, std::string> ConfigurationManager::getRingtoneList()
{
    std::map<std::string, std::string> ringToneList;
308
    std::string r_path(fileutils::get_data_dir());
309
310
    struct dirent **namelist;
    int n = scandir(r_path.c_str(), &namelist, 0, alphasort);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    if (n == -1) {
        ERROR("%s", strerror(errno));
        return ringToneList;
    }

    while (n--) {
        if (strcmp(namelist[n]->d_name, ".") and strcmp(namelist[n]->d_name, "..")) {
            std::string file(namelist[n]->d_name);

            if (file.find(".wav") != std::string::npos)
                file.replace(file.find(".wav"), 4, "");
            else
                file.replace(file.size() - 3, 3, "");
            if (file[0] <= 0x7A and file[0] >= 0x61) file[0] = file[0] - 32;
            ringToneList[r_path + namelist[n]->d_name] = file;
326
327
328
329
330
331
332
        }
        free(namelist[n]);
    }
    free(namelist);
    return ringToneList;
}

333
334
void ConfigurationManager::setEchoCancelState(const std::string& state)
{
335
    Manager::instance().setEchoCancelState(state);
336
337
}

338
int32_t ConfigurationManager::isIax2Enabled()
Julien Bonjean's avatar
Julien Bonjean committed
339
{
340
    return HAVE_IAX;
341
342
}

343
std::string ConfigurationManager::getRecordPath()
Julien Bonjean's avatar
Julien Bonjean committed
344
{
345
    return Manager::instance().audioPreference.getRecordPath();
346
347
}

348
void ConfigurationManager::setRecordPath(const std::string& recPath)
Julien Bonjean's avatar
Julien Bonjean committed
349
{
350
    Manager::instance().audioPreference.setRecordPath(recPath);
351
352
}

353
bool ConfigurationManager::getIsAlwaysRecording()
354
{
355
    return Manager::instance().getIsAlwaysRecording();
356
357
358
359
}

void ConfigurationManager::setIsAlwaysRecording(const bool& rec)
{
360
    Manager::instance().setIsAlwaysRecording(rec);
361
}
362

363
int32_t ConfigurationManager::getHistoryLimit()
Julien Bonjean's avatar
Julien Bonjean committed
364
365
{
    return Manager::instance().getHistoryLimit();
366
367
}

368
369
370
371
372
void ConfigurationManager::clearHistory()
{
    return Manager::instance().clearHistory();
}

373
void ConfigurationManager::setHistoryLimit(const int32_t& days)
Julien Bonjean's avatar
Julien Bonjean committed
374
{
375
    Manager::instance().setHistoryLimit(days);
376
377
}

378
void ConfigurationManager::setAudioManager(const std::string& api)
Julien Bonjean's avatar
Julien Bonjean committed
379
{
380
    Manager::instance().setAudioManager(api);
381
382
}

383
std::string ConfigurationManager::getAudioManager()
Julien Bonjean's avatar
Julien Bonjean committed
384
385
{
    return Manager::instance().getAudioManager();
386
387
}

388
std::map<std::string, std::string> ConfigurationManager::getHookSettings()
Julien Bonjean's avatar
Julien Bonjean committed
389
{
390
    return Manager::instance().hookPreference.toMap();
391
392
}

393
void ConfigurationManager::setHookSettings(const std::map<std::string,
Julien Bonjean's avatar
Julien Bonjean committed
394
395
        std::string>& settings)
{
396
    Manager::instance().hookPreference = HookPreference(settings);
397
}
398

399
void ConfigurationManager::setAccountsOrder(const std::string& order)
Julien Bonjean's avatar
Julien Bonjean committed
400
{
401
    Manager::instance().setAccountsOrder(order);
402
403
}

404
std::vector<std::map<std::string, std::string> > ConfigurationManager::getHistory()
Julien Bonjean's avatar
Julien Bonjean committed
405
{
406
    return Manager::instance().getHistory();
407
}
408

409
410
std::string
ConfigurationManager::getAddrFromInterfaceName(const std::string& interface)
Julien Bonjean's avatar
Julien Bonjean committed
411
{
412
    return SipTransport::getInterfaceAddrFromName(interface);
413
414
}

415
std::vector<std::string> ConfigurationManager::getAllIpInterface()
Julien Bonjean's avatar
Julien Bonjean committed
416
{
417
    return SipTransport::getAllIpInterface();
418
}
419

420
std::vector<std::string> ConfigurationManager::getAllIpInterfaceByName()
Julien Bonjean's avatar
Julien Bonjean committed
421
{
422
    return SipTransport::getAllIpInterfaceByName();
423
}
424

Julien Bonjean's avatar
Julien Bonjean committed
425
426
427
std::map<std::string, std::string> ConfigurationManager::getShortcuts()
{
    return Manager::instance().shortcutPreferences.getShortcuts();
428
429
}

430
void ConfigurationManager::setShortcuts(
Julien Bonjean's avatar
Julien Bonjean committed
431
432
    const std::map<std::string, std::string>& shortcutsMap)
{
433
    Manager::instance().shortcutPreferences.setShortcuts(shortcutsMap);
Julien Bonjean's avatar
Julien Bonjean committed
434
    Manager::instance().saveConfig();
435
}
Julien Bonjean's avatar
Julien Bonjean committed
436

437
438
std::vector<std::map<std::string, std::string> > ConfigurationManager::getCredentials(
    const std::string& accountID)
439
{
440
    SIPAccount *account = Manager::instance().getSipAccount(accountID);
441
    std::vector<std::map<std::string, std::string> > credentialInformation;
442

443
    if (!account)
444
        return credentialInformation;
445
446
    else
        return account->getCredentials();
447
}
448

449
void ConfigurationManager::setCredentials(const std::string& accountID,
450
451
        const std::vector<std::map<std::string, std::string> >& details)
{
452
    SIPAccount *account = Manager::instance().getSipAccount(accountID);
453
454
    if (account)
        account->setCredentials(details);
455
}