on e12ee10 help sign in
unity8/plugins/Unity/scope.cpp
287 lines 9.3 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;
90Scope::ViewType Scope::viewType() const
92 return (Scope::ViewType) m_unityScope->view_type();
95QString Scope::searchQuery() const
97 return m_searchQuery;
100QString Scope::noResultsHint() const
102 return m_noResultsHint;
105void Scope::setViewType(const Scope::ViewType& viewType)
107 m_unityScope->view_type = (unity::dash::ScopeViewType) viewType;
110void Scope::setSearchQuery(const QString& search_query)
112 /* Checking for m_searchQuery.isNull() which returns true only when the string
113 has never been set is necessary because when search_query is the empty
114 string ("") and m_searchQuery is the null string,
115 search_query != m_searchQuery is still true.
116 */
117 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
118 m_searchQuery = search_query;
119 m_unityScope->Search(search_query.toStdString(), sigc::mem_fun(this, &Scope::searchFinished));
120 Q_EMIT searchQueryChanged();
121 }
124void Scope::setNoResultsHint(const QString& hint) {
125 if (hint != m_noResultsHint) {
126 m_noResultsHint = hint;
127 Q_EMIT noResultsHintChanged();
128 }
131void Scope::activate(const QString& uri)
133// m_unityScope->Activate(QByteArray::fromPercentEncoding(uri.toUtf8()).constData()); TODO needs model row
136void Scope::onActivated(unity::dash::LocalResult const& result, unity::dash::ScopeHandledType type, unity::glib::HintsMap const&)
138 if (type == unity::dash::NOT_HANDLED) {
139 fallbackActivate(QString::fromStdString(result.uri));
140 }
143void Scope::fallbackActivate(const QString& uri)
145 /* FIXME: stripping all content before the first column because for some
146 reason the lenses give uri with junk content at their beginning.
147 */
148 QString tweakedUri = uri;
149 int firstColumnAt = tweakedUri.indexOf(":");
150 tweakedUri.remove(0, firstColumnAt+1);
152 /* Tries various methods to trigger a sensible action for the given 'uri'.
153 If it has no understanding of the given scheme it falls back on asking
154 Qt to open the uri.
155 */
156 QUrl url(tweakedUri);
157 if (url.scheme() == "file") {
158 /* Override the files place's default URI handler: we want the file
159 manager to handle opening folders, not the dash.
161 Ref: https://bugs.launchpad.net/upicek/+bug/689667
162 */
163 QDesktopServices::openUrl(url);
164 return;
165 }
166 if (url.scheme() == "application") {
167 // TODO: implement application handling
168 return;
169 }
171 qWarning() << "FIXME: Possibly no handler for scheme: " << url.scheme();
172 qWarning() << "Trying to open" << tweakedUri;
173 /* Try our luck */
174 QDesktopServices::openUrl(url);
177void Scope::setUnityScope(const unity::dash::Scope::Ptr& lens)
179 m_unityScope = lens;
181 if (QString::fromStdString(m_unityScope->results()->swarm_name) == QString(":local")) {
182 m_results->setModel(m_unityScope->results()->model());
183 } else {
184 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
185 }
187 if (QString::fromStdString(m_unityScope->categories()->swarm_name) == QString(":local")) {
188 m_categories->setModel(m_unityScope->categories()->model());
189 } else {
190 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
191 }
193 /* Property change signals */
194 m_unityScope->id.changed.connect(sigc::mem_fun(this, &Scope::idChanged));
195 m_unityScope->name.changed.connect(sigc::mem_fun(this, &Scope::nameChanged));
196 m_unityScope->icon_hint.changed.connect(sigc::mem_fun(this, &Scope::iconHintChanged));
197 m_unityScope->description.changed.connect(sigc::mem_fun(this, &Scope::descriptionChanged));
198 m_unityScope->search_hint.changed.connect(sigc::mem_fun(this, &Scope::searchHintChanged));
199 m_unityScope->visible.changed.connect(sigc::mem_fun(this, &Scope::visibleChanged));
200 m_unityScope->shortcut.changed.connect(sigc::mem_fun(this, &Scope::shortcutChanged));
201 m_unityScope->connected.changed.connect(sigc::mem_fun(this, &Scope::connectedChanged));
202 m_unityScope->results.changed.connect(sigc::mem_fun(this, &Scope::onResultsChanged));
203 m_unityScope->results()->swarm_name.changed.connect(sigc::mem_fun(this, &Scope::onResultsSwarmNameChanged));
204 m_unityScope->results()->model.changed.connect(sigc::mem_fun(this, &Scope::onResultsModelChanged));
205 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesModelChanged));
206 m_unityScope->categories.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesChanged));
207 m_unityScope->categories()->swarm_name.changed.connect(sigc::mem_fun(this, &Scope::onCategoriesSwarmNameChanged));
208 m_unityScope->view_type.changed.connect(sigc::mem_fun(this, &Scope::onViewTypeChanged));
210 /* Signals forwarding */
211 connect(this, SIGNAL(searchFinished(const std::string &, unity::glib::HintsMap const &, unity::glib::Error const &)), SLOT(onSearchFinished(const std::string &, unity::glib::HintsMap const &)));
213 /* FIXME: signal should be forwarded instead of calling the handler directly */
214 m_unityScope->activated.connect(sigc::mem_fun(this, &Scope::onActivated));
216 /* Synchronize local states with m_unityScope right now and whenever
217 m_unityScope becomes connected */
218 /* FIXME: should emit change notification signals for all properties */
219 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
220 synchronizeStates();
223unity::dash::Scope::Ptr Scope::unityScope() const
225 return m_unityScope;
228void Scope::synchronizeStates()
230 if (connected()) {
231 /* Forward local states to m_unityScope */
232 if (!m_searchQuery.isNull()) {
233 m_unityScope->Search(m_searchQuery.toStdString());
234 }
235 }
238void Scope::onResultsSwarmNameChanged(const std::string& /* swarm_name */)
240 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
243void Scope::onResultsChanged(const unity::dash::Results::Ptr& /* results */)
245 m_results->setName(QString::fromStdString(m_unityScope->results()->swarm_name));
248void Scope::onResultsModelChanged(unity::glib::Object<DeeModel> model)
250 m_results->setModel(m_unityScope->results()->model());
253void Scope::onCategoriesSwarmNameChanged(const std::string& /* swarm_name */)
255 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
258void Scope::onCategoriesChanged(const unity::dash::Categories::Ptr& /* categories */)
260 m_categories->setName(QString::fromStdString(m_unityScope->categories()->swarm_name));
263void Scope::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
265 m_categories->setModel(model);
268void Scope::onViewTypeChanged(unity::dash::ScopeViewType viewType)
270 Q_EMIT viewTypeChanged( (Scope::ViewType) viewType);
273void Scope::onSearchFinished(const std::string &query, unity::glib::HintsMap const &hints)
275 QString hint;
277 if (!m_unityScope->results()->count()) {
278 unity::glib::HintsMap::const_iterator it = hints.find("no-results-hint");
279 if (it != hints.end()) {
280 hint = QString::fromStdString(it->second.GetString());
281 } else {
282 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
283 }
284 }
286 setNoResultsHint(hint);