Fix documentation path
[kdepim.git] / grantleeeditor / contactprintthemeeditor / themeeditorpage.cpp
blob6eed7db791f394e34f29d5b0fb4ac214b30d4284
1 /*
2 Copyright (C) 2015-2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "themeeditorpage.h"
21 #include "desktopfilepage.h"
22 #include "editorpage.h"
23 #include "themeeditorwidget.h"
24 #include "themesession.h"
25 #include "themeeditortabwidget.h"
26 #include "contactprintthemepreview.h"
28 #include <kns3/uploaddialog.h>
30 #include <KLocalizedString>
31 #include <QInputDialog>
32 #include <KZip>
33 #include <QTemporaryDir>
34 #include "contactprintthemeeditor_debug.h"
35 #include <KMessageBox>
36 #include <QUrl>
38 #include <QHBoxLayout>
39 #include <QDir>
40 #include <QPointer>
41 #include <QFileDialog>
43 ThemeEditorPage::ThemeEditorPage(const QString &projectDir, const QString &themeName, QWidget *parent)
44 : QWidget(parent),
45 mThemeSession(new GrantleeThemeEditor::ThemeSession(projectDir, QStringLiteral("headerthemeeditor"))),
46 mChanged(false)
48 QHBoxLayout *lay = new QHBoxLayout;
49 mTabWidget = new GrantleeThemeEditor::ThemeEditorTabWidget;
50 connect(mTabWidget, &GrantleeThemeEditor::ThemeEditorTabWidget::currentChanged, this, &ThemeEditorPage::slotCurrentWidgetChanged);
51 lay->addWidget(mTabWidget);
52 mEditorPage = new EditorPage(EditorPage::MainPage, projectDir);
53 mEditorPage->setPageFileName(QStringLiteral("theme.html"));
54 connect(mEditorPage, &EditorPage::needUpdateViewer, this, &ThemeEditorPage::slotUpdateViewer);
55 connect(mEditorPage, &EditorPage::changed, this, &ThemeEditorPage::slotChanged);
56 mTabWidget->addTab(mEditorPage, i18n("Editor (%1)", QStringLiteral("theme.html")));
58 GrantleeThemeEditor::DesktopFilePage::DesktopFileOptions opt;
59 mDesktopPage = new GrantleeThemeEditor::DesktopFilePage(QStringLiteral("theme.html"), opt);
60 mDesktopPage->setDefaultDesktopName(QStringLiteral("theme.desktop"));
61 mDesktopPage->setThemeName(themeName);
62 mTabWidget->addTab(mDesktopPage, i18n("Desktop File"));
64 connect(mDesktopPage, &GrantleeThemeEditor::DesktopFilePage::mainFileNameChanged, mEditorPage->preview(), &GrantleeThemeEditor::PreviewWidget::slotMainFileNameChanged);
65 connect(mDesktopPage, &GrantleeThemeEditor::DesktopFilePage::mainFileNameChanged, mTabWidget, &GrantleeThemeEditor::ThemeEditorTabWidget::slotMainFileNameChanged);
66 connect(mDesktopPage, &GrantleeThemeEditor::DesktopFilePage::changed, this, &ThemeEditorPage::slotChanged);
67 connect(mTabWidget, &GrantleeThemeEditor::ThemeEditorTabWidget::tabCloseRequested, this, &ThemeEditorPage::slotCloseTab);
68 setLayout(lay);
71 ThemeEditorPage::~ThemeEditorPage()
73 qDeleteAll(mExtraPage);
74 mExtraPage.clear();
75 delete mThemeSession;
78 void ThemeEditorPage::slotCurrentWidgetChanged(int index)
80 if (index < 0) {
81 return;
83 GrantleeThemeEditor::EditorPage *page = dynamic_cast<GrantleeThemeEditor::EditorPage *>(mTabWidget->widget(index));
84 Q_EMIT canInsertFile(page);
87 void ThemeEditorPage::updatePreview()
89 mEditorPage->preview()->updateViewer();
92 void ThemeEditorPage::slotChanged()
94 setChanged(true);
97 void ThemeEditorPage::setChanged(bool b)
99 if (mChanged != b) {
100 mChanged = b;
101 Q_EMIT changed(b);
105 void ThemeEditorPage::slotUpdateViewer()
107 if (themeWasChanged()) {
108 saveTheme(false);
110 mEditorPage->preview()->updateViewer();
113 void ThemeEditorPage::slotCloseTab(int index)
115 mTabWidget->removeTab(index);
116 setChanged(true);
119 void ThemeEditorPage::insertFile()
121 QWidget *w = mTabWidget->currentWidget();
122 if (!w) {
123 return;
125 GrantleeThemeEditor::EditorPage *page = dynamic_cast<GrantleeThemeEditor::EditorPage *>(w);
126 if (page) {
127 const QString fileName = QFileDialog::getOpenFileName(this);
128 if (!fileName.isEmpty()) {
129 page->insertFile(fileName);
134 bool ThemeEditorPage::themeWasChanged() const
136 return mChanged;
139 void ThemeEditorPage::installTheme(const QString &themePath)
141 QDir dir(themePath);
142 QDir themeDir(themePath + QDir::separator() + mDesktopPage->themeName());
143 if (themeDir.exists()) {
144 if (KMessageBox::questionYesNo(this, i18n("Theme already exists. Do you want to overwrite it?"), i18n("Theme already exists")) == KMessageBox::No) {
145 return;
147 } else {
148 if (!dir.mkdir(mDesktopPage->themeName())) {
149 KMessageBox::error(this, i18n("Cannot create theme folder."));
150 return;
153 const QString newPath = themePath + QDir::separator() + mDesktopPage->themeName();
154 mEditorPage->installTheme(newPath);
155 Q_FOREACH (EditorPage *page, mExtraPage) {
156 page->installTheme(newPath);
158 mDesktopPage->installTheme(newPath);
159 KMessageBox::information(this, i18n("Theme installed in \"%1\"", themeDir.absolutePath()));
162 void ThemeEditorPage::uploadTheme()
164 //force update for screenshot
165 mEditorPage->preview()->updateViewer();
166 QTemporaryDir tmp;
167 const QString themename = mDesktopPage->themeName();
168 const QString zipFileName = tmp.path() + QDir::separator() + themename + QLatin1String(".zip");
169 KZip *zip = new KZip(zipFileName);
170 if (zip->open(QIODevice::WriteOnly)) {
171 const QString previewFileName = tmp.path() + QDir::separator() + themename + QLatin1String("_preview.png");
172 //qCDebug(CONTACTPRINTTHEMEEDITOR_LOG)<<" previewFileName"<<previewFileName;
173 QStringList lst;
174 lst << previewFileName;
175 mEditorPage->preview()->createScreenShot(lst);
177 const bool fileAdded = zip->addLocalFile(previewFileName, themename + QLatin1Char('/') + QLatin1String("theme_preview.png"));
178 if (!fileAdded) {
179 KMessageBox::error(this, i18n("We cannot add preview file in zip file"), i18n("Failed to add file."));
180 delete zip;
181 return;
184 createZip(themename, zip);
185 zip->close();
186 //qCDebug(CONTACTPRINTTHEMEEDITOR_LOG)<< "zipFilename"<<zipFileName;
188 QPointer<KNS3::UploadDialog> dialog = new KNS3::UploadDialog(QStringLiteral("messageviewer_header_themes.knsrc"), this);
189 dialog->setUploadFile(QUrl::fromLocalFile(zipFileName));
190 dialog->setUploadName(themename);
191 dialog->setPreviewImageFile(0, QUrl::fromLocalFile(previewFileName));
192 const QString description = mDesktopPage->description();
193 dialog->setDescription(description.isEmpty() ? i18n("My favorite KMail header") : description);
194 dialog->exec();
195 delete dialog;
196 } else {
197 qCDebug(CONTACTPRINTTHEMEEDITOR_LOG) << " We can't open in zip write mode";
199 delete zip;
202 void ThemeEditorPage::createZip(const QString &themeName, KZip *zip)
204 mEditorPage->createZip(themeName, zip);
206 Q_FOREACH (EditorPage *page, mExtraPage) {
207 page->createZip(themeName, zip);
209 mDesktopPage->createZip(themeName, zip);
212 void ThemeEditorPage::addExtraPage()
214 QString filename = QInputDialog::getText(this, i18n("Filename of extra page"), i18n("Filename:"));
215 if (!filename.trimmed().isEmpty()) {
216 if (!filename.endsWith(QStringLiteral(".html")) && !filename.endsWith(QStringLiteral(".css")) && !filename.endsWith(QStringLiteral(".js"))) {
217 filename += QLatin1String(".html");
219 createExtraPage(filename);
220 mThemeSession->addExtraPage(filename);
221 setChanged(true);
225 EditorPage *ThemeEditorPage::createExtraPage(const QString &filename)
227 EditorPage *extraPage = new EditorPage(EditorPage::ExtraPage, QString());
228 connect(extraPage, &EditorPage::changed, this, &ThemeEditorPage::slotChanged);
229 extraPage->setPageFileName(filename);
230 mTabWidget->addTab(extraPage, filename);
231 mTabWidget->setCurrentWidget(extraPage);
232 mExtraPage.append(extraPage);
233 return extraPage;
236 void ThemeEditorPage::storeTheme(const QString &directory)
238 const QString themeDirectory = directory.isEmpty() ? projectDirectory() : directory;
239 mEditorPage->saveTheme(themeDirectory);
241 Q_FOREACH (EditorPage *page, mExtraPage) {
242 page->saveTheme(themeDirectory);
244 mDesktopPage->saveTheme(themeDirectory);
245 mThemeSession->setMainPageFileName(mDesktopPage->filename());
246 mThemeSession->writeSession(directory);
247 if (directory.isEmpty()) {
248 setChanged(false);
252 bool ThemeEditorPage::saveTheme(bool withConfirmation)
254 if (themeWasChanged()) {
255 if (withConfirmation) {
256 const int result = KMessageBox::questionYesNoCancel(this, i18n("Do you want to save current project?"), i18n("Save current project"));
257 if (result == KMessageBox::Yes) {
258 storeTheme();
259 } else if (result == KMessageBox::Cancel) {
260 return false;
262 } else {
263 storeTheme();
266 setChanged(false);
267 return true;
270 void ThemeEditorPage::loadTheme(const QString &filename)
272 if (mThemeSession->loadSession(filename)) {
273 mDesktopPage->loadTheme(mThemeSession->projectDirectory());
274 mEditorPage->loadTheme(mThemeSession->projectDirectory() + QDir::separator() + mThemeSession->mainPageFileName());
275 mEditorPage->preview()->setThemePath(mThemeSession->projectDirectory(), mThemeSession->mainPageFileName());
277 const QStringList lstExtraPages = mThemeSession->extraPages();
278 Q_FOREACH (const QString &page, lstExtraPages) {
279 EditorPage *extraPage = createExtraPage(page);
280 extraPage->loadTheme(mThemeSession->projectDirectory() + QDir::separator() + page);
282 mTabWidget->setCurrentIndex(0);
283 setChanged(false);
287 void ThemeEditorPage::reloadConfig()
289 mEditorPage->preview()->loadConfig();
292 QString ThemeEditorPage::projectDirectory() const
294 return mThemeSession->projectDirectory();
297 void ThemeEditorPage::saveThemeAs(const QString &directory)
299 storeTheme(directory);