Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
197 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
21 andreas 18
 
2 andreas 19
#include <fstream>
20
#include <vector>
21
#include <iterator>
22
#include <unistd.h>
21 andreas 23
#include <sys/stat.h>
24
#include <sys/types.h>
22 andreas 25
#ifdef __ANDROID__
134 andreas 26
#include <QUuid>
22 andreas 27
#include <android/log.h>
28
#include "tvalidatefile.h"
43 andreas 29
#include "tvalidatefile.h"
134 andreas 30
#else
31
#include <uuid/uuid.h>
22 andreas 32
#endif
118 andreas 33
#include "ttpinit.h"
22 andreas 34
#include "tconfig.h"
2 andreas 35
#include "terror.h"
73 andreas 36
#include "tresources.h"
292 andreas 37
#include "tlock.h"
240 andreas 38
#ifdef __APPLE__
39
#include <TargetConditionals.h>
40
#if TARGET_OS_SIMULATOR || TARGET_OS_IOS
239 andreas 41
#include <QString>
250 andreas 42
#include "ios/QASettings.h"
239 andreas 43
#endif
240 andreas 44
#endif
2 andreas 45
using std::string;
46
using std::ifstream;
21 andreas 47
using std::ofstream;
2 andreas 48
using std::fstream;
49
using std::vector;
21 andreas 50
using std::cout;
51
using std::cerr;
52
using std::endl;
2 andreas 53
 
116 andreas 54
bool TConfig::mInitialized{false};
134 andreas 55
int TConfig::mChannel{0};
141 andreas 56
bool TConfig::mMute{false};
192 andreas 57
bool TConfig::mTemporary{false};
257 andreas 58
bool TConfig::mLogFileEnabled{false};
292 andreas 59
std::mutex config_mutex;
116 andreas 60
 
21 andreas 61
/**
62
 * @struct SETTINGS
63
 * @brief The SETTINGS struct bundles the configuration options.
64
 *
65
 * This structure contains variables for all possible configuration options.
66
 * It is used by the class TConfig. Through this class it's possible to
67
 * access all configuration options.
68
 */
2 andreas 69
struct SETTINGS
70
{
21 andreas 71
    string pname{"tpanel"};     //!< Name of the program (default "tpanel")
72
    string path;                //!< The path where the configuration file is located
73
    string name;                //!< The name of the configuration file
74
    string project;             //!< The path where the original project files are located
75
    string server;              //!< The name or IP address of the server to connect
76
    int system{0};              //!< The number of the AMX system
77
    int port{0};                //!< The port number
78
    int ID{0};                  //!< the panel ID (a number starting by 10000)
79
    string ptype;               //!< The type of the panel (android, ipad, iphone, ...)
80
    string version;             //!< The "firmware" version
81
    string logFile;             //!< Optional path and name of a logfile
367 andreas 82
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
83
#ifdef QT_DEBUG
84
    string logLevel{"INFO|WARNING|ERROR|DEBUG"};   //!< The log level(s).
85
    uint logLevelBits{HLOG_INFO|HLOG_WARNING|HLOG_ERROR|HLOG_DEBUG};//!< The numeric bit field of the loglevel
86
#else
87
    string logLevel{"NONE"};   //!< The log level(s).
88
    uint logLevelBits{HLOG_NONE};//!< The numeric bit field of the loglevel
89
#endif
90
#else
91
    string logLevel{"PROTOCOL"};//!< The log level(s).
92
    uint logLevelBits{HLOG_PROTOCOL};//!< The numeric bit field of the loglevel
93
#endif
21 andreas 94
    bool longformat{false};     //!< TRUE = long format
95
    bool noBanner{false};       //!< Startup without showing a banner on the command line.
96
    bool certCheck{false};      //!< TRUE = Check certificate for SSL connection
24 andreas 97
    bool scale{false};          //!< TRUE = Images are scaled to fit the whole screen
151 andreas 98
    bool tbsuppress{false};     //!< TRUE = Don't show toolbar even if enough space
99
    bool tbforce{true};         //!< Only if "tbsuppress" = FALSE: TRUE = The toolbar is forced to display, FALSE = The toolbar is only visible if there is enough space left
35 andreas 100
    bool profiling{false};      //!< TRUE = The declaration traces meassure the time and write it to the log
175 andreas 101
    size_t max_cache{100};      //!< Size of internal button cache in Mb
51 andreas 102
    string password1;           //!< First panel password
103
    string password2;           //!< Second panel password
104
    string password3;           //!< Third panel password
105
    string password4;           //!< Fourth panel password
134 andreas 106
    bool systemRotationFix{false};  //!< TRUE = Rotation is blocked and orientation sensor is ignored.
107
    string uuid;                //!< An UUID set automatically after first start.
112 andreas 108
    // FTP credentials
109
    string ftpUser;             //!< The username for FTP of the controller (default: administrator)
110
    string ftpPassword;         //!< The password for FTP of the controller (default: password)
115 andreas 111
    string ftpSurface;          //!< The name of the file containing the TPDesign4 file to load
116 andreas 112
    bool ftpPassive{true};      //!< If false the data port 20 is used for file transfer
115 andreas 113
    time_t ftpLastDownload{0};  //!< The timestamp of the last download
104 andreas 114
    // SIP settings
115
    string sip_proxy;           //!< The address of the SIP proxy
116
    int sip_port{5060};         //!< Initializes the port of the SIP proxy to 5060
127 andreas 117
    int sip_portTLS{0};         //!< Initializes the TLS port of the SIP proxy to 0 (not used by default)
104 andreas 118
    string sip_stun;            //!< STUN address
119
    string sip_domain;          //!< Local domain
120
    string sip_user;            //!< The SIP user to connect.
121
    string sip_password;        //!< The SIP password to connect. Note: This password is saved in plain text!
127 andreas 122
    bool sip_ipv4{true};        //!< Default: TRUE, Enables or disables IPv4.
123
    bool sip_ipv6{true};        //!< Default: TRUE, Enables or disables IPv6. Has precedence over IPv4.
139 andreas 124
    bool sip_iphone{false};     //!< Default: FALSE, if enabled and SIP is enabled then the internal phone dialog is used.
127 andreas 125
    TConfig::SIP_FIREWALL_t sip_firewall{TConfig::SIP_NO_FIREWALL}; //!< Defines how to deal with a firewall.
104 andreas 126
    bool sip_enabled{false};    //!< By default SIP is disabled
141 andreas 127
    // Sound settings
128
    string systemSound;         //!< name of the set system sound played on every touch.
129
    bool systemSoundState{false};   //!< TRUE = play systemsound on every touch
130
    string systemSingleBeep;    //!< name of the system sound file to play a single beep.
131
    string systemDoubleBeep;    //!< name of the system sound file to play a double beep.
132
    int systemVolume{100};      //!< The set volume to use [0 ... 100]
133
    int systemGain{100};        //!< The set microphone level to use [0 ... 100]
2 andreas 134
};
135
 
136
typedef struct SETTINGS settings_t;
192 andreas 137
static settings_t localSettings;        //!< Global defines settings used in class TConfig.
138
static settings_t localSettings_temp;   //!< Global defines settings temporary settings
2 andreas 139
 
21 andreas 140
/**
141
 * @brief TConfig::TConfig constructor
142
 *
143
 * @param path  A path and name of a configuration file.
144
 */
2 andreas 145
TConfig::TConfig(const std::string& path)
116 andreas 146
    : mPath(path)
2 andreas 147
{
240 andreas 148
#if TARGET_OS_IOS == 0 && TARGET_OS_SIMULATOR == 0
90 andreas 149
    // Initialize the possible configuration paths
150
    mCfgPaths.push_back("/etc");
151
    mCfgPaths.push_back("/etc/tpanel");
152
    mCfgPaths.push_back("/usr/etc");
153
    mCfgPaths.push_back("/usr/etc/tpanel");
154
#ifdef __APPLE__
235 andreas 155
    mCfgPaths.push_back("/opt/local/etc");
156
    mCfgPaths.push_back("/opt/local/etc/tpanel");
157
    mCfgPaths.push_back("/opt/local/usr/etc");
158
    mCfgPaths.push_back("/opt/local/usr/etc/tpanel");
90 andreas 159
#endif
160
    if (findConfig())
161
        readConfig();
239 andreas 162
#else
163
    readConfig();
164
#endif
2 andreas 165
}
166
 
118 andreas 167
/**
168
 * Simple method to read the configuration again. This is usefull if, for
169
 * example the configuration options changed but should not be saved. Instead
170
 * they were canceled and therefor the options are read again from file.
171
 *
172
 * @return On success it returns TRUE.
173
 */
23 andreas 174
bool TConfig::reReadConfig()
175
{
176
    return readConfig();
177
}
178
 
316 andreas 179
/**
180
 * @brief TConfig::setTemporary - Activate/deactivate temporary config
181
 * Activates or deactivates the state of the temporary configuration. The
182
 * temporary configuration is a shadow configuration which holds all changes
183
 * to the configuration without saving it.
184
 *
185
 * @param tmp   State of the active configuration.
186
 * @return
187
 * Returns the previous state of the configuration setting.
188
 */
189
bool TConfig::setTemporary(bool tmp)
192 andreas 190
{
191
    DECL_TRACER("TConfig::setTemporary(bool tmp)");
192
 
316 andreas 193
    bool old = mTemporary;
192 andreas 194
    mTemporary = tmp;
316 andreas 195
    return old;
192 andreas 196
}
197
 
198
void TConfig::reset()
199
{
200
    DECL_TRACER("TConfig::reset()");
201
 
202
    localSettings_temp = localSettings;
203
    mTemporary = false;
204
}
205
 
21 andreas 206
/**
207
 * @brief TConfig::setProgName Sets the name of the application.
208
 * @param pname The name of the application.
209
 */
2 andreas 210
void TConfig::setProgName(const std::string& pname)
211
{
192 andreas 212
    if (mTemporary)
213
        localSettings_temp.pname = pname;
214
    else
215
        localSettings.pname = pname;
216
 
217
    mTemporary = false;
2 andreas 218
}
219
 
21 andreas 220
/**
221
 * @brief TConfig::getProgName Retrieves the prevously stored application name.
222
 * @return The name of this application.
223
 */
2 andreas 224
std::string & TConfig::getProgName()
225
{
192 andreas 226
    return mTemporary ? localSettings_temp.pname : localSettings.pname;
2 andreas 227
}
228
 
21 andreas 229
/**
230
 * @brief TConfig::getChannel returns the AMX channel to use.
231
 *
232
 * The AMX channels an AMX panel can use start at 10000. This method returns
233
 * the channel number found in the configuration file. If there was no
234
 * channel defination found, it returns the default channel 10001.
235
 *
236
 * @return The AMX channel number to use.
237
 */
2 andreas 238
int TConfig::getChannel()
239
{
116 andreas 240
    DECL_TRACER("TConfig::getChannel()");
241
 
192 andreas 242
    int ID = mTemporary ? localSettings_temp.ID : localSettings.ID;
243
 
244
    if (mChannel > 0 && mChannel != ID)
134 andreas 245
        return mChannel;
246
 
192 andreas 247
    return ID;
2 andreas 248
}
249
 
21 andreas 250
/**
251
 * @brief TConfig::getConfigFileName returns the name of the configuration file.
252
 *
253
 * @return The name of the configuration file.
254
 */
2 andreas 255
std::string& TConfig::getConfigFileName()
256
{
192 andreas 257
    return mTemporary ? localSettings_temp.name : localSettings.name;
2 andreas 258
}
259
 
21 andreas 260
/**
261
 * @brief TConfig::getConfigPath returns the path configuration file.
262
 *
263
 * The path was defined on the command line or found by searching the standard
264
 * directories.
265
 *
266
 * @return The path of the configuration file.
267
 */
2 andreas 268
std::string& TConfig::getConfigPath()
269
{
192 andreas 270
    return mTemporary ? localSettings_temp.path : localSettings.path;
2 andreas 271
}
272
 
21 andreas 273
/**
274
 * @brief TConfig::getController returns the network name or IP address of the AMX controller.
275
 *
276
 * The network name or the IP address was read from the configuration file.
277
 *
278
 * @return The network name of the AMX controller.
279
 */
2 andreas 280
std::string& TConfig::getController()
281
{
116 andreas 282
    DECL_TRACER("TConfig::getController()");
283
 
192 andreas 284
    return mTemporary ? localSettings_temp.server : localSettings.server;
2 andreas 285
}
286
 
21 andreas 287
/**
288
 * @brief TConfig::getSystem return the AMX system number.
289
 *
290
 * This number was read from the configuration file. If there was no system
291
 * number defined in the configuration file, then the default number 0 is
292
 * returned.
293
 *
294
 * @return The AMX system number.
295
 */
11 andreas 296
int TConfig::getSystem()
297
{
116 andreas 298
    DECL_TRACER("TConfig::getSystem()");
299
 
192 andreas 300
    return mTemporary ? localSettings_temp.system :  localSettings.system;
11 andreas 301
}
302
 
21 andreas 303
/**
304
 * @brief TConfig::getFirmVersion returns the version of the firmware.
305
 *
306
 * This option was read from the configuration file. There can be any version
307
 * number defined. But you must keep in mind, that the AMX controller may not
308
 * accept any number. If there was no version number defined, the standard
309
 * version number 1.0 is returned.
310
 *
311
 * @return The firmware version of this panel.
312
 */
2 andreas 313
std::string& TConfig::getFirmVersion()
314
{
116 andreas 315
    DECL_TRACER("TConfig::getFirmVersion()");
316
 
192 andreas 317
    return mTemporary ? localSettings_temp.version : localSettings.version;
2 andreas 318
}
319
 
21 andreas 320
/**
321
 * @brief TConfig::getLogFile the path and name of a logfile.
322
 *
323
 * If there is a logfile name defined in the configuration file, it is used
324
 * to write messages there. It depends on the _log level_ what is logged.
325
 *
326
 * @return The path and name of a logfile.
327
 */
2 andreas 328
std::string& TConfig::getLogFile()
329
{
192 andreas 330
    return mTemporary ? localSettings_temp.logFile : localSettings.logFile;
2 andreas 331
}
332
 
21 andreas 333
/**
334
 * @brief TConfig::getLogLevel returns the defined log level.
335
 *
51 andreas 336
 * The loglevel can consist of the following values:
21 andreas 337
 *
338
 *     NONE         Logs nothing (default for Android)
339
 *     INFO         Logs only informations
340
 *     WARNING      Logs only warnings
51 andreas 341
 *     ERROR        Logs only errors
21 andreas 342
 *     TRACE        Logs only trace messages
343
 *     DEBUG        Logs only debug messages
344
 *     PROTOCOL     Logs only INFO and ERROR (default if NOT Android)
345
 *     ALL          Logs everything
346
 *
347
 * All log levels can be combined by concatenating them with the | symbol.
348
 *
349
 * @return The log level(s) as a string.
350
 */
2 andreas 351
string& TConfig::getLogLevel()
352
{
192 andreas 353
    return mTemporary ? localSettings_temp.logLevel : localSettings.logLevel;
2 andreas 354
}
355
 
21 andreas 356
/**
59 andreas 357
 * @brief TConfig::getLogLevelBits
358
 *
359
 * Returns the raw bit field defining the loglevels selected.
360
 *
361
 * @return The bit field of representing the selected log levels.
362
 */
363
uint TConfig::getLogLevelBits()
364
{
116 andreas 365
    DECL_TRACER("TConfig::getLogLevelBits()");
366
 
192 andreas 367
    return mTemporary ? localSettings_temp.logLevelBits : localSettings.logLevelBits;
59 andreas 368
}
369
/**
21 andreas 370
 * @brief TConfig::getPanelType the AMX type name of the panel.
371
 *
372
 * The type name of the panel is defined in the configuration file. If this
373
 * option was not defined, the default panel _android_ is returned.
374
 *
375
 * @return The type name of the panel.
376
 */
2 andreas 377
std::string& TConfig::getPanelType()
378
{
116 andreas 379
    DECL_TRACER("TConfig::getPanelType()");
380
 
192 andreas 381
    return mTemporary ? localSettings_temp.ptype : localSettings.ptype;
2 andreas 382
}
383
 
21 andreas 384
/**
385
 * @brief TConfig::getPort returnes the AMX port number to connect to.
386
 *
387
 * The port number can be defined in the configuration file. If there is no
388
 * configuration the default number 1319 is returned.
389
 *
390
 * @return The AMX network port number.
391
 */
2 andreas 392
int TConfig::getPort()
393
{
116 andreas 394
    DECL_TRACER("TConfig::getPort()");
395
 
192 andreas 396
    return mTemporary ? localSettings_temp.port : localSettings.port;
2 andreas 397
}
398
 
21 andreas 399
/**
400
 * @brief TConfig::getProjectPath returns the path to the AMX configuration files.
401
 *
402
 * The path was read from the configuration file. This path contains all the
403
 * files needed to display the elements of the surface.
404
 *
405
 * @return The path to the AMX configuration files.
406
 */
71 andreas 407
string& TConfig::getProjectPath()
2 andreas 408
{
197 andreas 409
    return localSettings.project;
2 andreas 410
}
411
 
197 andreas 412
/**
413
 * @brief TConfig::getSystemProjectPath returns the path to the AMX setup
414
 * configuration files.
415
 *
416
 * The path was read from the configuration file. This path contains all the
417
 * files needed to display the setup elements of the setup dialog.
418
 *
419
 * @return The path to the AMX setup configuration files.
420
 */
421
string TConfig::getSystemProjectPath()
422
{
423
    return localSettings.project + "/__system";
424
}
112 andreas 425
 
71 andreas 426
string TConfig::getSystemPath(SYSTEMRESOURCE_t sres)
427
{
116 andreas 428
    DECL_TRACER("TConfig::getSystemPath(SYSTEMRESOURCE_t sres)");
429
 
71 andreas 430
    string p;
431
 
432
    switch(sres)
433
    {
434
        case BORDERS:   p = "/borders"; break;
435
        case FONTS:     p = "/fonts"; break;
436
        case IMAGES:    p = "/images"; break;
437
        case SLIDERS:   p = "/sliders"; break;
438
        case SOUNDS:    p = "/sounds"; break;
439
        default:
440
            p.clear();
441
    }
442
 
197 andreas 443
    return localSettings.project + "/__system/graphics" + p;
71 andreas 444
}
445
 
23 andreas 446
bool TConfig::saveLogFile(const string &file)
447
{
448
    DECL_TRACER("TConfig::saveLogFile(const string &file)");
449
 
192 andreas 450
    string logFile = mTemporary ? localSettings_temp.logFile : localSettings.logFile;
451
 
452
    if (file.empty() || logFile.compare(file) == 0)
23 andreas 453
        return false;
454
 
192 andreas 455
    if (mTemporary)
456
        localSettings_temp.logFile = file;
457
    else
458
        localSettings.logFile = file;
459
 
460
    mTemporary = false;
23 andreas 461
    return true;
462
}
463
 
464
bool TConfig::saveLogLevel(const string &level)
465
{
466
    DECL_TRACER("TConfig::saveLogLevel(const string &level)");
467
 
59 andreas 468
    if (level.find(SLOG_NONE) == string::npos && level.find(SLOG_INFO) == string::npos && level.find(SLOG_WARNING) == string::npos &&
469
            level.find(SLOG_ERROR) == string::npos && level.find(SLOG_TRACE) == string::npos && level.find(SLOG_DEBUG) == string::npos &&
470
            level.find(SLOG_PROTOCOL) == string::npos && level.find(SLOG_ALL) == string::npos)
23 andreas 471
        return false;
472
 
192 andreas 473
    if (mTemporary)
474
    {
475
        localSettings_temp.logLevel = level;
476
        localSettings_temp.logLevelBits = logLevelStrToBits(level);
477
    }
478
    else
479
    {
480
        localSettings.logLevel = level;
481
        localSettings.logLevelBits = logLevelStrToBits(level);
482
    }
483
 
59 andreas 484
    MSG_INFO("New log level: " << level);
192 andreas 485
    mTemporary = false;
23 andreas 486
    return true;
487
}
488
 
59 andreas 489
bool TConfig::saveLogLevel(uint level)
490
{
491
    DECL_TRACER("TConfig::saveLogLevel(uint level)");
492
 
493
    if (level != 0 && !(level&HLOG_INFO) && !(level&HLOG_WARNING) &&
494
            !(level&HLOG_ERROR) && !(level&HLOG_TRACE) && !(level&HLOG_DEBUG))
495
        return false;
496
 
192 andreas 497
    if (mTemporary)
498
    {
499
        localSettings_temp.logLevelBits = level;
500
        localSettings_temp.logLevel = logLevelBitsToString(level);
501
    }
502
    else
503
    {
504
        localSettings.logLevelBits = level;
505
        localSettings.logLevel = logLevelBitsToString(level);
367 andreas 506
        TStreamError::setLogLevel(level);
192 andreas 507
        MSG_INFO("New log level from bits: " << localSettings.logLevel);
508
    }
509
 
510
    mTemporary = false;
59 andreas 511
    return true;
512
}
513
 
23 andreas 514
bool TConfig::saveChannel(int channel)
515
{
516
    DECL_TRACER("TConfig::saveChannel(int channel)");
517
 
271 andreas 518
    if (channel < 10000 || channel > 20000)
23 andreas 519
        return false;
520
 
192 andreas 521
    if (mTemporary)
522
        localSettings_temp.ID = channel;
523
    else
524
        localSettings.ID = channel;
525
 
526
    mTemporary = false;
23 andreas 527
    return true;
528
}
529
 
530
bool TConfig::saveController(const string &cnt)
531
{
532
    DECL_TRACER("TConfig::saveController(const string &cnt)");
533
 
192 andreas 534
    if (mTemporary)
535
        localSettings_temp.server = cnt;
536
    else
537
        localSettings.server = cnt;
538
 
539
    mTemporary = false;
23 andreas 540
    return true;
541
}
542
 
543
bool TConfig::savePanelType(const string &pt)
544
{
545
    DECL_TRACER("TConfig::savePanelType(const string &pt)");
546
 
192 andreas 547
    if (mTemporary)
548
        localSettings_temp.ptype = pt;
549
    else
550
        localSettings.ptype = pt;
551
 
552
    mTemporary = false;
23 andreas 553
    return true;
554
}
555
 
556
bool TConfig::savePort(int port)
557
{
558
    DECL_TRACER("TConfig::savePort(int port)");
559
 
560
    if (port < 1024 || port > 32767)
561
        return false;
562
 
192 andreas 563
    if (mTemporary)
564
        localSettings_temp.port = port;
565
    else
566
        localSettings.port = port;
567
 
568
    mTemporary = false;
23 andreas 569
    return true;
570
}
571
 
572
bool TConfig::saveProjectPath(const string &path)
573
{
574
    DECL_TRACER("TConfig::saveProjectPath(const string &path)");
575
 
576
    if (path.empty())
577
        return false;
578
 
192 andreas 579
    if (mTemporary)
580
        localSettings_temp.project = path;
581
    else
582
        localSettings.project = path;
583
 
584
    mTemporary = false;
23 andreas 585
    return true;
586
}
587
 
588
void TConfig::saveFormat(bool format)
589
{
192 andreas 590
    DECL_TRACER(string("TConfig::saveFormat(bool format) ") + (format ? "[TRUE]" : "[FALSE]"));
23 andreas 591
 
192 andreas 592
    if (mTemporary)
593
        localSettings_temp.longformat = format;
594
    else
595
        localSettings.longformat = format;
596
 
597
    mTemporary = false;
23 andreas 598
}
599
 
24 andreas 600
void TConfig::saveScale(bool scale)
601
{
602
    DECL_TRACER("TConfig::saveScale(bool scale)");
603
 
192 andreas 604
    if (mTemporary)
605
        localSettings_temp.scale = scale;
606
    else
607
        localSettings.scale = scale;
608
 
609
    mTemporary = false;
24 andreas 610
}
611
 
118 andreas 612
void TConfig::saveBanner(bool banner)
613
{
614
    DECL_TRACER("TConfig::saveBanner(bool banner)");
615
 
192 andreas 616
    if (mTemporary)
617
        localSettings_temp.noBanner = banner;
618
    else
619
        localSettings.noBanner = banner;
620
 
621
    mTemporary = false;
118 andreas 622
}
623
 
120 andreas 624
void TConfig::saveToolbarForce(bool tb)
625
{
626
    DECL_TRACER("TConfig::saveToolbarForce(bool tb)");
627
 
192 andreas 628
    if (mTemporary)
629
        localSettings_temp.tbforce = tb;
630
    else
631
        localSettings.tbforce = tb;
632
 
633
    mTemporary = false;
120 andreas 634
}
635
 
151 andreas 636
void TConfig::saveToolbarSuppress(bool tb)
637
{
638
    DECL_TRACER("TConfig::saveToolbarSuppress(bool tb)");
639
 
192 andreas 640
    if (mTemporary)
641
        localSettings_temp.tbsuppress = tb;
642
    else
643
        localSettings.tbsuppress = tb;
644
 
645
    mTemporary = false;
151 andreas 646
}
647
 
35 andreas 648
void TConfig::saveProfiling(bool prof)
649
{
116 andreas 650
    DECL_TRACER("TConfig::saveProfiling(bool prof)");
651
 
192 andreas 652
    if (mTemporary)
653
        localSettings_temp.profiling = prof;
654
    else
655
        localSettings.profiling = prof;
656
 
657
    mTemporary = false;
35 andreas 658
}
659
 
175 andreas 660
void TConfig::saveButtonCache(size_t size)
661
{
662
    DECL_TRACER("TConfig::saveButtonCache(size_t size)");
663
 
192 andreas 664
    if (mTemporary)
665
        localSettings_temp.max_cache = size;
666
    else
667
        localSettings.max_cache = size;
668
 
669
    mTemporary = false;
175 andreas 670
}
671
 
51 andreas 672
void TConfig::savePassword1(const std::string& pw)
673
{
116 andreas 674
    DECL_TRACER("TConfig::savePassword1(const std::string& pw)");
675
 
192 andreas 676
    if (mTemporary)
677
        localSettings_temp.password1 = pw;
678
    else
679
        localSettings.password1 = pw;
680
 
681
    mTemporary = false;
51 andreas 682
}
683
 
684
void TConfig::savePassword2(const std::string& pw)
685
{
116 andreas 686
    DECL_TRACER("TConfig::savePassword2(const std::string& pw)");
687
 
192 andreas 688
    if (mTemporary)
689
        localSettings_temp.password2 = pw;
690
    else
691
        localSettings.password2 = pw;
692
 
693
    mTemporary = false;
51 andreas 694
}
695
 
696
void TConfig::savePassword3(const std::string& pw)
697
{
116 andreas 698
    DECL_TRACER("TConfig::savePassword3(const std::string& pw)");
699
 
192 andreas 700
    if (mTemporary)
701
        localSettings_temp.password3 = pw;
702
    else
703
        localSettings.password3 = pw;
704
 
705
    mTemporary = false;
51 andreas 706
}
707
 
708
void TConfig::savePassword4(const std::string& pw)
709
{
116 andreas 710
    DECL_TRACER("TConfig::savePassword4(const std::string& pw)");
711
 
192 andreas 712
    if (mTemporary)
713
        localSettings_temp.password4 = pw;
714
    else
715
        localSettings.password4 = pw;
716
 
717
    mTemporary = false;
51 andreas 718
}
719
 
71 andreas 720
void TConfig::saveSystemSoundFile(const std::string& snd)
721
{
116 andreas 722
    DECL_TRACER("TConfig::saveSystemSoundFile(const std::string& snd)");
723
 
192 andreas 724
    if (mTemporary)
725
        localSettings_temp.systemSound = snd;
726
    else
727
        localSettings.systemSound = snd;
728
 
729
    mTemporary = false;
71 andreas 730
}
731
 
732
void TConfig::saveSystemSoundState(bool state)
733
{
116 andreas 734
    DECL_TRACER("TConfig::saveSystemSoundState(bool state)");
735
 
192 andreas 736
    if (mTemporary)
737
        localSettings_temp.systemSoundState = state;
738
    else
195 andreas 739
        localSettings.systemSoundState = localSettings_temp.systemSoundState = state;
192 andreas 740
 
741
    mTemporary = false;
71 andreas 742
}
743
 
141 andreas 744
void TConfig::saveSingleBeepFile(const std::string& snd)
745
{
746
    DECL_TRACER("TConfig::saveSingleBeepFile(const std::string& snd)");
747
 
192 andreas 748
    if (mTemporary)
749
        localSettings_temp.systemSingleBeep = snd;
750
    else
751
        localSettings.systemSingleBeep = snd;
752
 
753
    mTemporary = false;
141 andreas 754
}
755
 
756
void TConfig::saveDoubleBeepFile(const std::string& snd)
757
{
758
    DECL_TRACER("TConfig::saveDoubleBeepFile(const std::string& snd)");
759
 
192 andreas 760
    if (mTemporary)
761
        localSettings_temp.systemDoubleBeep = snd;
762
    else
763
        localSettings.systemDoubleBeep = snd;
764
 
765
    mTemporary = false;
141 andreas 766
}
767
 
768
void TConfig::saveSystemVolume(int volume)
769
{
770
    DECL_TRACER("TConfig::saveSystemVolume(int volume)");
771
 
772
    if (volume < 0 || volume > 100)
773
        return;
774
 
192 andreas 775
    if (mTemporary)
776
        localSettings_temp.systemVolume = volume;
777
    else
778
        localSettings.systemVolume = volume;
779
 
780
    mTemporary = false;
141 andreas 781
}
782
 
783
void TConfig::saveSystemGain(int gain)
784
{
785
    DECL_TRACER("TConfig::saveSystemGain(int gain)");
786
 
787
    if (gain < 0 || gain > 100)
788
        return;
789
 
192 andreas 790
    if (mTemporary)
791
        localSettings_temp.systemGain = gain;
792
    else
793
        localSettings.systemGain = gain;
794
 
795
    mTemporary = false;
141 andreas 796
}
797
 
112 andreas 798
void TConfig::saveFtpUser(const string& user)
799
{
116 andreas 800
    DECL_TRACER("TConfig::saveFtpUser(const string& user)");
801
 
192 andreas 802
    if (mTemporary)
803
        localSettings_temp.ftpUser = user;
804
    else
805
        localSettings.ftpUser = user;
806
 
807
    mTemporary = false;
112 andreas 808
}
809
 
810
void TConfig::saveFtpPassword(const string& pw)
811
{
116 andreas 812
    DECL_TRACER("TConfig::saveFtpPassword(const string& pw)");
813
 
192 andreas 814
    if (mTemporary)
815
        localSettings_temp.ftpPassword = pw;
816
    else
817
        localSettings.ftpPassword = pw;
818
 
819
    mTemporary = false;
112 andreas 820
}
821
 
115 andreas 822
void TConfig::saveFtpSurface(const string& fname)
823
{
116 andreas 824
    DECL_TRACER("TConfig::saveFtpSurface(const string& fname)");
825
 
192 andreas 826
    if (mTemporary)
827
        localSettings_temp.ftpSurface = fname;
828
    else
829
        localSettings.ftpSurface = fname;
830
 
831
    mTemporary = false;
115 andreas 832
}
833
 
116 andreas 834
void TConfig::saveFtpPassive(bool mode)
835
{
836
    DECL_TRACER("TConfig::saveFtpPassive(bool mode)");
837
 
192 andreas 838
    if (mTemporary)
839
        localSettings_temp.ftpPassive = mode;
840
    else
841
        localSettings.ftpPassive = mode;
842
 
843
    mTemporary = false;
116 andreas 844
}
845
 
115 andreas 846
void TConfig::saveFtpDownloadTime(time_t t)
847
{
116 andreas 848
    DECL_TRACER("TConfig::saveFtpDownloadTime(time_t t)");
849
 
192 andreas 850
    if (mTemporary)
851
        localSettings_temp.ftpLastDownload = t;
852
    else
853
        localSettings.ftpLastDownload = t;
854
 
855
    mTemporary = false;
115 andreas 856
}
857
 
104 andreas 858
std::string& TConfig::getSIPproxy()
859
{
116 andreas 860
    DECL_TRACER("TConfig::getSIPproxy()");
861
 
192 andreas 862
    return mTemporary ? localSettings_temp.sip_proxy : localSettings.sip_proxy;
104 andreas 863
}
864
 
865
void TConfig::setSIPproxy(const std::string& address)
866
{
116 andreas 867
    DECL_TRACER("TConfig::setSIPproxy(const std::string& address)");
868
 
192 andreas 869
    if (mTemporary)
870
        localSettings_temp.sip_proxy = address;
871
    else
872
        localSettings.sip_proxy = address;
873
 
874
    mTemporary = false;
104 andreas 875
}
876
 
877
int TConfig::getSIPport()
878
{
116 andreas 879
    DECL_TRACER("TConfig::getSIPport()");
880
 
192 andreas 881
    return mTemporary ? localSettings_temp.sip_port : localSettings.sip_port;
104 andreas 882
}
883
 
884
void TConfig::setSIPport(int port)
885
{
116 andreas 886
    DECL_TRACER("TConfig::setSIPport(int port)");
887
 
192 andreas 888
    if (mTemporary)
889
        localSettings_temp.sip_port = port;
890
    else
891
        localSettings.sip_port = port;
892
 
893
    mTemporary = false;
104 andreas 894
}
895
 
127 andreas 896
int TConfig::getSIPportTLS()
897
{
898
    DECL_TRACER("TConfig::getSIPportTLS()");
899
 
192 andreas 900
    return mTemporary ? localSettings_temp.sip_portTLS : localSettings.sip_portTLS;
127 andreas 901
}
902
 
903
void TConfig::setSIPportTLS(int port)
904
{
905
    DECL_TRACER("TConfig::setSIPportTLS(int port)");
906
 
192 andreas 907
    if (mTemporary)
908
        localSettings_temp.sip_portTLS = port;
909
    else
910
        localSettings.sip_portTLS = port;
911
 
912
    mTemporary = false;
127 andreas 913
}
914
 
104 andreas 915
std::string& TConfig::getSIPstun()
916
{
116 andreas 917
    DECL_TRACER("TConfig::getSIPstun()");
918
 
192 andreas 919
    return mTemporary ? localSettings_temp.sip_stun : localSettings.sip_stun;
104 andreas 920
}
921
 
922
void TConfig::setSIPstun(const std::string& address)
923
{
116 andreas 924
    DECL_TRACER("TConfig::setSIPstun(const std::string& address)");
925
 
192 andreas 926
    if (mTemporary)
927
        localSettings_temp.sip_stun = address;
928
    else
929
        localSettings.sip_stun = address;
930
 
931
    mTemporary = false;
104 andreas 932
}
933
 
934
std::string& TConfig::getSIPdomain()
935
{
116 andreas 936
    DECL_TRACER("TConfig::getSIPdomain()");
937
 
192 andreas 938
    return mTemporary ? localSettings_temp.sip_domain : localSettings.sip_domain;
104 andreas 939
}
940
 
941
void TConfig::setSIPdomain(const std::string& domain)
942
{
116 andreas 943
    DECL_TRACER("TConfig::setSIPdomain(const std::string& domain)");
944
 
192 andreas 945
    if (mTemporary)
946
        localSettings_temp.sip_domain = domain;
947
    else
948
        localSettings.sip_domain = domain;
949
 
950
    mTemporary = false;
104 andreas 951
}
952
 
953
std::string& TConfig::getSIPuser()
954
{
116 andreas 955
    DECL_TRACER("TConfig::getSIPuser()");
956
 
192 andreas 957
    return mTemporary ? localSettings_temp.sip_user : localSettings.sip_user;
104 andreas 958
}
959
 
960
void TConfig::setSIPuser(const std::string& user)
961
{
116 andreas 962
    DECL_TRACER("TConfig::setSIPuser(const std::string& user)");
963
 
192 andreas 964
    if (mTemporary)
965
        localSettings_temp.sip_user = user;
966
    else
967
        localSettings.sip_user = user;
968
 
969
    mTemporary = false;
104 andreas 970
}
971
 
972
std::string& TConfig::getSIPpassword()
973
{
116 andreas 974
    DECL_TRACER("TConfig::getSIPpassword()");
975
 
192 andreas 976
    return mTemporary ? localSettings_temp.sip_password : localSettings.sip_password;
104 andreas 977
}
978
 
979
void TConfig::setSIPpassword(const std::string& pw)
980
{
116 andreas 981
    DECL_TRACER("TConfig::setSIPpassword(const std::string& pw)");
982
 
192 andreas 983
    if (mTemporary)
984
        localSettings_temp.sip_password = pw;
985
    else
986
        localSettings.sip_password = pw;
987
 
988
    mTemporary = false;
104 andreas 989
}
990
 
991
bool TConfig::getSIPstatus()
992
{
116 andreas 993
    DECL_TRACER("TConfig::getSIPstatus()");
994
 
192 andreas 995
    return mTemporary ? localSettings_temp.sip_enabled : localSettings.sip_enabled;
104 andreas 996
}
997
 
127 andreas 998
bool TConfig::getSIPnetworkIPv4()
999
{
1000
    DECL_TRACER("TConfig::getSIPnetworkIPv4()");
1001
 
192 andreas 1002
    return mTemporary ? localSettings_temp.sip_ipv4 : localSettings.sip_ipv4;
127 andreas 1003
}
1004
 
1005
void TConfig::setSIPnetworkIPv4(bool state)
1006
{
1007
    DECL_TRACER("TConfig::setSIPnetworkIPv4(bool state)");
1008
 
192 andreas 1009
    if (mTemporary)
1010
        localSettings_temp.sip_ipv4 = state;
1011
    else
1012
        localSettings.sip_ipv4 = state;
1013
 
1014
    mTemporary = false;
127 andreas 1015
}
1016
 
1017
bool TConfig::getSIPnetworkIPv6()
1018
{
1019
    DECL_TRACER("TConfig::getSIPnetworkIPv6()");
1020
 
192 andreas 1021
    return mTemporary ? localSettings_temp.sip_ipv6 : localSettings.sip_ipv6;
127 andreas 1022
}
1023
 
1024
void TConfig::setSIPnetworkIPv6(bool state)
1025
{
1026
    DECL_TRACER("TConfig::setSIPnetworkIPv6(bool state)");
1027
 
192 andreas 1028
    if (mTemporary)
1029
        localSettings_temp.sip_ipv6 = state;
1030
    else
1031
        localSettings.sip_ipv6 = state;
1032
 
1033
    mTemporary = false;
127 andreas 1034
}
1035
 
139 andreas 1036
void TConfig::setSIPiphone(bool state)
1037
{
1038
    DECL_TRACER("TConfig::setSIPiphone(bool state)");
1039
 
192 andreas 1040
    if (mTemporary)
1041
        localSettings_temp.sip_iphone = state;
1042
    else
1043
        localSettings.sip_iphone = state;
1044
 
1045
    mTemporary = false;
139 andreas 1046
}
1047
 
1048
bool TConfig::getSIPiphone()
1049
{
1050
    DECL_TRACER("TConfig::getSIPiphone()");
1051
 
192 andreas 1052
    return mTemporary ? localSettings_temp.sip_iphone : localSettings.sip_iphone;
139 andreas 1053
}
1054
 
127 andreas 1055
TConfig::SIP_FIREWALL_t TConfig::getSIPfirewall()
1056
{
1057
    DECL_TRACER("TConfig::getSIPfirewall()");
1058
 
192 andreas 1059
    return mTemporary ? localSettings_temp.sip_firewall : localSettings.sip_firewall;
127 andreas 1060
}
1061
 
1062
string TConfig::getSIPfirewallStr()
1063
{
1064
    DECL_TRACER("TConfig::getSIPfirewallStr()");
1065
 
192 andreas 1066
    return sipFirewallToString(mTemporary ? localSettings_temp.sip_firewall : localSettings.sip_firewall);
127 andreas 1067
}
1068
 
1069
void TConfig::setSIPfirewall(TConfig::SIP_FIREWALL_t fw)
1070
{
1071
    DECL_TRACER("TConfig::setSIPfirewall(TConfig::SIP_FIREWALL_t fw)")
1072
 
192 andreas 1073
    if (mTemporary)
1074
        localSettings_temp.sip_firewall = fw;
1075
    else
1076
        localSettings.sip_firewall = fw;
1077
 
1078
    mTemporary = false;
127 andreas 1079
}
1080
 
104 andreas 1081
void TConfig::setSIPstatus(bool state)
1082
{
116 andreas 1083
    DECL_TRACER("TConfig::setSIPstatus(bool state)");
1084
 
192 andreas 1085
    if (mTemporary)
1086
        localSettings_temp.sip_enabled = state;
1087
    else
1088
        localSettings.sip_enabled = state;
1089
 
1090
    mTemporary = false;
104 andreas 1091
}
1092
 
23 andreas 1093
bool TConfig::saveSettings()
1094
{
1095
    DECL_TRACER("TConfig::saveSettings()");
1096
 
292 andreas 1097
    TLock<std::mutex> guard(config_mutex);
1098
 
23 andreas 1099
    try
1100
    {
1101
        string fname = localSettings.path + "/" + localSettings.name;
192 andreas 1102
 
1103
        if (mTemporary)
1104
        {
1105
            localSettings = localSettings_temp;
1106
            MSG_INFO("Temporary settings were copied over.");
1107
        }
1108
 
1109
        MSG_DEBUG("Saving to file " << fname);
23 andreas 1110
        ofstream file(fname);
1111
        string lines = "LogFile=" + localSettings.logFile + "\n";
1112
        lines += "LogLevel=" + localSettings.logLevel + "\n";
1113
        lines += "ProjectPath=" + localSettings.project + "\n";
1114
        lines += string("NoBanner=") + (localSettings.noBanner ? "true" : "false") + "\n";
151 andreas 1115
        lines += string("ToolbarSuppress=") + (localSettings.tbsuppress ? "true" : "false") + "\n";
120 andreas 1116
        lines += string("ToolbarForce=") + (localSettings.tbforce ? "true" : "false") + "\n";
23 andreas 1117
        lines += string("LongFormat=") + (localSettings.longformat ? "true" : "false") + "\n";
1118
        lines += "Address=" + localSettings.server + "\n";
1119
        lines += "Port=" + std::to_string(localSettings.port) + "\n";
1120
        lines += "Channel=" + std::to_string(localSettings.ID) + "\n";
1121
        lines += "System=" + std::to_string(localSettings.system) + "\n";
1122
        lines += "PanelType=" + localSettings.ptype + "\n";
1123
        lines += "Firmware=" + localSettings.version + "\n";
1124
        lines += string("CertCheck=") + (localSettings.certCheck ? "true" : "false") + "\n";
24 andreas 1125
        lines += string("Scale=") + (localSettings.scale ? "true" : "false") + "\n";
35 andreas 1126
        lines += string("Profiling=") + (localSettings.profiling ? "true" : "false") + "\n";
175 andreas 1127
        lines += "MaxButtonCache=" + std::to_string(localSettings.max_cache) + "\n";
51 andreas 1128
        lines += string("Password1=") + localSettings.password1 + "\n";
1129
        lines += string("Password2=") + localSettings.password2 + "\n";
1130
        lines += string("Password3=") + localSettings.password3 + "\n";
1131
        lines += string("Password4=") + localSettings.password4 + "\n";
71 andreas 1132
        lines += string("SystemSoundFile=") + localSettings.systemSound + "\n";
1133
        lines += string("SystemSoundState=") + (localSettings.systemSoundState ? "ON" : "OFF") + "\n";
1134
        lines += string("SystemSingleBeep=") + localSettings.systemSingleBeep + "\n";
1135
        lines += string("SystemDoubleBeep=") + localSettings.systemDoubleBeep + "\n";
141 andreas 1136
        lines += "SystemVolume=" + std::to_string(localSettings.systemVolume) + "\n";
1137
        lines += "SystemGain=" + std::to_string(localSettings.systemGain) + "\n";
134 andreas 1138
        lines += string("SystemRotationFix=") + (localSettings.systemRotationFix ? "ON" : "OFF") + "\n";
1139
        lines += string("UUID=") + localSettings.uuid + "\n";
112 andreas 1140
        // FTP credentials
1141
        lines += string("FTPuser=") + localSettings.ftpUser + "\n";
1142
        lines += string("FTPpassword=") + localSettings.ftpPassword + "\n";
115 andreas 1143
        lines += string("FTPsurface=") + localSettings.ftpSurface + "\n";
116 andreas 1144
        lines += string("FTPpassive=") + (localSettings.ftpPassive ? "true" : "false") + "\n";
115 andreas 1145
        lines += string("FTPdownloadTime=") + std::to_string(localSettings.ftpLastDownload) + "\n";
104 andreas 1146
        // SIP settings
116 andreas 1147
        lines += string("SIP_DOMAIN=") + localSettings.sip_domain + "\n";
127 andreas 1148
        lines += string("SIP_PROXY=") + localSettings.sip_proxy + "\n";
116 andreas 1149
        lines += string("SIP_PORT=") + std::to_string(localSettings.sip_port) + "\n";
127 andreas 1150
        lines += string("SIP_PORTTLS=") + std::to_string(localSettings.sip_portTLS) + "\n";
116 andreas 1151
        lines += string("SIP_STUN=") + localSettings.sip_stun + "\n";
1152
        lines += string("SIP_USER=") + localSettings.sip_user + "\n";
1153
        lines += string("SIP_PASSWORD=") + localSettings.sip_password + "\n";
127 andreas 1154
        lines += string("SIP_IPV4=") + (localSettings.sip_ipv4 ? "true" : "false") + "\n";
1155
        lines += string("SIP_IPV6=") + (localSettings.sip_ipv6 ? "true" : "false") + "\n";
139 andreas 1156
        lines += string("SIP_IPHONE=") + (localSettings.sip_iphone ? "true" : "false") + "\n";
127 andreas 1157
        lines += "SIP_FIREWALL=" + sipFirewallToString(localSettings.sip_firewall) + "\n";
116 andreas 1158
        lines += string("SIP_ENABLED=") + (localSettings.sip_enabled ? "true" : "false") + "\n";
23 andreas 1159
        file.write(lines.c_str(), lines.size());
1160
        file.close();
59 andreas 1161
        MSG_INFO("Actual log level: " << localSettings.logLevel);
214 andreas 1162
 
1163
        if (mTemporary)
1164
        {
1165
            TError::Current()->setLogLevel(localSettings.logLevel);
1166
            TError::Current()->setLogFile(localSettings.logFile);
1167
        }
1168
 
1169
        mTemporary = false;
23 andreas 1170
    }
1171
    catch (std::exception& e)
1172
    {
1173
        MSG_ERROR("Couldn't write configs: " << e.what());
1174
        return false;
1175
    }
1176
 
1177
    return true;
1178
}
1179
 
21 andreas 1180
/**
1181
 * @brief TConfig::isLongFormat defines the format in the logfile.
1182
 *
1183
 * If this returns `true` the format in the logfile is a long format. This
1184
 * means, that in front of each message is an additional timestamp.
1185
 *
1186
 * @return `true` = long format, `false` = short format (default).
1187
 */
2 andreas 1188
bool TConfig::isLongFormat()
1189
{
192 andreas 1190
    return mTemporary ? localSettings_temp.longformat : localSettings.longformat;
2 andreas 1191
}
1192
 
21 andreas 1193
/**
1194
 * @brief TConfig::showBanner defines whether the banner should be showed or not.
1195
 *
1196
 * If this method returns `false` the banner on startup is not displayed.
1197
 *
1198
 * @return `true` = display the banner (default), `false` = show no banner.
1199
 */
2 andreas 1200
bool TConfig::showBanner()
1201
{
116 andreas 1202
    DECL_TRACER("TConfig::showBanner()");
1203
 
192 andreas 1204
    return mTemporary ? (!localSettings_temp.noBanner) : (!localSettings.noBanner);
2 andreas 1205
}
1206
 
21 andreas 1207
/**
24 andreas 1208
 * @brief TConfig::getScale returns the scale setting
1209
 *
1210
 * If this is set to TRUE, all images are scaled to fit the screen.
1211
 *
1212
 * @return scale state
1213
 */
1214
bool TConfig::getScale()
1215
{
116 andreas 1216
    DECL_TRACER("TConfig::getScale()");
1217
 
192 andreas 1218
    return mTemporary ? localSettings_temp.scale : localSettings.scale;
24 andreas 1219
}
1220
 
120 andreas 1221
bool TConfig::getToolbarForce()
1222
{
1223
    DECL_TRACER("TConfig::getToolbarForce()");
1224
 
192 andreas 1225
    return mTemporary ? localSettings_temp.tbforce : localSettings.tbforce;
120 andreas 1226
}
1227
 
151 andreas 1228
bool TConfig::getToolbarSuppress()
1229
{
1230
    DECL_TRACER("TConfig::getToolbarSuppress()");
1231
 
192 andreas 1232
    return mTemporary ? localSettings_temp.tbsuppress : localSettings.tbsuppress;
151 andreas 1233
}
1234
 
35 andreas 1235
bool TConfig::getProfiling()
1236
{
192 andreas 1237
    return mTemporary ? localSettings_temp.profiling : localSettings.profiling;
35 andreas 1238
}
1239
 
175 andreas 1240
size_t TConfig::getButttonCache()
1241
{
192 andreas 1242
    if (mTemporary && localSettings_temp.max_cache > 0)
1243
        return localSettings_temp.max_cache;
1244
 
1245
    if (!mTemporary && localSettings.max_cache > 0)
177 andreas 1246
        return localSettings.max_cache * 1000 * 1000;
1247
 
1248
    return 0;
175 andreas 1249
}
1250
 
51 andreas 1251
string & TConfig::getPassword1()
1252
{
116 andreas 1253
    DECL_TRACER("TConfig::getPassword1()");
1254
 
192 andreas 1255
    return mTemporary ? localSettings_temp.password1 : localSettings.password1;
51 andreas 1256
}
1257
 
1258
string & TConfig::getPassword2()
1259
{
116 andreas 1260
    DECL_TRACER("TConfig::getPassword2()");
1261
 
192 andreas 1262
    return mTemporary ? localSettings_temp.password2 : localSettings.password2;
51 andreas 1263
}
1264
 
1265
string & TConfig::getPassword3()
1266
{
116 andreas 1267
    DECL_TRACER("TConfig::getPassword3()");
1268
 
192 andreas 1269
    return mTemporary ? localSettings_temp.password3 : localSettings.password3;
51 andreas 1270
}
1271
 
1272
string & TConfig::getPassword4()
1273
{
116 andreas 1274
    DECL_TRACER("TConfig::getPassword4()");
1275
 
192 andreas 1276
    return mTemporary ? localSettings_temp.password4 : localSettings.password4;
51 andreas 1277
}
1278
 
71 andreas 1279
string & TConfig::getSystemSound()
1280
{
116 andreas 1281
    DECL_TRACER("TConfig::getSystemSound()");
1282
 
192 andreas 1283
    return mTemporary ? localSettings_temp.systemSound : localSettings.systemSound;
71 andreas 1284
}
1285
 
1286
bool TConfig::getSystemSoundState()
1287
{
116 andreas 1288
    DECL_TRACER("TConfig::getSystemSoundState()");
1289
 
192 andreas 1290
    return mTemporary ? localSettings_temp.systemSoundState : localSettings.systemSoundState;
71 andreas 1291
}
1292
 
141 andreas 1293
int TConfig::getSystemVolume()
1294
{
1295
    DECL_TRACER("TConfig::getSystemVolume()");
1296
 
192 andreas 1297
    return mTemporary ? localSettings_temp.systemVolume : localSettings.systemVolume;
141 andreas 1298
}
1299
 
1300
int TConfig::getSystemGain()
1301
{
1302
    DECL_TRACER("TConfig::getSystemGain()");
1303
 
192 andreas 1304
    return mTemporary ? localSettings_temp.systemGain : localSettings.systemGain;
141 andreas 1305
}
1306
 
134 andreas 1307
bool TConfig::getRotationFixed()
1308
{
1309
    DECL_TRACER("TConfig::getRotationFixed()");
1310
 
192 andreas 1311
    return mTemporary ? localSettings_temp.systemRotationFix : localSettings.systemRotationFix;
134 andreas 1312
}
1313
 
1314
void TConfig::setRotationFixed(bool fix)
1315
{
1316
    DECL_TRACER("TConfig::setRotationFixed(bool fix)");
1317
 
192 andreas 1318
    if (mTemporary)
1319
        localSettings_temp.systemRotationFix = fix;
1320
    else
1321
        localSettings.systemRotationFix = fix;
1322
 
1323
    mTemporary = false;
134 andreas 1324
}
1325
 
1326
void TConfig::setSystemChannel(int ch)
1327
{
1328
    DECL_TRACER("TConfig::setSystemChannel(int ch)");
1329
 
1330
    if (ch >= 10000 && ch < 30000)
1331
        mChannel = ch;
1332
}
1333
 
71 andreas 1334
string& TConfig::getSingleBeepSound()
1335
{
116 andreas 1336
    DECL_TRACER("TConfig::getSingleBeepSound()");
1337
 
192 andreas 1338
    return mTemporary ? localSettings_temp.systemSingleBeep : localSettings.systemSingleBeep;
71 andreas 1339
}
1340
 
1341
string& TConfig::getDoubleBeepSound()
1342
{
116 andreas 1343
    DECL_TRACER("TConfig::getDoubleBeepSound()");
1344
 
192 andreas 1345
    return mTemporary ? localSettings_temp.systemDoubleBeep : localSettings.systemDoubleBeep;
71 andreas 1346
}
1347
 
134 andreas 1348
string& TConfig::getUUID()
1349
{
1350
    DECL_TRACER("TConfig::getUUID()");
1351
 
192 andreas 1352
    return mTemporary ? localSettings_temp.uuid : localSettings.uuid;
134 andreas 1353
}
1354
 
112 andreas 1355
string& TConfig::getFtpUser()
1356
{
116 andreas 1357
    DECL_TRACER("TConfig::getFtpUser()");
1358
 
192 andreas 1359
    return mTemporary ? localSettings_temp.ftpUser : localSettings.ftpUser;
112 andreas 1360
}
1361
 
1362
string& TConfig::getFtpPassword()
1363
{
116 andreas 1364
    DECL_TRACER("TConfig::getFtpPassword()");
1365
 
192 andreas 1366
    return mTemporary ? localSettings_temp.ftpPassword : localSettings.ftpPassword;
112 andreas 1367
}
1368
 
115 andreas 1369
string& TConfig::getFtpSurface()
1370
{
116 andreas 1371
    DECL_TRACER("TConfig::getFtpSurface()");
1372
 
192 andreas 1373
    return mTemporary ? localSettings_temp.ftpSurface : localSettings.ftpSurface;
115 andreas 1374
}
1375
 
116 andreas 1376
bool TConfig::getFtpPassive()
1377
{
1378
    DECL_TRACER("TConfig::getFtpPassive()");
1379
 
192 andreas 1380
    return mTemporary ? localSettings_temp.ftpPassive : localSettings.ftpPassive;
116 andreas 1381
}
1382
 
115 andreas 1383
time_t TConfig::getFtpDownloadTime()
1384
{
116 andreas 1385
    DECL_TRACER("TConfig::getFtpDownloadTime()");
1386
 
192 andreas 1387
    return mTemporary ? localSettings_temp.ftpLastDownload : localSettings.ftpLastDownload;
115 andreas 1388
}
1389
 
24 andreas 1390
/**
21 andreas 1391
 * @brief TConfig::certCheck check the certificate if the connection is encrypted.
1392
 *
1393
 * Currently not implemented!
1394
 *
1395
 * @return `true` = check the certificate, `false` = accept any certificate (default).
1396
 */
1397
bool TConfig::certCheck()
1398
{
192 andreas 1399
    return mTemporary ? localSettings_temp.certCheck : localSettings.certCheck;
21 andreas 1400
}
1401
 
1402
/**
59 andreas 1403
 * @brief TConfig::logLevelStrToBits
1404
 * Converts a string containing one or more loglevels into a bit field.
1405
 * @param level A string containing the log levels
1406
 * @return A bit field representing the log levels.
1407
 */
1408
uint TConfig::logLevelStrToBits(const string& level)
1409
{
1410
    uint l = 0;
1411
 
1412
    if (level.find("INFO") != string::npos)
1413
        l |= HLOG_INFO;
1414
 
1415
    if (level.find("WARNING") != string::npos)
1416
        l |= HLOG_WARNING;
1417
 
1418
    if (level.find("ERROR") != string::npos)
1419
        l |= HLOG_ERROR;
1420
 
1421
    if (level.find("TRACE") != string::npos)
1422
        l |= HLOG_TRACE;
1423
 
1424
    if (level.find("DEBUG") != string::npos)
1425
        l |= HLOG_DEBUG;
1426
 
1427
    if (level.find("PROTOCOL") != string::npos)
71 andreas 1428
        l |= HLOG_PROTOCOL;
59 andreas 1429
 
1430
    if (level.find("ALL") != string::npos)
1431
        l = HLOG_ALL;
1432
 
1433
    return l;
1434
}
1435
 
1436
string TConfig::logLevelBitsToString(uint level)
1437
{
1438
    string l;
1439
 
1440
    if (level == 0)
1441
    {
1442
        l = SLOG_NONE;
1443
        return l;
1444
    }
1445
 
1446
    if (level & HLOG_INFO)
1447
    {
1448
        if (l.length() > 0)
1449
            l.append("|");
1450
 
1451
        l.append(SLOG_INFO);
1452
    }
1453
 
1454
    if (level & HLOG_WARNING)
1455
    {
1456
        if (l.length() > 0)
1457
            l.append("|");
1458
 
1459
        l.append(SLOG_WARNING);
1460
    }
1461
 
1462
    if (level & HLOG_ERROR)
1463
    {
1464
        if (l.length() > 0)
1465
            l.append("|");
1466
 
1467
        l.append(SLOG_ERROR);
1468
    }
1469
 
1470
    if (level & HLOG_TRACE)
1471
    {
1472
        if (l.length() > 0)
1473
            l.append("|");
1474
 
1475
        l.append(SLOG_TRACE);
1476
    }
1477
 
1478
    if (level & HLOG_DEBUG)
1479
    {
1480
        if (l.length() > 0)
1481
            l.append("|");
1482
 
1483
        l.append(SLOG_DEBUG);
1484
    }
1485
 
1486
    return l;
1487
}
118 andreas 1488
 
127 andreas 1489
string TConfig::sipFirewallToString(TConfig::SIP_FIREWALL_t fw)
1490
{
1491
    switch(fw)
1492
    {
1493
        case SIP_NO_FIREWALL:   return "SIP_NO_FIREWALL";
1494
        case SIP_NAT_ADDRESS:   return "SIP_NAT_ADDRESS";
1495
        case SIP_STUN:          return "SIP_STUN";
1496
        case SIP_ICE:           return "SIP_ICE";
1497
        case SIP_UPNP:          return "SIP_UPNP";
1498
    }
140 andreas 1499
 
1500
    return string();
127 andreas 1501
}
1502
 
1503
TConfig::SIP_FIREWALL_t TConfig::sipFirewallStrToEnum(const std::string& str)
1504
{
1505
    if (strCaseCompare(str, "SIP_NO_FIREWALL") == 0)
1506
        return SIP_NO_FIREWALL;
1507
    else if (strCaseCompare(str, "SIP_NAT_ADDRESS") == 0)
1508
        return SIP_NAT_ADDRESS;
1509
    else if (strCaseCompare(str, "SIP_STUN") == 0)
1510
        return SIP_STUN;
1511
    else if (strCaseCompare(str, "SIP_ICE") == 0)
1512
        return SIP_ICE;
1513
    else if (strCaseCompare(str, "SIP_UPNP") == 0)
1514
        return SIP_UPNP;
1515
 
1516
    return SIP_NO_FIREWALL;
1517
}
1518
 
118 andreas 1519
string TConfig::makeConfigDefault(const std::string& log, const std::string& project)
1520
{
1521
    string content = "LogFile=" + log + "\n";
365 andreas 1522
#ifndef NDEBUG
1523
    content += "LogLevel=ALL\n";
131 andreas 1524
#else
1525
    content += "LogLevel=NONE\n";
1526
#endif
118 andreas 1527
    content += "ProjectPath=" + project + "\n";
1528
    content += "LongFormat=false\n";
1529
    content += "Address=0.0.0.0\n";
1530
    content += "Port=1319\n";
1531
    content += "Channel=10001\n";
1532
    content += "PanelType=Android\n";
1533
    content += string("Firmware=") + VERSION_STRING() + "\n";
1534
    content += "Scale=true\n";
134 andreas 1535
    content += "ToolbarForce=true\n";
118 andreas 1536
    content += "Profiling=false\n";
1537
    content += "Password1=1988\n";
1538
    content += "Password2=1988\n";
1539
    content += "Password3=1988\n";
1540
    content += "Password4=1988\n";
1541
    content += "SystemSoundFile=singleBeep.wav\n";
1542
    content += "SystemSoundState=ON\n";
1543
    content += "SystemSingleBeep=singleBeep01.wav\n";
1544
    content += "SystemDoubleBeep=doubleBeep01.wav\n";
134 andreas 1545
    content += "SystemRotationFix=" + string(localSettings.systemRotationFix ? "TRUE" : "FALSE") + "\n";
1546
    content += "UUID=" + localSettings.uuid + "\n";
118 andreas 1547
    content += "FTPuser=administrator\n";
1548
    content += "FTPpassword=password\n";
1549
    content += "FTPsurface=tpanel.tp4\n";
1550
    content += "FTPpassive=true\n";
1551
    content += "FTPdownloadTime=0\n";
127 andreas 1552
    content += "SIP_PROXY=" + localSettings.sip_proxy + "\n";
118 andreas 1553
    content += "SIP_PORT=" + std::to_string(localSettings.sip_port) + "\n";
127 andreas 1554
    content += "SIP_PORTTLS=" + std::to_string(localSettings.sip_portTLS) + "\n";
1555
    content += "SIP_STUN=" + localSettings.sip_stun + "\n";
1556
    content += "SIP_DOMAIN=" + localSettings.sip_domain + "\n";
1557
    content += "SIP_USER=" + localSettings.sip_user + "\n";
1558
    content += "SIP_PASSWORD=" + localSettings.sip_password + "\n";
1559
    content += "SIP_IPV4=" + string(localSettings.sip_ipv4 ? "TRUE" : "FALSE") + "\n";
139 andreas 1560
    content += "SIP_IPV6=" + string(localSettings.sip_ipv6 ? "TRUE" : "FALSE") + "\n";
1561
    content += "SIP_IPHONE=" + string(localSettings.sip_iphone ? "TRUE" : "FALSE") + "\n";
127 andreas 1562
    content += "SIP_FIREWALL=" + sipFirewallToString(localSettings.sip_firewall) + "\n";
1563
    content += "SIP_ENABLED=" + string(localSettings.sip_ipv6 ? "TRUE" : "FALSE") + "\n";
118 andreas 1564
 
1565
    return content;
1566
}
1567
 
59 andreas 1568
/**
21 andreas 1569
 * @brief TConfig::findConfig search for the location of the configuration file.
1570
 *
1571
 * If there was no configuration file given on the command line, this method
1572
 * searches for a configuration file on a few standard directories. This are:
1573
 *
1574
 *     /etc/tpanel.conf
1575
 *     /etc/tpanel/tpanel.conf
1576
 *     /usr/etc/tpanel.conf
1577
 *     $HOME/.tpanel.conf
1578
 *
1579
 * On macOS the following additional directories are searched:
1580
 *
1581
 *     /opt/local/etc/tpanel.conf
1582
 *     /opt/local/etc/tpanel/tpanel.conf
1583
 *     /opt/local/usr/etc/tpanel.conf
1584
 *
1585
 * @return On success `true`, otherwise `false`.
1586
 */
2 andreas 1587
bool TConfig::findConfig()
1588
{
292 andreas 1589
    TLock<std::mutex> guard(config_mutex);
1590
 
90 andreas 1591
    char *HOME = nullptr;
1592
    string sFileName;
22 andreas 1593
 
90 andreas 1594
    if (!mPath.empty())
1595
    {
1596
        size_t pos = mPath.find_last_of("/");
2 andreas 1597
 
90 andreas 1598
        if (pos != string::npos)
1599
        {
1600
            localSettings.path = mPath.substr(0, pos);
1601
            localSettings.name = mPath.substr(pos+1);
192 andreas 1602
            localSettings_temp.path = localSettings.path;
1603
            localSettings_temp.name = localSettings.name;
90 andreas 1604
            mCFile = mPath;
1605
            return !mCFile.empty();
1606
        }
2 andreas 1607
 
192 andreas 1608
        localSettings.path = ".";
90 andreas 1609
        localSettings.name = mPath;
192 andreas 1610
        localSettings_temp.path = localSettings.path;
1611
        localSettings_temp.name = localSettings.name;
90 andreas 1612
        mCFile = mPath;
1613
        return !mCFile.empty();
1614
    }
2 andreas 1615
 
90 andreas 1616
    localSettings.name = "tpanel.conf";
51 andreas 1617
    localSettings.password1 = "1988";
1618
    localSettings.password2 = "1988";
1619
    localSettings.password3 = "1988";
1620
    localSettings.password4 = "1988";
71 andreas 1621
    localSettings.systemSound = "singleBeep.wav";
1622
    localSettings.systemSoundState = true;
1623
    localSettings.systemSingleBeep = "singleBeep01.wav";
1624
    localSettings.systemDoubleBeep = "doubleBeep01.wav";
112 andreas 1625
    localSettings.ftpUser = "administrator";
1626
    localSettings.ftpPassword = "password";
115 andreas 1627
    localSettings.ftpSurface = "tpanel.tp4";
21 andreas 1628
#ifdef __ANDROID__
22 andreas 1629
    std::stringstream s;
1630
    TValidateFile vf;
1631
    HOME = getenv("HOME");
2 andreas 1632
 
22 andreas 1633
    if (!HOME || !*HOME)
21 andreas 1634
    {
43 andreas 1635
        s << "Error: Environment variable HOME does not exist!";
22 andreas 1636
        __android_log_print(ANDROID_LOG_ERROR, "tpanel", "%s", s.str().c_str());
1637
        TError::setErrorMsg(s.str());
21 andreas 1638
        return false;
1639
    }
1640
 
22 andreas 1641
    localSettings.path = HOME;
1642
 
88 andreas 1643
    if (!vf.isValidDir(localSettings.path))
21 andreas 1644
    {
22 andreas 1645
        s << "Error: Path " << localSettings.path << " does not exist!";
1646
        __android_log_print(ANDROID_LOG_ERROR, "tpanel", "%s", s.str().c_str());
1647
        TError::setErrorMsg(s.str());
21 andreas 1648
        return false;
1649
    }
1650
 
22 andreas 1651
    sFileName = localSettings.path + "/" + localSettings.name;
21 andreas 1652
 
22 andreas 1653
    if (access(sFileName.c_str(), F_OK) == -1)    // Does the configuration file exist?
21 andreas 1654
    {                                       // No, than create it and also the directory structure
1655
        try
1656
        {
1657
            ofstream cfg(sFileName);
1658
 
118 andreas 1659
            string content = makeConfigDefault(localSettings.path + "/tpanel.log", localSettings.path + "/tpanel");
21 andreas 1660
            cfg.write(content.c_str(), content.size());
1661
            cfg.close();
1662
 
1663
            string path = localSettings.path + "/tpanel";
22 andreas 1664
            TTPInit init(path);
21 andreas 1665
        }
1666
        catch (std::exception& e)
1667
        {
23 andreas 1668
            s << "Error: " << e.what();
22 andreas 1669
            __android_log_print(ANDROID_LOG_ERROR, "tpanel" , "%s", s.str().c_str());
21 andreas 1670
            TError::setErrorMsg(TERRERROR, string("Error: ") + e.what());
1671
            return false;
1672
        }
1673
    }
1674
#else
90 andreas 1675
    if (!(HOME = getenv("HOME")))
116 andreas 1676
        MSG_ERROR("TConfig::findConfig: No environment variable HOME!");
2 andreas 1677
 
90 andreas 1678
    vector<string>::iterator iter;
1679
    bool found = false;
2 andreas 1680
 
90 andreas 1681
    for (iter = mCfgPaths.begin(); iter != mCfgPaths.end(); ++iter)
1682
    {
1683
        string f = *iter + "/tpanel.conf";
1684
 
1685
        if (!access(f.c_str(), R_OK))
1686
        {
1687
            sFileName = f;
1688
            localSettings.path = *iter;
113 andreas 1689
            found = true;
90 andreas 1690
            break;
1691
        }
1692
    }
1693
 
1694
    // The local configuration file has precedence over the global one.
1695
    if (HOME)
1696
    {
1697
        string f = HOME;
113 andreas 1698
#ifndef __ANDROID__
90 andreas 1699
        f += "/.tpanel.conf";
113 andreas 1700
#else
1701
        f += "/tpanel.conf";
1702
#endif
90 andreas 1703
        if (!access(f.data(), R_OK))
1704
        {
1705
            sFileName = f;
1706
            localSettings.path = HOME;
1707
            found = true;
1708
        }
1709
    }
1710
 
192 andreas 1711
    localSettings_temp = localSettings;
1712
 
90 andreas 1713
    if (!found)
1714
    {
118 andreas 1715
        MSG_WARNING("This seems to be the first start because of missing configuration file. Will try to create a default one ...");
1716
 
1717
        if (HOME)
1718
        {
1719
            localSettings.path = HOME;
1720
            sFileName = string(HOME) + "/.tpanel.conf";
1721
 
1722
            try
1723
            {
1724
                ofstream cfg(sFileName);
1725
 
1726
                string content = makeConfigDefault(localSettings.path + "/tpanel.log", localSettings.path + "/tpanel");
1727
                cfg.write(content.c_str(), content.size());
1728
                cfg.close();
1729
 
192 andreas 1730
                localSettings_temp = localSettings;
118 andreas 1731
                string path = localSettings.path + "/tpanel";
1732
                TTPInit init(path);
1733
            }
1734
            catch (std::exception& e)
1735
            {
1736
                cerr << "Error: " << e.what();
1737
                TError::setErrorMsg(TERRERROR, string("Error: ") + e.what());
1738
                return false;
1739
            }
1740
        }
1741
        else
1742
        {
1743
            MSG_ERROR("TConfig::findConfig: Can't find any configuration file!");
1744
            TError::setError();
1745
            sFileName.clear();
1746
            localSettings.name.clear();
1747
            localSettings.path.clear();
192 andreas 1748
            localSettings_temp = localSettings;
118 andreas 1749
        }
90 andreas 1750
    }
21 andreas 1751
#endif
90 andreas 1752
    mCFile = sFileName;
1753
    return !sFileName.empty();
2 andreas 1754
}
1755
 
21 andreas 1756
/**
1757
 * @brief TConfig::readConfig reads a config file.
1758
 *
1759
 * This method reads a config file and stores the known options into the
1760
 * struct localSettings.
1761
 *
1762
 * @return `true` on success.\n
1763
 * Returns `false` on error and sets the internal error.
1764
 */
2 andreas 1765
bool TConfig::readConfig()
1766
{
240 andreas 1767
#if defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_SIMULATOR)
239 andreas 1768
    QASettings settings;
1769
    localSettings.path = QASettings::getLibraryPath().toStdString();
1770
    localSettings.project = localSettings.path + "/tpanel";
242 andreas 1771
    localSettings.version = "1.0";
1772
    localSettings.ptype = "iPhone";
1773
    localSettings.max_cache = 100;
1774
    localSettings.logLevel = SLOG_NONE;
1775
    localSettings.logLevelBits = HLOG_NONE;
239 andreas 1776
    mkdir(localSettings.project.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
1777
    localSettings.name = "tpanel.cfg";
1778
    localSettings.scale = true;
1779
    localSettings.tbforce = false;
1780
    localSettings.tbsuppress = false;
1781
 
1782
    // Settings for NetLinx
1783
    settings.registerDefaultPrefs();
1784
    localSettings.server = settings.getNetlinxIP().toStdString();
1785
    localSettings.port = settings.getNetlinxPort();
1786
    localSettings.ID = settings.getNetlinxChannel();
1787
    localSettings.ptype = settings.getNetlinxPanelType().toStdString();
1788
    localSettings.ftpUser = settings.getFTPUser().toStdString();
1789
    localSettings.ftpPassword = settings.getFTPPassword().toStdString();
1790
    localSettings.ftpSurface = settings.getNetlinxSurface().toStdString();
1791
 
1792
    // Settings for SIP
1793
    localSettings.sip_proxy = settings.getSipProxy().toStdString();
1794
    localSettings.sip_port = settings.getSipNetworkPort();
1795
    localSettings.sip_portTLS = settings.getSipNetworkTlsPort();
1796
    localSettings.sip_stun = settings.getSipStun().toStdString();
1797
    localSettings.sip_domain = settings.getSipDomain().toStdString();
1798
    localSettings.sip_ipv4 = settings.getSipNetworkIpv4();
1799
    localSettings.sip_ipv6 = settings.getSipNetworkIpv6();
1800
    localSettings.sip_user = settings.getSipUser().toStdString();
1801
    localSettings.sip_password = settings.getSipPassword().toStdString();
1802
    localSettings.sip_enabled = settings.getSipEnabled();
1803
    localSettings.sip_iphone = settings.getSipIntegratedPhone();
1804
 
1805
    // Settings for View
1806
    localSettings.scale = settings.getViewScale();
1807
    localSettings.tbforce = settings.getViewToolbarForce();
1808
    localSettings.tbsuppress = !settings.getViewToolbarVisible();
1809
 
1810
    if (localSettings.tbforce)
1811
        localSettings.tbsuppress = false;
1812
 
1813
    localSettings.systemRotationFix = settings.getViewRotation();
1814
 
1815
    // Settings for sound
1816
    localSettings.systemSound = settings.getSoundSystem().toStdString();
1817
    localSettings.systemSingleBeep = settings.getSoundSingleBeep().toStdString();
1818
    localSettings.systemDoubleBeep = settings.getSoundDoubleBeep().toStdString();
1819
    localSettings.systemSoundState = settings.getSoundEnabled();
1820
    localSettings.systemVolume = settings.getSoundVolume();
1821
    localSettings.systemGain = settings.getSoundGain();
1822
 
1823
    // Settings for logging
1824
    unsigned int logLevel = 0;
1825
 
1826
    if (settings.getLoggingInfo())
1827
        logLevel |= HLOG_INFO;
1828
 
1829
    if (settings.getLoggingWarning())
1830
        logLevel |= HLOG_WARNING;
1831
 
1832
    if (settings.getLoggingError())
1833
        logLevel |= HLOG_ERROR;
1834
 
1835
    if (settings.getLoggingDebug())
1836
        logLevel |= HLOG_DEBUG;
1837
 
1838
    if (settings.getLoggingTrace())
1839
        logLevel |= HLOG_TRACE;
1840
 
1841
    if (settings.getLoggingProfile())
1842
        localSettings.profiling = true;
1843
    else
1844
        localSettings.profiling = false;
1845
 
1846
    if (settings.getLoggingLogFormat())
1847
        localSettings.longformat = true;
1848
    else
1849
        localSettings.longformat = false;
1850
 
1851
    localSettings.logLevelBits = logLevel;
1852
    localSettings.logLevel = logLevelBitsToString(logLevel);
243 andreas 1853
    TStreamError::setLogLevel(localSettings.logLevel);
239 andreas 1854
 
242 andreas 1855
    if (settings.getLoggingLogfileEnabled())
1856
    {
1857
        string fname = settings.getLoggingLogfile().toStdString();
1858
 
1859
        if (!fname.empty())
1860
        {
1861
            localSettings.logFile = QASettings::getDocumentPath().toStdString() + "/" + fname;
243 andreas 1862
            TStreamError::setLogFile(localSettings.logFile);
242 andreas 1863
        }
1864
    }
1865
    else
1866
        localSettings.logFile.clear();
1867
 
239 andreas 1868
    if (localSettings.uuid.empty())
1869
    {
1870
        uuid_t uuid;
1871
        char sUUID[256];
1872
 
1873
        uuid_generate_random(uuid);
1874
        uuid_unparse_lower(uuid, sUUID);
1875
        localSettings.uuid.assign(sUUID);
1876
        localSettings_temp = localSettings;
1877
    }
1878
 
1879
    mInitialized = true;
1880
 
242 andreas 1881
    if (IS_LOG_DEBUG())
1882
    {
1883
        cout << "Selected Parameters:" << endl;
1884
        cout << "    Path to cfg.: " << localSettings.path << endl;
1885
        cout << "    Name of cfg.: " << localSettings.name << endl;
1886
        cout << "    Logfile use:  " << (settings.getLoggingLogfileEnabled() ? "YES": "NO") << endl;
1887
        cout << "    Logfile:      " << localSettings.logFile  << endl;
1888
        cout << "    LogLevel:     " << localSettings.logLevel  << endl;
1889
        cout << "    Long format:  " << (localSettings.longformat ? "YES" : "NO")  << endl;
1890
        cout << "    Project path: " << localSettings.project  << endl;
1891
        cout << "    Controller:   " << localSettings.server  << endl;
1892
        cout << "    Port:         " << localSettings.port  << endl;
1893
        cout << "    Channel:      " << localSettings.ID  << endl;
1894
        cout << "    Panel type:   " << localSettings.ptype  << endl;
1895
        cout << "    Firmware ver. " << localSettings.version  << endl;
1896
        cout << "    Scaling:      " << (localSettings.scale ? "YES" : "NO")  << endl;
1897
        cout << "    Profiling:    " << (localSettings.profiling ? "YES" : "NO")  << endl;
1898
        cout << "    Button cache: " << localSettings.max_cache  << endl;
1899
        cout << "    System Sound: " << localSettings.systemSound  << endl;
1900
        cout << "    Sound state:  " << (localSettings.systemSoundState ? "ACTIVATED" : "DEACTIVATED")  << endl;
1901
        cout << "    Single beep:  " << localSettings.systemSingleBeep  << endl;
1902
        cout << "    Double beep:  " << localSettings.systemDoubleBeep  << endl;
1903
        cout << "    Volume:       " << localSettings.systemVolume  << endl;
1904
        cout << "    Gain:         " << localSettings.systemGain  << endl;
1905
        cout << "    Rotation:     " << (localSettings.systemRotationFix ? "LOCKED" : "UNLOCKED")  << endl;
1906
        cout << "    UUID:         " << localSettings.uuid  << endl;
1907
        cout << "    FTP user:     " << localSettings.ftpUser  << endl;
1908
        cout << "    FTP password: " << localSettings.ftpPassword  << endl;
1909
        cout << "    FTP surface:  " << localSettings.ftpSurface  << endl;
1910
        cout << "    FTP passive:  " << (localSettings.ftpPassive ? "YES" : "NO")  << endl;
1911
        cout << "    FTP dl. time: " << localSettings.ftpLastDownload  << endl;
1912
        cout << "    SIP proxy:    " << localSettings.sip_proxy  << endl;
1913
        cout << "    SIP port:     " << localSettings.sip_port  << endl;
1914
        cout << "    SIP TLS port: " << localSettings.sip_portTLS  << endl;
1915
        cout << "    SIP STUN:     " << localSettings.sip_stun  << endl;
1916
        cout << "    SIP doamain:  " << localSettings.sip_domain  << endl;
1917
        cout << "    SIP user:     " << localSettings.sip_user  << endl;
1918
        cout << "    SIP IPv4:     " << (localSettings.sip_ipv4 ? "YES" : "NO")  << endl;
1919
        cout << "    SIP IPv6:     " << (localSettings.sip_ipv6 ? "YES" : "NO")  << endl;
1920
        cout << "    SIP Int.Phone:" << (localSettings.sip_iphone ? "YES" : "NO")  << endl;
1921
        cout << "    SIP firewall: " << sipFirewallToString(localSettings.sip_firewall)  << endl;
1922
        cout << "    SIP enabled:  " << (localSettings.sip_enabled ? "YES" : "NO")  << endl;
1923
    }
239 andreas 1924
#else
35 andreas 1925
    ifstream fs;
2 andreas 1926
 
192 andreas 1927
    mTemporary = false;
35 andreas 1928
    // First initialize the defaults
1929
    localSettings.ID = 0;
1930
    localSettings.port = 1397;
1931
    localSettings.ptype = "android";
1932
    localSettings.version = "1.0";
1933
    localSettings.longformat = false;
43 andreas 1934
    localSettings.profiling = false;
175 andreas 1935
#ifdef __ANDROID__
1936
    localSettings.max_cache = 100;
1937
#else
1938
    localSettings.max_cache = 400;
1939
#endif
71 andreas 1940
    localSettings.systemSoundState = true;
1941
    localSettings.systemSound = "singleBeep.wav";
1942
    localSettings.systemSingleBeep = "singleBeep01.wav";
1943
    localSettings.systemDoubleBeep = "doubleBeep01.wav";
112 andreas 1944
    localSettings.ftpUser = "administrator";
1945
    localSettings.ftpPassword = "password";
115 andreas 1946
    localSettings.ftpSurface = "tpanel.tp4";
116 andreas 1947
    localSettings.sip_port = 5060;
127 andreas 1948
    localSettings.sip_portTLS = 0;
2 andreas 1949
 
35 andreas 1950
    // Now get the settings from file
1951
    try
1952
    {
1953
        fs.open(mCFile.c_str(), fstream::in);
1954
    }
1955
    catch (const fstream::failure e)
1956
    {
192 andreas 1957
        localSettings_temp = localSettings;
116 andreas 1958
        cerr << "TConfig::readConfig: Error on file " << mCFile << ": " << e.what() << endl;
35 andreas 1959
        TError::setError();
1960
        return false;
1961
    }
2 andreas 1962
 
35 andreas 1963
    for (string line; getline(fs, line);)
1964
    {
1965
        size_t pos;
2 andreas 1966
 
35 andreas 1967
        if ((pos = line.find("#")) != string::npos)
1968
        {
1969
            if (pos == 0)
1970
                line.clear();
1971
            else
1972
                line = line.substr(0, pos);
1973
        }
2 andreas 1974
 
35 andreas 1975
        if (line.empty() || line.find("=") == string::npos)
1976
            continue;
2 andreas 1977
 
35 andreas 1978
        vector<string> parts = split(line, "=", true);
2 andreas 1979
 
35 andreas 1980
        if (parts.size() == 2)
1981
        {
1982
            string left = parts[0];
1983
            string right = ltrim(parts[1]);
2 andreas 1984
 
35 andreas 1985
            if (caseCompare(left, "PORT") == 0 && !right.empty())
1986
                localSettings.port = atoi(right.c_str());
1987
            else if (caseCompare(left, "LOGFILE") == 0 && !right.empty())
1988
            {
1989
                localSettings.logFile = right;
23 andreas 1990
                TStreamError::setLogFileOnly(right);
35 andreas 1991
            }
1992
            else if (caseCompare(left, "LOGLEVEL") == 0 && !right.empty())
1993
            {
1994
                TStreamError::setLogLevel(right);
1995
                localSettings.logLevel = right;
59 andreas 1996
                localSettings.logLevelBits = logLevelStrToBits(right);
35 andreas 1997
            }
1998
            else if (caseCompare(left, "ProjectPath") == 0 && !right.empty())
43 andreas 1999
            {
35 andreas 2000
                localSettings.project = right;
43 andreas 2001
#ifdef __ANDROID__
2002
                TValidateFile vf;
2003
 
2004
                if (!vf.isValidFile(right))
2005
                {
2006
                    char *HOME = getenv("HOME");
2007
 
2008
                    if (HOME)
2009
                    {
2010
                        localSettings.project = HOME;
2011
                        localSettings.project += "/tpanel";
2012
                    }
2013
                }
2014
#endif
2015
            }
11 andreas 2016
            else if (caseCompare(left, "System") == 0 && !right.empty())
2017
                localSettings.system = atoi(right.c_str());
2018
            else if (caseCompare(left, "PanelType") == 0 && !right.empty())
35 andreas 2019
                localSettings.ptype = right;
2020
            else if (caseCompare(left, "Address") == 0 && !right.empty())
2021
                localSettings.server = right;
2022
            else if (caseCompare(left, "Firmware") == 0 && !right.empty())
2023
                localSettings.version = right;
2024
            else if (caseCompare(left, "LongFormat") == 0 && !right.empty())
71 andreas 2025
                localSettings.longformat = isTrue(right);
35 andreas 2026
            else if (caseCompare(left, "NoBanner") == 0 && !right.empty())
71 andreas 2027
                localSettings.noBanner = isTrue(right);
35 andreas 2028
            else if (caseCompare(left, "CertCheck") == 0 && !right.empty())
71 andreas 2029
                localSettings.certCheck = isTrue(right);
21 andreas 2030
            else if (caseCompare(left, "Channel") == 0 && !right.empty())
35 andreas 2031
            {
2032
                localSettings.ID = atoi(right.c_str());
2 andreas 2033
 
113 andreas 2034
                if (localSettings.ID < 10000 || localSettings.ID >= 29000)
35 andreas 2035
                {
260 andreas 2036
#ifdef __ANDROID__
2037
                    __android_log_print(ANDROID_LOG_ERROR, "tpanel", "TConfig::readConfig: Invalid port number %s", right.c_str());
2038
#else
116 andreas 2039
                    cerr << "TConfig::readConfig: Invalid port number " << right << endl;
260 andreas 2040
#endif
35 andreas 2041
                    localSettings.ID = 0;
2042
                }
134 andreas 2043
 
2044
                mChannel = localSettings.ID;
35 andreas 2045
            }
26 andreas 2046
            else if (caseCompare(left, "Scale") == 0 && !right.empty())
71 andreas 2047
                localSettings.scale = isTrue(right);
120 andreas 2048
            else if (caseCompare(left, "ToolbarForce") == 0 && !right.empty())
2049
                localSettings.tbforce = isTrue(right);
151 andreas 2050
            else if (caseCompare(left, "ToolbarSuppress") == 0 && !right.empty())
2051
                localSettings.tbsuppress = isTrue(right);
35 andreas 2052
            else if (caseCompare(left, "Profiling") == 0 && !right.empty())
71 andreas 2053
                localSettings.profiling = isTrue(right);
175 andreas 2054
            else if (caseCompare(left, "MaxButtonCache") == 0 && !right.empty())
2055
                localSettings.max_cache = atoi(right.c_str());
51 andreas 2056
            else if (caseCompare(left, "Password1") == 0 && !right.empty())
2057
                localSettings.password1 = right;
2058
            else if (caseCompare(left, "Password2") == 0 && !right.empty())
2059
                localSettings.password2 = right;
2060
            else if (caseCompare(left, "Password3") == 0 && !right.empty())
2061
                localSettings.password3 = right;
2062
            else if (caseCompare(left, "Password4") == 0 && !right.empty())
2063
                localSettings.password4 = right;
71 andreas 2064
            else if (caseCompare(left, "SystemSoundFile") == 0 && !right.empty())
2065
                localSettings.systemSound = right;
2066
            else if (caseCompare(left, "SystemSoundState") == 0 && !right.empty())
2067
                localSettings.systemSoundState = isTrue(right);
2068
            else if (caseCompare(left, "SystemSingleBeep") == 0 && !right.empty())
2069
                localSettings.systemSingleBeep = right;
2070
            else if (caseCompare(left, "SystemDoubleBeep") == 0 && !right.empty())
2071
                localSettings.systemDoubleBeep = right;
141 andreas 2072
            else if (caseCompare(left, "SystemVolume") == 0 && !right.empty())
2073
            {
2074
                int volume = atoi(right.c_str());
2075
 
2076
                if (volume < 0)
2077
                    volume = 0;
2078
                else if (volume > 100)
2079
                    volume = 100;
2080
 
2081
                localSettings.systemVolume = volume;
2082
            }
2083
            else if (caseCompare(left, "SystemGain") == 0 && !right.empty())
2084
            {
2085
                int gain = atoi(right.c_str());
2086
 
2087
                if (gain < 0)
2088
                    gain = 0;
2089
                else if (gain > 100)
2090
                    gain = 100;
2091
 
2092
                localSettings.systemGain = gain;
2093
            }
134 andreas 2094
            else if (caseCompare(left, "SystemRotationFix") == 0 && !right.empty())
2095
                localSettings.systemRotationFix = isTrue(right);
2096
            else if (caseCompare(left, "UUID") == 0 && !right.empty())
2097
                localSettings.uuid = right;
112 andreas 2098
            else if (caseCompare(left, "FTPuser") == 0 && !right.empty())       // FTP credentials
2099
                localSettings.ftpUser = right;
2100
            else if (caseCompare(left, "FTPpassword") == 0 && !right.empty())
2101
                localSettings.ftpPassword = right;
115 andreas 2102
            else if (caseCompare(left, "FTPsurface") == 0 && !right.empty())
2103
                localSettings.ftpSurface = right;
116 andreas 2104
            else if (caseCompare(left, "FTPpassive") == 0 && !right.empty())
2105
                localSettings.ftpPassive = isTrue(right);
115 andreas 2106
            else if (caseCompare(left, "FTPdownloadTime") == 0 && !right.empty())
2107
                localSettings.ftpLastDownload = atol(right.c_str());
104 andreas 2108
            else if (caseCompare(left, "SIP_PROXY") == 0 && !right.empty())     // SIP settings starting here
2109
                localSettings.sip_proxy = right;
2110
            else if (caseCompare(left, "SIP_PORT") == 0 && !right.empty())
2111
                localSettings.sip_port = atoi(right.c_str());
127 andreas 2112
            else if (caseCompare(left, "SIP_PORTTLS") == 0 && !right.empty())
2113
                localSettings.sip_portTLS = atoi(right.c_str());
104 andreas 2114
            else if (caseCompare(left, "SIP_STUN") == 0 && !right.empty())
2115
                localSettings.sip_stun = right;
2116
            else if (caseCompare(left, "SIP_DOMAIN") == 0 && !right.empty())
2117
                localSettings.sip_domain = right;
2118
            else if (caseCompare(left, "SIP_USER") == 0 && !right.empty())
2119
                localSettings.sip_user = right;
2120
            else if (caseCompare(left, "SIP_PASSWORD") == 0 && !right.empty())
2121
                localSettings.sip_password = right;
127 andreas 2122
            else if (caseCompare(left, "SIP_IPV4") == 0 && !right.empty())
2123
                localSettings.sip_ipv4 = isTrue(right);
2124
            else if (caseCompare(left, "SIP_IPV6") == 0 && !right.empty())
2125
                localSettings.sip_ipv6 = isTrue(right);
139 andreas 2126
            else if (caseCompare(left, "SIP_IPHONE") == 0 && !right.empty())
2127
                localSettings.sip_iphone = isTrue(right);
127 andreas 2128
            else if (caseCompare(left, "SIP_FIREWALL") == 0 && !right.empty())
2129
                localSettings.sip_firewall = sipFirewallStrToEnum(right);
104 andreas 2130
            else if (caseCompare(left, "SIP_ENABLED") == 0 && !right.empty())
2131
                localSettings.sip_enabled = isTrue(right);
26 andreas 2132
        }
35 andreas 2133
    }
2 andreas 2134
 
35 andreas 2135
    fs.close();
116 andreas 2136
    mInitialized = true;
2137
    TStreamError::setLogLevel(localSettings.logLevel);
2138
    TStreamError::setLogFile(localSettings.logFile);
2 andreas 2139
 
134 andreas 2140
    if (localSettings.uuid.empty())
2141
    {
2142
#ifdef __ANDROID__
2143
        QUuid qUid = QUuid::createUuid();
2144
        localSettings.uuid = qUid.toString().toStdString();
2145
#else
2146
        uuid_t uuid;
2147
        char sUUID[256];
2148
 
2149
        uuid_generate_random(uuid);
2150
        uuid_unparse_lower(uuid, sUUID);
2151
        localSettings.uuid.assign(sUUID);
2152
#endif
192 andreas 2153
        localSettings_temp = localSettings;
134 andreas 2154
        saveSettings();
2155
    }
192 andreas 2156
 
23 andreas 2157
    if (TStreamError::checkFilter(HLOG_DEBUG))
35 andreas 2158
    {
2159
        MSG_INFO("Selected Parameters:");
2160
        MSG_INFO("    Path to cfg.: " << localSettings.path);
2161
        MSG_INFO("    Name of cfg.: " << localSettings.name);
23 andreas 2162
#ifndef __ANDROID__
2163
        MSG_INFO("    Logfile:      " << localSettings.logFile);
2164
#endif
26 andreas 2165
        MSG_INFO("    LogLevel:     " << localSettings.logLevel);
35 andreas 2166
        MSG_INFO("    Long format:  " << (localSettings.longformat ? "YES" : "NO"));
2167
        MSG_INFO("    Project path: " << localSettings.project);
23 andreas 2168
#ifndef __ANDROID__
35 andreas 2169
        MSG_INFO("    Show banner:  " << (localSettings.noBanner ? "NO" : "YES"));
23 andreas 2170
#endif
35 andreas 2171
        MSG_INFO("    Controller:   " << localSettings.server);
2172
        MSG_INFO("    Port:         " << localSettings.port);
2173
        MSG_INFO("    Channel:      " << localSettings.ID);
2174
        MSG_INFO("    Panel type:   " << localSettings.ptype);
2175
        MSG_INFO("    Firmware ver. " << localSettings.version);
26 andreas 2176
        MSG_INFO("    Scaling:      " << (localSettings.scale ? "YES" : "NO"));
35 andreas 2177
        MSG_INFO("    Profiling:    " << (localSettings.profiling ? "YES" : "NO"));
175 andreas 2178
        MSG_INFO("    Button cache: " << localSettings.max_cache);
71 andreas 2179
        MSG_INFO("    System Sound: " << localSettings.systemSound);
2180
        MSG_INFO("    Sound state:  " << (localSettings.systemSoundState ? "ACTIVATED" : "DEACTIVATED"));
2181
        MSG_INFO("    Single beep:  " << localSettings.systemSingleBeep);
2182
        MSG_INFO("    Double beep:  " << localSettings.systemDoubleBeep);
141 andreas 2183
        MSG_INFO("    Volume:       " << localSettings.systemVolume);
2184
        MSG_INFO("    Gain:         " << localSettings.systemGain);
134 andreas 2185
        MSG_INFO("    Rotation:     " << (localSettings.systemRotationFix ? "LOCKED" : "UNLOCKED"));
2186
        MSG_INFO("    UUID:         " << localSettings.uuid);
115 andreas 2187
        MSG_INFO("    FTP user:     " << localSettings.ftpUser);
2188
        MSG_INFO("    FTP password: " << localSettings.ftpPassword);
2189
        MSG_INFO("    FTP surface:  " << localSettings.ftpSurface);
116 andreas 2190
        MSG_INFO("    FTP passive:  " << (localSettings.ftpPassive ? "YES" : "NO"));
115 andreas 2191
        MSG_INFO("    FTP dl. time: " << localSettings.ftpLastDownload);
127 andreas 2192
        MSG_INFO("    SIP proxy:    " << localSettings.sip_proxy);
2193
        MSG_INFO("    SIP port:     " << localSettings.sip_port);
2194
        MSG_INFO("    SIP TLS port: " << localSettings.sip_portTLS);
2195
        MSG_INFO("    SIP STUN:     " << localSettings.sip_stun);
2196
        MSG_INFO("    SIP doamain:  " << localSettings.sip_domain);
2197
        MSG_INFO("    SIP user:     " << localSettings.sip_user);
2198
        MSG_INFO("    SIP IPv4:     " << (localSettings.sip_ipv4 ? "YES" : "NO"));
2199
        MSG_INFO("    SIP IPv6:     " << (localSettings.sip_ipv6 ? "YES" : "NO"));
139 andreas 2200
        MSG_INFO("    SIP Int.Phone:" << (localSettings.sip_iphone ? "YES" : "NO"));
127 andreas 2201
        MSG_INFO("    SIP firewall: " << sipFirewallToString(localSettings.sip_firewall));
2202
        MSG_INFO("    SIP enabled:  " << (localSettings.sip_enabled ? "YES" : "NO"));
35 andreas 2203
    }
2 andreas 2204
 
192 andreas 2205
    localSettings_temp = localSettings;
239 andreas 2206
#endif
35 andreas 2207
    return true;
2 andreas 2208
}
2209
 
21 andreas 2210
/**
2211
 * @brief TConfig::split splitts a string into parts.
2212
 *
2213
 * The method splitts a string into parts separated by \p seps. It puts the
2214
 * parts into a vector array.
2215
 *
2216
 * @param str           The string to split
2217
 * @param seps          The separator(s)
2218
 * @param trimEmpty     `true` = trum the parts.
2219
 *
2220
 * @return A vector array containing the parts of the string \p str.
2221
 */
2 andreas 2222
vector<string> TConfig::split(const string& str, const string& seps, const bool trimEmpty)
2223
{
116 andreas 2224
    DECL_TRACER("TConfig::split(const string& str, const string& seps, const bool trimEmpty)");
2225
 
2 andreas 2226
	size_t pos = 0, mark = 0;
2227
	vector<string> parts;
2228
 
2229
	for (auto it = str.begin(); it != str.end(); ++it)
2230
	{
2231
		for (auto sepIt = seps.begin(); sepIt != seps.end(); ++sepIt)
2232
		{
2233
			if (*it == *sepIt)
2234
			{
2235
				size_t len = pos - mark;
2236
				parts.push_back(str.substr(mark, len));
2237
				mark = pos + 1;
2238
				break;
2239
			}
2240
		}
2241
 
2242
		pos++;
2243
	}
2244
 
2245
	parts.push_back(str.substr(mark));
2246
 
2247
	if (trimEmpty)
2248
	{
2249
		vector<string> nparts;
2250
 
2251
		for (auto it = parts.begin(); it != parts.end(); ++it)
2252
		{
2253
			if (it->empty())
2254
				continue;
2255
 
2256
			nparts.push_back(*it);
2257
		}
2258
 
2259
		return nparts;
2260
	}
2261
 
2262
	return parts;
2263
}
2264
 
21 andreas 2265
/**
2266
 * @brief TConfig::caseCompare compares 2 strings
2267
 *
2268
 * This method compares 2 strings case insensitive. This means that it ignores
2269
 * the case of the letters. For example:
2270
 *
2271
 *     BLAME
2272
 *     blame
2273
 *     Blame
2274
 *
2275
 * are all the same and would return 0, which means _equal_.
2276
 *
2277
 * @param str1  1st string to compare
2278
 * @param str2  2nd string to compare
2279
 *
2280
 * @return 0 if the strings are equal\n
2281
 * less than 0 if the byte of \p str1 is bigger than the byte of \p str2\n
2282
 * grater than 0 if the byte of \p str1 is smaller than the byte of \p str2.
2283
 */
2 andreas 2284
int TConfig::caseCompare(const string& str1, const string& str2)
2285
{
116 andreas 2286
    DECL_TRACER("TConfig::caseCompare(const string& str1, const string& str2)");
2287
 
2 andreas 2288
	size_t i = 0;
2289
 
2290
	if (str1.length() != str2.length())
2291
		return ((str1.length() < str2.length()) ? -1 : 1);
2292
 
2293
	for (auto it = str1.begin(); it != str1.end(); ++it)
2294
	{
2295
		if (tolower(*it) != tolower(str2.at(i)))
2296
			return (int)(*it - str2.at(i));
2297
 
2298
		i++;
2299
	}
2300
 
2301
	return 0;
2302
}