backport 1145339: no error box spam
[kdepim.git] / kmail / filterlog.cpp
blob18ccbef0b5ca9468d7d577f1103f00c43f9f1d48
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 KMail;
47 FilterLog * FilterLog::mSelf = NULL;
50 FilterLog::FilterLog()
52 mSelf = this;
53 // start with logging disabled by default
54 mLogging = false;
55 // better limit the log to 512 KByte to avoid out of memory situations
56 // when the log i sgoing to become very long
57 mMaxLogSize = 512 * 1024;
58 mCurrentLogSize = 0;
59 mAllowedTypes = meta | patternDesc | ruleResult |
60 patternResult | appliedAction;
64 FilterLog::~FilterLog()
68 FilterLog * FilterLog::instance()
70 if ( !mSelf ) mSelf = new FilterLog();
71 return mSelf;
75 void FilterLog::add( const QString& logEntry, ContentType contentType )
77 if ( isLogging() && ( mAllowedTypes & contentType ) )
79 QString timedLog = '[' + QTime::currentTime().toString() + "] ";
80 if ( contentType & ~meta )
81 timedLog += logEntry;
82 else
83 timedLog = logEntry;
84 mLogEntries.append( timedLog );
85 emit logEntryAdded( timedLog );
86 mCurrentLogSize += timedLog.length();
87 checkLogSize();
92 void FilterLog::setMaxLogSize( long size )
94 if ( size < -1)
95 size = -1;
96 // do not allow less than 1 KByte except unlimited (-1)
97 if ( size >= 0 && size < 1024 )
98 size = 1024;
99 mMaxLogSize = size;
100 emit logStateChanged();
101 checkLogSize();
105 void FilterLog::dump()
107 #ifndef NDEBUG
108 kDebug() << "----- starting filter log -----";
109 for ( QStringList::ConstIterator it = mLogEntries.constBegin();
110 it != mLogEntries.constEnd(); ++it )
112 kDebug() << *it;
114 kDebug() << "------ end of filter log ------";
115 #endif
119 void FilterLog::checkLogSize()
121 if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 )
123 kDebug() << "Filter log: memory limit reached, starting to discard old items, size ="
124 << QString::number( mCurrentLogSize );
125 // avoid some kind of hysteresis, shrink the log to 90% of its maximum
126 while ( mCurrentLogSize > ( mMaxLogSize * 0.9 ) )
128 QStringList::Iterator it = mLogEntries.begin();
129 if ( it != mLogEntries.end())
131 mCurrentLogSize -= (*it).length();
132 mLogEntries.erase( it );
133 kDebug() << "Filter log: new size ="
134 << QString::number( mCurrentLogSize );
136 else
138 kDebug() << "Filter log: size reduction disaster!";
139 clear();
142 emit logShrinked();
147 bool FilterLog::saveToFile( const QString &fileName )
149 QFile file( fileName );
150 if( file.open( QIODevice::WriteOnly ) ) {
151 fchmod( file.handle(), MessageViewer::Util::getWritePermissions() );
153 QDataStream ds( &file );
154 for ( QStringList::const_iterator it = mLogEntries.constBegin();
155 it != mLogEntries.constEnd(); ++it )
157 const QString tmpString = *it + '\n';
158 QByteArray cstr( tmpString.toLocal8Bit() );
159 ds.writeRawData( cstr.data(), cstr.size() );
162 return true;
164 else
165 return false;
169 #include "filterlog.moc"