Changeset View
Changeset View
Standalone View
Standalone View
host/bookmarksrunnerplugin.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 by Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | | ||||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
5 | of this software and associated documentation files (the "Software"), to deal | ||||
6 | in the Software without restriction, including without limitation the rights | ||||
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
8 | copies of the Software, and to permit persons to whom the Software is | ||||
9 | furnished to do so, subject to the following conditions: | ||||
10 | | ||||
11 | The above copyright notice and this permission notice shall be included in | ||||
12 | all copies or substantial portions of the Software. | ||||
13 | | ||||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
20 | THE SOFTWARE. | ||||
21 | */ | ||||
22 | | ||||
23 | #include "bookmarksrunnerplugin.h" | ||||
24 | | ||||
25 | #include "connection.h" | ||||
26 | | ||||
27 | #include <QDesktopServices> | ||||
28 | | ||||
29 | #include <KLocalizedString> | ||||
30 | | ||||
31 | #include "dbusutils_p.h" | ||||
32 | #include "krunner1adaptor.h" | ||||
33 | | ||||
34 | BookmarksRunnerPlugin::BookmarksRunnerPlugin(QObject *parent) | ||||
35 | : AbstractBrowserPlugin(QStringLiteral("bookmarksrunner"), 1, parent) | ||||
36 | { | ||||
37 | new Krunner1Adaptor(this); | ||||
38 | qDBusRegisterMetaType<RemoteMatch>(); | ||||
39 | qDBusRegisterMetaType<RemoteMatches>(); | ||||
40 | qDBusRegisterMetaType<RemoteAction>(); | ||||
41 | qDBusRegisterMetaType<RemoteActions>(); | ||||
42 | } | ||||
43 | | ||||
44 | bool BookmarksRunnerPlugin::onLoad() | ||||
45 | { | ||||
46 | return QDBusConnection::sessionBus().registerObject("/BookmarksRunner", this); | ||||
47 | } | ||||
48 | | ||||
49 | bool BookmarksRunnerPlugin::onUnload() | ||||
50 | { | ||||
51 | QDBusConnection::sessionBus().unregisterObject(QStringLiteral("/BookmarksRunner")); | ||||
52 | return true; | ||||
53 | } | ||||
54 | | ||||
55 | RemoteActions BookmarksRunnerPlugin::Actions() | ||||
56 | { | ||||
57 | return {}; | ||||
58 | } | ||||
59 | | ||||
60 | RemoteMatches BookmarksRunnerPlugin::Match(const QString &searchTerm) | ||||
61 | { | ||||
62 | setDelayedReply(true); | ||||
63 | | ||||
64 | if (m_lastRequest.type() != QDBusMessage::InvalidMessage) { | ||||
65 | QDBusConnection::sessionBus().send(m_lastRequest.createReply(QVariantList())); | ||||
66 | } | ||||
67 | | ||||
68 | m_lastRequest = message(); | ||||
69 | m_searchTerm = searchTerm; | ||||
70 | | ||||
71 | // TODO enforce minimum length? | ||||
72 | sendData(QStringLiteral("search"), { | ||||
73 | {QStringLiteral("query"), searchTerm} | ||||
74 | }); | ||||
75 | | ||||
76 | return RemoteMatches(); | ||||
77 | } | ||||
78 | | ||||
79 | void BookmarksRunnerPlugin::handleData(const QString &event, const QJsonObject &json) | ||||
80 | { | ||||
81 | if (event != QLatin1String("gotBookmarks")) { | ||||
82 | return; | ||||
83 | } | ||||
84 | | ||||
85 | if (m_lastRequest.type() == QDBusMessage::InvalidMessage) { | ||||
86 | return; | ||||
87 | } | ||||
88 | | ||||
89 | RemoteMatches matches; | ||||
90 | | ||||
91 | const QJsonArray &bookmarks = json.value(QStringLiteral("bookmarks")).toArray(); | ||||
92 | | ||||
93 | qreal relevance = 0.75; // FIXME | ||||
94 | | ||||
95 | for (auto it = bookmarks.constBegin(), end = bookmarks.constEnd(); it != end; ++it) { | ||||
96 | const QJsonObject &bookmark = it->toObject(); | ||||
97 | | ||||
98 | const QString urlString = bookmark.value(QStringLiteral("url")).toString(); | ||||
99 | const QString title = bookmark.value(QStringLiteral("title")).toString(); | ||||
100 | // folders don't have a URL | ||||
101 | if (urlString.isEmpty()) { | ||||
102 | continue; | ||||
103 | } | ||||
104 | | ||||
105 | RemoteMatch match; | ||||
106 | match.id = urlString; | ||||
107 | match.text = title; | ||||
108 | // TODO bookmarks API doesn't expose bookmarks, at least show browser icon instead? | ||||
109 | match.iconName = QStringLiteral("text-html"); | ||||
110 | match.relevance = relevance; | ||||
111 | match.type = Plasma::QueryMatch::PossibleMatch; // TODO when's an exact match? | ||||
112 | match.properties = QVariantMap{ | ||||
113 | {QStringLiteral("urls"), QStringList(urlString)}, | ||||
114 | {QStringLiteral("subtext"), QUrl(urlString).toDisplayString()} | ||||
115 | }; | ||||
116 | | ||||
117 | relevance -= 0.05; | ||||
118 | | ||||
119 | // TODO should we limit the number of results? I often get like 70 of them | ||||
120 | matches << match; | ||||
121 | } | ||||
122 | | ||||
123 | QDBusConnection::sessionBus().send(m_lastRequest.createReply(QVariant::fromValue(matches))); | ||||
124 | m_lastRequest = QDBusMessage(); | ||||
125 | } | ||||
126 | | ||||
127 | void BookmarksRunnerPlugin::Run(const QString &id, const QString &actionId) | ||||
128 | { | ||||
129 | if (actionId.isEmpty()) { | ||||
130 | // didn't want to pull in KIOWidgets just for KRun | ||||
131 | QDesktopServices::openUrl(QUrl(id)); | ||||
132 | } | ||||
133 | } |