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

kio/bookmarks

  • kio
  • bookmarks
kbookmarkimporter_opera.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) 2002-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 <kfiledialog.h>
22 #include <kstringhandler.h>
23 #include <klocale.h>
24 #include <kdebug.h>
25 #include <tqtextcodec.h>
26 
27 #include <sys/types.h>
28 #include <stddef.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 
32 #include "kbookmarkimporter.h"
33 #include "kbookmarkimporter_opera.h"
34 
35 void KOperaBookmarkImporter::parseOperaBookmarks( )
36 {
37  TQFile file(m_fileName);
38  if(!file.open(IO_ReadOnly)) {
39  return;
40  }
41 
42  TQTextCodec * codec = TQTextCodec::codecForName("UTF-8");
43  Q_ASSERT(codec);
44  if (!codec)
45  return;
46 
47  int lineno = 0;
48  TQString url, name, type;
49  static const int g_lineLimit = 16*1024;
50  TQCString line(g_lineLimit);
51 
52  while ( file.readLine(line.data(), g_lineLimit) >=0 ) {
53  lineno++;
54 
55  // skip lines that didn't fit in buffer and first two headers lines
56  if ( line[line.length()-1] != '\n' || lineno <= 2 )
57  continue;
58 
59  TQString currentLine = codec->toUnicode(line).stripWhiteSpace();
60 
61  if (currentLine.isEmpty()) {
62  // end of data block
63  if (type.isNull())
64  continue;
65  else if ( type == "URL")
66  emit newBookmark( name, url.latin1(), "" );
67  else if (type == "FOLDER" )
68  emit newFolder( name, false, "" );
69 
70  type = TQString::null;
71  name = TQString::null;
72  url = TQString::null;
73 
74  } else if (currentLine == "-") {
75  // end of folder
76  emit endFolder();
77 
78  } else {
79  // data block line
80  TQString tag;
81  if ( tag = "#", currentLine.startsWith( tag ) )
82  type = currentLine.remove( 0, tag.length() );
83  else if ( tag = "NAME=", currentLine.startsWith( tag ) )
84  name = currentLine.remove(0, tag.length());
85  else if ( tag = "URL=", currentLine.startsWith( tag ) )
86  url = currentLine.remove(0, tag.length());
87  }
88  }
89 
90 }
91 
92 TQString KOperaBookmarkImporter::operaBookmarksFile()
93 {
94  static KOperaBookmarkImporterImpl *p = 0;
95  if (!p)
96  p = new KOperaBookmarkImporterImpl;
97  return p->findDefaultLocation();
98 }
99 
100 void KOperaBookmarkImporterImpl::parse() {
101  KOperaBookmarkImporter importer(m_fileName);
102  setupSignalForwards(&importer, this);
103  importer.parseOperaBookmarks();
104 }
105 
106 TQString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
107 {
108  return saving ? KFileDialog::getSaveFileName(
109  TQDir::homeDirPath() + "/.opera",
110  i18n("*.adr|Opera Bookmark Files (*.adr)") )
111  : KFileDialog::getOpenFileName(
112  TQDir::homeDirPath() + "/.opera",
113  i18n("*.adr|Opera Bookmark Files (*.adr)") );
114 }
115 
117 
118 class OperaExporter : private KBookmarkGroupTraverser {
119 public:
120  OperaExporter();
121  TQString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; };
122 private:
123  virtual void visit( const KBookmark & );
124  virtual void visitEnter( const KBookmarkGroup & );
125  virtual void visitLeave( const KBookmarkGroup & );
126 private:
127  TQString m_string;
128  TQTextStream m_out;
129 };
130 
131 OperaExporter::OperaExporter() : m_out(&m_string, IO_WriteOnly) {
132  m_out << "Opera Hotlist version 2.0" << endl;
133  m_out << "Options: encoding = utf8, version=3" << endl;
134 }
135 
136 void OperaExporter::visit( const KBookmark &bk ) {
137  // kdDebug() << "visit(" << bk.text() << ")" << endl;
138  m_out << "#URL" << endl;
139  m_out << "\tNAME=" << bk.fullText() << endl;
140  m_out << "\tURL=" << bk.url().url().utf8() << endl;
141  m_out << endl;
142 }
143 
144 void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
145  // kdDebug() << "visitEnter(" << grp.text() << ")" << endl;
146  m_out << "#FOLDER" << endl;
147  m_out << "\tNAME="<< grp.fullText() << endl;
148  m_out << endl;
149 }
150 
151 void OperaExporter::visitLeave( const KBookmarkGroup & ) {
152  // kdDebug() << "visitLeave()" << endl;
153  m_out << "-" << endl;
154  m_out << endl;
155 }
156 
157 void KOperaBookmarkExporterImpl::write(KBookmarkGroup parent) {
158  OperaExporter exporter;
159  TQString content = exporter.generate( parent );
160  TQFile file(m_fileName);
161  if (!file.open(IO_WriteOnly)) {
162  kdError(7043) << "Can't write to file " << m_fileName << endl;
163  return;
164  }
165  TQTextStream fstream(&file);
166  fstream.setEncoding(TQTextStream::UnicodeUTF8);
167  fstream << content;
168 }
169 
170 #include "kbookmarkimporter_opera.moc"
KOperaBookmarkImporter
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:35
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:199
KOperaBookmarkImporterImpl
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:61
KBookmarkGroupTraverser
Definition: kbookmark.h:318

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.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |