Fix Bug 358855 - message list column widths lost when program closed
[kdepim.git] / sieveeditor / sieveeditormainwidget.cpp
blob1f57383553ac15d4765cf73df221d6b4baabe9b3
1 /*
2 Copyright (c) 2013-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
17 02110-1301, USA.
21 #include "sieveeditormainwidget.h"
22 #include "sieveeditorscriptmanagerwidget.h"
23 #include "sieveeditorpagewidget.h"
24 #include "sieveeditortabwidget.h"
25 #include "sieveeditoremptytabwidgetlabel.h"
26 #include "ksieveui/sieveeditor.h"
28 #include <KLocalizedString>
29 #include <KConfigGroup>
30 #include <KSharedConfig>
31 #include <QTabWidget>
32 #include <KColorScheme>
33 #include <KMessageBox>
35 #include <QSplitter>
36 #include <QStackedWidget>
37 #include <QTabBar>
39 SieveEditorMainWidget::SieveEditorMainWidget(QWidget *parent)
40 : QSplitter(parent)
42 mStackedWidget = new QStackedWidget(this);
43 mStackedWidget->setObjectName(QStringLiteral("stackedwidget"));
45 mEditorEmptyLabel = new SieveEditorEmptyTabWidgetLabel;
47 mTabWidget = new SieveEditorTabWidget;
48 connect(mTabWidget, &SieveEditorTabWidget::tabCloseRequestedIndex, this, &SieveEditorMainWidget::slotTabCloseRequested);
49 connect(mTabWidget, &SieveEditorTabWidget::tabRemoveAllExclude, this, &SieveEditorMainWidget::slotTabRemoveAllExclude);
50 connect(mTabWidget, &SieveEditorTabWidget::tabCloseAllTab, this, &SieveEditorMainWidget::slotTabCloseAllRequested);
52 mStackedWidget->addWidget(mTabWidget);
53 mStackedWidget->addWidget(mEditorEmptyLabel);
54 addWidget(mStackedWidget);
56 mScriptManagerWidget = new SieveEditorScriptManagerWidget;
57 connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::createScriptPage, this, &SieveEditorMainWidget::slotCreateScriptPage);
58 connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::updateButtons, this, &SieveEditorMainWidget::updateButtons);
59 connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::scriptDeleted, this, &SieveEditorMainWidget::slotScriptDeleted);
60 connect(mScriptManagerWidget, &SieveEditorScriptManagerWidget::serverSieveFound, this, &SieveEditorMainWidget::serverSieveFound);
61 connect(this, &SieveEditorMainWidget::updateScriptList, mScriptManagerWidget, &SieveEditorScriptManagerWidget::slotRefreshList);
62 addWidget(mScriptManagerWidget);
63 setChildrenCollapsible(false);
64 QList<int> splitterSizes;
65 splitterSizes << 80 << 20;
66 KConfigGroup myGroup(KSharedConfig::openConfig(), "SieveEditorMainWidget");
67 setSizes(myGroup.readEntry("mainSplitter", splitterSizes));
68 updateStackedWidget();
71 SieveEditorMainWidget::~SieveEditorMainWidget()
73 KConfigGroup myGroup(KSharedConfig::openConfig(), "SieveEditorMainWidget");
74 myGroup.writeEntry("mainSplitter", sizes());
75 myGroup.sync();
78 QWidget *SieveEditorMainWidget::hasExistingPage(const QUrl &url)
80 for (int i = 0; i < mTabWidget->count(); ++i) {
81 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(i));
82 if (page) {
83 if (page->currentUrl() == url) {
84 return page;
88 return Q_NULLPTR;
91 void SieveEditorMainWidget::updateStackedWidget()
93 if (mTabWidget->count() == 0) {
94 mStackedWidget->setCurrentWidget(mEditorEmptyLabel);
95 } else {
96 mStackedWidget->setCurrentWidget(mTabWidget);
100 void SieveEditorMainWidget::slotScriptDeleted(const QUrl &url)
102 QWidget *page = hasExistingPage(url);
103 if (page) {
104 mTabWidget->removeTab(mTabWidget->indexOf(page));
105 delete page;
107 updateStackedWidget();
110 void SieveEditorMainWidget::slotCreateScriptPage(const QUrl &url, const QStringList &capabilities, bool isNewScript)
112 QWidget *page = hasExistingPage(url);
113 if (page) {
114 mTabWidget->setCurrentWidget(page);
115 } else {
116 SieveEditorPageWidget *editor = new SieveEditorPageWidget;
117 connect(editor, &SieveEditorPageWidget::refreshList, this, &SieveEditorMainWidget::updateScriptList);
118 connect(editor, &SieveEditorPageWidget::scriptModified, this, &SieveEditorMainWidget::slotScriptModified);
119 connect(editor, &SieveEditorPageWidget::modeEditorChanged, this, &SieveEditorMainWidget::modeEditorChanged);
120 connect(editor, &SieveEditorPageWidget::undoAvailable, this, &SieveEditorMainWidget::undoAvailable);
121 connect(editor, &SieveEditorPageWidget::redoAvailable, this, &SieveEditorMainWidget::redoAvailable);
122 connect(editor, &SieveEditorPageWidget::copyAvailable, this, &SieveEditorMainWidget::copyAvailable);
123 editor->setIsNewScript(isNewScript);
124 editor->loadScript(url, capabilities);
125 mTabWidget->addTab(editor, url.fileName());
126 mTabWidget->setCurrentWidget(editor);
127 if (isNewScript) {
128 editor->uploadScript(false, true);
130 updateStackedWidget();
134 void SieveEditorMainWidget::createNewScript()
136 mScriptManagerWidget->slotCreateNewScript();
139 void SieveEditorMainWidget::deleteScript()
141 mScriptManagerWidget->slotDeleteScript();
144 void SieveEditorMainWidget::updateServerList()
146 mScriptManagerWidget->updateServerList();
149 void SieveEditorMainWidget::editScript()
151 mScriptManagerWidget->editScript();
154 void SieveEditorMainWidget::desactivateScript()
156 mScriptManagerWidget->desactivateScript();
159 void SieveEditorMainWidget::refreshList()
161 mScriptManagerWidget->refreshList();
164 void SieveEditorMainWidget::uploadScript()
166 QWidget *w = mTabWidget->currentWidget();
167 if (w) {
168 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
169 if (page) {
170 page->uploadScript();
175 bool SieveEditorMainWidget::needToSaveScript()
177 bool scriptSaved = false;
178 for (int i = 0; i < mTabWidget->count(); ++i) {
179 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(i));
180 if (page) {
181 const bool result = page->needToSaveScript();
182 if (result) {
183 scriptSaved = true;
187 return scriptSaved;
190 QTabWidget *SieveEditorMainWidget::tabWidget() const
192 return mTabWidget;
195 bool SieveEditorMainWidget::isUndoAvailable() const
197 QWidget *w = mTabWidget->currentWidget();
198 if (w) {
199 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
200 if (page) {
201 return page->isUndoAvailable();
204 return false;
207 bool SieveEditorMainWidget::isRedoAvailable() const
209 QWidget *w = mTabWidget->currentWidget();
210 if (w) {
211 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
212 if (page) {
213 return page->isRedoAvailable();
216 return false;
219 bool SieveEditorMainWidget::isWordWrap() const
221 QWidget *w = mTabWidget->currentWidget();
222 if (w) {
223 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
224 if (page) {
225 return page->isWordWrap();
228 return false;
231 bool SieveEditorMainWidget::hasSelection() const
233 QWidget *w = mTabWidget->currentWidget();
234 if (w) {
235 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
236 if (page) {
237 return page->hasSelection();
240 return false;
243 void SieveEditorMainWidget::slotSelectAll()
245 QWidget *w = mTabWidget->currentWidget();
246 if (w) {
247 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
248 if (page) {
249 page->selectAll();
254 void SieveEditorMainWidget::slotCopy()
256 QWidget *w = mTabWidget->currentWidget();
257 if (w) {
258 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
259 if (page) {
260 page->copy();
265 void SieveEditorMainWidget::slotPaste()
267 QWidget *w = mTabWidget->currentWidget();
268 if (w) {
269 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
270 if (page) {
271 page->paste();
276 void SieveEditorMainWidget::slotCut()
278 QWidget *w = mTabWidget->currentWidget();
279 if (w) {
280 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
281 if (page) {
282 page->cut();
287 void SieveEditorMainWidget::slotUndo()
289 QWidget *w = mTabWidget->currentWidget();
290 if (w) {
291 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
292 if (page) {
293 page->undo();
298 void SieveEditorMainWidget::slotRedo()
300 QWidget *w = mTabWidget->currentWidget();
301 if (w) {
302 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
303 if (page) {
304 page->redo();
309 void SieveEditorMainWidget::slotGoToLine()
311 QWidget *w = mTabWidget->currentWidget();
312 if (w) {
313 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
314 if (page) {
315 page->goToLine();
320 void SieveEditorMainWidget::slotFind()
322 QWidget *w = mTabWidget->currentWidget();
323 if (w) {
324 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
325 if (page) {
326 page->find();
331 void SieveEditorMainWidget::slotReplace()
333 QWidget *w = mTabWidget->currentWidget();
334 if (w) {
335 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
336 if (page) {
337 page->replace();
342 void SieveEditorMainWidget::slotShare()
344 QWidget *w = mTabWidget->currentWidget();
345 if (w) {
346 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
347 if (page) {
348 page->share();
353 void SieveEditorMainWidget::slotAutoGenerateScript()
355 QWidget *w = mTabWidget->currentWidget();
356 if (w) {
357 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
358 if (page) {
359 page->autoGenerateScript();
364 void SieveEditorMainWidget::slotCheckSyntax()
366 QWidget *w = mTabWidget->currentWidget();
367 if (w) {
368 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
369 if (page) {
370 page->checkSyntax();
375 void SieveEditorMainWidget::slotComment()
377 QWidget *w = mTabWidget->currentWidget();
378 if (w) {
379 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
380 if (page) {
381 page->comment();
386 void SieveEditorMainWidget::slotUncomment()
388 QWidget *w = mTabWidget->currentWidget();
389 if (w) {
390 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
391 if (page) {
392 page->uncomment();
397 void SieveEditorMainWidget::slotImport()
399 QWidget *w = mTabWidget->currentWidget();
400 if (w) {
401 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
402 if (page) {
403 page->import();
408 void SieveEditorMainWidget::slotCheckSpelling()
410 QWidget *w = mTabWidget->currentWidget();
411 if (w) {
412 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
413 if (page) {
414 page->checkSpelling();
419 void SieveEditorMainWidget::slotSaveAs()
421 QWidget *w = mTabWidget->currentWidget();
422 if (w) {
423 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
424 if (page) {
425 page->saveAs();
430 void SieveEditorMainWidget::slotReverseCase()
432 QWidget *w = mTabWidget->currentWidget();
433 if (w) {
434 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
435 if (page) {
436 page->reverseCase();
441 void SieveEditorMainWidget::slotDebugSieveScript()
443 QWidget *w = mTabWidget->currentWidget();
444 if (w) {
445 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
446 if (page) {
447 page->debugSieveScript();
452 void SieveEditorMainWidget::slotUpperCase()
454 QWidget *w = mTabWidget->currentWidget();
455 if (w) {
456 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
457 if (page) {
458 page->upperCase();
463 void SieveEditorMainWidget::slotSentenceCase()
465 QWidget *w = mTabWidget->currentWidget();
466 if (w) {
467 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
468 if (page) {
469 page->sentenceCase();
474 void SieveEditorMainWidget::slotLowerCase()
476 QWidget *w = mTabWidget->currentWidget();
477 if (w) {
478 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
479 if (page) {
480 page->lowerCase();
485 void SieveEditorMainWidget::slotScriptModified(bool modified, SieveEditorPageWidget *page)
487 const int index = mTabWidget->indexOf(page);
488 if (index >= 0) {
489 if (!mScriptColor.isValid()) {
490 slotGeneralPaletteChanged();
492 mTabWidget->tabBar()->setTabTextColor(index, modified ? mModifiedScriptColor : mScriptColor);
496 void SieveEditorMainWidget::slotZoomIn()
498 QWidget *w = mTabWidget->currentWidget();
499 if (w) {
500 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
501 if (page) {
502 page->zoomIn();
507 void SieveEditorMainWidget::slotZoomOut()
509 QWidget *w = mTabWidget->currentWidget();
510 if (w) {
511 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
512 if (page) {
513 page->zoomOut();
518 void SieveEditorMainWidget::slotZoomReset()
520 QWidget *w = mTabWidget->currentWidget();
521 if (w) {
522 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
523 if (page) {
524 page->zoomReset();
529 void SieveEditorMainWidget::slotWordWrap(bool state)
531 QWidget *w = mTabWidget->currentWidget();
532 if (w) {
533 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
534 if (page) {
535 page->wordWrap(state);
540 void SieveEditorMainWidget::slotGeneralPaletteChanged()
542 const QPalette pal = palette();
543 mScriptColor = pal.text().color();
544 mModifiedScriptColor = pal.text().color();
546 const KColorScheme scheme(QPalette::Active, KColorScheme::View);
547 mModifiedScriptColor = scheme.foreground(KColorScheme::NegativeText).color();
550 void SieveEditorMainWidget::slotTabCloseRequested(int index)
552 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(mTabWidget->widget(index));
553 if (page) {
554 if (page->isModified()) {
555 const int result = KMessageBox::questionYesNoCancel(this, i18n("Script was modified. Do you want to save before closing?"), i18n("Close script"));
556 if (result == KMessageBox::Yes) {
557 page->uploadScript();
558 } else if (result == KMessageBox::Cancel) {
559 return;
562 mTabWidget->removeTab(index);
563 delete page;
565 updateStackedWidget();
568 void SieveEditorMainWidget::slotTabCloseAllRequested()
570 for (int i = mTabWidget->count() - 1; i >= 0; --i) {
571 slotTabCloseRequested(i);
575 void SieveEditorMainWidget::slotTabRemoveAllExclude(int index)
577 for (int i = mTabWidget->count() - 1; i >= 0; --i) {
578 if (i == index) {
579 continue;
581 slotTabCloseRequested(i);
585 KSieveUi::SieveEditorWidget::EditorMode SieveEditorMainWidget::pageMode() const
587 QWidget *w = mTabWidget->currentWidget();
588 if (w) {
589 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
590 if (page) {
591 return page->pageMode();
594 return KSieveUi::SieveEditorWidget::Unknown;
597 void SieveEditorMainWidget::openBookmarkUrl(const QUrl &url)
599 QWidget *w = mTabWidget->currentWidget();
600 if (w) {
601 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
602 if (page) {
603 page->openBookmarkUrl(url);
608 QString SieveEditorMainWidget::currentHelpTitle() const
610 QWidget *w = mTabWidget->currentWidget();
611 if (w) {
612 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
613 if (page) {
614 return page->currentHelpTitle();
617 return QString();
620 QUrl SieveEditorMainWidget::currentHelpUrl() const
622 QWidget *w = mTabWidget->currentWidget();
623 if (w) {
624 SieveEditorPageWidget *page = qobject_cast<SieveEditorPageWidget *>(w);
625 if (page) {
626 return page->currentHelpUrl();
629 return QUrl();