on bc0a410 help sign in
unity8/tests/mocks/Unity/fake_scope.cpp
271 lines 7.5 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)
29 : QObject(parent)
30 , m_visible(false)
31 , m_searching(false)
32 , m_isActive(false)
33 , m_categories(new Categories(this))
34 , m_results(new DeeListModel(this))
36 DeeModel* results_model = create_results_model(4, 30);
37 m_categories->setResultModel(results_model);
38 m_categories->setModel(create_categories_model(4));
39 m_results->setModel(results_model);
41 m_timer.setInterval(1000);
42 m_timer.setSingleShot(true);
43 connect(&m_timer, &QTimer::timeout, [this]() {
44 Preview *p = new Preview(this);
45 Q_EMIT previewReady(p);
46 });
49Scope::Scope(QString const& id, QString const& name, bool visible, QObject* parent)
50 : QObject(parent)
51 , m_id(id)
52 , m_name(name)
53 , m_visible(visible)
54 , m_searching(false)
55 , m_isActive(false)
56 , m_categories(new Categories(this))
57 , m_results(new DeeListModel(this))
59 DeeModel* results_model = create_results_model(4, 30);
60 m_categories->setResultModel(results_model);
61 m_categories->setModel(create_categories_model(4));
62 m_results->setModel(results_model);
64 m_timer.setInterval(1000);
65 m_timer.setSingleShot(true);
66 connect(&m_timer, &QTimer::timeout, [this]() {
67 Preview *p = new Preview(this);
68 Q_EMIT previewReady(p);
69 });
72QString Scope::id() const {
73 return m_id;
76QString Scope::name() const {
77 return m_name;
80QString Scope::searchQuery() const {
81 return m_searchQuery;
84QString Scope::iconHint() const {
85 return m_iconHint;
88QString Scope::description() const {
89 return m_description;
92QString Scope::searchHint() const {
93 return QString("");
96QString Scope::shortcut() const {
97 return QString("");
100bool Scope::connected() const {
101 return true;
104bool Scope::searchInProgress() const {
105 return m_searching;
108Categories* Scope::categories() const {
109 return m_categories;
112QString Scope::noResultsHint() const {
113 return m_noResultsHint;
116QString Scope::formFactor() const {
117 return m_formFactor;
120bool Scope::visible() const {
121 return m_visible;
124bool Scope::isActive() const {
125 return m_isActive;
128void Scope::setName(const QString &str) {
129 if (str != m_name) {
130 m_name = str;
131 Q_EMIT nameChanged(m_name);
132 }
135void Scope::setSearchQuery(const QString &str) {
136 if (str != m_searchQuery) {
137 m_searchQuery = str;
138 Q_EMIT searchQueryChanged();
139 }
142void Scope::setFormFactor(const QString &str) {
143 if (str != m_formFactor) {
144 m_formFactor = str;
145 Q_EMIT formFactorChanged();
146 }
149void Scope::setActive(const bool active) {
150 if (active != m_isActive) {
151 m_isActive = active;
152 Q_EMIT isActiveChanged();
153 }
156void Scope::setSearchInProgress(const bool inProg) {
157 if (inProg != m_searching) {
158 m_searching = inProg;
159 Q_EMIT searchInProgressChanged();
160 }
163void Scope::setNoResultsHint(const QString& str) {
164 if (str != m_noResultsHint) {
165 m_noResultsHint = str;
166 Q_EMIT noResultsHintChanged();
167 }
170void Scope::activate(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();
187void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
188 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
189 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
191 Q_UNUSED(uri);
192 Q_UNUSED(icon_hint);
193 Q_UNUSED(category);
194 Q_UNUSED(result_type);
195 Q_UNUSED(mimetype);
196 Q_UNUSED(title);
197 Q_UNUSED(comment);
198 Q_UNUSED(dnd_uri);
199 Q_UNUSED(metadata);
201 m_timer.start();
204static const gchar * categories_model_schema[] = {
205 "s", //ID
206 "s", // DISPLAY_NAME
207 "s", // ICON_HINT
208 "s", // RENDERER_NAME
209 "a{sv}" // HINTS
210};
213DeeModel* create_categories_model(unsigned category_count) {
214 DeeModel* category_model = dee_sequence_model_new();
215 dee_model_set_schema_full(category_model, categories_model_schema, G_N_ELEMENTS(categories_model_schema));
216 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
218 for(unsigned i = 0; i < category_count; ++i)
219 {
220 dee_model_append(category_model,
221 std::to_string(i).c_str(),
222 ("Category "+std::to_string(i)).c_str(),
223 "gtk-apply",
224 "grid",
225 hints);
226 }
227 return category_model;
231/* Schema that is used in the DeeModel representing
232 the results */
233static const gchar * results_model_schema[] = {
234 "s", // URI
235 "s", // ICON_HINT
236 "u", // CATEGORY
237 "u", // RESULT_TYPE
238 "s", // MIMETYPE
239 "s", // TITLE
240 "s", // COMMENT
241 "s", // DND_URI
242 "a{sv}" // METADATA
243};
245DeeModel* create_results_model(unsigned category_count, unsigned result_count) {
246 DeeModel* results_model = dee_sequence_model_new();
247 dee_model_set_schema_full(results_model, results_model_schema, G_N_ELEMENTS(results_model_schema));
248 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
250 for(unsigned i = 0; i < result_count; ++i)
251 {
252 unsigned category = i % category_count;
254 dee_model_append(results_model,
255 ("uri://result."+std::to_string(i)).c_str(),
256 (shellAppDirectory() + "graphics/applicationIcons/dash.png").toLatin1().data(),
257 category,
258 0,
259 "application/x-desktop",
260 ("Title."+std::to_string(i)).c_str(),
261 ("Comment."+std::to_string(i)).c_str(),
262 ("uri://result."+std::to_string(i)).c_str(),
263 hints);
264 }
265 return results_model;
268void Scope::cancelActivation()
270 m_timer.stop();