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