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
 
25
using std::string;
26
using std::vector;
78 andreas 27
using namespace Expat;
3 andreas 28
 
29
TPageList::TPageList()
30
{
23 andreas 31
    DECL_TRACER("TPageList::TPageList()");
3 andreas 32
    mProject = makeFileName(TConfig::getProjectPath(), "prj.xma");
33
    initialize();
43 andreas 34
    // Add the virtual progress page
35
    PAGELIST_T pl;
36
    pl.name = "_progress";
37
    pl.pageID = 300;
38
    mPageList.push_back(pl);
3 andreas 39
}
40
 
41
TPageList::~TPageList()
42
{
43
    DECL_TRACER("TPageList::~TPageList()");
44
}
45
 
46
void TPageList::initialize()
47
{
48
    DECL_TRACER("TPageList::initialize()");
49
 
50
    TError::clear();
51
 
11 andreas 52
    if (mPageList.size() > 0)
53
        mPageList.clear();
54
 
55
    if (mSubPageList.size() > 0)
56
        mSubPageList.clear();
57
 
3 andreas 58
    if (mProject.empty() || !isValidFile(mProject))
59
    {
60
        TError::setErrorMsg("Empty or invalid project file! <" + mProject + ">");
61
        MSG_ERROR(TError::getErrorMsg());
62
        return;
63
    }
64
 
78 andreas 65
    TExpat xml(mProject);
66
    xml.setEncoding(ENC_CP1250);
3 andreas 67
 
78 andreas 68
    if (!xml.parse())
3 andreas 69
        return;
70
 
78 andreas 71
    int depth = 0;
72
    size_t index = 0;
3 andreas 73
 
78 andreas 74
    if ((index = xml.getElementIndex("pageList", &depth)) == TExpat::npos)
3 andreas 75
    {
78 andreas 76
        MSG_ERROR("Couldn't find the section \"pageList\" in file!");
77
        TError::setError();
3 andreas 78
        return;
79
    }
80
 
78 andreas 81
    do
3 andreas 82
    {
78 andreas 83
        vector<ATTRIBUTE_t> attrs = xml.getAttributes();
84
        string attribute = xml.getAttribute("type", attrs);
3 andreas 85
 
78 andreas 86
        if (attribute.empty())
3 andreas 87
        {
78 andreas 88
            TError::setErrorMsg("Missing element \"pageList\" in file " + mProject);
89
            MSG_ERROR(TError::getErrorMsg());
90
            return;
91
        }
92
        else if (attribute.compare("page") != 0 && attribute.compare("subpage") != 0)
93
        {
94
            TError::setErrorMsg("Invalid page type " + attribute + " found!");
95
            MSG_ERROR(TError::getErrorMsg());
96
            return;
97
        }
98
 
99
        while ((index = xml.getNextElementIndex("pageEntry", depth+1)) != TExpat::npos)
100
        {
3 andreas 101
            PAGELIST_T pl;
102
            SUBPAGELIST_T spl;
103
 
104
            pl.clear();
105
            spl.clear();
78 andreas 106
            string e, content;
3 andreas 107
 
78 andreas 108
            while ((index = xml.getNextElementFromIndex(index, &e, &content, nullptr)) != TExpat::npos)
3 andreas 109
            {
110
                if (attribute.compare("page") == 0)
111
                {
112
                    if (e.compare("name") == 0)
78 andreas 113
                        pl.name = content;
3 andreas 114
                    else if (e.compare("pageID") == 0)
78 andreas 115
                        pl.pageID = xml.convertElementToInt(content);
3 andreas 116
                    else if (e.compare("file") == 0)
78 andreas 117
                        pl.file = content;
3 andreas 118
                    else if (e.compare("isValid") == 0)
78 andreas 119
                        pl.isValid = xml.convertElementToInt(content);
3 andreas 120
                }
121
                else if (attribute.compare("subpage") == 0)
122
                {
123
                    if (e.compare("name") == 0)
78 andreas 124
                        spl.name = content;
3 andreas 125
                    else if (e.compare("pageID") == 0)
78 andreas 126
                        spl.pageID = xml.convertElementToInt(content);
3 andreas 127
                    else if (e.compare("file") == 0)
78 andreas 128
                        spl.file = content;
3 andreas 129
                    else if (e.compare("group") == 0)
78 andreas 130
                        spl.group = content;
3 andreas 131
                    else if (e.compare("isValid") == 0)
78 andreas 132
                        spl.isValid = xml.convertElementToInt(content);
3 andreas 133
                    else if (e.compare("popupType") == 0)
78 andreas 134
                        spl.popupType = xml.convertElementToInt(content);
3 andreas 135
                }
136
            }
137
 
138
            if (attribute.compare("page") == 0)
65 andreas 139
            {
140
                MSG_TRACE("Added page " << pl.pageID << ", " << pl.name << " to page list.");
3 andreas 141
                mPageList.push_back(pl);
65 andreas 142
            }
3 andreas 143
            else if (attribute.compare("subpage") == 0)
65 andreas 144
            {
145
                MSG_TRACE("Added subpage " << spl.pageID << ", " << spl.name << " to subpage list.");
3 andreas 146
                mSubPageList.push_back(spl);
65 andreas 147
            }
3 andreas 148
        }
149
 
78 andreas 150
        attribute.clear();
151
        attrs.clear();
3 andreas 152
    }
78 andreas 153
    while ((index = xml.getNextElementIndex("pageList", depth)) != TExpat::npos);
3 andreas 154
}
155
 
156
PAGELIST_T TPageList::findPage(const std::string& name)
157
{
158
    DECL_TRACER("TPageList::findPage(const std::string& name)");
159
 
160
    vector<PAGELIST_T>::iterator iter;
161
    PAGELIST_T page;
162
 
118 andreas 163
    for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
3 andreas 164
    {
165
        if (iter->name.compare(name) == 0)
166
        {
167
            page = *iter;
168
            return page;
169
        }
170
    }
171
 
172
    TError::setErrorMsg("Page " + name + " not found!");
173
    TError::setError();
174
    return page;
175
}
176
 
177
PAGELIST_T TPageList::findPage(int pageID)
178
{
179
    DECL_TRACER("TPageList::findPage(int pageID)");
180
 
181
    vector<PAGELIST_T>::iterator iter;
182
    PAGELIST_T page;
183
 
118 andreas 184
    for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
3 andreas 185
    {
186
        if (iter->pageID == pageID)
187
        {
188
            page = *iter;
189
            return page;
190
        }
191
    }
192
 
193
    TError::setErrorMsg("Page " + std::to_string(pageID) + " not found!");
194
    TError::setError();
195
    return page;
196
}
197
 
198
SUBPAGELIST_T TPageList::findSubPage(const std::string& name)
199
{
200
    DECL_TRACER("TPageList::findSubPage(const std::string& name)");
201
 
202
    vector<SUBPAGELIST_T>::iterator iter;
203
    SUBPAGELIST_T page;
204
 
118 andreas 205
    for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
3 andreas 206
    {
207
        if (iter->name.compare(name) == 0)
208
        {
209
            page = *iter;
210
            return page;
211
        }
212
    }
213
 
214
    TError::setErrorMsg("Subpage " + name + " not found!");
215
    TError::setError();
216
    return page;
217
}
218
 
219
SUBPAGELIST_T TPageList::findSubPage(int pageID)
220
{
221
    DECL_TRACER("TPageList::findSubPage(int pageID)");
222
 
223
    vector<SUBPAGELIST_T>::iterator iter;
224
    SUBPAGELIST_T page;
225
 
118 andreas 226
    for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
3 andreas 227
    {
228
        if (iter->pageID == pageID)
229
        {
230
            page = *iter;
231
            return page;
232
        }
233
    }
234
 
235
    TError::setErrorMsg("Subpage " + std::to_string(pageID) + " not found!");
236
    TError::setError();
237
    return page;
238
}