2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-09-23 22:06:21 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-09-11 19:15:06 +02:00
|
|
|
#include "thememanager.h"
|
|
|
|
#include "ui_thememanager.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
2012-03-04 18:30:34 +01:00
|
|
|
#include "licenseviewer.h"
|
|
|
|
#include "preferences.h"
|
2018-02-11 11:19:27 +01:00
|
|
|
#include "desktopfile.h"
|
|
|
|
#include "mainapplication.h"
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
ThemeManager::ThemeManager(QWidget* parent, Preferences* preferences)
|
2011-09-11 19:15:06 +02:00
|
|
|
: QWidget()
|
|
|
|
, ui(new Ui::ThemeManager)
|
2012-03-04 18:30:34 +01:00
|
|
|
, m_preferences(preferences)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
|
|
|
ui->setupUi(parent);
|
2013-07-11 18:18:32 +02:00
|
|
|
ui->listWidget->setLayoutDirection(Qt::LeftToRight);
|
2011-09-18 15:35:44 +02:00
|
|
|
ui->license->hide();
|
2012-01-21 20:27:45 +01:00
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-09-11 19:15:06 +02:00
|
|
|
settings.beginGroup("Themes");
|
2011-12-28 13:16:04 +01:00
|
|
|
m_activeTheme = settings.value("activeTheme", DEFAULT_THEME_NAME).toString();
|
2011-09-11 19:15:06 +02:00
|
|
|
settings.endGroup();
|
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
const QStringList themePaths = DataPaths::allPaths(DataPaths::Themes);
|
2013-06-07 12:59:22 +02:00
|
|
|
|
|
|
|
foreach (const QString &path, themePaths) {
|
|
|
|
QDir dir(path);
|
|
|
|
QStringList list = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
|
|
|
|
foreach (const QString &name, list) {
|
2014-03-09 22:17:13 +01:00
|
|
|
Theme themeInfo = parseTheme(dir.absoluteFilePath(name) + QLatin1Char('/'), name);
|
2013-06-07 12:59:22 +02:00
|
|
|
if (!themeInfo.isValid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
|
2018-02-11 11:19:27 +01:00
|
|
|
item->setText(themeInfo.name);
|
2013-06-07 12:59:22 +02:00
|
|
|
item->setIcon(themeInfo.icon);
|
|
|
|
item->setData(Qt::UserRole, name);
|
|
|
|
|
|
|
|
if (m_activeTheme == name) {
|
|
|
|
ui->listWidget->setCurrentItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->listWidget->addItem(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(currentChanged()));
|
2011-09-18 15:35:44 +02:00
|
|
|
connect(ui->license, SIGNAL(clicked(QPoint)), this, SLOT(showLicense()));
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
currentChanged();
|
|
|
|
}
|
|
|
|
|
2011-09-18 15:35:44 +02:00
|
|
|
void ThemeManager::showLicense()
|
|
|
|
{
|
|
|
|
QListWidgetItem* currentItem = ui->listWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!currentItem) {
|
2011-09-18 15:35:44 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
|
|
|
Theme currentTheme = m_themeHash[currentItem->data(Qt::UserRole).toString()];
|
|
|
|
|
2012-03-04 18:30:34 +01:00
|
|
|
LicenseViewer* v = new LicenseViewer(m_preferences);
|
|
|
|
v->setText(currentTheme.license);
|
|
|
|
v->show();
|
2011-09-18 15:35:44 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
void ThemeManager::currentChanged()
|
|
|
|
{
|
|
|
|
QListWidgetItem* currentItem = ui->listWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!currentItem) {
|
2011-09-11 19:15:06 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
Theme currentTheme = m_themeHash[currentItem->data(Qt::UserRole).toString()];
|
|
|
|
|
|
|
|
ui->name->setText(currentTheme.name);
|
|
|
|
ui->author->setText(currentTheme.author);
|
2018-02-11 11:19:27 +01:00
|
|
|
ui->descirption->setText(currentTheme.description);
|
2011-09-18 15:35:44 +02:00
|
|
|
ui->license->setHidden(currentTheme.license.isEmpty());
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 12:59:22 +02:00
|
|
|
ThemeManager::Theme ThemeManager::parseTheme(const QString &path, const QString &name)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
|
|
|
Theme info;
|
2014-02-01 19:21:49 +01:00
|
|
|
info.isValid = false;
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2018-02-11 11:19:27 +01:00
|
|
|
if (!QFile(path + "main.css").exists() || !QFile(path + "metadata.desktop").exists()) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.isValid = false;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:19:27 +01:00
|
|
|
DesktopFile metadata(path + QSL("metadata.desktop"));
|
2018-02-26 21:52:39 +01:00
|
|
|
info.name = metadata.name();
|
|
|
|
info.description = metadata.comment();
|
2018-02-11 11:19:27 +01:00
|
|
|
info.author = metadata.value(QSL("X-Falkon-Author")).toString();
|
|
|
|
|
|
|
|
const QString iconName = metadata.icon();
|
|
|
|
if (!iconName.isEmpty()) {
|
|
|
|
if (QFileInfo::exists(path + iconName)) {
|
|
|
|
info.icon = QIcon(path + iconName);
|
|
|
|
} else {
|
|
|
|
info.icon = QIcon::fromTheme(iconName);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
2018-02-11 11:19:27 +01:00
|
|
|
const QString licensePath = metadata.value(QSL("X-Falkon-License")).toString();
|
|
|
|
if (!licensePath.isEmpty() && QFileInfo::exists(path + licensePath)) {
|
|
|
|
info.license = QzTools::readAllFileContents(path + licensePath);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2018-03-02 10:48:55 +01:00
|
|
|
if (info.name.isEmpty() || m_themeHash.contains(name)) {
|
2013-06-07 12:59:22 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
info.isValid = true;
|
|
|
|
m_themeHash.insert(name, info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThemeManager::save()
|
|
|
|
{
|
2013-07-13 00:01:24 +02:00
|
|
|
QListWidgetItem* currentItem = ui->listWidget->currentItem();
|
|
|
|
if (!currentItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-09-11 19:15:06 +02:00
|
|
|
settings.beginGroup("Themes");
|
2013-07-13 00:01:24 +02:00
|
|
|
settings.setValue("activeTheme", currentItem->data(Qt::UserRole));
|
2011-09-11 19:15:06 +02:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThemeManager::~ThemeManager()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|