Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
193 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
 */
18
 
19
#include <unistd.h>
20
#include "tsettings.h"
77 andreas 21
#include "texpat++.h"
22 andreas 22
#include "tconfig.h"
23
#include "ttpinit.h"
2 andreas 24
 
186 andreas 25
#if __cplusplus < 201402L
26
#   error "This module requires at least C++14 standard!"
27
#else
28
#   if __cplusplus < 201703L
29
#       include <experimental/filesystem>
30
namespace fs = std::experimental::filesystem;
31
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
32
#   else
33
#       include <filesystem>
34
#       ifdef __ANDROID__
35
namespace fs = std::__fs::filesystem;
36
#       else
37
namespace fs = std::filesystem;
38
#       endif
39
#   endif
40
#endif
41
 
2 andreas 42
using std::string;
8 andreas 43
using std::vector;
77 andreas 44
using namespace Expat;
2 andreas 45
 
46
TSettings::TSettings(const string& path)
47
    : mPath(path)
48
{
3 andreas 49
    DECL_TRACER("TSettings::TSettings(const string& path)");
186 andreas 50
 
197 andreas 51
    MSG_DEBUG("Loading from path: " << path);
11 andreas 52
    loadSettings(true);
2 andreas 53
}
54
 
11 andreas 55
bool TSettings::loadSettings(bool initial)
2 andreas 56
{
3 andreas 57
    DECL_TRACER("TSettings::loadSettings()");
2 andreas 58
 
11 andreas 59
    if (!initial)
60
    {
61
        mResourceLists.clear();
62
    }
63
 
3 andreas 64
    TError::clear();
65
    string fname = makeFileName(mPath, "prj.xma");
2 andreas 66
 
3 andreas 67
    if (!isValidFile())
68
    {
122 andreas 69
        MSG_ERROR("Error: File " << fname << " doesn't exist or can't be opened!");
70
        TError::setError();
71
        return false;
3 andreas 72
    }
2 andreas 73
 
77 andreas 74
    TExpat xml(fname);
75
    xml.setEncoding(ENC_CP1250);
2 andreas 76
 
77 andreas 77
    if (!xml.parse())
3 andreas 78
        return false;
2 andreas 79
 
77 andreas 80
    int depth = 0;
81
    size_t index = 0;
82
 
178 andreas 83
    if ((index = xml.getElementIndex("projectInfo", &depth)) == TExpat::npos)
84
    {
85
        MSG_ERROR("Couldn't find the project information! Broken surface?");
86
        TError::setError();
87
        return false;
88
    }
89
 
90
    depth++;
91
    mProject.protection = xml.getElement("protection", depth);
92
    mProject.password = xml.getElement("password", depth);
93
    vector<Expat::ATTRIBUTE_t> attr = xml.getAttributes();
94
    mProject.encrypted = xml.getAttributeInt("encrypted", attr);
95
    mProject.panelType = xml.getElement("panelType", depth);
96
    mProject.fileRevision = xml.getElement("fileRevision", depth);
97
    mProject.dealerID = xml.getElement("dealerId", depth);
98
    mProject.jobName = xml.getElement("jobName", depth);
99
    mProject.salesOrder = xml.getElement("salesOrder", depth);
100
    mProject.purchaseOrder = xml.getElement("purchaseOrder", depth);
101
    mProject.jobComment = xml.getElement("jobComment", depth);
102
    mProject.designerID = xml.getElement("designerId", depth);
103
    mProject.creationDate = xml.getElement("creationDate", depth);
104
    mProject.revisionDate = xml.getElement("revisionDate", depth);
105
    mProject.lastSaveDate = xml.getElement("lastSaveDate", depth);
106
    mProject.fileName = xml.getElement("fileName", depth);
107
    mProject.colorChoice = xml.getElement("colorChoice", depth);
108
    mProject.specifyPortCount = xml.getElementInt("specifyPortCount", depth);
109
    mProject.specifyChanCount = xml.getElementInt("specifyChanCount", depth);
110
 
77 andreas 111
    if ((index = xml.getElementIndex("panelSetup", &depth)) == TExpat::npos)
112
    {
113
        MSG_ERROR("Couldn't find the section \"panelSetup\" in file!");
114
        TError::setError();
115
        return false;
116
    }
117
 
78 andreas 118
    depth++;
3 andreas 119
    string value;
77 andreas 120
    mSetup.portCount = xml.getElementInt("portCount", depth);
121
    mSetup.setupPort = xml.getElementInt("setupPort", depth);
122
    mSetup.addressCount = xml.getElementInt("addressCount", depth);
123
    mSetup.channelCount = xml.getElementInt("channelCount", depth);
124
    mSetup.levelCount = xml.getElementInt("levelCount", depth);
125
    mSetup.powerUpPage = xml.getElement("powerUpPage", depth);
2 andreas 126
 
77 andreas 127
    value = xml.getElement("powerUpPopup", depth);
2 andreas 128
 
5 andreas 129
    if (!value.empty())
7 andreas 130
    {
3 andreas 131
        mSetup.powerUpPopup.push_back(value);
77 andreas 132
        bool valid;
8 andreas 133
 
134
        do
135
        {
77 andreas 136
            value = xml.getNextElement("powerUpPopup", depth, &valid);
8 andreas 137
 
77 andreas 138
            if (valid)
78 andreas 139
            {
8 andreas 140
                mSetup.powerUpPopup.push_back(value);
78 andreas 141
                MSG_DEBUG("powerUpPopup: " << value);
142
            }
8 andreas 143
        }
77 andreas 144
        while (valid);
7 andreas 145
    }
2 andreas 146
 
78 andreas 147
    xml.setIndex(index);
77 andreas 148
    mSetup.feedbackBlinkRate = xml.getElementInt("feedbackBlinkRate", depth);
149
    mSetup.startupString = xml.getElement("startupString", depth);
150
    mSetup.wakeupString = xml.getElement("wakeupString", depth);
151
    mSetup.sleepString = xml.getElement("sleepString", depth);
152
    mSetup.standbyString = xml.getElement("standbyString", depth);
153
    mSetup.shutdownString = xml.getElement("shutdownString", depth);
154
    mSetup.idlePage = xml.getElement("idlePage", depth);
155
    mSetup.idleTimeout = xml.getElementInt("idleTimeout", depth);
156
    mSetup.extButtonsKey = xml.getElementInt("extButtonsKey", depth);
157
    mSetup.screenWidth = xml.getElementInt("screenWidth", depth);
158
    mSetup.screenHeight = xml.getElementInt("screenHeight", depth);
159
    mSetup.screenRefresh = xml.getElementInt("screenRefresh", depth);
160
    mSetup.screenRotate = xml.getElementInt("screenRotate", depth);
161
    mSetup.screenDescription = xml.getElement("screenDescription", depth);
162
    mSetup.pageTracking = xml.getElementInt("pageTracking", depth);
163
    mSetup.cursor = xml.getElementInt("cursor", depth);
164
    mSetup.brightness = xml.getElementInt("brightness", depth);
165
    mSetup.lightSensorLevelPort = xml.getElementInt("lightSensorLevelPort", depth);
166
    mSetup.lightSensorLevelCode = xml.getElementInt("lightSensorLevelCode", depth);
167
    mSetup.lightSensorChannelPort = xml.getElementInt("lightSensorChannelPort", depth);
168
    mSetup.lightSensorChannelCode = xml.getElementInt("lightSensorChannelCode", depth);
169
    mSetup.motionSensorChannelPort = xml.getElementInt("motionSensorChannelPort", depth);
170
    mSetup.motionSensorChannelCode = xml.getElementInt("motionSensorChannelCode", depth);
171
    mSetup.batteryLevelPort = xml.getElementInt("batteryLevelPort", depth);
172
    mSetup.batteryLevelCode = xml.getElementInt("batteryLevelCode", depth);
173
    mSetup.irPortAMX38Emit = xml.getElementInt("irPortAMX38Emit", depth);
174
    mSetup.irPortAMX455Emit = xml.getElementInt("irPortAMX455Emit", depth);
175
    mSetup.irPortAMX38Recv = xml.getElementInt("irPortAMX38Recv", depth);
176
    mSetup.irPortAMX455Recv = xml.getElementInt("irPortAMX455Recv", depth);
177
    mSetup.irPortUser1 = xml.getElementInt("irPortUser1", depth);
178
    mSetup.irPortUser2 = xml.getElementInt("irPortUser2", depth);
179
    mSetup.cradleChannelPort = xml.getElementInt("cradleChannelPort", depth);
180
    mSetup.cradleChannelCode = xml.getElementInt("cradleChannelCode", depth);
181
    mSetup.uniqueID = xml.getElementInt("uniqueID", depth);
182
    mSetup.appCreated = xml.getElementInt("appCreated", depth);
183
    mSetup.buildNumber = xml.getElementInt("buildNumber", depth);
184
    mSetup.appModified = xml.getElement("appModified", depth);
185
    mSetup.buildNumberMod = xml.getElementInt("buildNumberMod", depth);
186
    mSetup.buildStatusMod = xml.getElement("buildStatusMod", depth);
187
    mSetup.activePalette = xml.getElementInt("activePalette", depth);
188
    mSetup.marqueeSpeed = xml.getElementInt("marqueeSpeed", depth);
189
    mSetup.setupPagesProject = xml.getElementInt("setupPagesProject", depth);
190
    mSetup.voipCommandPort = xml.getElementInt("voipCommandPort", depth);
4 andreas 191
 
77 andreas 192
    if ((index = xml.getElementIndex("resourceList", &depth)) == TExpat::npos)
193
    {
84 andreas 194
        MSG_WARNING("Missing element \"resourceList\" in file!");
77 andreas 195
    }
8 andreas 196
 
77 andreas 197
    string name, content;
198
    vector<ATTRIBUTE_t> attrs;
199
 
84 andreas 200
    if (index != TExpat::npos)
8 andreas 201
    {
84 andreas 202
        depth++;
203
        size_t oldIndex = 0;
204
        MSG_DEBUG("Index " << index << " and depth " << depth << " and entity " << xml.getElementName());
8 andreas 205
 
84 andreas 206
        do
8 andreas 207
        {
84 andreas 208
            attrs = xml.getAttributes();
209
            string type = xml.getAttribute("type", attrs);
210
            RESOURCE_LIST_T list = findResourceType(type);
211
            MSG_DEBUG("resource type: " << type);
8 andreas 212
 
84 andreas 213
            if (mResourceLists.size() == 0 || list.type.empty())
214
            {
215
                list.type = type;
216
                list.ressource.clear();
217
                mResourceLists.push_back(list);
218
            }
78 andreas 219
 
84 andreas 220
            RESOURCE_T resource;
221
 
222
            while ((index = xml.getNextElementIndex("resource", depth)) != TExpat::npos)
8 andreas 223
            {
84 andreas 224
                while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
8 andreas 225
                {
84 andreas 226
                    string e = name;
8 andreas 227
 
84 andreas 228
                    if (e.compare("name") == 0)
229
                        resource.name = content;
230
                    else if (e.compare("protocol") == 0)
231
                        resource.protocol = content;
232
                    else if (e.compare("host") == 0)
233
                        resource.host = content;
234
                    else if (e.compare("file") == 0)
235
                        resource.file = content;
236
                    else if (e.compare("password") == 0)
237
                    {
238
                        resource.password = content;
239
                        int enc = xml.getAttributeInt("encrypted", attrs);
240
 
241
                        if (enc != 0)
242
                            resource.encrypted = true;
243
                        else
244
                            resource.encrypted = false;
245
                    }
246
                    else if (e.compare("user") == 0)
247
                        resource.user = content;
248
                    else if (e.compare("path") == 0)
249
                        resource.path = content;
250
                    else if (e.compare("refresh") == 0)
251
                        resource.refresh = xml.convertElementToInt(content);
252
                    else if (e.compare("dynamo") == 0)
253
                        resource.dynamo = ((xml.convertElementToInt(content) == 0) ? false : true);
254
                    else if (e.compare("preserve") == 0)
255
                        resource.preserve = ((xml.convertElementToInt(content) == 0) ? false : true);
256
 
257
                    oldIndex = index;
8 andreas 258
                }
78 andreas 259
 
84 andreas 260
                list.ressource.push_back(resource);
261
                MSG_DEBUG("Scheme: " << resource.protocol << ", Host: " << resource.host << ", Path: " << resource.path << ", File: " << resource.file << ", Name: " << resource.name);
262
                resource.clear();
263
 
264
                if (index == TExpat::npos)
265
                    index = oldIndex + 2;
8 andreas 266
            }
267
 
84 andreas 268
            vector<RESOURCE_LIST_T>::iterator itResList;
78 andreas 269
 
84 andreas 270
            for (itResList = mResourceLists.begin(); itResList != mResourceLists.end(); itResList++)
21 andreas 271
            {
84 andreas 272
                if (itResList->type.compare(type) == 0)
273
                {
274
                    mResourceLists.erase(itResList);
275
                    mResourceLists.push_back(list);
276
                    break;
277
                }
21 andreas 278
            }
279
        }
84 andreas 280
        while ((index = xml.getNextElementIndex("resourceList", depth)) != TExpat::npos);
8 andreas 281
    }
282
 
156 andreas 283
    if (xml.getElementIndex("paletteList", &depth) == TExpat::npos)
4 andreas 284
    {
285
        MSG_WARNING("There exists no color palette! There will be only the system colors available.");
286
        return true;
287
    }
288
 
77 andreas 289
    depth++;
4 andreas 290
 
78 andreas 291
    while ((index = xml.getNextElementIndex("palette", depth)) != TExpat::npos)
4 andreas 292
    {
293
        PALETTE_SETUP ps;
294
 
78 andreas 295
        while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
4 andreas 296
        {
78 andreas 297
            if (name.compare("name") == 0)
77 andreas 298
                ps.name = content;
78 andreas 299
            else if (name.compare("file") == 0)
77 andreas 300
                ps.file = content;
78 andreas 301
            else if (name.compare("paletteID") == 0)
77 andreas 302
                ps.paletteID = xml.convertElementToInt(content);
4 andreas 303
        }
304
 
305
        mSetup.palettes.push_back(ps);
306
    }
307
 
3 andreas 308
    return true;
2 andreas 309
}
8 andreas 310
 
311
RESOURCE_LIST_T TSettings::findResourceType(const string& type)
312
{
313
    DECL_TRACER ("TSettings::findResourceType(const string& type)");
314
 
315
    vector<RESOURCE_LIST_T>::iterator iter;
316
 
317
    for (iter = mResourceLists.begin(); iter != mResourceLists.end(); iter++)
318
    {
319
        if (iter->type.compare(type) == 0)
320
            return *iter;
321
    }
322
 
323
    return RESOURCE_LIST_T();
324
}
151 andreas 325
 
326
bool TSettings::isPortrait()
327
{
328
    DECL_TRACER("TSettings::isPortrait()");
329
 
330
    return mSetup.screenWidth < mSetup.screenHeight;
331
}
332
 
333
bool TSettings::isLandscape()
334
{
335
    DECL_TRACER("TSettings::isLandscape()");
336
 
337
    return mSetup.screenWidth > mSetup.screenHeight;
338
}