4
"launchpad.net/go-unityscopes/v1"
8
const searchCategoryTemplate = `{
11
"category-layout": "grid",
17
"subtitle": "username"
21
// SCOPE ***********************************************************************
23
var scope_interface scopes.Scope
26
base *scopes.ScopeBase
29
func (s *MyScope) Preview(result *scopes.Result, metadata *scopes.ActionMetadata, reply *scopes.PreviewReply, cancelled <-chan bool) error {
30
layout1col := scopes.NewColumnLayout(1)
31
layout2col := scopes.NewColumnLayout(2)
32
layout3col := scopes.NewColumnLayout(3)
34
// Single column layout
35
layout1col.AddColumn([]string{"image", "header", "summary", "actions"})
38
layout2col.AddColumn([]string{"image"})
39
layout2col.AddColumn([]string{"header", "summary", "actions"})
41
// Three cokumn layout
42
layout3col.AddColumn([]string{"image"})
43
layout3col.AddColumn([]string{"header", "summary", "actions"})
44
layout3col.AddColumn([]string{})
46
// Register the layouts we just created
47
reply.RegisterLayout([]*scopes.ColumnLayout{layout1col, layout2col, layout3col})
49
header := scopes.NewPreviewWidget("header", "header")
51
// It has title and a subtitle properties
52
header.AddAttributeMapping("title", "title")
53
header.AddAttributeMapping("subtitle", "subtitle")
55
// Define the image section
56
image := scopes.NewPreviewWidget("image", "image")
57
// It has a single source property, mapped to the result's art property
58
image.AddAttributeMapping("source", "art")
60
// Define the summary section
61
description := scopes.NewPreviewWidget("summary", "text")
62
// It has a text property, mapped to the result's description property
63
description.AddAttributeMapping("text", "description")
66
tuple1 := make(map[string]interface{})
68
tuple1["label"] = "Open"
69
tuple1["uri"] = "application:///tmp/non-existent.desktop"
71
tuple2 := make(map[string]interface{})
72
tuple1["id"] = "download"
73
tuple1["label"] = "Download"
75
tuple3 := make(map[string]interface{})
77
tuple1["label"] = "Hide"
79
actions := scopes.NewPreviewWidget("actions", "actions")
80
actions.AddAttributeValue("actions", []interface{}{tuple1, tuple2, tuple3})
83
metadata.ScopeData(scope_data)
84
if len(scope_data) > 0 {
85
extra := scopes.NewPreviewWidget("extra", "text")
86
extra.AddAttributeValue("text", "test Text")
87
reply.PushWidgets(header, image, description, actions, extra)
89
reply.PushWidgets(header, image, description, actions)
95
func (s *MyScope) Search(query *scopes.CannedQuery, metadata *scopes.SearchMetadata, reply *scopes.SearchReply, cancelled <-chan bool) error {
96
root_department := s.CreateDepartments(query, metadata, reply)
97
reply.RegisterDepartments(root_department)
99
return s.AddQueryResults(reply, query.QueryString())
102
func (s *MyScope) SetScopeBase(base *scopes.ScopeBase) {
106
// RESULTS *********************************************************************
108
func (s *MyScope) AddQueryResults(reply *scopes.SearchReply, query string) error {
109
cat := reply.RegisterCategory("category", "Category", "", searchCategoryTemplate)
111
result := scopes.NewCategorisedResult(cat)
112
result.SetURI("http://localhost/" + query)
113
result.SetDndURI("http://localhost_dnduri" + query)
114
result.SetTitle("TEST" + query)
115
result.SetArt("https://pbs.twimg.com/profile_images/1117820653/5ttls5.jpg.png")
116
result.Set("test_value_bool", true)
117
result.Set("test_value_string", "test_value"+query)
118
result.Set("test_value_int", 1999)
119
result.Set("test_value_float", 1.999)
120
if err := reply.Push(result); err != nil {
124
result.SetURI("http://localhost2/" + query)
125
result.SetDndURI("http://localhost_dnduri2" + query)
126
result.SetTitle("TEST2")
127
result.SetArt("https://pbs.twimg.com/profile_images/1117820653/5ttls5.jpg.png")
128
result.Set("test_value_bool", false)
129
result.Set("test_value_string", "test_value2"+query)
130
result.Set("test_value_int", 2000)
131
result.Set("test_value_float", 2.100)
133
// add a variant map value
134
m := make(map[string]interface{})
136
m["value2"] = "string_value"
137
result.Set("test_value_map", m)
139
// add a variant array value
140
l := make([]interface{}, 0)
142
l = append(l, "string_value")
143
result.Set("test_value_array", l)
144
if err := reply.Push(result); err != nil {
151
// DEPARTMENTS *****************************************************************
153
func SearchDepartment(root *scopes.Department, id string) *scopes.Department {
154
sub_depts := root.Subdepartments()
155
for _, element := range sub_depts {
156
if element.Id() == id {
163
func (s *MyScope) GetRockSubdepartments(query *scopes.CannedQuery,
164
metadata *scopes.SearchMetadata,
165
reply *scopes.SearchReply) *scopes.Department {
166
active_dep, err := scopes.NewDepartment("Rock", query, "Rock Music")
168
active_dep.SetAlternateLabel("Rock Music Alt")
169
department, _ := scopes.NewDepartment("60s", query, "Rock from the 60s")
170
active_dep.AddSubdepartment(department)
172
department2, _ := scopes.NewDepartment("70s", query, "Rock from the 70s")
173
active_dep.AddSubdepartment(department2)
179
func (s *MyScope) GetSoulSubdepartments(query *scopes.CannedQuery,
180
metadata *scopes.SearchMetadata,
181
reply *scopes.SearchReply) *scopes.Department {
182
active_dep, err := scopes.NewDepartment("Soul", query, "Soul Music")
184
active_dep.SetAlternateLabel("Soul Music Alt")
185
department, _ := scopes.NewDepartment("Motown", query, "Motown Soul")
186
active_dep.AddSubdepartment(department)
188
department2, _ := scopes.NewDepartment("New Soul", query, "New Soul")
189
active_dep.AddSubdepartment(department2)
195
func (s *MyScope) CreateDepartments(query *scopes.CannedQuery,
196
metadata *scopes.SearchMetadata,
197
reply *scopes.SearchReply) *scopes.Department {
198
department, _ := scopes.NewDepartment("", query, "Browse Music")
199
department.SetAlternateLabel("Browse Music Alt")
201
rock_dept := s.GetRockSubdepartments(query, metadata, reply)
202
if rock_dept != nil {
203
department.AddSubdepartment(rock_dept)
206
soul_dept := s.GetSoulSubdepartments(query, metadata, reply)
207
if soul_dept != nil {
208
department.AddSubdepartment(soul_dept)
214
func (s *MyScope) PerformAction(result *scopes.Result, metadata *scopes.ActionMetadata, widgetId, actionId string) (*scopes.ActivationResponse, error) {
216
if (widgetId == "actions" && actionId == "hide")
218
return ActivationQueryBase::UPtr(new MyActivation(result, meta));
220
else if (widget_id == "actions" && action_id == "download")
222
MyActivation* response = new MyActivation(result, meta, ActivationResponse::ShowPreview);
223
response->setExtraData(meta.scope_data());
224
return ActivationQueryBase::UPtr(response);
226
return ActivationQueryBase::UPtr(new MyActivation(result, meta, ActivationResponse::NotHandled));
229
// MAIN ************************************************************************
233
scope_interface = &sc
235
if err := scopes.Run(&MyScope{}); err != nil {