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

KIO

  • 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
38void 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
103QString KOperaBookmarkImporter::operaBookmarksFile()
104{
105 static KOperaBookmarkImporterImpl *p = 0;
106 if (!p)
107 p = new KOperaBookmarkImporterImpl;
108 return p->findDefaultLocation();
109}
110
111void KOperaBookmarkImporterImpl::parse() {
112 KOperaBookmarkImporter importer(m_fileName);
113 setupSignalForwards(&importer, this);
114 importer.parseOperaBookmarks();
115}
116
117QString 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
131class OperaExporter : private KBookmarkGroupTraverser {
132public:
133 OperaExporter();
134 QString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; }
135private:
136 virtual void visit( const KBookmark & );
137 virtual void visitEnter( const KBookmarkGroup & );
138 virtual void visitLeave( const KBookmarkGroup & );
139private:
140 QString m_string;
141 QTextStream m_out;
142};
143
144OperaExporter::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
149void 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
157void 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
164void OperaExporter::visitLeave( const KBookmarkGroup & ) {
165 // kDebug() << "visitLeave()";
166 m_out << "-" << endl;
167 m_out << endl;
168}
169
170void 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"
KBookmarkExporterBase::m_fileName
QString m_fileName
Definition: kbookmarkexporter.h:38
KBookmarkGroupTraverser
Definition: kbookmark.h:459
KBookmarkGroupTraverser::traverse
void traverse(const KBookmarkGroup &)
Definition: kbookmark.cc:621
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:348
KBookmarkImporterBase::m_fileName
QString m_fileName
Definition: kbookmarkimporter.h:75
KBookmarkImporterBase::setupSignalForwards
void setupSignalForwards(QObject *src, QObject *dst)
Definition: kbookmarkimporter.cc:73
KBookmark
Definition: kbookmark.h:35
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
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
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
KOperaBookmarkExporterImpl::write
virtual void write(const KBookmarkGroup &parent)
Definition: kbookmarkimporter_opera.cc:170
KOperaBookmarkImporterImpl
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:31
KOperaBookmarkImporterImpl::findDefaultLocation
virtual QString findDefaultLocation(bool forSaving=false) const
Definition: kbookmarkimporter_opera.cc:117
KOperaBookmarkImporterImpl::parse
virtual void parse()
Definition: kbookmarkimporter_opera.cc:111
KOperaBookmarkImporter
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera_p.h:27
KOperaBookmarkImporter::m_fileName
QString m_fileName
Definition: kbookmarkimporter_opera_p.h:45
KOperaBookmarkImporter::newBookmark
void newBookmark(const QString &text, const QString &url, const QString &additionalInfo)
KOperaBookmarkImporter::operaBookmarksFile
static QString operaBookmarksFile()
Definition: kbookmarkimporter_opera.cc:103
KOperaBookmarkImporter::newFolder
void newFolder(const QString &text, bool open, const QString &additionalInfo)
KOperaBookmarkImporter::parseOperaBookmarks
void parseOperaBookmarks()
Definition: kbookmarkimporter_opera.cc:38
KOperaBookmarkImporter::endFolder
void endFolder()
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QIODevice
kbookmarkimporter.h
kbookmarkimporter_opera.h
kbookmarkimporter_opera_p.h
kdebug.h
kfiledialog.h
klocale.h
i18n
QString i18n(const char *text)
kstringhandler.h
generate
void generate(const QStringList &source, QStringList &target)
name
const char * name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

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