-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprofilemanageimpl.cpp
More file actions
executable file
·434 lines (348 loc) · 12.6 KB
/
profilemanageimpl.cpp
File metadata and controls
executable file
·434 lines (348 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "profilemanageimpl.h"
#include "core.h"
#include "datasourcefactory.h"
#include "stbpluginobject.h"
#include "browser.h"
#include "mediaplayer.h"
#include "stbprofile.h"
#include "statistics.h"
#include "webpage.h"
#include "networkstatistics.h"
#include "datasource.h"
#include <QFile>
#include <QDir>
#include <QRegularExpression>
using namespace yasem;
ProfileManageImpl::ProfileManageImpl(QObject *parent):
SDK::ProfileManager(parent)
{
profilesDir = QFileInfo(SDK::Core::instance()->settings()->fileName()).absoluteDir();
}
ProfileManageImpl::~ProfileManageImpl()
{
STUB();
qDeleteAll(m_profiles_list);
qDeleteAll(m_profile_classes);
}
QSet<SDK::Profile*> ProfileManageImpl::getProfiles()
{
return m_profiles_list;
}
SDK::Profile* ProfileManageImpl::getActiveProfile()
{
return m_active_profile;
}
void ProfileManageImpl::addProfile(SDK::Profile* profile)
{
Q_ASSERT(profile);
if(!m_profiles_list.contains(profile))
{
m_profiles_list.insert(profile);
emit profileAdded(profile);
}
}
void ProfileManageImpl::setActiveProfile(SDK::Profile* profile)
{
STUB() << profile;
if(m_active_profile)
{
m_active_profile->stop();
m_active_profile->cleanApi();
}
Q_ASSERT(profile);
foreach (const SDK::Profile* item, m_profiles_list) {
if(item == profile)
{
m_active_profile = profile;
SDK::Core::instance()->settings()->setValue("active_profile", profile->getId());
qDebug() << QString("Active profile: %1").arg(profile->getName());
SDK::GUI::instance()->setTopWidget(SDK::GUI::TOP_WIDGET_BROWSER);
SDK::Browser* browser = SDK::Browser::instance();
if(browser)
{
SDK::WebPage* page = browser->getMainWebPage();
page->reset();
SDK::StbPluginObject* stb_object = profile->getProfilePlugin();
stb_object->initObject(page);
}
else
qDebug() << "[V] No browser found!";
loadProfileKeymap(profile);
SDK::Core::instance()->statistics()->network()->reset();
profile->start();
SDK::MediaPlayer* player = SDK::MediaPlayer::instance();
if(player && player->isInitialized())
player->mediaStop();
else
qDebug() << "[V] No player found!";
m_profiles_stack.push(profile);
emit profileChanged(profile);
return;
}
}
qWarning() << QString("Cannot change profile '%1': not found!").arg(profile->getId());
}
void ProfileManageImpl::loadDefaultKeymapFileIfNotExists(QSettings& keymap, const QString &classId, bool force_overwrite)
{
QFile file(keymap.fileName());
if(!file.exists() || force_overwrite)
{
QFileInfo fileInfo(file);
QDir dir = fileInfo.absoluteDir();
if(!dir.exists())
{
DEBUG() << "Creating directory" << dir.absolutePath();
if(dir.mkpath(dir.absolutePath()))
{
DEBUG() << "Directory created";
}
else
{
ERROR() << "Directory not created!";
return;
}
}
if(!file.open(QFile::WriteOnly))
{
ERROR() << "Cannot open file" << file.fileName() << "to write into!";
return;
}
DEBUG() << "keymap file" << keymap.fileName() << "doesn't exists. Copying from resourses";
QString defaultKeymapName = QString(":/defaults/keymaps/%1/default.ini").arg(classId);
QFile res(defaultKeymapName);
if(res.open(QIODevice::ReadOnly|QIODevice::Text))
{
QByteArray content = res.readAll();
if(file.isWritable())
{
file.write(content);
file.flush();
}
else
{
ERROR() << "Keymap file" << file.fileName() << "is not writable!";
return;
}
}
else
{
ERROR() << "Cannot load default keymap from resourses. File:" << defaultKeymapName;
return;
}
}
}
void ProfileManageImpl::loadProfileKeymap(SDK::Profile* profile)
{
DEBUG() << "Loading keymap for profile" << profile->getName();
QString classId = profile->getProfilePlugin()->getProfileClassId();
QSettings keymap(SDK::Core::instance()->getConfigDir().append("keymaps/%1/default.ini").arg(classId), QSettings::IniFormat, this);
loadDefaultKeymapFileIfNotExists(keymap, classId, true);
keymap.sync();
SDK::GUI* gui = SDK::GUI::instance();
keymap.beginGroup("keymap");
QStringList keys = keymap.allKeys();
SDK::Browser* browser = SDK::Browser::instance();
if(browser)
browser->clearKeyEvents();
for(QString key: keys)
{
QString value = keymap.value(key).toString();
QStringList data = value.split("|");
int code = -1;
int which = -1;
bool alt = false;
bool ctrl = false;
bool shift = false;
for(QString element: data)
{
QStringList val = element.split(":");
if(val.length() != 2)
{
WARN() << "Value length for keymap element" << element << "is" << val.length();
continue;
}
QString key_name = val.at(0);
QString key_value = val.at(1);
if(key_name == "code")
code = QString(key_value).toInt();
else if(key_name == "which")
which = QString(key_value).toInt();
else if(key_name == "alt")
alt = (key_value == "true");
else if(key_name == "ctrl")
ctrl = (key_value == "true");
else if(key_name == "shift")
shift = (key_value == "true");
else
WARN() << "Undefined key" << key_name << "->" << key_value << "in keymap record" << value;
if(which == -1)
which = code;
}
SDK::GUI::RcKey keycode_value = gui->getRcKeyByName(key);
if(keycode_value == SDK::GUI::RC_KEY_NO_KEY)
WARN() << "Key value for" << key << "not found!";
else
if(browser) browser->registerKeyEvent(keycode_value, code, which, alt, ctrl, shift);
}
keymap.endGroup();
DEBUG() << "Keymap loaded";
}
bool ProfileManageImpl::removeProfile(SDK::Profile* profile)
{
if(profile == m_active_profile)
return false;
QFile file(profilesDir.path().append("/").append(profile->getId()).append(".ini"));
DEBUG() << "Removing profile file" << file.fileName();
bool is_removed = file.remove();
emit profileRemoved(is_removed);
return is_removed && m_profiles_list.remove(profile);
}
void ProfileManageImpl::loadProfiles()
{
QString profilePath = SDK::Core::instance()->settings()->value(CONFIG_PROFILES_DIR, "profiles").toString();
QString full_profile_path = profilesDir.absolutePath().append("/").append(profilePath);
QDir configDir(full_profile_path);
if(!configDir.exists())
{
DEBUG() << "Profiles directory doesn't exist. Creating" << full_profile_path;
bool is_created = configDir.mkpath(full_profile_path);
DEBUG() << "Directory create result" << is_created;
}
qDebug() << "Looking for profiles in" << full_profile_path;
if(!profilesDir.cd(full_profile_path))
{
ERROR() << "Cannot go to profiles dir" << full_profile_path;
return;
}
DEBUG() << "Searching for profiles in" << profilesDir.path();
profilesDir.setNameFilters(QStringList() << "*.ini");
foreach (QString fileName, profilesDir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable))
{
DEBUG() << "Loading profile from" << fileName;
QSettings s(profilesDir.path().append("/").append(fileName), QSettings::IniFormat);
s.beginGroup("profile");
QString classId = s.value("classid").toString();
SDK::StbPluginObject* stbPlugin = ProfileManager::instance()->getProfilePluginByClassId(classId);
if(stbPlugin == NULL)
{
WARN() << "Plugin for profile classid " << classId << " not found! Profile will be skipped!";
continue;
}
SDK::Profile* profile = stbPlugin->createProfile(s.value("uuid").toString());
Q_ASSERT(profile);
profile->setName(s.value("name").toString());
profile->setSubmodel(stbPlugin->getSubmodels().at(s.value("submodel").toInt()));
DEBUG() << "Profile" << profile->getName() << "loaded";
s.endGroup();
m_profiles_list.insert(profile);
}
}
SDK::Profile* ProfileManageImpl::createProfile(const QString &classId, const QString &submodel, const QString &baseName = "", bool overwrite = false)
{
DEBUG() << "Creaing profile" << classId << baseName << submodel << baseName;
SDK::StbPluginObject* stbPlugin = getProfilePluginByClassId(classId);
SDK::Profile* profile(stbPlugin->createProfile());
profile->setName(createUniqueName(classId, baseName, overwrite));
profile->setSubmodel(stbPlugin->findSubmodel(submodel));
profile->datasource()->set("profile", "uuid", profile->getId());
profile->datasource()->set("profile", "name", profile->getName());
profile->datasource()->set("profile", "classid", classId);
profile->initDefaults();
return profile;
}
void ProfileManageImpl::registerProfileClassId(const QString &classId, SDK::StbPluginObject* profilePlugin)
{
if(!m_profile_classes.contains(classId))
{
qDebug() << "Registered" << classId << "as profile class ID";
m_profile_classes.insert(classId, profilePlugin);
}
else
WARN() << "Profile class ID" << classId << "has bee already registered!";
}
QMap<QString, SDK::StbPluginObject*> ProfileManageImpl::getRegisteredClasses()
{
return m_profile_classes;
}
SDK::StbPluginObject* ProfileManageImpl::getProfilePluginByClassId(const QString &classId)
{
for(auto iterator = m_profile_classes.begin(); iterator != m_profile_classes.end(); iterator++)
{
if(iterator.key() == classId)
return iterator.value();
}
qWarning() << QString("Profile plugin '%1' not found!").arg(classId);
return NULL;
}
SDK::Profile* ProfileManageImpl::findById(const QString &id)
{
foreach (SDK::Profile* profile, m_profiles_list) {
if(id == profile->getId())
{
DEBUG() << "PROFILE: " << profile;
return profile;
}
}
ERROR() << QString("Profile '%1' not found!").arg(id);
return NULL;
}
SDK::Profile* ProfileManageImpl::findByName(const QString &id)
{
foreach (SDK::Profile* profile, m_profiles_list) {
if(id == profile->getName())
{
DEBUG() << "PROFILE: " << profile;
return profile;
}
}
ERROR() << QString("Profile '%1' not found!").arg(id);
return NULL;
}
SDK::Profile* ProfileManageImpl::backToPreviousProfile()
{
qDebug() << "profiles:" << m_profiles_stack.size();
if(m_profiles_stack.size() < 1)
return NULL;
SDK::Profile* currentProfile = m_profiles_stack.pop();
if(m_profiles_stack.size() < 1) return currentProfile;
SDK::Profile* profile = m_profiles_stack.pop();
Q_ASSERT(profile);
setActiveProfile(profile);
return profile;
}
void ProfileManageImpl::backToMainPage()
{
if(m_profiles_stack.size() < 1)
return;
m_profiles_stack.resize(1);
SDK::Profile* profile = m_profiles_stack.at(0);
setActiveProfile(profile);
}
bool ProfileManageImpl::canGoBack()
{
return m_profiles_stack.size() > 1;
}
QString ProfileManageImpl::createUniqueName(const QString &classId, const QString &baseName = "", bool overwrite = false)
{
const QString newProfileName = baseName != "" ? baseName : QObject::tr("New %1 profile").arg(classId.toCaseFolded());
QRegularExpression rx(QString("^%1(?:\\s\\#(\\d+))?$").arg(newProfileName));
int maxIndex = 0;
foreach(const SDK::Profile* profile, ProfileManager::instance()->getProfiles())
{
auto match = rx.match(profile->getName());
if(match.hasMatch())
{
if(match.capturedLength() > 1)
{
int currIndex = match.captured(1).toInt();
currIndex = currIndex > 0 ? currIndex : 1;
if(currIndex > maxIndex)
maxIndex = currIndex;
}
}
}
maxIndex++;
if(overwrite)
return newProfileName;
return maxIndex > 1 ? newProfileName + QString(" #") + QString::number(maxIndex) : newProfileName;
}