2
This file is part of ut-tweak-tool
3
Copyright (C) 2015 Stefano Verzegnassi
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License 3 as published by
7
the Free Software Foundation.
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.
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see http://www.gnu.org/licenses/.
18
#include "package_p.h"
24
#include <QTextStream>
25
#include <QDirIterator>
27
#include <QJsonDocument>
28
#include <QJsonObject>
33
#define CACHE_FOLDER "/home/phablet/.cache/"
34
#define APPDATA_FOLDER "/home/phablet/.local/share/"
35
#define CONFIG_FOLDER "/home/phablet/.config/"
37
Package::Package(const QString &appId, const QString &directory, bool removable,
38
const QString &architecture, const QString &description,
39
const QString &framework, const QList<Hook> &hooks,
40
const QString &iconPath, const QString &maintainer,
41
const QString &title, const QString &version):
43
m_directory(directory),
44
m_removable(removable),
45
m_architecture(architecture),
46
m_description(description),
47
m_framework(framework),
50
m_maintainer(maintainer),
57
m_lifeCycleException(false)
59
// Get life cycle exception state
60
QGSettings lceSettings("com.canonical.qtmir", "/com/canonical/qtmir/");
61
QStringList appIds = lceSettings.get("lifecycle-exempt-appids").toStringList();
63
Q_FOREACH(const QString &id, appIds) {
65
m_lifeCycleException = true;
66
Q_EMIT lifeCycleExceptionChanged();
72
connect(this, &Package::lifeCycleExceptionChanged, this, &Package::updateLifeCycleExceptionState);
74
// Get general hooks info
77
int accountsNumber = 0;
78
int pushHelpersNumber = 0;
80
Q_FOREACH(const Hook &hook, hooks) {
81
if (hook.type == Hook::HookType::App)
84
if (hook.type == Hook::HookType::Scope)
87
if (hook.type == Hook::HookType::OnlineAccount)
90
if (hook.type == Hook::HookType::PushHelper)
94
m_containsApp = (appsNumber > 0);
95
m_containsScope = (scopesNumber > 0);
96
m_containsOnlineAccount = (accountsNumber > 0);
97
m_containsPushHelper = (pushHelpersNumber > 0);
100
void Package::clearAppData()
102
QDir d(APPDATA_FOLDER + m_appId);
105
d.removeRecursively();
110
void Package::clearAppCache()
112
QDir d(CACHE_FOLDER + m_appId);
115
d.removeRecursively();
120
void Package::clearAppConfig()
122
QDir d(CONFIG_FOLDER + m_appId);
125
d.removeRecursively();
130
QVariantMap Package::getHook(int index)
132
const Hook &hook = m_hooks.at(index);
136
result["name"] = hook.name;
139
case Hook::HookType::Unknown:
140
result["type"] = "Unknown";
142
case Hook::HookType::App:
143
result["type"] = "App";
145
case Hook::HookType::Scope:
146
result["type"] = "Scope";
148
case Hook::HookType::OnlineAccount:
149
result["type"] = "OnlineAccount";
151
case Hook::HookType::PushHelper:
152
result["type"] = "PushHelper";
156
qDebug() << "Looking for an AppArmor profile at" << hook.appArmorPath;
158
QVariantMap appArmor;
160
if (QFile::exists(hook.appArmorPath)) {
161
QFile file(hook.appArmorPath);
163
if (file.open(QIODevice::ReadOnly)) {
164
QTextStream in(&file);
165
const QString &apparmorTxt = in.readAll();
168
appArmor = QJsonDocument::fromJson(apparmorTxt.toUtf8()).object().toVariantMap();
170
qDebug() << "Cannot read the AppArmor profile!";
173
qDebug() << "AppArmor profile not found!";
176
result["apparmor"] = appArmor;
181
void Package::updateSize()
183
QDirIterator appFolderIt(m_directory, QDirIterator::Subdirectories);
184
qint64 newInstalledSize = 0;
185
while (appFolderIt.hasNext()) {
187
newInstalledSize += appFolderIt.fileInfo().size();
190
QDirIterator appDataIt(APPDATA_FOLDER + m_appId, QDirIterator::Subdirectories);
191
qint64 newAppDataSize = 0;
192
while (appDataIt.hasNext()) {
194
newAppDataSize += appDataIt.fileInfo().size();
197
QDirIterator cacheIt(CACHE_FOLDER + m_appId, QDirIterator::Subdirectories);
198
qint64 newCacheSize = 0;
199
while (cacheIt.hasNext()) {
201
newCacheSize += cacheIt.fileInfo().size();
204
QDirIterator configIt(CONFIG_FOLDER + m_appId, QDirIterator::Subdirectories);
205
qint64 newConfigSize = 0;
206
while (configIt.hasNext()) {
208
newConfigSize += configIt.fileInfo().size();
211
if (m_installedSize != newInstalledSize) {
212
m_installedSize = newInstalledSize;
213
Q_EMIT installedSizeChanged();
217
if (m_dataSize != newAppDataSize) {
218
m_dataSize = newAppDataSize;
219
Q_EMIT dataSizeChanged();
222
if (m_configSize != newConfigSize) {
223
m_configSize = newConfigSize;
224
Q_EMIT configSizeChanged();
227
if (m_cacheSize != newCacheSize) {
228
m_cacheSize = newCacheSize;
229
Q_EMIT cacheSizeChanged();
233
void Package::updateLifeCycleExceptionState()
235
QGSettings lceSettings("com.canonical.qtmir", "/com/canonical/qtmir/");
236
QStringList appIds = lceSettings.get("lifecycle-exempt-appids").toStringList();
238
if (m_lifeCycleException) { // Set to true -> add exception
239
if (!appIds.contains(m_appId)) {
240
appIds.append(m_appId);
242
} else { // Set to false -> remove exception
243
if (appIds.contains(m_appId)) {
244
appIds.removeAll(m_appId);
248
lceSettings.set("lifecycle-exempt-appids", appIds);