on bfec158 help sign in
unity8/plugins/Unity/scope.cpp
403 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 <QDebug>
32#include <QtGui/QDesktopServices>
33#include <QQmlEngine>
35#include <UnityCore/Variant.h>
36#include <UnityCore/GLibWrapper.h>
38#include <libintl.h>
39#include <glib.h>
41Scope::Scope(QObject *parent) : QObject(parent)
42 , m_formFactor("phone")
43 , m_searchInProgress(false)
45 m_categories.reset(new Categories(this));
47 connect(this, &Scope::isActiveChanged, this, &Scope::scopeIsActiveChanged);
50QString Scope::id() const
52 return QString::fromStdString(m_unityScope->id());
55QString Scope::name() const
57 return QString::fromStdString(m_unityScope->name());
60QString Scope::iconHint() const
62 return QString::fromStdString(m_unityScope->icon_hint());
65QString Scope::description() const
67 return QString::fromStdString(m_unityScope->description());
70QString Scope::searchHint() const
72 return QString::fromStdString(m_unityScope->search_hint());
75bool Scope::searchInProgress() const
77 return m_searchInProgress;
80bool Scope::visible() const
82 return m_unityScope->visible();
85QString Scope::shortcut() const
87 return QString::fromStdString(m_unityScope->shortcut());
90bool Scope::connected() const
92 return m_unityScope->connected();
95Categories* Scope::categories() const
97 return m_categories.get();
100QString Scope::searchQuery() const
102 return m_searchQuery;
105QString Scope::noResultsHint() const
107 return m_noResultsHint;
110QString Scope::formFactor() const
112 return m_formFactor;
115bool Scope::isActive() const
117 return m_isActive;
120Filters* Scope::filters() const
122 return m_filters.get();
125void Scope::setSearchQuery(const QString& search_query)
127 /* Checking for m_searchQuery.isNull() which returns true only when the string
128 has never been set is necessary because when search_query is the empty
129 string ("") and m_searchQuery is the null string,
130 search_query != m_searchQuery is still true.
131 */
132 using namespace std::placeholders;
134 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
135 m_searchQuery = search_query;
136 m_cancellable.Renew();
137 m_unityScope->Search(search_query.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
138 Q_EMIT searchQueryChanged();
139 if (!m_searchInProgress) {
140 m_searchInProgress = true;
141 Q_EMIT searchInProgressChanged();
142 }
143 }
146void Scope::setNoResultsHint(const QString& hint) {
147 if (hint != m_noResultsHint) {
148 m_noResultsHint = hint;
149 Q_EMIT noResultsHintChanged();
150 }
153void Scope::setFormFactor(const QString& form_factor) {
154 if (form_factor != m_formFactor) {
155 m_formFactor = form_factor;
156 if (m_unityScope) {
157 m_unityScope->form_factor = m_formFactor.toStdString();
158 synchronizeStates(); // will trigger a re-search
159 }
160 Q_EMIT formFactorChanged();
161 }
164void Scope::setActive(const bool active) {
165 if (active != m_isActive) {
166 m_isActive = active;
167 Q_EMIT isActiveChanged(m_isActive);
168 }
171unity::dash::LocalResult Scope::createLocalResult(const QVariant &uri, const QVariant &icon_hint,
172 const QVariant &category, const QVariant &result_type,
173 const QVariant &mimetype, const QVariant &title,
174 const QVariant &comment, const QVariant &dnd_uri,
175 const QVariant &metadata)
177 unity::dash::LocalResult res;
178 res.uri = uri.toString().toStdString();
179 res.icon_hint = icon_hint.toString().toStdString();
180 res.category_index = category.toUInt();
181 res.result_type = result_type.toUInt();
182 res.mimetype = mimetype.toString().toStdString();
183 res.name = title.toString().toStdString();
184 res.comment = comment.toString().toStdString();
185 res.dnd_uri = dnd_uri.toString().toStdString();
186 res.hints = convertToHintsMap(metadata);
188 return res;
191// FIXME: Change to use row index.
192void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
193 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
194 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
196 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
197 m_unityScope->Activate(res);
200// FIXME: Change to use row index.
201void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
202 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
203 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
205 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
206 m_previewCancellable.Renew();
208 // canned queries don't have previews, must be activated
209 if (uri.toString().startsWith("x-unity-no-preview-scopes-query://")) {
210 m_unityScope->Activate(res);
211 } else {
212 m_unityScope->Preview(res, nullptr, m_previewCancellable);
213 }
216void Scope::cancelActivation()
218 m_previewCancellable.Cancel();
221void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const& hints)
223 // note: we will not get called on SHOW_PREVIEW, instead UnityCore will signal preview_ready.
224 switch (type)
225 {
226 case unity::dash::NOT_HANDLED:
227 fallbackActivate(QString::fromStdString(result.uri));
228 break;
229 case unity::dash::SHOW_DASH:
230 Q_EMIT showDash();
231 break;
232 case unity::dash::HIDE_DASH:
233 Q_EMIT hideDash();
234 break;
235 case unity::dash::GOTO_DASH_URI:
236 if (hints.find("goto-uri") != hints.end()) {
237 Q_EMIT gotoUri(QString::fromStdString(g_variant_get_string(hints.at("goto-uri"), nullptr)));
238 } else {
239 qWarning() << "Missing goto-uri hint for GOTO_DASH_URI activation reply";
240 }
241 break;
242 case unity::dash::PERFORM_SEARCH:
243 if (hints.find("query") != hints.end()) {
244 // note: this will emit searchQueryChanged, and shell will call setSearchQuery back with a new query,
245 // but it will get ignored.
246 setSearchQuery(QString::fromStdString(g_variant_get_string(hints.at("query"), nullptr)));
247 }
248 break;
249 default:
250 qWarning() << "Unhandled activation response:" << type;
251 }
254void Scope::onPreviewReady(unity::dash::LocalResult const& /* result */, unity::dash::Preview::Ptr const& preview)
256 auto prv = Preview::newFromUnityPreview(preview);
257 // is this the best solution? QML may need to keep more than one preview instance around, so we can't own it.
258 // passing it by value is not possible.
259 QQmlEngine::setObjectOwnership(prv, QQmlEngine::JavaScriptOwnership);
260 Q_EMIT previewReady(prv);
263void Scope::fallbackActivate(const QString& uri)
265 /* Tries various methods to trigger a sensible action for the given 'uri'.
266 If it has no understanding of the given scheme it falls back on asking
267 Qt to open the uri.
268 */
269 QUrl url(uri);
270 if (url.scheme() == "file") {
271 /* Override the files place's default URI handler: we want the file
272 manager to handle opening folders, not the dash.
274 Ref: https://bugs.launchpad.net/upicek/+bug/689667
275 */
276 QDesktopServices::openUrl(url);
277 return;
278 }
279 if (url.scheme() == "application") {
280 // get the full path to the desktop file
281 QString path(url.path().isEmpty() ? url.authority() : url.path());
282 if (path.startsWith("/")) {
283 Q_EMIT activateApplication(path);
284 } else {
285 // TODO: use the new desktop file finder/parser when it's ready
286 gchar* full_path = nullptr;
287 GKeyFile* key_file = g_key_file_new();
288 QString apps_path = "applications/" + path;
289 if (g_key_file_load_from_data_dirs(key_file, apps_path.toLocal8Bit().constData(), &full_path,
290 G_KEY_FILE_NONE, nullptr)) {
291 path = full_path;
292 Q_EMIT activateApplication(path);
293 } else {
294 qWarning() << "Unable to activate " << path;
295 }
296 g_key_file_free(key_file);
297 g_free(full_path);
298 }
299 return;
300 }
302 qDebug() << "Trying to open" << uri;
304 /* Try our luck */
305 QDesktopServices::openUrl(uri); //url?
308void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
310 m_unityScope = scope;
312 m_categories->setUnityScope(m_unityScope);
313 m_filters.reset(new Filters(m_unityScope->filters, this));
315 m_unityScope->form_factor = m_formFactor.toStdString();
316 /* Property change signals */
317 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
318 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
319 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
320 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
321 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
322 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
323 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
324 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
325 m_unityScope->results_dirty.changed.connect(sigc::mem_fun(this, &Scope::resultsDirtyToggled));
327 /* FIXME: signal should be forwarded instead of calling the handler directly */
328 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
330 m_unityScope->preview_ready.connect(sigc::mem_fun(this, &Scope::onPreviewReady));
332 /* Synchronize local states with m_unityScope right now and whenever
333 m_unityScope becomes connected */
334 /* FIXME: should emit change notification signals for all properties */
335 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
336 synchronizeStates();
339unity::dash::Scope::Ptr Scope::unityScope() const
341 return m_unityScope;
344void Scope::synchronizeStates()
346 using namespace std::placeholders;
348 if (connected()) {
349 /* Forward local states to m_unityScope */
350 if (!m_searchQuery.isNull()) {
351 m_cancellable.Renew();
352 m_unityScope->Search(m_searchQuery.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
353 if (!m_searchInProgress) {
354 m_searchInProgress = true;
355 Q_EMIT searchInProgressChanged();
356 }
357 }
358 }
361void Scope::scopeIsActiveChanged()
363 if (!isActive() || !m_unityScope || !m_unityScope->results_dirty()) return;
365 // force new search
366 synchronizeStates();
369void Scope::resultsDirtyToggled(bool results_dirty)
371 if (!results_dirty || !isActive()) return;
373 // force new search
374 synchronizeStates();
377void Scope::onSearchFinished(std::string const& /* query */, unity::glib::HintsMap const& hints, unity::glib::Error const& err)
379 QString hint;
381 GError* error = const_cast<unity::glib::Error&>(err);
383 if (!err || !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
384 if (m_searchInProgress) {
385 m_searchInProgress = false;
386 Q_EMIT searchInProgressChanged();
387 }
388 } else {
389 // no need to check the results hint, we're still searching
390 return;
391 }
393 if (!m_unityScope->results()->count()) {
394 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
395 if (it != hints.end()) {
396 hint = QString::fromStdString(it->second.GetString());
397 } else {
398 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
399 }
400 }
402 setNoResultsHint(hint);