1 /*
2  * %kadu copyright begin%
3  * Copyright 2012 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "expander/emoticon-path-provider.h"
23 
24 #include "dom/dom-visitor.h"
25 
26 #include <memory>
27 
28 class EmoticonPrefixTree;
29 
30 /**
31  * @addtogroup Emoticons
32  * @{
33  */
34 
35 /**
36  * @class EmoticonExpander
37  * @short This DomVisitor expands emoticons in all text nodes in processed DOM document.
38  * @author Rafał 'Vogel' Malinowski
39  */
40 class EmoticonExpander : public DomVisitor
41 {
42 	EmoticonPrefixTree *m_tree;
43 	std::unique_ptr<EmoticonPathProvider> m_pathProvider;
44 
45 	/**
46 	 * @short Insertes emoticon img element at index.
47 	 * @author Rafał 'Vogel' Malinowski
48 	 * @param textNode text node to insert emoticon into
49 	 * @param emoticon emoticon to insert
50 	 * @param index index of first charater of emoticon in textNode text content
51 	 * @return new text node created with text after emoticon
52 	 *
53 	 * This method splits textNode into two text nodes. First one contains text before emoticon, second one contains
54 	 * text after emoticon. New img element with emoticon is inserted between these two nodes.
55 	 *
56 	 * New text node is returned. Searching for emoticons can be started again for this node.
57 	 */
58 	QDomText insertEmoticon(QDomText textNode, const Emoticon &emoticon, int index) const;
59 
60 	/**
61 	 * @short Expands first found emoticon.
62 	 * @author Rafał 'Vogel' Malinowski
63 	 * @param textNode text node to expand first emoticon in
64 	 * @return new text node created with text after first emoticon
65 	 *
66 	 * This node search for first emoticon into text in textNode. If no emoticon is found then null QDomText object
67 	 * is returned. If emoticon is found then it is expanded and new text node that contains text after this emoticon
68 	 * is returned. Searching for emoticons can be started again for this node.
69 	 */
70 	QDomText expandFirstEmoticon(QDomText textNode) const;
71 
72 	virtual QDomNode beginVisit(QDomElement elementNode) const;
73 	virtual QDomNode endVisit(QDomElement elementNode) const;
74 
75 public:
76 	/**
77 	 * @short Create new EmoticonExpander instance.
78 	 * @author Rafał 'Vogel' Malinowski
79 	 * @param tree emoticon prefix tree used to expand emoticons
80 	 * @param pathProvider provider that changed emoticon instances into paths to emoticon files
81 	 *
82 	 * New object takes ownership of pathProvider object but not on tree object. Tree object must not be destroyed
83 	 * during EmoticonExpander lifetime.
84 	 */
85 	explicit EmoticonExpander(EmoticonPrefixTree *tree, std::unique_ptr<EmoticonPathProvider> pathProvider);
86 	virtual ~EmoticonExpander();
87 
88 	/**
89 	 * @short Expands emoticons in given text node.
90 	 * @author Rafał 'Vogel' Malinowski
91 	 * @param textNode text node to expand emoticons in
92 	 *
93 	 * This method expands emoticons in given text node. After that new text nodes and elements with img tag names will
94 	 * be inserted after given textNode. Also text content of current textNode will be cut to first emoticon occurence.
95 	 */
96 	virtual QDomNode visit(QDomText textNode) const;
97 
98 };
99 
100 /**
101  * @}
102  */
103