Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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