summaryrefslogtreecommitdiffstats
path: root/src/applicationsmodel.cpp
blob: ed54584339ad547598adad1106ad647c939c7e94 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Device Creation.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "applicationsmodel.h"

#include <QCoreApplication>
#include <QDirIterator>
#include <QEvent>
#include <QThread>
#include <QDebug>
#include <QRegExp>
#include <QJsonDocument>
#include <QJsonObject>
#include <QXmlStreamReader>

const QEvent::Type RESULT_EVENT = (QEvent::Type) (QEvent::User + 1);
class ResultEvent : public QEvent
{
public:
    ResultEvent(const QList<AppData> &r)
        : QEvent(RESULT_EVENT)
        , results(r)
    {
    }
    QList<AppData> results;
};

static bool appOrder(const AppData& a, const AppData& b)
{
    if (a.priority != b.priority)
        return a.priority > b.priority;
    else
        return a.name < b.name;
}

class IndexingThread : public QThread
{
public:

    void run()
    {
        QList<AppData> results;
        QList<QString> roots = root.split(":");
        target = qgetenv("B2QT_BASE") + "-" + qgetenv("B2QT_PLATFORM");
        foreach (const QString &root, roots) {
            if (QFile::exists(root + "/demos.xml")) {

                QFile file(root + "/demos.xml");

                if (!file.open(QIODevice::ReadOnly))
                    break;

                QXmlStreamReader xml(&file);

                AppData data;
                bool exclude = false;

                while (!xml.atEnd()) {
                    switch (xml.readNext()) {

                    case QXmlStreamReader::StartElement:
                        if (xml.name().toString().toLower() == "application") {

                            const QStringList excludeList = xml.attributes().value("exclude").toString().split(QStringLiteral(";"));

                            exclude = excludeList.contains(target) || excludeList.contains(QStringLiteral("all"));

                            if (exclude)
                                break;

                            data.name = xml.attributes().value("title").toString().trimmed();

                            QString path = xml.attributes().value("location").toString();
                            data.location = QUrl::fromLocalFile(path);

                            data.main = QString("/%1").arg(xml.attributes().value("main").toString());

                            QString imageName = xml.attributes().value("icon").toString();

                            data.icon = QFile::exists(imageName)
                                    ? QUrl::fromLocalFile(imageName)
                                    : QUrl("qrc:///qml/images/codeless.png");


                            data.priority = xml.attributes().value("priority").toInt();

                        } else if (xml.name().toString().toLower() == "description") {
                            data.description = xml.readElementText().trimmed();
                        }
                        break;

                    case QXmlStreamReader::EndElement:
                        if (xml.name().toString().toLower() == "application" && !exclude)
                            results << data;
                        break;

                    default:
                        break;
                    }
                }

                if (xml.error() != QXmlStreamReader::NoError)
                    qWarning("XML Parser error: %s", qPrintable(xml.errorString()));
            }
        }

        std::sort(results.begin(), results.end(), appOrder);

        qDebug() << "Indexer: all done... total:" << results.size();
        QCoreApplication::postEvent(model, new ResultEvent(results));
    }

    QList<AppData> indexDirectory(const QString &root) {
        QDirIterator iterator(root);

        QList<AppData> results;

        while (iterator.hasNext()) {
            QString path = iterator.next();

            if (!QFile::exists(path + "/main.qml"))
                continue;

            QFile excludeFile(path + "/exclude.txt");
            if (excludeFile.open(QFile::ReadOnly)) {
                const QStringList excludeList = QString::fromUtf8(excludeFile.readAll()).split(QRegExp(":|\\s+"));
                if (excludeList.contains(target) || excludeList.contains(QStringLiteral("all")))
                    continue;
            }

            AppData data;
            data.location = QUrl::fromLocalFile(path);

            QFile titleFile(path + "/title.txt");
            if (titleFile.open(QFile::ReadOnly))
                data.name = QString::fromUtf8(titleFile.readAll());

            if (data.name.isEmpty())
                data.name = iterator.fileName();

            data.main = "main.qml";

            QFile file(path + "/description.txt");
            if (file.open(QFile::ReadOnly))
                data.description = QString::fromUtf8(file.readAll());

            QString imageName = path + "/preview_l.jpg";
            data.icon = QFile::exists(imageName)
                    ? QUrl::fromLocalFile(imageName)
                    : QUrl("qrc:///qml/images/codeless.png");

            results << data;
        }

        std::sort(results.begin(), results.end(), appOrder);

        // Remove any leading digits followed by '.' from the name
        for (int i = 0; i < results.count(); ++i) {
            results[i].name.remove(QRegExp("^\\d+\\.\\s*"));
        }

        return results;
    }

    QString root;
    ApplicationsModel *model;
    QString target;
};

ApplicationsModel::ApplicationsModel(QObject *parent) :
    QAbstractItemModel(parent)
{
}

QHash<int, QByteArray> ApplicationsModel::roleNames() const
{
    QHash<int, QByteArray> names;
    names[NameRole] = "name";
    names[DescriptionRole] = "description";
    names[MainFileRole] = "mainFile";
    names[LocationRole] = "location";
    names[IconRole] = "icon";
    names[PriorityRole] = "priority";
    return names;
}

void ApplicationsModel::initialize(const QString &appsRoot)
{
    IndexingThread *thread = new IndexingThread;
    thread->root = appsRoot;
    thread->model = this;
    thread->start();
}

bool ApplicationsModel::event(QEvent *e)
{
    if (e->type() == RESULT_EVENT) {
        beginResetModel();
        m_data = static_cast<ResultEvent *>(e)->results;
        endResetModel();
        emit ready();
        return true;
    }

    return QAbstractItemModel::event(e);
}

QVariant ApplicationsModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(index.row() >= 0 && index.row() < m_data.size());
    Q_ASSERT(index.column() == 0);

    const AppData &ad = m_data.at(index.row());

    switch (role) {
    case NameRole: return ad.name;
    case DescriptionRole: return ad.description;
    case LocationRole: return ad.location;
    case MainFileRole: return ad.main;
    case IconRole: return ad.icon;
    case PriorityRole: return ad.priority;
    default: qDebug() << "ApplicationsModel::data: unhandled role" << role;
    }

    return QVariant();
}

QString ApplicationsModel::nameAt(int i) const
{
    if (i < 0 || i >= m_data.size()) {
        return "ERROR: out-of-range";
    }
    return m_data[i].name;
}

QString ApplicationsModel::locationAt(int i) const
{
    if (i < 0 || i >= m_data.size()) {
        return "ERROR: out-of-range";
    }
    return m_data[i].location.toLocalFile();
}

QVariant ApplicationsModel::query(int i, const QString &name) const
{
    if (i < 0 || i >= m_data.size()) {
        return QVariant();
    }

    const AppData &ad = m_data.at(i);
    if (name == QStringLiteral("description")) return ad.description;
    else if (name == QStringLiteral("name")) return ad.name;
    else if (name == QStringLiteral("location")) return ad.location;
    else if (name == QStringLiteral("mainFile")) return ad.main;
    else if (name == QStringLiteral("icon")) return ad.icon;
    else if (name == QStringLiteral("priority")) return ad.priority;

    qWarning("ApplicationsModel::query: Asking for bad name %s", qPrintable(name));

    return QVariant();
}