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