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
383 andreas 458
    {
192 andreas 459
        localSettings.logFile = file;
383 andreas 460
        TStreamError::setLogFile(file);
461
    }
192 andreas 462
 
463
    mTemporary = false;
23 andreas 464
    return true;
465
}
466
 
467
bool TConfig::saveLogLevel(const string &level)
468
{
469
    DECL_TRACER("TConfig::saveLogLevel(const string &level)");
470
 
59 andreas 471
    if (level.find(SLOG_NONE) == string::npos && level.find(SLOG_INFO) == string::npos && level.find(SLOG_WARNING) == string::npos &&
472
            level.find(SLOG_ERROR) == string::npos && level.find(SLOG_TRACE) == string::npos && level.find(SLOG_DEBUG) == string::npos &&
473
            level.find(SLOG_PROTOCOL) == string::npos && level.find(SLOG_ALL) == string::npos)
23 andreas 474
        return false;
475
 
192 andreas 476
    if (mTemporary)
477
    {
478
        localSettings_temp.logLevel = level;
479
        localSettings_temp.logLevelBits = logLevelStrToBits(level);
480
    }
481
    else
482
    {
483
        localSettings.logLevel = level;
484
        localSettings.logLevelBits = logLevelStrToBits(level);
485
    }
486
 
59 andreas 487
    MSG_INFO("New log level: " << level);
192 andreas 488
    mTemporary = false;
23 andreas 489
    return true;
490
}
491
 
59 andreas 492
bool TConfig::saveLogLevel(uint level)
493
{
494
    DECL_TRACER("TConfig::saveLogLevel(uint level)");
495
 
496
    if (level != 0 && !(level&HLOG_INFO) && !(level&HLOG_WARNING) &&
497
            !(level&HLOG_ERROR) && !(level&HLOG_TRACE) && !(level&HLOG_DEBUG))
498
        return false;
499
 
192 andreas 500
    if (mTemporary)
501
    {
502
        localSettings_temp.logLevelBits = level;
503
        localSettings_temp.logLevel = logLevelBitsToString(level);
504
    }
505
    else
506
    {
507
        localSettings.logLevelBits = level;
508
        localSettings.logLevel = logLevelBitsToString(level);
367 andreas 509
        TStreamError::setLogLevel(level);
192 andreas 510
        MSG_INFO("New log level from bits: " << localSettings.logLevel);
511
    }
512
 
513
    mTemporary = false;
59 andreas 514
    return true;
515
}
516
 
23 andreas 517
bool TConfig::saveChannel(int channel)
518
{
519
    DECL_TRACER("TConfig::saveChannel(int channel)");
520
 
271 andreas 521
    if (channel < 10000 || channel > 20000)
23 andreas 522
        return false;
523
 
192 andreas 524
    if (mTemporary)
525
        localSettings_temp.ID = channel;
526
    else
527
        localSettings.ID = channel;
528
 
529
    mTemporary = false;
23 andreas 530
    return true;
531
}
532
 
533
bool TConfig::saveController(const string &cnt)
534
{
535
    DECL_TRACER("TConfig::saveController(const string &cnt)");
536
 
192 andreas 537
    if (mTemporary)
538
        localSettings_temp.server = cnt;
539
    else
540
        localSettings.server = cnt;
541
 
542
    mTemporary = false;
23 andreas 543
    return true;
544
}
545
 
546
bool TConfig::savePanelType(const string &pt)
547
{
548
    DECL_TRACER("TConfig::savePanelType(const string &pt)");
549
 
192 andreas 550
    if (mTemporary)
551
        localSettings_temp.ptype = pt;
552
    else
553
        localSettings.ptype = pt;
554
 
555
    mTemporary = false;
23 andreas 556
    return true;
557
}
558
 
559
bool TConfig::savePort(int port)
560
{
561
    DECL_TRACER("TConfig::savePort(int port)");
562
 
563
    if (port < 1024 || port > 32767)
564
        return false;
565
 
192 andreas 566
    if (mTemporary)
567
        localSettings_temp.port = port;
568
    else
569
        localSettings.port = port;
570
 
571
    mTemporary = false;
23 andreas 572
    return true;
573
}
574
 
575
bool TConfig::saveProjectPath(const string &path)
576
{
577
    DECL_TRACER("TConfig::saveProjectPath(const string &path)");
578
 
579
    if (path.empty())
580
        return false;
581
 
192 andreas 582
    if (mTemporary)
583
        localSettings_temp.project = path;
584
    else
585
        localSettings.project = path;
586
 
587
    mTemporary = false;
23 andreas 588
    return true;
589
}
590
 
591
void TConfig::saveFormat(bool format)
592
{
192 andreas 593
    DECL_TRACER(string("TConfig::saveFormat(bool format) ") + (format ? "[TRUE]" : "[FALSE]"));
23 andreas 594
 
192 andreas 595
    if (mTemporary)
596
        localSettings_temp.longformat = format;
597
    else
598
        localSettings.longformat = format;
599
 
600
    mTemporary = false;
23 andreas 601
}
602
 
24 andreas 603
void TConfig::saveScale(bool scale)
604
{
605
    DECL_TRACER("TConfig::saveScale(bool scale)");
606
 
192 andreas 607
    if (mTemporary)
608
        localSettings_temp.scale = scale;
609
    else
610
        localSettings.scale = scale;
611
 
612
    mTemporary = false;
24 andreas 613
}
614
 
118 andreas 615
void TConfig::saveBanner(bool banner)
616
{
617
    DECL_TRACER("TConfig::saveBanner(bool banner)");
618
 
192 andreas 619
    if (mTemporary)
620
        localSettings_temp.noBanner = banner;
621
    else
622
        localSettings.noBanner = banner;
623
 
624
    mTemporary = false;
118 andreas 625
}
626
 
120 andreas 627
void TConfig::saveToolbarForce(bool tb)
628
{
629
    DECL_TRACER("TConfig::saveToolbarForce(bool tb)");
630
 
192 andreas 631
    if (mTemporary)
632
        localSettings_temp.tbforce = tb;
633
    else
634
        localSettings.tbforce = tb;
635
 
636
    mTemporary = false;
120 andreas 637
}
638
 
151 andreas 639
void TConfig::saveToolbarSuppress(bool tb)
640
{
641
    DECL_TRACER("TConfig::saveToolbarSuppress(bool tb)");
642
 
192 andreas 643
    if (mTemporary)
644
        localSettings_temp.tbsuppress = tb;
645
    else
646
        localSettings.tbsuppress = tb;
647
 
648
    mTemporary = false;
151 andreas 649
}
650
 
35 andreas 651
void TConfig::saveProfiling(bool prof)
652
{
116 andreas 653
    DECL_TRACER("TConfig::saveProfiling(bool prof)");
654
 
192 andreas 655
    if (mTemporary)
656
        localSettings_temp.profiling = prof;
657
    else
658
        localSettings.profiling = prof;
659
 
660
    mTemporary = false;
35 andreas 661
}
662
 
175 andreas 663
void TConfig::saveButtonCache(size_t size)
664
{
665
    DECL_TRACER("TConfig::saveButtonCache(size_t size)");
666
 
192 andreas 667
    if (mTemporary)
668
        localSettings_temp.max_cache = size;
669
    else
670
        localSettings.max_cache = size;
671
 
672
    mTemporary = false;
175 andreas 673
}
674
 
51 andreas 675
void TConfig::savePassword1(const std::string& pw)
676
{
116 andreas 677
    DECL_TRACER("TConfig::savePassword1(const std::string& pw)");
678
 
192 andreas 679
    if (mTemporary)
680
        localSettings_temp.password1 = pw;
681
    else
682
        localSettings.password1 = pw;
683
 
684
    mTemporary = false;
51 andreas 685
}
686
 
687
void TConfig::savePassword2(const std::string& pw)
688
{
116 andreas 689
    DECL_TRACER("TConfig::savePassword2(const std::string& pw)");
690
 
192 andreas 691
    if (mTemporary)
692
        localSettings_temp.password2 = pw;
693
    else
694
        localSettings.password2 = pw;
695
 
696
    mTemporary = false;
51 andreas 697
}
698
 
699
void TConfig::savePassword3(const std::string& pw)
700
{
116 andreas 701
    DECL_TRACER("TConfig::savePassword3(const std::string& pw)");
702
 
192 andreas 703
    if (mTemporary)
704
        localSettings_temp.password3 = pw;
705
    else
706
        localSettings.password3 = pw;
707
 
708
    mTemporary = false;
51 andreas 709
}
710
 
711
void TConfig::savePassword4(const std::string& pw)
712
{
116 andreas 713
    DECL_TRACER("TConfig::savePassword4(const std::string& pw)");
714
 
192 andreas 715
    if (mTemporary)
716
        localSettings_temp.password4 = pw;
717
    else
718
        localSettings.password4 = pw;
719
 
720
    mTemporary = false;
51 andreas 721
}
722
 
71 andreas 723
void TConfig::saveSystemSoundFile(const std::string& snd)
724
{
116 andreas 725
    DECL_TRACER("TConfig::saveSystemSoundFile(const std::string& snd)");
726
 
192 andreas 727
    if (mTemporary)
728
        localSettings_temp.systemSound = snd;
729
    else
730
        localSettings.systemSound = snd;
731
 
732
    mTemporary = false;
71 andreas 733
}
734
 
735
void TConfig::saveSystemSoundState(bool state)
736
{
116 andreas 737
    DECL_TRACER("TConfig::saveSystemSoundState(bool state)");
738
 
192 andreas 739
    if (mTemporary)
740
        localSettings_temp.systemSoundState = state;
741
    else
195 andreas 742
        localSettings.systemSoundState = localSettings_temp.systemSoundState = state;
192 andreas 743
 
744
    mTemporary = false;
71 andreas 745
}
746
 
141 andreas 747
void TConfig::saveSingleBeepFile(const std::string& snd)
748
{
749
    DECL_TRACER("TConfig::saveSingleBeepFile(const std::string& snd)");
750
 
192 andreas 751
    if (mTemporary)
752
        localSettings_temp.systemSingleBeep = snd;
753
    else
754
        localSettings.systemSingleBeep = snd;
755
 
756
    mTemporary = false;
141 andreas 757
}
758
 
759
void TConfig::saveDoubleBeepFile(const std::string& snd)
760
{
761
    DECL_TRACER("TConfig::saveDoubleBeepFile(const std::string& snd)");
762
 
192 andreas 763
    if (mTemporary)
764
        localSettings_temp.systemDoubleBeep = snd;
765
    else
766
        localSettings.systemDoubleBeep = snd;
767
 
768
    mTemporary = false;
141 andreas 769
}
770
 
771
void TConfig::saveSystemVolume(int volume)
772
{
773
    DECL_TRACER("TConfig::saveSystemVolume(int volume)");
774
 
775
    if (volume < 0 || volume > 100)
776
        return;
777
 
192 andreas 778
    if (mTemporary)
779
        localSettings_temp.systemVolume = volume;
780
    else
781
        localSettings.systemVolume = volume;
782
 
783
    mTemporary = false;
141 andreas 784
}
785
 
786
void TConfig::saveSystemGain(int gain)
787
{
788
    DECL_TRACER("TConfig::saveSystemGain(int gain)");
789
 
790
    if (gain < 0 || gain > 100)
791
        return;
792
 
192 andreas 793
    if (mTemporary)
794
        localSettings_temp.systemGain = gain;
795
    else
796
        localSettings.systemGain = gain;
797
 
798
    mTemporary = false;
141 andreas 799
}
800
 
112 andreas 801
void TConfig::saveFtpUser(const string& user)
802
{
116 andreas 803
    DECL_TRACER("TConfig::saveFtpUser(const string& user)");
804
 
192 andreas 805
    if (mTemporary)
806
        localSettings_temp.ftpUser = user;
807
    else
808
        localSettings.ftpUser = user;
809
 
810
    mTemporary = false;
112 andreas 811
}
812
 
813
void TConfig::saveFtpPassword(const string& pw)
814
{
116 andreas 815
    DECL_TRACER("TConfig::saveFtpPassword(const string& pw)");
816
 
192 andreas 817
    if (mTemporary)
818
        localSettings_temp.ftpPassword = pw;
819
    else
820
        localSettings.ftpPassword = pw;
821
 
822
    mTemporary = false;
112 andreas 823
}
824
 
115 andreas 825
void TConfig::saveFtpSurface(const string& fname)
826
{
116 andreas 827
    DECL_TRACER("TConfig::saveFtpSurface(const string& fname)");
828
 
192 andreas 829
    if (mTemporary)
830
        localSettings_temp.ftpSurface = fname;
831
    else
832
        localSettings.ftpSurface = fname;
833
 
834
    mTemporary = false;
115 andreas 835
}
836
 
116 andreas 837
void TConfig::saveFtpPassive(bool mode)
838
{
839
    DECL_TRACER("TConfig::saveFtpPassive(bool mode)");
840
 
192 andreas 841
    if (mTemporary)
842
        localSettings_temp.ftpPassive = mode;
843
    else
844
        localSettings.ftpPassive = mode;
845
 
846
    mTemporary = false;
116 andreas 847
}
848
 
115 andreas 849
void TConfig::saveFtpDownloadTime(time_t t)
850
{
116 andreas 851
    DECL_TRACER("TConfig::saveFtpDownloadTime(time_t t)");
852
 
192 andreas 853
    if (mTemporary)
854
        localSettings_temp.ftpLastDownload = t;
855
    else
856
        localSettings.ftpLastDownload = t;
857
 
858
    mTemporary = false;
115 andreas 859
}
860
 
104 andreas 861
std::string& TConfig::getSIPproxy()
862
{
116 andreas 863
    DECL_TRACER("TConfig::getSIPproxy()");
864
 
192 andreas 865
    return mTemporary ? localSettings_temp.sip_proxy : localSettings.sip_proxy;
104 andreas 866
}
867
 
868
void TConfig::setSIPproxy(const std::string& address)
869
{
116 andreas 870
    DECL_TRACER("TConfig::setSIPproxy(const std::string& address)");
871
 
192 andreas 872
    if (mTemporary)
873
        localSettings_temp.sip_proxy = address;
874
    else
875
        localSettings.sip_proxy = address;
876
 
877
    mTemporary = false;
104 andreas 878
}
879
 
880
int TConfig::getSIPport()
881
{
116 andreas 882
    DECL_TRACER("TConfig::getSIPport()");
883
 
192 andreas 884
    return mTemporary ? localSettings_temp.sip_port : localSettings.sip_port;
104 andreas 885
}
886
 
887
void TConfig::setSIPport(int port)
888
{
116 andreas 889
    DECL_TRACER("TConfig::setSIPport(int port)");
890
 
192 andreas 891
    if (mTemporary)
892
        localSettings_temp.sip_port = port;
893
    else
894
        localSettings.sip_port = port;
895
 
896
    mTemporary = false;
104 andreas 897
}
898
 
127 andreas 899
int TConfig::getSIPportTLS()
900
{
901
    DECL_TRACER("TConfig::getSIPportTLS()");
902
 
192 andreas 903
    return mTemporary ? localSettings_temp.sip_portTLS : localSettings.sip_portTLS;
127 andreas 904
}
905
 
906
void TConfig::setSIPportTLS(int port)
907
{
908
    DECL_TRACER("TConfig::setSIPportTLS(int port)");
909
 
192 andreas 910
    if (mTemporary)
911
        localSettings_temp.sip_portTLS = port;
912
    else
913
        localSettings.sip_portTLS = port;
914
 
915
    mTemporary = false;
127 andreas 916
}
917
 
104 andreas 918
std::string& TConfig::getSIPstun()
919
{
116 andreas 920
    DECL_TRACER("TConfig::getSIPstun()");
921
 
192 andreas 922
    return mTemporary ? localSettings_temp.sip_stun : localSettings.sip_stun;
104 andreas 923
}
924
 
925
void TConfig::setSIPstun(const std::string& address)
926
{
116 andreas 927
    DECL_TRACER("TConfig::setSIPstun(const std::string& address)");
928
 
192 andreas 929
    if (mTemporary)
930
        localSettings_temp.sip_stun = address;
931
    else
932
        localSettings.sip_stun = address;
933
 
934
    mTemporary = false;
104 andreas 935
}
936
 
937
std::string& TConfig::getSIPdomain()
938
{
116 andreas 939
    DECL_TRACER("TConfig::getSIPdomain()");
940
 
192 andreas 941
    return mTemporary ? localSettings_temp.sip_domain : localSettings.sip_domain;
104 andreas 942
}
943
 
944
void TConfig::setSIPdomain(const std::string& domain)
945
{
116 andreas 946
    DECL_TRACER("TConfig::setSIPdomain(const std::string& domain)");
947
 
192 andreas 948
    if (mTemporary)
949
        localSettings_temp.sip_domain = domain;
950
    else
951
        localSettings.sip_domain = domain;
952
 
953
    mTemporary = false;
104 andreas 954
}
955
 
956
std::string& TConfig::getSIPuser()
957
{
116 andreas 958
    DECL_TRACER("TConfig::getSIPuser()");
959
 
192 andreas 960
    return mTemporary ? localSettings_temp.sip_user : localSettings.sip_user;
104 andreas 961
}
962
 
963
void TConfig::setSIPuser(const std::string& user)
964
{
116 andreas 965
    DECL_TRACER("TConfig::setSIPuser(const std::string& user)");
966
 
192 andreas 967
    if (mTemporary)
968
        localSettings_temp.sip_user = user;
969
    else
970
        localSettings.sip_user = user;
971
 
972
    mTemporary = false;
104 andreas 973
}
974
 
975
std::string& TConfig::getSIPpassword()
976
{
116 andreas 977
    DECL_TRACER("TConfig::getSIPpassword()");
978
 
192 andreas 979
    return mTemporary ? localSettings_temp.sip_password : localSettings.sip_password;
104 andreas 980
}
981
 
982
void TConfig::setSIPpassword(const std::string& pw)
983
{
116 andreas 984
    DECL_TRACER("TConfig::setSIPpassword(const std::string& pw)");
985
 
192 andreas 986
    if (mTemporary)
987
        localSettings_temp.sip_password = pw;
988
    else
989
        localSettings.sip_password = pw;
990
 
991
    mTemporary = false;
104 andreas 992
}
993
 
994
bool TConfig::getSIPstatus()
995
{
116 andreas 996
    DECL_TRACER("TConfig::getSIPstatus()");
997
 
192 andreas 998
    return mTemporary ? localSettings_temp.sip_enabled : localSettings.sip_enabled;
104 andreas 999
}
1000
 
127 andreas 1001
bool TConfig::getSIPnetworkIPv4()
1002
{
1003
    DECL_TRACER("TConfig::getSIPnetworkIPv4()");
1004
 
192 andreas 1005
    return mTemporary ? localSettings_temp.sip_ipv4 : localSettings.sip_ipv4;
127 andreas 1006
}
1007
 
1008
void TConfig::setSIPnetworkIPv4(bool state)
1009
{
1010
    DECL_TRACER("TConfig::setSIPnetworkIPv4(bool state)");
1011
 
192 andreas 1012
    if (mTemporary)
1013
        localSettings_temp.sip_ipv4 = state;
1014
    else
1015
        localSettings.sip_ipv4 = state;
1016
 
1017
    mTemporary = false;
127 andreas 1018
}
1019
 
1020
bool TConfig::getSIPnetworkIPv6()
1021
{
1022
    DECL_TRACER("TConfig::getSIPnetworkIPv6()");
1023
 
192 andreas 1024
    return mTemporary ? localSettings_temp.sip_ipv6 : localSettings.sip_ipv6;
127 andreas 1025
}
1026
 
1027
void TConfig::setSIPnetworkIPv6(bool state)
1028
{
1029
    DECL_TRACER("TConfig::setSIPnetworkIPv6(bool state)");
1030
 
192 andreas 1031
    if (mTemporary)
1032
        localSettings_temp.sip_ipv6 = state;
1033
    else
1034
        localSettings.sip_ipv6 = state;
1035
 
1036
    mTemporary = false;
127 andreas 1037
}
1038
 
139 andreas 1039
void TConfig::setSIPiphone(bool state)
1040
{
1041
    DECL_TRACER("TConfig::setSIPiphone(bool state)");
1042
 
192 andreas 1043
    if (mTemporary)
1044
        localSettings_temp.sip_iphone = state;
1045
    else
1046
        localSettings.sip_iphone = state;
1047
 
1048
    mTemporary = false;
139 andreas 1049
}
1050
 
1051
bool TConfig::getSIPiphone()
1052
{
1053
    DECL_TRACER("TConfig::getSIPiphone()");
1054
 
192 andreas 1055
    return mTemporary ? localSettings_temp.sip_iphone : localSettings.sip_iphone;
139 andreas 1056
}
1057
 
127 andreas 1058
TConfig::SIP_FIREWALL_t TConfig::getSIPfirewall()
1059
{
1060
    DECL_TRACER("TConfig::getSIPfirewall()");
1061
 
192 andreas 1062
    return mTemporary ? localSettings_temp.sip_firewall : localSettings.sip_firewall;
127 andreas 1063
}
1064
 
1065
string TConfig::getSIPfirewallStr()
1066
{
1067
    DECL_TRACER("TConfig::getSIPfirewallStr()");
1068
 
192 andreas 1069
    return sipFirewallToString(mTemporary ? localSettings_temp.sip_firewall : localSettings.sip_firewall);
127 andreas 1070
}
1071
 
1072
void TConfig::setSIPfirewall(TConfig::SIP_FIREWALL_t fw)
1073
{
1074
    DECL_TRACER("TConfig::setSIPfirewall(TConfig::SIP_FIREWALL_t fw)")
1075
 
192 andreas 1076
    if (mTemporary)
1077
        localSettings_temp.sip_firewall = fw;
1078
    else
1079
        localSettings.sip_firewall = fw;
1080
 
1081
    mTemporary = false;
127 andreas 1082
}
1083
 
104 andreas 1084
void TConfig::setSIPstatus(bool state)
1085
{
116 andreas 1086
    DECL_TRACER("TConfig::setSIPstatus(bool state)");
1087
 
192 andreas 1088
    if (mTemporary)
1089
        localSettings_temp.sip_enabled = state;
1090
    else
1091
        localSettings.sip_enabled = state;
1092
 
1093
    mTemporary = false;
104 andreas 1094
}
1095
 
23 andreas 1096
bool TConfig::saveSettings()
1097
{
1098
    DECL_TRACER("TConfig::saveSettings()");
1099
 
292 andreas 1100
    TLock<std::mutex> guard(config_mutex);
1101
 
23 andreas 1102
    try
1103
    {
1104
        string fname = localSettings.path + "/" + localSettings.name;
192 andreas 1105
 
1106
        if (mTemporary)
1107
        {
1108
            localSettings = localSettings_temp;
1109
            MSG_INFO("Temporary settings were copied over.");
1110
        }
1111
 
1112
        MSG_DEBUG("Saving to file " << fname);
23 andreas 1113
        ofstream file(fname);
1114
        string lines = "LogFile=" + localSettings.logFile + "\n";
1115
        lines += "LogLevel=" + localSettings.logLevel + "\n";
1116
        lines += "ProjectPath=" + localSettings.project + "\n";
1117
        lines += string("NoBanner=") + (localSettings.noBanner ? "true" : "false") + "\n";
151 andreas 1118
        lines += string("ToolbarSuppress=") + (localSettings.tbsuppress ? "true" : "false") + "\n";
120 andreas 1119
        lines += string("ToolbarForce=") + (localSettings.tbforce ? "true" : "false") + "\n";
23 andreas 1120
        lines += string("LongFormat=") + (localSettings.longformat ? "true" : "false") + "\n";
1121
        lines += "Address=" + localSettings.server + "\n";
1122
        lines += "Port=" + std::to_string(localSettings.port) + "\n";
1123
        lines += "Channel=" + std::to_string(localSettings.ID) + "\n";
1124
        lines += "System=" + std::to_string(localSettings.system) + "\n";
1125
        lines += "PanelType=" + localSettings.ptype + "\n";
1126
        lines += "Firmware=" + localSettings.version + "\n";
1127
        lines += string("CertCheck=") + (localSettings.certCheck ? "true" : "false") + "\n";
24 andreas 1128
        lines += string("Scale=") + (localSettings.scale ? "true" : "false") + "\n";
35 andreas 1129
        lines += string("Profiling=") + (localSettings.profiling ? "true" : "false") + "\n";
175 andreas 1130
        lines += "MaxButtonCache=" + std::to_string(localSettings.max_cache) + "\n";
51 andreas 1131
        lines += string("Password1=") + localSettings.password1 + "\n";
1132
        lines += string("Password2=") + localSettings.password2 + "\n";
1133
        lines += string("Password3=") + localSettings.password3 + "\n";
1134
        lines += string("Password4=") + localSettings.password4 + "\n";
71 andreas 1135
        lines += string("SystemSoundFile=") + localSettings.systemSound + "\n";
1136
        lines += string("SystemSoundState=") + (localSettings.systemSoundState ? "ON" : "OFF") + "\n";
1137
        lines += string("SystemSingleBeep=") + localSettings.systemSingleBeep + "\n";
1138
        lines += string("SystemDoubleBeep=") + localSettings.systemDoubleBeep + "\n";
141 andreas 1139
        lines += "SystemVolume=" + std::to_string(localSettings.systemVolume) + "\n";
1140
        lines += "SystemGain=" + std::to_string(localSettings.systemGain) + "\n";
134 andreas 1141
        lines += string("SystemRotationFix=") + (localSettings.systemRotationFix ? "ON" : "OFF") + "\n";
1142
        lines += string("UUID=") + localSettings.uuid + "\n";
112 andreas 1143
        // FTP credentials
1144
        lines += string("FTPuser=") + localSettings.ftpUser + "\n";
1145
        lines += string("FTPpassword=") + localSettings.ftpPassword + "\n";
115 andreas 1146
        lines += string("FTPsurface=") + localSettings.ftpSurface + "\n";
116 andreas 1147
        lines += string("FTPpassive=") + (localSettings.ftpPassive ? "true" : "false") + "\n";
115 andreas 1148
        lines += string("FTPdownloadTime=") + std::to_string(localSettings.ftpLastDownload) + "\n";
104 andreas 1149
        // SIP settings
116 andreas 1150
        lines += string("SIP_DOMAIN=") + localSettings.sip_domain + "\n";
127 andreas 1151
        lines += string("SIP_PROXY=") + localSettings.sip_proxy + "\n";
116 andreas 1152
        lines += string("SIP_PORT=") + std::to_string(localSettings.sip_port) + "\n";
127 andreas 1153
        lines += string("SIP_PORTTLS=") + std::to_string(localSettings.sip_portTLS) + "\n";
116 andreas 1154
        lines += string("SIP_STUN=") + localSettings.sip_stun + "\n";
1155
        lines += string("SIP_USER=") + localSettings.sip_user + "\n";
1156
        lines += string("SIP_PASSWORD=") + localSettings.sip_password + "\n";
127 andreas 1157
        lines += string("SIP_IPV4=") + (localSettings.sip_ipv4 ? "true" : "false") + "\n";
1158
        lines += string("SIP_IPV6=") + (localSettings.sip_ipv6 ? "true" : "false") + "\n";
139 andreas 1159
        lines += string("SIP_IPHONE=") + (localSettings.sip_iphone ? "true" : "false") + "\n";
127 andreas 1160
        lines += "SIP_FIREWALL=" + sipFirewallToString(localSettings.sip_firewall) + "\n";
116 andreas 1161
        lines += string("SIP_ENABLED=") + (localSettings.sip_enabled ? "true" : "false") + "\n";
23 andreas 1162
        file.write(lines.c_str(), lines.size());
1163
        file.close();
59 andreas 1164
        MSG_INFO("Actual log level: " << localSettings.logLevel);
214 andreas 1165
 
1166
        if (mTemporary)
1167
        {
1168
            TError::Current()->setLogLevel(localSettings.logLevel);
1169
            TError::Current()->setLogFile(localSettings.logFile);
1170
        }
1171
 
1172
        mTemporary = false;
23 andreas 1173
    }
1174
    catch (std::exception& e)
1175
    {
1176
        MSG_ERROR("Couldn't write configs: " << e.what());
1177
        return false;
1178
    }
1179
 
1180
    return true;
1181
}
1182
 
21 andreas 1183
/**
1184
 * @brief TConfig::isLongFormat defines the format in the logfile.
1185
 *
1186
 * If this returns `true` the format in the logfile is a long format. This
1187
 * means, that in front of each message is an additional timestamp.
1188
 *
1189
 * @return `true` = long format, `false` = short format (default).
1190
 */
2 andreas 1191
bool TConfig::isLongFormat()
1192
{
192 andreas 1193
    return mTemporary ? localSettings_temp.longformat : localSettings.longformat;
2 andreas 1194
}
1195
 
21 andreas 1196
/**
1197
 * @brief TConfig::showBanner defines whether the banner should be showed or not.
1198
 *
1199
 * If this method returns `false` the banner on startup is not displayed.
1200
 *
1201
 * @return `true` = display the banner (default), `false` = show no banner.
1202
 */
2 andreas 1203
bool TConfig::showBanner()
1204
{
116 andreas 1205
    DECL_TRACER("TConfig::showBanner()");
1206
 
192 andreas 1207
    return mTemporary ? (!localSettings_temp.noBanner) : (!localSettings.noBanner);
2 andreas 1208
}
1209
 
21 andreas 1210
/**
24 andreas 1211
 * @brief TConfig::getScale returns the scale setting
1212
 *
1213
 * If this is set to TRUE, all images are scaled to fit the screen.
1214
 *
1215
 * @return scale state
1216
 */
1217
bool TConfig::getScale()
1218
{
116 andreas 1219
    DECL_TRACER("TConfig::getScale()");
1220
 
192 andreas 1221
    return mTemporary ? localSettings_temp.scale : localSettings.scale;
24 andreas 1222
}
1223
 
120 andreas 1224
bool TConfig::getToolbarForce()
1225
{
1226
    DECL_TRACER("TConfig::getToolbarForce()");
1227
 
192 andreas 1228
    return mTemporary ? localSettings_temp.tbforce : localSettings.tbforce;
120 andreas 1229
}
1230
 
151 andreas 1231
bool TConfig::getToolbarSuppress()
1232
{
1233
    DECL_TRACER("TConfig::getToolbarSuppress()");
1234
 
192 andreas 1235
    return mTemporary ? localSettings_temp.tbsuppress : localSettings.tbsuppress;
151 andreas 1236
}
1237
 
35 andreas 1238
bool TConfig::getProfiling()
1239
{
192 andreas 1240
    return mTemporary ? localSettings_temp.profiling : localSettings.profiling;
35 andreas 1241
}
1242
 
175 andreas 1243
size_t TConfig::getButttonCache()
1244
{
192 andreas 1245
    if (mTemporary && localSettings_temp.max_cache > 0)
1246
        return localSettings_temp.max_cache;
1247
 
1248
    if (!mTemporary && localSettings.max_cache > 0)
177 andreas 1249
        return localSettings.max_cache * 1000 * 1000;
1250
 
1251
    return 0;
175 andreas 1252
}
1253
 
51 andreas 1254
string & TConfig::getPassword1()
1255
{
116 andreas 1256
    DECL_TRACER("TConfig::getPassword1()");
1257
 
192 andreas 1258
    return mTemporary ? localSettings_temp.password1 : localSettings.password1;
51 andreas 1259
}
1260
 
1261
string & TConfig::getPassword2()
1262
{
116 andreas 1263
    DECL_TRACER("TConfig::getPassword2()");
1264
 
192 andreas 1265
    return mTemporary ? localSettings_temp.password2 : localSettings.password2;
51 andreas 1266
}
1267
 
1268
string & TConfig::getPassword3()
1269
{
116 andreas 1270
    DECL_TRACER("TConfig::getPassword3()");
1271
 
192 andreas 1272
    return mTemporary ? localSettings_temp.password3 : localSettings.password3;
51 andreas 1273
}
1274
 
1275
string & TConfig::getPassword4()
1276
{
116 andreas 1277
    DECL_TRACER("TConfig::getPassword4()");
1278
 
192 andreas 1279
    return mTemporary ? localSettings_temp.password4 : localSettings.password4;
51 andreas 1280
}
1281
 
71 andreas 1282
string & TConfig::getSystemSound()
1283
{
116 andreas 1284
    DECL_TRACER("TConfig::getSystemSound()");
1285
 
192 andreas 1286
    return mTemporary ? localSettings_temp.systemSound : localSettings.systemSound;
71 andreas 1287
}
1288
 
1289
bool TConfig::getSystemSoundState()
1290
{
116 andreas 1291
    DECL_TRACER("TConfig::getSystemSoundState()");
1292
 
192 andreas 1293
    return mTemporary ? localSettings_temp.systemSoundState : localSettings.systemSoundState;
71 andreas 1294
}
1295
 
141 andreas 1296
int TConfig::getSystemVolume()
1297
{
1298
    DECL_TRACER("TConfig::getSystemVolume()");
1299
 
192 andreas 1300
    return mTemporary ? localSettings_temp.systemVolume : localSettings.systemVolume;
141 andreas 1301
}
1302
 
1303
int TConfig::getSystemGain()
1304
{
1305
    DECL_TRACER("TConfig::getSystemGain()");
1306
 
192 andreas 1307
    return mTemporary ? localSettings_temp.systemGain : localSettings.systemGain;
141 andreas 1308
}
1309
 
134 andreas 1310
bool TConfig::getRotationFixed()
1311
{
1312
    DECL_TRACER("TConfig::getRotationFixed()");
1313
 
192 andreas 1314
    return mTemporary ? localSettings_temp.systemRotationFix : localSettings.systemRotationFix;
134 andreas 1315
}
1316
 
1317
void TConfig::setRotationFixed(bool fix)
1318
{
1319
    DECL_TRACER("TConfig::setRotationFixed(bool fix)");
1320
 
192 andreas 1321
    if (mTemporary)
1322
        localSettings_temp.systemRotationFix = fix;
1323
    else
1324
        localSettings.systemRotationFix = fix;
1325
 
1326
    mTemporary = false;
134 andreas 1327
}
1328
 
1329
void TConfig::setSystemChannel(int ch)
1330
{
1331
    DECL_TRACER("TConfig::setSystemChannel(int ch)");
1332
 
1333
    if (ch >= 10000 && ch < 30000)
1334
        mChannel = ch;
1335
}
1336
 
71 andreas 1337
string& TConfig::getSingleBeepSound()
1338
{
116 andreas 1339
    DECL_TRACER("TConfig::getSingleBeepSound()");
1340
 
192 andreas 1341
    return mTemporary ? localSettings_temp.systemSingleBeep : localSettings.systemSingleBeep;
71 andreas 1342
}
1343
 
1344
string& TConfig::getDoubleBeepSound()
1345
{
116 andreas 1346
    DECL_TRACER("TConfig::getDoubleBeepSound()");
1347
 
192 andreas 1348
    return mTemporary ? localSettings_temp.systemDoubleBeep : localSettings.systemDoubleBeep;
71 andreas 1349
}
1350
 
134 andreas 1351
string& TConfig::getUUID()
1352
{
1353
    DECL_TRACER("TConfig::getUUID()");
1354
 
192 andreas 1355
    return mTemporary ? localSettings_temp.uuid : localSettings.uuid;
134 andreas 1356
}
1357
 
112 andreas 1358
string& TConfig::getFtpUser()
1359
{
116 andreas 1360
    DECL_TRACER("TConfig::getFtpUser()");
1361
 
192 andreas 1362
    return mTemporary ? localSettings_temp.ftpUser : localSettings.ftpUser;
112 andreas 1363
}
1364
 
1365
string& TConfig::getFtpPassword()
1366
{
116 andreas 1367
    DECL_TRACER("TConfig::getFtpPassword()");
1368
 
192 andreas 1369
    return mTemporary ? localSettings_temp.ftpPassword : localSettings.ftpPassword;
112 andreas 1370
}
1371
 
115 andreas 1372
string& TConfig::getFtpSurface()
1373
{
116 andreas 1374
    DECL_TRACER("TConfig::getFtpSurface()");
1375
 
192 andreas 1376
    return mTemporary ? localSettings_temp.ftpSurface : localSettings.ftpSurface;
115 andreas 1377
}
1378
 
116 andreas 1379
bool TConfig::getFtpPassive()
1380
{
1381
    DECL_TRACER("TConfig::getFtpPassive()");
1382
 
192 andreas 1383
    return mTemporary ? localSettings_temp.ftpPassive : localSettings.ftpPassive;
116 andreas 1384
}
1385
 
115 andreas 1386
time_t TConfig::getFtpDownloadTime()
1387
{
116 andreas 1388
    DECL_TRACER("TConfig::getFtpDownloadTime()");
1389
 
192 andreas 1390
    return mTemporary ? localSettings_temp.ftpLastDownload : localSettings.ftpLastDownload;
115 andreas 1391
}
1392
 
24 andreas 1393
/**
21 andreas 1394
 * @brief TConfig::certCheck check the certificate if the connection is encrypted.
1395
 *
1396
 * Currently not implemented!
1397
 *
1398
 * @return `true` = check the certificate, `false` = accept any certificate (default).
1399
 */
1400
bool TConfig::certCheck()
1401
{
192 andreas 1402
    return mTemporary ? localSettings_temp.certCheck : localSettings.certCheck;
21 andreas 1403
}
1404
 
1405
/**
59 andreas 1406
 * @brief TConfig::logLevelStrToBits
1407
 * Converts a string containing one or more loglevels into a bit field.
1408
 * @param level A string containing the log levels
1409
 * @return A bit field representing the log levels.
1410
 */
1411
uint TConfig::logLevelStrToBits(const string& level)
1412
{
1413
    uint l = 0;
1414
 
1415
    if (level.find("INFO") != string::npos)
1416
        l |= HLOG_INFO;
1417
 
1418
    if (level.find("WARNING") != string::npos)
1419
        l |= HLOG_WARNING;
1420
 
1421
    if (level.find("ERROR") != string::npos)
1422
        l |= HLOG_ERROR;
1423
 
1424
    if (level.find("TRACE") != string::npos)
1425
        l |= HLOG_TRACE;
1426
 
1427
    if (level.find("DEBUG") != string::npos)
1428
        l |= HLOG_DEBUG;
1429
 
1430
    if (level.find("PROTOCOL") != string::npos)
71 andreas 1431
        l |= HLOG_PROTOCOL;
59 andreas 1432
 
1433
    if (level.find("ALL") != string::npos)
1434
        l = HLOG_ALL;
1435
 
1436
    return l;
1437
}
1438
 
1439
string TConfig::logLevelBitsToString(uint level)
1440
{
1441
    string l;
1442
 
1443
    if (level == 0)
1444
    {
1445
        l = SLOG_NONE;
1446
        return l;
1447
    }
1448
 
1449
    if (level & HLOG_INFO)
1450
    {
1451
        if (l.length() > 0)
1452
            l.append("|");
1453
 
1454
        l.append(SLOG_INFO);
1455
    }
1456
 
1457
    if (level & HLOG_WARNING)
1458
    {
1459
        if (l.length() > 0)
1460
            l.append("|");
1461
 
1462
        l.append(SLOG_WARNING);
1463
    }
1464
 
1465
    if (level & HLOG_ERROR)
1466
    {
1467
        if (l.length() > 0)
1468
            l.append("|");
1469
 
1470
        l.append(SLOG_ERROR);
1471
    }
1472
 
1473
    if (level & HLOG_TRACE)
1474
    {
1475
        if (l.length() > 0)
1476
            l.append("|");
1477
 
1478
        l.append(SLOG_TRACE);
1479
    }
1480
 
1481
    if (level & HLOG_DEBUG)
1482
    {
1483
        if (l.length() > 0)
1484
            l.append("|");
1485
 
1486
        l.append(SLOG_DEBUG);
1487
    }
1488
 
1489
    return l;
1490
}
118 andreas 1491
 
127 andreas 1492
string TConfig::sipFirewallToString(TConfig::SIP_FIREWALL_t fw)
1493
{
1494
    switch(fw)
1495
    {
1496
        case SIP_NO_FIREWALL:   return "SIP_NO_FIREWALL";
1497
        case SIP_NAT_ADDRESS:   return "SIP_NAT_ADDRESS";
1498
        case SIP_STUN:          return "SIP_STUN";
1499
        case SIP_ICE:           return "SIP_ICE";
1500
        case SIP_UPNP:          return "SIP_UPNP";
1501
    }
140 andreas 1502
 
1503
    return string();
127 andreas 1504
}
1505
 
1506
TConfig::SIP_FIREWALL_t TConfig::sipFirewallStrToEnum(const std::string& str)
1507
{
1508
    if (strCaseCompare(str, "SIP_NO_FIREWALL") == 0)
1509
        return SIP_NO_FIREWALL;
1510
    else if (strCaseCompare(str, "SIP_NAT_ADDRESS") == 0)
1511
        return SIP_NAT_ADDRESS;
1512
    else if (strCaseCompare(str, "SIP_STUN") == 0)
1513
        return SIP_STUN;
1514
    else if (strCaseCompare(str, "SIP_ICE") == 0)
1515
        return SIP_ICE;
1516
    else if (strCaseCompare(str, "SIP_UPNP") == 0)
1517
        return SIP_UPNP;
1518
 
1519
    return SIP_NO_FIREWALL;
1520
}
1521
 
118 andreas 1522
string TConfig::makeConfigDefault(const std::string& log, const std::string& project)
1523
{
1524
    string content = "LogFile=" + log + "\n";
365 andreas 1525
#ifndef NDEBUG
1526
    content += "LogLevel=ALL\n";
131 andreas 1527
#else
1528
    content += "LogLevel=NONE\n";
1529
#endif
118 andreas 1530
    content += "ProjectPath=" + project + "\n";
1531
    content += "LongFormat=false\n";
1532
    content += "Address=0.0.0.0\n";
1533
    content += "Port=1319\n";
1534
    content += "Channel=10001\n";
1535
    content += "PanelType=Android\n";
1536
    content += string("Firmware=") + VERSION_STRING() + "\n";
1537
    content += "Scale=true\n";
134 andreas 1538
    content += "ToolbarForce=true\n";
118 andreas 1539
    content += "Profiling=false\n";
1540
    content += "Password1=1988\n";
1541
    content += "Password2=1988\n";
1542
    content += "Password3=1988\n";
1543
    content += "Password4=1988\n";
1544
    content += "SystemSoundFile=singleBeep.wav\n";
1545
    content += "SystemSoundState=ON\n";
1546
    content += "SystemSingleBeep=singleBeep01.wav\n";
1547
    content += "SystemDoubleBeep=doubleBeep01.wav\n";
134 andreas 1548
    content += "SystemRotationFix=" + string(localSettings.systemRotationFix ? "TRUE" : "FALSE") + "\n";
1549
    content += "UUID=" + localSettings.uuid + "\n";
118 andreas 1550
    content += "FTPuser=administrator\n";
1551
    content += "FTPpassword=password\n";
1552
    content += "FTPsurface=tpanel.tp4\n";
1553
    content += "FTPpassive=true\n";
1554
    content += "FTPdownloadTime=0\n";
127 andreas 1555
    content += "SIP_PROXY=" + localSettings.sip_proxy + "\n";
118 andreas 1556
    content += "SIP_PORT=" + std::to_string(localSettings.sip_port) + "\n";
127 andreas 1557
    content += "SIP_PORTTLS=" + std::to_string(localSettings.sip_portTLS) + "\n";
1558
    content += "SIP_STUN=" + localSettings.sip_stun + "\n";
1559
    content += "SIP_DOMAIN=" + localSettings.sip_domain + "\n";
1560
    content += "SIP_USER=" + localSettings.sip_user + "\n";
1561
    content += "SIP_PASSWORD=" + localSettings.sip_password + "\n";
1562
    content += "SIP_IPV4=" + string(localSettings.sip_ipv4 ? "TRUE" : "FALSE") + "\n";
139 andreas 1563
    content += "SIP_IPV6=" + string(localSettings.sip_ipv6 ? "TRUE" : "FALSE") + "\n";
1564
    content += "SIP_IPHONE=" + string(localSettings.sip_iphone ? "TRUE" : "FALSE") + "\n";
127 andreas 1565
    content += "SIP_FIREWALL=" + sipFirewallToString(localSettings.sip_firewall) + "\n";
1566
    content += "SIP_ENABLED=" + string(localSettings.sip_ipv6 ? "TRUE" : "FALSE") + "\n";
118 andreas 1567
 
1568
    return content;
1569
}
1570
 
59 andreas 1571
/**
21 andreas 1572
 * @brief TConfig::findConfig search for the location of the configuration file.
1573
 *
1574
 * If there was no configuration file given on the command line, this method
1575
 * searches for a configuration file on a few standard directories. This are:
1576
 *
1577
 *     /etc/tpanel.conf
1578
 *     /etc/tpanel/tpanel.conf
1579
 *     /usr/etc/tpanel.conf
1580
 *     $HOME/.tpanel.conf
1581
 *
1582
 * On macOS the following additional directories are searched:
1583
 *
1584
 *     /opt/local/etc/tpanel.conf
1585
 *     /opt/local/etc/tpanel/tpanel.conf
1586
 *     /opt/local/usr/etc/tpanel.conf
1587
 *
1588
 * @return On success `true`, otherwise `false`.
1589
 */
2 andreas 1590
bool TConfig::findConfig()
1591
{
292 andreas 1592
    TLock<std::mutex> guard(config_mutex);
1593
 
90 andreas 1594
    char *HOME = nullptr;
1595
    string sFileName;
22 andreas 1596
 
90 andreas 1597
    if (!mPath.empty())
1598
    {
1599
        size_t pos = mPath.find_last_of("/");
2 andreas 1600
 
90 andreas 1601
        if (pos != string::npos)
1602
        {
1603
            localSettings.path = mPath.substr(0, pos);
1604
            localSettings.name = mPath.substr(pos+1);
192 andreas 1605
            localSettings_temp.path = localSettings.path;
1606
            localSettings_temp.name = localSettings.name;
90 andreas 1607
            mCFile = mPath;
1608
            return !mCFile.empty();
1609
        }
2 andreas 1610
 
192 andreas 1611
        localSettings.path = ".";
90 andreas 1612
        localSettings.name = mPath;
192 andreas 1613
        localSettings_temp.path = localSettings.path;
1614
        localSettings_temp.name = localSettings.name;
90 andreas 1615
        mCFile = mPath;
1616
        return !mCFile.empty();
1617
    }
2 andreas 1618
 
90 andreas 1619
    localSettings.name = "tpanel.conf";
51 andreas 1620
    localSettings.password1 = "1988";
1621
    localSettings.password2 = "1988";
1622
    localSettings.password3 = "1988";
1623
    localSettings.password4 = "1988";
71 andreas 1624
    localSettings.systemSound = "singleBeep.wav";
1625
    localSettings.systemSoundState = true;
1626
    localSettings.systemSingleBeep = "singleBeep01.wav";
1627
    localSettings.systemDoubleBeep = "doubleBeep01.wav";
112 andreas 1628
    localSettings.ftpUser = "administrator";
1629
    localSettings.ftpPassword = "password";
115 andreas 1630
    localSettings.ftpSurface = "tpanel.tp4";
21 andreas 1631
#ifdef __ANDROID__
22 andreas 1632
    std::stringstream s;
1633
    TValidateFile vf;
1634
    HOME = getenv("HOME");
2 andreas 1635
 
22 andreas 1636
    if (!HOME || !*HOME)
21 andreas 1637
    {
43 andreas 1638
        s << "Error: Environment variable HOME does not exist!";
22 andreas 1639
        __android_log_print(ANDROID_LOG_ERROR, "tpanel", "%s", s.str().c_str());
1640
        TError::setErrorMsg(s.str());
21 andreas 1641
        return false;
1642
    }
1643
 
22 andreas 1644
    localSettings.path = HOME;
1645
 
88 andreas 1646
    if (!vf.isValidDir(localSettings.path))
21 andreas 1647
    {
22 andreas 1648
        s << "Error: Path " << localSettings.path << " does not exist!";
1649
        __android_log_print(ANDROID_LOG_ERROR, "tpanel", "%s", s.str().c_str());
1650
        TError::setErrorMsg(s.str());
21 andreas 1651
        return false;
1652
    }
1653
 
22 andreas 1654
    sFileName = localSettings.path + "/" + localSettings.name;
21 andreas 1655
 
22 andreas 1656
    if (access(sFileName.c_str(), F_OK) == -1)    // Does the configuration file exist?
21 andreas 1657
    {                                       // No, than create it and also the directory structure
1658
        try
1659
        {
1660
            ofstream cfg(sFileName);
1661
 
118 andreas 1662
            string content = makeConfigDefault(localSettings.path + "/tpanel.log", localSettings.path + "/tpanel");
21 andreas 1663
            cfg.write(content.c_str(), content.size());
1664
            cfg.close();
1665
 
1666
            string path = localSettings.path + "/tpanel";
22 andreas 1667
            TTPInit init(path);
21 andreas 1668
        }
1669
        catch (std::exception& e)
1670
        {
23 andreas 1671
            s << "Error: " << e.what();
22 andreas 1672
            __android_log_print(ANDROID_LOG_ERROR, "tpanel" , "%s", s.str().c_str());
21 andreas 1673
            TError::setErrorMsg(TERRERROR, string("Error: ") + e.what());
1674
            return false;
1675
        }
1676
    }
1677
#else
90 andreas 1678
    if (!(HOME = getenv("HOME")))
116 andreas 1679
        MSG_ERROR("TConfig::findConfig: No environment variable HOME!");
2 andreas 1680
 
90 andreas 1681
    vector<string>::iterator iter;
1682
    bool found = false;
2 andreas 1683
 
90 andreas 1684
    for (iter = mCfgPaths.begin(); iter != mCfgPaths.end(); ++iter)
1685
    {
1686
        string f = *iter + "/tpanel.conf";
1687
 
1688
        if (!access(f.c_str(), R_OK))
1689
        {
1690
            sFileName = f;
1691
            localSettings.path = *iter;
113 andreas 1692
            found = true;
90 andreas 1693
            break;
1694
        }
1695
    }
1696
 
1697
    // The local configuration file has precedence over the global one.
1698
    if (HOME)
1699
    {
1700
        string f = HOME;
113 andreas 1701
#ifndef __ANDROID__
90 andreas 1702
        f += "/.tpanel.conf";
113 andreas 1703
#else
1704
        f += "/tpanel.conf";
1705
#endif
90 andreas 1706
        if (!access(f.data(), R_OK))
1707
        {
1708
            sFileName = f;
1709
            localSettings.path = HOME;
1710
            found = true;
1711
        }
1712
    }
1713
 
192 andreas 1714
    localSettings_temp = localSettings;
1715
 
90 andreas 1716
    if (!found)
1717
    {
118 andreas 1718
        MSG_WARNING("This seems to be the first start because of missing configuration file. Will try to create a default one ...");
1719
 
1720
        if (HOME)
1721
        {
1722
            localSettings.path = HOME;
1723
            sFileName = string(HOME) + "/.tpanel.conf";
1724
 
1725
            try
1726
            {
1727
                ofstream cfg(sFileName);
1728
 
1729
                string content = makeConfigDefault(localSettings.path + "/tpanel.log", localSettings.path + "/tpanel");
1730
                cfg.write(content.c_str(), content.size());
1731
                cfg.close();
1732
 
192 andreas 1733
                localSettings_temp = localSettings;
118 andreas 1734
                string path = localSettings.path + "/tpanel";
1735
                TTPInit init(path);
1736
            }
1737
            catch (std::exception& e)
1738
            {
1739
                cerr << "Error: " << e.what();
1740
                TError::setErrorMsg(TERRERROR, string("Error: ") + e.what());
1741
                return false;
1742
            }
1743
        }
1744
        else
1745
        {
1746
            MSG_ERROR("TConfig::findConfig: Can't find any configuration file!");
1747
            TError::setError();
1748
            sFileName.clear();
1749
            localSettings.name.clear();
1750
            localSettings.path.clear();
192 andreas 1751
            localSettings_temp = localSettings;
118 andreas 1752
        }
90 andreas 1753
    }
21 andreas 1754
#endif
90 andreas 1755
    mCFile = sFileName;
1756
    return !sFileName.empty();
2 andreas 1757
}
1758
 
21 andreas 1759
/**
1760
 * @brief TConfig::readConfig reads a config file.
1761
 *
1762
 * This method reads a config file and stores the known options into the
1763
 * struct localSettings.
1764
 *
1765
 * @return `true` on success.\n
1766
 * Returns `false` on error and sets the internal error.
1767
 */
2 andreas 1768
bool TConfig::readConfig()
1769
{
240 andreas 1770
#if defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_SIMULATOR)
239 andreas 1771
    QASettings settings;
1772
    localSettings.path = QASettings::getLibraryPath().toStdString();
1773
    localSettings.project = localSettings.path + "/tpanel";
242 andreas 1774
    localSettings.version = "1.0";
1775
    localSettings.ptype = "iPhone";
1776
    localSettings.max_cache = 100;
1777
    localSettings.logLevel = SLOG_NONE;
1778
    localSettings.logLevelBits = HLOG_NONE;
239 andreas 1779
    mkdir(localSettings.project.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
1780
    localSettings.name = "tpanel.cfg";
1781
    localSettings.scale = true;
1782
    localSettings.tbforce = false;
1783
    localSettings.tbsuppress = false;
1784
 
1785
    // Settings for NetLinx
1786
    settings.registerDefaultPrefs();
1787
    localSettings.server = settings.getNetlinxIP().toStdString();
1788
    localSettings.port = settings.getNetlinxPort();
1789
    localSettings.ID = settings.getNetlinxChannel();
1790
    localSettings.ptype = settings.getNetlinxPanelType().toStdString();
1791
    localSettings.ftpUser = settings.getFTPUser().toStdString();
1792
    localSettings.ftpPassword = settings.getFTPPassword().toStdString();
1793
    localSettings.ftpSurface = settings.getNetlinxSurface().toStdString();
1794
 
1795
    // Settings for SIP
1796
    localSettings.sip_proxy = settings.getSipProxy().toStdString();
1797
    localSettings.sip_port = settings.getSipNetworkPort();
1798
    localSettings.sip_portTLS = settings.getSipNetworkTlsPort();
1799
    localSettings.sip_stun = settings.getSipStun().toStdString();
1800
    localSettings.sip_domain = settings.getSipDomain().toStdString();
1801
    localSettings.sip_ipv4 = settings.getSipNetworkIpv4();
1802
    localSettings.sip_ipv6 = settings.getSipNetworkIpv6();
1803
    localSettings.sip_user = settings.getSipUser().toStdString();
1804
    localSettings.sip_password = settings.getSipPassword().toStdString();
1805
    localSettings.sip_enabled = settings.getSipEnabled();
1806
    localSettings.sip_iphone = settings.getSipIntegratedPhone();
1807
 
1808
    // Settings for View
1809
    localSettings.scale = settings.getViewScale();
1810
    localSettings.tbforce = settings.getViewToolbarForce();
1811
    localSettings.tbsuppress = !settings.getViewToolbarVisible();
1812
 
1813
    if (localSettings.tbforce)
1814
        localSettings.tbsuppress = false;
1815
 
1816
    localSettings.systemRotationFix = settings.getViewRotation();
1817
 
1818
    // Settings for sound
1819
    localSettings.systemSound = settings.getSoundSystem().toStdString();
1820
    localSettings.systemSingleBeep = settings.getSoundSingleBeep().toStdString();
1821
    localSettings.systemDoubleBeep = settings.getSoundDoubleBeep().toStdString();
1822
    localSettings.systemSoundState = settings.getSoundEnabled();
1823
    localSettings.systemVolume = settings.getSoundVolume();
1824
    localSettings.systemGain = settings.getSoundGain();
1825
 
1826
    // Settings for logging
1827
    unsigned int logLevel = 0;
1828
 
1829
    if (settings.getLoggingInfo())
1830
        logLevel |= HLOG_INFO;
1831
 
1832
    if (settings.getLoggingWarning())
1833
        logLevel |= HLOG_WARNING;
1834
 
1835
    if (settings.getLoggingError())
1836
        logLevel |= HLOG_ERROR;
1837
 
1838
    if (settings.getLoggingDebug())
1839
        logLevel |= HLOG_DEBUG;
1840
 
1841
    if (settings.getLoggingTrace())
1842
        logLevel |= HLOG_TRACE;
1843
 
1844
    if (settings.getLoggingProfile())
1845
        localSettings.profiling = true;
1846
    else
1847
        localSettings.profiling = false;
1848
 
1849
    if (settings.getLoggingLogFormat())
1850
        localSettings.longformat = true;
1851
    else
1852
        localSettings.longformat = false;
1853
 
1854
    localSettings.logLevelBits = logLevel;
1855
    localSettings.logLevel = logLevelBitsToString(logLevel);
243 andreas 1856
    TStreamError::setLogLevel(localSettings.logLevel);
239 andreas 1857
 
242 andreas 1858
    if (settings.getLoggingLogfileEnabled())
1859
    {
1860
        string fname = settings.getLoggingLogfile().toStdString();
1861
 
1862
        if (!fname.empty())
1863
        {
1864
            localSettings.logFile = QASettings::getDocumentPath().toStdString() + "/" + fname;
243 andreas 1865
            TStreamError::setLogFile(localSettings.logFile);
242 andreas 1866
        }
1867
    }
1868
    else
1869
        localSettings.logFile.clear();
1870
 
239 andreas 1871
    if (localSettings.uuid.empty())
1872
    {
1873
        uuid_t uuid;
1874
        char sUUID[256];
1875
 
1876
        uuid_generate_random(uuid);
1877
        uuid_unparse_lower(uuid, sUUID);
1878
        localSettings.uuid.assign(sUUID);
1879
        localSettings_temp = localSettings;
1880
    }
1881
 
1882
    mInitialized = true;
1883
 
242 andreas 1884
    if (IS_LOG_DEBUG())
1885
    {
1886
        cout << "Selected Parameters:" << endl;
1887
        cout << "    Path to cfg.: " << localSettings.path << endl;
1888
        cout << "    Name of cfg.: " << localSettings.name << endl;
1889
        cout << "    Logfile use:  " << (settings.getLoggingLogfileEnabled() ? "YES": "NO") << endl;
1890
        cout << "    Logfile:      " << localSettings.logFile  << endl;
1891
        cout << "    LogLevel:     " << localSettings.logLevel  << endl;
1892
        cout << "    Long format:  " << (localSettings.longformat ? "YES" : "NO")  << endl;
1893
        cout << "    Project path: " << localSettings.project  << endl;
1894
        cout << "    Controller:   " << localSettings.server  << endl;
1895
        cout << "    Port:         " << localSettings.port  << endl;
1896
        cout << "    Channel:      " << localSettings.ID  << endl;
1897
        cout << "    Panel type:   " << localSettings.ptype  << endl;
1898
        cout << "    Firmware ver. " << localSettings.version  << endl;
1899
        cout << "    Scaling:      " << (localSettings.scale ? "YES" : "NO")  << endl;
1900
        cout << "    Profiling:    " << (localSettings.profiling ? "YES" : "NO")  << endl;
1901
        cout << "    Button cache: " << localSettings.max_cache  << endl;
1902
        cout << "    System Sound: " << localSettings.systemSound  << endl;
1903
        cout << "    Sound state:  " << (localSettings.systemSoundState ? "ACTIVATED" : "DEACTIVATED")  << endl;
1904
        cout << "    Single beep:  " << localSettings.systemSingleBeep  << endl;
1905
        cout << "    Double beep:  " << localSettings.systemDoubleBeep  << endl;
1906
        cout << "    Volume:       " << localSettings.systemVolume  << endl;
1907
        cout << "    Gain:         " << localSettings.systemGain  << endl;
1908
        cout << "    Rotation:     " << (localSettings.systemRotationFix ? "LOCKED" : "UNLOCKED")  << endl;
1909
        cout << "    UUID:         " << localSettings.uuid  << endl;
1910
        cout << "    FTP user:     " << localSettings.ftpUser  << endl;
1911
        cout << "    FTP password: " << localSettings.ftpPassword  << endl;
1912
        cout << "    FTP surface:  " << localSettings.ftpSurface  << endl;
1913
        cout << "    FTP passive:  " << (localSettings.ftpPassive ? "YES" : "NO")  << endl;
1914
        cout << "    FTP dl. time: " << localSettings.ftpLastDownload  << endl;
1915
        cout << "    SIP proxy:    " << localSettings.sip_proxy  << endl;
1916
        cout << "    SIP port:     " << localSettings.sip_port  << endl;
1917
        cout << "    SIP TLS port: " << localSettings.sip_portTLS  << endl;
1918
        cout << "    SIP STUN:     " << localSettings.sip_stun  << endl;
1919
        cout << "    SIP doamain:  " << localSettings.sip_domain  << endl;
1920
        cout << "    SIP user:     " << localSettings.sip_user  << endl;
1921
        cout << "    SIP IPv4:     " << (localSettings.sip_ipv4 ? "YES" : "NO")  << endl;
1922
        cout << "    SIP IPv6:     " << (localSettings.sip_ipv6 ? "YES" : "NO")  << endl;
1923
        cout << "    SIP Int.Phone:" << (localSettings.sip_iphone ? "YES" : "NO")  << endl;
1924
        cout << "    SIP firewall: " << sipFirewallToString(localSettings.sip_firewall)  << endl;
1925
        cout << "    SIP enabled:  " << (localSettings.sip_enabled ? "YES" : "NO")  << endl;
1926
    }
239 andreas 1927
#else
35 andreas 1928
    ifstream fs;
2 andreas 1929
 
192 andreas 1930
    mTemporary = false;
35 andreas 1931
    // First initialize the defaults
1932
    localSettings.ID = 0;
1933
    localSettings.port = 1397;
1934
    localSettings.ptype = "android";
1935
    localSettings.version = "1.0";
1936
    localSettings.longformat = false;
43 andreas 1937
    localSettings.profiling = false;
175 andreas 1938
#ifdef __ANDROID__
1939
    localSettings.max_cache = 100;
1940
#else
1941
    localSettings.max_cache = 400;
1942
#endif
71 andreas 1943
    localSettings.systemSoundState = true;
1944
    localSettings.systemSound = "singleBeep.wav";
1945
    localSettings.systemSingleBeep = "singleBeep01.wav";
1946
    localSettings.systemDoubleBeep = "doubleBeep01.wav";
112 andreas 1947
    localSettings.ftpUser = "administrator";
1948
    localSettings.ftpPassword = "password";
115 andreas 1949
    localSettings.ftpSurface = "tpanel.tp4";
116 andreas 1950
    localSettings.sip_port = 5060;
127 andreas 1951
    localSettings.sip_portTLS = 0;
2 andreas 1952
 
35 andreas 1953
    // Now get the settings from file
1954
    try
1955
    {
1956
        fs.open(mCFile.c_str(), fstream::in);
1957
    }
1958
    catch (const fstream::failure e)
1959
    {
192 andreas 1960
        localSettings_temp = localSettings;
116 andreas 1961
        cerr << "TConfig::readConfig: Error on file " << mCFile << ": " << e.what() << endl;
35 andreas 1962
        TError::setError();
1963
        return false;
1964
    }
2 andreas 1965
 
35 andreas 1966
    for (string line; getline(fs, line);)
1967
    {
1968
        size_t pos;
2 andreas 1969
 
35 andreas 1970
        if ((pos = line.find("#")) != string::npos)
1971
        {
1972
            if (pos == 0)
1973
                line.clear();
1974
            else
1975
                line = line.substr(0, pos);
1976
        }
2 andreas 1977
 
35 andreas 1978
        if (line.empty() || line.find("=") == string::npos)
1979
            continue;
2 andreas 1980
 
35 andreas 1981
        vector<string> parts = split(line, "=", true);
2 andreas 1982
 
35 andreas 1983
        if (parts.size() == 2)
1984
        {
1985
            string left = parts[0];
1986
            string right = ltrim(parts[1]);
2 andreas 1987
 
35 andreas 1988
            if (caseCompare(left, "PORT") == 0 && !right.empty())
1989
                localSettings.port = atoi(right.c_str());
1990
            else if (caseCompare(left, "LOGFILE") == 0 && !right.empty())
1991
            {
1992
                localSettings.logFile = right;
23 andreas 1993
                TStreamError::setLogFileOnly(right);
383 andreas 1994
#ifdef __ANDROID__
1995
                if (std::filesystem::is_regular_file(right))
1996
                    mLogFileEnabled = true;
1997
#else
1998
                mLogFileEnabled = true;
1999
#endif
35 andreas 2000
            }
2001
            else if (caseCompare(left, "LOGLEVEL") == 0 && !right.empty())
2002
            {
2003
                TStreamError::setLogLevel(right);
2004
                localSettings.logLevel = right;
59 andreas 2005
                localSettings.logLevelBits = logLevelStrToBits(right);
35 andreas 2006
            }
2007
            else if (caseCompare(left, "ProjectPath") == 0 && !right.empty())
43 andreas 2008
            {
35 andreas 2009
                localSettings.project = right;
43 andreas 2010
#ifdef __ANDROID__
2011
                TValidateFile vf;
2012
 
2013
                if (!vf.isValidFile(right))
2014
                {
2015
                    char *HOME = getenv("HOME");
2016
 
2017
                    if (HOME)
2018
                    {
2019
                        localSettings.project = HOME;
2020
                        localSettings.project += "/tpanel";
2021
                    }
2022
                }
2023
#endif
2024
            }
11 andreas 2025
            else if (caseCompare(left, "System") == 0 && !right.empty())
2026
                localSettings.system = atoi(right.c_str());
2027
            else if (caseCompare(left, "PanelType") == 0 && !right.empty())
35 andreas 2028
                localSettings.ptype = right;
2029
            else if (caseCompare(left, "Address") == 0 && !right.empty())
2030
                localSettings.server = right;
2031
            else if (caseCompare(left, "Firmware") == 0 && !right.empty())
2032
                localSettings.version = right;
2033
            else if (caseCompare(left, "LongFormat") == 0 && !right.empty())
71 andreas 2034
                localSettings.longformat = isTrue(right);
35 andreas 2035
            else if (caseCompare(left, "NoBanner") == 0 && !right.empty())
71 andreas 2036
                localSettings.noBanner = isTrue(right);
35 andreas 2037
            else if (caseCompare(left, "CertCheck") == 0 && !right.empty())
71 andreas 2038
                localSettings.certCheck = isTrue(right);
21 andreas 2039
            else if (caseCompare(left, "Channel") == 0 && !right.empty())
35 andreas 2040
            {
2041
                localSettings.ID = atoi(right.c_str());
2 andreas 2042
 
113 andreas 2043
                if (localSettings.ID < 10000 || localSettings.ID >= 29000)
35 andreas 2044
                {
260 andreas 2045
#ifdef __ANDROID__
2046
                    __android_log_print(ANDROID_LOG_ERROR, "tpanel", "TConfig::readConfig: Invalid port number %s", right.c_str());
2047
#else
116 andreas 2048
                    cerr << "TConfig::readConfig: Invalid port number " << right << endl;
260 andreas 2049
#endif
35 andreas 2050
                    localSettings.ID = 0;
2051
                }
134 andreas 2052
 
2053
                mChannel = localSettings.ID;
35 andreas 2054
            }
26 andreas 2055
            else if (caseCompare(left, "Scale") == 0 && !right.empty())
71 andreas 2056
                localSettings.scale = isTrue(right);
120 andreas 2057
            else if (caseCompare(left, "ToolbarForce") == 0 && !right.empty())
2058
                localSettings.tbforce = isTrue(right);
151 andreas 2059
            else if (caseCompare(left, "ToolbarSuppress") == 0 && !right.empty())
2060
                localSettings.tbsuppress = isTrue(right);
35 andreas 2061
            else if (caseCompare(left, "Profiling") == 0 && !right.empty())
71 andreas 2062
                localSettings.profiling = isTrue(right);
175 andreas 2063
            else if (caseCompare(left, "MaxButtonCache") == 0 && !right.empty())
2064
                localSettings.max_cache = atoi(right.c_str());
51 andreas 2065
            else if (caseCompare(left, "Password1") == 0 && !right.empty())
2066
                localSettings.password1 = right;
2067
            else if (caseCompare(left, "Password2") == 0 && !right.empty())
2068
                localSettings.password2 = right;
2069
            else if (caseCompare(left, "Password3") == 0 && !right.empty())
2070
                localSettings.password3 = right;
2071
            else if (caseCompare(left, "Password4") == 0 && !right.empty())
2072
                localSettings.password4 = right;
71 andreas 2073
            else if (caseCompare(left, "SystemSoundFile") == 0 && !right.empty())
2074
                localSettings.systemSound = right;
2075
            else if (caseCompare(left, "SystemSoundState") == 0 && !right.empty())
2076
                localSettings.systemSoundState = isTrue(right);
2077
            else if (caseCompare(left, "SystemSingleBeep") == 0 && !right.empty())
2078
                localSettings.systemSingleBeep = right;
2079
            else if (caseCompare(left, "SystemDoubleBeep") == 0 && !right.empty())
2080
                localSettings.systemDoubleBeep = right;
141 andreas 2081
            else if (caseCompare(left, "SystemVolume") == 0 && !right.empty())
2082
            {
2083
                int volume = atoi(right.c_str());
2084
 
2085
                if (volume < 0)
2086
                    volume = 0;
2087
                else if (volume > 100)
2088
                    volume = 100;
2089
 
2090
                localSettings.systemVolume = volume;
2091
            }
2092
            else if (caseCompare(left, "SystemGain") == 0 && !right.empty())
2093
            {
2094
                int gain = atoi(right.c_str());
2095
 
2096
                if (gain < 0)
2097
                    gain = 0;
2098
                else if (gain > 100)
2099
                    gain = 100;
2100
 
2101
                localSettings.systemGain = gain;
2102
            }
134 andreas 2103
            else if (caseCompare(left, "SystemRotationFix") == 0 && !right.empty())
2104
                localSettings.systemRotationFix = isTrue(right);
2105
            else if (caseCompare(left, "UUID") == 0 && !right.empty())
2106
                localSettings.uuid = right;
112 andreas 2107
            else if (caseCompare(left, "FTPuser") == 0 && !right.empty())       // FTP credentials
2108
                localSettings.ftpUser = right;
2109
            else if (caseCompare(left, "FTPpassword") == 0 && !right.empty())
2110
                localSettings.ftpPassword = right;
115 andreas 2111
            else if (caseCompare(left, "FTPsurface") == 0 && !right.empty())
2112
                localSettings.ftpSurface = right;
116 andreas 2113
            else if (caseCompare(left, "FTPpassive") == 0 && !right.empty())
2114
                localSettings.ftpPassive = isTrue(right);
115 andreas 2115
            else if (caseCompare(left, "FTPdownloadTime") == 0 && !right.empty())
2116
                localSettings.ftpLastDownload = atol(right.c_str());
104 andreas 2117
            else if (caseCompare(left, "SIP_PROXY") == 0 && !right.empty())     // SIP settings starting here
2118
                localSettings.sip_proxy = right;
2119
            else if (caseCompare(left, "SIP_PORT") == 0 && !right.empty())
2120
                localSettings.sip_port = atoi(right.c_str());
127 andreas 2121
            else if (caseCompare(left, "SIP_PORTTLS") == 0 && !right.empty())
2122
                localSettings.sip_portTLS = atoi(right.c_str());
104 andreas 2123
            else if (caseCompare(left, "SIP_STUN") == 0 && !right.empty())
2124
                localSettings.sip_stun = right;
2125
            else if (caseCompare(left, "SIP_DOMAIN") == 0 && !right.empty())
2126
                localSettings.sip_domain = right;
2127
            else if (caseCompare(left, "SIP_USER") == 0 && !right.empty())
2128
                localSettings.sip_user = right;
2129
            else if (caseCompare(left, "SIP_PASSWORD") == 0 && !right.empty())
2130
                localSettings.sip_password = right;
127 andreas 2131
            else if (caseCompare(left, "SIP_IPV4") == 0 && !right.empty())
2132
                localSettings.sip_ipv4 = isTrue(right);
2133
            else if (caseCompare(left, "SIP_IPV6") == 0 && !right.empty())
2134
                localSettings.sip_ipv6 = isTrue(right);
139 andreas 2135
            else if (caseCompare(left, "SIP_IPHONE") == 0 && !right.empty())
2136
                localSettings.sip_iphone = isTrue(right);
127 andreas 2137
            else if (caseCompare(left, "SIP_FIREWALL") == 0 && !right.empty())
2138
                localSettings.sip_firewall = sipFirewallStrToEnum(right);
104 andreas 2139
            else if (caseCompare(left, "SIP_ENABLED") == 0 && !right.empty())
2140
                localSettings.sip_enabled = isTrue(right);
26 andreas 2141
        }
35 andreas 2142
    }
2 andreas 2143
 
35 andreas 2144
    fs.close();
116 andreas 2145
    mInitialized = true;
2146
    TStreamError::setLogLevel(localSettings.logLevel);
2147
    TStreamError::setLogFile(localSettings.logFile);
2 andreas 2148
 
134 andreas 2149
    if (localSettings.uuid.empty())
2150
    {
2151
#ifdef __ANDROID__
2152
        QUuid qUid = QUuid::createUuid();
2153
        localSettings.uuid = qUid.toString().toStdString();
2154
#else
2155
        uuid_t uuid;
2156
        char sUUID[256];
2157
 
2158
        uuid_generate_random(uuid);
2159
        uuid_unparse_lower(uuid, sUUID);
2160
        localSettings.uuid.assign(sUUID);
2161
#endif
192 andreas 2162
        localSettings_temp = localSettings;
134 andreas 2163
        saveSettings();
2164
    }
192 andreas 2165
 
23 andreas 2166
    if (TStreamError::checkFilter(HLOG_DEBUG))
35 andreas 2167
    {
2168
        MSG_INFO("Selected Parameters:");
2169
        MSG_INFO("    Path to cfg.: " << localSettings.path);
2170
        MSG_INFO("    Name of cfg.: " << localSettings.name);
383 andreas 2171
        MSG_INFO("    Logf. enabled:" << (mLogFileEnabled ? "ENABLED" : "DISABLED"));
2172
#ifdef __ANDROID__
2173
        if (mLogFileEnabled)
2174
            MSG_INFO("    Logfile:      " << localSettings.logFile);
2175
#else
23 andreas 2176
        MSG_INFO("    Logfile:      " << localSettings.logFile);
2177
#endif
26 andreas 2178
        MSG_INFO("    LogLevel:     " << localSettings.logLevel);
35 andreas 2179
        MSG_INFO("    Long format:  " << (localSettings.longformat ? "YES" : "NO"));
383 andreas 2180
        MSG_INFO("    Logf. enabled:" << (localSettings.logFile))
35 andreas 2181
        MSG_INFO("    Project path: " << localSettings.project);
23 andreas 2182
#ifndef __ANDROID__
35 andreas 2183
        MSG_INFO("    Show banner:  " << (localSettings.noBanner ? "NO" : "YES"));
23 andreas 2184
#endif
35 andreas 2185
        MSG_INFO("    Controller:   " << localSettings.server);
2186
        MSG_INFO("    Port:         " << localSettings.port);
2187
        MSG_INFO("    Channel:      " << localSettings.ID);
2188
        MSG_INFO("    Panel type:   " << localSettings.ptype);
2189
        MSG_INFO("    Firmware ver. " << localSettings.version);
26 andreas 2190
        MSG_INFO("    Scaling:      " << (localSettings.scale ? "YES" : "NO"));
35 andreas 2191
        MSG_INFO("    Profiling:    " << (localSettings.profiling ? "YES" : "NO"));
175 andreas 2192
        MSG_INFO("    Button cache: " << localSettings.max_cache);
71 andreas 2193
        MSG_INFO("    System Sound: " << localSettings.systemSound);
2194
        MSG_INFO("    Sound state:  " << (localSettings.systemSoundState ? "ACTIVATED" : "DEACTIVATED"));
2195
        MSG_INFO("    Single beep:  " << localSettings.systemSingleBeep);
2196
        MSG_INFO("    Double beep:  " << localSettings.systemDoubleBeep);
141 andreas 2197
        MSG_INFO("    Volume:       " << localSettings.systemVolume);
2198
        MSG_INFO("    Gain:         " << localSettings.systemGain);
134 andreas 2199
        MSG_INFO("    Rotation:     " << (localSettings.systemRotationFix ? "LOCKED" : "UNLOCKED"));
2200
        MSG_INFO("    UUID:         " << localSettings.uuid);
115 andreas 2201
        MSG_INFO("    FTP user:     " << localSettings.ftpUser);
2202
        MSG_INFO("    FTP password: " << localSettings.ftpPassword);
2203
        MSG_INFO("    FTP surface:  " << localSettings.ftpSurface);
116 andreas 2204
        MSG_INFO("    FTP passive:  " << (localSettings.ftpPassive ? "YES" : "NO"));
115 andreas 2205
        MSG_INFO("    FTP dl. time: " << localSettings.ftpLastDownload);
127 andreas 2206
        MSG_INFO("    SIP proxy:    " << localSettings.sip_proxy);
2207
        MSG_INFO("    SIP port:     " << localSettings.sip_port);
2208
        MSG_INFO("    SIP TLS port: " << localSettings.sip_portTLS);
2209
        MSG_INFO("    SIP STUN:     " << localSettings.sip_stun);
2210
        MSG_INFO("    SIP doamain:  " << localSettings.sip_domain);
2211
        MSG_INFO("    SIP user:     " << localSettings.sip_user);
2212
        MSG_INFO("    SIP IPv4:     " << (localSettings.sip_ipv4 ? "YES" : "NO"));
2213
        MSG_INFO("    SIP IPv6:     " << (localSettings.sip_ipv6 ? "YES" : "NO"));
139 andreas 2214
        MSG_INFO("    SIP Int.Phone:" << (localSettings.sip_iphone ? "YES" : "NO"));
127 andreas 2215
        MSG_INFO("    SIP firewall: " << sipFirewallToString(localSettings.sip_firewall));
2216
        MSG_INFO("    SIP enabled:  " << (localSettings.sip_enabled ? "YES" : "NO"));
35 andreas 2217
    }
2 andreas 2218
 
192 andreas 2219
    localSettings_temp = localSettings;
239 andreas 2220
#endif
35 andreas 2221
    return true;
2 andreas 2222
}
2223
 
21 andreas 2224
/**
2225
 * @brief TConfig::split splitts a string into parts.
2226
 *
2227
 * The method splitts a string into parts separated by \p seps. It puts the
2228
 * parts into a vector array.
2229
 *
2230
 * @param str           The string to split
2231
 * @param seps          The separator(s)
2232
 * @param trimEmpty     `true` = trum the parts.
2233
 *
2234
 * @return A vector array containing the parts of the string \p str.
2235
 */
2 andreas 2236
vector<string> TConfig::split(const string& str, const string& seps, const bool trimEmpty)
2237
{
383 andreas 2238
//    DECL_TRACER("TConfig::split(const string& str, const string& seps, const bool trimEmpty)");
116 andreas 2239
 
2 andreas 2240
	size_t pos = 0, mark = 0;
2241
	vector<string> parts;
2242
 
2243
	for (auto it = str.begin(); it != str.end(); ++it)
2244
	{
2245
		for (auto sepIt = seps.begin(); sepIt != seps.end(); ++sepIt)
2246
		{
2247
			if (*it == *sepIt)
2248
			{
2249
				size_t len = pos - mark;
2250
				parts.push_back(str.substr(mark, len));
2251
				mark = pos + 1;
2252
				break;
2253
			}
2254
		}
2255
 
2256
		pos++;
2257
	}
2258
 
2259
	parts.push_back(str.substr(mark));
2260
 
2261
	if (trimEmpty)
2262
	{
2263
		vector<string> nparts;
2264
 
2265
		for (auto it = parts.begin(); it != parts.end(); ++it)
2266
		{
2267
			if (it->empty())
2268
				continue;
2269
 
2270
			nparts.push_back(*it);
2271
		}
2272
 
2273
		return nparts;
2274
	}
2275
 
2276
	return parts;
2277
}
2278
 
21 andreas 2279
/**
2280
 * @brief TConfig::caseCompare compares 2 strings
2281
 *
2282
 * This method compares 2 strings case insensitive. This means that it ignores
2283
 * the case of the letters. For example:
2284
 *
2285
 *     BLAME
2286
 *     blame
2287
 *     Blame
2288
 *
2289
 * are all the same and would return 0, which means _equal_.
2290
 *
2291
 * @param str1  1st string to compare
2292
 * @param str2  2nd string to compare
2293
 *
2294
 * @return 0 if the strings are equal\n
2295
 * less than 0 if the byte of \p str1 is bigger than the byte of \p str2\n
2296
 * grater than 0 if the byte of \p str1 is smaller than the byte of \p str2.
2297
 */
2 andreas 2298
int TConfig::caseCompare(const string& str1, const string& str2)
2299
{
383 andreas 2300
//    DECL_TRACER("TConfig::caseCompare(const string& str1, const string& str2)");
116 andreas 2301
 
2 andreas 2302
	size_t i = 0;
2303
 
2304
	if (str1.length() != str2.length())
2305
		return ((str1.length() < str2.length()) ? -1 : 1);
2306
 
2307
	for (auto it = str1.begin(); it != str1.end(); ++it)
2308
	{
2309
		if (tolower(*it) != tolower(str2.at(i)))
2310
			return (int)(*it - str2.at(i));
2311
 
2312
		i++;
2313
	}
2314
 
2315
	return 0;
2316
}