summaryrefslogtreecommitdiffstats
path: root/application/pages/modplatform/FtbListModel.cpp
blob: 51aec8907f25bbc44aac44862914a720010d0544 (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
#include "FtbListModel.h"
#include "MultiMC.h"

#include <MMCStrings.h>
#include <Version.h>

#include <QtMath>
#include <QLabel>

#include <RWStorage.h>
#include <Env.h>

#include "net/URLConstants.h"

FtbFilterModel::FtbFilterModel(QObject *parent) : QSortFilterProxyModel(parent)
{
    currentSorting = Sorting::ByGameVersion;
    sortings.insert(tr("Sort by name"), Sorting::ByName);
    sortings.insert(tr("Sort by game version"), Sorting::ByGameVersion);
}

bool FtbFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
    FtbModpack leftPack = sourceModel()->data(left, Qt::UserRole).value<FtbModpack>();
    FtbModpack rightPack = sourceModel()->data(right, Qt::UserRole).value<FtbModpack>();

    if(currentSorting == Sorting::ByGameVersion) {
        Version lv(leftPack.mcVersion);
        Version rv(rightPack.mcVersion);
        return lv < rv;

    } else if(currentSorting == Sorting::ByName) {
        return Strings::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
    }

    //UHM, some inavlid value set?!
    qWarning() << "Invalid sorting set!";
    return true;
}

bool FtbFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    return true;
}

const QMap<QString, FtbFilterModel::Sorting> FtbFilterModel::getAvailableSortings()
{
    return sortings;
}

QString FtbFilterModel::translateCurrentSorting()
{
    return sortings.key(currentSorting);
}

void FtbFilterModel::setSorting(Sorting s)
{
    currentSorting = s;
    invalidate();
}

FtbFilterModel::Sorting FtbFilterModel::getCurrentSorting()
{
    return currentSorting;
}

FtbListModel::FtbListModel(QObject *parent) : QAbstractListModel(parent)
{
}

FtbListModel::~FtbListModel()
{
}

QString FtbListModel::translatePackType(FtbPackType type) const
{
    switch(type)
    {
        case FtbPackType::Public:
            return tr("Public Modpack");
        case FtbPackType::ThirdParty:
            return tr("Third Party Modpack");
        case FtbPackType::Private:
            return tr("Private Modpack");
    }
    qWarning() << "Unknown FTB modpack type:" << int(type);
    return QString();
}

int FtbListModel::rowCount(const QModelIndex &parent) const
{
    return modpacks.size();
}

int FtbListModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

QVariant FtbListModel::data(const QModelIndex &index, int role) const
{
    int pos = index.row();
    if(pos >= modpacks.size() || pos < 0 || !index.isValid())
    {
        return QString("INVALID INDEX %1").arg(pos);
    }

    FtbModpack pack = modpacks.at(pos);
    if(role == Qt::DisplayRole)
    {
        return pack.name + "\n" + translatePackType(pack.type);
    }
    else if (role == Qt::ToolTipRole)
    {
        if(pack.description.length() > 100)
        {
            //some magic to prevent to long tooltips and replace html linebreaks
            QString edit = pack.description.left(97);
            edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
            return edit;

        }
        return pack.description;
    }
    else if(role == Qt::DecorationRole)
    {
        if(m_logoMap.contains(pack.logo))
        {
            return (m_logoMap.value(pack.logo));
        }
        QIcon icon = MMC->getThemedIcon("screenshot-placeholder");
        ((FtbListModel *)this)->requestLogo(pack.logo);
        return icon;
    }
    else if(role == Qt::TextColorRole)
    {
        if(pack.broken)
        {
            //FIXME: Hardcoded color
            return QColor(255, 0, 50);
        }
        else if(pack.bugged)
        {
            //FIXME: Hardcoded color
            //bugged pack, currently only indicates bugged xml
            return QColor(244, 229, 66);
        }
    }
    else if(role == Qt::UserRole)
    {
        QVariant v;
        v.setValue(pack);
        return v;
    }

    return QVariant();
}

void FtbListModel::fill(FtbModpackList modpacks)
{
    beginResetModel();
    this->modpacks = modpacks;
    endResetModel();
}

void FtbListModel::addPack(FtbModpack modpack)
{
    beginResetModel();
    this->modpacks.append(modpack);
    endResetModel();
}

void FtbListModel::clear()
{
    beginResetModel();
    modpacks.clear();
    endResetModel();
}

FtbModpack FtbListModel::at(int row)
{
    return modpacks.at(row);
}

void FtbListModel::remove(int row)
{
    if(row < 0 || row >= modpacks.size())
    {
        qWarning() << "Attempt to remove FTB modpacks with invalid row" << row;
        return;
    }
    beginRemoveRows(QModelIndex(), row, row);
    modpacks.removeAt(row);
    endRemoveRows();
}

void FtbListModel::logoLoaded(QString logo, QIcon out)
{
    m_loadingLogos.removeAll(logo);
    m_logoMap.insert(logo, out);
    emit dataChanged(createIndex(0, 0), createIndex(1, 0));
}

void FtbListModel::logoFailed(QString logo)
{
    m_failedLogos.append(logo);
    m_loadingLogos.removeAll(logo);
}

void FtbListModel::requestLogo(QString file)
{
    if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
    {
        return;
    }

    MetaEntryPtr entry = ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(file.section(".", 0, 0)));
    NetJob *job = new NetJob(QString("FTB Icon Download for %1").arg(file));
    job->addNetAction(Net::Download::makeCached(QUrl(QString(URLConstants::FTB_CDN_BASE_URL + "static/%1").arg(file)), entry));

    auto fullPath = entry->getFullPath();
    QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]
    {
        emit logoLoaded(file, QIcon(fullPath));
        if(waitingCallbacks.contains(file))
        {
            waitingCallbacks.value(file)(fullPath);
        }
    });

    QObject::connect(job, &NetJob::failed, this, [this, file]
    {
        emit logoFailed(file);
    });

    job->start();

    m_loadingLogos.append(file);
}

void FtbListModel::getLogo(const QString &logo, LogoCallback callback)
{
    if(m_logoMap.contains(logo))
    {
        callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
    }
    else
    {
        requestLogo(logo);
    }
}

Qt::ItemFlags FtbListModel::flags(const QModelIndex &index) const
{
    return QAbstractListModel::flags(index);
}