on 32f07dd help sign in
unity8/plugins/Unity/lens.cpp
338 lines 11 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 "lens.h"
22#include "categories.h"
24// Qt
25#include <QUrl>
26#include <QDebug>
27#include <QtGui/QDesktopServices>
29#include <libintl.h>
31Lens::Lens(QObject *parent) :
32 QObject(parent)
34 m_results = new DeeListModel(this);
35 m_globalResults = new DeeListModel(this);
36 m_categories = new Categories(this);
38 m_categories->setResultModel(m_results);
39 m_categories->setGlobalResultModel(m_globalResults);
42QString Lens::id() const
44 return QString::fromStdString(m_unityLens->id());
47QString Lens::dbusName() const
49 return QString::fromStdString(m_unityLens->dbus_name());
52QString Lens::dbusPath() const
54 return QString::fromStdString(m_unityLens->dbus_path());
57QString Lens::name() const
59 return QString::fromStdString(m_unityLens->name());
62QString Lens::iconHint() const
64 return QString::fromStdString(m_unityLens->icon_hint());
67QString Lens::description() const
69 return QString::fromStdString(m_unityLens->description());
72QString Lens::searchHint() const
74 return QString::fromStdString(m_unityLens->search_hint());
77bool Lens::visible() const
79 return m_unityLens->visible();
82bool Lens::searchInGlobal() const
84 return m_unityLens->search_in_global();
87QString Lens::shortcut() const
89 return QString::fromStdString(m_unityLens->shortcut());
92bool Lens::connected() const
94 return m_unityLens->connected();
97DeeListModel* Lens::results() const
99 return m_results;
102DeeListModel* Lens::globalResults() const
104 return m_globalResults;
107Categories* Lens::categories() const
109 return m_categories;
112Lens::ViewType Lens::viewType() const
114 return (Lens::ViewType) m_unityLens->view_type();
117QString Lens::searchQuery() const
119 return m_searchQuery;
122QString Lens::globalSearchQuery() const
124 return m_globalSearchQuery;
127QString Lens::noResultsHint() const
129 return m_noResultsHint;
132void Lens::setViewType(const Lens::ViewType& viewType)
134 m_unityLens->view_type = (unity::dash::ViewType) viewType;
137void Lens::setSearchQuery(const QString& search_query)
139 /* Checking for m_searchQuery.isNull() which returns true only when the string
140 has never been set is necessary because when search_query is the empty
141 string ("") and m_searchQuery is the null string,
142 search_query != m_searchQuery is still true.
143 */
144 if (m_searchQuery.isNull() || search_query != m_searchQuery) {
145 m_searchQuery = search_query;
146 m_unityLens->Search(search_query.toStdString(), sigc::mem_fun(this, &Lens::searchFinished));
147 Q_EMIT searchQueryChanged();
148 }
151void Lens::setGlobalSearchQuery(const QString& search_query)
153 /* Checking for m_globalSearchQuery.isNull() which returns true only when the string
154 has never been set is necessary because when search_query is the empty
155 string ("") and m_globalSearchQuery is the null string,
156 search_query != m_globalSearchQuery is still true.
157 */
158 if (m_globalSearchQuery.isNull() || search_query != m_globalSearchQuery) {
159 m_globalSearchQuery = search_query;
160 m_unityLens->GlobalSearch(search_query.toStdString(), sigc::mem_fun(this, &Lens::globalSearchFinished));
161 Q_EMIT globalSearchQueryChanged();
162 }
165void Lens::setNoResultsHint(const QString& hint) {
166 if (hint != m_noResultsHint) {
167 m_noResultsHint = hint;
168 Q_EMIT noResultsHintChanged();
169 }
172void Lens::activate(const QString& uri)
174 m_unityLens->Activate(QByteArray::fromPercentEncoding(uri.toUtf8()).constData());
177void Lens::onActivated(std::string const& uri, unity::dash::HandledType type, unity::dash::Lens::Hints const&)
179 if (type == unity::dash::NOT_HANDLED) {
180 fallbackActivate(QString::fromStdString(uri));
181 }
184void Lens::fallbackActivate(const QString& uri)
186 /* FIXME: stripping all content before the first column because for some
187 reason the lenses give uri with junk content at their beginning.
188 */
189 QString tweakedUri = uri;
190 int firstColumnAt = tweakedUri.indexOf(":");
191 tweakedUri.remove(0, firstColumnAt+1);
193 /* Tries various methods to trigger a sensible action for the given 'uri'.
194 If it has no understanding of the given scheme it falls back on asking
195 Qt to open the uri.
196 */
197 QUrl url(tweakedUri);
198 if (url.scheme() == "file") {
199 /* Override the files place's default URI handler: we want the file
200 manager to handle opening folders, not the dash.
202 Ref: https://bugs.launchpad.net/upicek/+bug/689667
203 */
204 QDesktopServices::openUrl(url);
205 return;
206 }
207 if (url.scheme() == "application") {
208 // TODO: implement application handling
209 return;
210 }
212 qWarning() << "FIXME: Possibly no handler for scheme: " << url.scheme();
213 qWarning() << "Trying to open" << tweakedUri;
214 /* Try our luck */
215 QDesktopServices::openUrl(url);
218void Lens::setUnityLens(const unity::dash::Lens::Ptr& lens)
220 m_unityLens = lens;
222 if (QString::fromStdString(m_unityLens->results()->swarm_name) == QString(":local")) {
223 m_results->setModel(m_unityLens->results()->model());
224 } else {
225 m_results->setName(QString::fromStdString(m_unityLens->results()->swarm_name));
226 }
227 if (QString::fromStdString(m_unityLens->global_results()->swarm_name) == QString(":local")) {
228 m_globalResults->setModel(m_unityLens->global_results()->model());
229 } else {
230 m_globalResults->setName(QString::fromStdString(m_unityLens->global_results()->swarm_name));
231 }
232 if (QString::fromStdString(m_unityLens->categories()->swarm_name) == QString(":local")) {
233 m_categories->setModel(m_unityLens->categories()->model());
234 } else {
235 m_categories->setName(QString::fromStdString(m_unityLens->categories()->swarm_name));
236 }
238 /* Property change signals */
239 m_unityLens->id.changed.connect(sigc::mem_fun(this, &Lens::idChanged));
240 m_unityLens->dbus_name.changed.connect(sigc::mem_fun(this, &Lens::dbusNameChanged));
241 m_unityLens->dbus_path.changed.connect(sigc::mem_fun(this, &Lens::dbusPathChanged));
242 m_unityLens->name.changed.connect(sigc::mem_fun(this, &Lens::nameChanged));
243 m_unityLens->icon_hint.changed.connect(sigc::mem_fun(this, &Lens::iconHintChanged));
244 m_unityLens->description.changed.connect(sigc::mem_fun(this, &Lens::descriptionChanged));
245 m_unityLens->search_hint.changed.connect(sigc::mem_fun(this, &Lens::searchHintChanged));
246 m_unityLens->visible.changed.connect(sigc::mem_fun(this, &Lens::visibleChanged));
247 m_unityLens->search_in_global.changed.connect(sigc::mem_fun(this, &Lens::searchInGlobalChanged));
248 m_unityLens->shortcut.changed.connect(sigc::mem_fun(this, &Lens::shortcutChanged));
249 m_unityLens->connected.changed.connect(sigc::mem_fun(this, &Lens::connectedChanged));
250 m_unityLens->results.changed.connect(sigc::mem_fun(this, &Lens::onResultsChanged));
251 m_unityLens->results()->swarm_name.changed.connect(sigc::mem_fun(this, &Lens::onResultsSwarmNameChanged));
252 m_unityLens->global_results.changed.connect(sigc::mem_fun(this, &Lens::onGlobalResultsChanged));
253 m_unityLens->global_results()->swarm_name.changed.connect(sigc::mem_fun(this, &Lens::onGlobalResultsSwarmNameChanged));
254 m_unityLens->categories.changed.connect(sigc::mem_fun(this, &Lens::onCategoriesChanged));
255 m_unityLens->categories()->swarm_name.changed.connect(sigc::mem_fun(this, &Lens::onCategoriesSwarmNameChanged));
256 m_unityLens->view_type.changed.connect(sigc::mem_fun(this, &Lens::onViewTypeChanged));
258 /* Signals forwarding */
259 connect(this, SIGNAL(searchFinished(unity::dash::Lens::Hints const &, unity::glib::Error const &)), SLOT(onSearchFinished(unity::dash::Lens::Hints const &)));
261 /* FIXME: signal should be forwarded instead of calling the handler directly */
262 m_unityLens->activated.connect(sigc::mem_fun(this, &Lens::onActivated));
264 /* Synchronize local states with m_unityLens right now and whenever
265 m_unityLens becomes connected */
266 /* FIXME: should emit change notification signals for all properties */
267 connect(this, SIGNAL(connectedChanged(bool)), SLOT(synchronizeStates()));
268 synchronizeStates();
271unity::dash::Lens::Ptr Lens::unityLens() const
273 return m_unityLens;
276void Lens::synchronizeStates()
278 if (connected()) {
279 /* Forward local states to m_unityLens */
280 if (!m_searchQuery.isNull()) {
281 m_unityLens->Search(m_searchQuery.toStdString());
282 }
283 if (!m_globalSearchQuery.isNull()) {
284 m_unityLens->GlobalSearch(m_globalSearchQuery.toStdString());
285 }
286 }
289void Lens::onResultsSwarmNameChanged(const std::string& /* swarm_name */)
291 m_results->setName(QString::fromStdString(m_unityLens->results()->swarm_name));
294void Lens::onResultsChanged(const unity::dash::Results::Ptr& /* results */)
296 m_results->setName(QString::fromStdString(m_unityLens->results()->swarm_name));
299void Lens::onGlobalResultsSwarmNameChanged(const std::string& /* swarm_name */)
301 m_globalResults->setName(QString::fromStdString(m_unityLens->global_results()->swarm_name));
304void Lens::onGlobalResultsChanged(const unity::dash::Results::Ptr& /* global_results */)
306 m_globalResults->setName(QString::fromStdString(m_unityLens->global_results()->swarm_name));
309void Lens::onCategoriesSwarmNameChanged(const std::string& /* swarm_name */)
311 m_categories->setName(QString::fromStdString(m_unityLens->categories()->swarm_name));
314void Lens::onCategoriesChanged(const unity::dash::Categories::Ptr& /* categories */)
316 m_categories->setName(QString::fromStdString(m_unityLens->categories()->swarm_name));
319void Lens::onViewTypeChanged(unity::dash::ViewType viewType)
321 Q_EMIT viewTypeChanged( (Lens::ViewType) viewType);
324void Lens::onSearchFinished(unity::dash::Lens::Hints const &hints)
326 QString hint;
328 if (!m_unityLens->results()->count()) {
329 unity::dash::Lens::Hints::const_iterator it = hints.find("no-results-hint");
330 if (it != hints.end()) {
331 hint = QString::fromStdString(it->second.GetString());
332 } else {
333 hint = QString::fromUtf8(dgettext("unity", "Sorry, there is nothing that matches your search."));
334 }
335 }
337 setNoResultsHint(hint);