Astyle kdelibs
[kdepim.git] / sieveeditor / sieveeditorpagewidget.cpp
blob3ce0453fe4b5b604d52ae49d40eda6f9c911ca22
1 /*
2 Copyright (c) 2014-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 "sieveeditorpagewidget.h"
22 #include "ksieveui/sieveeditorwidget.h"
23 #include "sieveeditorglobalconfig.h"
25 #include <kmanagesieve/sievejob.h>
27 #include <KLocalizedString>
28 #include <KMessageBox>
30 #include "sieveeditor_debug.h"
31 #include <QVBoxLayout>
33 SieveEditorPageWidget::SieveEditorPageWidget(QWidget *parent)
34 : QWidget(parent),
35 mWasActive(false),
36 mIsNewScript(false)
38 QVBoxLayout *vbox = new QVBoxLayout;
39 setLayout(vbox);
40 mSieveEditorWidget = new KSieveUi::SieveEditorWidget(false);
41 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::valueChanged, this, &SieveEditorPageWidget::slotValueChanged);
42 vbox->addWidget(mSieveEditorWidget);
43 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::checkSyntax, this, &SieveEditorPageWidget::slotCheckSyntaxClicked);
44 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::modeEditorChanged, this, &SieveEditorPageWidget::modeEditorChanged);
45 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::undoAvailable, this, &SieveEditorPageWidget::undoAvailable);
46 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::redoAvailable, this, &SieveEditorPageWidget::redoAvailable);
47 connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::copyAvailable, this, &SieveEditorPageWidget::copyAvailable);
50 SieveEditorPageWidget::~SieveEditorPageWidget()
54 void SieveEditorPageWidget::slotCheckSyntaxClicked()
56 const QString script = mSieveEditorWidget->script();
57 if (script.isEmpty()) {
58 return;
60 KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, script, mWasActive, mWasActive);
61 job->setInteractive(false);
62 connect(job, &KManageSieve::SieveJob::errorMessage, this, &SieveEditorPageWidget::slotPutResultDebug);
65 void SieveEditorPageWidget::slotPutResultDebug(KManageSieve::SieveJob *, bool success, const QString &errorMsg)
67 if (success) {
68 mSieveEditorWidget->addOkMessage(i18n("No errors found."));
69 } else {
70 if (errorMsg.isEmpty()) {
71 mSieveEditorWidget->addFailedMessage(i18n("An unknown error was encountered."));
72 } else {
73 mSieveEditorWidget->addFailedMessage(errorMsg);
76 //Put original script after check otherwise we will put a script even if we don't click on ok
77 KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, mSieveEditorWidget->originalScript(), mWasActive, mWasActive);
78 job->setInteractive(false);
79 mSieveEditorWidget->resultDone();
82 void SieveEditorPageWidget::setIsNewScript(bool isNewScript)
84 mIsNewScript = isNewScript;
87 void SieveEditorPageWidget::loadScript(const QUrl &url, const QStringList &capabilities)
89 mCurrentURL = url;
90 mSieveEditorWidget->setSieveCapabilities(capabilities);
91 mSieveEditorWidget->setReadOnly(true);
92 mSieveEditorWidget->wordWrap(SieveEditorGlobalConfig::self()->wrapText());
93 KManageSieve::SieveJob *job = KManageSieve::SieveJob::get(url);
94 connect(job, &KManageSieve::SieveJob::result, this, &SieveEditorPageWidget::slotGetResult);
97 QUrl SieveEditorPageWidget::currentUrl() const
99 return mCurrentURL;
102 void SieveEditorPageWidget::slotGetResult(KManageSieve::SieveJob *, bool success, const QString &script, bool isActive)
104 mSieveEditorWidget->setReadOnly(false);
105 if (!success) {
106 return;
108 mSieveEditorWidget->setScriptName(mCurrentURL.fileName());
109 mSieveEditorWidget->setScript(script);
110 mWasActive = isActive;
111 mSieveEditorWidget->setModified(false);
114 void SieveEditorPageWidget::uploadScript(bool showInformation, bool forceSave)
116 if (mSieveEditorWidget->isModified() || forceSave) {
117 KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, mSieveEditorWidget->script(), mWasActive, mWasActive);
118 job->setProperty("showuploadinformation", showInformation);
119 connect(job, &KManageSieve::SieveJob::result, this, &SieveEditorPageWidget::slotPutResult);
123 void SieveEditorPageWidget::slotPutResult(KManageSieve::SieveJob *job, bool success)
125 if (mIsNewScript) {
126 Q_EMIT refreshList();
128 if (success) {
129 if (job->property("showuploadinformation").toBool()) {
130 KMessageBox::information(this, i18n("The Sieve script was successfully uploaded."),
131 i18n("Sieve Script Upload"));
133 mIsNewScript = false;
134 mSieveEditorWidget->setModified(false);
135 } else {
136 //TODO error
140 bool SieveEditorPageWidget::needToSaveScript()
142 bool result = false;
143 if (mIsNewScript) {
144 const int resultQuestion = KMessageBox::warningYesNoCancel(this, i18n("Script '%1' is new. Do you want to save it?", mCurrentURL.fileName()));
145 if (resultQuestion == KMessageBox::Yes) {
146 uploadScript();
147 result = true;
148 } else if (resultQuestion == KMessageBox::Cancel) {
149 result = true;
151 } else {
152 if (mSieveEditorWidget->isModified()) {
153 const int resultQuestion = KMessageBox::warningYesNoCancel(this, i18n("Script '%1' was changed. Do you want to save it ?", mCurrentURL.fileName()));
154 if (resultQuestion == KMessageBox::Yes) {
155 uploadScript();
156 result = true;
157 } else if (resultQuestion == KMessageBox::Cancel) {
158 result = true;
162 return result;
165 void SieveEditorPageWidget::slotValueChanged(bool b)
167 Q_EMIT scriptModified(b, this);
170 bool SieveEditorPageWidget::isModified() const
172 return mSieveEditorWidget->isModified();
175 void SieveEditorPageWidget::goToLine()
177 mSieveEditorWidget->goToLine();
180 void SieveEditorPageWidget::undo()
182 mSieveEditorWidget->undo();
185 bool SieveEditorPageWidget::isUndoAvailable() const
187 return mSieveEditorWidget->isUndoAvailable();
190 bool SieveEditorPageWidget::isRedoAvailable() const
192 return mSieveEditorWidget->isRedoAvailable();
195 bool SieveEditorPageWidget::hasSelection() const
197 return mSieveEditorWidget->hasSelection();
200 void SieveEditorPageWidget::redo()
202 mSieveEditorWidget->redo();
205 void SieveEditorPageWidget::find()
207 mSieveEditorWidget->find();
210 void SieveEditorPageWidget::replace()
212 mSieveEditorWidget->replace();
215 void SieveEditorPageWidget::share()
217 mSieveEditorWidget->slotUploadScripts();
220 void SieveEditorPageWidget::import()
222 mSieveEditorWidget->slotImport();
225 void SieveEditorPageWidget::comment()
227 mSieveEditorWidget->comment();
230 void SieveEditorPageWidget::uncomment()
232 mSieveEditorWidget->uncomment();
235 void SieveEditorPageWidget::checkSpelling()
237 mSieveEditorWidget->checkSpelling();
240 void SieveEditorPageWidget::autoGenerateScript()
242 mSieveEditorWidget->slotAutoGenerateScripts();
245 void SieveEditorPageWidget::checkSyntax()
247 mSieveEditorWidget->slotCheckSyntax();
250 void SieveEditorPageWidget::saveAs()
252 mSieveEditorWidget->slotSaveAs();
255 void SieveEditorPageWidget::reverseCase()
257 mSieveEditorWidget->reverseCase();
260 void SieveEditorPageWidget::lowerCase()
262 mSieveEditorWidget->lowerCase();
265 void SieveEditorPageWidget::debugSieveScript()
267 mSieveEditorWidget->debugSieveScript();
270 void SieveEditorPageWidget::upperCase()
272 mSieveEditorWidget->upperCase();
275 void SieveEditorPageWidget::sentenceCase()
277 mSieveEditorWidget->sentenceCase();
280 KSieveUi::SieveEditorWidget::EditorMode SieveEditorPageWidget::pageMode() const
282 return mSieveEditorWidget->mode();
285 void SieveEditorPageWidget::paste()
287 mSieveEditorWidget->paste();
290 void SieveEditorPageWidget::cut()
292 mSieveEditorWidget->cut();
295 void SieveEditorPageWidget::copy()
297 mSieveEditorWidget->copy();
300 void SieveEditorPageWidget::selectAll()
302 mSieveEditorWidget->selectAll();
305 void SieveEditorPageWidget::zoomIn()
307 mSieveEditorWidget->zoomIn();
310 void SieveEditorPageWidget::zoomOut()
312 mSieveEditorWidget->zoomOut();
315 void SieveEditorPageWidget::wordWrap(bool state)
317 mSieveEditorWidget->wordWrap(state);
320 bool SieveEditorPageWidget::isWordWrap() const
322 return mSieveEditorWidget->isWordWrap();
325 void SieveEditorPageWidget::zoomReset()
327 mSieveEditorWidget->zoomReset();
330 void SieveEditorPageWidget::openBookmarkUrl(const QUrl &url)
332 mSieveEditorWidget->openBookmarkUrl(url);
335 QString SieveEditorPageWidget::currentHelpTitle() const
337 return mSieveEditorWidget->currentHelpTitle();
340 QUrl SieveEditorPageWidget::currentHelpUrl() const
342 return mSieveEditorWidget->currentHelpUrl();