1
/* Copyright (C) 2011 Laurent Montel <montel@kde.org>
3
* This library is free software; you can redistribute it and/or
4
* modify it under the terms of the GNU Library General Public
5
* License as published by the Free Software Foundation; either
6
* version 2 of the License, or (at your option) any later version.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Library General Public License for more details.
13
* You should have received a copy of the GNU Library General Public License
14
* along with this library; see the file COPYING.LIB. If not, write to
15
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
* Boston, MA 02110-1301, USA.
19
#include "sieveeditor.h"
21
#include <kiconloader.h>
22
#include <kmessagebox.h>
23
#include <kfiledialog.h>
27
#include <QTextStream>
29
#include <QVBoxLayout>
33
using namespace KSieveUi;
35
SieveEditor::SieveEditor( QWidget * parent )
38
setCaption( i18n( "Edit Sieve Script" ) );
39
setButtons( Ok|Cancel|User1|User2|User3 );
40
setButtonText( User1, i18n( "Check Syntax" ) );
41
setButtonGuiItem( User2, KStandardGuiItem::saveAs() );
42
setButtonText( User3, i18n( "Import..." ) );
43
setDefaultButton( Ok );
46
QWidget *mainWidget = new QWidget;
47
QVBoxLayout *lay = new QVBoxLayout;
48
mainWidget->setLayout( lay );
49
QHBoxLayout *nameLayout = new QHBoxLayout;
50
QLabel * label = new QLabel( i18n( "Script name:" ) );
51
nameLayout->addWidget( label );
52
mScriptName = new QLineEdit;
53
mScriptName->setReadOnly( true );
54
nameLayout->addWidget( mScriptName );
55
lay->addLayout( nameLayout );
57
QSplitter *splitter = new QSplitter;
58
splitter->setOrientation( Qt::Vertical );
59
lay->addWidget( splitter );
62
mTextEdit = new SieveTextEdit( splitter );
63
mDebugTextEdit = new QTextEdit;
64
mDebugTextEdit->setReadOnly( true );
65
splitter->addWidget( mTextEdit );
66
splitter->addWidget( mDebugTextEdit );
67
splitter->setSizes( size );
68
connect( mTextEdit, SIGNAL(textChanged()), SLOT(slotTextChanged()) );
69
connect( this, SIGNAL(user2Clicked()), SLOT(slotSaveAs()) );
70
connect( this, SIGNAL(user3Clicked()), SLOT(slotImport()) );
72
setMainWidget( mainWidget );
76
SieveEditor::~SieveEditor()
80
void SieveEditor::slotSaveAs()
83
QPointer<KFileDialog> fdlg( new KFileDialog( url, QString(), this) );
85
fdlg->setMode( KFile::File );
86
fdlg->setOperationMode( KFileDialog::Saving );
87
if ( fdlg->exec() == QDialog::Accepted && fdlg )
89
const QString fileName = fdlg->selectedFile();
90
if ( !saveToFile( fileName ) )
92
KMessageBox::error( this,
93
i18n( "Could not write the file %1:\n"
94
"\"%2\" is the detailed error description.",
96
QString::fromLocal8Bit( strerror( errno ) ) ),
97
i18n( "Sieve Editor Error" ) );
104
bool SieveEditor::saveToFile( const QString&filename )
106
QFile file( filename );
107
if ( !file.open( QIODevice::WriteOnly|QIODevice::Text ) )
109
QTextStream out(&file);
110
out << mTextEdit->toPlainText();
114
void SieveEditor::slotImport()
116
if ( !mTextEdit->toPlainText().isEmpty() )
118
if ( KMessageBox::warningYesNo(this, i18n( "You will overwrite script. Do you want to continue?" ), i18n( "Import Script" ) ) == KMessageBox::No )
122
QPointer<KFileDialog> fdlg( new KFileDialog( url, QString(), this) );
124
fdlg->setMode( KFile::File );
125
fdlg->setOperationMode( KFileDialog::Opening );
126
if ( fdlg->exec() == QDialog::Accepted && fdlg )
128
const QString fileName = fdlg->selectedFile();
129
if ( !loadFromFile( fileName ) )
131
KMessageBox::error( this,
132
i18n( "Could not load the file %1:\n"
133
"\"%2\" is the detailed error description.",
135
QString::fromLocal8Bit( strerror( errno ) ) ),
136
i18n( "Sieve Editor Error" ) );
142
bool SieveEditor::loadFromFile( const QString& filename )
144
QFile file( filename );
145
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
148
QTextStream in(&file);
149
QString line = in.readLine();
151
while (!line.isNull()) {
152
if ( scriptText.isEmpty() )
155
scriptText += QLatin1String( "\n" ) + line;
156
line = in.readLine();
158
mTextEdit->setPlainText( scriptText );
162
void SieveEditor::slotTextChanged()
164
const bool enabled = !script().isEmpty();
165
enableButton( User1, enabled );
166
enableButtonOk( enabled );
169
QString SieveEditor::script() const
171
return mTextEdit->toPlainText();
174
void SieveEditor::setScript( const QString & script )
176
mTextEdit->append( script );
179
void SieveEditor::setDebugColor( const QColor& col )
181
mDebugTextEdit->setTextColor( col );
184
void SieveEditor::setDebugScript( const QString& debug )
186
mDebugTextEdit->setText( debug );
189
void SieveEditor::setScriptName( const QString&name )
191
mScriptName->setText( name );
194
#include "sieveeditor.moc"