initial import
[vym] / noteobj.cpp
1 #include <qfile.h>
2 #include <qtextstream.h>
3 #include <qmessagebox.h>
4 #include <qregexp.h>
5
6 #include "noteobj.h"
7
8 /////////////////////////////////////////////////////////////////
9 // NoteObj
10 /////////////////////////////////////////////////////////////////
11
12 NoteObj::NoteObj()
13 {
14         clear();
15 }
16
17 NoteObj::NoteObj(const QString &s)
18 {
19         clear();
20         note=s;
21 }
22
23 void NoteObj::copy (NoteObj other)
24 {
25         note=other.note;
26         fonthint=other.fonthint;
27         filenamehint="";
28 }
29
30 void NoteObj::clear()
31 {
32         note="";
33         fonthint="undef";
34         filenamehint="";
35 }
36
37 void NoteObj::setNote (const QString &s)
38 {
39         note=s;
40 }
41
42 QString NoteObj::getNote()
43 {
44         return note;
45 }
46
47 QString NoteObj::getNoteASCII()
48 {
49         return getNoteASCII (QString(""),80);
50 }
51
52 QString NoteObj::getNoteASCII(const QString &indent, const int &width)
53 {
54         QString r=note;
55
56         // Remove all <style...> ...</style>
57         QRegExp rx ("<style.*>.*</style>");
58         rx.setMinimal(true);
59         r.replace (rx,"");
60
61         // convert all "<br*>" to "\n"
62         rx.setPattern ("<br.*>");
63         r.replace (rx,"\n");
64
65         // convert all "</p>" to "\n"
66         rx.setPattern ("</p>");
67         r.replace (rx,"\n");
68         
69         // remove all remaining tags 
70         rx.setPattern ("<.*>");
71         r.replace (rx,"");
72
73         // If string starts with \n now, remove it.
74         // It would be wrong in an OOo export for example
75         while (r.at(0)=='\n') r.remove (0,1);
76         
77         // convert "&", "<" and ">"
78         rx.setPattern ("&gt;");
79         r.replace (rx,">");
80         rx.setPattern ("&lt;");
81         r.replace (rx,"<");
82         rx.setPattern ("&amp;");
83         r.replace (rx,"&");
84         rx.setPattern ("&quot;");
85         r.replace (rx,"\"");
86
87         // Indent everything
88         rx.setPattern ("^\n");
89         r.replace (rx,indent);
90         r=indent + r;   // Don't forget first line
91
92 /* FIXME        wrap text at width
93         if (fonthint !="fixed")
94         {
95         }
96 */      
97         r=indent+"\n"+r+indent+"\n\n";
98         return r;
99 }
100
101 QString NoteObj::getNoteOpenDoc()
102 {
103         // Evil hack to transform QT Richtext into
104         // something which can be used in OpenDoc format
105         // 
106         // TODO create clean XML transformation which also
107         // considers fonts, colors, ...
108
109         QString r=note;
110
111         // convert all "<br*>"
112         QRegExp re("<br.*>");
113         re.setMinimal(true);
114         r.replace (re,"<text:line-break/>");
115
116         // convert all "<p>" 
117         re.setPattern ("<p>");
118         r.replace (re,"<text:line-break/>");
119         
120         // Remove all other tags, e.g. paragraphs will be added in 
121         // templates used during export
122         re.setPattern ("</?html.*>");
123         r.replace (re,"");
124         re.setPattern ("</?head.*>");
125         r.replace (re,"");
126         re.setPattern ("</?body.*>");
127         r.replace (re,"");
128         re.setPattern ("</?meta.*>");
129         r.replace (re,"");
130         re.setPattern ("</?span.*>");
131         r.replace (re,"");
132         re.setPattern ("</?p.*>");
133         r.replace (re,"");
134
135         r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
136         return r;
137 }
138
139 void NoteObj::setFontHint (const QString &s)
140 {
141         // only for backward compatibility (pre 1.5 )
142         fonthint=s;
143 }
144
145 QString NoteObj::getFontHint()
146 {
147         // only for backward compatibility (pre 1.5 )
148         return fonthint;
149 }
150
151 void NoteObj::setFilenameHint (const QString &s)
152 {
153         filenamehint=s;
154 }
155
156 QString NoteObj::getFilenameHint()
157 {
158         return filenamehint;
159 }
160
161 bool NoteObj::isEmpty ()
162 {
163         return note.isEmpty();
164 }
165
166 QString NoteObj::saveToDir ()
167 {
168         QString n=note;
169
170         // Remove the doctype, which will confuse parsing 
171         // with XmlReader in Qt >= 4.4
172         QRegExp rx("<!DOCTYPE.*>");
173         rx.setMinimal(true);
174         n.replace (rx,"");
175         
176         // QTextEdit may generate fontnames with unquoted &, like
177         // in "Lucida B&H". This is invalid in XML and thus would crash
178         // the XML parser
179
180         // More invalid XML is generated with bullet lists:
181         // There are 2 <style> tags in one <li>, so we merge them here
182         int pos=0;
183         bool inbracket=false;
184         int begin_bracket=0;
185         bool inquot=false;
186         while (pos<n.length())
187         {
188                 if (n.mid(pos,1)=="<") 
189                 {
190                         inbracket=true;
191                         begin_bracket=pos;
192                 }
193                 if (n.mid(pos,1)==">") 
194                 {
195                         inbracket=false;
196                         QString s=n.mid(begin_bracket,pos-begin_bracket+1);
197                         int sl=s.length();
198                         if (s.count("style=\"")>1)
199                         {
200                                 rx.setPattern("style=\\s*\"(.*)\"\\s*style=\\s*\"(.*)\"");
201                                 s.replace(rx,"style=\"\\1 \\2\"");
202                                 n.replace (begin_bracket,sl,s);
203                                 pos=pos-(sl-s.length());
204                         }       
205                 }       
206                 if (n.mid(pos,1)=="\"" && inbracket)
207                 {
208                         if (!inquot)
209                                 inquot=true;
210                         else
211                                 inquot=false;
212                 }
213                 if (n.mid(pos,1)=="&" && inquot)
214                 {
215                         // Now we are inside  <  "  "  >
216                         n.replace(pos,1,"&amp;");
217                         pos=pos+3;
218                 }
219                 pos++;
220         }
221
222         
223         return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
224 }
225