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

KIO

  • 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  if(t.left(12).toUpper() == "<DT><A HREF=" ||
65  t.left(16).toUpper() == "<DT><H3><A HREF=") {
66 
67  int firstQuotes = t.indexOf('"')+1;
68  int secondQuotes = t.indexOf('"', firstQuotes);
69  if (firstQuotes != -1 && secondQuotes != -1)
70  {
71  QByteArray link = t.mid(firstQuotes, secondQuotes-firstQuotes);
72  int endTag = t.indexOf('>', secondQuotes+1);
73 
74  int closeTag = t.indexOf('<', endTag + 1);
75 
76  QByteArray name = t.mid(endTag + 1, closeTag - endTag - 1);
77  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
78  QByteArray additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
79 
80  emit newBookmark( qname,
81  codec->toUnicode(link),
82  QByteArray() );
83  }
84  }
85  else if(t.left(7).toUpper() == "<DT><H3") {
86  int endTag = t.indexOf('>', 7);
87  QByteArray name = t.mid(endTag+1);
88  name = name.left(name.indexOf('<'));
89  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
90  QByteArray additionalInfo = t.mid( 8, endTag-8 );
91  bool folded = (additionalInfo.left(6) == "FOLDED");
92  if (folded) additionalInfo.remove(0,7);
93 
94  emit newFolder( qname,
95  !folded,
96  QByteArray() );
97  }
98  else if(t.left(4).toUpper() == "<HR>")
99  emit newSeparator();
100  else if(t.left(8).toUpper() == "</DL><P>")
101  emit endFolder();
102  }
103 
104  f.close();
105  }
106 }
107 
108 QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
109 {
110  if (m_utf8)
111  {
112  if ( forSaving )
113  return KFileDialog::getSaveFileName( QString(QDir::homePath() + "/.mozilla"),
114  i18n("*.html|HTML Files (*.html)"),
115  QApplication::activeWindow() );
116  else
117  return KFileDialog::getOpenFileName( QString(QDir::homePath() + "/.mozilla"),
118  i18n("*.html|HTML Files (*.html)"),
119  QApplication::activeWindow() );
120  }
121  else
122  {
123  return QDir::homePath() + "/.netscape/bookmarks.html";
124  }
125 }
126 
128 
129 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
130  m_utf8 = utf8;
131 }
132 
133 void KNSBookmarkExporterImpl::write(const KBookmarkGroup &parent)
134 {
135  if (!QFile::exists(m_fileName)) {
136  QString errorMsg = QString("Could not find %1. Netscape is probably not installed. "
137  "Aborting the export.").arg(m_fileName);
138  KMessageBox::error(0,errorMsg , "Netscape not found");
139  return;
140  }
141  if (QFile::exists(m_fileName)) {
142  (void)QFile::rename(m_fileName, m_fileName + ".beforekde");
143  }
144 
145  QFile file(m_fileName);
146 
147  if (!file.open(QIODevice::WriteOnly)) {
148  kError(7043) << "Can't write to file " << m_fileName << endl;
149  return;
150  }
151 
152  QTextStream fstream(&file);
153  fstream.setCodec(m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale());
154 
155  QString charset
156  = m_utf8 ? "UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->name()).toUpper();
157 
158  fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
159  << i18n("<!-- This file was generated by Konqueror -->") << endl
160  << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
161  << charset << "\">" << endl
162  << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
163  << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
164  << "<DL><p>" << endl
165  << folderAsString(parent)
166  << "</DL><P>" << endl;
167 }
168 
169 QString KNSBookmarkExporterImpl::folderAsString(const KBookmarkGroup &parent) const {
170  QString str;
171  QTextStream fstream(&str, QIODevice::WriteOnly);
172 
173  for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
174  if (bk.isSeparator()) {
175  fstream << "<HR>" << endl;
176  continue;
177  }
178 
179  QString text = Qt::escape(bk.fullText());
180 
181  if (bk.isGroup() ) {
182  fstream << "<DT><H3 "
183  << (!bk.toGroup().isOpen() ? "FOLDED " : "")
184  << bk.internalElement().attribute("netscapeinfo") << ">"
185  << text << "</H3>" << endl
186  << "<DL><P>" << endl
187  << folderAsString(bk.toGroup())
188  << "</DL><P>" << endl;
189  continue;
190 
191  } else {
192  // note - netscape seems to use local8bit for url...
193  fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
194  << bk.internalElement().attribute("netscapeinfo") << ">"
195  << text << "</A>" << endl;
196  continue;
197  }
198  }
199 
200  return str;
201 }
202 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Mon Jul 15 2013 05:11:46 by doxygen 1.8.3.1 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-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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