~kyrofa/unity-scope-libertine/add_debian_packaging

« back to all changes in this revision

Viewing changes to internal/launchpad.net/go-unityscopes/v2/metadata.go

  • Committer: Kyle Fazzari
  • Date: 2015-07-27 18:38:30 UTC
  • Revision ID: kyle@canonical.com-20150727183830-390on30ba491p1aq
Vendor dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package scopes
 
2
 
 
3
// #include <stdlib.h>
 
4
// #include "shim.h"
 
5
import "C"
 
6
import (
 
7
        "encoding/json"
 
8
        "fmt"
 
9
        "runtime"
 
10
        "unsafe"
 
11
)
 
12
 
 
13
type ConnectivityStatus int
 
14
 
 
15
const (
 
16
        ConnectivityStatusUnknown      ConnectivityStatus = 0
 
17
        ConnectivityStatusConnected    ConnectivityStatus = 1
 
18
        ConnectivityStatusDisconnected ConnectivityStatus = 2
 
19
)
 
20
 
 
21
// queryMetadata is the base class for extra metadata passed to scopes as a part of a request.
 
22
// This base class is not exported
 
23
type queryMetadata struct {
 
24
        m *C._QueryMetadata
 
25
}
 
26
 
 
27
// Locale returns the expected locale for the search request.
 
28
func (metadata *queryMetadata) Locale() string {
 
29
        locale := C.query_metadata_get_locale(metadata.m)
 
30
        defer C.free(unsafe.Pointer(locale))
 
31
        return C.GoString(locale)
 
32
}
 
33
 
 
34
// FormFactor returns the form factor for the search request.
 
35
func (metadata *queryMetadata) FormFactor() string {
 
36
        formFactor := C.query_metadata_get_form_factor(metadata.m)
 
37
        defer C.free(unsafe.Pointer(formFactor))
 
38
        return C.GoString(formFactor)
 
39
}
 
40
 
 
41
// SetInternetConnectivity indicates the internet connectivity status.
 
42
func (metadata *queryMetadata) SetInternetConnectivity(status ConnectivityStatus) {
 
43
        C.query_metadata_set_internet_connectivity(metadata.m, C.int(status))
 
44
}
 
45
 
 
46
// InternetConnectivity gets internet connectivity status.
 
47
func (metadata *queryMetadata) InternetConnectivity() ConnectivityStatus {
 
48
        return ConnectivityStatus(C.query_metadata_get_internet_connectivity(metadata.m))
 
49
}
 
50
 
 
51
// SearchMetadata holds additional metadata about the search request.
 
52
type SearchMetadata struct {
 
53
        queryMetadata
 
54
}
 
55
 
 
56
func finalizeSearchMetadata(metadata *SearchMetadata) {
 
57
        if metadata.m != nil {
 
58
                C.destroy_search_metadata((*C._SearchMetadata)(metadata.m))
 
59
        }
 
60
        metadata.m = nil
 
61
}
 
62
 
 
63
func makeSearchMetadata(m *C._SearchMetadata) *SearchMetadata {
 
64
        metadata := new(SearchMetadata)
 
65
        runtime.SetFinalizer(metadata, finalizeSearchMetadata)
 
66
        metadata.m = (*C._QueryMetadata)(m)
 
67
        return metadata
 
68
}
 
69
 
 
70
// NewSearchMetadata creates a new SearchMetadata with the given locale and
 
71
// form_factor
 
72
func NewSearchMetadata(cardinality int, locale, form_factor string) *SearchMetadata {
 
73
        return makeSearchMetadata(C.new_search_metadata(C.int(cardinality),
 
74
                unsafe.Pointer(&locale),
 
75
                unsafe.Pointer(&form_factor)))
 
76
}
 
77
 
 
78
// Cardinality returns the desired number of results for the search request.
 
79
func (metadata *SearchMetadata) Cardinality() int {
 
80
        return int(C.search_metadata_get_cardinality((*C._SearchMetadata)(metadata.m)))
 
81
}
 
82
 
 
83
type Location struct {
 
84
        Latitude           float64 `json:"latitude"`
 
85
        Longitude          float64 `json:"longitude"`
 
86
        Altitude           float64 `json:"altitude"`
 
87
        AreaCode           string  `json:"area_code"`
 
88
        City               string  `json:"city"`
 
89
        CountryCode        string  `json:"country_code"`
 
90
        CountryName        string  `json:"country_name"`
 
91
        HorizontalAccuracy float64 `json:"horizontal_accuracy"`
 
92
        VerticalAccuracy   float64 `json:"vertical_accuracy"`
 
93
        RegionCode         string  `json:"region_code"`
 
94
        RegionName         string  `json:"region_name"`
 
95
        ZipPostalCode      string  `json:"zip_postal_code"`
 
96
}
 
97
 
 
98
func (metadata *SearchMetadata) Location() *Location {
 
99
        var length C.int
 
100
        locData := C.search_metadata_get_location((*C._SearchMetadata)(metadata.m), &length)
 
101
        if locData == nil {
 
102
                return nil
 
103
        }
 
104
        defer C.free(locData)
 
105
        var location Location
 
106
        if err := json.Unmarshal(C.GoBytes(locData, length), &location); err != nil {
 
107
                panic(err)
 
108
        }
 
109
        return &location
 
110
}
 
111
 
 
112
// SetLocation sets the location
 
113
func (metadata *SearchMetadata) SetLocation(l *Location) error {
 
114
        location := locationMarshal{marshalFloat(l.Latitude),
 
115
                marshalFloat(l.Longitude),
 
116
                marshalFloat(l.Altitude),
 
117
                l.AreaCode,
 
118
                l.City,
 
119
                l.CountryCode,
 
120
                l.CountryName,
 
121
                marshalFloat(l.HorizontalAccuracy),
 
122
                marshalFloat(l.VerticalAccuracy),
 
123
                l.RegionCode,
 
124
                l.RegionName,
 
125
                l.ZipPostalCode}
 
126
        data, err := json.Marshal(location)
 
127
        if err != nil {
 
128
                return err
 
129
        }
 
130
        var errorString *C.char
 
131
        C.search_metadata_set_location((*C._SearchMetadata)(metadata.m), (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString)
 
132
        return checkError(errorString)
 
133
}
 
134
 
 
135
func (metadata *SearchMetadata) SetAggregatedKeywords(keywords []string) error {
 
136
        var errorString *C.char
 
137
        C.search_metadata_set_aggregated_keywords((*C._SearchMetadata)(metadata.m), unsafe.Pointer(&keywords[0]), C.int(len(keywords)), &errorString)
 
138
        return checkError(errorString)
 
139
}
 
140
 
 
141
func (metadata *SearchMetadata) AggregatedKeywords() []string {
 
142
        var length C.int
 
143
        keywordData := C.search_metadata_get_aggregated_keywords((*C._SearchMetadata)(metadata.m), &length)
 
144
        var keywords []string
 
145
        if err := json.Unmarshal(C.GoBytes(keywordData, length), &keywords); err != nil {
 
146
                panic(err)
 
147
        }
 
148
        return keywords
 
149
}
 
150
 
 
151
func (metadata *SearchMetadata) IsAggregated() bool {
 
152
        if C.search_metadata_is_aggregated((*C._SearchMetadata)(metadata.m)) == 0 {
 
153
                return false
 
154
        }
 
155
        return true
 
156
}
 
157
 
 
158
// ActionMetadata holds additional metadata about the preview request
 
159
// or result activation.
 
160
type ActionMetadata struct {
 
161
        queryMetadata
 
162
}
 
163
 
 
164
func finalizeActionMetadata(metadata *ActionMetadata) {
 
165
        if metadata.m != nil {
 
166
                C.destroy_action_metadata((*C._ActionMetadata)(metadata.m))
 
167
        }
 
168
        metadata.m = nil
 
169
}
 
170
 
 
171
// NewActionMetadata creates a new ActionMetadata with the given locale and
 
172
// form_factor
 
173
func NewActionMetadata(locale, form_factor string) *ActionMetadata {
 
174
        return makeActionMetadata(C.new_action_metadata(unsafe.Pointer(&locale),
 
175
                unsafe.Pointer(&form_factor)))
 
176
}
 
177
 
 
178
func makeActionMetadata(m *C._ActionMetadata) *ActionMetadata {
 
179
        metadata := new(ActionMetadata)
 
180
        runtime.SetFinalizer(metadata, finalizeActionMetadata)
 
181
        metadata.m = (*C._QueryMetadata)(m)
 
182
        return metadata
 
183
}
 
184
 
 
185
// ScopeData decodes the stored scope data into the given variable.
 
186
//
 
187
// Scope data is either set by the shell when calling a preview
 
188
// action, or set by the scope through an ActivationResponse object.
 
189
func (metadata *ActionMetadata) ScopeData(v interface{}) error {
 
190
        var dataLength C.int
 
191
        scopeData := C.action_metadata_get_scope_data((*C._ActionMetadata)(metadata.m), &dataLength)
 
192
        defer C.free(scopeData)
 
193
        return json.Unmarshal(C.GoBytes(scopeData, dataLength), v)
 
194
}
 
195
 
 
196
// SetScopeData attaches arbitrary data to this ActionMetadata.
 
197
func (metadata *ActionMetadata) SetScopeData(v interface{}) error {
 
198
        data, err := json.Marshal(v)
 
199
        if err != nil {
 
200
                return err
 
201
        }
 
202
        var errorString *C.char
 
203
        C.action_metadata_set_scope_data((*C._ActionMetadata)(metadata.m), (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString)
 
204
        return checkError(errorString)
 
205
}
 
206
 
 
207
// SetHint sets a hint.
 
208
func (metadata *ActionMetadata) SetHint(key string, value interface{}) error {
 
209
        data, err := json.Marshal(value)
 
210
        if err != nil {
 
211
                return err
 
212
        }
 
213
        var errorString *C.char
 
214
        C.action_metadata_set_hint((*C._ActionMetadata)(metadata.m), unsafe.Pointer(&key), (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString)
 
215
        return checkError(errorString)
 
216
}
 
217
 
 
218
// Hint returns a hint.
 
219
// Returns error if the hint does not exist or if we got an error unmarshaling
 
220
func (metadata *ActionMetadata) Hint(key string, value interface{}) error {
 
221
        var dataLength C.int
 
222
        var errorString *C.char
 
223
        scopeData := C.action_metadata_get_hint((*C._ActionMetadata)(metadata.m), unsafe.Pointer(&key), &dataLength, &errorString)
 
224
        if dataLength > 0 && errorString == nil {
 
225
                defer C.free(scopeData)
 
226
                return json.Unmarshal(C.GoBytes(scopeData, dataLength), value)
 
227
        } else {
 
228
                return checkError(errorString)
 
229
        }
 
230
}
 
231
 
 
232
// Hints gets all hints.
 
233
func (metadata *ActionMetadata) Hints(value interface{}) error {
 
234
        var length C.int
 
235
        data := C.action_metadata_get_hints((*C._ActionMetadata)(metadata.m), &length)
 
236
        if data == nil {
 
237
                return nil
 
238
        }
 
239
        defer C.free(data)
 
240
        return json.Unmarshal(C.GoBytes(data, length), value)
 
241
}
 
242
 
 
243
// we use this type to reimplement the marshaller interface in order to make values
 
244
// like 1.0 not being converted as 1 (integer).
 
245
type marshalFloat float64
 
246
 
 
247
func (n marshalFloat) MarshalJSON() ([]byte, error) {
 
248
        return []byte(fmt.Sprintf("%f", n)), nil
 
249
}
 
250
 
 
251
// the following structure is only used to control how the float64 types are
 
252
// marshaled. It is not exported.
 
253
type locationMarshal struct {
 
254
        Latitude           marshalFloat `json:"latitude"`
 
255
        Longitude          marshalFloat `json:"longitude"`
 
256
        Altitude           marshalFloat `json:"altitude"`
 
257
        AreaCode           string       `json:"area_code"`
 
258
        City               string       `json:"city"`
 
259
        CountryCode        string       `json:"country_code"`
 
260
        CountryName        string       `json:"country_name"`
 
261
        HorizontalAccuracy marshalFloat `json:"horizontal_accuracy"`
 
262
        VerticalAccuracy   marshalFloat `json:"vertical_accuracy"`
 
263
        RegionCode         string       `json:"region_code"`
 
264
        RegionName         string       `json:"region_name"`
 
265
        ZipPostalCode      string       `json:"zip_postal_code"`
 
266
}
 
267
 
 
268
type ProxyScopeMetadata struct {
 
269
        Identity string `json:"identity"`
 
270
        EndPoint string `json:"endpoint"`
 
271
}
 
272
 
 
273
// ScopeMetadata holds scope attributes such as name, description, icon etc.
 
274
//
 
275
// The information stored by ScopeMetadata comes from the .ini file for the given scope (for local scopes)
 
276
// or is fetched from the remote server (for scopes running on Smart Scopes Server).
 
277
// Use ListRegistryScopes from ScopeBase to get the metadata for all scopes.
 
278
type ScopeMetadata struct {
 
279
        m                    *C._ScopeMetadata
 
280
        Art                  string                 `json:"art"`
 
281
        Author               string                 `json:"author"`
 
282
        Description          string                 `json:"description"`
 
283
        DisplayName          string                 `json:"display_name"`
 
284
        Icon                 string                 `json:"icon"`
 
285
        Invisible            bool                   `json:"invisible"`
 
286
        IsAggregator         bool                   `json:"is_aggregator"`
 
287
        LocationDataNeeded   bool                   `json:"location_data_needed"`
 
288
        ScopeDir             string                 `json:"scope_dir"`
 
289
        ScopeId              string                 `json:"scope_id"`
 
290
        Version              int                    `json:"version"`
 
291
        Proxy                ProxyScopeMetadata     `json:"proxy"`
 
292
        AppearanceAttributes map[string]interface{} `json:"appearance_attributes"`
 
293
        SettingsDefinitions  []interface{}          `json:"settings_definitions"`
 
294
        Keywords             []string               `json:"keywords"`
 
295
}
 
296
 
 
297
func finalizeScopeMetadata(metadata *ScopeMetadata) {
 
298
        C.destroy_scope_metadata_ptr(metadata.m)
 
299
}
 
300
 
 
301
func makeScopeMetadata(m *C._ScopeMetadata, json_data string) *ScopeMetadata {
 
302
        metadata := new(ScopeMetadata)
 
303
        if err := json.Unmarshal([]byte(json_data), &metadata); err != nil {
 
304
                panic(err)
 
305
        }
 
306
        metadata.m = m
 
307
        runtime.SetFinalizer(metadata, finalizeScopeMetadata)
 
308
        return metadata
 
309
}