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

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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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