Subversion Repositories tpanel

Rev

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