1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#! /usr/bin/python
# Copyright (c) 2011 David Calle <davidc@framli.eu>
# Copyright (c) 2011 Michael Hall <mhall119@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
from gi.repository import GLib, GObject, Gio
from gi.repository import Dee
# FIXME: Some weird bug in Dee or PyGI makes Dee fail unless we probe
# it *before* we import the Unity module... ?!
_m = dir(Dee.SequenceModel)
from gi.repository import Unity
class LensBuilder(type):
'''
MetaClass for building Lens classes and subclasses
'''
def __new__(cls, name, bases, attrs):
#import pdb; pdb.set_trace()
super_new = super(LensBuilder, cls).__new__
parents = [b for b in bases if isinstance(b, LensBuilder)]
if not parents:
# If this isn't a subclass of LensBuilder, don't do anything special.
return super_new(cls, name, bases, attrs)
# Create the class.
module = attrs.pop('__module__')
new_class = super_new(cls, name, bases, {'__module__': module})
attr_meta = attrs.pop('Meta', None)
if not attr_meta:
meta = getattr(new_class, 'Meta', None)
else:
meta = attr_meta
base_meta = getattr(new_class, '_meta', None)
setattr(new_class, '_meta', LensMeta(meta))
for aName, a in attrs.items():
if isinstance(a, Unity.Scope):
new_class._meta.scope_dict[aName] = a
if not hasattr(meta, 'scope_order'):
new_class._meta.scope_order.append(aName)
elif isinstance(a, Unity.Category):
new_class._meta.category_dict[aName] = a
if not hasattr(meta, 'category_order'):
new_class._meta.category_order.append(aName)
setattr(new_class, aName, new_class._meta.category_order.index(aName))
elif isinstance(a, Unity.Filter):
new_class._meta.filter_dict[aName] = a
if not hasattr(meta, 'filter_order'):
new_class._meta.filter_order.append(aName)
else:
setattr(new_class, aName, a)
return new_class
class LensMeta(object):
'''
Metadata object for a Lens
'''
def __init__(self, meta):
self.name = getattr(meta, 'name', '')
self.title = getattr(meta, 'title', self.name.title()+' Lens')
self.bus_name = getattr(meta, 'bus_name', 'unity.singlet.lens.%s' % self.name)
self.bus_path = getattr(meta, 'bus_path', '/'+str(self.bus_name).replace('.', '/'))
self.category_dict = dict()
self.category_order = getattr(meta, 'category_order', [])
self.filter_dict = dict()
self.filter_order = getattr(meta, 'filter_order', [])
self.scope_dict = dict()
self.scope_order = getattr(meta, 'scope_order', [])
self.search_on_blank = getattr(meta, 'search_on_blank', False)
self.description = getattr(meta, 'description', '%s Lens' % self.name.title())
self.search_hint = getattr(meta, 'search_hint', '%s Search' % self.name.title())
self.icon = getattr(meta, 'icon', '/usr/share/icons/Humanity/mimes/24/unknown.svg')
@property
def categories(self):
return [self.category_dict[c] for c in self.category_order]
@property
def scopes(self):
return [self.scope_dict[s] for s in self.scope_order]
@property
def filters(self):
return [self.filter_dict[f] for f in self.filter_order]
class Lens(object):
__metaclass__ = LensBuilder
def __init__(self):
self._lens = Unity.Lens.new (self._meta.bus_path, self._meta.name)
self._lens.props.search_hint = self._meta.search_hint
self._lens.props.visible = True;
self._lens.props.search_in_global = False;
# Populate categories
self._lens.props.categories = self._meta.categories
# Populate filters
self._lens.props.filters = self._meta.filters
# Populate scopes
for scope in self._meta.scopes:
self._lens.add_local_scope (scope);
self._lens.export ()
class SingleScopeLens(Lens):
def __init__(self):
self._lens = Unity.Lens.new (self._meta.bus_path, self._meta.name)
self._lens.props.search_hint = self._meta.search_hint
self._lens.props.visible = True;
self._lens.props.search_in_global = False;
# Populate categories
self._lens.props.categories = self._meta.categories
# Populate filters
self._lens.props.filters = self._meta.filters
# Populate scopes
self._scope = Unity.Scope.new ("%s/main" % self._meta.bus_path)
self._scope.connect ("search-changed", self.on_search_changed)
self._scope.connect ("filters-changed", self.on_filtering_changed);
self._scope.connect('preview-uri', self.on_preview_uri)
if hasattr(self, 'handle_uri'):
self._scope.connect('activate-uri', self.handle_uri)
self._scope.export()
self._lens.add_local_scope (self._scope);
self._lens.export ()
def on_search_changed (self, entry, search, search_type, cancellable):
if search:
search_string = search.props.search_string
else:
search_string = None
if self._meta.search_on_blank or (search_string is not None and search_string != ''):
results = search.props.results_model
results.clear()
if not cancellable.is_cancelled():
if search_type == Unity.SearchType.GLOBAL:
self.global_search(search_string, results)
else:
self.search(search_string, results)
search.finished()
def on_preview_uri(self, scope, uri):
model = scope.props.results_model
iter_item = model.get_first_iter()
end_iter = model.get_last_iter()
while iter_item != end_iter:
if model.get_value(iter_item, 0) == uri:
result_item = {
'uri': uri,
'image': model.get_value(iter_item, 1),
'category': model.get_value(iter_item, 2),
'mime-type': model.get_value(iter_item, 3),
'title': model.get_value(iter_item, 4),
'description': model.get_value(iter_item, 5),
'dnd-uri': model.get_value(iter_item, 6),
}
return self.preview(result_item, model)
iter_item = model.next(iter_item)
return None
def on_filtering_changed(self, *_):
self._scope.queue_search_changed(Unity.SearchType.DEFAULT)
def hide_dash_response(self, uri=''):
return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri=uri)
def update_dash_response(self, uri=''):
return Unity.ActivationResponse(handled=Unity.HandledType.SHOW_DASH, goto_uri=uri)
def global_search(self, phrase, results):
return self.search(phrase, results)
def search(self, phrase, results):
pass
def preview(self, result_item, result_model):
preview = Unity.GenericPreview.new(result_item['title'], result_item['description'], None)
preview.props.image_source_uri = result_item['image']
if hasattr(self, 'add_preview_data'):
self.add_preview_data(result_item, preview)
if hasattr(self, 'handle_uri'):
view_action = Unity.PreviewAction.new("open", "Open", None)
view_action.connect('activated', self.handle_uri)
preview.add_action(view_action)
return preview
|