summaryrefslogtreecommitdiffstats
path: root/framework/src/extensionmodel.cpp
blob: 5f42aa7ad7b36719884b74cad23f5f67439ff56b (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
/*
    Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/
#include "extensionmodel.h"
#include <QStandardItemModel>
#include <QQmlEngine>
#include <QDir>
#include <QDebug>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonObject>

using namespace Kube;

ExtensionModel::ExtensionModel(QObject *parent)
    : QSortFilterProxyModel(parent)
{
    setDynamicSortFilter(true);
    sort(0, Qt::DescendingOrder);
    setFilterCaseSensitivity(Qt::CaseInsensitive);
    QTimer::singleShot(0, this, &ExtensionModel::load);
}

QHash<int, QByteArray> ExtensionModel::roleNames() const
{
    return {
        {Name, "name"},
        {Tooltip, "tooltip"},
        {Icon, "icon"}
    };
}

void ExtensionModel::load()
{
    if (auto m = sourceModel()) {
        setSourceModel(nullptr);
        delete m;
    }
    auto engine = qmlEngine(this);
    if (!engine) {
        return;
    }
    auto model = new QStandardItemModel(this);
    for (const auto &path : engine->importPathList()) {
        QDir dir{path + "/org/kube/" + mExtensionPoint};
        for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
            const auto pluginPath = dir.path() + "/" + pluginName;
            mPaths.insert(pluginName, pluginPath);
            auto item = new QStandardItem;
            item->setData(pluginName, Name);
            item->setData(pluginName, Tooltip);
            item->setData("kdocumentinfo-inverted", Icon);

            if (QFileInfo::exists(pluginPath + "/metadata.json")) {
                QFile file{pluginPath + "/metadata.json"};
                file.open(QIODevice::ReadOnly);
                auto json = QJsonDocument::fromJson(file.readAll());
                auto map = json.object().toVariantMap();
                item->setData(map.value("icon").toString(), Icon);
                item->setData(map.value("tooltip").toString(), Tooltip);
                if (map.value("hidden", false).toBool()) {
                    delete item;
                    continue;
                }
            }

            model->appendRow(item);
        }
    }
    setSourceModel(model);
}

QString ExtensionModel::findSource(const QString &extensionName, const QString &sourceName)
{
    if (mPaths.isEmpty()) {
        load();
    }
    return mPaths.value(extensionName) + "/" + sourceName;
}

void ExtensionModel::setSortOrder(const QVariantList &order)
{
    mSortOrder.clear();
    for (const auto &e : order) {
        mSortOrder << e.toString();
    }
}

QVariantList ExtensionModel::sortOrder() const
{
    return {};
}

void ExtensionModel::setExtensionPoint(const QString &extensionPoint)
{
    mExtensionPoint = extensionPoint;
    QTimer::singleShot(0, this, &ExtensionModel::load);
}

QString ExtensionModel::extensionPoint() const
{
    return mExtensionPoint;
}

QVariant ExtensionModel::data(const QModelIndex &idx, int role) const
{
    return QSortFilterProxyModel::data(idx, role);
}

bool ExtensionModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
    auto leftIndex = mSortOrder.indexOf(left.data(Name).toString());
    auto rightIndex = mSortOrder.indexOf(right.data(Name).toString());
    if (leftIndex >= 0 && rightIndex >= 0) {
        //Higher index is less than
        return leftIndex > rightIndex;
    }
    if (leftIndex < 0 && rightIndex < 0) {
        return QSortFilterProxyModel::lessThan(left, right);
    }
    return leftIndex < rightIndex;
}