Subversion Repositories tpanel

Rev

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