File indexing completed on 2024-05-12 05:13:31

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "themesession.h"
0007 #include "grantleethemeeditor_debug.h"
0008 #include <KConfig>
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 
0013 using namespace GrantleeThemeEditor;
0014 
0015 ThemeSession::ThemeSession(const QString &projectDirectory, const QString &themeTypeName)
0016     : mProjectDirectory(projectDirectory)
0017     , mThemeTypeName(themeTypeName)
0018 {
0019 }
0020 
0021 ThemeSession::~ThemeSession() = default;
0022 
0023 QString ThemeSession::projectDirectory() const
0024 {
0025     return mProjectDirectory;
0026 }
0027 
0028 void ThemeSession::addExtraPage(const QString &filename)
0029 {
0030     mExtraPage.append(filename);
0031 }
0032 
0033 QStringList ThemeSession::extraPages() const
0034 {
0035     return mExtraPage;
0036 }
0037 
0038 void ThemeSession::setMainPageFileName(const QString &filename)
0039 {
0040     mMainPageFileName = filename;
0041 }
0042 
0043 QString ThemeSession::mainPageFileName() const
0044 {
0045     return mMainPageFileName;
0046 }
0047 
0048 bool ThemeSession::loadSession(const QString &session)
0049 {
0050     KConfig config(session);
0051     if (config.hasGroup(QStringLiteral("Global"))) {
0052         KConfigGroup global = config.group(QStringLiteral("Global"));
0053         const int version = global.readEntry(QStringLiteral("version"), 0);
0054         if (version >= mVersion) {
0055             if (global.readEntry(QStringLiteral("themeTypeName")) != mThemeTypeName) {
0056                 KMessageBox::error(nullptr,
0057                                    i18n("Error during theme loading"),
0058                                    i18n("You are trying to load a theme which cannot be read by this application"));
0059                 return false;
0060             }
0061         }
0062         mProjectDirectory = global.readEntry("path", QString());
0063         mMainPageFileName = global.readEntry(QStringLiteral("mainPageName"), QString());
0064         mExtraPage = global.readEntry(QStringLiteral("extraPagesName"), QStringList());
0065         return true;
0066     } else {
0067         qCDebug(GRANTLEETHEMEEDITOR_LOG) << QStringLiteral("\"%1\" is not a session file").arg(session);
0068         return false;
0069     }
0070 }
0071 
0072 void ThemeSession::writeSession(const QString &directory)
0073 {
0074     QString themeDirectory = (directory.isEmpty() ? mProjectDirectory : directory);
0075     KConfig config(themeDirectory + QLatin1StringView("/theme.themerc"));
0076     KConfigGroup global = config.group(QStringLiteral("Global"));
0077     global.writeEntry(QStringLiteral("path"), themeDirectory);
0078     global.writeEntry(QStringLiteral("mainPageName"), mMainPageFileName);
0079     global.writeEntry(QStringLiteral("extraPagesName"), mExtraPage);
0080     global.writeEntry(QStringLiteral("themeTypeName"), mThemeTypeName);
0081     global.writeEntry(QStringLiteral("version"), mVersion);
0082     config.sync();
0083 }