sieveeditormainwindow.cpp 7.86 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2013, 2014 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 "sieveeditormainwindow.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "sieveeditormainwidget.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include "sieveeditorconfiguredialog.h"
Laurent Montel's avatar
Laurent Montel committed
24
#include "serversievesettingsdialog.h"
25
#include "sieveserversettings.h"
Laurent Montel's avatar
Laurent Montel committed
26
#include "sieveeditorcentralwidget.h"
27
#include "sieveeditorglobalconfig.h"
Laurent Montel's avatar
Laurent Montel committed
28 29 30 31 32

#include <KSharedConfig>
#include <KGlobal>
#include <KLocalizedString>
#include <KConfigGroup>
33 34 35
#include <KStandardAction>
#include <KApplication>
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
36
#include <KAction>
Laurent Montel's avatar
Laurent Montel committed
37
#include <KStatusBar>
38
#include <KTabWidget>
Laurent Montel's avatar
Laurent Montel committed
39

Laurent Montel's avatar
Laurent Montel committed
40
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
41
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
42
#include <QCloseEvent>
Laurent Montel's avatar
Laurent Montel committed
43

Laurent Montel's avatar
Laurent Montel committed
44
SieveEditorMainWindow::SieveEditorMainWindow()
Laurent Montel's avatar
Laurent Montel committed
45 46
    : KXmlGuiWindow(),
      mNetworkIsDown(false)
Laurent Montel's avatar
Laurent Montel committed
47
{    
Laurent Montel's avatar
Laurent Montel committed
48
    mMainWidget = new SieveEditorCentralWidget(this);
Laurent Montel's avatar
Laurent Montel committed
49 50
    connect(mMainWidget, SIGNAL(configureClicked()), SLOT(slotConfigure()));
    connect(mMainWidget->sieveEditorMainWidget(), SIGNAL(updateButtons(bool,bool,bool,bool)), this, SLOT(slotUpdateButtons(bool,bool,bool,bool)));
51
    setCentralWidget(mMainWidget);
Laurent Montel's avatar
Laurent Montel committed
52 53 54
    setupActions();
    setupGUI();
    readConfig();
Laurent Montel's avatar
Laurent Montel committed
55 56 57
    initStatusBar();
    connect( Solid::Networking::notifier(), SIGNAL(statusChanged(Solid::Networking::Status)),
              this, SLOT(slotSystemNetworkStatusChanged(Solid::Networking::Status)) );
Laurent Montel's avatar
Laurent Montel committed
58 59
    connect(mMainWidget->sieveEditorMainWidget()->tabWidget(), SIGNAL(currentChanged(int)), SLOT(slotUpdateActions()));
    connect(mMainWidget->sieveEditorMainWidget(), SIGNAL(modeEditorChanged(KSieveUi::SieveEditorWidget::EditorMode)), SLOT(slotUpdateActions()));
Laurent Montel's avatar
Laurent Montel committed
60 61
    const Solid::Networking::Status status = Solid::Networking::status();
    slotSystemNetworkStatusChanged(status);
Laurent Montel's avatar
Laurent Montel committed
62
    slotRefreshList();
Laurent Montel's avatar
Laurent Montel committed
63 64 65 66 67 68 69 70
}

SieveEditorMainWindow::~SieveEditorMainWindow()
{
    KSharedConfig::Ptr config = KGlobal::config();

    KConfigGroup group = config->group( QLatin1String("SieveEditorMainWindow") );
    group.writeEntry( "Size", size() );
71 72
    if (SieveEditorGlobalConfig::self()->closeWallet())
        SieveServerSettings::self()->closeWallet();
Laurent Montel's avatar
Laurent Montel committed
73 74
}

Laurent Montel's avatar
Laurent Montel committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
void SieveEditorMainWindow::initStatusBar()
{
    mStatusBarInfo = new QLabel;
    statusBar()->insertWidget(0, mStatusBarInfo, 4);
}

void SieveEditorMainWindow::slotSystemNetworkStatusChanged(Solid::Networking::Status status)
{
    if ( status == Solid::Networking::Connected || status == Solid::Networking::Unknown) {
        mNetworkIsDown = false;
        mStatusBarInfo->setText(i18n("Network is Up."));
    } else {
        mNetworkIsDown = true;
        mStatusBarInfo->setText(i18n("Network is Down."));
    }
Laurent Montel's avatar
Laurent Montel committed
90
    mMainWidget->sieveEditorMainWidget()->setEnabled(!mNetworkIsDown);
Laurent Montel's avatar
Laurent Montel committed
91
    slotUpdateActions();
Laurent Montel's avatar
Laurent Montel committed
92 93
}

Laurent Montel's avatar
Laurent Montel committed
94 95 96 97
void SieveEditorMainWindow::slotUpdateButtons(bool newScriptAction, bool editScriptAction, bool deleteScriptAction, bool desactivateScriptAction)
{
    mDeleteScript->setEnabled(deleteScriptAction);
    mNewScript->setEnabled(newScriptAction);
Laurent Montel's avatar
Laurent Montel committed
98
    mEditScript->setEnabled(editScriptAction);
99
    mDesactivateScript->setEnabled(desactivateScriptAction);
Laurent Montel's avatar
Laurent Montel committed
100 101
}

Laurent Montel's avatar
Laurent Montel committed
102 103 104 105
void SieveEditorMainWindow::readConfig()
{
    KSharedConfig::Ptr config = KGlobal::config();
    KConfigGroup group = KConfigGroup( config, "SieveEditorMainWindow" );
Laurent Montel's avatar
Laurent Montel committed
106
    const QSize sizeDialog = group.readEntry( "Size", QSize(800,600) );
Laurent Montel's avatar
Laurent Montel committed
107 108 109 110 111 112 113
    if ( sizeDialog.isValid() ) {
        resize( sizeDialog );
    }
}

void SieveEditorMainWindow::setupActions()
{
Laurent Montel's avatar
Laurent Montel committed
114 115
    KActionCollection* ac=actionCollection();

Laurent Montel's avatar
Laurent Montel committed
116
    KStandardAction::quit(this, SLOT(close()), ac );
Laurent Montel's avatar
Laurent Montel committed
117
    KStandardAction::preferences( this, SLOT(slotConfigure()), ac );
Laurent Montel's avatar
Laurent Montel committed
118
    mSaveScript = KStandardAction::save( this, SLOT(slotSaveScript()), ac );
Laurent Montel's avatar
Laurent Montel committed
119
    mSaveScript->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
120

Laurent Montel's avatar
Laurent Montel committed
121 122
    KAction *act = ac->addAction(QLatin1String("add_server_sieve"), this, SLOT(slotAddServerSieve()));
    act->setText(i18n("Add Server Sieve..."));
Laurent Montel's avatar
Laurent Montel committed
123

Laurent Montel's avatar
Laurent Montel committed
124 125
    mDeleteScript = ac->addAction(QLatin1String("delete_script"), this, SLOT(slotDeleteScript()));
    mDeleteScript->setText(i18n("Delete Script"));
Laurent Montel's avatar
Laurent Montel committed
126
    mDeleteScript->setShortcut(QKeySequence( Qt::Key_Delete ));
Laurent Montel's avatar
Laurent Montel committed
127
    mDeleteScript->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
128

Laurent Montel's avatar
Laurent Montel committed
129
    mNewScript = ac->addAction(QLatin1String("create_new_script"), this, SLOT(slotCreateNewScript()));
Laurent Montel's avatar
Laurent Montel committed
130
    mNewScript->setShortcut(QKeySequence( Qt::CTRL + Qt::Key_N ));
Laurent Montel's avatar
Laurent Montel committed
131 132
    mNewScript->setText(i18n("Create New Script..."));
    mNewScript->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
133 134

    mEditScript = ac->addAction(QLatin1String("edit_script"), this, SLOT(slotEditScript()));
135
    mEditScript->setText(i18n("Edit Script"));
Laurent Montel's avatar
Laurent Montel committed
136
    mEditScript->setEnabled(false);
137 138

    mDesactivateScript = ac->addAction(QLatin1String("desactivate_script"), this, SLOT(slotDesactivateScript()));
Yuri Chornoivan's avatar
Yuri Chornoivan committed
139
    mDesactivateScript->setText(i18n("Deactivate Script"));
140
    mDesactivateScript->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
141 142 143

    mRefreshList = ac->addAction(QLatin1String("refresh_list"), this, SLOT(slotRefreshList()));
    mRefreshList->setText(i18n("Refresh List"));
Laurent Montel's avatar
Laurent Montel committed
144
    mRefreshList->setIcon(KIcon(QLatin1String("view-refresh")));
Laurent Montel's avatar
Laurent Montel committed
145
    mRefreshList->setShortcut(QKeySequence( Qt::Key_F5 ));
146

Laurent Montel's avatar
Laurent Montel committed
147
    mGoToLine = ac->addAction(QLatin1String("gotoline"), mMainWidget->sieveEditorMainWidget(), SLOT(slotGoToLine()));
148 149
    mGoToLine->setText(i18n("Go to Line"));
    mGoToLine->setIcon(KIcon(QLatin1String("go-jump")));
150
    mGoToLine->setShortcut(QKeySequence( Qt::CTRL + Qt::Key_G ));
151
    mGoToLine->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
152 153 154 155
}

void SieveEditorMainWindow::slotRefreshList()
{
Laurent Montel's avatar
Laurent Montel committed
156
    if (!mNetworkIsDown)
Laurent Montel's avatar
Laurent Montel committed
157
        mMainWidget->sieveEditorMainWidget()->refreshList();
158 159
}

Laurent Montel's avatar
Laurent Montel committed
160 161
void SieveEditorMainWindow::slotSaveScript()
{
Laurent Montel's avatar
Laurent Montel committed
162
    mMainWidget->sieveEditorMainWidget()->saveScript();
Laurent Montel's avatar
Laurent Montel committed
163 164
}

165 166
void SieveEditorMainWindow::slotDesactivateScript()
{
Laurent Montel's avatar
Laurent Montel committed
167
    mMainWidget->sieveEditorMainWidget()->desactivateScript();
Laurent Montel's avatar
Laurent Montel committed
168 169
}

Laurent Montel's avatar
Laurent Montel committed
170 171
void SieveEditorMainWindow::slotEditScript()
{
Laurent Montel's avatar
Laurent Montel committed
172
    mMainWidget->sieveEditorMainWidget()->editScript();
Laurent Montel's avatar
Laurent Montel committed
173 174
}

Laurent Montel's avatar
Laurent Montel committed
175 176
void SieveEditorMainWindow::slotCreateNewScript()
{
Laurent Montel's avatar
Laurent Montel committed
177
    mMainWidget->sieveEditorMainWidget()->createNewScript();
Laurent Montel's avatar
Laurent Montel committed
178 179 180 181
}

void SieveEditorMainWindow::slotDeleteScript()
{
Laurent Montel's avatar
Laurent Montel committed
182
    mMainWidget->sieveEditorMainWidget()->deleteScript();
Laurent Montel's avatar
Laurent Montel committed
183 184
}

Laurent Montel's avatar
Laurent Montel committed
185
void SieveEditorMainWindow::closeEvent(QCloseEvent *e)
186
{
Laurent Montel's avatar
Laurent Montel committed
187
    if (mMainWidget->sieveEditorMainWidget()->needToSaveScript()) {
Laurent Montel's avatar
Laurent Montel committed
188 189 190 191 192
        e->ignore();
        return;
    } else {
        e->accept();
    }
193 194 195 196
}

void SieveEditorMainWindow::slotConfigure()
{
Laurent Montel's avatar
Laurent Montel committed
197 198
    QPointer<SieveEditorConfigureDialog> dlg = new SieveEditorConfigureDialog(this);
    if (dlg->exec()) {
199
        dlg->saveServerSieveConfig();
Laurent Montel's avatar
Laurent Montel committed
200
        mMainWidget->sieveEditorMainWidget()->updateServerList();
Laurent Montel's avatar
Laurent Montel committed
201 202 203 204 205 206
    }
    delete dlg;
}

void SieveEditorMainWindow::slotAddServerSieve()
{
Laurent Montel's avatar
Laurent Montel committed
207
    QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this);
Laurent Montel's avatar
Laurent Montel committed
208
    if (dlg->exec()) {
Laurent Montel's avatar
Laurent Montel committed
209 210
        const SieveEditorUtil::SieveServerConfig conf = dlg->serverSieveConfig();
        SieveEditorUtil::addServerSieveConfig(conf);
Laurent Montel's avatar
Laurent Montel committed
211
        mMainWidget->sieveEditorMainWidget()->updateServerList();
Laurent Montel's avatar
Laurent Montel committed
212 213
    }
    delete dlg;
214
}
Laurent Montel's avatar
Laurent Montel committed
215 216 217

void SieveEditorMainWindow::slotUpdateActions()
{
Laurent Montel's avatar
Laurent Montel committed
218
    const bool hasPage = (mMainWidget->sieveEditorMainWidget()->tabWidget()->count()>0);
219
    mSaveScript->setEnabled(hasPage);
Laurent Montel's avatar
Laurent Montel committed
220
    mGoToLine->setEnabled(hasPage && mMainWidget->sieveEditorMainWidget()->pageMode() == KSieveUi::SieveEditorWidget::TextMode);
Laurent Montel's avatar
Laurent Montel committed
221 222
    mSaveScript->setEnabled(hasPage && !mNetworkIsDown);
    mRefreshList->setEnabled(!mNetworkIsDown);
Laurent Montel's avatar
Laurent Montel committed
223 224
}