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

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • bookmarks
kbookmarkimporter_opera.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) 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 "kbookmarkimporter_opera.h"
22 #include "kbookmarkimporter_opera_p.h"
23 
24 #include <kfiledialog.h>
25 #include <kstringhandler.h>
26 #include <klocale.h>
27 #include <kdebug.h>
28 #include <qtextcodec.h>
29 #include <QtGui/QApplication>
30 
31 #include <sys/types.h>
32 #include <stddef.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 
36 #include "kbookmarkimporter.h"
37 
38 void KOperaBookmarkImporter::parseOperaBookmarks( )
39 {
40  QFile file(m_fileName);
41  if(!file.open(QIODevice::ReadOnly)) {
42  return;
43  }
44 
45  QTextCodec * codec = QTextCodec::codecForName("UTF-8");
46  Q_ASSERT(codec);
47  if (!codec)
48  return;
49 
50  QString url, name, type;
51  int lineno = 0, version = 0;
52  QTextStream stream(&file);
53  stream.setCodec(codec);
54  while(! stream.atEnd()) {
55  lineno++;
56  QString line = stream.readLine().trimmed();
57 
58  // first two headers lines contain details about the format
59  if (lineno <= 2) {
60  if (line.toLower().startsWith(QLatin1String("options:"))) {
61  foreach(const QString &ba, line.mid(8).split(',')) {
62  const int pos = ba.indexOf('=');
63  if (pos < 1)
64  continue;
65  const QString key = ba.left(pos).trimmed().toLower();
66  const QString value = ba.mid(pos+1).trimmed();
67  if (key == "version")
68  version = value.toInt();
69  }
70  }
71  continue;
72  }
73 
74  // at least up till version<=3 the following is valid
75  if (line.isEmpty()) {
76  // end of data block
77  if (type.isNull())
78  continue;
79  else if ( type == "URL")
80  emit newBookmark( name, url, "" );
81  else if (type == "FOLDER" )
82  emit newFolder( name, false, "" );
83 
84  type.clear();
85  name.clear();
86  url.clear();
87  } else if (line == "-") {
88  // end of folder
89  emit endFolder();
90  } else {
91  // data block line
92  QString tag;
93  if ( tag = '#', line.startsWith( tag ) )
94  type = line.remove( 0, tag.length() );
95  else if ( tag = "NAME=", line.startsWith( tag ) )
96  name = line.remove(0, tag.length());
97  else if ( tag = "URL=", line.startsWith( tag ) )
98  url = line.remove(0, tag.length());
99  }
100  }
101 }
102 
103 QString KOperaBookmarkImporter::operaBookmarksFile()
104 {
105  static KOperaBookmarkImporterImpl *p = 0;
106  if (!p)
107  p = new KOperaBookmarkImporterImpl;
108  return p->findDefaultLocation();
109 }
110 
111 void KOperaBookmarkImporterImpl::parse() {
112  KOperaBookmarkImporter importer(m_fileName);
113  setupSignalForwards(&importer, this);
114  importer.parseOperaBookmarks();
115 }
116 
117 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
118 {
119  return saving ? KFileDialog::getSaveFileName(
120  QString(QDir::homePath() + "/.opera"),
121  i18n("*.adr|Opera Bookmark Files (*.adr)"),
122  QApplication::activeWindow() )
123  : KFileDialog::getOpenFileName(
124  QString(QDir::homePath() + "/.opera"),
125  i18n("*.adr|Opera Bookmark Files (*.adr)"),
126  QApplication::activeWindow() );
127 }
128 
130 
131 class OperaExporter : private KBookmarkGroupTraverser {
132 public:
133  OperaExporter();
134  QString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; }
135 private:
136  virtual void visit( const KBookmark & );
137  virtual void visitEnter( const KBookmarkGroup & );
138  virtual void visitLeave( const KBookmarkGroup & );
139 private:
140  QString m_string;
141  QTextStream m_out;
142 };
143 
144 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly) {
145  m_out << "Opera Hotlist version 2.0" << endl;
146  m_out << "Options: encoding = utf8, version=3" << endl;
147 }
148 
149 void OperaExporter::visit( const KBookmark &bk ) {
150  // kDebug() << "visit(" << bk.text() << ")";
151  m_out << "#URL" << endl;
152  m_out << "\tNAME=" << bk.fullText() << endl;
153  m_out << "\tURL=" << bk.url().url().toUtf8() << endl;
154  m_out << endl;
155 }
156 
157 void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
158  // kDebug() << "visitEnter(" << grp.text() << ")";
159  m_out << "#FOLDER" << endl;
160  m_out << "\tNAME="<< grp.fullText() << endl;
161  m_out << endl;
162 }
163 
164 void OperaExporter::visitLeave( const KBookmarkGroup & ) {
165  // kDebug() << "visitLeave()";
166  m_out << "-" << endl;
167  m_out << endl;
168 }
169 
170 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent) {
171  OperaExporter exporter;
172  QString content = exporter.generate( parent );
173  QFile file(m_fileName);
174  if (!file.open(QIODevice::WriteOnly)) {
175  kError(7043) << "Can't write to file " << m_fileName << endl;
176  return;
177  }
178  QTextStream fstream(&file);
179  fstream.setCodec(QTextCodec::codecForName("UTF-8"));
180  fstream << content;
181 }
182 
183 #include "kbookmarkimporter_opera_p.moc"
i18n
QString i18n(const char *text)
KBookmarkGroupTraverser::traverse
void traverse(const KBookmarkGroup &)
Definition: kbookmark.cc:621
KOperaBookmarkImporter::endFolder
void endFolder()
kdebug.h
KBookmarkGroupTraverser::visit
virtual void visit(const KBookmark &)
Definition: kbookmark.cc:647
kfiledialog.h
name
const char * name(StandardAction id)
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QString
kbookmarkimporter_opera_p.h
klocale.h
KBookmark
Definition: kbookmark.h:34
KBookmarkGroupTraverser::visitEnter
virtual void visitEnter(const KBookmarkGroup &)
Definition: kbookmark.cc:651
KOperaBookmarkImporter::newFolder
void newFolder(const QString &text, bool open, const QString &additionalInfo)
KOperaBookmarkImporter::parseOperaBookmarks
void parseOperaBookmarks()
Definition: kbookmarkimporter_opera.cc:38
kbookmarkimporter.h
KOperaBookmarkImporter
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera_p.h:26
KBookmarkExporterBase::m_fileName
QString m_fileName
Definition: kbookmarkexporter.h:38
KFileDialog::getOpenFileName
static QString getOpenFileName(const KUrl &startDir=KUrl(), const QString &filter=QString(), QWidget *parent=0, const QString &caption=QString())
Creates a modal file dialog and return the selected filename or an empty string if none was chosen...
Definition: kfiledialog.cpp:464
KOperaBookmarkImporter::newBookmark
void newBookmark(const QString &text, const QString &url, const QString &additionalInfo)
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:347
KOperaBookmarkImporter::m_fileName
QString m_fileName
Definition: kbookmarkimporter_opera_p.h:45
KBookmarkImporterBase::m_fileName
QString m_fileName
Definition: kbookmarkimporter.h:75
kbookmarkimporter_opera.h
KFileDialog::getSaveFileName
static QString getSaveFileName(const KUrl &startDir=KUrl(), const QString &filter=QString(), QWidget *parent=0, const QString &caption=QString())
Creates a modal file dialog and returns the selected filename or an empty string if none was chosen...
Definition: kfiledialog.cpp:701
kstringhandler.h
generate
void generate(Iter begin, Iter end, QStringList &target)
KBookmarkImporterBase::setupSignalForwards
void setupSignalForwards(QObject *src, QObject *dst)
Definition: kbookmarkimporter.cc:73
KOperaBookmarkImporter::operaBookmarksFile
static QString operaBookmarksFile()
Definition: kbookmarkimporter_opera.cc:103
version
unsigned int version()
KOperaBookmarkImporterImpl::parse
virtual void parse()
Definition: kbookmarkimporter_opera.cc:111
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QIODevice
KOperaBookmarkExporterImpl::write
virtual void write(const KBookmarkGroup &parent)
Definition: kbookmarkimporter_opera.cc:170
KOperaBookmarkImporterImpl::findDefaultLocation
virtual QString findDefaultLocation(bool forSaving=false) const
Definition: kbookmarkimporter_opera.cc:117
KOperaBookmarkImporterImpl
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:30
KBookmarkGroupTraverser
Definition: kbookmark.h:459
KBookmark::fullText
QString fullText() const
Text shown for the bookmark, not truncated.
Definition: kbookmark.cc:311
KBookmark::url
KUrl url() const
URL contained by the bookmark.
Definition: kbookmark.cc:338
KBookmarkGroupTraverser::visitLeave
virtual void visitLeave(const KBookmarkGroup &)
Definition: kbookmark.cc:655
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 by doxygen 1.8.7 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 API Reference

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

Search



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