backport 1145339: no error box spam
[kdepim.git] / kmail / filterlogdlg.cpp
blob41052536836b1f46cc3e0edc63b8655cf651628d
1 /*
2 This file is part of KMail.
3 Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>
5 KMail is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License, version 2, as
7 published by the Free Software Foundation.
9 KMail is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 In addition, as a special exception, the copyright holders give
19 permission to link the code of this program with any edition of
20 the Qt library by Trolltech AS, Norway (or with modified versions
21 of Qt that use the same license as Qt), and distribute linked
22 combinations including the two. You must obey the GNU General
23 Public License in all respects for all of the code used other than
24 Qt. If you modify this file, you may extend this exception to
25 your version of the file, but you are not obligated to do so. If
26 you do not wish to do so, delete this exception statement from
27 your version.
31 #include "filterlogdlg.h"
32 #include "filterlog.h"
33 #include "messageviewer/autoqpointer.h"
35 #include <kdebug.h>
36 #include <kfiledialog.h>
37 #include <klocale.h>
38 #include <kmessagebox.h>
39 #include <ktextedit.h>
40 #include <kvbox.h>
42 #include <QCheckBox>
43 #include <QLabel>
44 #include <QSpinBox>
45 #include <QStringList>
46 #include <QGroupBox>
47 #include <QVBoxLayout>
49 #include <errno.h>
51 using namespace KMail;
54 FilterLogDialog::FilterLogDialog( QWidget * parent )
55 : KDialog( parent )
57 setCaption( i18n( "Filter Log Viewer" ) );
58 setButtons( User1|User2|Close );
59 setObjectName( "FilterLogDlg" );
60 setModal( false );
61 setDefaultButton( Close );
62 setButtonGuiItem( User1, KStandardGuiItem::clear() );
63 setButtonGuiItem( User2, KStandardGuiItem::saveAs() );
64 setAttribute( Qt::WA_DeleteOnClose );
65 QFrame *page = new KVBox( this );
66 setMainWidget( page );
68 mTextEdit = new KTextEdit( page );
69 mTextEdit->setReadOnly( true );
70 mTextEdit->setLineWrapMode ( KTextEdit::NoWrap );
72 QString text;
73 const QStringList logEntries = FilterLog::instance()->getLogEntries();
74 for ( QStringList::ConstIterator it = logEntries.constBegin();
75 it != logEntries.constEnd(); ++it )
77 mTextEdit->append(*it);
80 mLogActiveBox = new QCheckBox( i18n("&Log filter activities"), page );
81 mLogActiveBox->setChecked( FilterLog::instance()->isLogging() );
82 connect( mLogActiveBox, SIGNAL(clicked()),
83 this, SLOT(slotSwitchLogState(void)) );
84 mLogActiveBox->setWhatsThis(
85 i18n( "You can turn logging of filter activities on and off here. "
86 "Of course, log data is collected and shown only when logging "
87 "is turned on. " ) );
89 mLogDetailsBox = new QGroupBox(i18n( "Logging Details" ), page );
90 QVBoxLayout *layout = new QVBoxLayout;
91 mLogDetailsBox->setLayout( layout );
92 mLogDetailsBox->setEnabled( mLogActiveBox->isChecked() );
93 connect( mLogActiveBox, SIGNAL( toggled( bool ) ),
94 mLogDetailsBox, SLOT( setEnabled( bool ) ) );
96 mLogPatternDescBox = new QCheckBox( i18n("Log pattern description") );
97 layout->addWidget( mLogPatternDescBox );
98 mLogPatternDescBox->setChecked(
99 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternDesc ) );
100 connect( mLogPatternDescBox, SIGNAL(clicked()),
101 this, SLOT(slotChangeLogDetail(void)) );
102 // TODO
103 //QWhatsThis::add( mLogPatternDescBox,
104 // i18n( "" ) );
106 mLogRuleEvaluationBox = new QCheckBox( i18n("Log filter &rule evaluation") );
107 layout->addWidget( mLogRuleEvaluationBox );
108 mLogRuleEvaluationBox->setChecked(
109 FilterLog::instance()->isContentTypeEnabled( FilterLog::ruleResult ) );
110 connect( mLogRuleEvaluationBox, SIGNAL(clicked()),
111 this, SLOT(slotChangeLogDetail(void)) );
112 mLogRuleEvaluationBox->setWhatsThis(
113 i18n( "You can control the feedback in the log concerning the "
114 "evaluation of the filter rules of applied filters: "
115 "having this option checked will give detailed feedback "
116 "for each single filter rule; alternatively, only "
117 "feedback about the result of the evaluation of all rules "
118 "of a single filter will be given." ) );
120 mLogPatternResultBox = new QCheckBox( i18n("Log filter pattern evaluation") );
121 layout->addWidget( mLogPatternResultBox );
122 mLogPatternResultBox->setChecked(
123 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternResult ) );
124 connect( mLogPatternResultBox, SIGNAL(clicked()),
125 this, SLOT(slotChangeLogDetail(void)) );
126 // TODO
127 //QWhatsThis::add( mLogPatternResultBox,
128 // i18n( "" ) );
130 mLogFilterActionBox = new QCheckBox( i18n("Log filter actions") );
131 layout->addWidget( mLogFilterActionBox );
132 mLogFilterActionBox->setChecked(
133 FilterLog::instance()->isContentTypeEnabled( FilterLog::appliedAction ) );
134 connect( mLogFilterActionBox, SIGNAL(clicked()),
135 this, SLOT(slotChangeLogDetail(void)) );
136 // TODO
137 //QWhatsThis::add( mLogFilterActionBox,
138 // i18n( "" ) );
140 KHBox * hbox = new KHBox( page );
141 new QLabel( i18n("Log size limit:"), hbox );
142 mLogMemLimitSpin = new QSpinBox( hbox );
143 mLogMemLimitSpin->setMinimum( 1 );
144 mLogMemLimitSpin->setMaximum( 1024 * 256 ); // 256 MB
145 // value in the QSpinBox is in KB while it's in Byte in the FilterLog
146 mLogMemLimitSpin->setValue( FilterLog::instance()->getMaxLogSize() / 1024 );
147 mLogMemLimitSpin->setSuffix( " KB" );
148 mLogMemLimitSpin->setSpecialValueText(
149 i18nc("@label:spinbox Set the size of the logfile to unlimited.", "unlimited") );
150 connect( mLogMemLimitSpin, SIGNAL(valueChanged(int)),
151 this, SLOT(slotChangeLogMemLimit(int)) );
152 mLogMemLimitSpin->setWhatsThis(
153 i18n( "Collecting log data uses memory to temporarily store the "
154 "log data; here you can limit the maximum amount of memory "
155 "to be used: if the size of the collected log data exceeds "
156 "this limit then the oldest data will be discarded until "
157 "the limit is no longer exceeded. " ) );
159 connect(FilterLog::instance(), SIGNAL(logEntryAdded(const QString&)),
160 this, SLOT(slotLogEntryAdded(const QString&)));
161 connect(FilterLog::instance(), SIGNAL(logShrinked(void)),
162 this, SLOT(slotLogShrinked(void)));
163 connect(FilterLog::instance(), SIGNAL(logStateChanged(void)),
164 this, SLOT(slotLogStateChanged(void)));
166 setInitialSize( QSize( 500, 500 ) );
167 connect( this, SIGNAL( user1Clicked() ), SLOT( slotUser1() ) );
168 connect( this, SIGNAL( user2Clicked() ), SLOT( slotUser2() ) );
172 void FilterLogDialog::slotLogEntryAdded(const QString& logEntry )
174 mTextEdit->append( logEntry );
178 void FilterLogDialog::slotLogShrinked()
180 // limit the size of the shown log lines as soon as
181 // the log has reached it's memory limit
182 if ( mTextEdit->document()->maximumBlockCount () <= 0 )
183 mTextEdit->document()->setMaximumBlockCount( mTextEdit->document()->blockCount() );
187 void FilterLogDialog::slotLogStateChanged()
189 mLogActiveBox->setChecked( FilterLog::instance()->isLogging() );
190 mLogPatternDescBox->setChecked(
191 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternDesc ) );
192 mLogRuleEvaluationBox->setChecked(
193 FilterLog::instance()->isContentTypeEnabled( FilterLog::ruleResult ) );
194 mLogPatternResultBox->setChecked(
195 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternResult ) );
196 mLogFilterActionBox->setChecked(
197 FilterLog::instance()->isContentTypeEnabled( FilterLog::appliedAction ) );
199 // value in the QSpinBox is in KB while it's in Byte in the FilterLog
200 int newLogSize = FilterLog::instance()->getMaxLogSize() / 1024;
201 if ( mLogMemLimitSpin->value() != newLogSize )
202 mLogMemLimitSpin->setValue( newLogSize );
206 void FilterLogDialog::slotChangeLogDetail()
208 if ( mLogPatternDescBox->isChecked() !=
209 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternDesc ) )
210 FilterLog::instance()->setContentTypeEnabled( FilterLog::patternDesc,
211 mLogPatternDescBox->isChecked() );
213 if ( mLogRuleEvaluationBox->isChecked() !=
214 FilterLog::instance()->isContentTypeEnabled( FilterLog::ruleResult ) )
215 FilterLog::instance()->setContentTypeEnabled( FilterLog::ruleResult,
216 mLogRuleEvaluationBox->isChecked() );
218 if ( mLogPatternResultBox->isChecked() !=
219 FilterLog::instance()->isContentTypeEnabled( FilterLog::patternResult ) )
220 FilterLog::instance()->setContentTypeEnabled( FilterLog::patternResult,
221 mLogPatternResultBox->isChecked() );
223 if ( mLogFilterActionBox->isChecked() !=
224 FilterLog::instance()->isContentTypeEnabled( FilterLog::appliedAction ) )
225 FilterLog::instance()->setContentTypeEnabled( FilterLog::appliedAction,
226 mLogFilterActionBox->isChecked() );
230 void FilterLogDialog::slotSwitchLogState()
232 FilterLog::instance()->setLogging( mLogActiveBox->isChecked() );
236 void FilterLogDialog::slotChangeLogMemLimit( int value )
238 FilterLog::instance()->setMaxLogSize( value * 1024 );
242 void FilterLogDialog::slotUser1()
244 FilterLog::instance()->clear();
245 mTextEdit->clear();
249 void FilterLogDialog::slotUser2()
251 QString fileName;
252 KUrl url;
253 MessageViewer::AutoQPointer<KFileDialog> fdlg( new KFileDialog( url, QString(), this) );
255 fdlg->setMode( KFile::File );
256 fdlg->setSelection( "kmail-filter.log" );
257 fdlg->setOperationMode( KFileDialog::Saving );
258 if ( fdlg->exec() == QDialog::Accepted && fdlg )
260 fileName = fdlg->selectedFile();
261 if ( !FilterLog::instance()->saveToFile( fileName ) )
263 KMessageBox::error( this,
264 i18n( "Could not write the file %1:\n"
265 "\"%2\" is the detailed error description.",
266 fileName,
267 QString::fromLocal8Bit( strerror( errno ) ) ),
268 i18n( "KMail Error" ) );
274 #include "filterlogdlg.moc"