on 8301d36 help sign in
unity8/plugins/Unity/scope.cpp
404 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 // note: we will not get called on SHOW_PREVIEW, instead UnityCore will signal preview_ready.
225 switch (type)
226 {
227 case unity::dash::NOT_HANDLED:
228 fallbackActivate(QString::fromStdString(result.uri));
229 break;
230 case unity::dash::SHOW_DASH:
231 Q_EMIT showDash();
232 break;
233 case unity::dash::HIDE_DASH:
234 Q_EMIT hideDash();
235 break;
236 case unity::dash::GOTO_DASH_URI:
237 if (hints.find("goto-uri") != hints.end()) {
238 Q_EMIT gotoUri(QString::fromStdString(g_variant_get_string(hints.at("goto-uri"), nullptr)));
239 } else {
240 qWarning() << "Missing goto-uri hint for GOTO_DASH_URI activation reply";
241 }
242 break;
243 case unity::dash::PERFORM_SEARCH:
244 if (hints.find("query") != hints.end()) {
245 // note: this will emit searchQueryChanged, and shell will call setSearchQuery back with a new query,
246 // but it will get ignored.
247 setSearchQuery(QString::fromStdString(g_variant_get_string(hints.at("query"), nullptr)));
248 }
249 break;
250 default:
251 qWarning() << "Unhandled activation response:" << type;
252 }
255void Scope::onPreviewReady(unity::dash::LocalResult const& /* result */, unity::dash::Preview::Ptr const& preview)
257 auto prv = Preview::newFromUnityPreview(preview);
258 // is this the best solution? QML may need to keep more than one preview instance around, so we can't own it.
259 // passing it by value is not possible.
260 QQmlEngine::setObjectOwnership(prv, QQmlEngine::JavaScriptOwnership);
261 Q_EMIT previewReady(prv);
264void Scope::fallbackActivate(const QString& uri)
266 /* Tries various methods to trigger a sensible action for the given 'uri'.
267 If it has no understanding of the given scheme it falls back on asking
268 Qt to open the uri.
269 */
270 QUrl url(uri);
271 if (url.scheme() == "file") {
272 /* Override the files place's default URI handler: we want the file
273 manager to handle opening folders, not the dash.
275 Ref: https://bugs.launchpad.net/upicek/+bug/689667
276 */
277 QDesktopServices::openUrl(url);
278 return;
279 }
280 if (url.scheme() == "application") {
281 // get the full path to the desktop file
282 QString path(url.path().isEmpty() ? url.authority() : url.path());
283 if (path.startsWith("/")) {
284 Q_EMIT activateApplication(path);
285 } else {
286 // TODO: use the new desktop file finder/parser when it's ready
287 gchar* full_path = nullptr;
288 GKeyFile* key_file = g_key_file_new();
289 QString apps_path = "applications/" + path;
290 if (g_key_file_load_from_data_dirs(key_file, apps_path.toLocal8Bit().constData(), &full_path,
291 G_KEY_FILE_NONE, nullptr)) {
292 path = full_path;
293 Q_EMIT activateApplication(path);
294 } else {
295 qWarning() << "Unable to activate " << path;
296 }
297 g_key_file_free(key_file);
298 g_free(full_path);
299 }
300 return;
301 }
303 qDebug() << "Trying to open" << uri;
305 /* Try our luck */
306 QDesktopServices::openUrl(uri); //url?
309void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
311 m_unityScope = scope;
313 m_categories->setUnityScope(m_unityScope);
314 m_filters.reset(new Filters(m_unityScope->filters, this));
316 m_unityScope->form_factor = m_formFactor.toStdString();
317 /* Property change signals */
318 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
319 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
320 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
321 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
322 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
323 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
324 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
325 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
326 m_unityScope->results_dirty.changed.connect(sigc::mem_fun(this, &Scope::resultsDirtyToggled));
328 /* FIXME: signal should be forwarded instead of calling the handler directly */
329 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
331 m_unityScope->preview_ready.connect(sigc::mem_fun(this, &Scope::onPreviewReady));
333 /* Synchronize local states with m_unityScope right now and whenever
334 m_unityScope becomes connected */
335 /* FIXME: should emit change notification signals for all properties */
336 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
337 synchronizeStates();
340unity::dash::Scope::Ptr Scope::unityScope() const
342 return m_unityScope;
345void Scope::synchronizeStates()
347 using namespace std::placeholders;
349 if (connected()) {
350 /* Forward local states to m_unityScope */
351 if (!m_searchQuery.isNull()) {
352 m_cancellable.Renew();
353 m_unityScope->Search(m_searchQuery.toStdString(), std::bind(&Scope::onSearchFinished, this, _1, _2, _3), m_cancellable);
354 if (!m_searchInProgress) {
355 m_searchInProgress = true;
356 Q_EMIT searchInProgressChanged();
357 }
358 }
359 }
362void Scope::scopeIsActiveChanged()
364 if (!isActive() || !m_unityScope || !m_unityScope->results_dirty()) return;
366 // force new search
367 synchronizeStates();
370void Scope::resultsDirtyToggled(bool results_dirty)
372 if (!results_dirty || !isActive()) return;
374 // force new search
375 synchronizeStates();
378void Scope::onSearchFinished(std::string const& /* query */, unity::glib::HintsMap const& hints, unity::glib::Error const& err)
380 QString hint;
382 GError* error = const_cast<unity::glib::Error&>(err);
384 if (!err || !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
385 if (m_searchInProgress) {
386 m_searchInProgress = false;
387 Q_EMIT searchInProgressChanged();
388 }
389 } else {
390 // no need to check the results hint, we're still searching
391 return;
392 }
394 if (!m_unityScope->results()->count()) {
395 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
396 if (it != hints.end()) {
397 hint = QString::fromStdString(it->second.GetString());
398 } else {
399 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
400 }
401 }
403 setNoResultsHint(hint);