french -> French
[kdepim.git] / headerthemeeditor / themeeditormainwindow.cpp
blob5ceb029bd676c41f3d18d4c6376ab843538b9a81
1 /*
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 "themeeditormainwindow.h"
19 #include "themeeditorpage.h"
20 #include "newthemedialog.h"
21 #include "themeconfiguredialog.h"
22 #include "managethemes.h"
24 #include <KTemporaryFile>
25 #include <KTempDir>
26 #include <KStandardAction>
27 #include <KApplication>
28 #include <KAction>
29 #include <KToggleAction>
30 #include <KActionCollection>
31 #include <KLocalizedString>
32 #include <KMessageBox>
33 #include <KFileDialog>
34 #include <KDebug>
35 #include <KStandardDirs>
36 #include <KNS3/KNewStuffAction>
37 #include <KRecentFilesAction>
39 #include <QPointer>
40 #include <QCloseEvent>
41 #include <QActionGroup>
43 ThemeEditorMainWindow::ThemeEditorMainWindow()
44 : KXmlGuiWindow(),
45 mThemeEditor(0)
47 setupActions();
48 setupGUI();
49 updateActions();
50 readConfig();
53 ThemeEditorMainWindow::~ThemeEditorMainWindow()
55 KSharedConfig::Ptr config = KGlobal::config();
57 KConfigGroup group = config->group( QLatin1String("ThemeEditorMainWindow") );
58 group.writeEntry( "Size", size() );
59 mRecentFileAction->saveEntries(group);
62 void ThemeEditorMainWindow::readConfig()
64 KSharedConfig::Ptr config = KGlobal::config();
65 KConfigGroup group = KConfigGroup( config, "ThemeEditorMainWindow" );
66 const QSize sizeDialog = group.readEntry( "Size", QSize(600,400) );
67 if ( sizeDialog.isValid() ) {
68 resize( sizeDialog );
72 void ThemeEditorMainWindow::updateActions()
74 const bool projectDirectoryIsEmpty = (mThemeEditor!=0);
75 mAddExtraPage->setEnabled(projectDirectoryIsEmpty);
76 mCloseAction->setEnabled(projectDirectoryIsEmpty);
77 mUploadTheme->setEnabled(projectDirectoryIsEmpty);
78 mSaveAction->setEnabled(projectDirectoryIsEmpty);
79 mInstallTheme->setEnabled(projectDirectoryIsEmpty);
80 mInsertFile->setEnabled(projectDirectoryIsEmpty);
81 mPrintingMode->setEnabled(projectDirectoryIsEmpty);
82 mNormalMode->setEnabled(projectDirectoryIsEmpty);
83 mUpdateView->setEnabled(projectDirectoryIsEmpty);
84 mSaveAsAction->setEnabled(projectDirectoryIsEmpty);
87 void ThemeEditorMainWindow::setupActions()
89 mRecentFileAction = new KRecentFilesAction(i18n("Load Recent Theme..."), this);
90 connect(mRecentFileAction, SIGNAL(urlSelected(KUrl)), this, SLOT(slotThemeSelected(KUrl)));
91 actionCollection()->addAction( QLatin1String( "load_recent_theme" ), mRecentFileAction );
92 KSharedConfig::Ptr config = KGlobal::config();
93 KConfigGroup groupConfig = config->group( QLatin1String("ThemeEditorMainWindow") );
94 mRecentFileAction->loadEntries(groupConfig);
96 mAddExtraPage = new KAction(i18n("Add Extra Page..."), this);
97 connect(mAddExtraPage, SIGNAL(triggered(bool)),SLOT(slotAddExtraPage()));
98 actionCollection()->addAction( QLatin1String( "add_extra_page" ), mAddExtraPage );
100 mUploadTheme = KNS3::standardAction(i18n("Upload theme..."), this, SLOT(slotUploadTheme()), actionCollection(), "upload_theme");
102 mNewThemeAction = KStandardAction::openNew(this, SLOT(slotNewTheme()), actionCollection());
103 mNewThemeAction->setText(i18n("New theme..."));
105 mOpenAction = KStandardAction::open(this, SLOT(slotOpenTheme()), actionCollection());
106 mOpenAction->setText(i18n("Open theme..."));
107 mSaveAction = KStandardAction::save(this, SLOT(slotSaveTheme()), actionCollection());
108 mSaveAction->setText(i18n("Save theme..."));
110 mSaveAsAction = KStandardAction::saveAs(this, SLOT(slotSaveAsTheme()), actionCollection());
111 mSaveAsAction->setText(i18n("Save theme as..."));
113 mCloseAction = KStandardAction::close( this, SLOT(slotCloseTheme()), actionCollection());
114 KStandardAction::quit(this, SLOT(slotQuitApp()), actionCollection() );
115 KStandardAction::preferences( this, SLOT(slotConfigure()), actionCollection() );
117 mInstallTheme = new KAction(i18n("Install theme"), this);
118 actionCollection()->addAction( QLatin1String( "install_theme" ), mInstallTheme );
119 connect(mInstallTheme, SIGNAL(triggered(bool)), SLOT(slotInstallTheme()));
121 mInsertFile = new KAction(i18n("Insert File..."), this);
122 actionCollection()->addAction( QLatin1String( "insert_file" ), mInsertFile );
123 connect(mInsertFile, SIGNAL(triggered(bool)), SLOT(slotInsertFile()));
125 QActionGroup *group = new QActionGroup( this );
127 mPrintingMode = new KToggleAction(i18n("Printing mode"), this);
128 actionCollection()->addAction(QLatin1String("printing_mode"), mPrintingMode );
129 connect(mPrintingMode, SIGNAL(triggered(bool)), SLOT(slotPrintingMode()));
130 group->addAction( mPrintingMode );
132 mNormalMode = new KToggleAction(i18n("Normal mode"), this);
133 mNormalMode->setChecked(true);
134 actionCollection()->addAction(QLatin1String("normal_mode"), mNormalMode );
135 connect(mNormalMode, SIGNAL(triggered(bool)), SLOT(slotNormalMode()));
136 group->addAction( mNormalMode );
138 mManageTheme = new KAction(i18n("Manage themes..."), this);
139 connect(mManageTheme, SIGNAL(triggered(bool)),SLOT(slotManageTheme()));
140 actionCollection()->addAction( QLatin1String( "manage_themes" ), mManageTheme );
142 mUpdateView = new KAction(i18n("Update view"), this);
143 mUpdateView->setShortcut(QKeySequence( Qt::Key_F5 ));
144 connect(mUpdateView, SIGNAL(triggered(bool)),SLOT(slotUpdateView()));
145 actionCollection()->addAction( QLatin1String( "update_view" ), mUpdateView );
148 void ThemeEditorMainWindow::slotManageTheme()
150 QPointer<GrantleeThemeEditor::ManageThemes> dialog = new GrantleeThemeEditor::ManageThemes(QLatin1String("messageviewer/themes/"), this);
151 dialog->exec();
152 delete dialog;
155 void ThemeEditorMainWindow::slotNormalMode()
157 mThemeEditor->setPrinting(false);
160 void ThemeEditorMainWindow::slotPrintingMode()
162 mThemeEditor->setPrinting(true);
165 void ThemeEditorMainWindow::slotInsertFile()
167 mThemeEditor->insertFile();
170 void ThemeEditorMainWindow::slotConfigure()
172 QPointer<ThemeConfigureDialog> dialog = new ThemeConfigureDialog(this);
173 if (dialog->exec()) {
174 if (mThemeEditor) {
175 mThemeEditor->reloadConfig();
178 delete dialog;
181 void ThemeEditorMainWindow::slotInstallTheme()
183 //Save before installing :)
184 if (slotSaveTheme()) {
185 const QString localThemePath = KStandardDirs::locateLocal("data",QLatin1String("messageviewer/themes/"));
186 mThemeEditor->installTheme(localThemePath);
190 void ThemeEditorMainWindow::slotUploadTheme()
192 //Save before upload :)
193 if (slotSaveTheme())
194 mThemeEditor->uploadTheme();
197 bool ThemeEditorMainWindow::slotSaveTheme()
199 bool result = false;
200 if (mThemeEditor)
201 result = mThemeEditor->saveTheme(false);
202 return result;
205 void ThemeEditorMainWindow::slotCloseTheme()
207 saveCurrentProject(false);
210 void ThemeEditorMainWindow::slotOpenTheme()
212 if (!saveCurrentProject(false))
213 return;
215 const QString directory = KFileDialog::getExistingDirectory(KUrl( "kfiledialog:///OpenTheme" ), this, i18n("Select theme directory"));
216 loadTheme(directory);
217 mRecentFileAction->addUrl(KUrl(directory));
220 void ThemeEditorMainWindow::loadTheme(const QString &directory)
222 if (!directory.isEmpty()) {
223 const QString filename = directory + QDir::separator() + QLatin1String("theme.themerc");
224 QFile file(filename);
225 if (!file.exists()) {
226 KMessageBox::error(this, i18n("Directory does not contain a theme file. We cannot load theme."));
227 return;
230 mThemeEditor = new ThemeEditorPage(QString(), QString());
231 connect(mThemeEditor, SIGNAL(changed(bool)), mSaveAction, SLOT(setEnabled(bool)));
232 connect(mThemeEditor, SIGNAL(canInsertFile(bool)), this, SLOT(slotCanInsertFile(bool)));
233 mThemeEditor->loadTheme(filename);
234 setCentralWidget(mThemeEditor);
235 updateActions();
239 void ThemeEditorMainWindow::slotAddExtraPage()
241 if (mThemeEditor)
242 mThemeEditor->addExtraPage();
245 bool ThemeEditorMainWindow::saveCurrentProject(bool createNewTheme)
247 if (mThemeEditor) {
248 if (!mThemeEditor->saveTheme())
249 return false;
251 if (createNewTheme) {
252 delete mThemeEditor;
253 mThemeEditor = 0;
254 QPointer<GrantleeThemeEditor::NewThemeDialog> dialog = new GrantleeThemeEditor::NewThemeDialog(this);
255 QString newTheme;
256 QString projectDirectory;
257 if (dialog->exec()) {
258 newTheme = dialog->themeName();
259 projectDirectory = dialog->directory();
261 if (!projectDirectory.isEmpty()) {
262 mRecentFileAction->addUrl(KUrl(projectDirectory));
263 mThemeEditor = new ThemeEditorPage(projectDirectory, newTheme);
264 connect(mThemeEditor, SIGNAL(changed(bool)), mSaveAction, SLOT(setEnabled(bool)));
265 connect(mThemeEditor, SIGNAL(canInsertFile(bool)), this, SLOT(slotCanInsertFile(bool)));
266 setCentralWidget(mThemeEditor);
267 } else {
268 setCentralWidget(0);
270 delete dialog;
271 updateActions();
272 } else {
273 delete mThemeEditor;
274 mThemeEditor = 0;
275 setCentralWidget(0);
276 updateActions();
278 return true;
281 void ThemeEditorMainWindow::slotNewTheme()
283 saveCurrentProject(true);
286 void ThemeEditorMainWindow::closeEvent(QCloseEvent *e)
288 if (!saveCurrentProject(false))
289 e->ignore();
290 else
291 e->accept();
294 void ThemeEditorMainWindow::slotQuitApp()
296 if (saveCurrentProject(false))
297 kapp->quit();
300 void ThemeEditorMainWindow::slotUpdateView()
302 if (mThemeEditor) {
303 mThemeEditor->saveTheme(false);
304 mThemeEditor->updatePreview();
308 void ThemeEditorMainWindow::slotCanInsertFile(bool b)
310 mInsertFile->setEnabled(b);
313 void ThemeEditorMainWindow::slotThemeSelected(const KUrl &url)
315 if (!saveCurrentProject(false))
316 return;
317 loadTheme(url.path());
320 void ThemeEditorMainWindow::slotSaveAsTheme()
322 const QString directory = KFileDialog::getExistingDirectory(KUrl( "kfiledialog:///SaveTheme" ), this, i18n("Select theme directory"));
323 if (!directory.isEmpty()) {
324 if (mThemeEditor)
325 mThemeEditor->saveThemeAs(directory);