Subversion Repositories tpanel

Rev

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

Rev Author Line No. Line
480 andreas 1
/*
2
 * Copyright (C) 2024 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 "tapps.h"
20
#include "ttpinit.h"
21
#include "texpat++.h"
22
#include "terror.h"
23
 
24
#if __cplusplus < 201402L
25
#   error "This module requires at least C++14 standard!"
26
#else
27
#   if __cplusplus < 201703L
28
#       include <experimental/filesystem>
29
namespace fs = std::experimental::filesystem;
30
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
31
#   else
32
#       include <filesystem>
33
#       ifdef __ANDROID__
34
namespace fs = std::__fs::filesystem;
35
#       else
36
namespace fs = std::filesystem;
37
#       endif
38
#   endif
39
#endif
40
 
41
using std::string;
42
using std::vector;
43
using namespace Expat;
44
 
45
vector<APP_SETTINGS_t> TApps::mAppSettings;
46
vector<APP_WINDOW_FRAME_t> TApps::mWindowFrames;
47
 
48
TApps::TApps()
49
{
50
    DECL_TRACER("TApps::TApps()");
51
}
52
 
53
TApps::~TApps()
54
{
55
    DECL_TRACER("TApps::~TApps()");
56
}
57
 
58
bool TApps::parseApps()
59
{
60
    DECL_TRACER("TApps::parseApps()");
61
 
62
    if (!TTPInit::getTP5())
63
    {
64
        MSG_WARNING("Can't read app configs because it's not a TP5 format!");
65
        return false;
66
    }
67
 
68
    if (!mAppSettings.empty())
69
        mAppSettings.clear();
70
 
71
    string projectPath = TConfig::getProjectPath();
72
 
73
    if (!fs::exists(projectPath + "/G5Apps.xma"))
74
        return false;
75
 
76
    string path = makeFileName(projectPath, "G5Apps.xma");
77
 
78
    if (!isValidFile())
79
    {
80
        MSG_ERROR("File " << path << " doesn't exist or is not readable!");
81
        TError::setError();
82
        return false;
83
    }
84
 
85
    TExpat xml(path);
86
    xml.setEncoding(ENC_UTF8);
87
 
88
    if (!xml.parse())
89
        return false;
90
 
91
    int depth = 0;
92
    size_t index = 0;
93
 
94
    if ((index = xml.getElementIndex("Apps", &depth)) == TExpat::npos)
95
    {
96
        MSG_DEBUG("File does not contain the element \"Apps\"!");
97
        TError::setError();
98
        return false;
99
    }
100
 
101
    depth++;
102
    string name, content;
103
    vector<ATTRIBUTE_t> attrs;
104
 
105
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
106
    {
107
        if (name.compare("App") == 0)
108
        {
109
            APP_SETTINGS_t app;
110
 
111
            app.appName = xml.getAttribute("name", attrs);
112
            app.packageName = xml.getAttribute("packageName", attrs);
113
            app.appID = xml.getAttribute("appID", attrs);
114
 
115
            string a;
116
 
117
            while ((index = xml.getNextElementFromIndex(index, &a, &content, &attrs)) != TExpat::npos)
118
            {
119
                if (a.compare("Info") == 0)
120
                    app.appInfo = content;
121
                else if (a.compare("Window") == 0)
122
                {
123
                    app.appWindow.aspectFixed = xml.getAttributeBool("aspectFixed", attrs);
124
                    string s;
125
 
126
                    while ((index = xml.getNextElementFromIndex(index, &s, &content, &attrs)) != TExpat::npos)
127
                    {
128
                        if (s.compare("AspectRatios") == 0)
129
                        {
130
                            APP_ASPECT_RATIO_t ar;
131
                            string r;
132
 
133
                            while ((index = xml.getNextElementFromIndex(index, &r, &content, &attrs)) != TExpat::npos)
134
                            {
135
                                if (r.compare("AspectRatio") == 0)
136
                                {
137
                                    ar.percent = xml.getAttributeDouble("percent", attrs);
138
                                    ar.ratioWidth = xml.getAttributeInt("ratioWidth", attrs);
139
                                    ar.ratioHeight = xml.getAttributeInt("ratioHeight", attrs);
140
                                    app.appWindow.aspectRatios.push_back(ar);
141
                                }
142
                            }
143
                        }
144
                        else if (s.compare("AspectRatioLimits") == 0)
145
                        {
146
                            app.appWindow.aspectRatioLimits.minWidth = xml.getAttributeInt("minWidth", attrs);
147
                            app.appWindow.aspectRatioLimits.minHeight = xml.getAttributeInt("minHeight", attrs);
148
                        }
149
                    }
150
                }
151
                else if (a.compare("Images") == 0)
152
                {
153
                    string i;
154
 
155
                    while ((index = xml.getNextElementFromIndex(index, &i, &content, &attrs)) != TExpat::npos)
156
                    {
157
                        if (i.compare("ThumbImage") == 0)
158
                            app.appImages.thumbImage = content;
159
                        else if (i.compare("WindowImage") == 0)
160
                            app.appImages.windowImage = content;
161
                    }
162
                }
163
                else if (name.compare("Parameters") == 0)
164
                {
165
                    string p;
166
                    APP_PARAMETER_t par;
167
 
168
                    while ((index = xml.getNextElementFromIndex(index, &p, &content, &attrs)) != TExpat::npos)
169
                    {
170
                        if (p.compare("Parameter") == 0)
171
                        {
172
                            par.name = xml.getAttribute("name", attrs);
173
                            par.eDataType = xml.getAttribute("eDataType", attrs);
174
                            par.value = xml.getAttribute("value", attrs);
175
                            par.info = xml.getAttribute("info", attrs);
176
                            APP_PAR_STRINGS_t val;
177
                            string v;
178
 
179
                            while ((index = xml.getNextElementFromIndex(index, &v, &content, &attrs)) != TExpat::npos)
180
                            {
181
                                if (v.compare("StringValues") == 0)
182
                                {
183
                                    string sv;
184
 
185
                                    while ((index = xml.getNextElementFromIndex(index, &sv, &content, &attrs)) != TExpat::npos)
186
                                    {
187
                                        if (sv.compare("StringValue") == 0)
188
                                        {
189
                                            val.key = xml.getAttribute("key", attrs);
190
                                            val.value = content;
191
                                            par.stringValues.push_back(val);
192
                                        }
193
                                    }
194
                                }
195
                            }
196
                        }
197
                    }
198
 
199
                    app.parameters.push_back(par);
200
                }
201
            }
202
 
203
            mAppSettings.push_back(app);
204
        }
205
        else if (name.compare("WindowFrames") == 0)
206
        {
207
            string w;
208
            APP_WINDOW_FRAME_t wf;
209
 
210
            while ((index = xml.getNextElementFromIndex(index, &w, &content, &attrs)) != TExpat::npos)
211
            {
212
                if (w.compare("WindowFrame") == 0)
213
                {
214
                    wf.eType = xml.getAttribute("eType", attrs);
215
                    wf.edgeSize = xml.getAttributeInt("edgeSize", attrs);
216
                    wf.barSize = xml.getAttributeInt("barSize", attrs);
217
                    string b;
218
 
219
                    while ((index = xml.getNextElementFromIndex(index, &b, &content, &attrs)) != TExpat::npos)
220
                    {
221
                        APP_BUTTON_t button;
222
 
223
                        if (b.compare("Buttons") == 0)
224
                        {
225
                            string a;
226
 
227
                            while ((index = xml.getNextElementFromIndex(index, &a, &content, &attrs)) != TExpat::npos)
228
                            {
229
                                if (a.compare("Button") == 0)
230
                                {
231
                                    button.eLocation = xml.getAttribute("eLocation", attrs);
232
                                    button.order = xml.getAttributeInt("order", attrs);
233
                                    button.spacing = xml.getAttributeInt("spacing", attrs);
234
                                    string i;
235
 
236
                                    while ((index = xml.getNextElementFromIndex(index, &i, &content, &attrs)) != TExpat::npos)
237
                                    {
238
                                        if (i.compare("ButtonImage") == 0)
239
                                            button.buttonImage.push_back(content);
240
                                    }
241
                                }
242
                            }
243
 
244
                            wf.buttons.push_back(button);
245
                        }
246
                    }
247
 
248
                    mWindowFrames.push_back(wf);
249
                }
250
            }
251
        }
252
    }
253
 
254
    return true;
255
}
256
 
257
APP_SETTINGS_t TApps::getApp(const string& name)
258
{
259
    DECL_TRACER("TApps::getApp(const string& name)");
260
 
261
    if (mAppSettings.empty())
262
        return APP_SETTINGS_t();
263
 
264
    vector<APP_SETTINGS_t>::iterator iter;
265
 
266
    for (iter = mAppSettings.begin(); iter != mAppSettings.end(); ++iter)
267
    {
268
        if (iter->appID == name)
269
            return *iter;
270
    }
271
 
272
    return APP_SETTINGS_t();
273
}
274
 
275
APP_WINDOW_FRAME_t TApps::getWindowFrame(const string& type)
276
{
277
    DECL_TRACER("TApps::getWindowFrame(const string& type)");
278
 
279
    if (mWindowFrames.empty())
280
        return APP_WINDOW_FRAME_t();
281
 
282
    vector<APP_WINDOW_FRAME_t>::iterator iter;
283
 
284
    for (iter = mWindowFrames.begin(); iter != mWindowFrames.end(); ++iter)
285
    {
286
        if (iter->eType == type)
287
            return *iter;
288
    }
289
 
290
    return APP_WINDOW_FRAME_t();
291
}