2013-11-04 07:23:05 +05:30
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-09-07 07:30:58 +05:30
|
|
|
#include "IconList.h"
|
2013-08-10 22:04:08 +05:30
|
|
|
#include <pathutils.h>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QDir>
|
2013-08-27 09:09:49 +05:30
|
|
|
#include <QMimeData>
|
|
|
|
#include <QUrl>
|
2013-08-10 22:04:08 +05:30
|
|
|
#define MAX_SIZE 1024
|
|
|
|
|
|
|
|
struct entry
|
|
|
|
{
|
|
|
|
QString key;
|
|
|
|
QString name;
|
|
|
|
QIcon icon;
|
|
|
|
bool is_builtin;
|
2013-08-27 09:09:49 +05:30
|
|
|
QString filename;
|
2013-08-10 22:04:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class Private : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
QMap<QString, int> index;
|
|
|
|
QVector<entry> icons;
|
|
|
|
Private()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IconList::IconList() : QAbstractListModel(), d(new Private())
|
|
|
|
{
|
|
|
|
QDir instance_icons(":/icons/instances/");
|
|
|
|
auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto file_info : file_info_list)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
|
|
|
QString key = file_info.baseName();
|
|
|
|
addIcon(key, key, file_info.absoluteFilePath(), true);
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
// FIXME: get from settings
|
2013-08-24 06:39:46 +05:30
|
|
|
ensureFolderPathExists("icons");
|
|
|
|
QDir user_icons("icons");
|
2013-08-10 22:04:08 +05:30
|
|
|
file_info_list = user_icons.entryInfoList(QDir::Files, QDir::Name);
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto file_info : file_info_list)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
|
|
|
QString filename = file_info.absoluteFilePath();
|
|
|
|
QString key = file_info.baseName();
|
|
|
|
addIcon(key, key, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IconList::~IconList()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
d = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-27 09:09:49 +05:30
|
|
|
QStringList IconList::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
|
|
|
types << "text/uri-list";
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
Qt::DropActions IconList::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::CopyAction;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
|
|
|
|
const QModelIndex &parent)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
if (action == Qt::IgnoreAction)
|
2013-11-04 07:23:05 +05:30
|
|
|
return true;
|
2013-08-27 09:09:49 +05:30
|
|
|
// check if the action is supported
|
|
|
|
if (!data || !(action & supportedDropActions()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// files dropped from outside?
|
2013-11-04 07:23:05 +05:30
|
|
|
if (data->hasUrls())
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
/*
|
|
|
|
bool was_watching = is_watching;
|
|
|
|
if(was_watching)
|
|
|
|
stopWatching();
|
|
|
|
*/
|
|
|
|
auto urls = data->urls();
|
|
|
|
QStringList iconFiles;
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto url : urls)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
// only local files may be dropped...
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!url.isLocalFile())
|
2013-08-27 09:09:49 +05:30
|
|
|
continue;
|
|
|
|
iconFiles += url.toLocalFile();
|
|
|
|
}
|
|
|
|
installIcons(iconFiles);
|
|
|
|
/*
|
|
|
|
if(was_watching)
|
|
|
|
startWatching();
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
Qt::ItemFlags IconList::flags(const QModelIndex &index) const
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
|
2013-08-27 09:09:49 +05:30
|
|
|
if (index.isValid())
|
|
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
|
|
|
else
|
|
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
QVariant IconList::data(const QModelIndex &index, int role) const
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!index.isValid())
|
2013-08-10 22:04:08 +05:30
|
|
|
return QVariant();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
int row = index.row();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
if (row < 0 || row >= d->icons.size())
|
2013-08-10 22:04:08 +05:30
|
|
|
return QVariant();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
switch (role)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
case Qt::DecorationRole:
|
|
|
|
return d->icons[row].icon;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return d->icons[row].name;
|
|
|
|
case Qt::UserRole:
|
|
|
|
return d->icons[row].key;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2013-08-10 22:04:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
int IconList::rowCount(const QModelIndex &parent) const
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
|
|
|
return d->icons.size();
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
void IconList::installIcons(QStringList iconFiles)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
for (QString file : iconFiles)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
QFileInfo fileinfo(file);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
2013-08-27 09:09:49 +05:30
|
|
|
continue;
|
|
|
|
QString target = PathCombine("icons", fileinfo.fileName());
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-27 09:09:49 +05:30
|
|
|
QString suffix = fileinfo.suffix();
|
2013-11-04 07:23:05 +05:30
|
|
|
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg")
|
2013-08-27 09:09:49 +05:30
|
|
|
continue;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
if (!QFile::copy(file, target))
|
2013-08-27 09:09:49 +05:30
|
|
|
continue;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-27 09:09:49 +05:30
|
|
|
QString key = fileinfo.baseName();
|
|
|
|
addIcon(key, key, target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
bool IconList::deleteIcon(QString key)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
int iconIdx = getIconIndex(key);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (iconIdx == -1)
|
2013-08-27 09:09:49 +05:30
|
|
|
return false;
|
2013-11-04 07:23:05 +05:30
|
|
|
auto &iconEntry = d->icons[iconIdx];
|
|
|
|
if (iconEntry.is_builtin)
|
2013-08-27 09:09:49 +05:30
|
|
|
return false;
|
2013-11-04 07:23:05 +05:30
|
|
|
if (QFile::remove(iconEntry.filename))
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), iconIdx, iconIdx);
|
|
|
|
d->icons.remove(iconIdx);
|
|
|
|
reindex();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
bool IconList::addIcon(QString key, QString name, QString path, bool is_builtin)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
|
|
|
auto iter = d->index.find(key);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (iter != d->index.end())
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (d->icons[*iter].is_builtin)
|
2013-08-27 09:09:49 +05:30
|
|
|
return false;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
QIcon icon(path);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (icon.isNull())
|
2013-08-27 09:09:49 +05:30
|
|
|
return false;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
auto &oldOne = d->icons[*iter];
|
|
|
|
|
|
|
|
if (!QFile::remove(oldOne.filename))
|
2013-08-27 09:09:49 +05:30
|
|
|
return false;
|
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
// replace the icon
|
2013-08-27 09:09:49 +05:30
|
|
|
oldOne = {key, name, icon, is_builtin, path};
|
2013-11-04 07:23:05 +05:30
|
|
|
dataChanged(index(*iter), index(*iter));
|
2013-08-10 22:04:08 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QIcon icon(path);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (icon.isNull())
|
|
|
|
return false;
|
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
// add a new icon
|
2013-11-04 07:23:05 +05:30
|
|
|
beginInsertRows(QModelIndex(), d->icons.size(), d->icons.size());
|
2013-08-27 09:09:49 +05:30
|
|
|
d->icons.push_back({key, name, icon, is_builtin, path});
|
2013-08-10 22:04:08 +05:30
|
|
|
d->index[key] = d->icons.size() - 1;
|
2013-08-27 09:09:49 +05:30
|
|
|
endInsertRows();
|
2013-08-10 22:04:08 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 09:09:49 +05:30
|
|
|
void IconList::reindex()
|
|
|
|
{
|
|
|
|
d->index.clear();
|
|
|
|
int i = 0;
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto &iter : d->icons)
|
2013-08-27 09:09:49 +05:30
|
|
|
{
|
|
|
|
d->index[iter.key] = i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
QIcon IconList::getIcon(QString key)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
|
|
|
int icon_index = getIconIndex(key);
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
if (icon_index != -1)
|
2013-08-10 22:04:08 +05:30
|
|
|
return d->icons[icon_index].icon;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
// Fallback for icons that don't exist.
|
|
|
|
icon_index = getIconIndex("infinity");
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
if (icon_index != -1)
|
2013-08-10 22:04:08 +05:30
|
|
|
return d->icons[icon_index].icon;
|
|
|
|
return QIcon();
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
int IconList::getIconIndex(QString key)
|
2013-08-10 22:04:08 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (key == "default")
|
2013-08-10 22:04:08 +05:30
|
|
|
key = "infinity";
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
auto iter = d->index.find(key);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (iter != d->index.end())
|
2013-08-10 22:04:08 +05:30
|
|
|
return *iter;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-08-10 22:04:08 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-07 07:30:58 +05:30
|
|
|
#include "IconList.moc"
|