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 | | ||||
32 | GtkPage::GtkPage(QObject *parent) | ||||
33 | : QObject(parent) | ||||
34 | , m_gtk2ThemesModel(new GtkThemesModel(this)) | ||||
35 | , m_gtk3ThemesModel(new GtkThemesModel(this)) | ||||
36 | , gtkConfigInterface( | ||||
37 | QStringLiteral("org.kde.GtkConfig"), | ||||
38 | QStringLiteral("/GtkConfig"), | ||||
39 | QStringLiteral("org.kde.GtkConfig") | ||||
40 | ) | ||||
41 | { | ||||
42 | connect(m_gtk2ThemesModel, &GtkThemesModel::themesListNeedsUpdate, this, &GtkPage::load); | ||||
43 | connect(m_gtk3ThemesModel, &GtkThemesModel::themesListNeedsUpdate, this, &GtkPage::load); | ||||
44 | connect(m_gtk2ThemesModel, &GtkThemesModel::themeRemoved, this, &GtkPage::onThemeRemoved); | ||||
45 | connect(m_gtk3ThemesModel, &GtkThemesModel::themeRemoved, this, &GtkPage::onThemeRemoved); | ||||
46 | | ||||
47 | connect(m_gtk2ThemesModel, &GtkThemesModel::selectedThemeChanged, this, [this](){ | ||||
48 | Q_EMIT gtkThemeSettingsChanged(); | ||||
49 | }); | ||||
50 | connect(m_gtk3ThemesModel, &GtkThemesModel::selectedThemeChanged, this, [this](){ | ||||
51 | Q_EMIT gtkThemeSettingsChanged(); | ||||
52 | }); | ||||
53 | } | ||||
54 | | ||||
55 | GtkPage::~GtkPage() | ||||
56 | { | ||||
57 | delete m_gtk2ThemesModel; | ||||
58 | delete m_gtk3ThemesModel; | ||||
59 | } | ||||
60 | | ||||
61 | QString GtkPage::gtk2ThemeFromConfig() | ||||
62 | { | ||||
63 | QDBusReply<QString> dbusReply = gtkConfigInterface.call(QStringLiteral("gtk2Theme")); | ||||
64 | return dbusReply.value(); | ||||
65 | } | ||||
66 | | ||||
67 | QString GtkPage::gtk3ThemeFromConfig() | ||||
68 | { | ||||
69 | QDBusReply<QString> dbusReply = gtkConfigInterface.call(QStringLiteral("gtk3Theme")); | ||||
70 | return dbusReply.value(); | ||||
71 | } | ||||
72 | | ||||
73 | void GtkPage::showGtk2Preview() | ||||
74 | { | ||||
75 | gtkConfigInterface.call(QStringLiteral("showGtk2ThemePreview"), m_gtk2ThemesModel->selectedTheme()); | ||||
76 | } | ||||
77 | | ||||
78 | void GtkPage::showGtk3Preview() | ||||
79 | { | ||||
80 | gtkConfigInterface.call(QStringLiteral("showGtk3ThemePreview"), m_gtk3ThemesModel->selectedTheme()); | ||||
81 | } | ||||
82 | | ||||
83 | | ||||
84 | void GtkPage::installGtk2ThemeFromGHNS() | ||||
85 | { | ||||
86 | KNS3::DownloadDialog downloadDialog(QStringLiteral("cgctheme.knsrc")); | ||||
87 | downloadDialog.setWindowTitle(i18n("Download New GNOME/GTK2 Application Styles")); | ||||
88 | downloadDialog.setWindowModality(Qt::WindowModal); | ||||
89 | if (downloadDialog.exec()) { | ||||
90 | m_gtk2ThemesModel->requestThemesListUpdate(); | ||||
91 | } | ||||
92 | } | ||||
93 | | ||||
94 | void GtkPage::installGtk3ThemeFromGHNS() | ||||
95 | { | ||||
96 | KNS3::DownloadDialog downloadDialog(QStringLiteral("cgcgtk3.knsrc")); | ||||
97 | downloadDialog.setWindowTitle(i18n("Download New GNOME/GTK3 Application Styles")); | ||||
98 | downloadDialog.setWindowModality(Qt::WindowModal); | ||||
99 | if (downloadDialog.exec()) { | ||||
100 | m_gtk3ThemesModel->requestThemesListUpdate(); | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | void GtkPage::onThemeRemoved() | ||||
105 | { | ||||
106 | load(); | ||||
107 | defaults(); | ||||
108 | save(); | ||||
109 | } | ||||
110 | | ||||
111 | void GtkPage::installGtkThemeFromFile(const QUrl &fileUrl) | ||||
112 | { | ||||
113 | QString themesInstallDirectoryPath(QDir::homePath() + QStringLiteral("/.themes")); | ||||
114 | QDir::home().mkpath(themesInstallDirectoryPath); | ||||
115 | KTar themeArchive(fileUrl.path()); | ||||
116 | themeArchive.open(QIODevice::ReadOnly); | ||||
117 | | ||||
118 | auto showError = [this, fileUrl]() { | ||||
119 | Q_EMIT showErrorMessage(i18n("%1 is not a valid GTK Theme archive.", fileUrl.fileName())); | ||||
120 | }; | ||||
121 | | ||||
122 | QString firstEntryName = themeArchive.directory()->entries().first(); | ||||
123 | const KArchiveEntry *possibleThemeDirectory = themeArchive.directory()->entry(firstEntryName); | ||||
124 | if (possibleThemeDirectory->isDirectory()) { | ||||
125 | const KArchiveDirectory *themeDirectory = static_cast<const KArchiveDirectory *>(possibleThemeDirectory); | ||||
126 | QStringList archiveSubitems = themeDirectory->entries(); | ||||
127 | | ||||
128 | if (!archiveSubitems.contains(QStringLiteral("gtk-2.0")) && archiveSubitems.indexOf(QRegExp("gtk-3.*")) == -1) { | ||||
129 | showError(); | ||||
130 | return; | ||||
131 | } | ||||
132 | } else { | ||||
133 | showError(); | ||||
134 | return; | ||||
135 | } | ||||
136 | | ||||
137 | themeArchive.directory()->copyTo(themesInstallDirectoryPath); | ||||
138 | | ||||
139 | // We need to send request from only one model, because models are connected to each other | ||||
140 | m_gtk2ThemesModel->requestThemesListUpdate(); | ||||
141 | } | ||||
142 | | ||||
143 | void GtkPage::save() | ||||
144 | { | ||||
145 | gtkConfigInterface.call(QStringLiteral("setGtk2Theme"), m_gtk2ThemesModel->selectedTheme()); | ||||
146 | gtkConfigInterface.call(QStringLiteral("setGtk3Theme"), m_gtk3ThemesModel->selectedTheme()); | ||||
147 | } | ||||
148 | | ||||
149 | void GtkPage::defaults() | ||||
150 | { | ||||
151 | Q_EMIT selectGtk2ThemeInCombobox(QStringLiteral("Breeze")); | ||||
152 | Q_EMIT selectGtk3ThemeInCombobox(QStringLiteral("Breeze")); | ||||
153 | } | ||||
154 | | ||||
155 | void GtkPage::load() | ||||
156 | { | ||||
157 | m_gtk2ThemesModel->loadGtk2(); | ||||
158 | m_gtk3ThemesModel->loadGtk3(); | ||||
159 | Q_EMIT selectGtk2ThemeInCombobox(gtk2ThemeFromConfig()); | ||||
160 | Q_EMIT selectGtk3ThemeInCombobox(gtk3ThemeFromConfig()); | ||||
161 | } |