2 * Copyright (C) 2011 Canonical, Ltd.
5 * Florian Boucault <florian.boucault@canonical.com>
6 * Michal Hruby <michal.hruby@canonical.com>
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.
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.
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/>.
25#include "categories.h"
27#include "variantutils.h"
32#include <QtGui/QDesktopServices>
35#include <UnityCore/Variant.h>
40Scope::Scope(QObject *parent) : QObject(parent)
41 , m_formFactor("phone")
42 , m_previewCancellable(nullptr)
44 m_categories.reset(new Categories(this));
49 if (m_previewCancellable) {
50 g_object_unref(m_previewCancellable);
54QString Scope::id() const
56 return QString::fromStdString(m_unityScope->id());
59QString Scope::name() const
61 return QString::fromStdString(m_unityScope->name());
64QString Scope::iconHint() const
66 return QString::fromStdString(m_unityScope->icon_hint());
69QString Scope::description() const
71 return QString::fromStdString(m_unityScope->description());
74QString Scope::searchHint() const
76 return QString::fromStdString(m_unityScope->search_hint());
79bool Scope::visible() const
81 return m_unityScope->visible();
84QString Scope::shortcut() const
86 return QString::fromStdString(m_unityScope->shortcut());
89bool Scope::connected() const
91 return m_unityScope->connected();
94Categories* Scope::categories() const
96 return m_categories.get();
99QString Scope::searchQuery() const
101 return m_searchQuery;
104QString Scope::noResultsHint() const
106 return m_noResultsHint;
109QString Scope::formFactor() const
114Filters* Scope::filters() const
116 return m_filters.get();
119void Scope::setSearchQuery(const QString& search_query)
121 /* Checking for m_searchQuery.isNull() which returns true only when the string
122 has never been set is necessary because when search_query is the empty
123 string ("") and m_searchQuery is the null string,
124 search_query != m_searchQuery is still true.
126 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
127 m_searchQuery = search_query;
128 m_unityScope->Search(search_query.toStdString(), sigc::mem_fun(this, &Scope::searchFinished));
129 Q_EMIT searchQueryChanged();
133void Scope::setNoResultsHint(const QString& hint) {
134 if (hint != m_noResultsHint) {
135 m_noResultsHint = hint;
136 Q_EMIT noResultsHintChanged();
140void Scope::setFormFactor(const QString& form_factor) {
141 if (form_factor != m_formFactor) {
142 m_formFactor = form_factor;
144 m_unityScope->form_factor = m_formFactor.toStdString();
145 synchronizeStates(); // will trigger a re-search
147 Q_EMIT formFactorChanged();
151unity::dash::LocalResult Scope::createLocalResult(const QVariant &uri, const QVariant &icon_hint,
152 const QVariant &category, const QVariant &result_type,
153 const QVariant &mimetype, const QVariant &title,
154 const QVariant &comment, const QVariant &dnd_uri,
155 const QVariant &metadata)
157 unity::dash::LocalResult res;
158 res.uri = uri.toString().toStdString();
159 res.icon_hint = icon_hint.toString().toStdString();
160 res.category_index = category.toUInt();
161 res.result_type = result_type.toUInt();
162 res.mimetype = mimetype.toString().toStdString();
163 res.name = title.toString().toStdString();
164 res.comment = comment.toString().toStdString();
165 res.dnd_uri = dnd_uri.toString().toStdString();
166 res.hints = convertToHintsMap(metadata);
171// FIXME: Change to use row index.
172void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
173 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
174 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
176 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
177 m_unityScope->Activate(res);
180// FIXME: Change to use row index.
181void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
182 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
183 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
185 auto res = createLocalResult(uri, icon_hint, category, result_type, mimetype, title, comment, dnd_uri, metadata);
187 if (m_previewCancellable) {
188 g_cancellable_cancel(m_previewCancellable);
189 g_object_unref(m_previewCancellable);
191 m_previewCancellable = g_cancellable_new();
192 m_unityScope->Preview(res, nullptr, m_previewCancellable);
195void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const& hints)
197 // note: we will not get called on SHOW_PREVIEW, instead UnityCore will signal preview_ready.
200 case unity::dash::NOT_HANDLED:
201 fallbackActivate(QString::fromStdString(result.uri));
203 case unity::dash::SHOW_DASH:
206 case unity::dash::HIDE_DASH:
209 case unity::dash::GOTO_DASH_URI:
210 if (hints.find("goto-uri") != hints.end()) {
211 Q_EMIT gotoUri(QString::fromStdString(g_variant_get_string(hints.at("goto-uri"), nullptr)));
213 qWarning() << "Missing goto-uri hint for GOTO_DASH_URI activation reply";
217 qWarning() << "Unhandled activation response:" << type;
221void Scope::onPreviewReady(unity::dash::LocalResult const& /* result */, unity::dash::Preview::Ptr const& preview)
223 if (m_previewCancellable) {
224 g_object_unref(m_previewCancellable);
225 m_previewCancellable = nullptr;
227 auto prv = Preview::newFromUnityPreview(preview);
228 // is this the best solution? QML may need to keep more than one preview instance around, so we can't own it.
229 // passing it by value is not possible.
230 QQmlEngine::setObjectOwnership(prv, QQmlEngine::JavaScriptOwnership);
231 Q_EMIT previewReady(prv);
234void Scope::fallbackActivate(const QString& uri)
236 /* Tries various methods to trigger a sensible action for the given 'uri'.
237 If it has no understanding of the given scheme it falls back on asking
241 if (url.scheme() == "file") {
242 /* Override the files place's default URI handler: we want the file
243 manager to handle opening folders, not the dash.
245 Ref: https://bugs.launchpad.net/upicek/+bug/689667
247 QDesktopServices::openUrl(url);
250 if (url.scheme() == "application") {
251 // get the full path to the desktop file
252 QString path(url.path().isEmpty() ? url.authority() : url.path());
253 if (path.startsWith("/")) {
254 Q_EMIT activateApplication(path);
256 // TODO: use the new desktop file finder/parser when it's ready
257 gchar* full_path = nullptr;
258 GKeyFile* key_file = g_key_file_new();
259 QString apps_path = "applications/" + path;
260 if (g_key_file_load_from_data_dirs(key_file, apps_path.toLocal8Bit().constData(), &full_path,
261 G_KEY_FILE_NONE, nullptr)) {
263 Q_EMIT activateApplication(path);
265 qWarning() << "Unable to activate " << path;
267 g_key_file_free(key_file);
273 qDebug() << "Trying to open" << uri;
276 QDesktopServices::openUrl(uri); //url?
279void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
281 m_unityScope = scope;
283 m_categories->setUnityScope(m_unityScope);
284 m_filters.reset(new Filters(m_unityScope->filters, this));
286 m_unityScope->form_factor = m_formFactor.toStdString();
287 /* Property change signals */
288 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
289 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
290 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
291 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
292 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
293 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
294 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
295 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
296 /* Signals forwarding */
297 connect(this, SIGNAL(searchFinished(const std::string &, unity::glib::HintsMap const &, unity::glib::Error const &)), SLOT(onSearchFinished(const std::string &, unity::glib::HintsMap const &)));
299 /* FIXME: signal should be forwarded instead of calling the handler directly */
300 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
302 m_unityScope->preview_ready.connect(sigc::mem_fun(this, &Scope::onPreviewReady));
304 /* Synchronize local states with m_unityScope right now and whenever
305 m_unityScope becomes connected */
306 /* FIXME: should emit change notification signals for all properties */
307 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
311unity::dash::Scope::Ptr Scope::unityScope() const
316void Scope::synchronizeStates()
319 /* Forward local states to m_unityScope */
320 if (!m_searchQuery.isNull()) {
321 m_unityScope->Search(m_searchQuery.toStdString());
326void Scope::onSearchFinished(const std::string& /* query */, unity::glib::HintsMap const &hints)
330 if (!m_unityScope->results()->count()) {
331 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
332 if (it != hints.end()) {
333 hint = QString::fromStdString(it->second.GetString());
335 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
339 setNoResultsHint(hint);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list