• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • filter
filterlog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17  In addition, as a special exception, the copyright holders give
18  permission to link the code of this program with any edition of
19  the Qt library by Trolltech AS, Norway (or with modified versions
20  of Qt that use the same license as Qt), and distribute linked
21  combinations including the two. You must obey the GNU General
22  Public License in all respects for all of the code used other than
23  Qt. If you modify this file, you may extend this exception to
24  your version of the file, but you are not obligated to do so. If
25  you do not wish to do so, delete this exception statement from
26  your version.
27 */
28 
29 #include "filterlog.h"
30 
31 #include "messageviewer/utils/util.h"
32 
33 #include <KDebug>
34 
35 #include <QFile>
36 #include <QTime>
37 #include <QTextDocument>
38 #include <QDir>
39 
40 #include <sys/stat.h>
41 
42 using namespace MailCommon;
43 
44 class FilterLog::Private
45 {
46 public:
47  Private( FilterLog *qq )
48  : q( qq ),
49  mLogging( false ),
50  mMaxLogSize( 512 * 1024 ),
51  mCurrentLogSize( 0 ),
52  mAllowedTypes( FilterLog::Meta |
53  FilterLog::PatternDescription |
54  FilterLog::RuleResult |
55  FilterLog::PatternResult |
56  FilterLog::AppliedAction )
57  {
58  }
59 
60  static FilterLog * mSelf;
61 
62  FilterLog *q;
63  QStringList mLogEntries;
64  bool mLogging;
65  long mMaxLogSize;
66  long mCurrentLogSize;
67  int mAllowedTypes;
68 
69  void checkLogSize();
70 };
71 
72 void FilterLog::Private::checkLogSize()
73 {
74  if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 ) {
75  kDebug() << "Filter log: memory limit reached, starting to discard old items, size ="
76  << QString::number( mCurrentLogSize );
77 
78  // avoid some kind of hysteresis, shrink the log to 90% of its maximum
79  while ( mCurrentLogSize > ( mMaxLogSize * 0.9 ) ) {
80  QStringList::Iterator it = mLogEntries.begin();
81  if ( it != mLogEntries.end() ) {
82  mCurrentLogSize -= (*it).length();
83  mLogEntries.erase( it );
84  kDebug() << "Filter log: new size =" << QString::number( mCurrentLogSize );
85  } else {
86  kDebug() << "Filter log: size reduction disaster!";
87  q->clear();
88  }
89  }
90 
91  emit q->logShrinked();
92  }
93 }
94 
95 FilterLog * FilterLog::Private::mSelf = 0;
96 
97 FilterLog::FilterLog()
98  : d( new Private( this ) )
99 {
100 }
101 
102 FilterLog::~FilterLog()
103 {
104  delete d;
105 }
106 
107 FilterLog *FilterLog::instance()
108 {
109  if ( !FilterLog::Private::mSelf ) {
110  FilterLog::Private::mSelf = new FilterLog();
111  }
112 
113  return FilterLog::Private::mSelf;
114 }
115 
116 bool FilterLog::isLogging() const
117 {
118  return d->mLogging;
119 }
120 
121 void FilterLog::setLogging( bool active )
122 {
123  d->mLogging = active;
124  emit logStateChanged();
125 }
126 
127 void FilterLog::setMaxLogSize( long size )
128 {
129  if ( size < -1 ) {
130  size = -1;
131  }
132 
133  // do not allow less than 1 KByte except unlimited (-1)
134  if ( size >= 0 && size < 1024 ) {
135  size = 1024;
136  }
137 
138  d->mMaxLogSize = size;
139  emit logStateChanged();
140  d->checkLogSize();
141 }
142 
143 long FilterLog::maxLogSize() const
144 {
145  return d->mMaxLogSize;
146 }
147 
148 void FilterLog::setContentTypeEnabled( ContentType contentType, bool enable )
149 {
150  if ( enable ) {
151  d->mAllowedTypes |= contentType;
152  } else {
153  d->mAllowedTypes &= ~contentType;
154  }
155 
156  emit logStateChanged();
157 }
158 
159 bool FilterLog::isContentTypeEnabled( ContentType contentType ) const
160 {
161  return ( d->mAllowedTypes & contentType );
162 }
163 
164 void FilterLog::add( const QString &logEntry, ContentType contentType )
165 {
166  if ( isLogging() && ( d->mAllowedTypes & contentType ) ) {
167  QString timedLog = QLatin1Char( '[' ) + QTime::currentTime().toString() + QLatin1String( "] " );
168  if ( contentType & ~Meta ) {
169  timedLog += logEntry;
170  } else {
171  timedLog = logEntry;
172  }
173 
174  d->mLogEntries.append( timedLog );
175  emit logEntryAdded( timedLog );
176  d->mCurrentLogSize += timedLog.length();
177  d->checkLogSize();
178  }
179 }
180 
181 void FilterLog::addSeparator()
182 {
183  add( QLatin1String("------------------------------"), Meta );
184 }
185 
186 void FilterLog::clear()
187 {
188  d->mLogEntries.clear();
189  d->mCurrentLogSize = 0;
190 }
191 
192 QStringList FilterLog::logEntries() const
193 {
194  return d->mLogEntries;
195 }
196 
197 void FilterLog::dump()
198 {
199 #ifndef NDEBUG
200  kDebug() << "----- starting filter log -----";
201  foreach ( const QString &entry, d->mLogEntries ) {
202  kDebug() << entry;
203  }
204  kDebug() << "------ end of filter log ------";
205 #endif
206 }
207 
208 bool FilterLog::saveToFile( const QString &fileName ) const
209 {
210  QFile file( fileName );
211  if ( !file.open( QIODevice::WriteOnly ) ) {
212  return false;
213  }
214 
215  fchmod( file.handle(), MessageViewer::Util::getWritePermissions() );
216 
217  file.write( "<html>\n<body>\n" );
218  file.write( "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
219  foreach ( const QString &entry, d->mLogEntries ) {
220  const QString line = QLatin1String( "<p>" ) + entry + QLatin1String( "</p>" ) + QLatin1Char( '\n' );
221  file.write( line.toLocal8Bit() );
222  }
223  file.write( "</body>\n</html>\n" );
224  file.close();
225  return true;
226 }
227 
228 QString FilterLog::recode( const QString &plain )
229 {
230  return Qt::escape( plain );
231 }
232 
QString::append
QString & append(QChar ch)
MailCommon::FilterLog::maxLogSize
long maxLogSize() const
Returns the maximum size of the log in bytes.
Definition: filterlog.cpp:143
MailCommon::FilterLog::recode
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
Definition: filterlog.cpp:228
QTime::toString
QString toString(Qt::DateFormat format) const
QFile::handle
int handle() const
MailCommon::FilterLog::Meta
Log all meta data.
Definition: filterlog.h:74
MailCommon::FilterLog::logEntries
QStringList logEntries() const
Returns the list of log entries.
Definition: filterlog.cpp:192
QFile
MailCommon::FilterLog::setMaxLogSize
void setMaxLogSize(long size=-1)
Sets the maximum size of the log in bytes.
Definition: filterlog.cpp:127
MailCommon::FilterLog::instance
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:107
MailCommon::FilterLog
KMail Filter Log Collector.
Definition: filterlog.h:55
filterlog.h
MailCommon::FilterLog::~FilterLog
virtual ~FilterLog()
Destroys the filter log.
Definition: filterlog.cpp:102
QString::number
QString number(int n, int base)
MailCommon::FilterLog::isLogging
bool isLogging() const
Returns whether the filter log is currently active.
Definition: filterlog.cpp:116
MailCommon::FilterLog::saveToFile
bool saveToFile(const QString &fileName) const
Saves the log to the file with the given fileName.
Definition: filterlog.cpp:208
MailCommon::FilterLog::setLogging
void setLogging(bool active)
Sets whether the filter log is currently active.
Definition: filterlog.cpp:121
QList::Iterator
typedef Iterator
QString
MailCommon::FilterLog::addSeparator
void addSeparator()
Adds a separator line to the log.
Definition: filterlog.cpp:181
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
MailCommon::FilterLog::logStateChanged
void logStateChanged()
This signal is emitted whenever the activity of the filter log has been changed.
MailCommon::FilterLog::PatternDescription
Log all pattern description.
Definition: filterlog.h:75
QStringList
MailCommon::FilterLog::clear
void clear()
Clears the log.
Definition: filterlog.cpp:186
QString::toLocal8Bit
QByteArray toLocal8Bit() const
MailCommon::FilterLog::AppliedAction
Log all applied actions.
Definition: filterlog.h:78
QLatin1Char
QFile::close
virtual void close()
QTime::currentTime
QTime currentTime()
MailCommon::FilterLog::logEntryAdded
void logEntryAdded(const QString &entry)
This signal is emitted whenever a new entry has been added to the log.
QLatin1String
Qt::escape
QString escape(const QString &plain)
MailCommon::FilterLog::PatternResult
Log all pattern matching results.
Definition: filterlog.h:77
QString::length
int length() const
QIODevice::write
qint64 write(const char *data, qint64 maxSize)
MailCommon::FilterLog::setContentTypeEnabled
void setContentTypeEnabled(ContentType type, bool enabled)
Sets whether a given content type will be enabled for logging.
Definition: filterlog.cpp:148
MailCommon::FilterLog::add
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:164
MailCommon::FilterLog::isContentTypeEnabled
bool isContentTypeEnabled(ContentType type) const
Returns whether the given content type is enabled for logging.
Definition: filterlog.cpp:159
MailCommon::FilterLog::ContentType
ContentType
Describes the type of content that will be logged.
Definition: filterlog.h:73
MailCommon::FilterLog::RuleResult
Log all rule matching results.
Definition: filterlog.h:76
MailCommon::FilterLog::dump
void dump()
Dumps the log to console.
Definition: filterlog.cpp:197
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal