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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "editorpage.h"
0007 #include "editorwidget.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 #include <KZip>
0012 #include <QTemporaryFile>
0013 
0014 #include <QFile>
0015 #include <QTextStream>
0016 
0017 using namespace GrantleeThemeEditor;
0018 
0019 EditorPage::EditorPage(PageType type, QWidget *parent)
0020     : QWidget(parent)
0021     , mType(type)
0022 {
0023 }
0024 
0025 EditorPage::~EditorPage() = default;
0026 
0027 EditorPage::PageType EditorPage::pageType() const
0028 {
0029     return mType;
0030 }
0031 
0032 void EditorPage::setPageFileName(const QString &filename)
0033 {
0034     mPageFileName = filename;
0035 }
0036 
0037 QString EditorPage::pageFileName() const
0038 {
0039     return mPageFileName;
0040 }
0041 
0042 GrantleeThemeEditor::EditorWidget *EditorPage::editor() const
0043 {
0044     return mEditor;
0045 }
0046 
0047 void EditorPage::insertFile(const QString &filename)
0048 {
0049     if (mEditor) {
0050         mEditor->insertFile(filename);
0051     }
0052 }
0053 
0054 void EditorPage::loadTheme(const QString &path)
0055 {
0056     if (!mEditor) {
0057         return;
0058     }
0059 
0060     mEditor->clear();
0061     QFile file(path);
0062     if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
0063         const QByteArray data = file.readAll();
0064         const QString str = QString::fromUtf8(data);
0065         file.close();
0066         mEditor->setPlainText(str);
0067     }
0068 }
0069 
0070 void EditorPage::saveTheme(const QString &path)
0071 {
0072     if (!mEditor) {
0073         return;
0074     }
0075 
0076     const QString filename = path + QLatin1Char('/') + mPageFileName;
0077     saveAsFilename(filename);
0078 }
0079 
0080 void EditorPage::saveAsFilename(const QString &filename)
0081 {
0082     QFile file(filename);
0083     if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0084         QTextStream out(&file);
0085         out << mEditor->toPlainText();
0086         file.close();
0087     } else {
0088         KMessageBox::error(this, i18n("Failed to open file \"%1\".", filename));
0089     }
0090 }
0091 
0092 void EditorPage::createZip(const QString &themeName, KZip *zip)
0093 {
0094     QTemporaryFile tmp;
0095     tmp.open();
0096     saveAsFilename(tmp.fileName());
0097     const bool fileAdded = zip->addLocalFile(tmp.fileName(), themeName + QLatin1Char('/') + mPageFileName);
0098     if (!fileAdded) {
0099         KMessageBox::error(this, i18n("Failed to add file into ZIP archive."), i18nc("@title:window", "Failed to add file"));
0100     }
0101 }
0102 
0103 void EditorPage::installTheme(const QString &themePath)
0104 {
0105     const QString filename = themePath + QLatin1Char('/') + mPageFileName;
0106     saveAsFilename(filename);
0107 }
0108 
0109 #include "moc_editorpage.cpp"