summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/private/kopeteemoticons.h
blob: 848185e6526d435439ba4a62c58de5bcf720996c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
    kopeteemoticons.cpp - Kopete Preferences Container-Class

    Copyright (c) 2002-2003 by Stefan Gehn            <metz AT gehn.net>
    Kopete    (c) 2002-2004 by the Kopete developers  <kopete-devel@kde.org>
    Copyright (c) 2005      by Engin AYDOGAN	      <engin @ bzzzt.biz>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#ifndef kopeteemoticons_h__
#define kopeteemoticons_h__

#include <qobject.h>
#include <qvaluelist.h>
#include <qregexp.h>

#include "kopete_export.h"

namespace Kopete {

class KOPETE_EXPORT Emoticons : public QObject
{
	Q_OBJECT
public:
	/**
	 * Constructor: DON'T use it if you want to use the emoticon theme
	 * chosen by the user.
	 * Instead, use @ref Kopete::Emoticons::self()
	 **/
	Emoticons( const QString &theme = QString::null );

	~Emoticons(  );

	/**
	 * The emoticons container-class by default is a singleton object.
	 * Use this method to retrieve the instance.
	 */
	static Emoticons *self();

	/**
	 * The possible parse modes
	 */
	enum ParseMode {  DefaultParseMode = 0x0 ,  /**  Use strict or relaxed according the config  */
			StrictParse = 0x1,			/** Strict parsing requires a space between each emoticon */
			RelaxedParse = 0x4,         /** Parse mode where all possible emoticon matches are allowed */
			SkipHTML = 0x2				/** Skip emoticons within HTML */
		 };

	/**
	 * Use it to parse emoticons in a text.
	 * You don't need to use this for chat windows,
	 * There is a special class that abstract a chat view
	 * and uses emoticons parser.
	 * This function will use the selected emoticon theme.
	 * If nicks is provided, they will not be parsed if they 
	 * exist in message.
	 */
	static QString parseEmoticons( const QString &message, ParseMode = SkipHTML ) ;

	
	QString parse( const QString &message, ParseMode = SkipHTML );

	/**
	 * TokenType, a token might be an image ( emoticon ) or text.
	 */
	enum TokenType { Undefined, /** Undefined, for completeness only */
					 Image, 	/** Token contains a path to an image */
					 Text 		/** Token contains test */
				   };
	
	/**
	 * A token consists of a QString text which is either a regular text
	 * or a path to image depending on the type.
	 * If type is Image the text refers to an image path.
	 * If type is Text the text refers to a regular text.
	 */
	struct Token {
		Token() : type( Undefined ) {}
		Token( TokenType t, const QString &m ) : type( t ), text(m) {}
		Token( TokenType t, const QString &m, const QString &p, const QString &html )
		 : type( t ), text( m ), picPath( p ), picHTMLCode( html ) {}
		TokenType	type;
		QString		text;
		QString		picPath;
		QString		picHTMLCode;
	};
	

	/**
	 * Static function which will call tokenize
	 * @see tokenize( const QString& )
	 */
	static QValueList<Token> tokenizeEmoticons( const QString &message, ParseMode mode = DefaultParseMode );

	/**
	 * Tokenizes an message.
	 * For example;
	 * Assume :], (H), :-x are three emoticons.
	 * A text "(H)(H) foo bar john :] :-x" would be tokenized as follows (not strict):
	 * 1- /path/to/shades.png
	 * 2- /path/to/shades.png
	 * 3- " foo bar john "
	 * 4- /path/to/bat.png
	 * 5- " "
	 * 6- /path/to/kiss.png
	 *
	 * Strict tokenization (require spaces around emoticons):
	 * 1- "(H)(H) foo bar john "
	 * 2- /path/to/bat.png
	 * 3- " "
	 * 4- /path/to/kiss.png
	 * Note: quotation marks are used to emphasize white spaces.
	 * @param message is the message to tokenize
	 * @param mode is a bitmask of ParseMode enum
	 * @return a QValueList which consiste of ordered tokens of the text.
	 * @author Engin AYDOGAN < engin@bzzzt.biz >
	 * @since 23-03-05
	 */
	QValueList<Token> tokenize( const QString &message, uint mode = DefaultParseMode );
	
	/**
	 * Return all emoticons and the corresponding icon.
	 * (only one emoticon per image)
	 */
	QMap<QString, QStringList> emoticonAndPicList();


private:
	/**
	 * Our instance
	 **/
	static Emoticons *s_self;

	/**
	 * add an emoticon to our mapping if
	 * an animation/pixmap has been found for it
	 **/
	void addIfPossible( const QString& filenameNoExt, const QStringList &emoticons );
	
	/**
	 * uses the kopete's emoticons.xml  for the theme
	 * @see initEmoticons
	 */
	void initEmoticon_emoticonsxml( const QString & filename);
	
	/**
	 * uses the JEP-0038 xml description for the theme
	 * @see initEmoticons
	 */
	void initEmoticon_JEP0038( const QString & filename);
	
	/**
	 * sorts emoticons for convenient parsing, which yields greedy matching on
	 * matchText
	 */
	void sortEmoticons();


	struct Emoticon;
	struct EmoticonNode;
	class Private;
	Private *d;
private slots:

	/**
	 * Fills the map with paths and emoticons
	 * This needs to be done on every emoticon-theme change
	 **/
	void initEmoticons ( const QString &theme = QString::null );
};


} //END namespace Kopete

#endif
// vim: set noet ts=4 sts=4 sw=4: