2 Copyright (c) 2014-2015 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
21 #include "sieveeditorpagewidget.h"
22 #include "ksieveui/sieveeditorwidget.h"
24 #include <kmanagesieve/sievejob.h>
26 #include <KLocalizedString>
27 #include <KMessageBox>
29 #include "sieveeditor_debug.h"
30 #include <QVBoxLayout>
32 SieveEditorPageWidget::SieveEditorPageWidget(QWidget
*parent
)
37 QVBoxLayout
*vbox
= new QVBoxLayout
;
39 mSieveEditorWidget
= new KSieveUi::SieveEditorWidget(false);
40 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::valueChanged
, this, &SieveEditorPageWidget::slotValueChanged
);
41 vbox
->addWidget(mSieveEditorWidget
);
42 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::checkSyntax
, this, &SieveEditorPageWidget::slotCheckSyntaxClicked
);
43 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::modeEditorChanged
, this, &SieveEditorPageWidget::modeEditorChanged
);
44 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::undoAvailable
, this, &SieveEditorPageWidget::undoAvailable
);
45 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::redoAvailable
, this, &SieveEditorPageWidget::redoAvailable
);
46 connect(mSieveEditorWidget
, &KSieveUi::SieveEditorWidget::copyAvailable
, this, &SieveEditorPageWidget::copyAvailable
);
49 SieveEditorPageWidget::~SieveEditorPageWidget()
53 void SieveEditorPageWidget::slotCheckSyntaxClicked()
55 const QString script
= mSieveEditorWidget
->script();
56 if (script
.isEmpty()) {
59 KManageSieve::SieveJob
*job
= KManageSieve::SieveJob::put(mCurrentURL
, script
, mWasActive
, mWasActive
);
60 job
->setInteractive(false);
61 connect(job
, &KManageSieve::SieveJob::errorMessage
, this, &SieveEditorPageWidget::slotPutResultDebug
);
64 void SieveEditorPageWidget::slotPutResultDebug(KManageSieve::SieveJob
*, bool success
, const QString
&errorMsg
)
67 mSieveEditorWidget
->addOkMessage(i18n("No errors found."));
69 if (errorMsg
.isEmpty()) {
70 mSieveEditorWidget
->addFailedMessage(i18n("An unknown error was encountered."));
72 mSieveEditorWidget
->addFailedMessage(errorMsg
);
75 //Put original script after check otherwise we will put a script even if we don't click on ok
76 KManageSieve::SieveJob
*job
= KManageSieve::SieveJob::put(mCurrentURL
, mSieveEditorWidget
->originalScript(), mWasActive
, mWasActive
);
77 job
->setInteractive(false);
78 mSieveEditorWidget
->resultDone();
81 void SieveEditorPageWidget::setIsNewScript(bool isNewScript
)
83 mIsNewScript
= isNewScript
;
86 void SieveEditorPageWidget::loadScript(const QUrl
&url
, const QStringList
&capabilities
)
89 mSieveEditorWidget
->setSieveCapabilities(capabilities
);
90 mSieveEditorWidget
->setReadOnly(true);
91 KManageSieve::SieveJob
*job
= KManageSieve::SieveJob::get(url
);
92 connect(job
, &KManageSieve::SieveJob::result
, this, &SieveEditorPageWidget::slotGetResult
);
95 QUrl
SieveEditorPageWidget::currentUrl() const
100 void SieveEditorPageWidget::slotGetResult(KManageSieve::SieveJob
*, bool success
, const QString
&script
, bool isActive
)
102 mSieveEditorWidget
->setReadOnly(false);
106 mSieveEditorWidget
->setScriptName(mCurrentURL
.fileName());
107 mSieveEditorWidget
->setScript(script
);
108 mWasActive
= isActive
;
109 mSieveEditorWidget
->setModified(false);
112 void SieveEditorPageWidget::uploadScript(bool showInformation
, bool forceSave
)
114 if (mSieveEditorWidget
->isModified() || forceSave
) {
115 KManageSieve::SieveJob
*job
= KManageSieve::SieveJob::put(mCurrentURL
, mSieveEditorWidget
->script(), mWasActive
, mWasActive
);
116 job
->setProperty("showuploadinformation", showInformation
);
117 connect(job
, &KManageSieve::SieveJob::result
, this, &SieveEditorPageWidget::slotPutResult
);
121 void SieveEditorPageWidget::slotPutResult(KManageSieve::SieveJob
*job
, bool success
)
124 Q_EMIT
refreshList();
127 if (job
->property("showuploadinformation").toBool()) {
128 KMessageBox::information(this, i18n("The Sieve script was successfully uploaded."),
129 i18n("Sieve Script Upload"));
131 mIsNewScript
= false;
132 mSieveEditorWidget
->setModified(false);
138 bool SieveEditorPageWidget::needToSaveScript()
142 const int resultQuestion
= KMessageBox::warningYesNoCancel(this, i18n("Script '%1' is new. Do you want to save it?", mCurrentURL
.fileName()));
143 if (resultQuestion
== KMessageBox::Yes
) {
146 } else if (resultQuestion
== KMessageBox::Cancel
) {
150 if (mSieveEditorWidget
->isModified()) {
151 const int resultQuestion
= KMessageBox::warningYesNoCancel(this, i18n("Script '%1' was changed. Do you want to save it ?", mCurrentURL
.fileName()));
152 if (resultQuestion
== KMessageBox::Yes
) {
155 } else if (resultQuestion
== KMessageBox::Cancel
) {
163 void SieveEditorPageWidget::slotValueChanged(bool b
)
165 Q_EMIT
scriptModified(b
, this);
168 bool SieveEditorPageWidget::isModified() const
170 return mSieveEditorWidget
->isModified();
173 void SieveEditorPageWidget::goToLine()
175 mSieveEditorWidget
->goToLine();
178 void SieveEditorPageWidget::undo()
180 mSieveEditorWidget
->undo();
183 bool SieveEditorPageWidget::isUndoAvailable() const
185 return mSieveEditorWidget
->isUndoAvailable();
188 bool SieveEditorPageWidget::isRedoAvailable() const
190 return mSieveEditorWidget
->isRedoAvailable();
193 bool SieveEditorPageWidget::hasSelection() const
195 return mSieveEditorWidget
->hasSelection();
198 void SieveEditorPageWidget::redo()
200 mSieveEditorWidget
->redo();
203 void SieveEditorPageWidget::find()
205 mSieveEditorWidget
->find();
208 void SieveEditorPageWidget::replace()
210 mSieveEditorWidget
->replace();
213 void SieveEditorPageWidget::share()
215 mSieveEditorWidget
->slotUploadScripts();
218 void SieveEditorPageWidget::import()
220 mSieveEditorWidget
->slotImport();
223 void SieveEditorPageWidget::comment()
225 mSieveEditorWidget
->comment();
228 void SieveEditorPageWidget::uncomment()
230 mSieveEditorWidget
->uncomment();
233 void SieveEditorPageWidget::checkSpelling()
235 mSieveEditorWidget
->checkSpelling();
238 void SieveEditorPageWidget::autoGenerateScript()
240 mSieveEditorWidget
->slotAutoGenerateScripts();
243 void SieveEditorPageWidget::checkSyntax()
245 mSieveEditorWidget
->slotCheckSyntax();
248 void SieveEditorPageWidget::saveAs()
250 mSieveEditorWidget
->slotSaveAs();
253 void SieveEditorPageWidget::reverseCase()
255 mSieveEditorWidget
->reverseCase();
258 void SieveEditorPageWidget::lowerCase()
260 mSieveEditorWidget
->lowerCase();
263 void SieveEditorPageWidget::debugSieveScript()
265 mSieveEditorWidget
->debugSieveScript();
268 void SieveEditorPageWidget::upperCase()
270 mSieveEditorWidget
->upperCase();
273 void SieveEditorPageWidget::sentenceCase()
275 mSieveEditorWidget
->sentenceCase();
278 KSieveUi::SieveEditorWidget::EditorMode
SieveEditorPageWidget::pageMode() const
280 return mSieveEditorWidget
->mode();
283 void SieveEditorPageWidget::paste()
285 mSieveEditorWidget
->paste();
288 void SieveEditorPageWidget::cut()
290 mSieveEditorWidget
->cut();
293 void SieveEditorPageWidget::copy()
295 mSieveEditorWidget
->copy();
298 void SieveEditorPageWidget::selectAll()
300 mSieveEditorWidget
->selectAll();
303 void SieveEditorPageWidget::zoomIn()
305 mSieveEditorWidget
->zoomIn();
308 void SieveEditorPageWidget::zoomOut()
310 mSieveEditorWidget
->zoomOut();
313 void SieveEditorPageWidget::wordWrap(bool state
)
315 mSieveEditorWidget
->wordWrap(state
);
318 bool SieveEditorPageWidget::isWordWrap() const
320 return mSieveEditorWidget
->isWordWrap();
323 void SieveEditorPageWidget::zoomReset()
325 mSieveEditorWidget
->zoomReset();
328 void SieveEditorPageWidget::openBookmarkUrl(const QUrl
&url
)
330 mSieveEditorWidget
->openBookmarkUrl(url
);
333 QString
SieveEditorPageWidget::currentHelpTitle() const
335 return mSieveEditorWidget
->currentHelpTitle();
338 QUrl
SieveEditorPageWidget::currentHelpUrl() const
340 return mSieveEditorWidget
->currentHelpUrl();