on 7cd287d help sign in
unity8/tests/mocks/Unity/fake_scope.cpp
254 lines 6.9 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "fake_scope.h"
18#include "fake_preview.h"
19#include "paths.h"
21#include <dee.h>
23static DeeModel* create_categories_model(unsigned category_count);
24static DeeModel* create_results_model(unsigned category_count, unsigned result_count);
26// TODO: Implement remaining pieces
28Scope::Scope(QObject* parent) : Scope(QString(), QString(), false, parent)
32Scope::Scope(QString const& id, QString const& name, bool visible, QObject* parent)
33 : QObject(parent)
34 , m_id(id)
35 , m_name(name)
36 , m_visible(visible)
37 , m_searching(false)
38 , m_isActive(false)
39 , m_categories(new Categories(this))
40 , m_results(new DeeListModel(this))
42 DeeModel* results_model = create_results_model(20, 300);
43 m_categories->setResultModel(results_model);
44 m_categories->setModel(create_categories_model(20));
45 m_results->setModel(results_model);
47 m_timer.setInterval(1000);
48 m_timer.setSingleShot(true);
49 connect(&m_timer, &QTimer::timeout, [this]() {
50 Preview *p = new Preview(this);
51 Q_EMIT previewReady(p);
52 });
55QString Scope::id() const {
56 return m_id;
59QString Scope::name() const {
60 return m_name;
63QString Scope::searchQuery() const {
64 return m_searchQuery;
67QString Scope::iconHint() const {
68 return m_iconHint;
71QString Scope::description() const {
72 return m_description;
75QString Scope::searchHint() const {
76 return QString("");
79QString Scope::shortcut() const {
80 return QString("");
83bool Scope::connected() const {
84 return true;
87bool Scope::searchInProgress() const {
88 return m_searching;
91Categories* Scope::categories() const {
92 return m_categories;
95QString Scope::noResultsHint() const {
96 return m_noResultsHint;
99QString Scope::formFactor() const {
100 return m_formFactor;
103bool Scope::visible() const {
104 return m_visible;
107bool Scope::isActive() const {
108 return m_isActive;
111void Scope::setName(const QString &str) {
112 if (str != m_name) {
113 m_name = str;
114 Q_EMIT nameChanged(m_name);
115 }
118void Scope::setSearchQuery(const QString &str) {
119 if (str != m_searchQuery) {
120 m_searchQuery = str;
121 Q_EMIT searchQueryChanged();
122 }
125void Scope::setFormFactor(const QString &str) {
126 if (str != m_formFactor) {
127 m_formFactor = str;
128 Q_EMIT formFactorChanged();
129 }
132void Scope::setActive(const bool active) {
133 if (active != m_isActive) {
134 m_isActive = active;
135 Q_EMIT isActiveChanged();
136 }
139void Scope::setSearchInProgress(const bool inProg) {
140 if (inProg != m_searching) {
141 m_searching = inProg;
142 Q_EMIT searchInProgressChanged();
143 }
146void Scope::setNoResultsHint(const QString& str) {
147 if (str != m_noResultsHint) {
148 m_noResultsHint = str;
149 Q_EMIT noResultsHintChanged();
150 }
153void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
154 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
155 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
157 Q_UNUSED(uri);
158 Q_UNUSED(icon_hint);
159 Q_UNUSED(category);
160 Q_UNUSED(result_type);
161 Q_UNUSED(mimetype);
162 Q_UNUSED(title);
163 Q_UNUSED(comment);
164 Q_UNUSED(dnd_uri);
165 Q_UNUSED(metadata);
167 m_timer.start();
170void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
171 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
172 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
174 Q_UNUSED(uri);
175 Q_UNUSED(icon_hint);
176 Q_UNUSED(category);
177 Q_UNUSED(result_type);
178 Q_UNUSED(mimetype);
179 Q_UNUSED(title);
180 Q_UNUSED(comment);
181 Q_UNUSED(dnd_uri);
182 Q_UNUSED(metadata);
184 m_timer.start();
187static const gchar * categories_model_schema[] = {
188 "s", //ID
189 "s", // DISPLAY_NAME
190 "s", // ICON_HINT
191 "s", // RENDERER_NAME
192 "a{sv}" // HINTS
193};
196DeeModel* create_categories_model(unsigned category_count) {
197 DeeModel* category_model = dee_sequence_model_new();
198 dee_model_set_schema_full(category_model, categories_model_schema, G_N_ELEMENTS(categories_model_schema));
199 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
201 for(unsigned i = 0; i < category_count; ++i)
202 {
203 dee_model_append(category_model,
204 std::to_string(i).c_str(),
205 ("Category "+std::to_string(i)).c_str(),
206 "gtk-apply",
207 i % 2 == 0 ? "grid" : "carousel",
208 hints);
209 }
210 return category_model;
214/* Schema that is used in the DeeModel representing
215 the results */
216static const gchar * results_model_schema[] = {
217 "s", // URI
218 "s", // ICON_HINT
219 "u", // CATEGORY
220 "u", // RESULT_TYPE
221 "s", // MIMETYPE
222 "s", // TITLE
223 "s", // COMMENT
224 "s", // DND_URI
225 "a{sv}" // METADATA
226};
228DeeModel* create_results_model(unsigned category_count, unsigned result_count) {
229 DeeModel* results_model = dee_sequence_model_new();
230 dee_model_set_schema_full(results_model, results_model_schema, G_N_ELEMENTS(results_model_schema));
231 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
233 for(unsigned i = 0; i < result_count; ++i)
234 {
235 unsigned category = i % category_count;
237 dee_model_append(results_model,
238 ("uri://result."+std::to_string(i)).c_str(),
239 (shellAppDirectory() + "graphics/applicationIcons/dash.png").toLatin1().data(),
240 category,
241 0,
242 "application/x-desktop",
243 ("Title."+std::to_string(i)).c_str(),
244 ("Comment."+std::to_string(i)).c_str(),
245 ("uri://result."+std::to_string(i)).c_str(),
246 hints);
247 }
248 return results_model;
251void Scope::cancelActivation()
253 m_timer.stop();