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
 
113 andreas 59
    vector<string> resFiles = {
60
        ":ressources/external.xma",
61
        ":ressources/fnt.xma",
62
        ":ressources/icon.xma",
63
        ":ressources/_main.xml",
64
        ":ressources/manifest.xma",
65
        ":ressources/map.xma",
66
        ":ressources/pal_001.xma",
67
        ":ressources/prj.xma",
68
        ":ressources/_setup.xml",
69
        ":ressources/table.xma",
70
        ":ressources/fonts/arial.ttf",
71
        ":ressources/images/theosys_logo.png",
72
        ":ressources/__system/graphics/fonts/amxbold_.ttf",
73
        ":ressources/__system/graphics/fonts/arialbd.ttf",
74
        ":ressources/__system/graphics/fonts/arial.ttf",
75
        ":ressources/__system/graphics/fonts/cour.ttf",
76
        ":ressources/__system/graphics/sounds/audioTest.wav",
77
        ":ressources/__system/graphics/sounds/docked.mp3",
78
        ":ressources/__system/graphics/sounds/doubleBeep01.wav",
79
        ":ressources/__system/graphics/sounds/doubleBeep02.wav",
80
        ":ressources/__system/graphics/sounds/doubleBeep03.wav",
81
        ":ressources/__system/graphics/sounds/doubleBeep04.wav",
82
        ":ressources/__system/graphics/sounds/doubleBeep05.wav",
83
        ":ressources/__system/graphics/sounds/doubleBeep06.wav",
84
        ":ressources/__system/graphics/sounds/doubleBeep07.wav",
85
        ":ressources/__system/graphics/sounds/doubleBeep08.wav",
86
        ":ressources/__system/graphics/sounds/doubleBeep09.wav",
87
        ":ressources/__system/graphics/sounds/doubleBeep10.wav",
88
        ":ressources/__system/graphics/sounds/doubleBeep.wav",
89
        ":ressources/__system/graphics/sounds/ringback.wav",
90
        ":ressources/__system/graphics/sounds/ringtone.wav",
91
        ":ressources/__system/graphics/sounds/singleBeep01.wav",
92
        ":ressources/__system/graphics/sounds/singleBeep02.wav",
93
        ":ressources/__system/graphics/sounds/singleBeep03.wav",
94
        ":ressources/__system/graphics/sounds/singleBeep04.wav",
95
        ":ressources/__system/graphics/sounds/singleBeep05.wav",
96
        ":ressources/__system/graphics/sounds/singleBeep06.wav",
97
        ":ressources/__system/graphics/sounds/singleBeep07.wav",
98
        ":ressources/__system/graphics/sounds/singleBeep08.wav",
99
        ":ressources/__system/graphics/sounds/singleBeep09.wav",
100
        ":ressources/__system/graphics/sounds/singleBeep10.wav",
101
        ":ressources/__system/graphics/sounds/singleBeep.wav",
102
        ":ressources/__system/graphics/draw.xma",
103
        ":ressources/__system/graphics/fnt.xma",
104
        ":ressources/__system/graphics/version.xma"
105
    };
106
 
22 andreas 107
    bool err = false;
113 andreas 108
    vector<string>::iterator iter;
22 andreas 109
 
113 andreas 110
    for (iter = resFiles.begin(); iter != resFiles.end(); ++iter)
22 andreas 111
    {
113 andreas 112
        if (!copyFile(*iter))
50 andreas 113
            err = true;
22 andreas 114
    }
115
 
116
 
117
    return err;
118
}
51 andreas 119
 
50 andreas 120
bool TTPInit::createDirectoryStructure()
121
{
122
    DECL_TRACER("TTPInit::createDirectoryStructure()");
123
 
51 andreas 124
    if (mPath.empty())
125
    {
126
        MSG_ERROR("Got no path to create the directory structure!");
127
        return false;
128
    }
129
 
50 andreas 130
    string pfad = mPath;
131
 
132
    if (!_makeDir(pfad))
133
        return false;
134
 
135
    pfad = mPath + "/fonts";
136
 
137
    if (!_makeDir(pfad))
138
        return false;
139
 
140
    pfad = mPath + "/images";
141
 
142
    if (!_makeDir(pfad))
143
        return false;
144
 
145
    pfad = mPath + "/sounds";
146
 
147
    if (!_makeDir(pfad))
148
        return false;
149
 
150
    pfad = mPath + "/__system";
151
 
152
    if (!_makeDir(pfad))
153
        return false;
154
 
155
    pfad = mPath + "/__system/graphics";
156
 
157
    if (!_makeDir(pfad))
158
        return false;
159
 
160
    pfad = mPath + "/__system/graphics/fonts";
161
 
162
    if (!_makeDir(pfad))
163
        return false;
164
 
165
    pfad = mPath + "/__system/graphics/images";
166
 
167
    if (!_makeDir(pfad))
168
        return false;
169
 
170
    pfad = mPath + "/__system/graphics/sounds";
171
 
172
    if (!_makeDir(pfad))
173
        return false;
174
 
175
    pfad = mPath + "/__system/graphics/cursors";
176
 
177
    if (!_makeDir(pfad))
178
        return false;
179
 
180
    pfad = mPath + "/__system/graphics/sliders";
181
 
182
    if (!_makeDir(pfad))
183
        return false;
184
 
88 andreas 185
    pfad = mPath + "/__system/graphics/borders";
186
 
187
    if (!_makeDir(pfad))
188
        return false;
189
 
50 andreas 190
    return true;
191
}
192
 
193
bool TTPInit::_makeDir(const std::string& dir)
194
{
195
    DECL_TRACER("TTPInit::_makeDir(const std::string& dir)");
196
 
197
    TValidateFile vf;
198
 
71 andreas 199
    if (!vf.isValidDir(dir))
50 andreas 200
    {
201
        if (mkdir (dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXG) != 0)
202
        {
203
            MSG_ERROR("Directory " << dir << ": " << strerror(errno));
204
            return false;
205
        }
206
    }
207
 
208
    return true;
209
}
210
 
113 andreas 211
bool TTPInit::copyFile(const std::string& fname)
212
{
213
    DECL_TRACER("TTPInit::copyFile(const std::string& fname)");
214
 
215
    bool err = false;
216
    QFile external(fname.c_str());
217
    size_t pos = fname.find_first_of("/");
218
    string bname;
219
 
220
    if (pos != string::npos)
221
        bname = fname.substr(pos);
222
    else
223
        bname = fname;
224
 
225
    if (external.exists())
226
    {
227
        QString path = mPath.c_str();
228
        path += bname.c_str();
229
 
230
        if (!external.copy(path))
231
        {
43 andreas 232
#ifdef __ANDROID__
113 andreas 233
            if (!askPermissions())
234
            {
235
                MSG_ERROR("Could not copy \"" << bname << "\" to " << path.toStdString() << " because permission was denied!");
236
                err = true;
237
            }
238
            else if (!external.copy(path))
239
            {
240
                MSG_ERROR("Could not copy \"" << bname << "\" to " << path.toStdString());
241
                err = true;
242
            }
243
#else
244
            MSG_ERROR("Could not copy \"" << bname << "\" to " << path.toStdString());
245
            err = true;
246
#endif
247
        }
248
    }
249
    else
250
    {
251
        MSG_ERROR("File " << external.fileName().toStdString() << " doesn't exist!");
252
        err = true;
253
    }
254
 
255
    return err;
256
}
257
 
258
#ifdef __ANDROID__
43 andreas 259
bool TTPInit::askPermissions()
260
{
261
    DECL_TRACER("TTPInit::askPermissions(const std::string& path)");
262
 
263
    QStringList permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" };
264
    QtAndroid::PermissionResultMap perms = QtAndroid::requestPermissionsSync(permissions);
265
 
266
    for (auto iter = perms.begin(); iter != perms.end(); ++iter)
267
    {
268
        if (iter.value() == QtAndroid::PermissionResult::Denied)
269
            return false;
270
    }
271
 
272
    return true;
273
}
274
#endif