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