Fix Bug 362152 - Font selection is not honored
[kdepim.git] / sieveeditor / sieveeditormainwindow.cpp
blobd96b63886ec8f206dd5f71195684ca6a8f6b4cc1
1 /*
2 Copyright (c) 2013-2016 Montel Laurent <montel@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
21 #include "sieveeditormainwindow.h"
22 #include "sieveeditormainwidget.h"
23 #include "sieveeditorconfiguredialog.h"
24 #include "serversievesettingsdialog.h"
25 #include "sieveserversettings.h"
26 #include "sieveeditorcentralwidget.h"
27 #include "sieveeditorglobalconfig.h"
28 #include "sieveeditorbookmarks.h"
29 #include "PimCommon/KActionMenuChangeCase"
31 #include <KStandardGuiItem>
32 #include <KSharedConfig>
33 #include <KIconEngine>
34 #include <KIconLoader>
36 #include <KLocalizedString>
37 #include <KConfigGroup>
38 #include <KStandardAction>
39 #include <KActionCollection>
40 #include <QAction>
41 #include <QStatusBar>
42 #include <QIcon>
44 #include <QPointer>
45 #include <QLabel>
46 #include <QCloseEvent>
47 #include <QNetworkConfigurationManager>
49 SieveEditorMainWindow::SieveEditorMainWindow()
50 : KXmlGuiWindow()
52 mMainWidget = new SieveEditorCentralWidget(this);
53 connect(mMainWidget, &SieveEditorCentralWidget::configureClicked, this, &SieveEditorMainWindow::slotConfigure);
54 connect(mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::updateButtons, this, &SieveEditorMainWindow::slotUpdateButtons);
55 setCentralWidget(mMainWidget);
56 setupActions();
57 setupGUI();
58 readConfig();
59 initStatusBar();
60 mNetworkConfigurationManager = new QNetworkConfigurationManager();
61 connect(mNetworkConfigurationManager, &QNetworkConfigurationManager::onlineStateChanged, this, &SieveEditorMainWindow::slotSystemNetworkOnlineStateChanged);
63 connect(mMainWidget->sieveEditorMainWidget()->tabWidget(), &QTabWidget::currentChanged, this, &SieveEditorMainWindow::slotUpdateActions);
64 connect(mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::modeEditorChanged, this, &SieveEditorMainWindow::slotUpdateActions);
65 slotSystemNetworkOnlineStateChanged(mNetworkConfigurationManager->isOnline());
66 connect(mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::undoAvailable, this, &SieveEditorMainWindow::slotUndoAvailable);
67 connect(mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::redoAvailable, this, &SieveEditorMainWindow::slotRedoAvailable);
68 connect(mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::copyAvailable, this, &SieveEditorMainWindow::slotCopyAvailable);
69 mMainWidget->sieveEditorMainWidget()->refreshList();
72 SieveEditorMainWindow::~SieveEditorMainWindow()
74 KSharedConfig::Ptr config = KSharedConfig::openConfig();
75 KConfigGroup group = config->group(QStringLiteral("SieveEditorMainWindow"));
76 group.writeEntry("Size", size());
77 if (SieveEditorGlobalConfig::self()->closeWallet()) {
78 SieveServerSettings::self()->closeWallet();
82 void SieveEditorMainWindow::initStatusBar()
84 mStatusBarInfo = new QLabel;
85 statusBar()->insertWidget(0, mStatusBarInfo, 4);
88 void SieveEditorMainWindow::slotSystemNetworkOnlineStateChanged(bool state)
90 if (state) {
91 mStatusBarInfo->setText(i18n("Network is Up."));
92 } else {
93 mStatusBarInfo->setText(i18n("Network is Down."));
95 mMainWidget->sieveEditorMainWidget()->setEnabled(state);
96 slotUpdateActions();
99 void SieveEditorMainWindow::slotUpdateButtons(bool newScriptAction, bool editScriptAction, bool deleteScriptAction, bool desactivateScriptAction)
101 mDeleteScript->setEnabled(deleteScriptAction);
102 mNewScript->setEnabled(newScriptAction);
103 mEditScript->setEnabled(editScriptAction);
104 mDesactivateScript->setEnabled(desactivateScriptAction);
107 void SieveEditorMainWindow::readConfig()
109 KSharedConfig::Ptr config = KSharedConfig::openConfig();
110 KConfigGroup group = KConfigGroup(config, "SieveEditorMainWindow");
111 const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
112 if (sizeDialog.isValid()) {
113 resize(sizeDialog);
117 void SieveEditorMainWindow::setupActions()
119 KActionCollection *ac = actionCollection();
121 KStandardAction::quit(this, SLOT(close()), ac);
122 KStandardAction::preferences(this, SLOT(slotConfigure()), ac);
124 mUploadScript = KStandardAction::save(this, SLOT(slotUploadScript()), ac);
125 mUploadScript->setText(i18n("Upload"));
126 mUploadScript->setEnabled(false);
128 QAction *act = ac->addAction(QStringLiteral("add_server_sieve"), this, SLOT(slotAddServerSieve()));
129 act->setText(i18n("Add Sieve Server..."));
131 mDeleteScript = ac->addAction(QStringLiteral("delete_script"), this, SLOT(slotDeleteScript()));
132 mDeleteScript->setText(i18n("Delete Script"));
133 ac->setDefaultShortcut(mDeleteScript, QKeySequence(Qt::Key_Delete));
134 mDeleteScript->setEnabled(false);
136 mNewScript = ac->addAction(QStringLiteral("create_new_script"), this, SLOT(slotCreateNewScript()));
137 ac->setDefaultShortcut(mNewScript, QKeySequence(Qt::CTRL + Qt::Key_N));
138 mNewScript->setText(i18n("Create New Script..."));
139 mNewScript->setEnabled(false);
141 mEditScript = ac->addAction(QStringLiteral("edit_script"), this, SLOT(slotEditScript()));
142 mEditScript->setText(i18n("Edit Script..."));
143 mEditScript->setEnabled(false);
145 mDesactivateScript = ac->addAction(QStringLiteral("desactivate_script"), this, SLOT(slotDesactivateScript()));
146 mDesactivateScript->setText(i18n("Deactivate Script"));
147 mDesactivateScript->setEnabled(false);
149 mRefreshList = ac->addAction(QStringLiteral("refresh_list"), this, SLOT(slotRefreshList()));
150 mRefreshList->setText(i18n("Refresh List"));
151 mRefreshList->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
152 ac->setDefaultShortcut(mRefreshList, QKeySequence(Qt::Key_F5));
154 mGoToLine = ac->addAction(QStringLiteral("gotoline"), mMainWidget->sieveEditorMainWidget(), SLOT(slotGoToLine()));
155 mGoToLine->setText(i18n("Go to Line..."));
156 mGoToLine->setIcon(QIcon::fromTheme(QStringLiteral("go-jump")));
157 ac->setDefaultShortcut(mGoToLine, QKeySequence(Qt::CTRL + Qt::Key_G));
158 mGoToLine->setEnabled(false);
160 mFindAction = KStandardAction::find(mMainWidget->sieveEditorMainWidget(), SLOT(slotFind()), ac);
161 mReplaceAction = KStandardAction::replace(mMainWidget->sieveEditorMainWidget(), SLOT(slotReplace()), ac);
162 mUndoAction = KStandardAction::undo(mMainWidget->sieveEditorMainWidget(), SLOT(slotUndo()), ac);
163 mRedoAction = KStandardAction::redo(mMainWidget->sieveEditorMainWidget(), SLOT(slotRedo()), ac);
164 mCopyAction = KStandardAction::copy(mMainWidget->sieveEditorMainWidget(), SLOT(slotCopy()), ac);
165 mPasteAction = KStandardAction::paste(mMainWidget->sieveEditorMainWidget(), SLOT(slotPaste()), ac);
166 mCutAction = KStandardAction::cut(mMainWidget->sieveEditorMainWidget(), SLOT(slotCut()), ac);
167 mSelectAllAction = KStandardAction::selectAll(mMainWidget->sieveEditorMainWidget(), SLOT(slotSelectAll()), ac);
168 mSaveAsAction = KStandardAction::saveAs(mMainWidget->sieveEditorMainWidget(), SLOT(slotSaveAs()), ac);
170 mImportAction = ac->addAction(QStringLiteral("import_script"), mMainWidget->sieveEditorMainWidget(), SLOT(slotImport()));
171 mImportAction->setText(i18n("Import..."));
172 mImportAction->setEnabled(false);
174 mShareAction = ac->addAction(QStringLiteral("share_script"), mMainWidget->sieveEditorMainWidget(), SLOT(slotShare()));
175 mShareAction->setText(i18n("Share..."));
176 const QStringList overlays = QStringList() << QStringLiteral("list-add");
177 mShareAction->setIcon(QIcon(new KIconEngine(QStringLiteral("get-hot-new-stuff"), KIconLoader::global(), overlays)));
178 mShareAction->setEnabled(false);
180 mSpellCheckAction = ac->addAction(QStringLiteral("check_spelling"), mMainWidget->sieveEditorMainWidget(), SLOT(slotCheckSpelling()));
181 mSpellCheckAction->setIcon(QIcon::fromTheme(QStringLiteral("tools-check-spelling")));
182 mSpellCheckAction->setText(i18n("Check Spelling..."));
183 mSpellCheckAction->setEnabled(false);
185 mCheckSyntaxAction = ac->addAction(QStringLiteral("check_syntax"), mMainWidget->sieveEditorMainWidget(), SLOT(slotCheckSyntax()));
186 mCheckSyntaxAction->setText(i18n("Check Syntax"));
187 mCheckSyntaxAction->setEnabled(false);
189 mAutoGenerateScriptAction = ac->addAction(QStringLiteral("autogenerate_script"), mMainWidget->sieveEditorMainWidget(), SLOT(slotAutoGenerateScript()));
190 mAutoGenerateScriptAction->setText(i18n("Autogenerate Script..."));
191 mAutoGenerateScriptAction->setEnabled(false);
193 mCommentAction = ac->addAction(QStringLiteral("comment_code"), mMainWidget->sieveEditorMainWidget(), SLOT(slotComment()));
194 ac->setDefaultShortcut(mCommentAction, Qt::CTRL + Qt::Key_D);
195 mCommentAction->setText(i18n("Comment"));
197 mUncommentAction = ac->addAction(QStringLiteral("uncomment_code"), mMainWidget->sieveEditorMainWidget(), SLOT(slotUncomment()));
198 ac->setDefaultShortcut(mUncommentAction, Qt::CTRL + Qt::SHIFT + Qt::Key_D);
199 mUncommentAction->setText(i18n("Uncomment"));
201 mZoomInAction = new QAction(QIcon::fromTheme(QStringLiteral("zoom-in")), i18n("&Zoom In"), this);
202 ac->addAction(QStringLiteral("zoom_in"), mZoomInAction);
203 connect(mZoomInAction, &QAction::triggered, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotZoomIn);
204 ac->setDefaultShortcut(mZoomInAction, QKeySequence(Qt::CTRL + Qt::Key_Plus));
206 mZoomOutAction = new QAction(QIcon::fromTheme(QStringLiteral("zoom-out")), i18n("Zoom &Out"), this);
207 ac->addAction(QStringLiteral("zoom_out"), mZoomOutAction);
208 connect(mZoomOutAction, &QAction::triggered, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotZoomOut);
209 ac->setDefaultShortcut(mZoomOutAction, QKeySequence(Qt::CTRL + Qt::Key_Minus));
211 mZoomResetAction = ac->addAction(QStringLiteral("zoom_reset"), mMainWidget->sieveEditorMainWidget(), SLOT(slotZoomReset()));
212 ac->setDefaultShortcut(mZoomResetAction, QKeySequence(Qt::CTRL + Qt::Key_0));
213 mZoomResetAction->setText(i18nc("Reset the zoom", "Reset"));
215 mMenuChangeCaseAction = new PimCommon::KActionMenuChangeCase(this);
216 ac->addAction(QStringLiteral("change_case_menu"), mMenuChangeCaseAction);
217 mMenuChangeCaseAction->appendInActionCollection(ac);
218 connect(mMenuChangeCaseAction, &PimCommon::KActionMenuChangeCase::upperCase, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotUpperCase);
219 connect(mMenuChangeCaseAction, &PimCommon::KActionMenuChangeCase::lowerCase, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotLowerCase);
220 connect(mMenuChangeCaseAction, &PimCommon::KActionMenuChangeCase::sentenceCase, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotSentenceCase);
221 connect(mMenuChangeCaseAction, &PimCommon::KActionMenuChangeCase::reverseCase, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotReverseCase);
223 mBookmarkMenu = new KActionMenu(i18nc("@title:menu", "&Bookmarks"), ac);
224 mSieveEditorBookmarks = new SieveEditorBookmarks(this, ac, mBookmarkMenu->menu(), this);
225 ac->addAction(QStringLiteral("bookmark"), mBookmarkMenu);
226 connect(mSieveEditorBookmarks, &SieveEditorBookmarks::openUrl, this, &SieveEditorMainWindow::slotOpenBookmarkUrl);
228 mDebugSieveScriptAction = ac->addAction(QStringLiteral("debug_sieve"), mMainWidget->sieveEditorMainWidget(), SLOT(slotDebugSieveScript()));
229 mDebugSieveScriptAction->setText(i18n("Debug Sieve Script..."));
230 ac->setDefaultShortcut(mDebugSieveScriptAction, QKeySequence(Qt::SHIFT + Qt::ALT + Qt::Key_D));
232 mWrapTextAction = new QAction(i18n("Wordwarp"), this);
233 mWrapTextAction->setCheckable(true);
234 ac->addAction(QStringLiteral("wordwrap"), mWrapTextAction);
235 connect(mWrapTextAction, &QAction::triggered, mMainWidget->sieveEditorMainWidget(), &SieveEditorMainWidget::slotWordWrap);
238 void SieveEditorMainWindow::slotRefreshList()
240 if (mNetworkConfigurationManager->isOnline()) {
241 mMainWidget->sieveEditorMainWidget()->refreshList();
245 void SieveEditorMainWindow::slotUploadScript()
247 mMainWidget->sieveEditorMainWidget()->uploadScript();
250 void SieveEditorMainWindow::slotDesactivateScript()
252 mMainWidget->sieveEditorMainWidget()->desactivateScript();
255 void SieveEditorMainWindow::slotEditScript()
257 mMainWidget->sieveEditorMainWidget()->editScript();
260 void SieveEditorMainWindow::slotCreateNewScript()
262 mMainWidget->sieveEditorMainWidget()->createNewScript();
265 void SieveEditorMainWindow::slotDeleteScript()
267 mMainWidget->sieveEditorMainWidget()->deleteScript();
270 void SieveEditorMainWindow::closeEvent(QCloseEvent *e)
272 if (mMainWidget->sieveEditorMainWidget()->needToSaveScript()) {
273 e->ignore();
274 return;
275 } else {
276 e->accept();
280 void SieveEditorMainWindow::slotConfigure()
282 QPointer<SieveEditorConfigureDialog> dlg = new SieveEditorConfigureDialog(this);
283 if (dlg->exec()) {
284 dlg->saveServerSieveConfig();
285 mMainWidget->sieveEditorMainWidget()->updateServerList();
287 delete dlg;
290 void SieveEditorMainWindow::slotAddServerSieve()
292 QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this);
293 if (dlg->exec()) {
294 const SieveEditorUtil::SieveServerConfig conf = dlg->serverSieveConfig();
295 SieveEditorUtil::addServerSieveConfig(conf);
296 mMainWidget->sieveEditorMainWidget()->updateServerList();
298 delete dlg;
301 void SieveEditorMainWindow::slotUpdateActions()
303 const bool hasPage = (mMainWidget->sieveEditorMainWidget()->tabWidget()->count() > 0);
304 mUploadScript->setEnabled(hasPage);
305 const bool editActionEnabled = (hasPage && mMainWidget->sieveEditorMainWidget()->pageMode() == KSieveUi::SieveEditorWidget::TextMode);
306 mGoToLine->setEnabled(editActionEnabled);
307 mFindAction->setEnabled(editActionEnabled);
308 mReplaceAction->setEnabled(editActionEnabled);
309 mUndoAction->setEnabled(editActionEnabled && mMainWidget->sieveEditorMainWidget()->isUndoAvailable());
310 mRedoAction->setEnabled(editActionEnabled && mMainWidget->sieveEditorMainWidget()->isRedoAvailable());
312 mCopyAction->setEnabled(editActionEnabled && mMainWidget->sieveEditorMainWidget()->hasSelection());
313 mPasteAction->setEnabled(editActionEnabled);
314 mCutAction->setEnabled(editActionEnabled && mMainWidget->sieveEditorMainWidget()->hasSelection());
316 mSelectAllAction->setEnabled(editActionEnabled);
318 mUploadScript->setEnabled(hasPage && !mNetworkIsDown);
319 mRefreshList->setEnabled(!mNetworkIsDown);
320 mSaveAsAction->setEnabled(hasPage);
321 mImportAction->setEnabled(hasPage);
322 mShareAction->setEnabled(hasPage && !mNetworkIsDown);
323 mSpellCheckAction->setEnabled(editActionEnabled);
324 mCheckSyntaxAction->setEnabled(editActionEnabled && !mNetworkIsDown);
325 mAutoGenerateScriptAction->setEnabled(editActionEnabled);
326 mCommentAction->setEnabled(editActionEnabled);
327 mUncommentAction->setEnabled(editActionEnabled);
328 mMenuChangeCaseAction->setEnabled(editActionEnabled);
329 mZoomInAction->setEnabled(editActionEnabled);
330 mZoomOutAction->setEnabled(editActionEnabled);
331 mZoomResetAction->setEnabled(editActionEnabled);
333 mBookmarkMenu->setEnabled(editActionEnabled);
334 mDebugSieveScriptAction->setEnabled(editActionEnabled);
335 mWrapTextAction->setEnabled(editActionEnabled);
336 mWrapTextAction->setChecked(mMainWidget->sieveEditorMainWidget()->isWordWrap());
339 void SieveEditorMainWindow::slotUndoAvailable(bool b)
341 const bool hasPage = (mMainWidget->sieveEditorMainWidget()->tabWidget()->count() > 0);
342 const bool editActionEnabled = (hasPage && mMainWidget->sieveEditorMainWidget()->pageMode() == KSieveUi::SieveEditorWidget::TextMode);
343 mUndoAction->setEnabled(editActionEnabled && b);
346 void SieveEditorMainWindow::slotRedoAvailable(bool b)
348 const bool hasPage = (mMainWidget->sieveEditorMainWidget()->tabWidget()->count() > 0);
349 const bool editActionEnabled = (hasPage && mMainWidget->sieveEditorMainWidget()->pageMode() == KSieveUi::SieveEditorWidget::TextMode);
350 mRedoAction->setEnabled(editActionEnabled && b);
353 void SieveEditorMainWindow::slotCopyAvailable(bool b)
355 const bool hasPage = (mMainWidget->sieveEditorMainWidget()->tabWidget()->count() > 0);
356 const bool editActionEnabled = (hasPage && mMainWidget->sieveEditorMainWidget()->pageMode() == KSieveUi::SieveEditorWidget::TextMode);
357 mCopyAction->setEnabled(editActionEnabled && b);
358 mCutAction->setEnabled(editActionEnabled && b);
361 void SieveEditorMainWindow::slotOpenBookmarkUrl(const QUrl &url)
363 mMainWidget->sieveEditorMainWidget()->openBookmarkUrl(url);
366 QString SieveEditorMainWindow::currentHelpTitle() const
368 return mMainWidget->sieveEditorMainWidget()->currentHelpTitle();
371 QUrl SieveEditorMainWindow::currentHelpUrl() const
373 return mMainWidget->sieveEditorMainWidget()->currentHelpUrl();