Falkon Develop
Cross-platform Qt-based web browser
thememanager.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "thememanager.h"
19#include "ui_thememanager.h"
20#include "qztools.h"
21#include "settings.h"
22#include "datapaths.h"
23#include "licenseviewer.h"
24#include "preferences.h"
25#include "desktopfile.h"
26#include "mainapplication.h"
27
28#include <QDir>
29#include <QMessageBox>
30#include <QtCoreVersion>
31
32ThemeManager::ThemeManager(QWidget* parent, Preferences* preferences)
33 : QWidget(parent)
34 , ui(new Ui::ThemeManager)
35 , m_preferences(preferences)
36{
37 ui->setupUi(parent);
38 ui->listWidget->setLayoutDirection(Qt::LeftToRight);
39 ui->license->hide();
40 ui->remove->setIcon(QIcon::fromTheme(QSL("edit-delete")));
41
42 Settings settings;
43 settings.beginGroup(QSL("Themes"));
44 m_activeTheme = settings.value(QSL("activeTheme"), DEFAULT_THEME_NAME).toString();
45 settings.endGroup();
46
47 const QStringList themePaths = DataPaths::allPaths(DataPaths::Themes);
48
49 for (const QString &path : themePaths) {
50 QDir dir(path);
51 const QStringList list = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
52 for (const QString &name : list) {
53 Theme themeInfo = parseTheme(dir.absoluteFilePath(name) + QLatin1Char('/'), name);
54 if (!themeInfo.isValid) {
55 continue;
56 }
57
58 auto* item = new QListWidgetItem(ui->listWidget);
59 item->setText(themeInfo.name);
60 item->setIcon(themeInfo.icon);
61 item->setData(Qt::UserRole, name);
62
63 if (m_activeTheme == name) {
64 ui->listWidget->setCurrentItem(item);
65 }
66
67 ui->listWidget->addItem(item);
68 }
69 }
70
71 connect(ui->listWidget, &QListWidget::currentItemChanged, this, &ThemeManager::currentChanged);
72 connect(ui->license, &ClickableLabel::clicked, this, &ThemeManager::showLicense);
73 connect(ui->remove, &QPushButton::clicked, this, &ThemeManager::removeTheme);
74
75 currentChanged();
76}
77
78void ThemeManager::showLicense()
79{
80 QListWidgetItem* currentItem = ui->listWidget->currentItem();
81 if (!currentItem) {
82 return;
83 }
84
85 Theme currentTheme = m_themeHash[currentItem->data(Qt::UserRole).toString()];
86
87 auto* v = new LicenseViewer(m_preferences);
88 v->setText(currentTheme.license);
89 v->show();
90}
91
92void ThemeManager::removeTheme()
93{
94 QListWidgetItem* currentItem = ui->listWidget->currentItem();
95 if (!currentItem) {
96 return;
97 }
98 Theme currentTheme = m_themeHash[currentItem->data(Qt::UserRole).toString()];
99
100 const auto button = QMessageBox::warning(this, tr("Confirmation"),
101 tr("Are you sure you want to remove '%1'?").arg(currentTheme.name),
102 QMessageBox::Yes | QMessageBox::No);
103 if (button != QMessageBox::Yes) {
104 return;
105 }
106
107 QDir(currentTheme.themePath).removeRecursively();
108 delete currentItem;
109}
110
111void ThemeManager::currentChanged()
112{
113 QListWidgetItem* currentItem = ui->listWidget->currentItem();
114 if (!currentItem) {
115 return;
116 }
117
118 Theme currentTheme = m_themeHash[currentItem->data(Qt::UserRole).toString()];
119
120 ui->name->setText(currentTheme.name);
121 ui->author->setText(currentTheme.author);
122 ui->description->setText(currentTheme.description);
123 ui->license->setHidden(currentTheme.license.isEmpty());
124 ui->remove->setEnabled(QFileInfo(currentTheme.themePath).isWritable());
125}
126
127ThemeManager::Theme ThemeManager::parseTheme(const QString &path, const QString &name)
128{
129 Theme info;
130 info.isValid = false;
131
132 if (!QFile(path + QStringLiteral("main.css")).exists() || !QFile(path + QSL("metadata.desktop")).exists()) {
133 info.isValid = false;
134 return info;
135 }
136
137 DesktopFile metadata(path + QSL("metadata.desktop"));
138 info.name = metadata.name();
139 info.description = metadata.comment();
140 info.author = metadata.value(QSL("X-Falkon-Author")).toString();
141 info.themePath = path.chopped(1);
142
143 const QString iconName = metadata.icon();
144 if (!iconName.isEmpty()) {
145 if (QFileInfo::exists(path + iconName)) {
146 info.icon = QIcon(path + iconName);
147 } else {
148 info.icon = QIcon::fromTheme(iconName);
149 }
150 }
151
152 const QString licensePath = metadata.value(QSL("X-Falkon-License")).toString();
153 if (!licensePath.isEmpty() && QFileInfo::exists(path + licensePath)) {
154 info.license = QzTools::readAllFileContents(path + licensePath);
155 }
156
157 if (info.name.isEmpty() || m_themeHash.contains(name)) {
158 return info;
159 }
160
161 info.isValid = true;
162 m_themeHash.insert(name, info);
163 return info;
164}
165
167{
168 QListWidgetItem* currentItem = ui->listWidget->currentItem();
169 if (!currentItem) {
170 return;
171 }
172
173 Settings settings;
174 settings.beginGroup(QSL("Themes"));
175 settings.setValue(QSL("activeTheme"), currentItem->data(Qt::UserRole));
176 settings.endGroup();
177}
178
180{
181 delete ui;
182}
void clicked(QPoint)
static QStringList allPaths(Path type)
Definition: datapaths.cpp:74
static QString readAllFileContents(const QString &filename)
Definition: qztools.cpp:98
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
ThemeManager(QWidget *parent, Preferences *preferences)
#define DEFAULT_THEME_NAME
Definition: qzcommon.h:123
#define QSL(x)
Definition: qzcommon.h:40