Changeset View
Changeset View
Standalone View
Standalone View
kcms/style/gtkpage.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2020 Mikhail Zolotukhin <zomial@protonmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QDir> | ||||
22 | #include <QUrl> | ||||
23 | #include <QDBusInterface> | ||||
24 | #include <QDBusReply> | ||||
25 | | ||||
26 | #include <KTar> | ||||
27 | #include <KLocalizedString> | ||||
28 | #include <KNS3/DownloadDialog> | ||||
29 | | ||||
30 | #include "gtkpage.h" | ||||
31 | #include "gtkthemesmodel.h" | ||||
32 | | ||||
33 | GtkPage::GtkPage(QObject *parent) | ||||
34 | : QObject(parent) | ||||
35 | , m_gtk2ThemesModel(new GtkThemesModel(this)) | ||||
36 | , m_gtk3ThemesModel(new GtkThemesModel(this)) | ||||
37 | , gtkConfigInterface( | ||||
38 | QStringLiteral("org.kde.GtkConfig"), | ||||
39 | QStringLiteral("/GtkConfig"), | ||||
40 | QStringLiteral("org.kde.GtkConfig") | ||||
41 | ) | ||||
42 | { | ||||
43 | connect(m_gtk2ThemesModel, &GtkThemesModel::themeRemoved, this, &GtkPage::onThemeRemoved); | ||||
44 | connect(m_gtk3ThemesModel, &GtkThemesModel::themeRemoved, this, &GtkPage::onThemeRemoved); | ||||
45 | | ||||
46 | connect(m_gtk2ThemesModel, &GtkThemesModel::selectedThemeChanged, this, [this](){ | ||||
47 | Q_EMIT gtkThemeSettingsChanged(); | ||||
48 | }); | ||||
49 | connect(m_gtk3ThemesModel, &GtkThemesModel::selectedThemeChanged, this, [this](){ | ||||
50 | Q_EMIT gtkThemeSettingsChanged(); | ||||
51 | }); | ||||
52 | } | ||||
53 | | ||||
54 | GtkPage::~GtkPage() | ||||
55 | { | ||||
56 | delete m_gtk2ThemesModel; | ||||
57 | delete m_gtk3ThemesModel; | ||||
58 | } | ||||
59 | | ||||
60 | QString GtkPage::gtk2ThemeFromConfig() | ||||
61 | { | ||||
62 | QDBusReply<QString> dbusReply = gtkConfigInterface.call(QStringLiteral("gtk2Theme")); | ||||
63 | return dbusReply.value(); | ||||
64 | } | ||||
65 | | ||||
66 | QString GtkPage::gtk3ThemeFromConfig() | ||||
67 | { | ||||
68 | QDBusReply<QString> dbusReply = gtkConfigInterface.call(QStringLiteral("gtk3Theme")); | ||||
69 | return dbusReply.value(); | ||||
70 | } | ||||
71 | | ||||
72 | void GtkPage::showGtk2Preview() | ||||
73 | { | ||||
74 | gtkConfigInterface.call(QStringLiteral("showGtk2ThemePreview"), m_gtk2ThemesModel->selectedTheme()); | ||||
75 | } | ||||
76 | | ||||
77 | void GtkPage::showGtk3Preview() | ||||
78 | { | ||||
79 | gtkConfigInterface.call(QStringLiteral("showGtk3ThemePreview"), m_gtk3ThemesModel->selectedTheme()); | ||||
80 | } | ||||
81 | | ||||
82 | | ||||
83 | void GtkPage::installGtk2ThemeFromGHNS() | ||||
84 | { | ||||
85 | KNS3::DownloadDialog downloadDialog(QStringLiteral("cgctheme.knsrc")); | ||||
86 | downloadDialog.setWindowTitle(i18n("Download New GNOME/GTK2 Application Styles")); | ||||
87 | downloadDialog.setWindowModality(Qt::WindowModal); | ||||
88 | if (downloadDialog.exec()) { | ||||
89 | load(); | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | void GtkPage::installGtk3ThemeFromGHNS() | ||||
94 | { | ||||
95 | KNS3::DownloadDialog downloadDialog(QStringLiteral("cgcgtk3.knsrc")); | ||||
96 | downloadDialog.setWindowTitle(i18n("Download New GNOME/GTK3 Application Styles")); | ||||
97 | downloadDialog.setWindowModality(Qt::WindowModal); | ||||
98 | if (downloadDialog.exec()) { | ||||
99 | load(); | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | void GtkPage::onThemeRemoved() | ||||
104 | { | ||||
105 | load(); | ||||
106 | defaults(); | ||||
107 | save(); | ||||
108 | } | ||||
109 | | ||||
110 | void GtkPage::installGtkThemeFromFile(const QUrl &fileUrl) | ||||
111 | { | ||||
112 | QString themesInstallDirectoryPath(QDir::homePath() + QStringLiteral("/.themes")); | ||||
113 | QDir::home().mkpath(themesInstallDirectoryPath); | ||||
114 | KTar themeArchive(fileUrl.path()); | ||||
115 | themeArchive.open(QIODevice::ReadOnly); | ||||
116 | | ||||
117 | auto showError = [this, fileUrl]() { | ||||
118 | Q_EMIT showErrorMessage(i18n("%1 is not a valid GTK Theme archive.", fileUrl.fileName())); | ||||
119 | }; | ||||
120 | | ||||
121 | QString firstEntryName = themeArchive.directory()->entries().first(); | ||||
122 | const KArchiveEntry *possibleThemeDirectory = themeArchive.directory()->entry(firstEntryName); | ||||
123 | if (possibleThemeDirectory->isDirectory()) { | ||||
124 | const KArchiveDirectory *themeDirectory = static_cast<const KArchiveDirectory *>(possibleThemeDirectory); | ||||
125 | QStringList archiveSubitems = themeDirectory->entries(); | ||||
126 | | ||||
127 | if (!archiveSubitems.contains(QStringLiteral("gtk-2.0")) && archiveSubitems.indexOf(QRegExp("gtk-3.*")) == -1) { | ||||
128 | showError(); | ||||
129 | return; | ||||
130 | } | ||||
131 | } else { | ||||
132 | showError(); | ||||
133 | return; | ||||
134 | } | ||||
135 | | ||||
136 | themeArchive.directory()->copyTo(themesInstallDirectoryPath); | ||||
137 | | ||||
138 | load(); | ||||
139 | } | ||||
140 | | ||||
141 | void GtkPage::save() | ||||
142 | { | ||||
143 | gtkConfigInterface.call(QStringLiteral("setGtk2Theme"), m_gtk2ThemesModel->selectedTheme()); | ||||
144 | gtkConfigInterface.call(QStringLiteral("setGtk3Theme"), m_gtk3ThemesModel->selectedTheme()); | ||||
145 | } | ||||
146 | | ||||
147 | void GtkPage::defaults() | ||||
148 | { | ||||
149 | Q_EMIT selectGtk2ThemeInCombobox(QStringLiteral("Breeze")); | ||||
150 | Q_EMIT selectGtk3ThemeInCombobox(QStringLiteral("Breeze")); | ||||
151 | } | ||||
152 | | ||||
153 | void GtkPage::load() | ||||
154 | { | ||||
155 | m_gtk2ThemesModel->loadGtk2(); | ||||
156 | m_gtk3ThemesModel->loadGtk3(); | ||||
157 | Q_EMIT selectGtk2ThemeInCombobox(gtk2ThemeFromConfig()); | ||||
158 | Q_EMIT selectGtk3ThemeInCombobox(gtk3ThemeFromConfig()); | ||||
159 | } |