sieveeditormainwidget.cpp 17.6 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2013-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 "sieveeditormainwidget.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "sieveeditorscriptmanagerwidget.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include "sieveeditorpagewidget.h"
24
#include "sieveeditortabwidget.h"
25
#include "sieveeditoremptytabwidgetlabel.h"
26
#include "ksieveui/sieveeditor.h"
Laurent Montel's avatar
Laurent Montel committed
27

Laurent Montel's avatar
Laurent Montel committed
28
#include <KLocalizedString>
29
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
30
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QTabWidget>
32
#include <KColorScheme>
Laurent Montel's avatar
Laurent Montel committed
33
#include <KMessageBox>
34

Laurent Montel's avatar
Laurent Montel committed
35
#include <QSplitter>
36
#include <QStackedWidget>
37
#include <QTabBar>
Laurent Montel's avatar
Laurent Montel committed
38 39

SieveEditorMainWidget::SieveEditorMainWidget(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
40
    : QSplitter(parent)
Laurent Montel's avatar
Laurent Montel committed
41
{
42 43 44 45 46
    mStackedWidget = new QStackedWidget(this);
    mStackedWidget->setObjectName(QStringLiteral("stackedwidget"));

    mEditorEmptyLabel = new SieveEditorEmptyTabWidgetLabel;

47
    mTabWidget = new SieveEditorTabWidget;
48 49
    connect(mTabWidget, &SieveEditorTabWidget::tabCloseRequestedIndex, this, &SieveEditorMainWidget::slotTabCloseRequested);
    connect(mTabWidget, &SieveEditorTabWidget::tabRemoveAllExclude, this, &SieveEditorMainWidget::slotTabRemoveAllExclude);
50
    connect(mTabWidget, &SieveEditorTabWidget::tabCloseAllTab, this, &SieveEditorMainWidget::slotTabCloseAllRequested);
51 52 53 54 55

    mStackedWidget->addWidget(mTabWidget);
    mStackedWidget->addWidget(mEditorEmptyLabel);
    addWidget(mStackedWidget);

Laurent Montel's avatar
Laurent Montel committed
56
    mScriptManagerWidget = new SieveEditorScriptManagerWidget;
Laurent Montel's avatar
Laurent Montel committed
57
    connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::createScriptPage, this, &SieveEditorMainWidget::slotCreateScriptPage);
Laurent Montel's avatar
Laurent Montel committed
58
    connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::updateButtons, this, &SieveEditorMainWidget::updateButtons);
59
    connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::scriptDeleted, this, &SieveEditorMainWidget::slotScriptDeleted);
Laurent Montel's avatar
Laurent Montel committed
60
    connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::serverSieveFound, this, &SieveEditorMainWidget::serverSieveFound);
61
    connect(this, &SieveEditorMainWidget::updateScriptList, mScriptManagerWidget, &SieveEditorScriptManagerWidget::slotRefreshList);
Laurent Montel's avatar
Laurent Montel committed
62 63 64 65
    addWidget(mScriptManagerWidget);
    setChildrenCollapsible(false);
    QList<int> splitterSizes;
    splitterSizes << 80 << 20;
Laurent Montel's avatar
Laurent Montel committed
66 67
    KConfigGroup myGroup(KSharedConfig::openConfig(), "SieveEditorMainWidget");
    setSizes(myGroup.readEntry("mainSplitter", splitterSizes));
68
    updateStackedWidget();
Laurent Montel's avatar
Laurent Montel committed
69 70 71 72
}

SieveEditorMainWidget::~SieveEditorMainWidget()
{
Laurent Montel's avatar
Laurent Montel committed
73 74
    KConfigGroup myGroup(KSharedConfig::openConfig(), "SieveEditorMainWidget");
    myGroup.writeEntry("mainSplitter", sizes());
Laurent Montel's avatar
Laurent Montel committed
75
    myGroup.sync();
Laurent Montel's avatar
Laurent Montel committed
76
}
Laurent Montel's avatar
Laurent Montel committed
77

Laurent Montel's avatar
Laurent Montel committed
78
QWidget *SieveEditorMainWidget::hasExistingPage(const QUrl &url)
79
{
Laurent Montel's avatar
Laurent Montel committed
80
    for (int i = 0; i < mTabWidget->count(); ++i) {
Laurent Montel's avatar
Laurent Montel committed
81
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(i));
82 83 84 85 86 87
        if (page) {
            if (page->currentUrl() == url) {
                return page;
            }
        }
    }
Laurent Montel's avatar
Laurent Montel committed
88
    return Q_NULLPTR;
89 90
}

91 92
void SieveEditorMainWidget::updateStackedWidget()
{
Laurent Montel's avatar
Laurent Montel committed
93
    if (mTabWidget->count() == 0) {
94 95 96 97 98 99
        mStackedWidget->setCurrentWidget(mEditorEmptyLabel);
    } else {
        mStackedWidget->setCurrentWidget(mTabWidget);
    }
}

Laurent Montel's avatar
Laurent Montel committed
100
void SieveEditorMainWidget::slotScriptDeleted(const QUrl &url)
Laurent Montel's avatar
Laurent Montel committed
101 102 103
{
    QWidget *page = hasExistingPage(url);
    if (page) {
Laurent Montel's avatar
Laurent Montel committed
104
        mTabWidget->removeTab(mTabWidget->indexOf(page));
Laurent Montel's avatar
Laurent Montel committed
105 106
        delete page;
    }
107
    updateStackedWidget();
Laurent Montel's avatar
Laurent Montel committed
108 109
}

Laurent Montel's avatar
Laurent Montel committed
110
void SieveEditorMainWidget::slotCreateScriptPage(const QUrl &url, const QStringList &capabilities, bool isNewScript)
Laurent Montel's avatar
Laurent Montel committed
111
{
112 113
    QWidget *page = hasExistingPage(url);
    if (page) {
Laurent Montel's avatar
Laurent Montel committed
114
        mTabWidget->setCurrentWidget(page);
115 116
    } else {
        SieveEditorPageWidget *editor = new SieveEditorPageWidget;
Laurent Montel's avatar
Laurent Montel committed
117
        connect(editor, &SieveEditorPageWidget::refreshList, this, &SieveEditorMainWidget::updateScriptList);
Laurent Montel's avatar
Laurent Montel committed
118
        connect(editor, &SieveEditorPageWidget::scriptModified, this, &SieveEditorMainWidget::slotScriptModified);
Laurent Montel's avatar
Laurent Montel committed
119
        connect(editor, &SieveEditorPageWidget::modeEditorChanged, this, &SieveEditorMainWidget::modeEditorChanged);
Laurent Montel's avatar
Laurent Montel committed
120 121 122
        connect(editor, &SieveEditorPageWidget::undoAvailable, this, &SieveEditorMainWidget::undoAvailable);
        connect(editor, &SieveEditorPageWidget::redoAvailable, this, &SieveEditorMainWidget::redoAvailable);
        connect(editor, &SieveEditorPageWidget::copyAvailable, this, &SieveEditorMainWidget::copyAvailable);
123 124
        editor->setIsNewScript(isNewScript);
        editor->loadScript(url, capabilities);
Laurent Montel's avatar
Laurent Montel committed
125 126
        mTabWidget->addTab(editor, url.fileName());
        mTabWidget->setCurrentWidget(editor);
Laurent Montel's avatar
Laurent Montel committed
127
        if (isNewScript) {
Laurent Montel's avatar
Laurent Montel committed
128
            editor->uploadScript(false, true);
Laurent Montel's avatar
Laurent Montel committed
129
        }
130
        updateStackedWidget();
131
    }
Laurent Montel's avatar
Laurent Montel committed
132 133
}

Laurent Montel's avatar
Laurent Montel committed
134 135 136 137 138 139 140 141 142
void SieveEditorMainWidget::createNewScript()
{
    mScriptManagerWidget->slotCreateNewScript();
}

void SieveEditorMainWidget::deleteScript()
{
    mScriptManagerWidget->slotDeleteScript();
}
Laurent Montel's avatar
Laurent Montel committed
143 144 145 146 147

void SieveEditorMainWidget::updateServerList()
{
    mScriptManagerWidget->updateServerList();
}
Laurent Montel's avatar
Laurent Montel committed
148 149 150 151 152

void SieveEditorMainWidget::editScript()
{
    mScriptManagerWidget->editScript();
}
153 154 155 156 157

void SieveEditorMainWidget::desactivateScript()
{
    mScriptManagerWidget->desactivateScript();
}
Laurent Montel's avatar
Laurent Montel committed
158 159 160 161 162

void SieveEditorMainWidget::refreshList()
{
    mScriptManagerWidget->refreshList();
}
Laurent Montel's avatar
Laurent Montel committed
163

Laurent Montel's avatar
Laurent Montel committed
164
void SieveEditorMainWidget::uploadScript()
Laurent Montel's avatar
Laurent Montel committed
165
{
Laurent Montel's avatar
Laurent Montel committed
166
    QWidget *w = mTabWidget->currentWidget();
Laurent Montel's avatar
Laurent Montel committed
167 168 169
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
Laurent Montel's avatar
Laurent Montel committed
170
            page->uploadScript();
Laurent Montel's avatar
Laurent Montel committed
171 172 173
        }
    }
}
174

175
bool SieveEditorMainWidget::needToSaveScript()
176
{
177
    bool scriptSaved = false;
Laurent Montel's avatar
Laurent Montel committed
178
    for (int i = 0; i < mTabWidget->count(); ++i) {
Laurent Montel's avatar
Laurent Montel committed
179
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(i));
180
        if (page) {
181
            const bool result = page->needToSaveScript();
Laurent Montel's avatar
Laurent Montel committed
182
            if (result) {
183
                scriptSaved = true;
Laurent Montel's avatar
Laurent Montel committed
184
            }
185 186
        }
    }
187
    return scriptSaved;
188
}
Laurent Montel's avatar
Laurent Montel committed
189

Laurent Montel's avatar
Laurent Montel committed
190
QTabWidget *SieveEditorMainWidget::tabWidget() const
Laurent Montel's avatar
Laurent Montel committed
191
{
Laurent Montel's avatar
Laurent Montel committed
192
    return mTabWidget;
Laurent Montel's avatar
Laurent Montel committed
193
}
194

Laurent Montel's avatar
Laurent Montel committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
bool SieveEditorMainWidget::isUndoAvailable() const
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->isUndoAvailable();
        }
    }
    return false;
}

bool SieveEditorMainWidget::isRedoAvailable() const
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->isRedoAvailable();
        }
    }
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
219 220 221 222 223 224 225 226 227 228 229 230
bool SieveEditorMainWidget::isWordWrap() const
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->isWordWrap();
        }
    }
    return false;
}

231 232 233 234 235 236 237 238 239 240 241 242
bool SieveEditorMainWidget::hasSelection() const
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->hasSelection();
        }
    }
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
243 244 245 246 247 248 249 250 251 252 253
void SieveEditorMainWidget::slotSelectAll()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->selectAll();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
void SieveEditorMainWidget::slotCopy()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->copy();
        }
    }
}

void SieveEditorMainWidget::slotPaste()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->paste();
        }
    }
}

void SieveEditorMainWidget::slotCut()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->cut();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
void SieveEditorMainWidget::slotUndo()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->undo();
        }
    }
}

void SieveEditorMainWidget::slotRedo()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->redo();
        }
    }
}

309 310 311 312 313 314 315 316 317 318 319
void SieveEditorMainWidget::slotGoToLine()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->goToLine();
        }
    }
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
void SieveEditorMainWidget::slotFind()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->find();
        }
    }
}

void SieveEditorMainWidget::slotReplace()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->replace();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
342
void SieveEditorMainWidget::slotShareScript()
Laurent Montel's avatar
Laurent Montel committed
343 344 345 346 347
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
Laurent Montel's avatar
Laurent Montel committed
348
            page->shareScript();
Laurent Montel's avatar
Laurent Montel committed
349 350 351 352
        }
    }
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
void SieveEditorMainWidget::slotAutoGenerateScript()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->autoGenerateScript();
        }
    }
}

void SieveEditorMainWidget::slotCheckSyntax()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->checkSyntax();
        }
    }
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
void SieveEditorMainWidget::slotComment()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->comment();
        }
    }
}

void SieveEditorMainWidget::slotUncomment()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->uncomment();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
397 398 399 400 401 402 403 404 405 406 407
void SieveEditorMainWidget::slotImport()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->import();
        }
    }
}

408 409 410 411 412 413 414 415 416 417 418
void SieveEditorMainWidget::slotCheckSpelling()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->checkSpelling();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
419 420 421 422 423 424 425 426 427 428 429
void SieveEditorMainWidget::slotSaveAs()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->saveAs();
        }
    }
}

430 431 432 433 434 435 436 437 438 439 440
void SieveEditorMainWidget::slotReverseCase()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->reverseCase();
        }
    }
}

441 442 443 444 445 446 447 448 449 450 451
void SieveEditorMainWidget::slotDebugSieveScript()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->debugSieveScript();
        }
    }
}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
void SieveEditorMainWidget::slotUpperCase()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->upperCase();
        }
    }
}

void SieveEditorMainWidget::slotSentenceCase()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->sentenceCase();
        }
    }
}

void SieveEditorMainWidget::slotLowerCase()
{
    QWidget *w = mTabWidget->currentWidget();
Laurent Montel's avatar
Laurent Montel committed
477 478 479 480 481 482
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->lowerCase();
        }
    }
483 484
}

Laurent Montel's avatar
Laurent Montel committed
485
void SieveEditorMainWidget::slotScriptModified(bool modified, SieveEditorPageWidget *page)
486 487 488 489 490 491
{
    const int index = mTabWidget->indexOf(page);
    if (index >= 0) {
        if (!mScriptColor.isValid()) {
            slotGeneralPaletteChanged();
        }
Laurent Montel's avatar
Laurent Montel committed
492
        mTabWidget->tabBar()->setTabTextColor(index, modified ? mModifiedScriptColor : mScriptColor);
493 494 495
    }
}

Laurent Montel's avatar
Laurent Montel committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
void SieveEditorMainWidget::slotZoomIn()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->zoomIn();
        }
    }
}

void SieveEditorMainWidget::slotZoomOut()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->zoomOut();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
518 519 520 521 522 523 524 525 526 527 528
void SieveEditorMainWidget::slotZoomReset()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->zoomReset();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
529 530 531 532 533 534 535 536 537 538 539
void SieveEditorMainWidget::slotWordWrap(bool state)
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->wordWrap(state);
        }
    }
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
void SieveEditorMainWidget::slotPrintPreview()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->printPreview();
        }
    }
}

void SieveEditorMainWidget::slotPrint()
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            page->print();
        }
    }
}

562 563 564 565 566 567
void SieveEditorMainWidget::slotGeneralPaletteChanged()
{
    const QPalette pal = palette();
    mScriptColor = pal.text().color();
    mModifiedScriptColor = pal.text().color();

Laurent Montel's avatar
Laurent Montel committed
568 569
    const KColorScheme scheme(QPalette::Active, KColorScheme::View);
    mModifiedScriptColor = scheme.foreground(KColorScheme::NegativeText).color();
570
}
Laurent Montel's avatar
Laurent Montel committed
571 572 573 574 575 576 577 578

void SieveEditorMainWidget::slotTabCloseRequested(int index)
{
    SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(index));
    if (page) {
        if (page->isModified()) {
            const int result = KMessageBox::questionYesNoCancel(this, i18n("Script was modified. Do you want to save before closing?"), i18n("Close script"));
            if (result == KMessageBox::Yes) {
Laurent Montel's avatar
Laurent Montel committed
579
                page->uploadScript();
Laurent Montel's avatar
Laurent Montel committed
580 581 582 583 584 585 586
            } else if (result == KMessageBox::Cancel) {
                return;
            }
        }
        mTabWidget->removeTab(index);
        delete page;
    }
587
    updateStackedWidget();
Laurent Montel's avatar
Laurent Montel committed
588
}
589

590 591
void SieveEditorMainWidget::slotTabCloseAllRequested()
{
Laurent Montel's avatar
Laurent Montel committed
592 593 594
    for (int i = mTabWidget->count() - 1; i >= 0; --i) {
        slotTabCloseRequested(i);
    }
595 596
}

597 598
void SieveEditorMainWidget::slotTabRemoveAllExclude(int index)
{
Laurent Montel's avatar
Laurent Montel committed
599
    for (int i = mTabWidget->count() - 1; i >= 0; --i) {
600 601 602 603 604 605
        if (i == index) {
            continue;
        }
        slotTabCloseRequested(i);
    }
}
606 607 608 609 610 611 612 613 614 615 616 617

KSieveUi::SieveEditorWidget::EditorMode SieveEditorMainWidget::pageMode() const
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->pageMode();
        }
    }
    return KSieveUi::SieveEditorWidget::Unknown;
}
Laurent Montel's avatar
Laurent Montel committed
618

Laurent Montel's avatar
Laurent Montel committed
619
void SieveEditorMainWidget::openBookmarkUrl(const QUrl &url)
620 621 622 623 624
{
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
Laurent Montel's avatar
Laurent Montel committed
625
            page->openBookmarkUrl(url);
626 627 628 629
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
630 631
QString SieveEditorMainWidget::currentHelpTitle() const
{
Laurent Montel's avatar
Laurent Montel committed
632 633 634 635 636 637 638
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->currentHelpTitle();
        }
    }
Laurent Montel's avatar
Laurent Montel committed
639 640 641 642 643
    return QString();
}

QUrl SieveEditorMainWidget::currentHelpUrl() const
{
Laurent Montel's avatar
Laurent Montel committed
644 645 646 647 648 649 650
    QWidget *w = mTabWidget->currentWidget();
    if (w) {
        SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
        if (page) {
            return page->currentHelpUrl();
        }
    }
Laurent Montel's avatar
Laurent Montel committed
651 652
    return QUrl();
}