Build with clang.
[kdepim.git] / mailcommon / filterlog.cpp
blob9c04faa825b7d21c73a44d0a3e71cad67a6b491c
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 "filterlog.h"
33 #include "messageviewer/util.h"
35 #include <kdebug.h>
37 #include <QFile>
38 #include <QByteArray>
39 #include <QTime>
41 #include <sys/stat.h>
44 using namespace MailCommon;
46 class FilterLog::Private
48 public:
49 Private( FilterLog *qq )
50 : q( qq ),
51 mLogging( false ),
52 mMaxLogSize( 512 * 1024 ),
53 mCurrentLogSize( 0 ),
54 mAllowedTypes( FilterLog::Meta |
55 FilterLog::PatternDescription |
56 FilterLog::RuleResult |
57 FilterLog::PatternResult |
58 FilterLog::AppliedAction )
62 static FilterLog * mSelf;
64 FilterLog *q;
65 QStringList mLogEntries;
66 bool mLogging;
67 long mMaxLogSize;
68 long mCurrentLogSize;
69 int mAllowedTypes;
71 void checkLogSize();
74 void FilterLog::Private::checkLogSize()
76 if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 ) {
77 kDebug() << "Filter log: memory limit reached, starting to discard old items, size ="
78 << QString::number( mCurrentLogSize );
80 // avoid some kind of hysteresis, shrink the log to 90% of its maximum
81 while ( mCurrentLogSize > (mMaxLogSize * 0.9) ) {
82 QStringList::Iterator it = mLogEntries.begin();
83 if ( it != mLogEntries.end() ) {
84 mCurrentLogSize -= (*it).length();
85 mLogEntries.erase( it );
86 kDebug() << "Filter log: new size =" << QString::number( mCurrentLogSize );
87 } else {
88 kDebug() << "Filter log: size reduction disaster!";
89 q->clear();
93 emit q->logShrinked();
97 FilterLog * FilterLog::Private::mSelf = 0;
100 FilterLog::FilterLog()
101 : d( new Private( this ) )
105 FilterLog::~FilterLog()
107 delete d;
110 FilterLog* FilterLog::instance()
112 if ( !FilterLog::Private::mSelf )
113 FilterLog::Private::mSelf = new FilterLog();
115 return FilterLog::Private::mSelf;
118 bool FilterLog::isLogging() const
120 return d->mLogging;
123 void FilterLog::setLogging( bool active )
125 d->mLogging = active;
126 emit logStateChanged();
129 void FilterLog::setMaxLogSize( long size )
131 if ( size < -1)
132 size = -1;
134 // do not allow less than 1 KByte except unlimited (-1)
135 if ( size >= 0 && size < 1024 )
136 size = 1024;
138 d->mMaxLogSize = size;
139 emit logStateChanged();
140 d->checkLogSize();
143 long FilterLog::maxLogSize() const
145 return d->mMaxLogSize;
148 void FilterLog::setContentTypeEnabled( ContentType contentType, bool enable )
150 if ( enable )
151 d->mAllowedTypes |= contentType;
152 else
153 d->mAllowedTypes &= ~contentType;
155 emit logStateChanged();
158 bool FilterLog::isContentTypeEnabled( ContentType contentType ) const
160 return (d->mAllowedTypes & contentType);
163 void FilterLog::add( const QString &logEntry, ContentType contentType )
165 if ( isLogging() && (d->mAllowedTypes & contentType) ) {
166 QString timedLog = QLatin1Char( '[' ) + QTime::currentTime().toString() + QLatin1String( "] " );
167 if ( contentType & ~Meta )
168 timedLog += logEntry;
169 else
170 timedLog = logEntry;
172 d->mLogEntries.append( timedLog );
173 emit logEntryAdded( timedLog );
174 d->mCurrentLogSize += timedLog.length();
175 d->checkLogSize();
179 void FilterLog::addSeparator()
181 add( "------------------------------", Meta );
184 void FilterLog::clear()
186 d->mLogEntries.clear();
187 d->mCurrentLogSize = 0;
188 emit logShrinked();
191 QStringList FilterLog::logEntries() const
193 return d->mLogEntries;
196 void FilterLog::dump()
198 #ifndef NDEBUG
199 kDebug() << "----- starting filter log -----";
200 foreach ( const QString &entry, d->mLogEntries ) {
201 kDebug() << entry;
203 kDebug() << "------ end of filter log ------";
204 #endif
207 bool FilterLog::saveToFile( const QString &fileName ) const
209 QFile file( fileName );
210 if ( !file.open( QIODevice::WriteOnly ) )
211 return false;
213 fchmod( file.handle(), MessageViewer::Util::getWritePermissions() );
215 foreach ( const QString &entry, d->mLogEntries ) {
216 const QString line = entry + QLatin1Char( '\n' );
217 file.write( line.toLocal8Bit() );
220 return true;
223 QString FilterLog::recode( const QString &plain )
225 return Qt::escape( plain );
228 #include "filterlog.moc"