sieveeditorpagewidget.cpp 9.46 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2014-2016 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.

*/

#include "sieveeditorpagewidget.h"
22
#include "ksieveui/sieveeditorwidget.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include "sieveeditorglobalconfig.h"
Laurent Montel's avatar
Laurent Montel committed
24 25 26

#include <kmanagesieve/sievejob.h>

Laurent Montel's avatar
Laurent Montel committed
27 28 29
#include <KLocalizedString>
#include <KMessageBox>

Laurent Montel's avatar
Laurent Montel committed
30
#include "sieveeditor_debug.h"
Laurent Montel's avatar
Laurent Montel committed
31
#include <QVBoxLayout>
32

Laurent Montel's avatar
Laurent Montel committed
33
SieveEditorPageWidget::SieveEditorPageWidget(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
34
    : QWidget(parent),
35
      mWasActive(false),
36
      mIsNewScript(false)
Laurent Montel's avatar
Laurent Montel committed
37
{
Laurent Montel's avatar
Laurent Montel committed
38 39
    QVBoxLayout *vbox = new QVBoxLayout;
    setLayout(vbox);
Laurent Montel's avatar
Laurent Montel committed
40
    mSieveEditorWidget = new KSieveUi::SieveEditorWidget(false);
41
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::valueChanged, this, &SieveEditorPageWidget::slotValueChanged);
Laurent Montel's avatar
Laurent Montel committed
42
    vbox->addWidget(mSieveEditorWidget);
43
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::checkSyntax, this, &SieveEditorPageWidget::slotCheckSyntaxClicked);
Laurent Montel's avatar
Laurent Montel committed
44
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::modeEditorChanged, this, &SieveEditorPageWidget::modeEditorChanged);
Laurent Montel's avatar
Laurent Montel committed
45 46 47
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::undoAvailable, this, &SieveEditorPageWidget::undoAvailable);
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::redoAvailable, this, &SieveEditorPageWidget::redoAvailable);
    connect(mSieveEditorWidget, &KSieveUi::SieveEditorWidget::copyAvailable, this, &SieveEditorPageWidget::copyAvailable);
Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52 53
}

SieveEditorPageWidget::~SieveEditorPageWidget()
{
}

Laurent Montel's avatar
Laurent Montel committed
54 55
void SieveEditorPageWidget::slotCheckSyntaxClicked()
{
56 57 58 59 60
    const QString script = mSieveEditorWidget->script();
    if (script.isEmpty()) {
        return;
    }
    KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, script, mWasActive, mWasActive);
Laurent Montel's avatar
Laurent Montel committed
61
    job->setInteractive(false);
Laurent Montel's avatar
Laurent Montel committed
62
    connect(job, &KManageSieve::SieveJob::errorMessage, this, &SieveEditorPageWidget::slotPutResultDebug);
Laurent Montel's avatar
Laurent Montel committed
63 64
}

Laurent Montel's avatar
Laurent Montel committed
65
void SieveEditorPageWidget::slotPutResultDebug(KManageSieve::SieveJob *, bool success, const QString &errorMsg)
Laurent Montel's avatar
Laurent Montel committed
66
{
Laurent Montel's avatar
Laurent Montel committed
67 68
    if (success) {
        mSieveEditorWidget->addOkMessage(i18n("No errors found."));
Laurent Montel's avatar
Laurent Montel committed
69
    } else {
Laurent Montel's avatar
Laurent Montel committed
70 71 72 73 74
        if (errorMsg.isEmpty()) {
            mSieveEditorWidget->addFailedMessage(i18n("An unknown error was encountered."));
        } else {
            mSieveEditorWidget->addFailedMessage(errorMsg);
        }
Laurent Montel's avatar
Laurent Montel committed
75 76
    }
    //Put original script after check otherwise we will put a script even if we don't click on ok
Laurent Montel's avatar
Laurent Montel committed
77 78
    KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, mSieveEditorWidget->originalScript(), mWasActive, mWasActive);
    job->setInteractive(false);
Laurent Montel's avatar
Laurent Montel committed
79
    mSieveEditorWidget->resultDone();
Laurent Montel's avatar
Laurent Montel committed
80 81
}

82 83 84 85 86
void SieveEditorPageWidget::setIsNewScript(bool isNewScript)
{
    mIsNewScript = isNewScript;
}

Laurent Montel's avatar
Laurent Montel committed
87
void SieveEditorPageWidget::loadScript(const QUrl &url, const QStringList &capabilities)
Laurent Montel's avatar
Laurent Montel committed
88 89
{
    mCurrentURL = url;
Laurent Montel's avatar
Laurent Montel committed
90
    mSieveEditorWidget->setSieveCapabilities(capabilities);
91
    mSieveEditorWidget->setReadOnly(true);
Laurent Montel's avatar
Laurent Montel committed
92
    mSieveEditorWidget->wordWrap(SieveEditorGlobalConfig::self()->wrapText());
Laurent Montel's avatar
Laurent Montel committed
93
    KManageSieve::SieveJob *job = KManageSieve::SieveJob::get(url);
Laurent Montel's avatar
Laurent Montel committed
94
    connect(job, &KManageSieve::SieveJob::result, this, &SieveEditorPageWidget::slotGetResult);
Laurent Montel's avatar
Laurent Montel committed
95 96
}

Laurent Montel's avatar
Laurent Montel committed
97
QUrl SieveEditorPageWidget::currentUrl() const
98 99 100 101
{
    return mCurrentURL;
}

Laurent Montel's avatar
Laurent Montel committed
102
void SieveEditorPageWidget::slotGetResult(KManageSieve::SieveJob *, bool success, const QString &script, bool isActive)
Laurent Montel's avatar
Laurent Montel committed
103
{
104
    mSieveEditorWidget->setReadOnly(false);
Laurent Montel's avatar
Laurent Montel committed
105
    if (!success) {
Laurent Montel's avatar
Laurent Montel committed
106
        return;
Laurent Montel's avatar
Laurent Montel committed
107 108 109
    }
    mSieveEditorWidget->setScriptName(mCurrentURL.fileName());
    mSieveEditorWidget->setScript(script);
110
    mWasActive = isActive;
111
    mSieveEditorWidget->setModified(false);
Laurent Montel's avatar
Laurent Montel committed
112
}
Laurent Montel's avatar
Laurent Montel committed
113

Laurent Montel's avatar
Laurent Montel committed
114
void SieveEditorPageWidget::uploadScript(bool showInformation, bool forceSave)
Laurent Montel's avatar
Laurent Montel committed
115
{
116
    if (mSieveEditorWidget->isModified() || forceSave) {
Laurent Montel's avatar
Laurent Montel committed
117
        KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(mCurrentURL, mSieveEditorWidget->script(), mWasActive, mWasActive);
118
        job->setProperty("showuploadinformation", showInformation);
Laurent Montel's avatar
Laurent Montel committed
119
        connect(job, &KManageSieve::SieveJob::result, this, &SieveEditorPageWidget::slotPutResult);
120
    }
Laurent Montel's avatar
Laurent Montel committed
121 122
}

Laurent Montel's avatar
Laurent Montel committed
123
void SieveEditorPageWidget::slotPutResult(KManageSieve::SieveJob *job, bool success)
Laurent Montel's avatar
Laurent Montel committed
124
{
Laurent Montel's avatar
Laurent Montel committed
125
    if (mIsNewScript) {
Laurent Montel's avatar
Laurent Montel committed
126
        Q_EMIT refreshList();
Laurent Montel's avatar
Laurent Montel committed
127 128
    }
    if (success) {
129
        if (job->property("showuploadinformation").toBool()) {
Laurent Montel's avatar
Laurent Montel committed
130 131
            KMessageBox::information(this, i18n("The Sieve script was successfully uploaded."),
                                     i18n("Sieve Script Upload"));
132
        }
Laurent Montel's avatar
Laurent Montel committed
133
        mIsNewScript = false;
Laurent Montel's avatar
Laurent Montel committed
134
        mSieveEditorWidget->updateOriginalScript();
135
        mSieveEditorWidget->setModified(false);
Laurent Montel's avatar
Laurent Montel committed
136 137 138 139
    } else {
        //TODO error
    }
}
140

141
bool SieveEditorPageWidget::needToSaveScript()
142
{
143
    bool result = false;
144
    if (mIsNewScript) {
Laurent Montel's avatar
Laurent Montel committed
145 146
        const int resultQuestion = KMessageBox::warningYesNoCancel(this, i18n("Script '%1' is new. Do you want to save it?", mCurrentURL.fileName()));
        if (resultQuestion == KMessageBox::Yes) {
Laurent Montel's avatar
Laurent Montel committed
147
            uploadScript();
148
            result = true;
Laurent Montel's avatar
Laurent Montel committed
149 150
        } else if (resultQuestion == KMessageBox::Cancel) {
            result = true;
151
        }
152
    } else {
153
        if (mSieveEditorWidget->isModified()) {
Laurent Montel's avatar
Laurent Montel committed
154
            const int resultQuestion = KMessageBox::warningYesNoCancel(this, i18n("Script '%1' was changed. Do you want to save it ?", mCurrentURL.fileName()));
Laurent Montel's avatar
Laurent Montel committed
155
            if (resultQuestion == KMessageBox::Yes) {
Laurent Montel's avatar
Laurent Montel committed
156
                uploadScript();
157
                result = true;
Laurent Montel's avatar
Laurent Montel committed
158 159
            } else if (resultQuestion == KMessageBox::Cancel) {
                result = true;
160 161
            }
        }
162
    }
163
    return result;
164
}
Laurent Montel's avatar
Laurent Montel committed
165

166
void SieveEditorPageWidget::slotValueChanged(bool b)
167
{
168
    Q_EMIT scriptModified(b, this);
169 170
}

171
bool SieveEditorPageWidget::isModified() const
Laurent Montel's avatar
Laurent Montel committed
172
{
173
    return mSieveEditorWidget->isModified();
Laurent Montel's avatar
Laurent Montel committed
174
}
175 176 177 178 179

void SieveEditorPageWidget::goToLine()
{
    mSieveEditorWidget->goToLine();
}
180

Laurent Montel's avatar
Laurent Montel committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
void SieveEditorPageWidget::undo()
{
    mSieveEditorWidget->undo();
}

bool SieveEditorPageWidget::isUndoAvailable() const
{
    return mSieveEditorWidget->isUndoAvailable();
}

bool SieveEditorPageWidget::isRedoAvailable() const
{
    return mSieveEditorWidget->isRedoAvailable();
}

196 197 198 199 200
bool SieveEditorPageWidget::hasSelection() const
{
    return mSieveEditorWidget->hasSelection();
}

Laurent Montel's avatar
Laurent Montel committed
201 202 203 204 205
void SieveEditorPageWidget::redo()
{
    mSieveEditorWidget->redo();
}

206 207 208 209 210 211 212 213 214 215
void SieveEditorPageWidget::find()
{
    mSieveEditorWidget->find();
}

void SieveEditorPageWidget::replace()
{
    mSieveEditorWidget->replace();
}

Laurent Montel's avatar
Laurent Montel committed
216
void SieveEditorPageWidget::shareScript()
Laurent Montel's avatar
Laurent Montel committed
217
{
Laurent Montel's avatar
Laurent Montel committed
218
    mSieveEditorWidget->slotShareScript();
Laurent Montel's avatar
Laurent Montel committed
219 220 221 222 223 224 225
}

void SieveEditorPageWidget::import()
{
    mSieveEditorWidget->slotImport();
}

226 227 228 229 230 231 232 233 234 235
void SieveEditorPageWidget::comment()
{
    mSieveEditorWidget->comment();
}

void SieveEditorPageWidget::uncomment()
{
    mSieveEditorWidget->uncomment();
}

236 237 238 239 240
void SieveEditorPageWidget::checkSpelling()
{
    mSieveEditorWidget->checkSpelling();
}

241 242 243 244 245 246 247 248 249 250
void SieveEditorPageWidget::autoGenerateScript()
{
    mSieveEditorWidget->slotAutoGenerateScripts();
}

void SieveEditorPageWidget::checkSyntax()
{
    mSieveEditorWidget->slotCheckSyntax();
}

Laurent Montel's avatar
Laurent Montel committed
251 252 253 254 255
void SieveEditorPageWidget::saveAs()
{
    mSieveEditorWidget->slotSaveAs();
}

256 257 258 259 260 261 262 263 264 265
void SieveEditorPageWidget::reverseCase()
{
    mSieveEditorWidget->reverseCase();
}

void SieveEditorPageWidget::lowerCase()
{
    mSieveEditorWidget->lowerCase();
}

266 267 268 269 270
void SieveEditorPageWidget::debugSieveScript()
{
    mSieveEditorWidget->debugSieveScript();
}

271 272 273 274 275 276 277 278 279 280
void SieveEditorPageWidget::upperCase()
{
    mSieveEditorWidget->upperCase();
}

void SieveEditorPageWidget::sentenceCase()
{
    mSieveEditorWidget->sentenceCase();
}

281 282 283 284
KSieveUi::SieveEditorWidget::EditorMode SieveEditorPageWidget::pageMode() const
{
    return mSieveEditorWidget->mode();
}
Laurent Montel's avatar
Laurent Montel committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

void SieveEditorPageWidget::paste()
{
    mSieveEditorWidget->paste();
}

void SieveEditorPageWidget::cut()
{
    mSieveEditorWidget->cut();
}

void SieveEditorPageWidget::copy()
{
    mSieveEditorWidget->copy();
}

Laurent Montel's avatar
Laurent Montel committed
301 302 303 304
void SieveEditorPageWidget::selectAll()
{
    mSieveEditorWidget->selectAll();
}
Laurent Montel's avatar
Laurent Montel committed
305 306 307 308 309 310 311 312 313 314

void SieveEditorPageWidget::zoomIn()
{
    mSieveEditorWidget->zoomIn();
}

void SieveEditorPageWidget::zoomOut()
{
    mSieveEditorWidget->zoomOut();
}
Laurent Montel's avatar
Laurent Montel committed
315

Laurent Montel's avatar
Laurent Montel committed
316 317 318 319 320
void SieveEditorPageWidget::wordWrap(bool state)
{
    mSieveEditorWidget->wordWrap(state);
}

Laurent Montel's avatar
Laurent Montel committed
321 322 323 324 325
bool SieveEditorPageWidget::isWordWrap() const
{
    return mSieveEditorWidget->isWordWrap();
}

326 327
void SieveEditorPageWidget::print()
{
Laurent Montel's avatar
Laurent Montel committed
328
    mSieveEditorWidget->print();
329 330 331 332
}

void SieveEditorPageWidget::printPreview()
{
Laurent Montel's avatar
Laurent Montel committed
333
    mSieveEditorWidget->printPreview();
334 335
}

Laurent Montel's avatar
Laurent Montel committed
336 337 338 339 340
void SieveEditorPageWidget::zoomReset()
{
    mSieveEditorWidget->zoomReset();
}

Laurent Montel's avatar
Laurent Montel committed
341
void SieveEditorPageWidget::openBookmarkUrl(const QUrl &url)
342
{
Laurent Montel's avatar
Laurent Montel committed
343
    mSieveEditorWidget->openBookmarkUrl(url);
344 345
}

Laurent Montel's avatar
Laurent Montel committed
346 347 348 349 350 351 352 353 354
QString SieveEditorPageWidget::currentHelpTitle() const
{
    return mSieveEditorWidget->currentHelpTitle();
}

QUrl SieveEditorPageWidget::currentHelpUrl() const
{
    return mSieveEditorWidget->currentHelpUrl();
}