Subversion Repositories tpanel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2020 to 2023 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 <string>
20
#include "tpageinterface.h"
21
#include "tpagelist.h"
22
#include "tconfig.h"
23
#include "terror.h"
24
 
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
 
42
using std::string;
43
using std::vector;
44
using namespace Expat;
45
 
46
TPageList::TPageList()
47
{
48
    DECL_TRACER("TPageList::TPageList()");
49
 
50
    string projectPath = TConfig::getProjectPath();
51
    mProject = makeFileName(projectPath, "prj.xma");
52
 
53
    if (fs::exists(mProject))
54
        initialize();
55
 
56
    projectPath += "/__system";
57
    mSystemProject = makeFileName(projectPath, "prj.xma");
58
 
59
    if (fs::exists(mSystemProject))
60
        initialize(true);
61
 
62
    // Add the virtual progress page
63
    PAGELIST_T pl;
64
    pl.name = "_progress";
65
    pl.pageID = 300;
66
    mPageList.push_back(pl);
67
}
68
 
69
TPageList::~TPageList()
70
{
71
    DECL_TRACER("TPageList::~TPageList()");
72
}
73
 
74
void TPageList::initialize(bool system)
75
{
76
    DECL_TRACER("TPageList::initialize(bool system)");
77
 
78
    TError::clear();
79
    string sProject;
80
 
81
    if (!system)
82
    {
83
        if (mPageList.size() > 0)
84
            mPageList.clear();
85
 
86
        if (mSubPageList.size() > 0)
87
            mSubPageList.clear();
88
 
89
        sProject = mProject;
90
    }
91
    else
92
    {
93
        if (mSystemPageList.size() > 0)
94
            mSystemPageList.clear();
95
 
96
        if (mSystemSubPageList.size() > 0)
97
            mSystemSubPageList.clear();
98
 
99
        sProject = mSystemProject;
100
    }
101
 
102
    if (sProject.empty() || !isValidFile(sProject))
103
    {
104
        TError::setErrorMsg("Empty or invalid project file! <" + sProject + ">");
105
        MSG_ERROR(TError::getErrorMsg());
106
        return;
107
    }
108
 
109
    TExpat xml(sProject);
110
    xml.setEncoding(ENC_CP1250);
111
 
112
    if (!xml.parse())
113
        return;
114
 
115
    int depth = 0;
116
    size_t index = 0;
117
 
118
    if (xml.getElementIndex("pageList", &depth) == TExpat::npos)
119
    {
120
        MSG_ERROR("Couldn't find the section \"pageList\" in file!");
121
        TError::setError();
122
        return;
123
    }
124
 
125
    do
126
    {
127
        vector<ATTRIBUTE_t> attrs = xml.getAttributes();
128
        string attribute = xml.getAttribute("type", attrs);
129
 
130
        if (attribute.empty())
131
        {
132
            TError::setErrorMsg("Missing element \"pageList\" in file " + sProject);
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
        {
145
            PAGELIST_T pl;
146
            SUBPAGELIST_T spl;
147
 
148
            pl.clear();
149
            spl.clear();
150
            string e, content;
151
 
152
            while ((index = xml.getNextElementFromIndex(index, &e, &content, nullptr)) != TExpat::npos)
153
            {
154
                if (attribute.compare("page") == 0)
155
                {
156
                    if (e.compare("name") == 0)
157
                        pl.name = content;
158
                    else if (e.compare("pageID") == 0)
159
                        pl.pageID = xml.convertElementToInt(content);
160
                    else if (e.compare("file") == 0)
161
                        pl.file = content;
162
                    else if (e.compare("isValid") == 0)
163
                        pl.isValid = xml.convertElementToInt(content);
164
                }
165
                else if (attribute.compare("subpage") == 0)
166
                {
167
                    if (e.compare("name") == 0)
168
                        spl.name = content;
169
                    else if (e.compare("pageID") == 0)
170
                        spl.pageID = xml.convertElementToInt(content);
171
                    else if (e.compare("file") == 0)
172
                        spl.file = content;
173
                    else if (e.compare("group") == 0)
174
                        spl.group = content;
175
                    else if (e.compare("isValid") == 0)
176
                        spl.isValid = xml.convertElementToInt(content);
177
                    else if (e.compare("popupType") == 0)
178
                        spl.popupType = xml.convertElementToInt(content);
179
                }
180
            }
181
 
182
            if (attribute.compare("page") == 0)
183
            {
184
                if (!system)
185
                    mPageList.push_back(pl);
186
                else
187
                    mSystemPageList.push_back(pl);
188
            }
189
            else if (attribute.compare("subpage") == 0)
190
            {
191
                if (!system)
192
                    mSubPageList.push_back(spl);
193
                else
194
                    mSystemSubPageList.push_back(spl);
195
            }
196
        }
197
 
198
        attribute.clear();
199
        attrs.clear();
200
    }
201
    while (xml.getNextElementIndex("pageList", depth) != TExpat::npos);
202
 
203
    loadSubPageSets(&xml);
204
}
205
 
206
void TPageList::loadSubPageSets(TExpat *xml)
207
{
208
    DECL_TRACER("TPageList::loadSubPageSets(TExpat *xml)");
209
 
210
    int depth = 0;
211
    size_t index = 0;
212
    size_t oldIndex = 0;
213
 
214
    if (xml->getElementIndex("subPageSets", &depth) == TExpat::npos)
215
    {
216
        MSG_WARNING("Couldn't find the section \"subPageSets\" in file!");
217
        return;
218
    }
219
 
220
    do
221
    {
222
        while ((index = xml->getNextElementIndex("subPageSetEntry", depth+1)) != TExpat::npos)
223
        {
224
            SUBVIEWLIST_T svl;
225
            SUBVIEWITEM_T svi;
226
            string e, content;
227
 
228
            vector<ATTRIBUTE_t> attrs = xml->getAttributes();
229
            svl.id = xml->getAttributeInt("id", attrs);
230
 
231
            while ((index = xml->getNextElementFromIndex(index, &e, &content, nullptr)) != TExpat::npos)
232
            {
233
                if (e.compare("name") == 0)
234
                    svl.name = content;
235
                else if (e.compare("pgWidth") == 0)
236
                    svl.pgWidth = xml->convertElementToInt(content);
237
                else if (e.compare("pgHeight") == 0)
238
                    svl.pgHeight = xml->convertElementToInt(content);
239
                else if (e.compare("items") == 0)
240
                {
241
                    string et;
242
 
243
                    while ((index = xml->getNextElementFromIndex(index, &et, &content, &attrs)) != TExpat::npos)
244
                    {
245
                        if (et.compare("item") == 0)
246
                        {
247
                            svi.index = xml->getAttributeInt("index", attrs);
248
                            string it;
249
 
250
                            while ((index = xml->getNextElementFromIndex(index, &it, &content, nullptr)) != TExpat::npos)
251
                            {
252
                                if (it.compare("pageID") == 0)
253
                                    svi.pageID = xml->convertElementToInt(content);
254
 
255
                                oldIndex = index;
256
                            }
257
 
258
                            svl.items.push_back(svi);
259
                            svi.index = svi.pageID = 0;
260
 
261
                            if (index == TExpat::npos)
262
                                index = oldIndex + 1;
263
                        }
264
                    }
265
                }
266
            }
267
 
268
            mSubViewList.push_back(svl);
269
            svl.id = svl.pgWidth = svl.pgHeight = 0;
270
            svl.items.clear();
271
            svl.name.clear();
272
        }
273
    }
274
    while (xml->getNextElementIndex("subPageSets", depth) != TExpat::npos);
275
 
276
    if (TStreamError::checkFilter(HLOG_DEBUG))
277
    {
278
        vector<SUBVIEWLIST_T>::iterator iterList;
279
        vector<SUBVIEWITEM_T>::iterator iterItem;
280
 
281
        for (iterList = mSubViewList.begin(); iterList != mSubViewList.end(); ++iterList)
282
        {
283
            MSG_DEBUG("Subview container " << iterList->id << ": " << iterList->name);
284
            MSG_DEBUG("        pgWidth:  " << iterList->pgWidth);
285
            MSG_DEBUG("        pgHeight: " << iterList->pgHeight);
286
 
287
            for (iterItem = iterList->items.begin(); iterItem != iterList->items.end(); ++iterItem)
288
            {
289
                MSG_DEBUG("        Item:     " << iterItem->index << ", pageID: " << iterItem->pageID);
290
            }
291
        }
292
    }
293
}
294
 
295
PAGELIST_T TPageList::findPage(const std::string& name, bool system)
296
{
297
    DECL_TRACER("TPageList::findPage(const std::string& name, bool system)");
298
 
299
    vector<PAGELIST_T>::iterator iter;
300
    PAGELIST_T page;
301
 
302
    if (!system && mPageList.size() > 0)
303
    {
304
        for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
305
        {
306
            if (iter->name.compare(name) == 0)
307
            {
308
                page = *iter;
309
                return page;
310
            }
311
        }
312
    }
313
    else if (mSystemPageList.size() > 0)
314
    {
315
        for (iter = mSystemPageList.begin(); iter != mSystemPageList.end(); ++iter)
316
        {
317
            if (iter->name.compare(name) == 0)
318
            {
319
                page = *iter;
320
                return page;
321
            }
322
        }
323
    }
324
 
325
    TError::setErrorMsg("Page " + name + " not found!");
326
    TError::setError();
327
    return page;
328
}
329
 
330
PAGELIST_T TPageList::findPage(int pageID)
331
{
332
    DECL_TRACER("TPageList::findPage(int pageID)");
333
 
334
    vector<PAGELIST_T>::iterator iter;
335
    PAGELIST_T page;
336
 
337
    if (pageID < SYSTEM_PAGE_START)
338
    {
339
        for (iter = mPageList.begin(); iter != mPageList.end(); ++iter)
340
        {
341
            if (iter->pageID == pageID)
342
            {
343
                page = *iter;
344
                return page;
345
            }
346
        }
347
    }
348
    else
349
    {
350
        for (iter = mSystemPageList.begin(); iter != mSystemPageList.end(); ++iter)
351
        {
352
            if (iter->pageID == pageID)
353
            {
354
                page = *iter;
355
                return page;
356
            }
357
        }
358
    }
359
 
360
    TError::setErrorMsg("Page " + std::to_string(pageID) + " not found!");
361
    TError::setError();
362
    return page;
363
}
364
 
365
SUBPAGELIST_T TPageList::findSubPage(const std::string& name, bool system)
366
{
367
    DECL_TRACER("TPageList::findSubPage(const std::string& name, bool system)");
368
 
369
    vector<SUBPAGELIST_T>::iterator iter;
370
    SUBPAGELIST_T page;
371
 
372
    if (!system && mSubPageList.size() > 0)
373
    {
374
        for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
375
        {
376
            if (iter->name.compare(name) == 0)
377
            {
378
                page = *iter;
379
                return page;
380
            }
381
        }
382
    }
383
    else if (mSystemSubPageList.size() > 0)
384
    {
385
        for (iter = mSystemSubPageList.begin(); iter != mSystemSubPageList.end(); ++iter)
386
        {
387
            if (iter->name.compare(name) == 0)
388
            {
389
                page = *iter;
390
                return page;
391
            }
392
        }
393
    }
394
 
395
    TError::setErrorMsg("Subpage " + name + " not found!");
396
    TError::setError();
397
    return page;
398
}
399
 
400
SUBPAGELIST_T TPageList::findSubPage(int pageID)
401
{
402
    DECL_TRACER("TPageList::findSubPage(int pageID)");
403
 
404
    vector<SUBPAGELIST_T>::iterator iter;
405
    SUBPAGELIST_T page;
406
 
407
    if (pageID < SYSTEM_PAGE_START)
408
    {
409
        for (iter = mSubPageList.begin(); iter != mSubPageList.end(); ++iter)
410
        {
411
            if (iter->pageID == pageID)
412
            {
413
                page = *iter;
414
                return page;
415
            }
416
        }
417
    }
418
    else
419
    {
420
        for (iter = mSystemSubPageList.begin(); iter != mSystemSubPageList.end(); ++iter)
421
        {
422
            if (iter->pageID == pageID)
423
            {
424
                page = *iter;
425
                return page;
426
            }
427
        }
428
    }
429
 
430
    TError::setErrorMsg("Subpage " + std::to_string(pageID) + " not found!");
431
    TError::setError();
432
    return page;
433
}
434
 
435
/*******************************************************************************
436
 * SubViewList
437
 *
438
 * A subview list is a container which defines one or more subpages. All the
439
 * subpages in the container are displayed inside a scroll area. This area can
440
 * be defined to scroll vertical or horizontal. On typing on one of the
441
 * subpages in the scroll area the defined action is made. It behaves like a
442
 * normal button and sends the push notification to the NetLinx. If there are
443
 * some actions defined they will be executed, just like a normal button.
444
 *
445
 * Only Modero X panels are supporting this subviews! And TPanel of course :-)
446
 ******************************************************************************/
447
 
448
/**
449
 * @brief TPageList::findSubViewList
450
 * Searches the list of subviews for an entry with the ID \a id. On success
451
 * the type \a SUBVIEWLIST_T is returned.
452
 *
453
 * @param id    The ID of the wanted subview list.
454
 * @return On success returns the SUBVIEWLIST_T with all data found. Otherwise
455
 * an empty structure is returned.
456
 */
457
SUBVIEWLIST_T TPageList::findSubViewList(int id)
458
{
459
    DECL_TRACER("TPageList::findSubViewList(int id)");
460
 
461
    if (mSubViewList.empty())
462
        return SUBVIEWLIST_T();
463
 
464
    vector<SUBVIEWLIST_T>::iterator iter;
465
 
466
    for (iter = mSubViewList.begin(); iter != mSubViewList.end(); ++iter)
467
    {
468
        if (iter->id == id)
469
            return *iter;
470
    }
471
 
472
    return SUBVIEWLIST_T();
473
}
474
 
475
/**
476
 * @brief TPageList::findSubViewListPageID
477
 * Find the page ID inside of a subview list. The ID of the subview as well as
478
 * the index number of the item must be known to get the page ID.
479
 *
480
 * @param id        ID of subview.
481
 * @param index     Index number of item in the subview list.
482
 *
483
 * @return On success returns the page ID. If the page ID couldn't be found
484
 * either because there are no subviews or the subview ID doesn't exist or the
485
 * index number inside the subview doesn't exist, it returns -1.
486
 */
487
int TPageList::findSubViewListPageID(int id, int index)
488
{
489
    DECL_TRACER("TPageList::findSubViewListPageID(int id, int index)");
490
 
491
    if (mSubViewList.empty())
492
        return -1;
493
 
494
    SUBVIEWLIST_T slist = findSubViewList(id);
495
 
496
    if (slist.id == 0 || slist.items.empty())
497
        return -1;
498
 
499
 
500
    vector<SUBVIEWITEM_T>::iterator iter;
501
 
502
    for (iter = slist.items.begin(); iter != slist.items.end(); ++iter)
503
    {
504
        if (iter->index == index)
505
            return iter->pageID;
506
    }
507
 
508
    return -1;
509
}
510
 
511
/**
512
 * @brief TPageList::findSubViewListNextPageID
513
 * Searches for the first or the next page ID inside a subview list.
514
 * If the parameter \a *index is less then 0, the first index is returned.
515
 * otherwise the next index grater then \a *index is returned, if there is one.
516
 * If no more indexes are found it returns -1.
517
 *
518
 * @param id        The ID of the subview list.
519
 * @param index     The index number.
520
 *
521
 * @return On success the next page ID where the index is higher then \a index
522
 * is returned. If there is no more page ID with a higher index it returns -1.
523
 */
524
int TPageList::findSubViewListNextPageID(int id, int *index)
525
{
526
    DECL_TRACER("TPageList::findSubViewListNextPageID(int id, int *index)");
527
 
528
    if (mSubViewList.empty() || !index)
529
        return -1;
530
 
531
    SUBVIEWLIST_T slist = findSubViewList(id);
532
 
533
    if (slist.id == 0 || slist.items.empty())
534
        return -1;
535
 
536
    if (*index < 0)
537
    {
538
        *index = slist.items[0].index;
539
        return slist.items[0].pageID;
540
    }
541
 
542
    vector<SUBVIEWITEM_T>::iterator iter;
543
 
544
    for (iter = slist.items.begin(); iter != slist.items.end(); ++iter)
545
    {
546
        if (iter->index > *index)
547
            return iter->pageID;
548
    }
549
 
550
    return -1;
551
}
552
 
553
int TPageList::findSubViewListID(int pageID, int *index)
554
{
555
    DECL_TRACER("TPageList::findSubViewListID(int pageID, int *index)");
556
 
557
    if (mSubViewList.empty() || pageID <= 0)
558
        return -1;
559
 
560
    vector<SUBVIEWLIST_T>::iterator iter;
561
 
562
    for (iter = mSubViewList.begin(); iter != mSubViewList.end(); ++iter)
563
    {
564
        vector<SUBVIEWITEM_T>::iterator itItem;
565
 
566
        for (itItem = iter->items.begin(); itItem != iter->items.end(); ++itItem)
567
        {
568
            if (itItem->pageID == pageID)
569
            {
570
                if (index)
571
                    *index = itItem->index;
572
 
573
                return iter->id;
574
            }
575
        }
576
    }
577
 
578
    return -1;
579
}
580