File indexing completed on 2024-04-28 05:49:59

0001 //  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2002-2003 Alexander Kellett <lypanov@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include "kbookmarkimporter_opera.h"
0010 #include "kbookmarkimporter.h"
0011 #include "kbookmarkimporter_opera_p.h"
0012 
0013 #include <QApplication>
0014 #include <QDebug>
0015 #include <QFileDialog>
0016 
0017 #include <qplatformdefs.h>
0018 
0019 void KOperaBookmarkImporter::parseOperaBookmarks()
0020 {
0021     QFile file(m_fileName);
0022     if (!file.open(QIODevice::ReadOnly)) {
0023         return;
0024     }
0025 
0026     QString url;
0027     QString name;
0028     QString type;
0029     int lineno = 0;
0030     QTextStream stream(&file);
0031     while (!stream.atEnd()) {
0032         lineno++;
0033         QString line = stream.readLine().trimmed();
0034 
0035         // first two headers lines contain details about the format
0036         if (lineno <= 2) {
0037             if (line.startsWith(QLatin1String("options:"), Qt::CaseInsensitive)) {
0038                 const auto lst = line.mid(8).split(QLatin1Char(','));
0039                 for (const QString &ba : lst) {
0040                     const int pos = ba.indexOf(QLatin1Char('='));
0041                     if (pos < 1) {
0042                         continue;
0043                     }
0044                 }
0045             }
0046             continue;
0047         }
0048 
0049         // at least up till version<=3 the following is valid
0050         if (line.isEmpty()) {
0051             // end of data block
0052             if (type.isNull()) {
0053                 continue;
0054             } else if (type == QLatin1String("URL")) {
0055                 Q_EMIT newBookmark(name, url, QLatin1String(""));
0056             } else if (type == QLatin1String("FOLDER")) {
0057                 Q_EMIT newFolder(name, false, QLatin1String(""));
0058             }
0059 
0060             type.clear();
0061             name.clear();
0062             url.clear();
0063         } else if (line == QLatin1String("-")) {
0064             // end of folder
0065             Q_EMIT endFolder();
0066         } else {
0067             // data block line
0068             QString tag;
0069             if (tag = QStringLiteral("#"), line.startsWith(tag)) {
0070                 type = line.remove(0, tag.length());
0071             } else if (tag = QStringLiteral("NAME="), line.startsWith(tag)) {
0072                 name = line.remove(0, tag.length());
0073             } else if (tag = QStringLiteral("URL="), line.startsWith(tag)) {
0074                 url = line.remove(0, tag.length());
0075             }
0076         }
0077     }
0078 }
0079 
0080 QString KOperaBookmarkImporter::operaBookmarksFile()
0081 {
0082     static OperaBookmarkImporterImpl *p = nullptr;
0083     if (!p) {
0084         p = new OperaBookmarkImporterImpl;
0085     }
0086     return p->findDefaultLocation();
0087 }
0088 
0089 void OperaBookmarkImporterImpl::parse()
0090 {
0091     KOperaBookmarkImporter importer(m_fileName);
0092     setupSignalForwards(&importer, this);
0093     importer.parseOperaBookmarks();
0094 }
0095 
0096 QString OperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
0097 {
0098     const QString operaHomePath = QDir::homePath() + QLatin1String("/.opera");
0099     return saving ? QFileDialog::getSaveFileName(QApplication::activeWindow(), QString(), operaHomePath, tr("Opera Bookmark Files (*.adr)"))
0100                   : QFileDialog::getOpenFileName(QApplication::activeWindow(), QString(), operaHomePath, tr("*.adr|Opera Bookmark Files (*.adr)"));
0101 }
0102 
0103 /////////////////////////////////////////////////
0104 
0105 class OperaExporter : private KBookmarkGroupTraverser
0106 {
0107 public:
0108     OperaExporter();
0109     QString generate(const KBookmarkGroup &grp)
0110     {
0111         traverse(grp);
0112         return m_string;
0113     }
0114 
0115 private:
0116     void visit(const KBookmark &) override;
0117     void visitEnter(const KBookmarkGroup &) override;
0118     void visitLeave(const KBookmarkGroup &) override;
0119 
0120 private:
0121     QString m_string;
0122     QTextStream m_out;
0123 };
0124 
0125 OperaExporter::OperaExporter()
0126     : m_out(&m_string, QIODevice::WriteOnly)
0127 {
0128     m_out << "Opera Hotlist version 2.0\n";
0129     m_out << "Options: encoding = utf8, version=3\n";
0130     m_out.flush();
0131 }
0132 
0133 void OperaExporter::visit(const KBookmark &bk)
0134 {
0135     // qCDebug(KBOOKMARKS_LOG) << "visit(" << bk.text() << ")";
0136     m_out << "#URL\n";
0137     m_out << "\tNAME=" << bk.fullText() << '\n';
0138     m_out << "\tURL=" << bk.url().toString().toUtf8() << '\n';
0139     m_out << '\n';
0140     m_out.flush();
0141 }
0142 
0143 void OperaExporter::visitEnter(const KBookmarkGroup &grp)
0144 {
0145     // qCDebug(KBOOKMARKS_LOG) << "visitEnter(" << grp.text() << ")";
0146     m_out << "#FOLDER\n";
0147     m_out << "\tNAME=" << grp.fullText() << '\n';
0148     m_out << '\n';
0149     m_out.flush();
0150 }
0151 
0152 void OperaExporter::visitLeave(const KBookmarkGroup &)
0153 {
0154     // qCDebug(KBOOKMARKS_LOG) << "visitLeave()";
0155     m_out << "-\n";
0156     m_out << '\n';
0157     m_out.flush();
0158 }
0159 
0160 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent)
0161 {
0162     OperaExporter exporter;
0163     QString content = exporter.generate(parent);
0164     QFile file(m_fileName);
0165     if (!file.open(QIODevice::WriteOnly)) {
0166         qCritical() << "Can't write to file " << m_fileName;
0167         return;
0168     }
0169     QTextStream fstream(&file);
0170     fstream << content;
0171 }
0172 
0173 #include "moc_kbookmarkimporter_opera.cpp"
0174 #include "moc_kbookmarkimporter_opera_p.cpp"