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"
|
2013-02-24 10:57:58 +01:00
|
|
|
#include "qzregexp.h"
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTextBrowser>
|
|
|
|
#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);
|
|
|
|
item->setText(themeInfo.name + "\n" + themeInfo.shortDescription);
|
|
|
|
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);
|
|
|
|
ui->descirption->setText(currentTheme.longDescription);
|
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
|
|
|
|
|
|
|
if (!QFile(path + "main.css").exists() || !QFile(path + "theme.info").exists()) {
|
|
|
|
info.isValid = false;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (QFile(path + "theme.png").exists()) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.icon = QIcon(path + "theme.png");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.icon = QIcon(":icons/preferences/style-default.png");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (QFile(path + "theme.license").exists()) {
|
2013-01-22 19:04:22 +01:00
|
|
|
info.license = QzTools::readAllFileContents(path + "theme.license");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
2013-01-22 19:04:22 +01:00
|
|
|
QString theme_info = QzTools::readAllFileContents(path + "theme.info");
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2013-02-24 10:57:58 +01:00
|
|
|
QzRegExp rx("Name:(.*)\\n");
|
2011-09-11 19:15:06 +02:00
|
|
|
rx.setMinimal(true);
|
|
|
|
rx.indexIn(theme_info);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (rx.captureCount() == 1) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.name = rx.cap(1).trimmed();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2013-06-07 12:59:22 +02:00
|
|
|
if (info.name.isEmpty() || m_themeHash.contains(info.name)) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
rx.setPattern("Author:(.*)\\n");
|
|
|
|
rx.indexIn(theme_info);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (rx.captureCount() == 1) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.author = rx.cap(1).trimmed();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
rx.setPattern("Short Description:(.*)\\n");
|
|
|
|
rx.indexIn(theme_info);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (rx.captureCount() == 1) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.shortDescription = rx.cap(1).trimmed();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
rx.setPattern("Long Description:(.*)\\n");
|
|
|
|
rx.indexIn(theme_info);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (rx.captureCount() == 1) {
|
2011-09-11 19:15:06 +02:00
|
|
|
info.longDescription = rx.cap(1).trimmed();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|