File indexing completed on 2024-05-19 15:10:52

0001 /*
0002     SPDX-FileCopyrightText: 2002-2008 The Kopete developers <kopete-devel@kde.org>
0003     SPDX-FileCopyrightText: 2008 Carlo Segato <brandon.ml@gmail.com>
0004     SPDX-FileCopyrightText: 2002-2003 Stefan Gehn <metz@gehn.net>
0005     SPDX-FileCopyrightText: 2005 Engin AYDOGAN <engin@bzzzt.biz>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #ifndef KEMOTICONS_THEME_H
0011 #define KEMOTICONS_THEME_H
0012 
0013 #include "kemoticonsprovider.h"
0014 
0015 #include <QSharedDataPointer>
0016 
0017 class QString;
0018 
0019 /**
0020  * This class contains the emoticons theme
0021  */
0022 class KEMOTICONS_EXPORT KEmoticonsTheme
0023 {
0024 public:
0025 
0026     /**
0027      * The possible parse modes
0028      */
0029     enum ParseModeEnum {
0030         DefaultParse = 0x0,      /**< Use strict or relaxed according to the config  */
0031         StrictParse = 0x1,       /**< Strict parsing requires a space between each emoticon */
0032         RelaxedParse = 0x2,      /**< Parse mode where all possible emoticon matches are allowed */
0033         SkipHTML = 0x4           /**< Skip emoticons within HTML */
0034     };
0035 
0036     Q_DECLARE_FLAGS(ParseMode, ParseModeEnum)
0037 
0038     /**
0039      * TokenType, a token might be an image ( emoticon ) or text.
0040      */
0041     enum TokenType {
0042         Undefined, /**< Undefined, for completeness only */
0043         Image,     /**< Token contains a path to an image */
0044         Text       /**< Token contains text */
0045     };
0046 
0047     /**
0048      * A token consists of a QString text which is either a regular text
0049      * or a path to image depending on the type.
0050      * If type is Image the text refers to an image path.
0051      * If type is Text the text refers to a regular text.
0052      */
0053     struct Token {
0054         Token() : type(Undefined) {}
0055         /**
0056          * Creates a Token of type @p t, and text @p m
0057          */
0058         Token(TokenType t, const QString &m) : type(t), text(m) {}
0059         /**
0060          * Creates a Token of type @p t, text @p m, image path @p p and html code @p html
0061          */
0062         Token(TokenType t, const QString &m, const QString &p, const QString &html)
0063             : type(t), text(m), picPath(p), picHTMLCode(html) {}
0064         TokenType   type; /**< type */
0065         QString     text; /**< text */
0066         QString     picPath; /**< path to the image */
0067         QString     picHTMLCode; /**< \<img> html code */
0068     };
0069 
0070     /**
0071      * Default constructor, it creates a null emoticon theme
0072      * You should probably never use this, use KEmoticons::theme() instead
0073      */
0074     KEmoticonsTheme();
0075 
0076     /**
0077      * Copy constructor
0078      */
0079     KEmoticonsTheme(const KEmoticonsTheme &ket);
0080 
0081     /**
0082      * Another constructor where you set the KEmoticonsProvider @p p
0083      * You should probably never use this, use KEmoticons::theme() instead
0084      */
0085     KEmoticonsTheme(KEmoticonsProvider *p);
0086 
0087     /**
0088      * Destructor
0089      */
0090     ~KEmoticonsTheme();
0091 
0092     /**
0093      * Parses emoticons in text @p text with ParseMode @p mode and optionally excluding emoticons from @p exclude
0094      * @code
0095      * KEmoticonsTheme theme = KEmoticons().theme();
0096      * QString text = ":D hi :)";
0097      * QStringList exclude(":)");
0098      * QString parsed = theme.parseEmoticons(text, KEmoticonsTheme::DefaultParse, exclude);
0099      * // parsed will be "<img align="center" title=":D" alt=":D" src="/path/to/:D.png" width="24" height="24" /> hi :)"
0100      * @endcode
0101      * @param text the text to parse
0102      * @param mode how to parse the text
0103      * @param exclude a list of emoticons to exclude from the parsing
0104      * @return the text with emoticons replaced by html images
0105      * @note SkipHTML is forced when using this function
0106      */
0107     QString parseEmoticons(const QString &text, ParseMode mode = DefaultParse, const QStringList &exclude = QStringList()) const;
0108 
0109     /**
0110      * Tokenizes the message @p message with ParseMode @p mode
0111      * @code
0112      * KEmoticonsTheme theme = KEmoticons().theme();
0113      * QString text = "hi :)";
0114      * QList<Token> tokens = theme.tokenize(text, KEmoticonsTheme::DefaultParse);
0115      * // tokens[0].text = "hi "
0116      * // tokens[1].text = ":)"
0117      * // tokens[1].picPath = "/path/to/:).png"
0118      * // tokens[1].picHTMLCode = "<img align="center" title=":)" alt=":)" src="/path/to/:).png" width="24" height="24" />"
0119      * @endcode
0120      */
0121     QList<Token> tokenize(const QString &message, ParseMode mode = DefaultParse) const;
0122 
0123 #if KEMOTICONS_ENABLE_DEPRECATED_SINCE(5, 0)
0124     /**
0125      * Loads the emoticon theme inside the directory @p path
0126      * @param path path to the directory
0127      * @return @c true if the emoticon is successfully loaded
0128      *
0129      * @deprecated since 5.0, subclass KEmoticonsProvider instead
0130      */
0131     KEMOTICONS_DEPRECATED_VERSION(5, 0, "Subclass KEmoticonsProvider")
0132     bool loadTheme(const QString &path);
0133 #endif
0134 
0135 #if KEMOTICONS_ENABLE_DEPRECATED_SINCE(5, 0)
0136     /**
0137      * Removes the emoticon @p emo. This doesn't delete the image file.
0138      * @code
0139      * KEmoticonsTheme theme = KEmoticons().theme();
0140      * theme.removeEmoticon(":)");
0141      * @endcode
0142      * @param emo the emoticon text to remove
0143      * @return @c true if the emoticon is successfully removed
0144      *
0145      * @deprecated since 5.0, subclass KEmoticonsProvider instead
0146      */
0147     KEMOTICONS_DEPRECATED_VERSION(5, 0, "Subclass KEmoticonsProvider")
0148     bool removeEmoticon(const QString &emo);
0149 #endif
0150 
0151 #if KEMOTICONS_ENABLE_DEPRECATED_SINCE(5, 0)
0152     /**
0153      * Adds the emoticon @p emo with text @p text
0154      * @code
0155      * KEmoticonsTheme theme = KEmoticons().theme();
0156      * theme.addEmoticon("/path/to/smiley.png", ":) :-)");
0157      * @endcode
0158      * @param emo path to the emoticon image
0159      * @param text the emoticon text. If alternative texts are to be added,
0160      * use spaces to separate them.
0161      * @param copy whether or not to copy @p emo into the theme directory
0162      * @return @c true if the emoticon is successfully added
0163      *
0164      * @deprecated since 5.0, subclass KEmoticonsProvider instead
0165      */
0166     KEMOTICONS_DEPRECATED_VERSION(5, 0, "Subclass KEmoticonsProvider")
0167     bool addEmoticon(const QString &emo,
0168                      const QString &text,
0169                      KEmoticonsProvider::AddEmoticonOption option = KEmoticonsProvider::DoNotCopy);
0170 #endif
0171 
0172 #if KEMOTICONS_ENABLE_DEPRECATED_SINCE(5, 0)
0173     /**
0174      * Saves the emoticon theme
0175      *
0176      * @deprecated since 5.0, subclass KEmoticonsProvider instead
0177      */
0178     KEMOTICONS_DEPRECATED_VERSION(5, 0, "Subclass KEmoticonsProvider")
0179     void save();
0180 #endif
0181 
0182     /**
0183      * Returns the theme name
0184      */
0185     QString themeName() const;
0186 
0187     /**
0188      * Sets the emoticon theme name
0189      * @param name name of the theme
0190      */
0191     void setThemeName(const QString &name);
0192 
0193     /**
0194      * Returns the emoticon theme path
0195      */
0196     QString themePath() const;
0197 
0198     /**
0199      * Returns the file name of the emoticon theme
0200      */
0201     QString fileName() const;
0202 
0203     /**
0204      * Returns a QHash that contains the emoticon path as keys and the text as values
0205      */
0206     QHash<QString, QStringList> emoticonsMap() const;
0207 
0208 #if KEMOTICONS_ENABLE_DEPRECATED_SINCE(5, 0)
0209     /**
0210      * Creates a new theme
0211      *
0212      * @deprecated since 5.0, subclass KEmoticonsProvider instead
0213      */
0214     KEMOTICONS_DEPRECATED_VERSION(5, 0, "Subclass KEmoticonsProvider")
0215     void createNew();
0216 #endif
0217 
0218     /**
0219      * Checks if the emoticon theme has a valid provider
0220      * @return true if it can't find a valid provider
0221      */
0222     bool isNull() const;
0223 
0224     /**
0225      * @internal
0226      */
0227     KEmoticonsTheme &operator=(const KEmoticonsTheme &ket);
0228 private:
0229     class KEmoticonsThemeData;
0230     /**
0231      * Private implementation class
0232      */
0233     QSharedDataPointer<KEmoticonsThemeData> d;
0234 };
0235 
0236 Q_DECLARE_OPERATORS_FOR_FLAGS(KEmoticonsTheme::ParseMode)
0237 
0238 #endif /* KEMOTICONS_THEME_H */
0239