File indexing completed on 2024-05-19 16:41:36

0001 /*
0002     SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
0003     SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "opera.h"
0009 #include "bookmarksrunner_defs.h"
0010 #include "favicon.h"
0011 #include <QDebug>
0012 #include <QDir>
0013 #include <QFile>
0014 
0015 Opera::Opera(QObject *parent)
0016     : QObject(parent)
0017     , m_favicon(new FallbackFavicon(this))
0018 {
0019 }
0020 
0021 QList<BookmarkMatch> Opera::match(const QString &term, bool addEverything)
0022 {
0023     QList<BookmarkMatch> matches;
0024 
0025     QLatin1String nameStart("\tNAME=");
0026     QLatin1String urlStart("\tURL=");
0027     QLatin1String descriptionStart("\tDESCRIPTION=");
0028 
0029     // search
0030     for (const QString &entry : qAsConst(m_operaBookmarkEntries)) {
0031         QStringList entryLines = entry.split(QStringLiteral("\n"));
0032         if (!entryLines.first().startsWith(QLatin1String("#URL"))) {
0033             continue; // skip folder entries
0034         }
0035         entryLines.pop_front();
0036 
0037         QString name;
0038         QString url;
0039         QString description;
0040 
0041         for (const QString &line : qAsConst(entryLines)) {
0042             if (line.startsWith(nameStart)) {
0043                 name = line.mid(QString(nameStart).length()).simplified();
0044             } else if (line.startsWith(urlStart)) {
0045                 url = line.mid(QString(urlStart).length()).simplified();
0046             } else if (line.startsWith(descriptionStart)) {
0047                 description = line.mid(QString(descriptionStart).length()).simplified();
0048             }
0049         }
0050 
0051         BookmarkMatch bookmarkMatch(m_favicon->iconFor(url), term, name, url, description);
0052         bookmarkMatch.addTo(matches, addEverything);
0053     }
0054     return matches;
0055 }
0056 
0057 void Opera::prepare()
0058 {
0059     // open bookmarks file
0060     QString operaBookmarksFilePath = QDir::homePath() + "/.opera/bookmarks.adr";
0061     QFile operaBookmarksFile(operaBookmarksFilePath);
0062     if (!operaBookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
0063         // qDebug() << "Could not open Operas Bookmark File " + operaBookmarksFilePath;
0064         return;
0065     }
0066 
0067     // check format
0068     QString firstLine = operaBookmarksFile.readLine();
0069     if (firstLine.compare(QLatin1String("Opera Hotlist version 2.0\n"))) {
0070         // qDebug() << "Format of Opera Bookmarks File might have changed.";
0071     }
0072     operaBookmarksFile.readLine(); // skip options line ("Options: encoding = utf8, version=3")
0073     operaBookmarksFile.readLine(); // skip empty line
0074 
0075     // load contents
0076     QString contents = operaBookmarksFile.readAll();
0077     m_operaBookmarkEntries = contents.split(QStringLiteral("\n\n"), Qt::SkipEmptyParts);
0078 
0079     // close file
0080     operaBookmarksFile.close();
0081 }
0082 
0083 void Opera::teardown()
0084 {
0085     m_operaBookmarkEntries.clear();
0086 }