34 #include <qtextcodec.h>
35 #include <qtextdocument.h>
36 #include <QtGui/QApplication>
43 QTextCodec * codec = m_utf8 ? QTextCodec::codecForName(
"UTF-8") : QTextCodec::codecForLocale();
48 if(f.open(QIODevice::ReadOnly)) {
50 static const int g_lineLimit = 16*1024;
51 QByteArray s(g_lineLimit,0);
53 while(f.readLine(s.data(), g_lineLimit) >= 1 && !s.contains(
"<DL>")) {
57 while(
int size = f.readLine(s.data(), g_lineLimit)>=1) {
58 if ( size == g_lineLimit )
60 kWarning() <<
"Netscape bookmarks contain a line longer than " << g_lineLimit <<
". Skipping.";
63 QByteArray t = s.trimmed();
64 if(t.left(12).toUpper() ==
"<DT><A HREF=" ||
65 t.left(16).toUpper() ==
"<DT><H3><A HREF=") {
67 int firstQuotes = t.indexOf(
'"')+1;
68 int secondQuotes = t.indexOf(
'"', firstQuotes);
69 if (firstQuotes != -1 && secondQuotes != -1)
71 QByteArray
link = t.mid(firstQuotes, secondQuotes-firstQuotes);
72 int endTag = t.indexOf(
'>', secondQuotes+1);
74 int closeTag = t.indexOf(
'<', endTag + 1);
76 QByteArray
name = t.mid(endTag + 1, closeTag - endTag - 1);
78 QByteArray additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
81 codec->toUnicode(link),
85 else if(t.left(7).toUpper() ==
"<DT><H3") {
86 int endTag = t.indexOf(
'>', 7);
87 QByteArray
name = t.mid(endTag+1);
88 name = name.left(name.indexOf(
'<'));
90 QByteArray additionalInfo = t.mid( 8, endTag-8 );
91 bool folded = (additionalInfo.left(6) ==
"FOLDED");
92 if (folded) additionalInfo.remove(0,7);
98 else if(t.left(4).toUpper() ==
"<HR>")
100 else if(t.left(8).toUpper() ==
"</DL><P>")
114 i18n(
"*.html|HTML Files (*.html)"),
115 QApplication::activeWindow() );
118 i18n(
"*.html|HTML Files (*.html)"),
119 QApplication::activeWindow() );
123 return QDir::homePath() +
"/.netscape/bookmarks.html";
136 QString errorMsg =
QString(
"Could not find %1. Netscape is probably not installed. "
147 if (!file.open(QIODevice::WriteOnly)) {
152 QTextStream fstream(&file);
153 fstream.setCodec(m_utf8 ? QTextCodec::codecForName(
"UTF-8") : QTextCodec::codecForLocale());
156 = m_utf8 ?
"UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->
name()).toUpper();
158 fstream <<
"<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
159 <<
i18n(
"<!-- This file was generated by Konqueror -->") << endl
160 <<
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
161 << charset <<
"\">" << endl
162 <<
"<TITLE>" <<
i18n(
"Bookmarks") <<
"</TITLE>" << endl
163 <<
"<H1>" <<
i18n(
"Bookmarks") <<
"</H1>" << endl
166 <<
"</DL><P>" << endl;
171 QTextStream fstream(&str, QIODevice::WriteOnly);
174 if (bk.isSeparator()) {
175 fstream <<
"<HR>" << endl;
179 QString text = Qt::escape(bk.fullText());
182 fstream <<
"<DT><H3 "
183 << (!bk.toGroup().isOpen() ?
"FOLDED " :
"")
184 << bk.internalElement().attribute(
"netscapeinfo") <<
">"
185 << text <<
"</H3>" << endl
188 <<
"</DL><P>" << endl;
193 fstream <<
"<DT><A HREF=\"" << bk.url().url() <<
"\""
194 << bk.internalElement().attribute(
"netscapeinfo") <<
">"
195 << text <<
"</A>" << endl;