KIO
kbookmarkimporter_ns.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kbookmarkimporter_ns.h"
00024 #include "kbookmarkimporter.h"
00025 #include "kbookmarkexporter.h"
00026 #include "kbookmarkmanager.h"
00027 #include <kfiledialog.h>
00028 #include <kstringhandler.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kde_file.h>
00032 #include <kcharsets.h>
00033
00034 #include <qtextcodec.h>
00035 #include <qtextdocument.h>
00036 #include <QtGui/QApplication>
00037
00038
00039
00040 void KNSBookmarkImporterImpl::parse()
00041 {
00042 QFile f(m_fileName);
00043 QTextCodec * codec = m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale();
00044 Q_ASSERT(codec);
00045 if (!codec)
00046 return;
00047
00048 if(f.open(QIODevice::ReadOnly)) {
00049
00050 static const int g_lineLimit = 16*1024;
00051 QByteArray s(g_lineLimit,0);
00052
00053 while(f.readLine(s.data(), g_lineLimit) >= 1 && !s.contains("<DL>")) {
00054 ;
00055 }
00056
00057 while( int size = f.readLine(s.data(), g_lineLimit)>=1) {
00058 if ( size == g_lineLimit )
00059 {
00060 kWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
00061 continue;
00062 }
00063 QByteArray t = s.trimmed();
00064 if(t.left(12).toUpper() == "<DT><A HREF=" ||
00065 t.left(16).toUpper() == "<DT><H3><A HREF=") {
00066
00067 int firstQuotes = t.indexOf('"')+1;
00068 int secondQuotes = t.indexOf('"', firstQuotes);
00069 if (firstQuotes != -1 && secondQuotes != -1)
00070 {
00071 QByteArray link = t.mid(firstQuotes, secondQuotes-firstQuotes);
00072 int endTag = t.indexOf('>', secondQuotes+1);
00073
00074 int closeTag = t.indexOf('<', endTag + 1);
00075
00076 QByteArray name = t.mid(endTag + 1, closeTag - endTag - 1);
00077 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
00078 QByteArray additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
00079
00080 emit newBookmark( qname,
00081 codec->toUnicode(link),
00082 QByteArray() );
00083 }
00084 }
00085 else if(t.left(7).toUpper() == "<DT><H3") {
00086 int endTag = t.indexOf('>', 7);
00087 QByteArray name = t.mid(endTag+1);
00088 name = name.left(name.indexOf('<'));
00089 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
00090 QByteArray additionalInfo = t.mid( 8, endTag-8 );
00091 bool folded = (additionalInfo.left(6) == "FOLDED");
00092 if (folded) additionalInfo.remove(0,7);
00093
00094 emit newFolder( qname,
00095 !folded,
00096 QByteArray() );
00097 }
00098 else if(t.left(4).toUpper() == "<HR>")
00099 emit newSeparator();
00100 else if(t.left(8).toUpper() == "</DL><P>")
00101 emit endFolder();
00102 }
00103
00104 f.close();
00105 }
00106 }
00107
00108 QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
00109 {
00110 if (m_utf8)
00111 {
00112 if ( forSaving )
00113 return KFileDialog::getSaveFileName( QDir::homePath() + "/.mozilla",
00114 i18n("*.html|HTML Files (*.html)"),
00115 QApplication::activeWindow() );
00116 else
00117 return KFileDialog::getOpenFileName( QDir::homePath() + "/.mozilla",
00118 i18n("*.html|HTML Files (*.html)"),
00119 QApplication::activeWindow() );
00120 }
00121 else
00122 {
00123 return QDir::homePath() + "/.netscape/bookmarks.html";
00124 }
00125 }
00126
00128
00129 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
00130 m_utf8 = utf8;
00131 }
00132
00133 void KNSBookmarkExporterImpl::write(const KBookmarkGroup &parent) {
00134 if (QFile::exists(m_fileName)) {
00135 KDE_rename(
00136 QFile::encodeName(m_fileName),
00137 QFile::encodeName(m_fileName + ".beforekde"));
00138 }
00139
00140 QFile file(m_fileName);
00141
00142 if (!file.open(QIODevice::WriteOnly)) {
00143 kError(7043) << "Can't write to file " << m_fileName << endl;
00144 return;
00145 }
00146
00147 QTextStream fstream(&file);
00148 fstream.setCodec(m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale());
00149
00150 QString charset
00151 = m_utf8 ? "UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->name()).toUpper();
00152
00153 fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
00154 << i18n("<!-- This file was generated by Konqueror -->") << endl
00155 << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
00156 << charset << "\">" << endl
00157 << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
00158 << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
00159 << "<DL><p>" << endl
00160 << folderAsString(parent)
00161 << "</DL><P>" << endl;
00162 }
00163
00164 QString KNSBookmarkExporterImpl::folderAsString(const KBookmarkGroup &parent) const {
00165 QString str;
00166 QTextStream fstream(&str, QIODevice::WriteOnly);
00167
00168 for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
00169 if (bk.isSeparator()) {
00170 fstream << "<HR>" << endl;
00171 continue;
00172 }
00173
00174 QString text = Qt::escape(bk.fullText());
00175
00176 if (bk.isGroup() ) {
00177 fstream << "<DT><H3 "
00178 << (!bk.toGroup().isOpen() ? "FOLDED " : "")
00179 << bk.internalElement().attribute("netscapeinfo") << ">"
00180 << text << "</H3>" << endl
00181 << "<DL><P>" << endl
00182 << folderAsString(bk.toGroup())
00183 << "</DL><P>" << endl;
00184 continue;
00185
00186 } else {
00187
00188 fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
00189 << bk.internalElement().attribute("netscapeinfo") << ">"
00190 << text << "</A>" << endl;
00191 continue;
00192 }
00193 }
00194
00195 return str;
00196 }
00197
00199
00200 #include "kbookmarkimporter_ns.moc"