~bhdouglass/ubuntu-touch-tweak-tool/fix-disappearing-options

« back to all changes in this revision

Viewing changes to src/plugin/tweaktool/package.cpp

  • Committer: Stefano Verzegnassi
  • Date: 2016-04-16 17:04:08 UTC
  • Revision ID: stefano92.100@gmail.com-20160416170408-t0ck3hoi0qifmehw
Stable codebase for 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of ut-tweak-tool
 
3
  Copyright (C) 2015 Stefano Verzegnassi
 
4
 
 
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.
 
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, see http://www.gnu.org/licenses/.
 
16
*/
 
17
 
 
18
#include "package_p.h"
 
19
 
 
20
#include <QGSettings>
 
21
 
 
22
#include <QDir>
 
23
#include <QFile>
 
24
#include <QTextStream>
 
25
#include <QDirIterator>
 
26
 
 
27
#include <QJsonDocument>
 
28
#include <QJsonObject>
 
29
#include <QJsonArray>
 
30
 
 
31
#include <QDebug>
 
32
 
 
33
#define CACHE_FOLDER "/home/phablet/.cache/"
 
34
#define APPDATA_FOLDER "/home/phablet/.local/share/"
 
35
#define CONFIG_FOLDER "/home/phablet/.config/"
 
36
 
 
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):
 
42
    m_appId(appId),
 
43
    m_directory(directory),
 
44
    m_removable(removable),
 
45
    m_architecture(architecture),
 
46
    m_description(description),
 
47
    m_framework(framework),
 
48
    m_hooks(hooks),
 
49
    m_iconPath(iconPath),
 
50
    m_maintainer(maintainer),
 
51
    m_title(title),
 
52
    m_version(version),
 
53
    m_installedSize(0),
 
54
    m_dataSize(0),
 
55
    m_configSize(0),
 
56
    m_cacheSize(0),
 
57
    m_lifeCycleException(false)
 
58
{
 
59
    // Get life cycle exception state
 
60
    QGSettings lceSettings("com.canonical.qtmir", "/com/canonical/qtmir/");
 
61
    QStringList appIds = lceSettings.get("lifecycle-exempt-appids").toStringList();
 
62
 
 
63
    Q_FOREACH(const QString &id, appIds) {
 
64
        if (id == m_appId) {
 
65
            m_lifeCycleException = true;
 
66
            Q_EMIT lifeCycleExceptionChanged();
 
67
 
 
68
            break;
 
69
        }
 
70
    }
 
71
 
 
72
    connect(this, &Package::lifeCycleExceptionChanged, this, &Package::updateLifeCycleExceptionState);
 
73
 
 
74
    // Get general hooks info
 
75
    int appsNumber = 0;
 
76
    int scopesNumber = 0;
 
77
    int accountsNumber = 0;
 
78
    int pushHelpersNumber = 0;
 
79
 
 
80
    Q_FOREACH(const Hook &hook, hooks) {
 
81
        if (hook.type == Hook::HookType::App)
 
82
            ++appsNumber;
 
83
 
 
84
        if (hook.type == Hook::HookType::Scope)
 
85
            ++scopesNumber;
 
86
 
 
87
        if (hook.type == Hook::HookType::OnlineAccount)
 
88
            ++accountsNumber;
 
89
 
 
90
        if (hook.type == Hook::HookType::PushHelper)
 
91
            ++pushHelpersNumber;
 
92
    }
 
93
 
 
94
    m_containsApp = (appsNumber > 0);
 
95
    m_containsScope = (scopesNumber > 0);
 
96
    m_containsOnlineAccount = (accountsNumber > 0);
 
97
    m_containsPushHelper = (pushHelpersNumber > 0);
 
98
}
 
99
 
 
100
void Package::clearAppData()
 
101
{
 
102
    QDir d(APPDATA_FOLDER + m_appId);
 
103
 
 
104
    if (d.exists())
 
105
        d.removeRecursively();
 
106
 
 
107
    updateSize();
 
108
}
 
109
 
 
110
void Package::clearAppCache()
 
111
{
 
112
    QDir d(CACHE_FOLDER + m_appId);
 
113
 
 
114
    if (d.exists())
 
115
        d.removeRecursively();
 
116
 
 
117
    updateSize();
 
118
}
 
119
 
 
120
void Package::clearAppConfig()
 
121
{
 
122
    QDir d(CONFIG_FOLDER + m_appId);
 
123
 
 
124
    if (d.exists())
 
125
        d.removeRecursively();
 
126
 
 
127
    updateSize();
 
128
}
 
129
 
 
130
QVariantMap Package::getHook(int index)
 
131
{
 
132
    const Hook &hook = m_hooks.at(index);
 
133
 
 
134
    QVariantMap result;
 
135
 
 
136
    result["name"] = hook.name;
 
137
 
 
138
    switch(hook.type) {
 
139
    case Hook::HookType::Unknown:
 
140
        result["type"] = "Unknown";
 
141
        break;
 
142
    case Hook::HookType::App:
 
143
        result["type"] = "App";
 
144
        break;
 
145
    case Hook::HookType::Scope:
 
146
        result["type"] = "Scope";
 
147
        break;
 
148
    case Hook::HookType::OnlineAccount:
 
149
        result["type"] = "OnlineAccount";
 
150
        break;
 
151
    case Hook::HookType::PushHelper:
 
152
        result["type"] = "PushHelper";
 
153
        break;
 
154
    }
 
155
 
 
156
    qDebug() << "Looking for an AppArmor profile at" << hook.appArmorPath;
 
157
 
 
158
    QVariantMap appArmor;
 
159
 
 
160
    if (QFile::exists(hook.appArmorPath)) {
 
161
        QFile file(hook.appArmorPath);
 
162
 
 
163
        if (file.open(QIODevice::ReadOnly)) {
 
164
            QTextStream in(&file);
 
165
            const QString &apparmorTxt = in.readAll();
 
166
            file.close();
 
167
 
 
168
            appArmor = QJsonDocument::fromJson(apparmorTxt.toUtf8()).object().toVariantMap();
 
169
        } else {
 
170
            qDebug() << "Cannot read the AppArmor profile!";
 
171
        }
 
172
    } else {
 
173
        qDebug() << "AppArmor profile not found!";
 
174
    }
 
175
 
 
176
    result["apparmor"] = appArmor;
 
177
 
 
178
    return result;
 
179
}
 
180
 
 
181
void Package::updateSize()
 
182
{
 
183
    QDirIterator appFolderIt(m_directory, QDirIterator::Subdirectories);
 
184
    qint64 newInstalledSize = 0;
 
185
    while (appFolderIt.hasNext()) {
 
186
        appFolderIt.next();
 
187
        newInstalledSize += appFolderIt.fileInfo().size();
 
188
    }
 
189
 
 
190
    QDirIterator appDataIt(APPDATA_FOLDER + m_appId, QDirIterator::Subdirectories);
 
191
    qint64 newAppDataSize = 0;
 
192
    while (appDataIt.hasNext()) {
 
193
        appDataIt.next();
 
194
        newAppDataSize += appDataIt.fileInfo().size();
 
195
    }
 
196
 
 
197
    QDirIterator cacheIt(CACHE_FOLDER + m_appId, QDirIterator::Subdirectories);
 
198
    qint64 newCacheSize = 0;
 
199
    while (cacheIt.hasNext()) {
 
200
        cacheIt.next();
 
201
        newCacheSize += cacheIt.fileInfo().size();
 
202
    }
 
203
 
 
204
    QDirIterator configIt(CONFIG_FOLDER + m_appId, QDirIterator::Subdirectories);
 
205
    qint64 newConfigSize = 0;
 
206
    while (configIt.hasNext()) {
 
207
        configIt.next();
 
208
        newConfigSize += configIt.fileInfo().size();
 
209
    }
 
210
 
 
211
    if (m_installedSize != newInstalledSize) {
 
212
        m_installedSize = newInstalledSize;
 
213
        Q_EMIT installedSizeChanged();
 
214
    }
 
215
 
 
216
 
 
217
    if (m_dataSize != newAppDataSize) {
 
218
        m_dataSize = newAppDataSize;
 
219
        Q_EMIT dataSizeChanged();
 
220
    }
 
221
 
 
222
    if (m_configSize != newConfigSize) {
 
223
        m_configSize = newConfigSize;
 
224
        Q_EMIT configSizeChanged();
 
225
    }
 
226
 
 
227
    if (m_cacheSize != newCacheSize) {
 
228
        m_cacheSize = newCacheSize;
 
229
        Q_EMIT cacheSizeChanged();
 
230
    }
 
231
}
 
232
 
 
233
void Package::updateLifeCycleExceptionState()
 
234
{
 
235
    QGSettings lceSettings("com.canonical.qtmir", "/com/canonical/qtmir/");
 
236
    QStringList appIds = lceSettings.get("lifecycle-exempt-appids").toStringList();
 
237
 
 
238
    if (m_lifeCycleException) {     // Set to true -> add exception
 
239
        if (!appIds.contains(m_appId)) {
 
240
            appIds.append(m_appId);
 
241
        }
 
242
    } else {    // Set to false -> remove exception
 
243
        if (appIds.contains(m_appId)) {
 
244
            appIds.removeAll(m_appId);
 
245
        }
 
246
    }
 
247
 
 
248
    lceSettings.set("lifecycle-exempt-appids", appIds);
 
249
}