2 Copyright (c) 2013 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "themeeditorpage.h"
19 #include "desktopfilepage.h"
20 #include "editorpage.h"
21 #include "themeeditorwidget.h"
22 #include "previewwidget.h"
23 #include "themesession.h"
24 #include "themeeditortabwidget.h"
26 #include <knewstuff3/uploaddialog.h>
30 #include <KInputDialog>
34 #include <KMessageBox>
35 #include <KFileDialog>
37 #include <QHBoxLayout>
42 ThemeEditorPage::ThemeEditorPage(const QString
&projectDir
, const QString
&themeName
, QWidget
*parent
)
44 mThemeSession(new GrantleeThemeEditor::ThemeSession(projectDir
, QLatin1String("headerthemeeditor"))),
47 QHBoxLayout
*lay
= new QHBoxLayout
;
48 mTabWidget
= new GrantleeThemeEditor::ThemeEditorTabWidget
;
49 connect(mTabWidget
, SIGNAL(currentChanged(QWidget
*)), this, SLOT(slotCurrentWidgetChanged(QWidget
*)));
50 lay
->addWidget(mTabWidget
);
51 mEditorPage
= new EditorPage(EditorPage::MainPage
, projectDir
);
52 connect(mEditorPage
, SIGNAL(needUpdateViewer()), this, SLOT(slotUpdateViewer()));
53 connect(mEditorPage
, SIGNAL(changed()), SLOT(slotChanged()));
54 mTabWidget
->addTab(mEditorPage
, i18n("Editor") + QLatin1String(" (header.html)"));
56 GrantleeThemeEditor::DesktopFilePage::DesktopFileOptions opt
;
57 opt
|=GrantleeThemeEditor::DesktopFilePage::ExtraDisplayVariables
;
58 opt
|= GrantleeThemeEditor::DesktopFilePage::SpecifyFileName
;
60 mDesktopPage
= new GrantleeThemeEditor::DesktopFilePage(QLatin1String("header.html"), opt
);
61 mDesktopPage
->setDefaultDesktopName(QLatin1String("header.desktop"));
62 mDesktopPage
->setThemeName(themeName
);
63 mTabWidget
->addTab(mDesktopPage
, i18n("Desktop File"));
65 connect(mDesktopPage
, SIGNAL(mainFileNameChanged(QString
)), mEditorPage
->preview(), SLOT(slotMainFileNameChanged(QString
)));
66 connect(mDesktopPage
, SIGNAL(mainFileNameChanged(QString
)), mTabWidget
, SLOT(slotMainFileNameChanged(QString
)));
67 connect(mDesktopPage
, SIGNAL(extraDisplayHeaderChanged(QStringList
)), this, SLOT(slotExtraHeaderDisplayChanged(QStringList
)));
68 connect(mDesktopPage
, SIGNAL(changed()), SLOT(slotChanged()));
69 connect(mTabWidget
, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
73 ThemeEditorPage::~ThemeEditorPage()
75 qDeleteAll(mExtraPage
);
80 void ThemeEditorPage::slotCurrentWidgetChanged(QWidget
*w
)
82 GrantleeThemeEditor::EditorPage
*page
= dynamic_cast<GrantleeThemeEditor::EditorPage
*>(w
);
83 Q_EMIT
canInsertFile(page
);
86 void ThemeEditorPage::updatePreview()
88 mEditorPage
->preview()->updateViewer();
91 void ThemeEditorPage::setPrinting(bool print
)
93 mEditorPage
->preview()->setPrinting(print
);
96 void ThemeEditorPage::slotExtraHeaderDisplayChanged(const QStringList
&extraHeaders
)
98 mEditorPage
->preview()->slotExtraHeaderDisplayChanged(extraHeaders
);
101 Q_FOREACH(QString var
, extraHeaders
) {
102 var
= QLatin1String("header.") + var
.remove(QLatin1Char('-'));
106 mEditorPage
->editor()->createCompleterList(result
);
107 Q_FOREACH (EditorPage
*page
, mExtraPage
) {
108 page
->editor()->createCompleterList(result
);
112 void ThemeEditorPage::slotChanged()
117 void ThemeEditorPage::setChanged(bool b
)
125 void ThemeEditorPage::slotUpdateViewer()
127 if (themeWasChanged()) {
130 mEditorPage
->preview()->updateViewer();
133 void ThemeEditorPage::slotCloseTab(int index
)
135 mTabWidget
->removeTab(index
);
139 void ThemeEditorPage::insertFile()
141 QWidget
*w
= mTabWidget
->currentWidget();
144 GrantleeThemeEditor::EditorPage
* page
= dynamic_cast<GrantleeThemeEditor::EditorPage
*>(w
);
146 const QString fileName
= KFileDialog::getOpenFileName(KUrl(), QLatin1String("*"), this);
147 if (!fileName
.isEmpty()) {
148 page
->insertFile(fileName
);
153 bool ThemeEditorPage::themeWasChanged() const
158 void ThemeEditorPage::installTheme(const QString
&themePath
)
161 QDir
themeDir(themePath
+ QDir::separator() + mDesktopPage
->themeName());
162 if (themeDir
.exists()) {
163 if (KMessageBox::questionYesNo(this, i18n("Theme already exists. Do you want to overwrite it?"), i18n("Theme already exists")) == KMessageBox::No
) {
167 if (!dir
.mkdir(mDesktopPage
->themeName())) {
168 KMessageBox::error(this, i18n("Cannot create theme folder."));
172 const QString newPath
= themePath
+ QDir::separator() + mDesktopPage
->themeName();
173 mEditorPage
->installTheme(newPath
);
175 Q_FOREACH (EditorPage
*page
, mExtraPage
) {
176 page
->installTheme(newPath
);
178 mDesktopPage
->installTheme(newPath
);
179 KMessageBox::information(this, i18n("Theme installed in \"%1\"", themeDir
.absolutePath()));
182 void ThemeEditorPage::uploadTheme()
184 //force update for screenshot
185 mEditorPage
->preview()->updateViewer();
187 const QString themename
= mDesktopPage
->themeName();
188 const QString zipFileName
= tmp
.name() + QDir::separator() + themename
+ QLatin1String(".zip");
189 KZip
*zip
= new KZip(zipFileName
);
190 if (zip
->open(QIODevice::WriteOnly
)) {
191 const QString previewFileName
= tmp
.name() + QDir::separator() + themename
+ QLatin1String("_preview.png");
192 //qDebug()<<" previewFileName"<<previewFileName;
194 lst
<< previewFileName
;
195 mEditorPage
->preview()->createScreenShot(lst
);
197 const bool fileAdded
= zip
->addLocalFile(previewFileName
, themename
+ QLatin1Char('/') + QLatin1String("theme_preview.png"));
199 KMessageBox::error(this, i18n("We cannot add preview file in zip file"), i18n("Failed to add file."));
204 createZip(themename
, zip
);
206 //qDebug()<< "zipFilename"<<zipFileName;
208 QPointer
<KNS3::UploadDialog
> dialog
= new KNS3::UploadDialog(QLatin1String("messageviewer_header_themes.knsrc"), this);
209 dialog
->setUploadFile(zipFileName
);
210 dialog
->setUploadName(themename
);
211 dialog
->setPreviewImageFile(0, KUrl(previewFileName
));
212 const QString description
= mDesktopPage
->description();
213 dialog
->setDescription(description
.isEmpty() ? i18n("My favorite KMail header") : description
);
217 kDebug()<<" We can't open in zip write mode";
222 void ThemeEditorPage::createZip(const QString
&themeName
, KZip
*zip
)
224 mEditorPage
->createZip(themeName
, zip
);
226 Q_FOREACH (EditorPage
*page
, mExtraPage
) {
227 page
->createZip(themeName
, zip
);
229 mDesktopPage
->createZip(themeName
, zip
);
232 void ThemeEditorPage::addExtraPage()
234 QString filename
= KInputDialog::getText(i18n("Filename of extra page"), i18n("Filename:"));
235 if (!filename
.isEmpty()) {
236 if (!filename
.endsWith(QLatin1String(".html"))) {
237 filename
+= QLatin1String(".html");
239 createExtraPage(filename
);
240 mThemeSession
->addExtraPage(filename
);
245 EditorPage
*ThemeEditorPage::createExtraPage(const QString
&filename
)
247 EditorPage
*extraPage
= new EditorPage(EditorPage::ExtraPage
, QString());
248 connect(extraPage
, SIGNAL(changed()), SLOT(slotChanged()));
249 extraPage
->setPageFileName(filename
);
250 mTabWidget
->addTab(extraPage
, filename
);
251 mTabWidget
->setCurrentWidget(extraPage
);
252 mExtraPage
.append(extraPage
);
256 void ThemeEditorPage::storeTheme(const QString
&directory
)
258 const QString themeDirectory
= directory
.isEmpty() ? projectDirectory() : directory
;
259 //set default page filename before saving
260 mEditorPage
->setPageFileName(mDesktopPage
->filename());
261 mEditorPage
->saveTheme(themeDirectory
);
263 Q_FOREACH (EditorPage
*page
, mExtraPage
) {
264 page
->saveTheme(themeDirectory
);
266 mDesktopPage
->saveTheme(themeDirectory
);
267 mThemeSession
->setMainPageFileName(mDesktopPage
->filename());
268 mThemeSession
->writeSession(directory
);
269 if (directory
.isEmpty())
273 bool ThemeEditorPage::saveTheme(bool withConfirmation
)
275 if (themeWasChanged()) {
276 if (withConfirmation
) {
277 const int result
= KMessageBox::questionYesNoCancel(this, i18n("Do you want to save current project?"), i18n("Save current project"));
278 if (result
== KMessageBox::Yes
) {
280 } else if (result
== KMessageBox::Cancel
) {
291 void ThemeEditorPage::loadTheme(const QString
&filename
)
293 if (mThemeSession
->loadSession(filename
)) {
294 mDesktopPage
->loadTheme(mThemeSession
->projectDirectory());
295 mEditorPage
->loadTheme(mThemeSession
->projectDirectory() + QDir::separator() + mThemeSession
->mainPageFileName());
296 mEditorPage
->preview()->setThemePath(mThemeSession
->projectDirectory(), mThemeSession
->mainPageFileName());
298 const QStringList lstExtraPages
= mThemeSession
->extraPages();
299 Q_FOREACH(const QString
&page
, lstExtraPages
) {
300 EditorPage
*extraPage
= createExtraPage(page
);
301 extraPage
->loadTheme(mThemeSession
->projectDirectory() + QDir::separator() + page
);
303 mTabWidget
->setCurrentIndex(0);
308 void ThemeEditorPage::reloadConfig()
310 mEditorPage
->preview()->loadConfig();
313 QString
ThemeEditorPage::projectDirectory() const
315 return mThemeSession
->projectDirectory();
318 void ThemeEditorPage::saveThemeAs(const QString
&directory
)
320 storeTheme(directory
);