• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kio/bookmarks
 

kio/bookmarks

  • kio
  • bookmarks
kbookmarkimporter_ns.cc
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.h"
24 #include "kbookmarkexporter.h"
25 #include "kbookmarkmanager.h"
26 #include <kfiledialog.h>
27 #include <kstringhandler.h>
28 #include <klocale.h>
29 #include <kdebug.h>
30 #include <kcharsets.h>
31 #include <tqtextcodec.h>
32 #include <tqstylesheet.h>
33 
34 #include <sys/types.h>
35 #include <stddef.h>
36 #include <dirent.h>
37 #include <sys/stat.h>
38 #include <assert.h>
39 
40 void KNSBookmarkImporterImpl::parse()
41 {
42  TQFile f(m_fileName);
43  TQTextCodec * codec = m_utf8 ? TQTextCodec::codecForName("UTF-8") : TQTextCodec::codecForLocale();
44  Q_ASSERT(codec);
45  if (!codec)
46  return;
47 
48  if(f.open(IO_ReadOnly)) {
49 
50  static const int g_lineLimit = 16*1024;
51  TQCString s(g_lineLimit);
52  // skip header
53  while(f.readLine(s.data(), g_lineLimit) >= 0 && !s.contains("<DL>"));
54 
55  while(f.readLine(s.data(), g_lineLimit)>=0) {
56  if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
57  {
58  kdWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
59  continue;
60  }
61  TQCString t = s.stripWhiteSpace();
62  if(t.left(12).upper() == "<DT><A HREF=" ||
63  t.left(16).upper() == "<DT><H3><A HREF=") {
64  int firstQuotes = t.find('"')+1;
65  int secondQuotes = t.find('"', firstQuotes);
66  if (firstQuotes != -1 && secondQuotes != -1)
67  {
68  TQCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
69  int endTag = t.find('>', secondQuotes+1);
70  TQCString name = t.mid(endTag+1);
71  name = name.left(name.findRev('<'));
72  if ( name.right(4) == "</A>" )
73  name = name.left( name.length() - 4 );
74  TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
75  TQCString additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
76 
77  emit newBookmark( qname,
78  link, codec->toUnicode(additionalInfo) );
79  }
80  }
81  else if(t.left(7).upper() == "<DT><H3") {
82  int endTag = t.find('>', 7);
83  TQCString name = t.mid(endTag+1);
84  name = name.left(name.findRev('<'));
85  TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
86  TQCString additionalInfo = t.mid( 8, endTag-8 );
87  bool folded = (additionalInfo.left(6) == "FOLDED");
88  if (folded) additionalInfo.remove(0,7);
89 
90  emit newFolder( qname,
91  !folded,
92  codec->toUnicode(additionalInfo) );
93  }
94  else if(t.left(4).upper() == "<HR>")
95  emit newSeparator();
96  else if(t.left(8).upper() == "</DL><P>")
97  emit endFolder();
98  }
99 
100  f.close();
101  }
102 }
103 
104 TQString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
105 {
106  if (m_utf8)
107  {
108  if ( forSaving )
109  return KFileDialog::getSaveFileName( TQDir::homeDirPath() + "/.mozilla",
110  i18n("*.html|HTML Files (*.html)") );
111  else
112  return KFileDialog::getOpenFileName( TQDir::homeDirPath() + "/.mozilla",
113  i18n("*.html|HTML Files (*.html)") );
114  }
115  else
116  {
117  return TQDir::homeDirPath() + "/.netscape/bookmarks.html";
118  }
119 }
120 
122 
123 
124 void KNSBookmarkImporter::parseNSBookmarks( bool utf8 )
125 {
126  KNSBookmarkImporterImpl importer;
127  importer.setFilename(m_fileName);
128  importer.setUtf8(utf8);
129  importer.setupSignalForwards(&importer, this);
130  importer.parse();
131 }
132 
133 TQString KNSBookmarkImporter::netscapeBookmarksFile( bool forSaving )
134 {
135  static KNSBookmarkImporterImpl *p = 0;
136  if (!p)
137  {
138  p = new KNSBookmarkImporterImpl;
139  p->setUtf8(false);
140  }
141  return p->findDefaultLocation(forSaving);
142 }
143 
144 TQString KNSBookmarkImporter::mozillaBookmarksFile( bool forSaving )
145 {
146  static KNSBookmarkImporterImpl *p = 0;
147  if (!p)
148  {
149  p = new KNSBookmarkImporterImpl;
150  p->setUtf8(true);
151  }
152  return p->findDefaultLocation(forSaving);
153 }
154 
155 
157 // compat only
159 
160 void KNSBookmarkExporter::write(bool utf8) {
161  KNSBookmarkExporterImpl exporter(m_pManager, m_fileName);
162  exporter.setUtf8(utf8);
163  exporter.write(m_pManager->root());
164 }
165 
166 void KNSBookmarkExporter::writeFolder(TQTextStream &/*stream*/, KBookmarkGroup /*gp*/) {
167  // TODO - requires a d pointer workaround hack?
168 }
169 
171 
172 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
173  m_utf8 = utf8;
174 }
175 
176 void KNSBookmarkExporterImpl::write(KBookmarkGroup parent) {
177  if (TQFile::exists(m_fileName)) {
178  ::rename(
179  TQFile::encodeName(m_fileName),
180  TQFile::encodeName(m_fileName + ".beforekde"));
181  }
182 
183  TQFile file(m_fileName);
184 
185  if (!file.open(IO_WriteOnly)) {
186  kdError(7043) << "Can't write to file " << m_fileName << endl;
187  return;
188  }
189 
190  TQTextStream fstream(&file);
191  fstream.setEncoding(m_utf8 ? TQTextStream::UnicodeUTF8 : TQTextStream::Locale);
192 
193  TQString charset
194  = m_utf8 ? "UTF-8" : TQString::fromLatin1(TQTextCodec::codecForLocale()->name()).upper();
195 
196  fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
197  << i18n("<!-- This file was generated by Konqueror -->") << endl
198  << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
199  << charset << "\">" << endl
200  << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
201  << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
202  << "<DL><p>" << endl
203  << folderAsString(parent)
204  << "</DL><P>" << endl;
205 }
206 
207 TQString KNSBookmarkExporterImpl::folderAsString(KBookmarkGroup parent) const {
208  TQString str;
209  TQTextStream fstream(&str, IO_WriteOnly);
210 
211  for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
212  if (bk.isSeparator()) {
213  fstream << "<HR>" << endl;
214  continue;
215  }
216 
217  TQString text = TQStyleSheet::escape(bk.fullText());
218 
219  if (bk.isGroup() ) {
220  fstream << "<DT><H3 "
221  << (!bk.toGroup().isOpen() ? "FOLDED " : "")
222  << bk.internalElement().attribute("netscapeinfo") << ">"
223  << text << "</H3>" << endl
224  << "<DL><P>" << endl
225  << folderAsString(bk.toGroup())
226  << "</DL><P>" << endl;
227  continue;
228 
229  } else {
230  // note - netscape seems to use local8bit for url...
231  fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
232  << bk.internalElement().attribute("netscapeinfo") << ">"
233  << text << "</A>" << endl;
234  continue;
235  }
236  }
237 
238  return str;
239 }
240 
242 
243 #include "kbookmarkimporter_ns.moc"

kio/bookmarks

Skip menu "kio/bookmarks"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kio/bookmarks

Skip menu "kio/bookmarks"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kio/bookmarks by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |