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
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>
32 #include <KColorScheme>
33 #include <KMessageBox>
36 #include <QStackedWidget>
39 SieveEditorMainWidget::SieveEditorMainWidget(QWidget
*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());
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
));
83 if (page
->currentUrl() == url
) {
91 void SieveEditorMainWidget::updateStackedWidget()
93 if (mTabWidget
->count() == 0) {
94 mStackedWidget
->setCurrentWidget(mEditorEmptyLabel
);
96 mStackedWidget
->setCurrentWidget(mTabWidget
);
100 void SieveEditorMainWidget::slotScriptDeleted(const QUrl
&url
)
102 QWidget
*page
= hasExistingPage(url
);
104 mTabWidget
->removeTab(mTabWidget
->indexOf(page
));
107 updateStackedWidget();
110 void SieveEditorMainWidget::slotCreateScriptPage(const QUrl
&url
, const QStringList
&capabilities
, bool isNewScript
)
112 QWidget
*page
= hasExistingPage(url
);
114 mTabWidget
->setCurrentWidget(page
);
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
);
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();
168 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
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
));
181 const bool result
= page
->needToSaveScript();
190 QTabWidget
*SieveEditorMainWidget::tabWidget() const
195 bool SieveEditorMainWidget::isUndoAvailable() const
197 QWidget
*w
= mTabWidget
->currentWidget();
199 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
201 return page
->isUndoAvailable();
207 bool SieveEditorMainWidget::isRedoAvailable() const
209 QWidget
*w
= mTabWidget
->currentWidget();
211 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
213 return page
->isRedoAvailable();
219 bool SieveEditorMainWidget::isWordWrap() const
221 QWidget
*w
= mTabWidget
->currentWidget();
223 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
225 return page
->isWordWrap();
231 bool SieveEditorMainWidget::hasSelection() const
233 QWidget
*w
= mTabWidget
->currentWidget();
235 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
237 return page
->hasSelection();
243 void SieveEditorMainWidget::slotSelectAll()
245 QWidget
*w
= mTabWidget
->currentWidget();
247 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
254 void SieveEditorMainWidget::slotCopy()
256 QWidget
*w
= mTabWidget
->currentWidget();
258 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
265 void SieveEditorMainWidget::slotPaste()
267 QWidget
*w
= mTabWidget
->currentWidget();
269 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
276 void SieveEditorMainWidget::slotCut()
278 QWidget
*w
= mTabWidget
->currentWidget();
280 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
287 void SieveEditorMainWidget::slotUndo()
289 QWidget
*w
= mTabWidget
->currentWidget();
291 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
298 void SieveEditorMainWidget::slotRedo()
300 QWidget
*w
= mTabWidget
->currentWidget();
302 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
309 void SieveEditorMainWidget::slotGoToLine()
311 QWidget
*w
= mTabWidget
->currentWidget();
313 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
320 void SieveEditorMainWidget::slotFind()
322 QWidget
*w
= mTabWidget
->currentWidget();
324 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
331 void SieveEditorMainWidget::slotReplace()
333 QWidget
*w
= mTabWidget
->currentWidget();
335 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
342 void SieveEditorMainWidget::slotShare()
344 QWidget
*w
= mTabWidget
->currentWidget();
346 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
353 void SieveEditorMainWidget::slotAutoGenerateScript()
355 QWidget
*w
= mTabWidget
->currentWidget();
357 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
359 page
->autoGenerateScript();
364 void SieveEditorMainWidget::slotCheckSyntax()
366 QWidget
*w
= mTabWidget
->currentWidget();
368 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
375 void SieveEditorMainWidget::slotComment()
377 QWidget
*w
= mTabWidget
->currentWidget();
379 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
386 void SieveEditorMainWidget::slotUncomment()
388 QWidget
*w
= mTabWidget
->currentWidget();
390 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
397 void SieveEditorMainWidget::slotImport()
399 QWidget
*w
= mTabWidget
->currentWidget();
401 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
408 void SieveEditorMainWidget::slotCheckSpelling()
410 QWidget
*w
= mTabWidget
->currentWidget();
412 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
414 page
->checkSpelling();
419 void SieveEditorMainWidget::slotSaveAs()
421 QWidget
*w
= mTabWidget
->currentWidget();
423 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
430 void SieveEditorMainWidget::slotReverseCase()
432 QWidget
*w
= mTabWidget
->currentWidget();
434 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
441 void SieveEditorMainWidget::slotDebugSieveScript()
443 QWidget
*w
= mTabWidget
->currentWidget();
445 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
447 page
->debugSieveScript();
452 void SieveEditorMainWidget::slotUpperCase()
454 QWidget
*w
= mTabWidget
->currentWidget();
456 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
463 void SieveEditorMainWidget::slotSentenceCase()
465 QWidget
*w
= mTabWidget
->currentWidget();
467 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
469 page
->sentenceCase();
474 void SieveEditorMainWidget::slotLowerCase()
476 QWidget
*w
= mTabWidget
->currentWidget();
478 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
485 void SieveEditorMainWidget::slotScriptModified(bool modified
, SieveEditorPageWidget
*page
)
487 const int index
= mTabWidget
->indexOf(page
);
489 if (!mScriptColor
.isValid()) {
490 slotGeneralPaletteChanged();
492 mTabWidget
->tabBar()->setTabTextColor(index
, modified
? mModifiedScriptColor
: mScriptColor
);
496 void SieveEditorMainWidget::slotZoomIn()
498 QWidget
*w
= mTabWidget
->currentWidget();
500 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
507 void SieveEditorMainWidget::slotZoomOut()
509 QWidget
*w
= mTabWidget
->currentWidget();
511 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
518 void SieveEditorMainWidget::slotZoomReset()
520 QWidget
*w
= mTabWidget
->currentWidget();
522 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
529 void SieveEditorMainWidget::slotWordWrap(bool state
)
531 QWidget
*w
= mTabWidget
->currentWidget();
533 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
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
));
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
) {
562 mTabWidget
->removeTab(index
);
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
) {
581 slotTabCloseRequested(i
);
585 KSieveUi::SieveEditorWidget::EditorMode
SieveEditorMainWidget::pageMode() const
587 QWidget
*w
= mTabWidget
->currentWidget();
589 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
591 return page
->pageMode();
594 return KSieveUi::SieveEditorWidget::Unknown
;
597 void SieveEditorMainWidget::openBookmarkUrl(const QUrl
&url
)
599 QWidget
*w
= mTabWidget
->currentWidget();
601 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
603 page
->openBookmarkUrl(url
);
608 QString
SieveEditorMainWidget::currentHelpTitle() const
610 QWidget
*w
= mTabWidget
->currentWidget();
612 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
614 return page
->currentHelpTitle();
620 QUrl
SieveEditorMainWidget::currentHelpUrl() const
622 QWidget
*w
= mTabWidget
->currentWidget();
624 SieveEditorPageWidget
*page
= qobject_cast
<SieveEditorPageWidget
*>(w
);
626 return page
->currentHelpUrl();