KBookmarks

kbookmarkimporter_opera.cpp
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 /* This file is part of the KDE libraries
3  Copyright (C) 2002-2003 Alexander Kellett <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kbookmarkimporter_opera.h"
21 #include "kbookmarkimporter_opera_p.h"
22 
23 #include <QFileDialog>
24 #include "kbookmarks_debug.h"
25 #include <qtextcodec.h>
26 #include <QApplication>
27 
28 #include <qplatformdefs.h>
29 
30 #include "kbookmarkimporter.h"
31 
32 void KOperaBookmarkImporter::parseOperaBookmarks()
33 {
34  QFile file(m_fileName);
35  if (!file.open(QIODevice::ReadOnly)) {
36  return;
37  }
38 
39  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
40  Q_ASSERT(codec);
41  if (!codec) {
42  return;
43  }
44 
45  QString url, name, type;
46  int lineno = 0;
47  QTextStream stream(&file);
48  stream.setCodec(codec);
49  while (! stream.atEnd()) {
50  lineno++;
51  QString line = stream.readLine().trimmed();
52 
53  // first two headers lines contain details about the format
54  if (lineno <= 2) {
55  if (line.startsWith(QLatin1String("options:"), Qt::CaseInsensitive)) {
56  const auto lst = line.mid(8).split(QLatin1Char(','));
57  for (const QString &ba : lst) {
58  const int pos = ba.indexOf(QLatin1Char('='));
59  if (pos < 1) {
60  continue;
61  }
62  const QString key = ba.left(pos).trimmed().toLower();
63  const QString value = ba.mid(pos + 1).trimmed();
64  }
65  }
66  continue;
67  }
68 
69  // at least up till version<=3 the following is valid
70  if (line.isEmpty()) {
71  // end of data block
72  if (type.isNull()) {
73  continue;
74  } else if (type == QLatin1String("URL")) {
75  emit newBookmark(name, url, QLatin1String(""));
76  } else if (type == QLatin1String("FOLDER")) {
77  emit newFolder(name, false, QLatin1String(""));
78  }
79 
80  type.clear();
81  name.clear();
82  url.clear();
83  } else if (line == QLatin1String("-")) {
84  // end of folder
85  emit endFolder();
86  } else {
87  // data block line
88  QString tag;
89  if (tag = QStringLiteral("#"), line.startsWith(tag)) {
90  type = line.remove(0, tag.length());
91  } else if (tag = QStringLiteral("NAME="), line.startsWith(tag)) {
92  name = line.remove(0, tag.length());
93  } else if (tag = QStringLiteral("URL="), line.startsWith(tag)) {
94  url = line.remove(0, tag.length());
95  }
96  }
97  }
98 }
99 
100 QString KOperaBookmarkImporter::operaBookmarksFile()
101 {
102  static KOperaBookmarkImporterImpl *p = nullptr;
103  if (!p) {
105  }
106  return p->findDefaultLocation();
107 }
108 
109 void KOperaBookmarkImporterImpl::parse()
110 {
111  KOperaBookmarkImporter importer(m_fileName);
112  setupSignalForwards(&importer, this);
113  importer.parseOperaBookmarks();
114 }
115 
116 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
117 {
118  const QString operaHomePath = QDir::homePath() + QLatin1String("/.opera");
120  operaHomePath,
121  tr("Opera Bookmark Files (*.adr)"))
122  : QFileDialog::getOpenFileName(QApplication::activeWindow(), QString(),
123  operaHomePath,
124  tr("*.adr|Opera Bookmark Files (*.adr)"));
125 }
126 
128 
129 class OperaExporter : private KBookmarkGroupTraverser
130 {
131 public:
132  OperaExporter();
133  QString generate(const KBookmarkGroup &grp)
134  {
135  traverse(grp);
136  return m_string;
137  }
138 private:
139  void visit(const KBookmark &) override;
140  void visitEnter(const KBookmarkGroup &) override;
141  void visitLeave(const KBookmarkGroup &) override;
142 private:
143  QString m_string;
144  QTextStream m_out;
145 };
146 
147 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly)
148 {
149  m_out << "Opera Hotlist version 2.0\n";
150  m_out << "Options: encoding = utf8, version=3\n";
151  m_out.flush();
152 }
153 
154 void OperaExporter::visit(const KBookmark &bk)
155 {
156  // qCDebug(KBOOKMARKS_LOG) << "visit(" << bk.text() << ")";
157  m_out << "#URL\n";
158  m_out << "\tNAME=" << bk.fullText() << '\n';
159  m_out << "\tURL=" << bk.url().toString().toUtf8() << '\n';
160  m_out << '\n';
161  m_out.flush();
162 }
163 
164 void OperaExporter::visitEnter(const KBookmarkGroup &grp)
165 {
166  // qCDebug(KBOOKMARKS_LOG) << "visitEnter(" << grp.text() << ")";
167  m_out << "#FOLDER\n";
168  m_out << "\tNAME=" << grp.fullText() << '\n';
169  m_out << '\n';
170  m_out.flush();
171 }
172 
173 void OperaExporter::visitLeave(const KBookmarkGroup &)
174 {
175  // qCDebug(KBOOKMARKS_LOG) << "visitLeave()";
176  m_out << "-\n";
177  m_out << '\n';
178  m_out.flush();
179 }
180 
181 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent)
182 {
183  OperaExporter exporter;
184  QString content = exporter.generate(parent);
185  QFile file(m_fileName);
186  if (!file.open(QIODevice::WriteOnly)) {
187  qCCritical(KBOOKMARKS_LOG) << "Can't write to file " << m_fileName;
188  return;
189  }
190  QTextStream fstream(&file);
191  fstream.setCodec(QTextCodec::codecForName("UTF-8"));
192  fstream << content;
193 }
194 
195 #include "moc_kbookmarkimporter_opera_p.cpp"
QString name(const QVariant &location)
QString & remove(int position, int n)
QString homePath()
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const const
void clear()
QWidget * activeWindow()
Type type(const QSqlDatabase &db)
CaseInsensitive
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
A group of bookmarks.
Definition: kbookmark.h:327
int indexOf(QStringView str, int from) const const
QString toLower() const const
void generate(Iter begin, Iter end, QStringList &target)
QString mid(int position, int n) const const
QTextCodec * codecForName(const QByteArray &name)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
int length() const const
QString left(int n) const const
QObject * parent() const const
A class for importing Opera bookmarks.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 2 2020 22:38:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.