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

mailcommon

  • sources
  • kde-4.12
  • 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 <QByteArray>
37 #include <QTime>
38 
39 #include <sys/stat.h>
40 
41 using namespace MailCommon;
42 
43 class FilterLog::Private
44 {
45 public:
46  Private( FilterLog *qq )
47  : q( qq ),
48  mLogging( false ),
49  mMaxLogSize( 512 * 1024 ),
50  mCurrentLogSize( 0 ),
51  mAllowedTypes( FilterLog::Meta |
52  FilterLog::PatternDescription |
53  FilterLog::RuleResult |
54  FilterLog::PatternResult |
55  FilterLog::AppliedAction )
56  {
57  }
58 
59  static FilterLog * mSelf;
60 
61  FilterLog *q;
62  QStringList mLogEntries;
63  bool mLogging;
64  long mMaxLogSize;
65  long mCurrentLogSize;
66  int mAllowedTypes;
67 
68  void checkLogSize();
69 };
70 
71 void FilterLog::Private::checkLogSize()
72 {
73  if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 ) {
74  kDebug() << "Filter log: memory limit reached, starting to discard old items, size ="
75  << QString::number( mCurrentLogSize );
76 
77  // avoid some kind of hysteresis, shrink the log to 90% of its maximum
78  while ( mCurrentLogSize > ( mMaxLogSize * 0.9 ) ) {
79  QStringList::Iterator it = mLogEntries.begin();
80  if ( it != mLogEntries.end() ) {
81  mCurrentLogSize -= (*it).length();
82  mLogEntries.erase( it );
83  kDebug() << "Filter log: new size =" << QString::number( mCurrentLogSize );
84  } else {
85  kDebug() << "Filter log: size reduction disaster!";
86  q->clear();
87  }
88  }
89 
90  emit q->logShrinked();
91  }
92 }
93 
94 FilterLog * FilterLog::Private::mSelf = 0;
95 
96 FilterLog::FilterLog()
97  : d( new Private( this ) )
98 {
99 }
100 
101 FilterLog::~FilterLog()
102 {
103  delete d;
104 }
105 
106 FilterLog *FilterLog::instance()
107 {
108  if ( !FilterLog::Private::mSelf ) {
109  FilterLog::Private::mSelf = new FilterLog();
110  }
111 
112  return FilterLog::Private::mSelf;
113 }
114 
115 bool FilterLog::isLogging() const
116 {
117  return d->mLogging;
118 }
119 
120 void FilterLog::setLogging( bool active )
121 {
122  d->mLogging = active;
123  emit logStateChanged();
124 }
125 
126 void FilterLog::setMaxLogSize( long size )
127 {
128  if ( size < -1 ) {
129  size = -1;
130  }
131 
132  // do not allow less than 1 KByte except unlimited (-1)
133  if ( size >= 0 && size < 1024 ) {
134  size = 1024;
135  }
136 
137  d->mMaxLogSize = size;
138  emit logStateChanged();
139  d->checkLogSize();
140 }
141 
142 long FilterLog::maxLogSize() const
143 {
144  return d->mMaxLogSize;
145 }
146 
147 void FilterLog::setContentTypeEnabled( ContentType contentType, bool enable )
148 {
149  if ( enable ) {
150  d->mAllowedTypes |= contentType;
151  } else {
152  d->mAllowedTypes &= ~contentType;
153  }
154 
155  emit logStateChanged();
156 }
157 
158 bool FilterLog::isContentTypeEnabled( ContentType contentType ) const
159 {
160  return ( d->mAllowedTypes & contentType );
161 }
162 
163 void FilterLog::add( const QString &logEntry, ContentType contentType )
164 {
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;
171  }
172 
173  d->mLogEntries.append( timedLog );
174  emit logEntryAdded( timedLog );
175  d->mCurrentLogSize += timedLog.length();
176  d->checkLogSize();
177  }
178 }
179 
180 void FilterLog::addSeparator()
181 {
182  add( QLatin1String("------------------------------"), Meta );
183 }
184 
185 void FilterLog::clear()
186 {
187  d->mLogEntries.clear();
188  d->mCurrentLogSize = 0;
189 }
190 
191 QStringList FilterLog::logEntries() const
192 {
193  return d->mLogEntries;
194 }
195 
196 void FilterLog::dump()
197 {
198 #ifndef NDEBUG
199  kDebug() << "----- starting filter log -----";
200  foreach ( const QString &entry, d->mLogEntries ) {
201  kDebug() << entry;
202  }
203  kDebug() << "------ end of filter log ------";
204 #endif
205 }
206 
207 bool FilterLog::saveToFile( const QString &fileName ) const
208 {
209  QFile file( fileName );
210  if ( !file.open( QIODevice::WriteOnly ) ) {
211  return false;
212  }
213 
214  fchmod( file.handle(), MessageViewer::Util::getWritePermissions() );
215 
216  file.write( "<html>\n<body>\n" );
217  file.write( "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
218  foreach ( const QString &entry, d->mLogEntries ) {
219  const QString line = QLatin1String( "<p>" ) + entry + QLatin1String( "</p>" ) + QLatin1Char( '\n' );
220  file.write( line.toLocal8Bit() );
221  }
222  file.write( "</body>\n</html>\n" );
223  file.close();
224  return true;
225 }
226 
227 QString FilterLog::recode( const QString &plain )
228 {
229  return Qt::escape( plain );
230 }
231 
232 #include "filterlog.moc"
MailCommon::FilterLog::maxLogSize
long maxLogSize() const
Returns the maximum size of the log in bytes.
Definition: filterlog.cpp:142
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:227
MailCommon::FilterLog::Meta
Log all meta data.
Definition: filterlog.h:75
MailCommon::FilterLog::logEntries
QStringList logEntries() const
Returns the list of log entries.
Definition: filterlog.cpp:191
MailCommon::FilterLog::setMaxLogSize
void setMaxLogSize(long size=-1)
Sets the maximum size of the log in bytes.
Definition: filterlog.cpp:126
MailCommon::FilterLog::instance
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:106
MailCommon::FilterLog
KMail Filter Log Collector.
Definition: filterlog.h:56
filterlog.h
MailCommon::FilterLog::~FilterLog
virtual ~FilterLog()
Destroys the filter log.
Definition: filterlog.cpp:101
MailCommon::FilterLog::isLogging
bool isLogging() const
Returns whether the filter log is currently active.
Definition: filterlog.cpp:115
MailCommon::FilterLog::saveToFile
bool saveToFile(const QString &fileName) const
Saves the log to the file with the given fileName.
Definition: filterlog.cpp:207
MailCommon::FilterLog::setLogging
void setLogging(bool active)
Sets whether the filter log is currently active.
Definition: filterlog.cpp:120
MailCommon::FilterLog::addSeparator
void addSeparator()
Adds a separator line to the log.
Definition: filterlog.cpp:180
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:76
MailCommon::FilterLog::clear
void clear()
Clears the log.
Definition: filterlog.cpp:185
MailCommon::FilterLog::AppliedAction
Log all applied actions.
Definition: filterlog.h:79
MailCommon::FilterLog::logEntryAdded
void logEntryAdded(const QString &entry)
This signal is emitted whenever a new entry has been added to the log.
MailCommon::FilterLog::PatternResult
Log all pattern matching results.
Definition: filterlog.h:78
MailCommon::FilterLog::setContentTypeEnabled
void setContentTypeEnabled(ContentType type, bool enabled)
Sets whether a given content type will be enabled for logging.
Definition: filterlog.cpp:147
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:163
MailCommon::FilterLog::isContentTypeEnabled
bool isContentTypeEnabled(ContentType type) const
Returns whether the given content type is enabled for logging.
Definition: filterlog.cpp:158
MailCommon::FilterLog::ContentType
ContentType
Describes the type of content that will be logged.
Definition: filterlog.h:74
MailCommon::FilterLog::RuleResult
Log all rule matching results.
Definition: filterlog.h:77
MailCommon::FilterLog::dump
void dump()
Dumps the log to console.
Definition: filterlog.cpp:196
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:14 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

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