From f149aaebf2dbfaf32984d6c18e804410572515b5 Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 7 Jun 2013 12:59:22 +0200 Subject: [PATCH] Add possibility to load themes from profile directories. Themes are now loaded from the following directories: 1. Directory "themes" in user profile 2. Directory "themes" in root profile directory 3. System data path > /usr/share/qupzilla/themes on Linux > $EXECUTABLE_DIR/themes on Windows Closes #928 --- CHANGELOG | 1 + src/lib/app/mainapplication.cpp | 121 +++++++++++++++++---------- src/lib/app/mainapplication.h | 1 + src/lib/preferences/thememanager.cpp | 48 ++++++----- src/lib/preferences/thememanager.h | 2 +- 5 files changed, 109 insertions(+), 64 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1c9f987b0..c26734ed1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,7 @@ Version 1.5.0 * added KWallet password backend plugin * added Gnome-Keyring password backend plugin * added StatusBar Icons plugin that adds extra icons to statusbar + * themes can now be loaded from profile directories * pagescreen can now save output into number of formats, including PDF * proxy exceptions now supports wildcards (*, ?) * cancel upload when trying to upload non-readable files diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index f58b835c7..9cb2613d3 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -338,53 +338,13 @@ void MainApplication::loadSettings() settings.beginGroup("Themes"); QString activeTheme = settings.value("activeTheme", DEFAULT_THEME_NAME).toString(); settings.endGroup(); - m_activeThemePath = THEMESDIR + activeTheme + "/"; - QFile cssFile(m_activeThemePath + "main.css"); - cssFile.open(QFile::ReadOnly); - QString css = cssFile.readAll(); - cssFile.close(); -#ifdef QZ_WS_X11 - if (QFile(m_activeThemePath + "linux.css").exists()) { - cssFile.setFileName(m_activeThemePath + "linux.css"); - cssFile.open(QFile::ReadOnly); - css.append(cssFile.readAll()); - cssFile.close(); - } -#endif -#ifdef Q_OS_MAC - if (QFile(m_activeThemePath + "mac.css").exists()) { - cssFile.setFileName(m_activeThemePath + "mac.css"); - cssFile.open(QFile::ReadOnly); - css.append(cssFile.readAll()); - cssFile.close(); - } -#endif -#if defined(Q_OS_WIN) || defined(Q_OS_OS2) - if (QFile(m_activeThemePath + "windows.css").exists()) { - cssFile.setFileName(m_activeThemePath + "windows.css"); - cssFile.open(QFile::ReadOnly); - css.append(cssFile.readAll()); - cssFile.close(); - } -#endif - //RTL Support - //loading 'rtl.css' when layout is right to left! - if (isRightToLeft() && QFile(m_activeThemePath + "rtl.css").exists()) { - cssFile.setFileName(m_activeThemePath + "rtl.css"); - cssFile.open(QFile::ReadOnly); - css.append(cssFile.readAll()); - cssFile.close(); - } - - QString relativePath = QDir::current().relativeFilePath(m_activeThemePath); - css.replace(QzRegExp("url\\s*\\(\\s*([^\\*:\\);]+)\\s*\\)", Qt::CaseSensitive), - QString("url(%1\\1)").arg(relativePath + "/")); - setStyleSheet(css); + loadTheme(activeTheme); + // Create global QWebSettings object webSettings(); - //Web browsing settings + // Web browsing settings settings.beginGroup("Web-Browser-Settings"); if (!m_isPrivateSession) { @@ -707,6 +667,81 @@ void MainApplication::connectDatabase() m_databaseConnected = true; } +void MainApplication::loadTheme(const QString &name) +{ + // Themes are loaded from the following directories: + // 1. Directory "themes" in user profile + // 2. Directory "themes" in root profile directory + // 3. System data path + // > /usr/share/qupzilla/themes on Linux + // > $EXECUTABLE_DIR/themes on Windows + + QStringList themePaths; + themePaths << m_activeProfil + "themes/" + << PROFILEDIR + "themes/" + << THEMESDIR; + + foreach (const QString &path, themePaths) { + const QString &theme = path + name + "/"; + if (QFile::exists(theme + "main.css")) { + m_activeThemePath = theme; + break; + } + } + + if (m_activeThemePath.isEmpty()) { + qWarning("Cannot load theme '%s'!", qPrintable(name)); + m_activeThemePath = THEMESDIR + DEFAULT_THEME_NAME + "/"; + } + + QFile cssFile(m_activeThemePath + "main.css"); + cssFile.open(QFile::ReadOnly); + QString css = cssFile.readAll(); + cssFile.close(); + +#ifdef QZ_WS_X11 + if (QFile(m_activeThemePath + "linux.css").exists()) { + cssFile.setFileName(m_activeThemePath + "linux.css"); + cssFile.open(QFile::ReadOnly); + css.append(cssFile.readAll()); + cssFile.close(); + } +#endif + +#ifdef Q_OS_MAC + if (QFile(m_activeThemePath + "mac.css").exists()) { + cssFile.setFileName(m_activeThemePath + "mac.css"); + cssFile.open(QFile::ReadOnly); + css.append(cssFile.readAll()); + cssFile.close(); + } +#endif + +#if defined(Q_OS_WIN) || defined(Q_OS_OS2) + if (QFile(m_activeThemePath + "windows.css").exists()) { + cssFile.setFileName(m_activeThemePath + "windows.css"); + cssFile.open(QFile::ReadOnly); + css.append(cssFile.readAll()); + cssFile.close(); + } +#endif + + // RTL Support + // Loading 'rtl.css' when layout is right to left! + if (isRightToLeft() && QFile(m_activeThemePath + "rtl.css").exists()) { + cssFile.setFileName(m_activeThemePath + "rtl.css"); + cssFile.open(QFile::ReadOnly); + css.append(cssFile.readAll()); + cssFile.close(); + } + + QString relativePath = QDir::current().relativeFilePath(m_activeThemePath); + css.replace(QzRegExp("url\\s*\\(\\s*([^\\*:\\);]+)\\s*\\)", Qt::CaseSensitive), + QString("url(%1\\1)").arg(relativePath + "/")); + + setStyleSheet(css); +} + void MainApplication::translateApp() { Settings settings; diff --git a/src/lib/app/mainapplication.h b/src/lib/app/mainapplication.h index 2b13dd09c..34ec03a2d 100644 --- a/src/lib/app/mainapplication.h +++ b/src/lib/app/mainapplication.h @@ -160,6 +160,7 @@ private slots: private: enum PostLaunchAction { OpenDownloadManager, OpenNewTab }; + void loadTheme(const QString &name); void translateApp(); void restoreOtherWindows(); diff --git a/src/lib/preferences/thememanager.cpp b/src/lib/preferences/thememanager.cpp index beedfe5b0..fe1c47165 100644 --- a/src/lib/preferences/thememanager.cpp +++ b/src/lib/preferences/thememanager.cpp @@ -40,25 +40,31 @@ ThemeManager::ThemeManager(QWidget* parent, Preferences* preferences) m_activeTheme = settings.value("activeTheme", DEFAULT_THEME_NAME).toString(); settings.endGroup(); - QDir themeDir(mApp->THEMESDIR); - QStringList list = themeDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot); + QStringList themePaths; + themePaths << mApp->currentProfilePath() + "themes/" + << mApp->PROFILEDIR + "themes/" + << mApp->THEMESDIR; - foreach (const QString &name, list) { - Theme themeInfo = parseTheme(name); - if (!themeInfo.isValid) { - continue; + foreach (const QString &path, themePaths) { + QDir dir(path); + QStringList list = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot); + foreach (const QString &name, list) { + Theme themeInfo = parseTheme(dir.absoluteFilePath(name) + "/", name); + 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); } - - 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); } connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(currentChanged())); @@ -96,12 +102,10 @@ void ThemeManager::currentChanged() ui->license->setHidden(currentTheme.license.isEmpty()); } -ThemeManager::Theme ThemeManager::parseTheme(const QString &name) +ThemeManager::Theme ThemeManager::parseTheme(const QString &path, 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; @@ -127,6 +131,10 @@ ThemeManager::Theme ThemeManager::parseTheme(const QString &name) info.name = rx.cap(1).trimmed(); } + if (info.name.isEmpty() || m_themeHash.contains(info.name)) { + return info; + } + rx.setPattern("Author:(.*)\\n"); rx.indexIn(theme_info); if (rx.captureCount() == 1) { diff --git a/src/lib/preferences/thememanager.h b/src/lib/preferences/thememanager.h index 787426bdb..358ba2939 100644 --- a/src/lib/preferences/thememanager.h +++ b/src/lib/preferences/thememanager.h @@ -57,7 +57,7 @@ private: QString license; }; - Theme parseTheme(const QString &name); + Theme parseTheme(const QString &path, const QString &name); Ui::ThemeManager* ui; Preferences* m_preferences;