File indexing completed on 2024-05-05 17:45:00

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 "bookmarksrunner.h"
0009 #include "browsers/browser.h"
0010 
0011 #include <QDebug>
0012 #include <QDir>
0013 #include <QList>
0014 #include <QStack>
0015 #include <QUrl>
0016 
0017 #include <KApplicationTrader>
0018 #include <KConfigGroup>
0019 #include <KIO/OpenUrlJob>
0020 #include <KLocalizedString>
0021 #include <KSharedConfig>
0022 
0023 #include "bookmarkmatch.h"
0024 #include "bookmarksrunner_defs.h"
0025 #include "browserfactory.h"
0026 
0027 K_PLUGIN_CLASS_WITH_JSON(BookmarksRunner, "plasma-runner-bookmarks.json")
0028 
0029 BookmarksRunner::BookmarksRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0030     : Plasma::AbstractRunner(parent, metaData, args)
0031     , m_browser(nullptr)
0032     , m_browserFactory(new BrowserFactory(this))
0033 {
0034     setObjectName(QStringLiteral("Bookmarks"));
0035     addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds web browser bookmarks matching :q:.")));
0036     addSyntax(Plasma::RunnerSyntax(i18nc("list of all web browser bookmarks", "bookmarks"), i18n("List all web browser bookmarks")));
0037 
0038     connect(this, &Plasma::AbstractRunner::prepare, this, &BookmarksRunner::prep);
0039     setMinLetterCount(3);
0040 }
0041 
0042 BookmarksRunner::~BookmarksRunner() = default;
0043 
0044 void BookmarksRunner::prep()
0045 {
0046     auto browser = m_browserFactory->find(findBrowserName(), this);
0047     if (m_browser != browser) {
0048         m_browser = browser;
0049         connect(this, &Plasma::AbstractRunner::teardown, dynamic_cast<QObject *>(m_browser), [this]() {
0050             m_browser->teardown();
0051         });
0052     }
0053     m_browser->prepare();
0054 }
0055 
0056 void BookmarksRunner::match(Plasma::RunnerContext &context)
0057 {
0058     const QString term = context.query();
0059     bool allBookmarks = term.compare(i18nc("list of all konqueror bookmarks", "bookmarks"), Qt::CaseInsensitive) == 0;
0060 
0061     const QList<BookmarkMatch> matches = m_browser->match(term, allBookmarks);
0062     for (BookmarkMatch match : matches) {
0063         if (!context.isValid())
0064             return;
0065         context.addMatch(match.asQueryMatch(this));
0066     }
0067 }
0068 
0069 QString BookmarksRunner::findBrowserName()
0070 {
0071     // HACK find the default browser
0072     KConfigGroup config(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), QStringLiteral("General"));
0073     QString exec = config.readPathEntry(QStringLiteral("BrowserApplication"), QString());
0074     // qDebug() << "Found exec string: " << exec;
0075     if (exec.isEmpty()) {
0076         KService::Ptr service = KApplicationTrader::preferredService(QStringLiteral("text/html"));
0077         if (service) {
0078             exec = service->exec();
0079         }
0080     }
0081 
0082     // qDebug() << "KRunner::Bookmarks: found executable " << exec << " as default browser";
0083     return exec;
0084 }
0085 
0086 void BookmarksRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action)
0087 {
0088     Q_UNUSED(context);
0089     const QString term = action.data().toString();
0090     QUrl url = QUrl(term);
0091 
0092     // support urls like "kde.org" by transforming them to https://kde.org
0093     if (url.scheme().isEmpty()) {
0094         const int idx = term.indexOf('/');
0095 
0096         url.clear();
0097         url.setHost(term.left(idx));
0098         if (idx != -1) {
0099             // allow queries
0100             const int queryStart = term.indexOf('?', idx);
0101             int pathLength = -1;
0102             if ((queryStart > -1) && (idx < queryStart)) {
0103                 pathLength = queryStart - idx;
0104                 url.setQuery(term.mid(queryStart));
0105             }
0106 
0107             url.setPath(term.mid(idx, pathLength));
0108         }
0109         url.setScheme(QStringLiteral("http"));
0110     }
0111 
0112     auto job = new KIO::OpenUrlJob(url);
0113     job->start();
0114 }
0115 
0116 #include "bookmarksrunner.moc"