2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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"
|
|
|
|
#include "mainapplication.h"
|
2011-09-18 15:35:44 +02:00
|
|
|
#include "globalfunctions.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2012-03-04 18:30:34 +01:00
|
|
|
#include "licenseviewer.h"
|
|
|
|
#include "preferences.h"
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTextBrowser>
|
|
|
|
#include <QDir>
|
|
|
|
|
2011-12-28 13:16:04 +01:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
#define DEFAULT_THEME_NAME "windows"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_THEME_NAME "linux"
|
|
|
|
#endif
|
|
|
|
|
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);
|
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();
|
|
|
|
|
|
|
|
QDir themeDir(mApp->THEMESDIR);
|
|
|
|
QStringList list = themeDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
|
2012-01-23 17:30:34 +01:00
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const QString & name, list) {
|
2011-09-11 19:15:06 +02:00
|
|
|
Theme themeInfo = parseTheme(name);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!themeInfo.isValid) {
|
2011-09-11 19:15:06 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
|
|
|
|
item->setText(themeInfo.name + "\n" + themeInfo.shortDescription);
|
|
|
|
item->setIcon(themeInfo.icon);
|
|
|
|
item->setData(Qt::UserRole, name);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_activeTheme == name) {
|
2011-09-11 19:15:06 +02:00
|
|
|
ui->listWidget->setCurrentItem(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
ui->listWidget->addItem(item);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +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
|
|
|
}
|
|
|
|
|
|
|
|
ThemeManager::Theme ThemeManager::parseTheme(const QString &name)
|
|
|
|
{
|
|
|
|
Theme info;
|
|
|
|
info.name = name;
|
|
|
|
|
|
|
|
QString path = mApp->THEMESDIR + name + "/";
|
|
|
|
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()) {
|
2011-09-18 15:35:44 +02:00
|
|
|
info.license = qz_readAllFileContents(path + "theme.license");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
|
|
|
QString theme_info = qz_readAllFileContents(path + "theme.info");
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
QRegExp rx("Name:(.*)\\n");
|
|
|
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-09-11 19:15:06 +02:00
|
|
|
settings.beginGroup("Themes");
|
|
|
|
settings.setValue("activeTheme", ui->listWidget->currentItem()->data(Qt::UserRole));
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThemeManager::~ThemeManager()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|