on 33eb6be help sign in
unity8/plugins/Unity/scope.cpp
411 lines 13 kB history raw
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 * Michal Hruby <michal.hruby@canonical.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 3.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
21// Self
22#include "scope.h"
24// local
25#include "categories.h"
26#include "preview.h"
27#include "variantutils.h"
29// Qt
30#include <QUrl>
31#include <QUrlQuery>
32#include <QDebug>
33#include <QtGui/QDesktopServices>
34#include <QQmlEngine>
36#include <UnityCore/Variant.h>
37#include <UnityCore/GLibWrapper.h>
39#include <libintl.h>
40#include <glib.h>
42Scope::Scope(QObject *parent) : QObject(parent)
43 , m_formFactor("phone")
44 , m_isActive(false)
45 , m_searchInProgress(false)
47 m_categories.reset(new Categories(this));
49 connect(this, &Scope::isActiveChanged, this, &Scope::scopeIsActiveChanged);
52QString Scope::id() const
54 return QString::fromStdString(m_unityScope->id());
57QString Scope::name() const
59 return QString::fromStdString(m_unityScope->name());
62QString Scope::iconHint() const
64 return QString::fromStdString(m_unityScope->icon_hint());
67QString Scope::description() const
69 return QString::fromStdString(m_unityScope->description());
72QString Scope::searchHint() const
74 return QString::fromStdString(m_unityScope->search_hint());
77bool Scope::searchInProgress() const
79 return m_searchInProgress;
82bool Scope::visible() const
84 return m_unityScope->visible();
87QString Scope::shortcut() const
89 return QString::fromStdString(m_unityScope->shortcut());
92bool Scope::connected() const
94 return m_unityScope->connected();
97Categories* Scope::categories() const
99 return m_categories.get();
102QString Scope::searchQuery() const
104 return m_searchQuery;
107QString Scope::noResultsHint() const
109 return m_noResultsHint;
112QString Scope::formFactor() const
114 return m_formFactor;
117bool Scope::isActive() const
119 return m_isActive;
122Filters* Scope::filters() const
124 return m_filters.get();
127void Scope::setSearchQuery(const QString& search_query)
129 /* Checking for m_searchQuery.isNull() which returns true only when the string
130 has never been set is necessary because when search_query is the empty
131 string ("") and m_searchQuery is the null string,
132 search_query != m_searchQuery is still true.
133 */
134 using namespace std::placeholders;
136 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
137 m_searchQuery = search_query;
138 m_cancellable.Renew();
139 m_unityScope->Search(search_query.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
140 Q_EMIT searchQueryChanged();
141 if (!m_searchInProgress) {
142 m_searchInProgress = true;
143 Q_EMIT searchInProgressChanged();
144 }
145 }
148void Scope::setNoResultsHint(const QString& hint) {
149 if (hint != m_noResultsHint) {
150 m_noResultsHint = hint;
151 Q_EMIT noResultsHintChanged();
152 }
155void Scope::setFormFactor(const QString& form_factor) {
156 if (form_factor != m_formFactor) {
157 m_formFactor = form_factor;
158 if (m_unityScope) {
159 m_unityScope->form_factor = m_formFactor.toStdString();
160 synchronizeStates(); // will trigger a re-search
161 }
162 Q_EMIT formFactorChanged();
163 }
166void Scope::setActive(const bool active) {
167 if (active != m_isActive) {
168 m_isActive = active;
169 Q_EMIT isActiveChanged(m_isActive);
170 }
173unity::dash::LocalResult Scope::createLocalResult(const QVariant &uri, const QVariant &icon_hint,
174 const QVariant &category, const QVariant &result_type,
175 const QVariant &mimetype, const QVariant &title,
176 const QVariant &comment, const QVariant &dnd_uri,
177 const QVariant &metadata)
179 unity::dash::LocalResult res;
180 res.uri = uri.toString().toStdString();
181 res.icon_hint = icon_hint.toString().toStdString();
182 res.category_index = category.toUInt();
183 res.result_type = result_type.toUInt();
184 res.mimetype = mimetype.toString().toStdString();
185 res.name = title.toString().toStdString();
186 res.comment = comment.toString().toStdString();
187 res.dnd_uri = dnd_uri.toString().toStdString();
188 res.hints = convertToHintsMap(metadata);
190 return res;
193// FIXME: Change to use row index.
194void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
195 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
196 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
198 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
199 m_unityScope->Activate(res);
202// FIXME: Change to use row index.
203void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
204 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
205 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
207 QVariant real_metadata(metadata);
208 // handle overridden results, since QML doesn't support defining maps
209 if (metadata.type() == QVariant::String) {
210 real_metadata = QVariant::fromValue(subscopeUriToMetadataHash(metadata.toString()));
211 }
213 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, real_metadata);
214 m_previewCancellable.Renew();
216 // canned queries don't have previews, must be activated
217 if (uri.toString().startsWith("x-unity-no-preview-scopes-query://")) {
218 m_unityScope->Activate(res);
219 } else {
220 m_unityScope->Preview(res, nullptr, m_previewCancellable);
221 }
224void Scope::cancelActivation()
226 m_previewCancellable.Cancel();
229void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const& hints)
231 // note: we will not get called on SHOW_PREVIEW, instead UnityCore will signal preview_ready.
232 switch (type)
233 {
234 case unity::dash::NOT_HANDLED:
235 fallbackActivate(QString::fromStdString(result.uri));
236 break;
237 case unity::dash::SHOW_DASH:
238 Q_EMIT showDash();
239 break;
240 case unity::dash::HIDE_DASH:
241 Q_EMIT hideDash();
242 break;
243 case unity::dash::GOTO_DASH_URI:
244 if (hints.find("goto-uri") != hints.end()) {
245 Q_EMIT gotoUri(QString::fromStdString(g_variant_get_string(hints.at("goto-uri"), nullptr)));
246 } else {
247 qWarning() << "Missing goto-uri hint for GOTO_DASH_URI activation reply";
248 }
249 break;
250 case unity::dash::PERFORM_SEARCH:
251 if (hints.find("query") != hints.end()) {
252 // note: this will emit searchQueryChanged, and shell will call setSearchQuery back with a new query,
253 // but it will get ignored.
254 setSearchQuery(QString::fromStdString(g_variant_get_string(hints.at("query"), nullptr)));
255 }
256 break;
257 default:
258 qWarning() << "Unhandled activation response:" << type;
259 }
262void Scope::onPreviewReady(unity::dash::LocalResult const& /* result */, unity::dash::Preview::Ptr const& preview)
264 auto prv = Preview::newFromUnityPreview(preview);
265 // is this the best solution? QML may need to keep more than one preview instance around, so we can't own it.
266 // passing it by value is not possible.
267 QQmlEngine::setObjectOwnership(prv, QQmlEngine::JavaScriptOwnership);
268 Q_EMIT previewReady(prv);
271void Scope::fallbackActivate(const QString& uri)
273 /* Tries various methods to trigger a sensible action for the given 'uri'.
274 If it has no understanding of the given scheme it falls back on asking
275 Qt to open the uri.
276 */
277 QUrl url(uri);
278 if (url.scheme() == "file") {
279 /* Override the files place's default URI handler: we want the file
280 manager to handle opening folders, not the dash.
282 Ref: https://bugs.launchpad.net/upicek/+bug/689667
283 */
284 QDesktopServices::openUrl(url);
285 return;
286 }
287 if (url.scheme() == "application") {
288 // get the full path to the desktop file
289 QString path(url.path().isEmpty() ? url.authority() : url.path());
290 if (path.startsWith("/")) {
291 Q_EMIT activateApplication(path);
292 } else {
293 // TODO: use the new desktop file finder/parser when it's ready
294 gchar* full_path = nullptr;
295 GKeyFile* key_file = g_key_file_new();
296 QString apps_path = "applications/" + path;
297 if (g_key_file_load_from_data_dirs(key_file, apps_path.toLocal8Bit().constData(), &full_path,
298 G_KEY_FILE_NONE, nullptr)) {
299 path = full_path;
300 Q_EMIT activateApplication(path);
301 } else {
302 qWarning() << "Unable to activate " << path;
303 }
304 g_key_file_free(key_file);
305 g_free(full_path);
306 }
307 return;
308 }
310 qDebug() << "Trying to open" << uri;
312 /* Try our luck */
313 QDesktopServices::openUrl(uri); //url?
316void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
318 m_unityScope = scope;
320 m_categories->setUnityScope(m_unityScope);
321 m_filters.reset(new Filters(m_unityScope->filters, this));
323 m_unityScope->form_factor = m_formFactor.toStdString();
324 /* Property change signals */
325 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
326 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
327 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
328 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
329 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
330 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
331 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
332 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
333 m_unityScope->results_dirty.changed.connect(sigc::mem_fun(this, &Scope::resultsDirtyToggled));
335 /* FIXME: signal should be forwarded instead of calling the handler directly */
336 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
338 m_unityScope->preview_ready.connect(sigc::mem_fun(this, &Scope::onPreviewReady));
340 /* Synchronize local states with m_unityScope right now and whenever
341 m_unityScope becomes connected */
342 /* FIXME: should emit change notification signals for all properties */
343 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
344 synchronizeStates();
347unity::dash::Scope::Ptr Scope::unityScope() const
349 return m_unityScope;
352void Scope::synchronizeStates()
354 using namespace std::placeholders;
356 if (connected()) {
357 /* Forward local states to m_unityScope */
358 if (!m_searchQuery.isNull()) {
359 m_cancellable.Renew();
360 m_unityScope->Search(m_searchQuery.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
361 if (!m_searchInProgress) {
362 m_searchInProgress = true;
363 Q_EMIT searchInProgressChanged();
364 }
365 }
366 }
369void Scope::scopeIsActiveChanged()
371 if (!isActive() || !m_unityScope || !m_unityScope->results_dirty()) return;
373 // force new search
374 synchronizeStates();
377void Scope::resultsDirtyToggled(bool results_dirty)
379 if (!results_dirty || !isActive()) return;
381 // force new search
382 synchronizeStates();
385void Scope::onSearchFinished(std::string const& /* query */, unity::glib::HintsMap const& hints, unity::glib::Error const& err)
387 QString hint;
389 GError* error = const_cast<unity::glib::Error&>(err);
391 if (!err || !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
392 if (m_searchInProgress) {
393 m_searchInProgress = false;
394 Q_EMIT searchInProgressChanged();
395 }
396 } else {
397 // no need to check the results hint, we're still searching
398 return;
399 }
401 if (!m_unityScope->results()->count()) {
402 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
403 if (it != hints.end()) {
404 hint = QString::fromStdString(it->second.GetString());
405 } else {
406 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
407 }
408 }
410 setNoResultsHint(hint);