• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 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 <kde_file.h>
32 #include <kcharsets.h>
33 #include <kmessagebox.h>
34 
35 #include <qtextcodec.h>
36 #include <qtextdocument.h> // Qt::escape
37 #include <QtGui/QApplication>
38 
39 
40 
41 void KNSBookmarkImporterImpl::parse()
42 {
43  QFile f(m_fileName);
44  QTextCodec * codec = m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale();
45  Q_ASSERT(codec);
46  if (!codec)
47  return;
48 
49  if(f.open(QIODevice::ReadOnly)) {
50 
51  static const int g_lineLimit = 16*1024;
52  QByteArray s(g_lineLimit,0);
53  // skip header
54  while(f.readLine(s.data(), g_lineLimit) >= 1 && !s.contains("<DL>")) {
55  ;
56  }
57 
58  while( int size = f.readLine(s.data(), g_lineLimit)>=1) {
59  if ( size == g_lineLimit ) // Gosh, this line is longer than g_lineLimit. Skipping.
60  {
61  kWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
62  continue;
63  }
64  QByteArray t = s.trimmed();
65  if(t.left(12).toUpper() == "<DT><A HREF=" ||
66  t.left(16).toUpper() == "<DT><H3><A HREF=") {
67 
68  int firstQuotes = t.indexOf('"')+1;
69  int secondQuotes = t.indexOf('"', firstQuotes);
70  if (firstQuotes != -1 && secondQuotes != -1)
71  {
72  QByteArray link = t.mid(firstQuotes, secondQuotes-firstQuotes);
73  int endTag = t.indexOf('>', secondQuotes+1);
74 
75  int closeTag = t.indexOf('<', endTag + 1);
76 
77  QByteArray name = t.mid(endTag + 1, closeTag - endTag - 1);
78  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
79  QByteArray additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
80 
81  emit newBookmark( qname,
82  codec->toUnicode(link),
83  QByteArray() );
84  }
85  }
86  else if(t.left(7).toUpper() == "<DT><H3") {
87  int endTag = t.indexOf('>', 7);
88  QByteArray name = t.mid(endTag+1);
89  name = name.left(name.indexOf('<'));
90  QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
91  QByteArray additionalInfo = t.mid( 8, endTag-8 );
92  bool folded = (additionalInfo.left(6) == "FOLDED");
93  if (folded) additionalInfo.remove(0,7);
94 
95  emit newFolder( qname,
96  !folded,
97  QByteArray() );
98  }
99  else if(t.left(4).toUpper() == "<HR>")
100  emit newSeparator();
101  else if(t.left(8).toUpper() == "</DL><P>")
102  emit endFolder();
103  }
104 
105  f.close();
106  }
107 }
108 
109 QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
110 {
111  if (m_utf8)
112  {
113  if ( forSaving )
114  return KFileDialog::getSaveFileName( QString(QDir::homePath() + "/.mozilla"),
115  i18n("*.html|HTML Files (*.html)"),
116  QApplication::activeWindow() );
117  else
118  return KFileDialog::getOpenFileName( QString(QDir::homePath() + "/.mozilla"),
119  i18n("*.html|HTML Files (*.html)"),
120  QApplication::activeWindow() );
121  }
122  else
123  {
124  return QDir::homePath() + "/.netscape/bookmarks.html";
125  }
126 }
127 
129 
130 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
131  m_utf8 = utf8;
132 }
133 
134 void KNSBookmarkExporterImpl::write(const KBookmarkGroup &parent)
135 {
136  if (!QFile::exists(m_fileName)) {
137  QString errorMsg = QString("Could not find %1. Netscape is probably not installed. "
138  "Aborting the export.").arg(m_fileName);
139  KMessageBox::error(0,errorMsg , "Netscape not found");
140  return;
141  }
142  if (QFile::exists(m_fileName)) {
143  KDE_rename(
144  QFile::encodeName(m_fileName),
145  QFile::encodeName(m_fileName + ".beforekde"));
146  }
147 
148  QFile file(m_fileName);
149 
150  if (!file.open(QIODevice::WriteOnly)) {
151  kError(7043) << "Can't write to file " << m_fileName << endl;
152  return;
153  }
154 
155  QTextStream fstream(&file);
156  fstream.setCodec(m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale());
157 
158  QString charset
159  = m_utf8 ? "UTF-8" : QString::fromLatin1(QTextCodec::codecForLocale()->name()).toUpper();
160 
161  fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
162  << i18n("<!-- This file was generated by Konqueror -->") << endl
163  << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
164  << charset << "\">" << endl
165  << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
166  << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
167  << "<DL><p>" << endl
168  << folderAsString(parent)
169  << "</DL><P>" << endl;
170 }
171 
172 QString KNSBookmarkExporterImpl::folderAsString(const KBookmarkGroup &parent) const {
173  QString str;
174  QTextStream fstream(&str, QIODevice::WriteOnly);
175 
176  for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
177  if (bk.isSeparator()) {
178  fstream << "<HR>" << endl;
179  continue;
180  }
181 
182  QString text = Qt::escape(bk.fullText());
183 
184  if (bk.isGroup() ) {
185  fstream << "<DT><H3 "
186  << (!bk.toGroup().isOpen() ? "FOLDED " : "")
187  << bk.internalElement().attribute("netscapeinfo") << ">"
188  << text << "</H3>" << endl
189  << "<DL><P>" << endl
190  << folderAsString(bk.toGroup())
191  << "</DL><P>" << endl;
192  continue;
193 
194  } else {
195  // note - netscape seems to use local8bit for url...
196  fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
197  << bk.internalElement().attribute("netscapeinfo") << ">"
198  << text << "</A>" << endl;
199  continue;
200  }
201  }
202 
203  return str;
204 }
205 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Dec 7 2012 16:08:36 by doxygen 1.8.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.8.5 API Reference

Skip menu "kdelibs-4.8.5 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