Make the boss happy.
[kdepim.git] / kmail / filterlogdlg.cpp
blobd00e023629e0e4ad294e8ffd3d8cce3943d40dbe
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 "mailcommon/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;
52 using namespace MailCommon;
55 FilterLogDialog::FilterLogDialog( QWidget * parent )
56 : KDialog( parent )
58 setCaption( i18n( "Filter Log Viewer" ) );
59 setButtons( User1|User2|Close );
60 setObjectName( "FilterLogDlg" );
61 setModal( false );
62 setDefaultButton( Close );
63 setButtonGuiItem( User1, KStandardGuiItem::clear() );
64 setButtonGuiItem( User2, KStandardGuiItem::saveAs() );
65 setAttribute( Qt::WA_DeleteOnClose );
66 QFrame *page = new KVBox( this );
67 setMainWidget( page );
69 mTextEdit = new KTextEdit( page );
70 mTextEdit->setReadOnly( true );
71 mTextEdit->setLineWrapMode ( KTextEdit::NoWrap );
73 QString text;
74 const QStringList logEntries = FilterLog::instance()->logEntries();
75 for ( QStringList::ConstIterator it = logEntries.constBegin();
76 it != logEntries.constEnd(); ++it )
78 mTextEdit->append(*it);
81 mLogActiveBox = new QCheckBox( i18n("&Log filter activities"), page );
82 mLogActiveBox->setChecked( FilterLog::instance()->isLogging() );
83 connect( mLogActiveBox, SIGNAL(clicked()),
84 this, SLOT(slotSwitchLogState()) );
85 mLogActiveBox->setWhatsThis(
86 i18n( "You can turn logging of filter activities on and off here. "
87 "Of course, log data is collected and shown only when logging "
88 "is turned on. " ) );
90 mLogDetailsBox = new QGroupBox(i18n( "Logging Details" ), page );
91 QVBoxLayout *layout = new QVBoxLayout;
92 mLogDetailsBox->setLayout( layout );
93 mLogDetailsBox->setEnabled( mLogActiveBox->isChecked() );
94 connect( mLogActiveBox, SIGNAL(toggled(bool)),
95 mLogDetailsBox, SLOT(setEnabled(bool)) );
97 mLogPatternDescBox = new QCheckBox( i18n("Log pattern description") );
98 layout->addWidget( mLogPatternDescBox );
99 mLogPatternDescBox->setChecked(
100 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternDescription ) );
101 connect( mLogPatternDescBox, SIGNAL(clicked()),
102 this, SLOT(slotChangeLogDetail()) );
103 // TODO
104 //QWhatsThis::add( mLogPatternDescBox,
105 // i18n( "" ) );
107 mLogRuleEvaluationBox = new QCheckBox( i18n("Log filter &rule evaluation") );
108 layout->addWidget( mLogRuleEvaluationBox );
109 mLogRuleEvaluationBox->setChecked(
110 FilterLog::instance()->isContentTypeEnabled( FilterLog::RuleResult ) );
111 connect( mLogRuleEvaluationBox, SIGNAL(clicked()),
112 this, SLOT(slotChangeLogDetail()) );
113 mLogRuleEvaluationBox->setWhatsThis(
114 i18n( "You can control the feedback in the log concerning the "
115 "evaluation of the filter rules of applied filters: "
116 "having this option checked will give detailed feedback "
117 "for each single filter rule; alternatively, only "
118 "feedback about the result of the evaluation of all rules "
119 "of a single filter will be given." ) );
121 mLogPatternResultBox = new QCheckBox( i18n("Log filter pattern evaluation") );
122 layout->addWidget( mLogPatternResultBox );
123 mLogPatternResultBox->setChecked(
124 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternResult ) );
125 connect( mLogPatternResultBox, SIGNAL(clicked()),
126 this, SLOT(slotChangeLogDetail()) );
127 // TODO
128 //QWhatsThis::add( mLogPatternResultBox,
129 // i18n( "" ) );
131 mLogFilterActionBox = new QCheckBox( i18n("Log filter actions") );
132 layout->addWidget( mLogFilterActionBox );
133 mLogFilterActionBox->setChecked(
134 FilterLog::instance()->isContentTypeEnabled( FilterLog::AppliedAction ) );
135 connect( mLogFilterActionBox, SIGNAL(clicked()),
136 this, SLOT(slotChangeLogDetail()) );
137 // TODO
138 //QWhatsThis::add( mLogFilterActionBox,
139 // i18n( "" ) );
141 KHBox * hbox = new KHBox( page );
142 new QLabel( i18n("Log size limit:"), hbox );
143 mLogMemLimitSpin = new QSpinBox( hbox );
144 mLogMemLimitSpin->setMinimum( 1 );
145 mLogMemLimitSpin->setMaximum( 1024 * 256 ); // 256 MB
146 // value in the QSpinBox is in KB while it's in Byte in the FilterLog
147 mLogMemLimitSpin->setValue( FilterLog::instance()->maxLogSize() / 1024 );
148 mLogMemLimitSpin->setSuffix( " KB" );
149 mLogMemLimitSpin->setSpecialValueText(
150 i18nc("@label:spinbox Set the size of the logfile to unlimited.", "unlimited") );
151 connect( mLogMemLimitSpin, SIGNAL(valueChanged(int)),
152 this, SLOT(slotChangeLogMemLimit(int)) );
153 mLogMemLimitSpin->setWhatsThis(
154 i18n( "Collecting log data uses memory to temporarily store the "
155 "log data; here you can limit the maximum amount of memory "
156 "to be used: if the size of the collected log data exceeds "
157 "this limit then the oldest data will be discarded until "
158 "the limit is no longer exceeded. " ) );
160 connect(FilterLog::instance(), SIGNAL(logEntryAdded(QString)),
161 this, SLOT(slotLogEntryAdded(QString)));
162 connect(FilterLog::instance(), SIGNAL(logShrinked()),
163 this, SLOT(slotLogShrinked()));
164 connect(FilterLog::instance(), SIGNAL(logStateChanged()),
165 this, SLOT(slotLogStateChanged()));
167 setInitialSize( QSize( 500, 500 ) );
168 connect( this, SIGNAL(user1Clicked()), SLOT(slotUser1()) );
169 connect( this, SIGNAL(user2Clicked()), SLOT(slotUser2()) );
173 void FilterLogDialog::slotLogEntryAdded(const QString& logEntry )
175 mTextEdit->append( logEntry );
179 void FilterLogDialog::slotLogShrinked()
181 // limit the size of the shown log lines as soon as
182 // the log has reached it's memory limit
183 if ( mTextEdit->document()->maximumBlockCount () <= 0 )
184 mTextEdit->document()->setMaximumBlockCount( mTextEdit->document()->blockCount() );
188 void FilterLogDialog::slotLogStateChanged()
190 mLogActiveBox->setChecked( FilterLog::instance()->isLogging() );
191 mLogPatternDescBox->setChecked(
192 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternDescription ) );
193 mLogRuleEvaluationBox->setChecked(
194 FilterLog::instance()->isContentTypeEnabled( FilterLog::RuleResult ) );
195 mLogPatternResultBox->setChecked(
196 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternResult ) );
197 mLogFilterActionBox->setChecked(
198 FilterLog::instance()->isContentTypeEnabled( FilterLog::AppliedAction ) );
200 // value in the QSpinBox is in KB while it's in Byte in the FilterLog
201 int newLogSize = FilterLog::instance()->maxLogSize() / 1024;
202 if ( mLogMemLimitSpin->value() != newLogSize )
203 mLogMemLimitSpin->setValue( newLogSize );
207 void FilterLogDialog::slotChangeLogDetail()
209 if ( mLogPatternDescBox->isChecked() !=
210 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternDescription ) )
211 FilterLog::instance()->setContentTypeEnabled( FilterLog::PatternDescription,
212 mLogPatternDescBox->isChecked() );
214 if ( mLogRuleEvaluationBox->isChecked() !=
215 FilterLog::instance()->isContentTypeEnabled( FilterLog::RuleResult ) )
216 FilterLog::instance()->setContentTypeEnabled( FilterLog::RuleResult,
217 mLogRuleEvaluationBox->isChecked() );
219 if ( mLogPatternResultBox->isChecked() !=
220 FilterLog::instance()->isContentTypeEnabled( FilterLog::PatternResult ) )
221 FilterLog::instance()->setContentTypeEnabled( FilterLog::PatternResult,
222 mLogPatternResultBox->isChecked() );
224 if ( mLogFilterActionBox->isChecked() !=
225 FilterLog::instance()->isContentTypeEnabled( FilterLog::AppliedAction ) )
226 FilterLog::instance()->setContentTypeEnabled( FilterLog::AppliedAction,
227 mLogFilterActionBox->isChecked() );
231 void FilterLogDialog::slotSwitchLogState()
233 FilterLog::instance()->setLogging( mLogActiveBox->isChecked() );
237 void FilterLogDialog::slotChangeLogMemLimit( int value )
239 FilterLog::instance()->setMaxLogSize( value * 1024 );
243 void FilterLogDialog::slotUser1()
245 FilterLog::instance()->clear();
246 mTextEdit->clear();
250 void FilterLogDialog::slotUser2()
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 const QString fileName = fdlg->selectedFile();
262 if ( !FilterLog::instance()->saveToFile( fileName ) )
264 KMessageBox::error( this,
265 i18n( "Could not write the file %1:\n"
266 "\"%2\" is the detailed error description.",
267 fileName,
268 QString::fromLocal8Bit( strerror( errno ) ) ),
269 i18n( "KMail Error" ) );
275 #include "filterlogdlg.moc"