Falkon Develop
Cross-platform Qt-based web browser
operaimporter.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program 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
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "operaimporter.h"
19#include "bookmarkitem.h"
20
21#include <QUrl>
22#include <QDir>
23#include <QFileDialog>
24
26 : BookmarksImporter(parent)
27{
28 m_stream.setEncoding(QStringConverter::Utf8);
29}
30
32{
33 return BookmarksImporter::tr("Opera stores its bookmarks in <b>bookmarks.adr</b> text file. "
34 "This file is usually located in");
35}
36
38{
39#ifdef Q_OS_WIN
40 return QString("%APPDATA%/Opera/");
41#else
42 return QDir::homePath() + QLatin1String("/.opera/");
43#endif
44}
45
46QString OperaImporter::getPath(QWidget* parent)
47{
48 m_path = QFileDialog::getOpenFileName(parent, BookmarksImporter::tr("Choose file..."), standardPath(), QStringLiteral("Bookmarks (*.adr)"));
49 return m_path;
50}
51
53{
54 m_file.setFileName(m_path);
55
56 if (!m_file.open(QFile::ReadOnly)) {
57 setError(BookmarksImporter::tr("Unable to open file."));
58 return false;
59 }
60
61 m_stream.setDevice(&m_file);
62
63 if (m_stream.readLine() != QLatin1String("Opera Hotlist version 2.0")) {
64 setError(BookmarksImporter::tr("File is not valid Opera bookmarks file!"));
65 return false;
66 }
67
68 if (!m_stream.readLine().startsWith(QLatin1String("Options: encoding = utf8"))) {
69 setError(BookmarksImporter::tr("Only UTF-8 encoded Opera bookmarks file is supported!"));
70 return false;
71 }
72
73 return true;
74}
75
77{
78 auto* root = new BookmarkItem(BookmarkItem::Folder);
79 root->setTitle(QSL("Opera Import"));
80
81 QList<BookmarkItem*> folders;
82 folders.append(root);
83
84 BookmarkItem* item = nullptr;
85
86#define PARENT folders.isEmpty() ? root : folders.last()
87
88 while (!m_stream.atEnd()) {
89 switch (parseLine(m_stream.readLine())) {
90 case StartFolder:
92 while (!m_stream.atEnd()) {
93 Token tok = parseLine(m_stream.readLine());
94 if (tok == EmptyLine)
95 break;
96 else if (tok == KeyValuePair && m_key == QLatin1String("NAME"))
97 item->setTitle(m_value);
98 }
99 folders.append(item);
100 break;
101
102 case EndFolder:
103 if (folders.count() > 0) {
104 folders.removeLast();
105 }
106 break;
107
108 case StartUrl:
110 while (!m_stream.atEnd()) {
111 Token tok = parseLine(m_stream.readLine());
112 if (tok == EmptyLine) {
113 break;
114 }
115 else if (tok == KeyValuePair) {
116 if (m_key == QL1S("NAME"))
117 item->setTitle(m_value);
118 else if (m_key == QL1S("URL"))
119 item->setUrl(QUrl(m_value));
120 else if (m_key == QL1S("DESCRIPTION"))
121 item->setDescription(m_value);
122 else if (m_key == QL1S("SHORT NAME"))
123 item->setKeyword(m_value);
124 }
125 }
126 break;
127
128 case StartSeparator:
130 while (!m_stream.atEnd()) {
131 if (parseLine(m_stream.readLine()) == EmptyLine) {
132 break;
133 }
134 }
135 break;
136
137 case StartDeleted:
138 while (!m_stream.atEnd()) {
139 if (parseLine(m_stream.readLine()) == EmptyLine) {
140 break;
141 }
142 }
143 break;
144
145 default: // EmptyLine
146 break;
147 }
148 }
149
150#undef PARENT
151
152 return root;
153}
154
155OperaImporter::Token OperaImporter::parseLine(const QString &line)
156{
157 const QString str = line.trimmed();
158
159 if (str.isEmpty()) {
160 return EmptyLine;
161 }
162
163 if (str == QLatin1String("#FOLDER")) {
164 return StartFolder;
165 }
166
167 if (str == QLatin1String("-")) {
168 return EndFolder;
169 }
170
171 if (str == QLatin1String("#URL")) {
172 return StartUrl;
173 }
174
175 if (str == QLatin1String("#SEPERATOR")) {
176 return StartSeparator;
177 }
178
179 if (str == QLatin1String("#DELETED")) {
180 return StartDeleted;
181 }
182
183 int index = str.indexOf(QLatin1Char('='));
184
185 // Let's assume "key=" is valid line with empty value (but not "=value")
186 if (index > 0) {
187 m_key = str.mid(0, index);
188 m_value = str.mid(index + 1);
189 return KeyValuePair;
190 }
191
192 return Invalid;
193}
void setDescription(const QString &description)
void setKeyword(const QString &keyword)
void setUrl(const QUrl &url)
void setTitle(const QString &title)
void setError(const QString &error)
BookmarkItem * importBookmarks() override
QString getPath(QWidget *parent) override
bool prepareImport() override
QString standardPath() const override
OperaImporter(QObject *parent=nullptr)
QString description() const override
#define PARENT
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40