diff --git a/CMakeLists.txt b/CMakeLists.txt index 677b8b9..907a801 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,71 +1,71 @@ project(kde-gtk-config) set(PROJECT_VERSION "5.10.90") cmake_minimum_required(VERSION 2.8.12) find_package(ECM 0.0.9 REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" ${ECM_MODULE_PATH}) find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Widgets Test) find_package(KF5 REQUIRED COMPONENTS I18n KIO ConfigWidgets NewStuff Archive KCMUtils IconThemes) find_package(X11 REQUIRED) include_directories( ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/ui ) include(ECMSetupVersion) include(ECMInstallIcons) include(ECMMarkAsTest) include(GenerateExportHeader) include(FeatureSummary) include(KDEInstallDirs) include(KDECMakeSettings) include(KDECompilerSettings NO_POLICY_SCOPE) # Set KI18n translation domain add_definitions(-DTRANSLATION_DOMAIN=\"kde-gtk-config\") set(kcm_SRCS src/iconthemesmodel.cpp src/cursorthemesmodel.cpp src/appearancegtk3.cpp src/appearancegtk2.cpp src/appearencegtk.cpp src/abstractappearance.cpp src/thread.cpp src/installer.cpp src/gtkconfigkcmodule.cpp src/dialog_installer.cpp src/dialog_uninstaller.cpp src/fontshelpers.cpp ) set(GHNS_FILES cgcicon.knsrc cgctheme.knsrc cgcgtk3.knsrc ) ki18n_wrap_ui(kcm_SRCS src/ui/gui.ui src/ui/dialog_installer.ui src/ui/dialog_uninstaller.ui ) add_library(kcm_kdegtkconfig MODULE ${kcm_SRCS}) target_compile_definitions(kcm_kdegtkconfig PRIVATE -DPROJECT_VERSION="${PROJECT_VERSION}") -target_link_libraries(kcm_kdegtkconfig ${X11_Xcursor_LIB} KF5::I18n KF5::KIOWidgets KF5::NewStuff KF5::Archive KF5::NewStuff KF5::ConfigWidgets KF5::IconThemes) +target_link_libraries(kcm_kdegtkconfig ${X11_Xcursor_LIB} KF5::ConfigCore KF5::I18n KF5::KIOWidgets KF5::NewStuff KF5::Archive KF5::NewStuff KF5::ConfigWidgets KF5::IconThemes) kcoreaddons_desktop_to_json(kcm_kdegtkconfig kde-gtk-config.desktop) install(TARGETS kcm_kdegtkconfig DESTINATION ${PLUGIN_INSTALL_DIR}) install(FILES ${GHNS_FILES} DESTINATION ${CONFIG_INSTALL_DIR}) install(FILES kde-gtk-config.desktop DESTINATION ${SERVICES_INSTALL_DIR}) add_subdirectory(gtkproxies) add_subdirectory(gtk3proxies) add_subdirectory(icons) add_subdirectory(tests) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/src/abstractappearance.h b/src/abstractappearance.h index 2961a09..208342e 100644 --- a/src/abstractappearance.h +++ b/src/abstractappearance.h @@ -1,70 +1,71 @@ /* KDE GTK Configuration Module * * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #ifndef ABSTACTAPPEARANCE_H #define ABSTACTAPPEARANCE_H #include #include #include class AbstractAppearance { public: virtual ~AbstractAppearance() {} - virtual QString defaultConfigFile() const = 0; + virtual bool loadSettings() = 0; + virtual bool saveSettings() const = 0; virtual bool loadSettings(const QString& path) = 0; virtual bool saveSettings(const QString& path) const = 0; - + /** @returns the installed themes' paths*/ virtual QStringList installedThemes() const = 0; void setTheme(const QString& name); void setCursor(const QString& name); void setIcon(const QString& name); void setIconFallback(const QString& name); void setFont(const QString& font); void setShowIconsInMenus(bool show); void setShowIconsInButtons(bool show); void setPrimaryButtonWarpsSlider(bool enabled); void setToolbarStyle(const QString& style); QString getTheme() const; QString getCursor() const; QString getIcon() const; QString getIconFallback() const; QString getFont() const; QString getThemeGtk3() const; QString getToolbarStyle() const; bool getShowIconsInButtons() const; bool getShowIconsInMenus() const; bool getPrimaryButtonWarpsSlider() const; QStringList installedThemesNames() const; bool hasProperty(const QString& key) const; static QMap readSettingsTuples(QIODevice* device); protected: QMap m_settings; }; #endif diff --git a/src/appearancegtk2.cpp b/src/appearancegtk2.cpp index 791bb1f..23a0b9a 100644 --- a/src/appearancegtk2.cpp +++ b/src/appearancegtk2.cpp @@ -1,184 +1,207 @@ /* KDE GTK Configuration Module * * Copyright 2011 José Antonio Sanchez Reynaga * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "appearancegtk2.h" #include #include #include #include #include #include #include -bool AppearanceGTK2::loadSettings(const QString& path) +bool AppearanceGTK2::loadSettingsPrivate(const QString& path) { QFile configFile(path); - bool canRead = configFile.open(QIODevice::ReadOnly | QIODevice::Text); + if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) + return false; - if(canRead) { -// qDebug() << "The gtk2 config file exists..."; - const QMap foundSettings = readSettingsTuples(&configFile); - m_settings = QMap { - {"toolbar_style", "GTK_TOOLBAR_ICONS"}, - {"show_icons_buttons", "0"}, - {"show_icons_menus", "0"}, - {"primary_button_warps_slider", "false"} - }; - - for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { - if (it.key() == "gtk-theme-name") - m_settings["theme"] = *it; - else if (it.key() == "gtk-icon-theme-name") - m_settings["icon"] = *it; - else if (it.key() == "gtk-fallback-icon-theme") - m_settings["icon_fallback"] = *it; - else if (it.key() == "gtk-cursor-theme-name") - m_settings["cursor"] = *it; - else if (it.key() == "gtk-font-name") - m_settings["font"] = *it; - else if (it.key() == "gtk-toolbar-style") - m_settings["toolbar_style"] = *it; - else if (it.key() == "gtk-button-images") - m_settings["show_icons_buttons"] = *it; - else if(it.key() == "gtk-menu-images") - m_settings["show_icons_menus"] = *it; - else if (it.key() == "gtk-primary-button-warps-slider") - m_settings["primary_button_warps_slider"] = *it; - else - qWarning() << "unknown field" << it.key(); - } - + const QMap foundSettings = readSettingsTuples(&configFile); + + for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { + if (it.key() == "gtk-theme-name") + m_settings["theme"] = *it; + else if (it.key() == "gtk-icon-theme-name") + m_settings["icon"] = *it; + else if (it.key() == "gtk-fallback-icon-theme") + m_settings["icon_fallback"] = *it; + else if (it.key() == "gtk-cursor-theme-name") + m_settings["cursor"] = *it; + else if (it.key() == "gtk-font-name") + m_settings["font"] = *it; + else if (it.key() == "gtk-toolbar-style") + m_settings["toolbar_style"] = *it; + else if (it.key() == "gtk-button-images") + m_settings["show_icons_buttons"] = *it; + else if(it.key() == "gtk-menu-images") + m_settings["show_icons_menus"] = *it; + else if (it.key() == "gtk-primary-button-warps-slider") + m_settings["primary_button_warps_slider"] = *it; + else + qWarning() << "unknown field" << it.key(); } - - return canRead; + return true; } QString AppearanceGTK2::themesGtkrcFile(const QString& themeName) const { QStringList themes=installedThemes(); themes=themes.filter(QRegExp("/"+themeName+"/?$")); if(themes.size()==1) { QDirIterator it(themes.first(), QDirIterator::Subdirectories); while(it.hasNext()) { it.next(); if(it.fileName()=="gtkrc") { // qDebug() << "\tgtkrc file found at : " << it.filePath(); return it.filePath(); } } } return QString(); } -bool AppearanceGTK2::saveSettings(const QString& gtkrcFile) const +bool AppearanceGTK2::saveSettingsPrivate(const QString& gtkrcFile) const { QFile gtkrc(gtkrcFile); gtkrc.remove(); if(!gtkrc.open(QIODevice::WriteOnly | QIODevice::Text)) { qWarning() << "There was unable to write the file .gtkrc-2.0"; return false; } QTextStream flow(>krc); flow << "# File created by KDE Gtk Config" << "\n" << "# Configs for GTK2 programs \n\n"; QString themeGtkrcFile=themesGtkrcFile(getTheme()); //TODO: is this really needed? if(!themeGtkrcFile.isEmpty()) flow << "include \"" << themeGtkrcFile << "\"\n"; //We include the theme's gtkrc file if(QFile::exists("/etc/gtk-2.0/gtkrc")) flow << "include \"/etc/gtk-2.0/gtkrc\"\n"; //We include the /etc's config file int nameEnd = m_settings["font"].lastIndexOf(QRegExp(" ([0-9]+|bold|italic)")); - QString fontFamily = m_settings["font"].left(nameEnd); + const auto fontFamily = m_settings["font"].leftRef(nameEnd); //TODO: is this really needed? flow << "style \"user-font\" \n" << "{\n" << "\tfont_name=\""<< fontFamily << "\"\n" << "}\n"; - + flow << "widget_class \"*\" style \"user-font\"\n"; flow << "gtk-font-name=\"" << m_settings["font"] << "\"\n"; flow << "gtk-theme-name=\"" << m_settings["theme"] << "\"\n"; flow << "gtk-icon-theme-name=\""<< m_settings["icon"] << "\"\n"; flow << "gtk-fallback-icon-theme=\"" << m_settings["icon_fallback"] << "\"\n"; flow << "gtk-cursor-theme-name=\"" << m_settings["cursor"] << "\"\n"; flow << "gtk-toolbar-style=" << m_settings["toolbar_style"] << "\n"; flow << "gtk-menu-images=" << m_settings["show_icons_menus"] << "\n"; flow << "gtk-button-images=" << m_settings["show_icons_buttons"] << "\n"; flow << "gtk-primary-button-warps-slider=" << m_settings["primary_button_warps_slider"] << "\n"; //we're done with the ~/.gtk-2.0 file gtkrc.close(); //TODO: do we really need the linked file? if(QFile::remove(gtkrcFile+"-kde4")) qDebug() << "ready to create the symbolic link"; if( !QFile::link(gtkrcFile, gtkrcFile+"-kde4") ) qWarning() << "Couldn't create the symboling link to .gtkrc-2.0-kde4 :("; // else // qDebug() << "Symbolic link created for .gtkrc-2.0-kde4 :D"; if(gtkrcFile==defaultConfigFile()) QProcess::startDetached(QStandardPaths::findExecutable("reload_gtk_apps")); return true; } +void AppearanceGTK2::reset() +{ + m_settings = QMap { + {"toolbar_style", "GTK_TOOLBAR_ICONS"}, + {"show_icons_buttons", "0"}, + {"show_icons_menus", "0"}, + {"primary_button_warps_slider", "false"} + }; +} + QString AppearanceGTK2::defaultConfigFile() const { return QDir::homePath()+"/.gtkrc-2.0"; } QStringList AppearanceGTK2::installedThemes() const { QFileInfoList availableThemes; foreach(const QString& themesDir, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "themes", QStandardPaths::LocateDirectory)) { QDir root(themesDir); availableThemes += root.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); } //Check if there are themes installed by the user QDir user(QDir::homePath()+"/.themes"); availableThemes += user.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); //we just want actual themes QStringList paths; for(QFileInfoList::const_iterator it=availableThemes.constBegin(); it!=availableThemes.constEnd(); ++it) { bool hasGtkrc = QDir(it->filePath()).exists("gtk-2.0"); //If it doesn't exist, we don't want it on the list if(hasGtkrc) paths += it->filePath(); } return paths; } + +bool AppearanceGTK2::loadSettings() +{ + reset(); + + return loadSettingsPrivate("/etc/gtk-2.0/gtkrc") && loadSettingsPrivate(defaultConfigFile()); +} + +bool AppearanceGTK2::saveSettings() const +{ + return saveSettings(defaultConfigFile()); +} + +bool AppearanceGTK2::loadSettings(const QString& gtkrcFile) +{ + reset(); + return loadSettingsPrivate(gtkrcFile); +} + +bool AppearanceGTK2::saveSettings(const QString& gtkrcFile) const +{ + return saveSettingsPrivate(gtkrcFile); +} diff --git a/src/appearancegtk2.h b/src/appearancegtk2.h index 8bc28ee..7df49bf 100644 --- a/src/appearancegtk2.h +++ b/src/appearancegtk2.h @@ -1,38 +1,45 @@ /* KDE GTK Configuration Module * * Copyright 2011 José Antonio Sanchez Reynaga * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #ifndef APPEARANCEGTK2_H #define APPEARANCEGTK2_H #include "abstractappearance.h" class AppearanceGTK2 : public AbstractAppearance { bool loadSettings(const QString& path) override; bool saveSettings(const QString& path) const override; - QString defaultConfigFile() const override; + bool loadSettings() override; + bool saveSettings() const override; QStringList installedThemes() const override; - + QString themesGtkrcFile(const QString& themeName) const; + +private: + void reset(); + QString defaultConfigFile() const; + bool loadSettingsPrivate(const QString& path); + bool saveSettingsPrivate(const QString& path) const; }; #endif // APPEARANCEGTK2_H diff --git a/src/appearancegtk3.cpp b/src/appearancegtk3.cpp index fa1bde5..7df48c3 100644 --- a/src/appearancegtk3.cpp +++ b/src/appearancegtk3.cpp @@ -1,143 +1,160 @@ /* KDE GTK Configuration Module * * Copyright 2011 José Antonio Sanchez Reynaga * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "appearancegtk3.h" #include #include #include #include +#include +#include QStringList AppearanceGTK3::installedThemes() const { QFileInfoList availableThemes; foreach(const QString& themesDir, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "themes", QStandardPaths::LocateDirectory)) { QDir root(themesDir); availableThemes += root.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); } //Also show the user-installed themes QDir user(QDir::homePath()+"/.themes"); availableThemes += user.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); //we just want actual themes QStringList themes; for(QFileInfoList::iterator it=availableThemes.begin(); it!=availableThemes.end(); ++it) { bool hasGtkrc = QDir(it->filePath()).exists("gtk-3.0"); //If it doesn't exist, we don't want it on the list if(hasGtkrc) themes += it->filePath(); } return themes; } -bool AppearanceGTK3::saveSettings(const QString& file) const +bool AppearanceGTK3::saveSettings(const KSharedConfig::Ptr& file) const { - //Opening GTK3 config file $ENV{XDG_CONFIG_HOME}/gtk-3.0/m_settings.ini - QDir::home().mkpath(file.left(file.lastIndexOf('/'))); //we make sure the path exists - QFile file_gtk3(file); - - if(!file_gtk3.open(QIODevice::WriteOnly | QIODevice::Text)) { - qWarning() << "Couldn't open GTK3 config file for writing at:" << file_gtk3.fileName(); + KConfigGroup group(file, "Settings"); + + group.writeEntry("gtk-font-name", m_settings["font"]); + group.writeEntry("gtk-theme-name", m_settings["theme"]); + group.writeEntry("gtk-icon-theme-name", m_settings["icon"]); + group.writeEntry("gtk-fallback-icon-theme", m_settings["icon_fallback"]); + group.writeEntry("gtk-cursor-theme-name", m_settings["cursor"]); + group.writeEntry("gtk-toolbar-style", m_settings["toolbar_style"]); + group.writeEntry("gtk-menu-images", m_settings["show_icons_menus"]); + group.writeEntry("gtk-button-images", m_settings["show_icons_buttons"]); + group.writeEntry("gtk-primary-button-warps-slider", m_settings["primary_button_warps_slider"]); + group.writeEntry("gtk-application-prefer-dark-theme", m_settings["application_prefer_dark_theme"]); + + const bool sync = group.sync(); + Q_ASSERT(sync); + return true; +} + +bool AppearanceGTK3::loadSettings(const KSharedConfig::Ptr& file) +{ + KConfigGroup group(file, "Settings"); + + if (!file || !group.isValid()) { + qWarning() << "Cannot open the GTK3 config file" << file; return false; } - QTextStream flow3(&file_gtk3); - flow3 << "[Settings]\n"; - flow3 << "gtk-font-name=" << m_settings["font"] << "\n"; - flow3 << "gtk-theme-name=" << m_settings["theme"] << "\n"; - flow3 << "gtk-icon-theme-name="<< m_settings["icon"] << "\n"; - flow3 << "gtk-fallback-icon-theme=" << m_settings["icon_fallback"] << "\n"; - flow3 << "gtk-cursor-theme-name=" << m_settings["cursor"] << "\n"; - flow3 << "gtk-toolbar-style=" << m_settings["toolbar_style"] << "\n"; - flow3 << "gtk-menu-images=" << m_settings["show_icons_menus"] << "\n"; - flow3 << "gtk-button-images=" << m_settings["show_icons_buttons"] << "\n"; - flow3 << "gtk-primary-button-warps-slider=" << m_settings["primary_button_warps_slider"] << "\n"; - flow3 << "gtk-application-prefer-dark-theme=" << m_settings["application_prefer_dark_theme"] << "\n"; + m_settings = QMap { + {"toolbar_style", "GTK_TOOLBAR_ICONS"}, + {"show_icons_buttons", "0"}, + {"show_icons_menus", "0"}, + {"primary_button_warps_slider", "false"}, + {"application_prefer_dark_theme", "false"} + }; + + m_settings["theme"] = group.readEntry("gtk-theme-name"); + m_settings["icon"] = group.readEntry("gtk-icon-theme-name"); + m_settings["icon_fallback"] = group.readEntry("gtk-fallback-icon-theme"); + m_settings["cursor"] = group.readEntry("gtk-cursor-theme-name"); + m_settings["font"] = group.readEntry("gtk-font-name"); + m_settings["toolbar_style"] = group.readEntry("gtk-toolbar-style"); + m_settings["show_icons_buttons"] = group.readEntry("gtk-button-images"); + m_settings["show_icons_menus"] = group.readEntry("gtk-menu-images"); + m_settings["primary_button_warps_slider"] = group.readEntry("gtk-primary-button-warps-slider"); + m_settings["application_prefer_dark_theme"] = group.readEntry("gtk-application-prefer-dark-theme"); + for(auto it = m_settings.begin(); it != m_settings.end(); ) { + if (it.value().isEmpty()) + it = m_settings.erase(it); + else + ++it; + } return true; } -bool AppearanceGTK3::loadSettings(const QString& path) +QString AppearanceGTK3::configFileName() const { - QFile fileGtk3(path); - bool canRead=fileGtk3.open(QIODevice::ReadOnly | QIODevice::Text); - - if(canRead) { - const QMap foundSettings = readSettingsTuples(&fileGtk3); - - m_settings = QMap { - {"toolbar_style", "GTK_TOOLBAR_ICONS"}, - {"show_icons_buttons", "0"}, - {"show_icons_menus", "0"}, - {"primary_button_warps_slider", "false"}, - {"application_prefer_dark_theme", "false"} - }; - - for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { - if (it.key() == "gtk-theme-name") - m_settings["theme"] = *it; - else if (it.key() == "gtk-icon-theme-name") - m_settings["icon"] = *it; - else if (it.key() == "gtk-fallback-icon-theme") - m_settings["icon_fallback"] = *it; - else if (it.key() == "gtk-cursor-theme-name") - m_settings["cursor"] = *it; - else if (it.key() == "gtk-font-name") - m_settings["font"] = *it; - else if (it.key() == "gtk-toolbar-style") - m_settings["toolbar_style"] = *it; - else if (it.key() == "gtk-button-images") - m_settings["show_icons_buttons"] = *it; - else if (it.key() == "gtk-menu-images") - m_settings["show_icons_menus"] = *it; - else if (it.key() == "gtk-primary-button-warps-slider") - m_settings["primary_button_warps_slider"] = *it; - else if (it.key() == "gtk-application-prefer-dark-theme") - m_settings["application_prefer_dark_theme"] = *it; - else - qWarning() << "unknown field" << it.key(); - } - } else - qWarning() << "Cannot open the GTK3 config file" << path; - - return canRead; + return QStringLiteral("gtk-3.0/settings.ini"); } QString AppearanceGTK3::defaultConfigFile() const { QString root = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); if(root.isEmpty()) root = QFileInfo(QDir::home(), ".config").absoluteFilePath(); - return root+"/gtk-3.0/settings.ini"; + return root + '/' + configFileName(); } bool AppearanceGTK3::getApplicationPreferDarkTheme() const { return m_settings["application_prefer_dark_theme"] == "1" || m_settings["application_prefer_dark_theme"] == "true"; } void AppearanceGTK3::setApplicationPreferDarkTheme(const bool& enable) { m_settings["application_prefer_dark_theme"] = enable ? "true" : "false"; } + +bool AppearanceGTK3::saveSettings(const QString& file) const +{ + auto cfg = KSharedConfig::openConfig(file); + return saveSettings(cfg); +} + +bool AppearanceGTK3::loadSettings(const QString& path) +{ + auto cfg = KSharedConfig::openConfig(path); + return loadSettings(cfg); +} + +bool AppearanceGTK3::loadSettings() +{ + auto cfg = KSharedConfig::openConfig(configFileName()); + cfg->setReadDefaults(true); + return loadSettings(cfg); +} + +bool AppearanceGTK3::saveSettings() const +{ + auto cfg = KSharedConfig::openConfig(configFileName()); + cfg->setReadDefaults(true); + return saveSettings(cfg); +} diff --git a/src/appearancegtk3.h b/src/appearancegtk3.h index d4562b1..3ce5a05 100644 --- a/src/appearancegtk3.h +++ b/src/appearancegtk3.h @@ -1,40 +1,48 @@ /* KDE GTK Configuration Module * * Copyright 2011 José Antonio Sanchez Reynaga * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #ifndef APPEARANCEGTK3_H #define APPEARANCEGTK3_H +#include #include "abstractappearance.h" class AppearanceGTK3 : public AbstractAppearance { public: QStringList installedThemes() const override; + bool saveSettings() const override; + bool loadSettings() override; bool saveSettings(const QString& file) const override; bool loadSettings(const QString& path) override; - QString defaultConfigFile() const override; bool getApplicationPreferDarkTheme() const; void setApplicationPreferDarkTheme(const bool& enable); + +private: + QString defaultConfigFile() const; + QString configFileName() const; + bool saveSettings(const KSharedConfig::Ptr& file) const; + bool loadSettings(const KSharedConfig::Ptr& file); }; #endif // APPEARANCEGTK3_H diff --git a/src/appearencegtk.cpp b/src/appearencegtk.cpp index 2e26a5a..95a6604 100644 --- a/src/appearencegtk.cpp +++ b/src/appearencegtk.cpp @@ -1,83 +1,83 @@ /* KDE GTK Configuration Module * * Copyright 2011 José Antonio Sanchez Reynaga * Copyright 2011 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "appearencegtk.h" #include #include "abstractappearance.h" #include "appearancegtk2.h" #include "appearancegtk3.h" AppearenceGTK::AppearenceGTK() { m_app << new AppearanceGTK2; m_app << new AppearanceGTK3; } AppearenceGTK::~AppearenceGTK() { qDeleteAll(m_app); } #define PROPERTY_IMPLEMENTATION(type, name, propname)\ void AppearenceGTK::set##name(const type& a) { foreach(AbstractAppearance* app, m_app) app->set##name(a); }\ type AppearenceGTK::get##name() const { foreach(AbstractAppearance* app, m_app) { if(app->hasProperty(propname)) return app->get##name(); } /*Q_ASSERT(false);*/ return type (); } PROPERTY_IMPLEMENTATION(QString, Cursor, "cursor") PROPERTY_IMPLEMENTATION(QString, Icon, "icon") PROPERTY_IMPLEMENTATION(QString, IconFallback, "icon_fallback") PROPERTY_IMPLEMENTATION(QString, Font, "font") PROPERTY_IMPLEMENTATION(QString, ToolbarStyle, "toolbar_style") PROPERTY_IMPLEMENTATION(bool, ShowIconsInButtons, "show_icons_buttons") PROPERTY_IMPLEMENTATION(bool, ShowIconsInMenus, "show_icons_menus") PROPERTY_IMPLEMENTATION(bool, PrimaryButtonWarpsSlider, "primary_button_warps_slider") QString AppearenceGTK::getTheme() const { return gtk2Appearance()->getTheme(); } void AppearenceGTK::setTheme(const QString& name) { return gtk2Appearance()->setTheme(name); } QString AppearenceGTK::getThemeGtk3() const { return gtk3Appearance()->getTheme(); } void AppearenceGTK::setThemeGtk3(const QString& name) { return gtk3Appearance()->setTheme(name); } bool AppearenceGTK::getApplicationPreferDarkTheme() const { return ((AppearanceGTK3*)gtk3Appearance())->getApplicationPreferDarkTheme(); } void AppearenceGTK::setApplicationPreferDarkTheme(const bool& enable) { return ((AppearanceGTK3*)gtk3Appearance())->setApplicationPreferDarkTheme(enable); } //////////////////////////////////// // Methods responsible of file creation bool AppearenceGTK::loadFileConfig() { bool correct = false; foreach(AbstractAppearance* app, m_app) { - bool c = app->loadSettings(app->defaultConfigFile()); + bool c = app->loadSettings(); correct = correct || c; } // qDebug() << "loading..." << correct; return correct; } bool AppearenceGTK::saveFileConfig() { bool correct = true; foreach(AbstractAppearance* app, m_app) { - bool c = app->saveSettings(app->defaultConfigFile()); + bool c = app->saveSettings(); correct = correct && c; } // qDebug() << "saving..." << correct; return correct; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 997eb39..c017424 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,9 @@ macro(add_kgc_test name) add_executable(${name} ${name}.cpp ${ARGV}) add_test(${name} ${name}) ecm_mark_as_test(${name}) - target_link_libraries(${name} Qt5::Core Qt5::Gui Qt5::Test ) + target_link_libraries(${name} Qt5::Core Qt5::Gui Qt5::Test KF5::ConfigCore) endmacro(add_kgc_test) add_kgc_test(fontstest ../src/fontshelpers.cpp) add_kgc_test(configsavetest ../src/abstractappearance.cpp ../src/appearancegtk2.cpp ../src/appearancegtk3.cpp) diff --git a/tests/configsavetest.cpp b/tests/configsavetest.cpp index d5d8460..1fe8f4f 100644 --- a/tests/configsavetest.cpp +++ b/tests/configsavetest.cpp @@ -1,68 +1,87 @@ /* * */ #include "configsavetest.h" #include #include #include QTEST_GUILESS_MAIN(ConfigSaveTest); -void ConfigSaveTest::fillValues(AbstractAppearance* a) +ConfigSaveTest::ConfigSaveTest() +{ + QStandardPaths::setTestModeEnabled(true); +} + +static void fillValues(QScopedPointer& a) { a->setFont("a"); a->setIcon("a"); a->setTheme("a"); a->setToolbarStyle("a"); a->setIconFallback("a"); + a->setCursor("a"); a->setShowIconsInButtons(true); a->setShowIconsInMenus(true); a->setPrimaryButtonWarpsSlider(true); - auto a3 = dynamic_cast(a); + auto a3 = dynamic_cast(a.data()); if (a3) { a3->setApplicationPreferDarkTheme(false); } } -bool compareAppearances(AbstractAppearance* a, AbstractAppearance* b) +void compareAppearances(QScopedPointer& reloaded, QScopedPointer& instance) { - return a->getFont() == b->getFont() - && a->getIcon() == b->getIcon() - && a->getTheme() == b->getTheme() - && a->getToolbarStyle() == b->getToolbarStyle() - && a->getIconFallback() == b->getIconFallback() - && a->getShowIconsInButtons() == b->getShowIconsInButtons() - && a->getShowIconsInMenus() == b->getShowIconsInMenus() - && a->getPrimaryButtonWarpsSlider() == b->getPrimaryButtonWarpsSlider(); + QCOMPARE(reloaded->getFont(), instance->getFont()); + QCOMPARE(reloaded->getIcon(), instance->getIcon()); + QCOMPARE(reloaded->getTheme(), instance->getTheme()); + QCOMPARE(reloaded->getCursor(), instance->getCursor()); + QCOMPARE(reloaded->getToolbarStyle(), instance->getToolbarStyle()); + QCOMPARE(reloaded->getIconFallback(), instance->getIconFallback()); + QCOMPARE(reloaded->getShowIconsInButtons(), instance->getShowIconsInButtons()); + QCOMPARE(reloaded->getShowIconsInMenus(), instance->getShowIconsInMenus()); + QCOMPARE(reloaded->getPrimaryButtonWarpsSlider(), instance->getPrimaryButtonWarpsSlider()); } QByteArray readFile(const QString& path) { QFile f(path); if(!f.open(QFile::ReadOnly | QFile::Text)) return QByteArray(); return f.readAll(); } -void ConfigSaveTest::testOpen() +void ConfigSaveTest::testGtk2() { - QVector instances; - instances << new AppearanceGTK2 << new AppearanceGTK3; - fillValues(instances[0]); - fillValues(instances[1]); - QVERIFY(instances[0]->saveSettings("test-gtk2")); - QVERIFY(instances[1]->saveSettings("test-gtk3")); + const QString pathA = QDir::current().absoluteFilePath("test-gtk2") + , pathB = QDir::current().absoluteFilePath("testB-gtk2"); + + QScopedPointer instance(new AppearanceGTK2); + fillValues(instance); + QVERIFY(instance->saveSettings(pathA)); + + QScopedPointer reloaded(new AppearanceGTK2); + QVERIFY(reloaded->loadSettings(pathA)); + compareAppearances(reloaded, instance); + QVERIFY(reloaded->saveSettings(pathB)); + QCOMPARE(readFile(pathA), readFile(pathB)); +} + +void ConfigSaveTest::testGtk3() +{ + QScopedPointer instance(new AppearanceGTK3); + fillValues(instance); + const QString pathA = QDir::current().absoluteFilePath("test-gtk3") + , pathB = QDir::current().absoluteFilePath("testB-gtk3"); + QVERIFY(instance->saveSettings(pathA)); + + QScopedPointer reloaded(new AppearanceGTK3); + QVERIFY(QFile::exists(pathA)); + QVERIFY(reloaded->loadSettings(pathA)); + compareAppearances(reloaded, instance); + QVERIFY(reloaded->saveSettings(pathB)); - QVector reloaded; - reloaded << new AppearanceGTK2 << new AppearanceGTK3; - QVERIFY(reloaded[0]->loadSettings("test-gtk2")); - QVERIFY(reloaded[1]->loadSettings("test-gtk3")); - QVERIFY(compareAppearances(reloaded[0], instances[0])); - QVERIFY(compareAppearances(reloaded[1], instances[1])); - QVERIFY(reloaded[0]->saveSettings("testB-gtk2")); - QVERIFY(reloaded[1]->saveSettings("testB-gtk3")); - QCOMPARE(readFile("test-gtk2"), readFile("testB-gtk2")); - QCOMPARE(readFile("test-gtk3"), readFile("testB-gtk3")); + QCOMPARE(readFile(pathA), readFile(pathB)); } diff --git a/tests/configsavetest.h b/tests/configsavetest.h index 39fb4c2..342b408 100644 --- a/tests/configsavetest.h +++ b/tests/configsavetest.h @@ -1,21 +1,21 @@ /* * */ #ifndef CONFIGSAVETEST_H #define CONFIGSAVETEST_H #include class AbstractAppearance; class ConfigSaveTest : public QObject { Q_OBJECT +public: + ConfigSaveTest(); private slots: - void testOpen(); - -private: - void fillValues(AbstractAppearance* a); + void testGtk2(); + void testGtk3(); }; #endif // CONFIGSAVETEST_H