on 87faa27 help sign in
unity8/plugins/Unity/scope.cpp
270 lines 8.8 kB history raw
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// Self
21#include "scope.h"
22#include "categories.h"
24// Qt
25#include <QUrl>
26#include <QDebug>
27#include <QtGui/QDesktopServices>
29#include <libintl.h>
31Scope::Scope(QObject *parent) :
32 QObject(parent)
34 m_results = new DeeListModel(this);
35 m_categories = new Categories(this);
37 m_categories->setResultModel(m_results);
40QString Scope::id() const
42 return QString::fromStdString(m_unityScope->id());
45QString Scope::name() const
47 return QString::fromStdString(m_unityScope->name());
50QString Scope::iconHint() const
52 return QString::fromStdString(m_unityScope->icon_hint());
55QString Scope::description() const
57 return QString::fromStdString(m_unityScope->description());
60QString Scope::searchHint() const
62 return QString::fromStdString(m_unityScope->search_hint());
65bool Scope::visible() const
67 return m_unityScope->visible();
70QString Scope::shortcut() const
72 return QString::fromStdString(m_unityScope->shortcut());
75bool Scope::connected() const
77 return m_unityScope->connected();
80DeeListModel* Scope::results() const
82 return m_results;
85Categories* Scope::categories() const
87 return m_categories;
90QString Scope::searchQuery() const
92 return m_searchQuery;
95QString Scope::noResultsHint() const
97 return m_noResultsHint;
100void Scope::setSearchQuery(const QString& search_query)
102 /* Checking for m_searchQuery.isNull() which returns true only when the string
103 has never been set is necessary because when search_query is the empty
104 string ("") and m_searchQuery is the null string,
105 search_query != m_searchQuery is still true.
106 */
107 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
108 m_searchQuery = search_query;
109 m_unityScope->Search(search_query.toStdString(), sigc::mem_fun(this, &Scope::searchFinished));
110 Q_EMIT searchQueryChanged();
111 }
114void Scope::setNoResultsHint(const QString& hint) {
115 if (hint != m_noResultsHint) {
116 m_noResultsHint = hint;
117 Q_EMIT noResultsHintChanged();
118 }
121void Scope::activate(const QString& uri)
123// m_unityScope->Activate(QByteArray::fromPercentEncoding(uri.toUtf8()).constData()); TODO needs model row
126void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const&)
128 if (type == unity::dash::NOT_HANDLED) {
129 fallbackActivate(QString::fromStdString(result.uri));
130 }
133void Scope::fallbackActivate(const QString& uri)
135 /* FIXME: stripping all content before the first column because for some
136 reason the lenses give uri with junk content at their beginning.
137 */
138 QString tweakedUri = uri;
139 int firstColumnAt = tweakedUri.indexOf(":");
140 tweakedUri.remove(0, firstColumnAt+1);
142 /* Tries various methods to trigger a sensible action for the given 'uri'.
143 If it has no understanding of the given scheme it falls back on asking
144 Qt to open the uri.
145 */
146 QUrl url(tweakedUri);
147 if (url.scheme() == "file") {
148 /* Override the files place's default URI handler: we want the file
149 manager to handle opening folders, not the dash.
151 Ref: https://bugs.launchpad.net/upicek/+bug/689667
152 */
153 QDesktopServices::openUrl(url);
154 return;
155 }
156 if (url.scheme() == "application") {
157 // TODO: implement application handling
158 return;
159 }
161 qWarning() << "FIXME: Possibly no handler for scheme: " << url.scheme();
162 qWarning() << "Trying to open" << tweakedUri;
163 /* Try our luck */
164 QDesktopServices::openUrl(url);
167void Scope::setUnityScope(const unity::dash::Scope::Ptr& scope)
169 m_unityScope = scope;
171 if (QString::fromStdString(m_unityScope->results()->swarm_name) == QString(":local")) {
172 m_results->setModel(m_unityScope->results()->model());
173 } else {
174 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
175 }
177 if (QString::fromStdString(m_unityScope->categories()->swarm_name) == QString(":local")) {
178 m_categories->setModel(m_unityScope->categories()->model());
179 } else {
180 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
181 }
183 /* Property change signals */
184 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
185 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
186 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
187 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
188 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
189 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
190 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
191 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
192 m_unityScope->results.changed.connect(sigc::mem_fun(this, &Scope::onResultsChanged));
193 m_unityScope->results()->swarm_name.changed.connect(sigc::mem_fun(this, &Scope::onResultsSwarmNameChanged));
194 m_unityScope->results()->model.changed.connect(sigc::mem_fun(this, &Scope::onResultsModelChanged));
195 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesModelChanged));
196 m_unityScope->categories.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesChanged));
197 m_unityScope->categories()->swarm_name.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesSwarmNameChanged));
198 /* Signals forwarding */
199 connect(this, SIGNAL(searchFinished(const std::string &, unity::glib::HintsMap const &, unity::glib::Error const &)), SLOT(onSearchFinished(const std::string &, unity::glib::HintsMap const &)));
201 /* FIXME: signal should be forwarded instead of calling the handler directly */
202 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
204 /* Synchronize local states with m_unityScope right now and whenever
205 m_unityScope becomes connected */
206 /* FIXME: should emit change notification signals for all properties */
207 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
208 synchronizeStates();
211unity::dash::Scope::Ptr Scope::unityScope() const
213 return m_unityScope;
216void Scope::synchronizeStates()
218 if (connected()) {
219 /* Forward local states to m_unityScope */
220 if (!m_searchQuery.isNull()) {
221 m_unityScope->Search(m_searchQuery.toStdString());
222 }
223 }
226void Scope::onResultsSwarmNameChanged(const std::string& /* swarm_name */)
228 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
231void Scope::onResultsChanged(const unity::dash::Results::Ptr& /* results */)
233 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
236void Scope::onResultsModelChanged(unity::glib::Object<DeeModel> model)
238 m_results->setModel(m_unityScope->results()->model());
241void Scope::onCategoriesSwarmNameChanged(const std::string& /* swarm_name */)
243 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
246void Scope::onCategoriesChanged(const unity::dash::Categories::Ptr& /* categories */)
248 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
251void Scope::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
253 m_categories->setModel(model);
256void Scope::onSearchFinished(const std::string &query, unity::glib::HintsMap const &hints)
258 QString hint;
260 if (!m_unityScope->results()->count()) {
261 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
262 if (it != hints.end()) {
263 hint = QString::fromStdString(it->second.GetString());
264 } else {
265 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
266 }
267 }
269 setNoResultsHint(hint);