File indexing completed on 2024-09-22 04:50:04

0001 /*
0002   SPDX-FileCopyrightText: 2003 Andreas Gungl <a.gungl@gmx.de>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #pragma once
0008 
0009 #include "mailcommon_export.h"
0010 
0011 #include <QObject>
0012 #include <QStringList>
0013 #include <memory>
0014 namespace MailCommon
0015 {
0016 /**
0017  * @short KMail Filter Log Collector.
0018  *
0019  * The filter log helps to collect log information about the
0020  * filter process in KMail. It's implemented as singleton,
0021  * so it's easy to direct pieces of information to a unique
0022  * instance.
0023  * It's possible to activate / deactivate logging. All
0024  * collected log information can get thrown away, the
0025  * next added log entry is the first one until another
0026  * clearing.
0027  * A signal is emitted whenever a new logentry is added,
0028  * when the log was cleared or any log state was changed.
0029  *
0030  * @author Andreas Gungl <a.gungl@gmx.de>
0031  */
0032 class MAILCOMMON_EXPORT FilterLog : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     /**
0038      * Destroys the filter log.
0039      */
0040     ~FilterLog() override;
0041 
0042     /**
0043      * Returns the single global instance of the filter log.
0044      */
0045     static FilterLog *instance();
0046 
0047     /**
0048      * Describes the type of content that will be logged.
0049      */
0050     enum ContentType {
0051         Meta = 1, ///< Log all meta data.
0052         PatternDescription = 2, ///< Log all pattern description.
0053         RuleResult = 4, ///< Log all rule matching results.
0054         PatternResult = 8, ///< Log all pattern matching results.
0055         AppliedAction = 16 ///< Log all applied actions.
0056     };
0057 
0058     /**
0059      * Sets whether the filter log is currently @p active.
0060      */
0061     void setLogging(bool active);
0062 
0063     /**
0064      * Returns whether the filter log is currently active.
0065      */
0066     [[nodiscard]] bool isLogging() const;
0067 
0068     /**
0069      * Sets the maximum @p size of the log in bytes.
0070      */
0071     void setMaxLogSize(long size = -1);
0072 
0073     /**
0074      * Returns the maximum size of the log in bytes.
0075      */
0076     [[nodiscard]] long maxLogSize() const;
0077 
0078     /**
0079      * Sets whether a given content @p type will be @p enabled for logging.
0080      */
0081     void setContentTypeEnabled(ContentType type, bool enabled);
0082 
0083     /**
0084      * Returns whether the given content @p type is enabled for logging.
0085      */
0086     [[nodiscard]] bool isContentTypeEnabled(ContentType type) const;
0087 
0088     /**
0089      * Adds the given log @p entry under the given content @p type to the log.
0090      */
0091     void add(const QString &entry, ContentType type);
0092 
0093     /**
0094      * Adds a separator line to the log.
0095      */
0096     void addSeparator();
0097 
0098     /**
0099      * Clears the log.
0100      */
0101     void clear();
0102 
0103     /**
0104      * Returns the list of log entries.
0105      */
0106     [[nodiscard]] QStringList logEntries() const;
0107 
0108     /**
0109      * Saves the log to the file with the given @p fileName.
0110      *
0111      * @return @c true on success or @c false on failure.
0112      */
0113     bool saveToFile(const QString &fileName) const;
0114 
0115     /**
0116      * Returns an escaped version of the log which can be used
0117      * in a HTML document.
0118      */
0119     [[nodiscard]] static QString recode(const QString &plain);
0120 
0121     /**
0122      * Dumps the log to console. Used for debugging.
0123      */
0124     void dump();
0125 
0126 Q_SIGNALS:
0127     /**
0128      * This signal is emitted whenever a new @p entry has been added to the log.
0129      */
0130     void logEntryAdded(const QString &entry);
0131 
0132     /**
0133      * This signal is emitted whenever the log has shrunk.
0134      */
0135     void logShrinked();
0136 
0137     /**
0138      * This signal is emitted whenever the activity of the filter log has been changed.
0139      */
0140     void logStateChanged();
0141 
0142 private:
0143     //@cond PRIVATE
0144     MAILCOMMON_NO_EXPORT FilterLog();
0145 
0146     class FilterLogPrivate;
0147     std::unique_ptr<FilterLogPrivate> const d;
0148     //@endcond
0149 };
0150 }