Subversion Repositories tpanel

Rev

Rev 449 | Rev 482 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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