KBookmarks

kbookmarkimporter_opera.cpp
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*
3  This file is part of the KDE libraries
4  SPDX-FileCopyrightText: 2002-2003 Alexander Kellett <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kbookmarkimporter_opera.h"
10 #include "kbookmarkimporter_opera_p.h"
11 
12 #include <QFileDialog>
13 #include "kbookmarks_debug.h"
14 #include <QTextCodec>
15 #include <QApplication>
16 
17 #include <qplatformdefs.h>
18 
19 #include "kbookmarkimporter.h"
20 
21 void KOperaBookmarkImporter::parseOperaBookmarks()
22 {
23  QFile file(m_fileName);
24  if (!file.open(QIODevice::ReadOnly)) {
25  return;
26  }
27 
28  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
29  Q_ASSERT(codec);
30  if (!codec) {
31  return;
32  }
33 
34  QString url, name, type;
35  int lineno = 0;
36  QTextStream stream(&file);
37  stream.setCodec(codec);
38  while (! stream.atEnd()) {
39  lineno++;
40  QString line = stream.readLine().trimmed();
41 
42  // first two headers lines contain details about the format
43  if (lineno <= 2) {
44  if (line.startsWith(QLatin1String("options:"), Qt::CaseInsensitive)) {
45  const auto lst = line.mid(8).split(QLatin1Char(','));
46  for (const QString &ba : lst) {
47  const int pos = ba.indexOf(QLatin1Char('='));
48  if (pos < 1) {
49  continue;
50  }
51  const QString key = ba.left(pos).trimmed().toLower();
52  const QString value = ba.mid(pos + 1).trimmed();
53  }
54  }
55  continue;
56  }
57 
58  // at least up till version<=3 the following is valid
59  if (line.isEmpty()) {
60  // end of data block
61  if (type.isNull()) {
62  continue;
63  } else if (type == QLatin1String("URL")) {
64  emit newBookmark(name, url, QLatin1String(""));
65  } else if (type == QLatin1String("FOLDER")) {
66  emit newFolder(name, false, QLatin1String(""));
67  }
68 
69  type.clear();
70  name.clear();
71  url.clear();
72  } else if (line == QLatin1String("-")) {
73  // end of folder
74  emit endFolder();
75  } else {
76  // data block line
77  QString tag;
78  if (tag = QStringLiteral("#"), line.startsWith(tag)) {
79  type = line.remove(0, tag.length());
80  } else if (tag = QStringLiteral("NAME="), line.startsWith(tag)) {
81  name = line.remove(0, tag.length());
82  } else if (tag = QStringLiteral("URL="), line.startsWith(tag)) {
83  url = line.remove(0, tag.length());
84  }
85  }
86  }
87 }
88 
89 QString KOperaBookmarkImporter::operaBookmarksFile()
90 {
91  static KOperaBookmarkImporterImpl *p = nullptr;
92  if (!p) {
94  }
95  return p->findDefaultLocation();
96 }
97 
98 void KOperaBookmarkImporterImpl::parse()
99 {
100  KOperaBookmarkImporter importer(m_fileName);
101  setupSignalForwards(&importer, this);
102  importer.parseOperaBookmarks();
103 }
104 
105 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
106 {
107  const QString operaHomePath = QDir::homePath() + QLatin1String("/.opera");
109  operaHomePath,
110  tr("Opera Bookmark Files (*.adr)"))
111  : QFileDialog::getOpenFileName(QApplication::activeWindow(), QString(),
112  operaHomePath,
113  tr("*.adr|Opera Bookmark Files (*.adr)"));
114 }
115 
117 
118 class OperaExporter : private KBookmarkGroupTraverser
119 {
120 public:
121  OperaExporter();
122  QString generate(const KBookmarkGroup &grp)
123  {
124  traverse(grp);
125  return m_string;
126  }
127 private:
128  void visit(const KBookmark &) override;
129  void visitEnter(const KBookmarkGroup &) override;
130  void visitLeave(const KBookmarkGroup &) override;
131 private:
132  QString m_string;
133  QTextStream m_out;
134 };
135 
136 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly)
137 {
138  m_out << "Opera Hotlist version 2.0\n";
139  m_out << "Options: encoding = utf8, version=3\n";
140  m_out.flush();
141 }
142 
143 void OperaExporter::visit(const KBookmark &bk)
144 {
145  // qCDebug(KBOOKMARKS_LOG) << "visit(" << bk.text() << ")";
146  m_out << "#URL\n";
147  m_out << "\tNAME=" << bk.fullText() << '\n';
148  m_out << "\tURL=" << bk.url().toString().toUtf8() << '\n';
149  m_out << '\n';
150  m_out.flush();
151 }
152 
153 void OperaExporter::visitEnter(const KBookmarkGroup &grp)
154 {
155  // qCDebug(KBOOKMARKS_LOG) << "visitEnter(" << grp.text() << ")";
156  m_out << "#FOLDER\n";
157  m_out << "\tNAME=" << grp.fullText() << '\n';
158  m_out << '\n';
159  m_out.flush();
160 }
161 
162 void OperaExporter::visitLeave(const KBookmarkGroup &)
163 {
164  // qCDebug(KBOOKMARKS_LOG) << "visitLeave()";
165  m_out << "-\n";
166  m_out << '\n';
167  m_out.flush();
168 }
169 
170 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent)
171 {
172  OperaExporter exporter;
173  QString content = exporter.generate(parent);
174  QFile file(m_fileName);
175  if (!file.open(QIODevice::WriteOnly)) {
176  qCCritical(KBOOKMARKS_LOG) << "Can't write to file " << m_fileName;
177  return;
178  }
179  QTextStream fstream(&file);
180  fstream.setCodec(QTextCodec::codecForName("UTF-8"));
181  fstream << content;
182 }
183 
184 #include "moc_kbookmarkimporter_opera_p.cpp"
QString name(const QVariant &location)
QUrl url() const
URL contained by the bookmark.
Definition: kbookmark.cpp:330
QString & remove(int position, int n)
A class representing a bookmark.
Definition: kbookmark.h:27
QString homePath()
QString toString(QUrl::FormattingOptions options) const const
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:321
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.
A class to traverse bookarm groups.
Definition: kbookmark.h:433
QString fullText() const
Text shown for the bookmark, not truncated.
Definition: kbookmark.cpp:302
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Sep 24 2020 22:40:30 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.