libqutim 0.2.80.1

emoticons.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** qutIM - instant messenger
00004 **
00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
00006 **
00007 *****************************************************************************
00008 **
00009 ** $QUTIM_BEGIN_LICENSE$
00010 ** This program is free software: you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation, either version 3 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 ** See the GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program.  If not, see http://www.gnu.org/licenses/.
00022 ** $QUTIM_END_LICENSE$
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef EMOTICONS_H
00027 #define EMOTICONS_H
00028 
00029 #include "libqutim_global.h"
00030 #include <QSharedData>
00031 #include <QStringList>
00032 
00033 namespace qutim_sdk_0_3
00034 {
00035 class EmoticonsProvider;
00036 struct EmoticonsThemeData;
00037 struct EmoticonsProviderPrivate;
00038 
00039 class LIBQUTIM_EXPORT EmoticonsTheme
00040 {
00041 public:
00042     enum ParseModeFlag
00043     {
00044         DefaultParse = 0,
00045         StrictParse  = 0x01,
00046     };
00047     Q_DECLARE_FLAGS(ParseMode, ParseModeFlag)
00048 
00049     enum TokenType
00050     {
00051         Undefined,
00052         Text,
00053         Image
00054     };
00055 
00056     struct Token
00057     {
00058         inline Token() : type(Undefined) {}
00059         inline Token(const QString &t) : type(Text), text(t) {}
00060         inline Token(const QString &t, const QString &p, const QString &h)
00061             : type(Image), text(t), imgPath(p), imgHtmlCode(h) {}
00062         TokenType type;
00063         QString text;
00064         QString imgPath;
00065         QString imgHtmlCode;
00066     };
00067 
00068     EmoticonsTheme(const QString &name = QString());
00069     EmoticonsTheme(EmoticonsThemeData *data);
00070     EmoticonsTheme(const EmoticonsTheme &theme);
00071     ~EmoticonsTheme();
00072     EmoticonsTheme &operator =(const EmoticonsTheme &theme);
00073 
00074     bool isNull() const;
00075 
00076     QHash<QString, QStringList> emoticonsMap() const;
00077     QStringList emoticonsIndexes() const;
00078     QString themeName() const;
00079 
00080     //      EmoticonsTheme pseudoClone();
00081 
00082     QString parseEmoticons(const QString &text, ParseMode mode = DefaultParse, const QStringList &exclude = QStringList());
00083     QList<Token> tokenize(const QString &text, ParseMode mode = DefaultParse);
00084 private:
00085     QExplicitlySharedDataPointer<EmoticonsThemeData> p;
00086 };
00087 
00088 class LIBQUTIM_EXPORT EmoticonsProvider
00089 {
00090 public:
00091     // Emoticon has the same structure as in KDE for binary hacks
00092     struct Emoticon
00093     {
00094         Emoticon(){}
00095         /* sort by longest to shortest matchText */
00096         bool operator < (const Emoticon &e) const { return matchText.length() > e.matchText.length(); }
00097         QString matchText;
00098         QString matchTextEscaped;
00099         QString picPath;
00100         QString picHTMLCode;
00101     };
00102     EmoticonsProvider();
00103     virtual ~EmoticonsProvider();
00104     QHash<QString, QStringList> emoticonsMap() const;
00105     QStringList emoticonsIndexes() const;
00106     QHash<QChar, QList<Emoticon> > emoticonsByChar() const;
00107     virtual QString themeName() const;
00108     virtual bool saveTheme();
00109     // Warning: QStringList must contain lower-case strings
00110     virtual bool addEmoticon(const QString &imgPath, const QStringList &codes);
00111     virtual bool removeEmoticon(const QStringList &codes);
00112 protected:
00113     void clearEmoticons();
00114     void appendEmoticon(const QString &imgPath, const QStringList &codes);
00115     void removeEmoticon(const QString &imgPath, const QStringList &codes);
00116 private:
00117     QScopedPointer<EmoticonsProviderPrivate> p;
00118 };
00119 
00120 class LIBQUTIM_EXPORT EmoticonsBackend : public QObject
00121 {
00122     Q_OBJECT
00123 public:
00124     virtual QStringList themeList() = 0;
00125     virtual EmoticonsProvider *loadTheme(const QString &name) = 0;
00126 };
00127 
00128 namespace Emoticons
00129 {
00130 LIBQUTIM_EXPORT EmoticonsTheme theme();
00131 LIBQUTIM_EXPORT QString currentThemeName();
00132 LIBQUTIM_EXPORT EmoticonsTheme theme(const QString &name);
00133 LIBQUTIM_EXPORT QStringList themeList();
00134 LIBQUTIM_EXPORT void setTheme(const QString &name);
00135 LIBQUTIM_EXPORT void setTheme(const EmoticonsTheme &theme);
00136 }
00137 }
00138 
00139 Q_DECLARE_OPERATORS_FOR_FLAGS(qutim_sdk_0_3::EmoticonsTheme::ParseMode)
00140 
00141 #endif // EMOTICONS_H
00142