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

tdeio/bookmarks

  • tdeio
  • bookmarks
kbookmarkimporter_ns.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1996-1998 Martin R. Jones <mjones@kde.org>
3  Copyright (C) 2000 David Faure <faure@kde.org>
4  Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kbookmarkimporter.h"
22 #include "kbookmarkexporter.h"
23 #include "kbookmarkmanager.h"
24 #include <tdefiledialog.h>
25 #include <kstringhandler.h>
26 #include <tdelocale.h>
27 #include <kdebug.h>
28 #include <kcharsets.h>
29 #include <tqtextcodec.h>
30 #include <tqstylesheet.h>
31 
32 #include <sys/types.h>
33 #include <stddef.h>
34 #include <dirent.h>
35 #include <sys/stat.h>
36 #include <assert.h>
37 
38 void KNSBookmarkImporterImpl::parse()
39 {
40  TQFile f(m_fileName);
41  TQTextCodec * codec = m_utf8 ? TQTextCodec::codecForName("UTF-8") : TQTextCodec::codecForLocale();
42  Q_ASSERT(codec);
43  if (!codec)
44  return;
45 
46  if(f.open(IO_ReadOnly)) {
47 
48  static const int g_lineLimit = 16*1024;
49  TQCString s(g_lineLimit);
50  // skip header
51  while(f.readLine(s.data(), g_lineLimit) >= 0 && !s.contains("<DL>"));
52 
53  while(f.readLine(s.data(), g_lineLimit)>=0) {
54  if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
55  {
56  kdWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
57  continue;
58  }
59  TQCString t = s.stripWhiteSpace();
60  if(t.left(12).upper() == "<DT><A HREF=" ||
61  t.left(16).upper() == "<DT><H3><A HREF=") {
62  int firstQuotes = t.find('"')+1;
63  int secondQuotes = t.find('"', firstQuotes);
64  if (firstQuotes != -1 && secondQuotes != -1)
65  {
66  TQCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
67  int endTag = t.find('>', secondQuotes+1);
68  TQCString name = t.mid(endTag+1);
69  name = name.left(name.findRev('<'));
70  if ( name.right(4) == "</A>" )
71  name = name.left( name.length() - 4 );
72  TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
73  TQCString additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
74 
75  emit newBookmark( qname,
76  link, codec->toUnicode(additionalInfo) );
77  }
78  }
79  else if(t.left(7).upper() == "<DT><H3") {
80  int endTag = t.find('>', 7);
81  TQCString name = t.mid(endTag+1);
82  name = name.left(name.findRev('<'));
83  TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
84  TQCString additionalInfo = t.mid( 8, endTag-8 );
85  bool folded = (additionalInfo.left(6) == "FOLDED");
86  if (folded) additionalInfo.remove(0,7);
87 
88  emit newFolder( qname,
89  !folded,
90  codec->toUnicode(additionalInfo) );
91  }
92  else if(t.left(4).upper() == "<HR>")
93  emit newSeparator();
94  else if(t.left(8).upper() == "</DL><P>")
95  emit endFolder();
96  }
97 
98  f.close();
99  }
100 }
101 
102 TQString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const
103 {
104  if (m_utf8)
105  {
106  if ( forSaving )
107  return KFileDialog::getSaveFileName( TQDir::homeDirPath() + "/.mozilla",
108  i18n("*.html|HTML Files (*.html)") );
109  else
110  return KFileDialog::getOpenFileName( TQDir::homeDirPath() + "/.mozilla",
111  i18n("*.html|HTML Files (*.html)") );
112  }
113  else
114  {
115  return TQDir::homeDirPath() + "/.netscape/bookmarks.html";
116  }
117 }
118 
120 
121 
122 void KNSBookmarkImporter::parseNSBookmarks( bool utf8 )
123 {
124  KNSBookmarkImporterImpl importer;
125  importer.setFilename(m_fileName);
126  importer.setUtf8(utf8);
127  importer.setupSignalForwards(&importer, this);
128  importer.parse();
129 }
130 
131 TQString KNSBookmarkImporter::netscapeBookmarksFile( bool forSaving )
132 {
133  static KNSBookmarkImporterImpl *p = 0;
134  if (!p)
135  {
136  p = new KNSBookmarkImporterImpl;
137  p->setUtf8(false);
138  }
139  return p->findDefaultLocation(forSaving);
140 }
141 
142 TQString KNSBookmarkImporter::mozillaBookmarksFile( bool forSaving )
143 {
144  static KNSBookmarkImporterImpl *p = 0;
145  if (!p)
146  {
147  p = new KNSBookmarkImporterImpl;
148  p->setUtf8(true);
149  }
150  return p->findDefaultLocation(forSaving);
151 }
152 
153 
155 // compat only
157 
158 void KNSBookmarkExporter::write(bool utf8) {
159  KNSBookmarkExporterImpl exporter(m_pManager, m_fileName);
160  exporter.setUtf8(utf8);
161  exporter.write(m_pManager->root());
162 }
163 
164 void KNSBookmarkExporter::writeFolder(TQTextStream &/*stream*/, KBookmarkGroup /*gp*/) {
165  // TODO - requires a d pointer workaround hack?
166 }
167 
169 
170 void KNSBookmarkExporterImpl::setUtf8(bool utf8) {
171  m_utf8 = utf8;
172 }
173 
174 void KNSBookmarkExporterImpl::write(KBookmarkGroup parent) {
175  if (TQFile::exists(m_fileName)) {
176  ::rename(
177  TQFile::encodeName(m_fileName),
178  TQFile::encodeName(m_fileName + ".beforekde"));
179  }
180 
181  TQFile file(m_fileName);
182 
183  if (!file.open(IO_WriteOnly)) {
184  kdError(7043) << "Can't write to file " << m_fileName << endl;
185  return;
186  }
187 
188  TQTextStream fstream(&file);
189  fstream.setEncoding(m_utf8 ? TQTextStream::UnicodeUTF8 : TQTextStream::Locale);
190 
191  TQString charset
192  = m_utf8 ? "UTF-8" : TQString::fromLatin1(TQTextCodec::codecForLocale()->name()).upper();
193 
194  fstream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
195  << i18n("<!-- This file was generated by Konqueror -->") << endl
196  << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
197  << charset << "\">" << endl
198  << "<TITLE>" << i18n("Bookmarks") << "</TITLE>" << endl
199  << "<H1>" << i18n("Bookmarks") << "</H1>" << endl
200  << "<DL><p>" << endl
201  << folderAsString(parent)
202  << "</DL><P>" << endl;
203 }
204 
205 TQString KNSBookmarkExporterImpl::folderAsString(KBookmarkGroup parent) const {
206  TQString str;
207  TQTextStream fstream(&str, IO_WriteOnly);
208 
209  for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) {
210  if (bk.isSeparator()) {
211  fstream << "<HR>" << endl;
212  continue;
213  }
214 
215  TQString text = TQStyleSheet::escape(bk.fullText());
216 
217  if (bk.isGroup() ) {
218  fstream << "<DT><H3 "
219  << (!bk.toGroup().isOpen() ? "FOLDED " : "")
220  << bk.internalElement().attribute("netscapeinfo") << ">"
221  << text << "</H3>" << endl
222  << "<DL><P>" << endl
223  << folderAsString(bk.toGroup())
224  << "</DL><P>" << endl;
225  continue;
226 
227  } else {
228  // note - netscape seems to use local8bit for url...
229  fstream << "<DT><A HREF=\"" << bk.url().url() << "\""
230  << bk.internalElement().attribute("netscapeinfo") << ">"
231  << text << "</A>" << endl;
232  continue;
233  }
234  }
235 
236  return str;
237 }
238 
240 
241 #include "kbookmarkimporter_ns.moc"
KNSBookmarkImporterImpl
A class for importing NS bookmarks utf8 defaults to off.
Definition: kbookmarkimporter_ns.h:65
KBookmarkImporterBase::endFolder
void endFolder()
Tell the outside world that we're going down one menu.
KBookmarkImporterBase::newFolder
void newFolder(const TQString &text, bool open, const TQString &additionalInfo)
Notify about a new folder Use "bookmark_folder" for the icon.
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:197
KBookmarkImporterBase::newBookmark
void newBookmark(const TQString &text, const TQCString &url, const TQString &additionalInfo)
Notify about a new bookmark Use "html" for the icon.
KBookmarkManager::root
KBookmarkGroup root() const
This will return the root bookmark.
Definition: kbookmarkmanager.cpp:383
KBookmarkImporterBase::newSeparator
void newSeparator()
Notify about a new separator.
KNSBookmarkExporterImpl
Definition: kbookmarkimporter_ns.h:114
KBookmarkGroup::next
KBookmark next(const KBookmark &current) const
Return the next sibling of a child bookmark of this group.
Definition: kbookmark.cpp:81
KBookmarkGroup::first
KBookmark first() const
Return the first child bookmark of this group.
Definition: kbookmark.cpp:71

tdeio/bookmarks

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

tdeio/bookmarks

Skip menu "tdeio/bookmarks"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/bookmarks by doxygen 1.8.8
This website is maintained by Timothy Pearson.