on 71269bb help sign in
unity8/plugins/Unity/scope.cpp
406 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_isActive(false)
44 , m_searchInProgress(false)
46 m_categories.reset(new Categories(this));
48 connect(this, &Scope::isActiveChanged, this, &Scope::scopeIsActiveChanged);
51QString Scope::id() const
53 return QString::fromStdString(m_unityScope->id());
56QString Scope::name() const
58 return QString::fromStdString(m_unityScope->name());
61QString Scope::iconHint() const
63 return QString::fromStdString(m_unityScope->icon_hint());
66QString Scope::description() const
68 return QString::fromStdString(m_unityScope->description());
71QString Scope::searchHint() const
73 return QString::fromStdString(m_unityScope->search_hint());
76bool Scope::searchInProgress() const
78 return m_searchInProgress;
81bool Scope::visible() const
83 return m_unityScope->visible();
86QString Scope::shortcut() const
88 return QString::fromStdString(m_unityScope->shortcut());
91bool Scope::connected() const
93 return m_unityScope->connected();
96Categories* Scope::categories() const
98 return m_categories.get();
101QString Scope::searchQuery() const
103 return m_searchQuery;
106QString Scope::noResultsHint() const
108 return m_noResultsHint;
111QString Scope::formFactor() const
113 return m_formFactor;
116bool Scope::isActive() const
118 return m_isActive;
121Filters* Scope::filters() const
123 return m_filters.get();
126void Scope::setSearchQuery(const QString& search_query)
128 /* Checking for m_searchQuery.isNull() which returns true only when the string
129 has never been set is necessary because when search_query is the empty
130 string ("") and m_searchQuery is the null string,
131 search_query != m_searchQuery is still true.
132 */
133 using namespace std::placeholders;
135 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
136 m_searchQuery = search_query;
137 m_cancellable.Renew();
138 m_unityScope->Search(search_query.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
139 Q_EMIT searchQueryChanged();
140 if (!m_searchInProgress) {
141 m_searchInProgress = true;
142 Q_EMIT searchInProgressChanged();
143 }
144 }
147void Scope::setNoResultsHint(const QString& hint) {
148 if (hint != m_noResultsHint) {
149 m_noResultsHint = hint;
150 Q_EMIT noResultsHintChanged();
151 }
154void Scope::setFormFactor(const QString& form_factor) {
155 if (form_factor != m_formFactor) {
156 m_formFactor = form_factor;
157 if (m_unityScope) {
158 m_unityScope->form_factor = m_formFactor.toStdString();
159 synchronizeStates(); // will trigger a re-search
160 }
161 Q_EMIT formFactorChanged();
162 }
165void Scope::setActive(const bool active) {
166 if (active != m_isActive) {
167 m_isActive = active;
168 Q_EMIT isActiveChanged(m_isActive);
169 }
172unity::dash::LocalResult Scope::createLocalResult(const QVariant &uri, const QVariant &icon_hint,
173 const QVariant &category, const QVariant &result_type,
174 const QVariant &mimetype, const QVariant &title,
175 const QVariant &comment, const QVariant &dnd_uri,
176 const QVariant &metadata)
178 unity::dash::LocalResult res;
179 res.uri = uri.toString().toStdString();
180 res.icon_hint = icon_hint.toString().toStdString();
181 res.category_index = category.toUInt();
182 res.result_type = result_type.toUInt();
183 res.mimetype = mimetype.toString().toStdString();
184 res.name = title.toString().toStdString();
185 res.comment = comment.toString().toStdString();
186 res.dnd_uri = dnd_uri.toString().toStdString();
187 res.hints = convertToHintsMap(metadata);
189 return res;
192// FIXME: Change to use row index.
193void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
194 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
195 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
197 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
198 m_unityScope->Activate(res);
201// FIXME: Change to use row index.
202void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
203 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
204 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
206 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
207 m_previewCancellable.Renew();
209 // canned queries don't have previews, must be activated
210 if (uri.toString().startsWith("x-unity-no-preview-scopes-query://")) {
211 m_unityScope->Activate(res);
212 } else {
213 m_unityScope->Preview(res, nullptr, m_previewCancellable);
214 }
217void Scope::cancelActivation()
219 m_previewCancellable.Cancel();
222void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const& hints)
224 Q_EMIT activated();
225 // note: we will not get called on SHOW_PREVIEW, instead UnityCore will signal preview_ready.
226 switch (type)
227 {
228 case unity::dash::NOT_HANDLED:
229 fallbackActivate(QString::fromStdString(result.uri));
230 break;
231 case unity::dash::SHOW_DASH:
232 Q_EMIT showDash();
233 break;
234 case unity::dash::HIDE_DASH:
235 Q_EMIT hideDash();
236 break;
237 case unity::dash::GOTO_DASH_URI:
238 if (hints.find("goto-uri") != hints.end()) {
239 Q_EMIT gotoUri(QString::fromStdString(g_variant_get_string(hints.at("goto-uri"), nullptr)));
240 } else {
241 qWarning() << "Missing goto-uri hint for GOTO_DASH_URI activation reply";
242 }
243 break;
244 case unity::dash::PERFORM_SEARCH:
245 if (hints.find("query") != hints.end()) {
246 // note: this will emit searchQueryChanged, and shell will call setSearchQuery back with a new query,
247 // but it will get ignored.
248 setSearchQuery(QString::fromStdString(g_variant_get_string(hints.at("query"), nullptr)));
249 }
250 break;
251 default:
252 qWarning() << "Unhandled activation response:" << type;
253 }
256void Scope::onPreviewReady(unity::dash::LocalResult const& /* result */, unity::dash::Preview::Ptr const& preview)
258 Q_EMIT activated();
259 auto prv = Preview::newFromUnityPreview(preview);
260 // is this the best solution? QML may need to keep more than one preview instance around, so we can't own it.
261 // passing it by value is not possible.
262 QQmlEngine::setObjectOwnership(prv, QQmlEngine::JavaScriptOwnership);
263 Q_EMIT previewReady(prv);
266void Scope::fallbackActivate(const QString& uri)
268 /* Tries various methods to trigger a sensible action for the given 'uri'.
269 If it has no understanding of the given scheme it falls back on asking
270 Qt to open the uri.
271 */
272 QUrl url(uri);
273 if (url.scheme() == "file") {
274 /* Override the files place's default URI handler: we want the file
275 manager to handle opening folders, not the dash.
277 Ref: https://bugs.launchpad.net/upicek/+bug/689667
278 */
279 QDesktopServices::openUrl(url);
280 return;
281 }
282 if (url.scheme() == "application") {
283 // get the full path to the desktop file
284 QString path(url.path().isEmpty() ? url.authority() : url.path());
285 if (path.startsWith("/")) {
286 Q_EMIT activateApplication(path);
287 } else {
288 // TODO: use the new desktop file finder/parser when it's ready
289 gchar* full_path = nullptr;
290 GKeyFile* key_file = g_key_file_new();
291 QString apps_path = "applications/" + path;
292 if (g_key_file_load_from_data_dirs(key_file, apps_path.toLocal8Bit().constData(), &full_path,
293 G_KEY_FILE_NONE, nullptr)) {
294 path = full_path;
295 Q_EMIT activateApplication(path);
296 } else {
297 qWarning() << "Unable to activate " << path;
298 }
299 g_key_file_free(key_file);
300 g_free(full_path);
301 }
302 return;
303 }
305 qDebug() << "Trying to open" << uri;
307 /* Try our luck */
308 QDesktopServices::openUrl(uri); //url?
311void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
313 m_unityScope = scope;
315 m_categories->setUnityScope(m_unityScope);
316 m_filters.reset(new Filters(m_unityScope->filters, this));
318 m_unityScope->form_factor = m_formFactor.toStdString();
319 /* Property change signals */
320 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
321 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
322 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
323 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
324 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
325 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
326 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
327 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
328 m_unityScope->results_dirty.changed.connect(sigc::mem_fun(this, &Scope::resultsDirtyToggled));
330 /* FIXME: signal should be forwarded instead of calling the handler directly */
331 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
333 m_unityScope->preview_ready.connect(sigc::mem_fun(this, &Scope::onPreviewReady));
335 /* Synchronize local states with m_unityScope right now and whenever
336 m_unityScope becomes connected */
337 /* FIXME: should emit change notification signals for all properties */
338 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
339 synchronizeStates();
342unity::dash::Scope::Ptr Scope::unityScope() const
344 return m_unityScope;
347void Scope::synchronizeStates()
349 using namespace std::placeholders;
351 if (connected()) {
352 /* Forward local states to m_unityScope */
353 if (!m_searchQuery.isNull()) {
354 m_cancellable.Renew();
355 m_unityScope->Search(m_searchQuery.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
356 if (!m_searchInProgress) {
357 m_searchInProgress = true;
358 Q_EMIT searchInProgressChanged();
359 }
360 }
361 }
364void Scope::scopeIsActiveChanged()
366 if (!isActive() || !m_unityScope || !m_unityScope->results_dirty()) return;
368 // force new search
369 synchronizeStates();
372void Scope::resultsDirtyToggled(bool results_dirty)
374 if (!results_dirty || !isActive()) return;
376 // force new search
377 synchronizeStates();
380void Scope::onSearchFinished(std::string const& /* query */, unity::glib::HintsMap const& hints, unity::glib::Error const& err)
382 QString hint;
384 GError* error = const_cast<unity::glib::Error&>(err);
386 if (!err || !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
387 if (m_searchInProgress) {
388 m_searchInProgress = false;
389 Q_EMIT searchInProgressChanged();
390 }
391 } else {
392 // no need to check the results hint, we're still searching
393 return;
394 }
396 if (!m_unityScope->results()->count()) {
397 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
398 if (it != hints.end()) {
399 hint = QString::fromStdString(it->second.GetString());
400 } else {
401 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
402 }
403 }
405 setNoResultsHint(hint);