• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kbookmarkimporter_ns.cc

Go to the documentation of this file.
00001 //  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
00002 // vim: set ts=4 sts=4 sw=4 et:
00003 /* This file is part of the KDE libraries
00004    Copyright (C) 1996-1998 Martin R. Jones <mjones@kde.org>
00005    Copyright (C) 2000 David Faure <faure@kde.org>
00006    Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License version 2 as published by the Free Software Foundation.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020    Boston, MA 02110-1301, USA.
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> // Qt::escape
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         // skip header
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 ) // Gosh, this line is longer than g_lineLimit. Skipping.
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          // note - netscape seems to use local8bit for url...
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"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal