Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
3 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 <string>
20
#include "tpagelist.h"
78 andreas 21
#include "texpat++.h"
3 andreas 22
#include "tconfig.h"
23
#include "terror.h"
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
 
3 andreas 42
using std::string;
43
using std::vector;
78 andreas 44
using namespace Expat;
3 andreas 45
 
46
TPageList::TPageList()
47
{
23 andreas 48
    DECL_TRACER("TPageList::TPageList()");
186 andreas 49
 
50
    string projectPath = TConfig::getProjectPath();
193 andreas 51
    mProject = makeFileName(projectPath, "prj.xma");
186 andreas 52
 
193 andreas 53
    if (fs::exists(mProject))
54
        initialize();
186 andreas 55
 
193 andreas 56
    projectPath += "/__system";
57
    mSystemProject = makeFileName(projectPath, "prj.xma");
58
 
59
    if (fs::exists(mSystemProject))
60
        initialize(true);
61
 
43 andreas 62
    // Add the virtual progress page
63
    PAGELIST_T pl;
64
    pl.name = "_progress";
65
    pl.pageID = 300;
66
    mPageList.push_back(pl);
3 andreas 67
}
68
 
69
TPageList::~TPageList()
70
{
71
    DECL_TRACER("TPageList::~TPageList()");
72
}
73
 
193 andreas 74
void TPageList::initialize(bool system)
3 andreas 75
{
193 andreas 76
    DECL_TRACER("TPageList::initialize(bool system)");
3 andreas 77
 
78
    TError::clear();
193 andreas 79
    string sProject;
3 andreas 80
 
193 andreas 81
    if (!system)
82
    {
83
        if (mPageList.size() > 0)
84
            mPageList.clear();
11 andreas 85
 
193 andreas 86
        if (mSubPageList.size() > 0)
87
            mSubPageList.clear();
11 andreas 88
 
193 andreas 89
        sProject = mProject;
90
    }
91
    else
3 andreas 92
    {
193 andreas 93
        if (mSystemPageList.size() > 0)
94
            mSystemPageList.clear();
95
 
96
        if (mSystemSubPageList.size() > 0)
97
            mSystemSubPageList.clear();
98
 
99
        sProject = mSystemProject;
100
    }
101
 
194 andreas 102
    if (sProject.empty() || !isValidFile(sProject))
193 andreas 103
    {
104
        TError::setErrorMsg("Empty or invalid project file! <" + sProject + ">");
3 andreas 105
        MSG_ERROR(TError::getErrorMsg());
106
        return;
107
    }
108
 
193 andreas 109
    TExpat xml(sProject);
78 andreas 110
    xml.setEncoding(ENC_CP1250);
3 andreas 111
 
78 andreas 112
    if (!xml.parse())
3 andreas 113
        return;
114
 
78 andreas 115
    int depth = 0;
116
    size_t index = 0;
3 andreas 117
 
78 andreas 118
    if ((index = xml.getElementIndex("pageList", &depth)) == TExpat::npos)
3 andreas 119
    {
78 andreas 120
        MSG_ERROR("Couldn't find the section \"pageList\" in file!");
121
        TError::setError();
3 andreas 122
        return;
123
    }
124
 
78 andreas 125
    do
3 andreas 126
    {
78 andreas 127
        vector<ATTRIBUTE_t> attrs = xml.getAttributes();
128
        string attribute = xml.getAttribute("type", attrs);
3 andreas 129
 
78 andreas 130
        if (attribute.empty())
3 andreas 131
        {
193 andreas 132
            TError::setErrorMsg("Missing element \"pageList\" in file " + sProject);
78 andreas 133
            MSG_ERROR(TError::getErrorMsg());
134
            return;
135
        }
136
        else if (attribute.compare("page") != 0 && attribute.compare("subpage") != 0)
137
        {
138
            TError::setErrorMsg("Invalid page type " + attribute + " found!");
139
            MSG_ERROR(TError::getErrorMsg());
140
            return;
141
        }
142
 
143
        while ((index = xml.getNextElementIndex("pageEntry", depth+1)) != TExpat::npos)
144
        {
3 andreas 145
            PAGELIST_T pl;
146
            SUBPAGELIST_T spl;
147
 
148
            pl.clear();
149
            spl.clear();
78 andreas 150
            string e, content;
3 andreas 151
 
78 andreas 152
            while ((index = xml.getNextElementFromIndex(index, &e, &content, nullptr)) != TExpat::npos)
3 andreas 153
            {
154
                if (attribute.compare("page") == 0)
155
                {
156
                    if (e.compare("name") == 0)
78 andreas 157
                        pl.name = content;
3 andreas 158
                    else if (e.compare("pageID") == 0)
78 andreas 159
                        pl.pageID = xml.convertElementToInt(content);
3 andreas 160
                    else if (e.compare("file") == 0)
78 andreas 161
                        pl.file = content;
3 andreas 162
                    else if (e.compare("isValid") == 0)
78 andreas 163
                        pl.isValid = xml.convertElementToInt(content);
3 andreas 164
                }
165
                else if (attribute.compare("subpage") == 0)
166
                {
167
                    if (e.compare("name") == 0)
78 andreas 168
                        spl.name = content;
3 andreas 169
                    else if (e.compare("pageID") == 0)
78 andreas 170
                        spl.pageID = xml.convertElementToInt(content);
3 andreas 171
                    else if (e.compare("file") == 0)
78 andreas 172
                        spl.file = content;
3 andreas 173
                    else if (e.compare("group") == 0)
78 andreas 174
                        spl.group = content;
3 andreas 175
                    else if (e.compare("isValid") == 0)
78 andreas 176
                        spl.isValid = xml.convertElementToInt(content);
3 andreas 177
                    else if (e.compare("popupType") == 0)
78 andreas 178
                        spl.popupType = xml.convertElementToInt(content);
3 andreas 179
                }
180
            }
181
 
182
            if (attribute.compare("page") == 0)
65 andreas 183
            {
193 andreas 184
                if (!system)
185
                    mPageList.push_back(pl);
186
                else
187
                    mSystemPageList.push_back(pl);
65 andreas 188
            }
3 andreas 189
            else if (attribute.compare("subpage") == 0)
65 andreas 190
            {
193 andreas 191
                if (!system)
192
                    mSubPageList.push_back(spl);
193
                else
194
                    mSystemSubPageList.push_back(spl);
65 andreas 195
            }
3 andreas 196
        }
197
 
78 andreas 198
        attribute.clear();
199
        attrs.clear();
3 andreas 200
    }
78 andreas 201
    while ((index = xml.getNextElementIndex("pageList", depth)) != TExpat::npos);
3 andreas 202
}
203
 
193 andreas 204
PAGELIST_T TPageList::findPage(const std::string& name, bool system)
3 andreas 205
{
193 andreas 206
    DECL_TRACER("TPageList::findPage(const std::string& name, bool system)");
3 andreas 207
 
208
    vector<PAGELIST_T>::iterator iter;
209
    PAGELIST_T page;
210
 
194 andreas 211
    if (!system && mPageList.size() > 0)
3 andreas 212
    {
193 andreas 213
        for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
3 andreas 214
        {
193 andreas 215
            if (iter->name.compare(name) == 0)
216
            {
217
                page = *iter;
218
                return page;
219
            }
3 andreas 220
        }
221
    }
194 andreas 222
    else if (mSystemPageList.size() > 0)
193 andreas 223
    {
224
        for (iter = mSystemPageList.begin(); iter != mSystemPageList.end(); ++iter)
225
        {
226
            if (iter->name.compare(name) == 0)
227
            {
228
                page = *iter;
229
                return page;
230
            }
231
        }
232
    }
3 andreas 233
 
234
    TError::setErrorMsg("Page " + name + " not found!");
235
    TError::setError();
236
    return page;
237
}
238
 
239
PAGELIST_T TPageList::findPage(int pageID)
240
{
241
    DECL_TRACER("TPageList::findPage(int pageID)");
242
 
243
    vector<PAGELIST_T>::iterator iter;
244
    PAGELIST_T page;
245
 
193 andreas 246
    if (pageID < 5000)
3 andreas 247
    {
193 andreas 248
        for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
3 andreas 249
        {
193 andreas 250
            if (iter->pageID == pageID)
251
            {
252
                page = *iter;
253
                return page;
254
            }
3 andreas 255
        }
256
    }
193 andreas 257
    else
258
    {
259
        for (iter = mSystemPageList.begin(); iter != mSystemPageList.end(); ++iter)
260
        {
261
            if (iter->pageID == pageID)
262
            {
263
                page = *iter;
264
                return page;
265
            }
266
        }
267
    }
3 andreas 268
 
269
    TError::setErrorMsg("Page " + std::to_string(pageID) + " not found!");
270
    TError::setError();
271
    return page;
272
}
273
 
193 andreas 274
SUBPAGELIST_T TPageList::findSubPage(const std::string& name, bool system)
3 andreas 275
{
193 andreas 276
    DECL_TRACER("TPageList::findSubPage(const std::string& name, bool system)");
3 andreas 277
 
278
    vector<SUBPAGELIST_T>::iterator iter;
279
    SUBPAGELIST_T page;
280
 
194 andreas 281
    if (!system && mSubPageList.size() > 0)
3 andreas 282
    {
193 andreas 283
        for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
3 andreas 284
        {
193 andreas 285
            if (iter->name.compare(name) == 0)
286
            {
287
                page = *iter;
288
                return page;
289
            }
3 andreas 290
        }
291
    }
194 andreas 292
    else if (mSystemSubPageList.size() > 0)
193 andreas 293
    {
294
        for (iter = mSystemSubPageList.begin(); iter != mSystemSubPageList.end(); ++iter)
295
        {
296
            if (iter->name.compare(name) == 0)
297
            {
298
                page = *iter;
299
                return page;
300
            }
301
        }
302
    }
3 andreas 303
 
304
    TError::setErrorMsg("Subpage " + name + " not found!");
305
    TError::setError();
306
    return page;
307
}
308
 
309
SUBPAGELIST_T TPageList::findSubPage(int pageID)
310
{
311
    DECL_TRACER("TPageList::findSubPage(int pageID)");
312
 
313
    vector<SUBPAGELIST_T>::iterator iter;
314
    SUBPAGELIST_T page;
315
 
193 andreas 316
    if (pageID < 5000)
3 andreas 317
    {
193 andreas 318
        for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
3 andreas 319
        {
193 andreas 320
            if (iter->pageID == pageID)
321
            {
322
                page = *iter;
323
                return page;
324
            }
3 andreas 325
        }
326
    }
193 andreas 327
    else
328
    {
329
        for (iter = mSystemSubPageList.begin(); iter != mSystemSubPageList.end(); ++iter)
330
        {
331
            if (iter->pageID == pageID)
332
            {
333
                page = *iter;
334
                return page;
335
            }
336
        }
337
    }
3 andreas 338
 
339
    TError::setErrorMsg("Subpage " + std::to_string(pageID) + " not found!");
340
    TError::setError();
341
    return page;
342
}