Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / groupwise / libgroupwise / rtf2html.h
bloba305b89d7f50bfa7380eb153f7c98df378d442b0
1 /*
2 rtf2html.h - A simple RTF Parser
4 Copyright (c) 2002 by Vladimir Shutoff <vovan@shutoff.ru> (original code)
5 Copyright (c) 2004 by Thiago S. Barcelos <barcelos@ime.usp.br> (Kopete port)
6 Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 *************************************************************************
18 #ifndef RTF2HTML_H
19 #define RTF2HTML_H
21 #include <qstring.h>
22 #include <stdio.h>
24 #include <qtextcodec.h>
25 #include <qcolor.h>
26 #include <qregexp.h>
27 #include <kdebug.h>
29 #include <vector>
30 #include <stack>
31 #include <string>
32 #include <stdarg.h>
34 using namespace std;
36 struct FontDef
38 int charset;
39 string taggedName;
40 string nonTaggedName;
43 class RTF2HTML;
45 enum TagEnum
47 TAG_ALL = 0,
48 TAG_FONT_SIZE,
49 TAG_FONT_COLOR,
50 TAG_FONT_FAMILY,
51 TAG_BG_COLOR,
52 TAG_BOLD,
53 TAG_ITALIC,
54 TAG_UNDERLINE
57 class ParStyle
59 public:
60 ParStyle() { dir = DirLTR; }
61 void clearFormatting();
63 public:
64 enum {DirLTR, DirRTL} dir;
67 class Level
69 public:
70 Level(RTF2HTML *_p);
71 Level(const Level&);
72 void setText(const char* str);
73 void setFontTbl() { m_bFontTbl = true; }
74 void setColors() { m_bColors = true; resetColors(); }
75 void setRed(unsigned char val) { setColor(val, &m_nRed); }
76 void setGreen(unsigned char val) { setColor(val, &m_nGreen); }
77 void setBlue(unsigned char val) { setColor(val, &m_nBlue); }
78 void setFont(unsigned nFont);
79 void setEncoding(unsigned nFont);
80 void setFontName();
81 void setFontColor(unsigned short color);
82 void setFontBgColor(unsigned short color);
83 void setFontSizeHalfPoints(unsigned short sizeInHalfPoints);
84 void setFontSize(unsigned short sizeInPoints);
85 void setBold(bool);
86 void setItalic(bool);
87 void setUnderline(bool);
88 void startParagraph();
89 bool isParagraphOpen() const;
90 void clearParagraphFormatting();
91 void setParagraphDirLTR();
92 void setParagraphDirRTL();
93 void addLineBreak();
94 void flush();
95 void reset();
96 void resetTag(TagEnum tag);
97 protected:
98 string text;
99 void Init();
100 RTF2HTML *p;
101 void resetColors() { m_nRed = m_nGreen = m_nBlue = 0; m_bColorInit = false; }
102 void setColor(unsigned char val, unsigned char *p)
103 { *p = val; m_bColorInit=true; }
105 // Marks the position in m_tags where this level begun.
106 unsigned m_nTagsStartPos;
108 // True when parsing the fonts table
109 bool m_bFontTbl;
110 // True when parsing the colors table.
111 bool m_bColors;
112 // True when inside a 'fname' block.
113 bool m_bFontName;
114 // False until we get the tagged font name.
115 bool m_bTaggedFontNameOk;
117 unsigned char m_nRed;
118 unsigned char m_nGreen;
119 unsigned char m_nBlue;
120 bool m_bColorInit;
121 unsigned m_nFont; // 1-based
122 unsigned m_nEncoding;
123 unsigned m_nFontColor; // 1-based
124 unsigned m_nFontSize;
125 unsigned m_nFontBgColor; // 1-based
126 bool m_bBold;
127 bool m_bItalic;
128 bool m_bUnderline;
131 class OutTag
133 public:
134 OutTag(TagEnum _tag, unsigned _param) : tag(_tag), param(_param) {}
135 TagEnum tag;
136 unsigned param;
139 enum quoteMode
141 quoteHTML,
142 quoteXML,
143 quoteNOBR
146 class RTF2HTML
148 friend class Level;
150 public:
151 RTF2HTML();
152 QString Parse(const char *rtf, const char *encoding);
154 // Paragraph-specific functions:
156 QString quoteString(const QString &_str, quoteMode mode = quoteHTML);
157 // Appends a string with formatting into the paragraph buffer.
158 void PrintUnquoted(const char *str, ...);
159 // Quotes and appends a string to the paragraph buffer.
160 void PrintQuoted(const QString &str);
161 // Writes down the tags from oTags into the paragraph buffer.
162 void FlushOutTags();
163 // Retrieves the top not-yet-written tag.
164 OutTag* getTopOutTag(TagEnum tagType);
165 // Writes down the paragraph buffer and resets the paragraph state.
166 void FlushParagraph();
168 // Document-wide functions:
170 void PutTag(TagEnum n)
172 tags.push(n);
175 protected:
177 // Paragraph members
179 // True if the paragraph was opened explicitly.
180 bool bExplicitParagraph;
181 // The paragraph's HTML buffer.
182 QString sParagraph;
183 // Defines the paragraph's formatting.
184 ParStyle parStyle;
185 // Tags which weren't yet printed out.
186 vector<OutTag> oTags;
188 // Document members
190 // The document HTML buffer.
191 QString s;
192 // Fonts table.
193 vector<FontDef> fonts;
194 // Colors table.
195 vector<QColor> colors;
196 // Stack of tags (across all levels, not just current level)
197 stack<TagEnum> tags;
199 // RTF parser internals
201 const char *rtf_ptr;
202 const char *encoding;
203 Level cur_level;
204 stack<Level> levels;
207 #endif