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

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • bookmarks
kbookmarkimporter_ns.cc
Go to the documentation of this file.
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 // vim: set ts=4 sts=4 sw=4 et:
3 /* This file is part of the KDE libraries
4  Copyright (C) 1996-1998 Martin R. Jones <mjones@kde.org>
5  Copyright (C) 2000 David Faure <faure@kde.org>
6  Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License version 2 as published by the Free Software Foundation.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "kbookmarkimporter_ns.h"
24 #include "kbookmarkimporter.h"
25 #include "kbookmarkexporter.h"
26 #include "kbookmarkmanager.h"
27 #include <kfiledialog.h>
28 #include <kstringhandler.h>
29 #include <klocale.h>
30 #include <kdebug.h>
31 #include <kcharsets.h>
32 #include <kmessagebox.h>
33 
34 #include <qtextcodec.h>
35 #include <qtextdocument.h> // Qt::escape
36 #include <QtGui/QApplication>
37 
38 
39 
40 void KNSBookmarkImporterImpl::parse()
41 {
42  QFile f(m_fileName);
43  QTextCodec * codec = m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale();
44  Q_ASSERT(codec);
45  if (!codec)
46  return;
47 
48  if(f.open(QIODevice::ReadOnly)) {
49 
50  static const int g_lineLimit = 16*1024;
51  QByteArray s(g_lineLimit,0);
52  // skip header
53  while(f.readLine(s.data(), g_lineLimit) >= 1 && !s.contains("<DL>")) {
54  ;
55  }
56 
57  while( int size = f.readLine(s.data(), g_lineLimit)>=1) {
58  if ( size == g_lineLimit ) // Gosh, this line is longer than g_lineLimit. Skipping.
59  {
60  kWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
61  continue;
62  }
63  QByteArray t = s.trimmed();
64 
65  if (t.left(4).toUpper() == "<HR>") {
66  emit newSeparator();
67  t = t.mid(4).trimmed();
68  if (t.isEmpty()) {
69  continue;
70  }
71  }
72 
73  if(t.left(12).toUpper() == "<DT><A HREF=" ||
74  t.left(16).toUpper() == "<DT><H3><A HREF=") {
75 
76  int firstQuotes = t.indexOf('"')+1;
77  int secondQuotes = t.indexOf('"', firstQuotes);
78  if (firstQuotes != -1 && secondQuotes != -1)
79  {
80  QByteArray link = t.mid(firstQuotes, secondQuotes-firstQuotes);
81  int endTag = t.indexOf('>', secondQuotes+1);
82 
83  int closeTag = t.indexOf('<', endTag + 1);
84 
85  QByteArray name = t.mid(endTag + 1, closeTag - endTag - 1);
86  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
87  QByteArray additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
88 
89  emit newBookmark( qname,
90  codec->toUnicode(link),
91  QByteArray() );
92  }
93  }
94  else if(t.left(7).toUpper() == "<DT><H3") {
95  int endTag = t.indexOf('>', 7);
96  QByteArray name = t.mid(endTag+1);
97  name = name.left(name.indexOf('<'));
98  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
99  QByteArray additionalInfo = t.mid( 8, endTag-8 );
100  bool folded = (additionalInfo.left(6) == "FOLDED");
101  if (folded) additionalInfo.remove(0,7);
102 
103  emit newFolder( qname,
104  !folded,
105  QByteArray() );
106  }
107  else if(t.left(8).toUpper() == "</DL><P>") {
108  emit endFolder();
109  }
110  }
111 
112  f.close();
113  }
114 }
115 
116 QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
117 {
118  if (m_utf8)
119  {
120  if ( forSaving )
121  return KFileDialog::getSaveFileName( QString(QDir::homePath() + "/.mozilla"),
122  i18n("*.html|HTML Files (*.html)"),
123  QApplication::activeWindow() );
124  else
125  return KFileDialog::getOpenFileName( QString(QDir::homePath() + "/.mozilla"),
126  i18n("*.html|HTML Files (*.html)"),
127  QApplication::activeWindow() );
128  }
129  else
130  {
131  return QDir::homePath() + "/.netscape/bookmarks.html";
132  }
133 }
134 
136 
137 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
138  m_utf8 = utf8;
139 }
140 
141 void KNSBookmarkExporterImpl::write(const KBookmarkGroup &parent)
142 {
143  if (!QFile::exists(m_fileName)) {
144  QString errorMsg = QString("Could not find %1. Netscape is probably not installed. "
145  "Aborting the export.").arg(m_fileName);
146  KMessageBox::error(0,errorMsg , "Netscape not found");
147  return;
148  }
149  if (QFile::exists(m_fileName)) {
150  (void)QFile::rename(m_fileName, m_fileName + ".beforekde");
151  }
152 
153  QFile file(m_fileName);
154 
155  if (!file.open(QIODevice::WriteOnly)) {
156  kError(7043) << "Can't write to file " << m_fileName << endl;
157  return;
158  }
159 
160  QTextStream fstream(&file);
161  fstream.setCodec(m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale());
162 
163  QString charset
164  = m_utf8 ? "UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->name()).toUpper();
165 
166  fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
167  << i18n("<!-- This file was generated by Konqueror -->") << endl
168  << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
169  << charset << "\">" << endl
170  << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
171  << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
172  << "<DL><p>" << endl
173  << folderAsString(parent)
174  << "</DL><P>" << endl;
175 }
176 
177 QString KNSBookmarkExporterImpl::folderAsString(const KBookmarkGroup &parent) const {
178  QString str;
179  QTextStream fstream(&str, QIODevice::WriteOnly);
180 
181  for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
182  if (bk.isSeparator()) {
183  fstream << "<HR>" << endl;
184  continue;
185  }
186 
187  QString text = Qt::escape(bk.fullText());
188 
189  if (bk.isGroup() ) {
190  fstream << "<DT><H3 "
191  << (!bk.toGroup().isOpen() ? "FOLDED " : "")
192  << bk.internalElement().attribute("netscapeinfo") << ">"
193  << text << "</H3>" << endl
194  << "<DL><P>" << endl
195  << folderAsString(bk.toGroup())
196  << "</DL><P>" << endl;
197  continue;
198 
199  } else {
200  // note - netscape seems to use local8bit for url...
201  fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
202  << bk.internalElement().attribute("netscapeinfo") << ">"
203  << text << "</A>" << endl;
204  continue;
205  }
206  }
207 
208  return str;
209 }
210 
i18n
QString i18n(const char *text)
kcharsets.h
kbookmarkmanager.h
kdebug.h
kfiledialog.h
KBookmarkImporterBase::endFolder
void endFolder()
Tell the outside world that we're going down one menu.
kbookmarkexporter.h
name
const char * name(StandardAction id)
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
f
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
Definition: des.cpp:378
KNSBookmarkImporterImpl::parse
virtual void parse()
Definition: kbookmarkimporter_ns.cc:40
QString
klocale.h
KBookmark
Definition: kbookmark.h:34
KNSBookmarkExporterImpl::setUtf8
void setUtf8(bool)
Definition: kbookmarkimporter_ns.cc:137
kbookmarkimporter.h
KBookmarkExporterBase::m_fileName
QString m_fileName
Definition: kbookmarkexporter.h:38
KNSBookmarkExporterImpl::write
virtual void write(const KBookmarkGroup &parent)
Definition: kbookmarkimporter_ns.cc:141
KFileDialog::getOpenFileName
static QString getOpenFileName(const KUrl &startDir=KUrl(), const QString &filter=QString(), QWidget *parent=0, const QString &caption=QString())
Creates a modal file dialog and return the selected filename or an empty string if none was chosen...
Definition: kfiledialog.cpp:464
KBookmarkImporterBase::newBookmark
void newBookmark(const QString &text, const QString &url, const QString &additionalInfo)
Notify about a new bookmark Use "html" for the icon.
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:347
KIO::link
CopyJob * link(const KUrl &src, const KUrl &destDir, JobFlags flags=DefaultFlags)
Create a link.
Definition: copyjob.cpp:2212
KBookmarkImporterBase::m_fileName
QString m_fileName
Definition: kbookmarkimporter.h:75
KFileDialog::getSaveFileName
static QString getSaveFileName(const KUrl &startDir=KUrl(), const QString &filter=QString(), QWidget *parent=0, const QString &caption=QString())
Creates a modal file dialog and returns the selected filename or an empty string if none was chosen...
Definition: kfiledialog.cpp:701
KNSBookmarkExporterImpl::folderAsString
QString folderAsString(const KBookmarkGroup &parent) const
Definition: kbookmarkimporter_ns.cc:177
kstringhandler.h
KCharsets::resolveEntities
static QString resolveEntities(const QString &text)
kbookmarkimporter_ns.h
KBookmarkImporterBase::newSeparator
void newSeparator()
Notify about a new separator.
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
kmessagebox.h
KBookmark::isNull
bool isNull() const
Definition: kbookmark.cc:295
KBookmarkGroup::next
KBookmark next(const KBookmark &current) const
Return the next sibling of a child bookmark of this group.
Definition: kbookmark.cc:123
KBookmarkGroup::first
KBookmark first() const
Return the first child bookmark of this group.
Definition: kbookmark.cc:113
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
KNSBookmarkImporterImpl::findDefaultLocation
virtual QString findDefaultLocation(bool forSaving=false) const
Definition: kbookmarkimporter_ns.cc:116
KBookmarkImporterBase::newFolder
void newFolder(const QString &text, bool open, const QString &additionalInfo)
Notify about a new folder Use "bookmark_folder" for the icon.
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal