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
|
/*
SPDX-FileCopyrightText: 2020 Mikhail Zolotukhin <zomial@protonmail.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
#include <QUrl>
#include <KIO/DeleteJob>
#include "gtkthemesmodel.h"
GtkThemesModel::GtkThemesModel(QObject *parent)
: QAbstractListModel(parent)
, m_selectedTheme(QStringLiteral("Breeze"))
, m_themes()
{
}
void GtkThemesModel::load()
{
QMap<QString, QString> gtk3ThemesNames;
static const QStringList gtk3SubdirPattern(QStringLiteral("gtk-3.*"));
for (const QString &possibleThemePath : possiblePathsToThemes()) {
// If the directory contains any of gtk-3.X folders, it is the GTK3 theme for sure
QDir possibleThemeDirectory(possibleThemePath);
if (!possibleThemeDirectory.entryList(gtk3SubdirPattern, QDir::Dirs).isEmpty()) {
// Do not show dark Breeze GTK variant, since the colors of it
// are coming from the color scheme and selecting them here
// is redundant and does not work
if (possibleThemeDirectory.dirName() == QStringLiteral("Breeze-Dark")) {
continue;
}
gtk3ThemesNames.insert(possibleThemeDirectory.dirName(), possibleThemeDirectory.path());
}
}
setThemesList(gtk3ThemesNames);
}
QString GtkThemesModel::themePath(const QString &themeName)
{
if (themeName.isEmpty()) {
return QString();
} else {
return m_themes.constFind(themeName).value();
}
}
QVariant GtkThemesModel::data(const QModelIndex &index, int role) const
{
if (!checkIndex(index)) {
return QVariant();
}
const auto &item = m_themes.constBegin() + index.row();
switch (role) {
case Qt::DisplayRole:
case Roles::ThemeNameRole:
return item.key();
case Roles::ThemePathRole:
return item.value();
default:
return QVariant();
}
}
QHash<int, QByteArray> GtkThemesModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
roles[Roles::ThemeNameRole] = "theme-name";
roles[Roles::ThemePathRole] = "theme-path";
return roles;
}
int GtkThemesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_themes.count();
}
void GtkThemesModel::setThemesList(const QMap<QString, QString> &themes)
{
beginResetModel();
m_themes = themes;
endResetModel();
}
QMap<QString, QString> GtkThemesModel::themesList()
{
return m_themes;
}
void GtkThemesModel::setSelectedTheme(const QString &themeName)
{
m_selectedTheme = themeName;
Q_EMIT selectedThemeChanged(themeName);
}
QString GtkThemesModel::selectedTheme()
{
return m_selectedTheme;
}
QStringList GtkThemesModel::possiblePathsToThemes()
{
QStringList possibleThemesPaths;
QStringList themesLocationsPaths =
QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("themes"), QStandardPaths::LocateDirectory);
themesLocationsPaths << QDir::homePath() + QStringLiteral("/.themes");
for (const QString &themesLocationPath : qAsConst(themesLocationsPaths)) {
const QStringList possibleThemesDirectoriesNames = QDir(themesLocationPath).entryList(QDir::NoDotAndDotDot | QDir::AllDirs);
for (const QString &possibleThemeDirectoryName : possibleThemesDirectoriesNames) {
possibleThemesPaths += themesLocationPath + '/' + possibleThemeDirectoryName;
}
}
return possibleThemesPaths;
}
bool GtkThemesModel::selectedThemeRemovable()
{
return themePath(m_selectedTheme).contains(QDir::homePath());
}
void GtkThemesModel::removeSelectedTheme()
{
QString path = themePath(m_selectedTheme);
KIO::DeleteJob *deleteJob = KIO::del(QUrl::fromLocalFile(path), KIO::HideProgressInfo);
connect(deleteJob, &KJob::finished, this, [this]() {
Q_EMIT themeRemoved();
});
}
int GtkThemesModel::findThemeIndex(const QString &themeName)
{
return static_cast<int>(std::distance(m_themes.constBegin(), m_themes.constFind(themeName)));
}
void GtkThemesModel::setSelectedThemeDirty()
{
Q_EMIT selectedThemeChanged(m_selectedTheme);
}
|