on d379263 help sign in
unity8/tests/mocks/Unity/fake_scope.cpp
255 lines 7.0 kB history raw
1/*
2 * Copyright (C) 2013, 2014 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_previewRendererName("preview-generic")
40 , m_categories(new Categories(this))
41 , m_results(new DeeListModel(this))
43 DeeModel* results_model = create_results_model(20, 300);
44 m_categories->setResultModel(results_model);
45 m_categories->setModel(create_categories_model(20));
46 m_results->setModel(results_model);
48 m_timer.setInterval(1000);
49 m_timer.setSingleShot(true);
50 connect(&m_timer, &QTimer::timeout, [this]() {
51 Preview *p = new Preview(m_previewRendererName, this);
52 Q_EMIT previewReady(p);
53 });
56QString Scope::id() const {
57 return m_id;
60QString Scope::name() const {
61 return m_name;
64QString Scope::searchQuery() const {
65 return m_searchQuery;
68QString Scope::iconHint() const {
69 return m_iconHint;
72QString Scope::description() const {
73 return m_description;
76QString Scope::searchHint() const {
77 return QString("");
80QString Scope::shortcut() const {
81 return QString("");
84bool Scope::connected() const {
85 return true;
88bool Scope::searchInProgress() const {
89 return m_searching;
92Categories* Scope::categories() const {
93 return m_categories;
96QString Scope::noResultsHint() const {
97 return m_noResultsHint;
100QString Scope::formFactor() const {
101 return m_formFactor;
104bool Scope::visible() const {
105 return m_visible;
108bool Scope::isActive() const {
109 return m_isActive;
112void Scope::setName(const QString &str) {
113 if (str != m_name) {
114 m_name = str;
115 Q_EMIT nameChanged(m_name);
116 }
119void Scope::setSearchQuery(const QString &str) {
120 if (str != m_searchQuery) {
121 m_searchQuery = str;
122 Q_EMIT searchQueryChanged();
123 }
126void Scope::setFormFactor(const QString &str) {
127 if (str != m_formFactor) {
128 m_formFactor = str;
129 Q_EMIT formFactorChanged();
130 }
133void Scope::setActive(const bool active) {
134 if (active != m_isActive) {
135 m_isActive = active;
136 Q_EMIT isActiveChanged();
137 }
140void Scope::setSearchInProgress(const bool inProg) {
141 if (inProg != m_searching) {
142 m_searching = inProg;
143 Q_EMIT searchInProgressChanged();
144 }
147void Scope::setNoResultsHint(const QString& str) {
148 if (str != m_noResultsHint) {
149 m_noResultsHint = str;
150 Q_EMIT noResultsHintChanged();
151 }
154void Scope::activate(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
155 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
156 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
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 if (uri != "uri://result.2")
168 m_timer.start();
171void Scope::preview(const QVariant &uri, const QVariant &icon_hint, const QVariant &category,
172 const QVariant &result_type, const QVariant &mimetype, const QVariant &title,
173 const QVariant &comment, const QVariant &dnd_uri, const QVariant &metadata)
175 Q_UNUSED(uri);
176 Q_UNUSED(icon_hint);
177 Q_UNUSED(category);
178 Q_UNUSED(result_type);
179 Q_UNUSED(mimetype);
180 Q_UNUSED(title);
181 Q_UNUSED(comment);
182 Q_UNUSED(dnd_uri);
183 Q_UNUSED(metadata);
185 m_timer.start();
188static const gchar * categories_model_schema[] = {
189 "s", //ID
190 "s", // DISPLAY_NAME
191 "s", // ICON_HINT
192 "s", // RENDERER_NAME
193 "a{sv}" // HINTS
194};
197DeeModel* create_categories_model(unsigned category_count) {
198 DeeModel* category_model = dee_sequence_model_new();
199 dee_model_set_schema_full(category_model, categories_model_schema, G_N_ELEMENTS(categories_model_schema));
200 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
202 for(unsigned i = 0; i < category_count; ++i)
203 {
204 dee_model_append(category_model,
205 (i == 2) ? "applications.scope" : std::to_string(i).c_str(),
206 ("Category "+std::to_string(i)).c_str(),
207 "gtk-apply",
208 i % 2 == 0 ? "grid" : "carousel",
209 hints);
210 }
211 return category_model;
215/* Schema that is used in the DeeModel representing
216 the results */
217static const gchar * results_model_schema[] = {
218 "s", // URI
219 "s", // ICON_HINT
220 "u", // CATEGORY
221 "u", // RESULT_TYPE
222 "s", // MIMETYPE
223 "s", // TITLE
224 "s", // COMMENT
225 "s", // DND_URI
226 "a{sv}" // METADATA
227};
229DeeModel* create_results_model(unsigned category_count, unsigned result_count) {
230 DeeModel* results_model = dee_sequence_model_new();
231 dee_model_set_schema_full(results_model, results_model_schema, G_N_ELEMENTS(results_model_schema));
232 GVariant* hints = g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0);
234 for(unsigned i = 0; i < result_count; ++i)
235 {
236 unsigned category = i % category_count;
238 dee_model_append(results_model,
239 ("uri://result."+std::to_string(i)).c_str(),
240 (qmlDirectory() + "graphics/applicationIcons/dash.png").toLatin1().data(),
241 category,
242 0,
243 "application/x-desktop",
244 ("Title."+std::to_string(i)).c_str(),
245 ("Comment."+std::to_string(i)).c_str(),
246 ("uri://result."+std::to_string(i)).c_str(),
247 hints);
248 }
249 return results_model;
252void Scope::cancelActivation()
254 m_timer.stop();