• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • 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.h"
00024 #include "kbookmarkexporter.h"
00025 #include "kbookmarkmanager.h"
00026 #include <kfiledialog.h>
00027 #include <kstringhandler.h>
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 #include <kcharsets.h>
00031 #include <qtextcodec.h>
00032 #include <qstylesheet.h>
00033 
00034 #include <sys/types.h>
00035 #include <stddef.h>
00036 #include <dirent.h>
00037 #include <sys/stat.h>
00038 #include <assert.h>
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(IO_ReadOnly)) {
00049 
00050         static const int g_lineLimit = 16*1024;
00051         QCString s(g_lineLimit);
00052         // skip header
00053         while(f.readLine(s.data(), g_lineLimit) >= 0 && !s.contains("<DL>"));
00054 
00055         while(f.readLine(s.data(), g_lineLimit)>=0) {
00056             if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
00057             {
00058                kdWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
00059                continue;
00060             }
00061             QCString t = s.stripWhiteSpace();
00062             if(t.left(12).upper() == "<DT><A HREF=" ||
00063                t.left(16).upper() == "<DT><H3><A HREF=") {
00064               int firstQuotes = t.find('"')+1;
00065               int secondQuotes = t.find('"', firstQuotes);
00066               if (firstQuotes != -1 && secondQuotes != -1)
00067               {
00068                 QCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
00069                 int endTag = t.find('>', secondQuotes+1);
00070                 QCString name = t.mid(endTag+1);
00071                 name = name.left(name.findRev('<'));
00072                 if ( name.right(4) == "</A>" )
00073                     name = name.left( name.length() - 4 );
00074                 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
00075                 QCString additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
00076 
00077                 emit newBookmark( qname,
00078                                   link, codec->toUnicode(additionalInfo) );
00079               }
00080             }
00081             else if(t.left(7).upper() == "<DT><H3") {
00082                 int endTag = t.find('>', 7);
00083                 QCString name = t.mid(endTag+1);
00084                 name = name.left(name.findRev('<'));
00085                 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
00086                 QCString additionalInfo = t.mid( 8, endTag-8 );
00087                 bool folded = (additionalInfo.left(6) == "FOLDED");
00088                 if (folded) additionalInfo.remove(0,7);
00089 
00090                 emit newFolder( qname,
00091                                 !folded,
00092                                 codec->toUnicode(additionalInfo) );
00093             }
00094             else if(t.left(4).upper() == "<HR>")
00095                 emit newSeparator();
00096             else if(t.left(8).upper() == "</DL><P>")
00097                 emit endFolder();
00098         }
00099 
00100         f.close();
00101     }
00102 }
00103 
00104 QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
00105 {
00106     if (m_utf8) 
00107     {
00108        if ( forSaving )
00109            return KFileDialog::getSaveFileName( QDir::homeDirPath() + "/.mozilla",
00110                                                 i18n("*.html|HTML Files (*.html)") );
00111        else
00112            return KFileDialog::getOpenFileName( QDir::homeDirPath() + "/.mozilla",
00113                                                 i18n("*.html|HTML Files (*.html)") );
00114     } 
00115     else 
00116     {
00117        return QDir::homeDirPath() + "/.netscape/bookmarks.html";
00118     }
00119 }
00120 
00122 
00123 
00124 void KNSBookmarkImporter::parseNSBookmarks( bool utf8 )
00125 {
00126     KNSBookmarkImporterImpl importer;
00127     importer.setFilename(m_fileName);
00128     importer.setUtf8(utf8);
00129     importer.setupSignalForwards(&importer, this);
00130     importer.parse();
00131 }
00132 
00133 QString KNSBookmarkImporter::netscapeBookmarksFile( bool forSaving )
00134 {
00135     static KNSBookmarkImporterImpl *p = 0;
00136     if (!p)
00137     {
00138         p = new KNSBookmarkImporterImpl;
00139         p->setUtf8(false);
00140     }
00141     return p->findDefaultLocation(forSaving);
00142 }
00143 
00144 QString KNSBookmarkImporter::mozillaBookmarksFile( bool forSaving )
00145 {
00146     static KNSBookmarkImporterImpl *p = 0;
00147     if (!p)
00148     {
00149         p = new KNSBookmarkImporterImpl;
00150         p->setUtf8(true);
00151     }
00152     return p->findDefaultLocation(forSaving);
00153 }
00154 
00155 
00157 //                   compat only
00159 
00160 void KNSBookmarkExporter::write(bool utf8) {
00161    KNSBookmarkExporterImpl exporter(m_pManager, m_fileName);
00162    exporter.setUtf8(utf8);
00163    exporter.write(m_pManager->root());
00164 }
00165 
00166 void KNSBookmarkExporter::writeFolder(QTextStream &/*stream*/, KBookmarkGroup /*gp*/) {
00167    // TODO - requires a d pointer workaround hack?
00168 }
00169 
00171 
00172 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
00173    m_utf8 = utf8;
00174 }
00175 
00176 void KNSBookmarkExporterImpl::write(KBookmarkGroup parent) {
00177    if (QFile::exists(m_fileName)) {
00178       ::rename(
00179          QFile::encodeName(m_fileName), 
00180          QFile::encodeName(m_fileName + ".beforekde"));
00181    }
00182 
00183    QFile file(m_fileName);
00184 
00185    if (!file.open(IO_WriteOnly)) {
00186       kdError(7043) << "Can't write to file " << m_fileName << endl;
00187       return;
00188    }
00189 
00190    QTextStream fstream(&file);
00191    fstream.setEncoding(m_utf8 ? QTextStream::UnicodeUTF8 : QTextStream::Locale);
00192 
00193    QString charset 
00194       = m_utf8 ? "UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->name()).upper();
00195 
00196    fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
00197            << i18n("<!-- This file was generated by Konqueror -->") << endl
00198            << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=" 
00199               << charset << "\">" << endl
00200            << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
00201            << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
00202            << "<DL><p>" << endl
00203            << folderAsString(parent)
00204            << "</DL><P>" << endl;
00205 }
00206 
00207 QString KNSBookmarkExporterImpl::folderAsString(KBookmarkGroup parent) const {
00208    QString str;
00209    QTextStream fstream(&str, IO_WriteOnly);
00210 
00211    for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
00212       if (bk.isSeparator()) {
00213          fstream << "<HR>" << endl;
00214          continue;
00215       }
00216 
00217       QString text = QStyleSheet::escape(bk.fullText());
00218 
00219       if (bk.isGroup() ) {
00220          fstream << "<DT><H3 " 
00221                     << (!bk.toGroup().isOpen() ? "FOLDED " : "")
00222                     << bk.internalElement().attribute("netscapeinfo") << ">" 
00223                  << text << "</H3>" << endl
00224                  << "<DL><P>" << endl
00225                  << folderAsString(bk.toGroup())
00226                  << "</DL><P>" << endl;
00227          continue;
00228 
00229       } else {
00230          // note - netscape seems to use local8bit for url...
00231          fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
00232                     << bk.internalElement().attribute("netscapeinfo") << ">" 
00233                  << text << "</A>" << endl;
00234          continue;
00235       }
00236    }
00237 
00238    return str;
00239 }
00240 
00242 
00243 #include "kbookmarkimporter_ns.moc"

kio

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
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