Warning, file /plasma/plasma-workspace/kcms/style/gtkpage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2020 Mikhail Zolotukhin <zomial@protonmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include <QDBusPendingCall>
0008 #include <QDir>
0009 #include <QStandardPaths>
0010 #include <QUrl>
0011 
0012 #include <KLocalizedString>
0013 #include <KNS3/DownloadDialog>
0014 #include <KTar>
0015 
0016 #include "gtkpage.h"
0017 
0018 GtkPage::GtkPage(QObject *parent)
0019     : QObject(parent)
0020     , m_gtkThemesModel(new GtkThemesModel(this))
0021     , m_gtkConfigInterface(QStringLiteral("org.kde.GtkConfig"), QStringLiteral("/GtkConfig"), QDBusConnection::sessionBus())
0022 {
0023     connect(m_gtkThemesModel, &GtkThemesModel::themeRemoved, this, &GtkPage::onThemeRemoved);
0024 
0025     connect(m_gtkThemesModel, &GtkThemesModel::selectedThemeChanged, this, [this]() {
0026         Q_EMIT gtkThemeSettingsChanged();
0027     });
0028 
0029     load();
0030 }
0031 
0032 GtkPage::~GtkPage() = default;
0033 
0034 bool GtkPage::gtkPreviewAvailable()
0035 {
0036     return !QStandardPaths::findExecutable(QStringLiteral("gtk3_preview"), {CMAKE_INSTALL_FULL_LIBEXECDIR}).isEmpty();
0037 }
0038 
0039 void GtkPage::showGtkPreview()
0040 {
0041     m_gtkConfigInterface.showGtkThemePreview(m_gtkThemesModel->selectedTheme());
0042 }
0043 
0044 void GtkPage::onThemeRemoved()
0045 {
0046     load();
0047     defaults();
0048     save();
0049 }
0050 
0051 void GtkPage::installGtkThemeFromFile(const QUrl &fileUrl)
0052 {
0053     QString themesInstallDirectoryPath(QDir::homePath() + QStringLiteral("/.themes"));
0054     QDir::home().mkpath(themesInstallDirectoryPath);
0055     KTar themeArchive(fileUrl.path());
0056     themeArchive.open(QIODevice::ReadOnly);
0057 
0058     auto showError = [this, fileUrl]() {
0059         Q_EMIT showErrorMessage(i18n("%1 is not a valid GTK Theme archive.", fileUrl.fileName()));
0060     };
0061 
0062     QString firstEntryName = themeArchive.directory()->entries().first();
0063     const KArchiveEntry *possibleThemeDirectory = themeArchive.directory()->entry(firstEntryName);
0064     if (possibleThemeDirectory->isDirectory()) {
0065         const KArchiveDirectory *themeDirectory = static_cast<const KArchiveDirectory *>(possibleThemeDirectory);
0066         QStringList archiveSubitems = themeDirectory->entries();
0067 
0068         if (!archiveSubitems.contains(QStringLiteral("gtk-2.0")) && archiveSubitems.indexOf(QRegularExpression(QStringLiteral("gtk-3.*"))) == -1) {
0069             showError();
0070             return;
0071         }
0072     } else {
0073         showError();
0074         return;
0075     }
0076 
0077     themeArchive.directory()->copyTo(themesInstallDirectoryPath);
0078 
0079     load();
0080 }
0081 
0082 void GtkPage::save()
0083 {
0084     auto call = m_gtkConfigInterface.setGtkTheme(m_gtkThemesModel->selectedTheme());
0085     // needs to block so "OK" button closing kcmshell still saves properly
0086     call.waitForFinished();
0087 }
0088 
0089 void GtkPage::defaults()
0090 {
0091     m_gtkThemesModel->setSelectedTheme(QStringLiteral("Breeze"));
0092 }
0093 
0094 void GtkPage::load()
0095 {
0096     m_gtkThemesModel->load();
0097     m_gtkThemesModel->setSelectedTheme(m_gtkConfigInterface.gtkTheme());
0098 }
0099 
0100 bool GtkPage::isDefaults() const
0101 {
0102     return m_gtkThemesModel->selectedTheme() == QLatin1String("Breeze");
0103 }
0104 
0105 bool GtkPage::isSaveNeeded()
0106 {
0107     return m_gtkThemesModel->selectedTheme() != m_gtkConfigInterface.gtkTheme();
0108 }