~ubuntu-branches/ubuntu/precise/kdepim/precise-proposed

« back to all changes in this revision

Viewing changes to libksieve/ksieveui/sieveeditor.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:51 UTC
  • mfrom: (0.2.20)
  • Revision ID: package-import@ubuntu.com-20111215141751-yg890pa7vnlo34e0
Tags: 4:4.7.90-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2011 Laurent Montel <montel@kde.org>
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "sieveeditor.h"
 
20
#include <klocale.h>
 
21
#include <kiconloader.h>
 
22
#include <kmessagebox.h>
 
23
#include <kfiledialog.h>
 
24
#include <errno.h>
 
25
 
 
26
#include <QSplitter>
 
27
#include <QTextStream>
 
28
#include <QPointer>
 
29
#include <QVBoxLayout>
 
30
#include <QLabel>
 
31
#include <QLineEdit>
 
32
 
 
33
using namespace KSieveUi;
 
34
 
 
35
SieveEditor::SieveEditor( QWidget * parent )
 
36
  : KDialog( parent )
 
37
{
 
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 );
 
44
  setModal( true );
 
45
 
 
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 );
 
56
  
 
57
  QSplitter *splitter = new QSplitter;
 
58
  splitter->setOrientation( Qt::Vertical );
 
59
  lay->addWidget( splitter );
 
60
  QList<int> size;
 
61
  size << 400 << 100;
 
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()) );
 
71
 
 
72
  setMainWidget( mainWidget );
 
73
  resize( 640,480);
 
74
}
 
75
 
 
76
SieveEditor::~SieveEditor()
 
77
{
 
78
}
 
79
 
 
80
void SieveEditor::slotSaveAs()
 
81
{
 
82
  KUrl url;
 
83
  QPointer<KFileDialog> fdlg( new KFileDialog( url, QString(), this) );
 
84
 
 
85
  fdlg->setMode( KFile::File );
 
86
  fdlg->setOperationMode( KFileDialog::Saving );
 
87
  if ( fdlg->exec() == QDialog::Accepted && fdlg )
 
88
  {
 
89
    const QString fileName = fdlg->selectedFile();
 
90
    if ( !saveToFile( fileName ) )
 
91
    {
 
92
      KMessageBox::error( this,
 
93
                          i18n( "Could not write the file %1:\n"
 
94
                                "\"%2\" is the detailed error description.",
 
95
                                fileName,
 
96
                                QString::fromLocal8Bit( strerror( errno ) ) ),
 
97
                          i18n( "Sieve Editor Error" ) );
 
98
    }
 
99
  }
 
100
  delete fdlg;
 
101
 
 
102
}
 
103
 
 
104
bool SieveEditor::saveToFile( const QString&filename )
 
105
{
 
106
  QFile file( filename );
 
107
  if ( !file.open( QIODevice::WriteOnly|QIODevice::Text ) )
 
108
    return false;
 
109
  QTextStream out(&file);
 
110
  out << mTextEdit->toPlainText();
 
111
  return true;
 
112
}
 
113
 
 
114
void SieveEditor::slotImport()
 
115
{
 
116
  if ( !mTextEdit->toPlainText().isEmpty() )
 
117
  {
 
118
    if ( KMessageBox::warningYesNo(this, i18n( "You will overwrite script. Do you want to continue?" ), i18n( "Import Script" ) ) == KMessageBox::No )
 
119
      return;
 
120
  }
 
121
  KUrl url;
 
122
  QPointer<KFileDialog> fdlg( new KFileDialog( url, QString(), this) );
 
123
 
 
124
  fdlg->setMode( KFile::File );
 
125
  fdlg->setOperationMode( KFileDialog::Opening );
 
126
  if ( fdlg->exec() == QDialog::Accepted && fdlg )
 
127
  {
 
128
    const QString fileName = fdlg->selectedFile();
 
129
    if ( !loadFromFile( fileName ) )
 
130
    {
 
131
      KMessageBox::error( this,
 
132
                          i18n( "Could not load the file %1:\n"
 
133
                                "\"%2\" is the detailed error description.",
 
134
                                fileName,
 
135
                                QString::fromLocal8Bit( strerror( errno ) ) ),
 
136
                          i18n( "Sieve Editor Error" ) );
 
137
    }
 
138
  }
 
139
  delete fdlg;
 
140
}
 
141
 
 
142
bool SieveEditor::loadFromFile( const QString& filename )
 
143
{
 
144
  QFile file( filename );
 
145
  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
 
146
    return false;
 
147
 
 
148
  QTextStream in(&file);
 
149
  QString line = in.readLine();
 
150
  QString scriptText;
 
151
  while (!line.isNull()) {
 
152
    if ( scriptText.isEmpty() )
 
153
      scriptText = line;
 
154
    else
 
155
      scriptText += QLatin1String( "\n" ) + line;
 
156
    line = in.readLine();
 
157
  }
 
158
  mTextEdit->setPlainText( scriptText );
 
159
  return true;
 
160
}
 
161
 
 
162
void SieveEditor::slotTextChanged()
 
163
{
 
164
  const bool enabled = !script().isEmpty();
 
165
  enableButton( User1, enabled );
 
166
  enableButtonOk( enabled );
 
167
}
 
168
 
 
169
QString SieveEditor::script() const
 
170
{
 
171
  return mTextEdit->toPlainText();
 
172
}
 
173
 
 
174
void SieveEditor::setScript( const QString & script )
 
175
{
 
176
  mTextEdit->append( script );
 
177
}
 
178
 
 
179
void SieveEditor::setDebugColor( const QColor& col )
 
180
{
 
181
  mDebugTextEdit->setTextColor( col );
 
182
}
 
183
 
 
184
void SieveEditor::setDebugScript( const QString& debug )
 
185
{
 
186
  mDebugTextEdit->setText( debug );
 
187
}
 
188
 
 
189
void SieveEditor::setScriptName( const QString&name )
 
190
{
 
191
  mScriptName->setText( name );
 
192
}  
 
193
 
 
194
#include "sieveeditor.moc"
 
195