Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
22 andreas 1
/*
2
 * Copyright (C) 2021 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 <iostream>
20
#include <fstream>
21
 
50 andreas 22
#include <sys/stat.h>
23
#include <fcntl.h>
24
#include <errno.h>
25
 
22 andreas 26
#include <QFile>
43 andreas 27
#ifdef __ANDROID__
28
#include <QtAndroid>
29
#endif
30
#include <QMap>
31
#include <QHash>
22 andreas 32
 
33
#include "ttpinit.h"
34
#include "terror.h"
50 andreas 35
#include "tvalidatefile.h"
22 andreas 36
 
37
using std::string;
85 andreas 38
using std::vector;
22 andreas 39
using std::ostream;
40
 
51 andreas 41
TTPInit::TTPInit()
42
{
43
    DECL_TRACER("TTPInit::TTPInit()");
44
}
45
 
22 andreas 46
TTPInit::TTPInit(const string& path)
47
    : mPath(path)
48
{
49
    DECL_TRACER("TTPInit::TTPInit(const string& path)")
50
 
50 andreas 51
    createDirectoryStructure();
22 andreas 52
    createPanelConfigs();
53
}
54
 
55
bool TTPInit::createPanelConfigs()
56
{
57
    DECL_TRACER("TTPInit::createPanelConfigs()");
58
 
59
    bool err = false;
60
    QFile external(":ressources/external.xma");
61
 
62
    if (external.exists())
63
    {
64
        QString path = mPath.c_str();
65
        path += "/external.xma";
50 andreas 66
 
22 andreas 67
        if (!external.copy(path))
68
        {
50 andreas 69
#ifdef __ANDROID__
43 andreas 70
            if (!askPermissions())
71
            {
50 andreas 72
                MSG_ERROR("Could not copy \"external.xma\" to " << path.toStdString());
43 andreas 73
                err = true;
74
            }
75
            else if (!external.copy(path))
76
            {
50 andreas 77
                MSG_ERROR("Could not copy \"external.xma\" to " << path.toStdString());
43 andreas 78
                err = true;
79
            }
80
#else
50 andreas 81
            MSG_ERROR("Could not copy \"external.xma\" to " << path.toStdString());
82
            err = true;
43 andreas 83
#endif
50 andreas 84
        }
22 andreas 85
    }
86
    else
87
    {
50 andreas 88
        MSG_ERROR("File " << external.fileName().toStdString() << " doesn't exist!");
22 andreas 89
        err = true;
90
    }
91
 
92
    QFile fnt(":ressources/fnt.xma");
93
 
94
    if (fnt.exists())
95
    {
96
        QString path = mPath.c_str();
97
        path += "/fnt.xma";
98
 
99
        if (!fnt.copy(path))
100
        {
50 andreas 101
            MSG_ERROR("Could not copy \"fnt.xma\" to " << path.toStdString());
22 andreas 102
            err = true;
103
        }
104
    }
105
    else
106
    {
50 andreas 107
        MSG_ERROR("File " << fnt.fileName().toStdString() << " doesn't exist!");
22 andreas 108
        err = true;
109
    }
110
 
111
    QFile icon(":ressources/icon.xma");
112
 
113
    if (icon.exists())
114
    {
115
        QString path = mPath.c_str();
116
        path += "/icon.xma";
117
 
118
        if (!icon.copy(path))
119
        {
50 andreas 120
            MSG_ERROR("Could not copy \"icon.xma\" to " << path.toStdString());
22 andreas 121
            err = true;
122
        }
123
    }
124
    else
125
    {
50 andreas 126
        MSG_ERROR("File " << icon.fileName().toStdString() << " doesn't exist!");
22 andreas 127
        err = true;
128
    }
129
 
130
    QFile _main(":ressources/_main.xml");
131
 
132
    if (_main.exists())
133
    {
134
        QString path = mPath.c_str();
135
        path += "/_main.xml";
136
 
137
        if (!_main.copy(path))
138
        {
50 andreas 139
            MSG_ERROR("Could not copy \"_main.xml\" to " << path.toStdString());
22 andreas 140
            err = true;
141
        }
142
    }
143
    else
144
    {
50 andreas 145
        MSG_ERROR("File " << _main.fileName().toStdString() << " doesn't exist!");
22 andreas 146
        err = true;
147
    }
148
 
149
    QFile manifest(":ressources/manifest.xma");
150
 
151
    if (manifest.exists())
152
    {
153
        QString path = mPath.c_str();
154
        path += "/manifest.xma";
155
 
156
        if (!manifest.copy(path))
157
        {
50 andreas 158
            MSG_ERROR("Could not copy \"manifest.xma\" to " << path.toStdString());
22 andreas 159
            err = true;
160
        }
161
    }
162
    else
163
    {
50 andreas 164
        MSG_ERROR("File " << manifest.fileName().toStdString() << " doesn't exist!");
22 andreas 165
        err = true;
166
    }
167
 
168
    QFile map(":ressources/map.xma");
169
 
170
    if (map.exists())
171
    {
172
        QString path = mPath.c_str();
173
        path += "/map.xma";
174
 
175
        if (!map.copy(path))
176
        {
50 andreas 177
            MSG_ERROR("Could not copy \"map.xma\" to " << path.toStdString());
22 andreas 178
            err = true;
179
        }
180
    }
181
    else
182
    {
50 andreas 183
        MSG_ERROR("File " << map.fileName().toStdString() << " doesn't exist!");
22 andreas 184
        err = true;
185
    }
186
 
187
    QFile pal(":ressources/pal_001.xma");
188
 
189
    if (pal.exists())
190
    {
191
        QString path = mPath.c_str();
192
        path += "/pal_001.xma";
193
 
194
        if (!pal.copy(path))
195
        {
50 andreas 196
            MSG_ERROR("Could not copy \"pal_001.xma\" to " << path.toStdString());
22 andreas 197
            err = true;
198
        }
199
    }
200
    else
201
    {
50 andreas 202
        MSG_ERROR("File " << pal.fileName().toStdString() << " doesn't exist!");
22 andreas 203
        err = true;
204
    }
205
 
206
    QFile prj(":ressources/prj.xma");
207
 
208
    if (prj.exists())
209
    {
210
        QString path = mPath.c_str();
211
        path += "/prj.xma";
212
 
213
        if (!prj.copy(path))
214
        {
50 andreas 215
            MSG_ERROR("Could not copy \"prj.xma\" to " << path.toStdString());
22 andreas 216
            err = true;
217
        }
218
    }
219
    else
220
    {
50 andreas 221
        MSG_ERROR("File " << prj.fileName().toStdString() << " doesn't exist!");
22 andreas 222
        err = true;
223
    }
224
 
225
    QFile _setup(":ressources/_setup.xml");
226
 
227
    if (_setup.exists())
228
    {
229
        QString path = mPath.c_str();
230
        path += "/_setup.xml";
231
 
232
        if (!_setup.copy(path))
233
        {
50 andreas 234
            MSG_ERROR("Could not copy \"_setup.xml\" to " << path.toStdString());
22 andreas 235
            err = true;
236
        }
237
    }
238
    else
239
    {
50 andreas 240
        MSG_ERROR("File " << _setup.fileName().toStdString() << " doesn't exist!");
22 andreas 241
        err = true;
242
    }
243
 
244
    QFile table(":ressources/table.xma");
245
 
246
    if (table.exists())
247
    {
248
        QString path = mPath.c_str();
249
        path += "/table.xma";
250
 
251
        if (!table.copy(path))
252
        {
50 andreas 253
            MSG_ERROR("Could not copy \"table.xma\" to " << path.toStdString());
22 andreas 254
            err = true;
255
        }
256
    }
257
    else
258
    {
50 andreas 259
        MSG_ERROR("File " << table.fileName().toStdString() << " doesn't exist!");
22 andreas 260
        err = true;
261
    }
262
 
263
    QFile fonts(":ressources/fonts/arial.ttf");
264
 
265
    if (fonts.exists())
266
    {
267
        QString path = mPath.c_str();
268
        path += "/fonts/arial.ttf";
269
 
270
        if (!fonts.copy(path))
271
        {
43 andreas 272
#ifdef __ANDROID__
273
            if (!askPermissions())
274
            {
50 andreas 275
                MSG_ERROR("Could not copy \"arial.ttf\" to " << path.toStdString());
43 andreas 276
                err = true;
277
            }
278
            else if (!fonts.copy(path))
279
            {
50 andreas 280
                MSG_ERROR("Could not copy \"arial.ttf\" to " << path.toStdString());
43 andreas 281
                err = true;
282
            }
283
#else
50 andreas 284
        MSG_ERROR("Could not copy \"arial.ttf\" to " << path.toStdString());
43 andreas 285
        err = true;
286
#endif
22 andreas 287
        }
288
    }
289
    else
290
    {
50 andreas 291
        MSG_ERROR("File " << fonts.fileName().toStdString() << " doesn't exist!");
22 andreas 292
        err = true;
293
    }
294
 
295
    QFile images(":ressources/images/theosys_logo.png");
296
 
297
    if (images.exists())
298
    {
299
        QString path = mPath.c_str();
300
        path += "/images/theosys_logo.png";
301
 
302
        if (!images.copy(path))
303
        {
43 andreas 304
#ifdef __ANDROID__
305
            if (!askPermissions())
306
            {
50 andreas 307
                MSG_ERROR("Could not copy \"theosys_logo.png\" to " << path.toStdString());
43 andreas 308
                err = true;
309
            }
310
            else if (!images.copy(path))
311
            {
50 andreas 312
                MSG_ERROR("Could not copy \"theosys_logo.png\" to " << path.toStdString());
43 andreas 313
                err = true;
314
            }
315
#else
50 andreas 316
        MSG_ERROR("Could not copy \"theosys_logo.png\" to " << path.toStdString());
43 andreas 317
        err = true;
318
#endif
22 andreas 319
        }
320
    }
321
    else
322
    {
50 andreas 323
        MSG_ERROR("File " << images.fileName().toStdString() << " doesn't exist!");
22 andreas 324
        err = true;
325
    }
326
 
85 andreas 327
    vector<QString> sysFonts = {
328
        "/__system/graphics/fonts/amxbold_.ttf",
329
        "/__system/graphics/fonts/arialbd.ttf",
330
        "/__system/graphics/fonts/arial.ttf",
331
        "/__system/graphics/fonts/cour.ttf"
332
    };
333
 
334
    vector<QString>::iterator iter;
335
 
336
    for (iter = sysFonts.begin(); iter != sysFonts.end(); ++iter)
337
    {
338
        QFile sys;
339
        sys.setFileName(":ressources" + *iter);
340
 
341
        if (sys.exists())
342
        {
343
            QString path = mPath.c_str();
344
            path += *iter;
345
 
346
            if (!sys.copy(path))
347
            {
87 andreas 348
                MSG_ERROR("Could not copy \":ressources" << iter->toStdString() << "\" to " << path.toStdString());
85 andreas 349
                err = true;
350
            }
351
        }
352
 
353
        if (sys.isOpen())
354
            sys.close();
355
    }
356
 
357
    vector<QString> sounds = {
358
        "/__system/graphics/sounds/audioTest.wav",
359
        "/__system/graphics/sounds/docked.mp3",
360
        "/__system/graphics/sounds/doubleBeep01.wav",
361
        "/__system/graphics/sounds/doubleBeep02.wav",
362
        "/__system/graphics/sounds/doubleBeep03.wav",
363
        "/__system/graphics/sounds/doubleBeep04.wav",
364
        "/__system/graphics/sounds/doubleBeep05.wav",
365
        "/__system/graphics/sounds/doubleBeep06.wav",
366
        "/__system/graphics/sounds/doubleBeep07.wav",
367
        "/__system/graphics/sounds/doubleBeep08.wav",
368
        "/__system/graphics/sounds/doubleBeep09.wav",
369
        "/__system/graphics/sounds/doubleBeep10.wav",
370
        "/__system/graphics/sounds/doubleBeep.wav",
371
        "/__system/graphics/sounds/ringback.wav",
372
        "/__system/graphics/sounds/ringtone.wav",
373
        "/__system/graphics/sounds/singleBeep01.wav",
374
        "/__system/graphics/sounds/singleBeep02.wav",
375
        "/__system/graphics/sounds/singleBeep03.wav",
376
        "/__system/graphics/sounds/singleBeep04.wav",
377
        "/__system/graphics/sounds/singleBeep05.wav",
378
        "/__system/graphics/sounds/singleBeep06.wav",
379
        "/__system/graphics/sounds/singleBeep07.wav",
380
        "/__system/graphics/sounds/singleBeep08.wav",
381
        "/__system/graphics/sounds/singleBeep09.wav",
382
        "/__system/graphics/sounds/singleBeep10.wav",
383
        "/__system/graphics/sounds/singleBeep.wav"
384
    };
385
 
386
    for (iter = sounds.begin(); iter != sounds.end(); ++iter)
387
    {
388
        QFile sysSounds;
389
        sysSounds.setFileName(":ressources" + *iter);
390
 
391
        if (sysSounds.exists())
392
        {
393
            QString path = mPath.c_str();
394
            path += *iter;
395
 
396
            if (!sysSounds.copy(path))
397
            {
398
                MSG_ERROR("Could not copy \"" << iter->toStdString() << "\" to " << path.toStdString());
399
                err = true;
400
            }
401
        }
402
 
403
        if (sysSounds.isOpen())
404
            sysSounds.close();
405
    }
406
 
407
    vector<QString> sysSettings = {
408
        "/__system/graphics/draw.xma",
409
        "/__system/graphics/fnt.xma",
410
        "/__system/graphics/version.xma"
411
    };
412
 
413
    for (iter = sysSettings.begin(); iter != sysSettings.end(); ++iter)
414
    {
415
        QFile sys;
416
        sys.setFileName(":ressources" + *iter);
417
 
418
        if (sys.exists())
419
        {
420
            QString path = mPath.c_str();
421
            path += *iter;
422
 
423
            if (!sys.copy(path))
424
            {
425
                MSG_ERROR("Could not copy \"" << iter->toStdString() << "\" to " << path.toStdString());
426
                err = true;
427
            }
428
        }
429
 
430
        if (sys.isOpen())
431
            sys.close();
432
    }
433
 
22 andreas 434
    return err;
435
}
51 andreas 436
 
50 andreas 437
bool TTPInit::createDirectoryStructure()
438
{
439
    DECL_TRACER("TTPInit::createDirectoryStructure()");
440
 
51 andreas 441
    if (mPath.empty())
442
    {
443
        MSG_ERROR("Got no path to create the directory structure!");
444
        return false;
445
    }
446
 
50 andreas 447
    string pfad = mPath;
448
 
449
    if (!_makeDir(pfad))
450
        return false;
451
 
452
    pfad = mPath + "/fonts";
453
 
454
    if (!_makeDir(pfad))
455
        return false;
456
 
457
    pfad = mPath + "/images";
458
 
459
    if (!_makeDir(pfad))
460
        return false;
461
 
462
    pfad = mPath + "/sounds";
463
 
464
    if (!_makeDir(pfad))
465
        return false;
466
 
467
    pfad = mPath + "/__system";
468
 
469
    if (!_makeDir(pfad))
470
        return false;
471
 
472
    pfad = mPath + "/__system/graphics";
473
 
474
    if (!_makeDir(pfad))
475
        return false;
476
 
477
    pfad = mPath + "/__system/graphics/fonts";
478
 
479
    if (!_makeDir(pfad))
480
        return false;
481
 
482
    pfad = mPath + "/__system/graphics/images";
483
 
484
    if (!_makeDir(pfad))
485
        return false;
486
 
487
    pfad = mPath + "/__system/graphics/sounds";
488
 
489
    if (!_makeDir(pfad))
490
        return false;
491
 
492
    pfad = mPath + "/__system/graphics/cursors";
493
 
494
    if (!_makeDir(pfad))
495
        return false;
496
 
497
    pfad = mPath + "/__system/graphics/sliders";
498
 
499
    if (!_makeDir(pfad))
500
        return false;
501
 
88 andreas 502
    pfad = mPath + "/__system/graphics/borders";
503
 
504
    if (!_makeDir(pfad))
505
        return false;
506
 
50 andreas 507
    return true;
508
}
509
 
510
bool TTPInit::_makeDir(const std::string& dir)
511
{
512
    DECL_TRACER("TTPInit::_makeDir(const std::string& dir)");
513
 
514
    TValidateFile vf;
515
 
71 andreas 516
    if (!vf.isValidDir(dir))
50 andreas 517
    {
518
        if (mkdir (dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXG) != 0)
519
        {
520
            MSG_ERROR("Directory " << dir << ": " << strerror(errno));
521
            return false;
522
        }
523
    }
524
 
525
    return true;
526
}
527
 
43 andreas 528
#ifdef __ANDROID__
529
bool TTPInit::askPermissions()
530
{
531
    DECL_TRACER("TTPInit::askPermissions(const std::string& path)");
532
 
533
    QStringList permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" };
534
    QtAndroid::PermissionResultMap perms = QtAndroid::requestPermissionsSync(permissions);
535
 
536
    for (auto iter = perms.begin(); iter != perms.end(); ++iter)
537
    {
538
        if (iter.value() == QtAndroid::PermissionResult::Denied)
539
            return false;
540
    }
541
 
542
    return true;
543
}
544
#endif