-
Notifications
You must be signed in to change notification settings - Fork 2
/
unity-lens-vbox.py
executable file
·250 lines (206 loc) · 9.35 KB
/
unity-lens-vbox.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /usr/bin/python
#
# (C) Pontus Ullgren
# Original code (C) Neil Jagdish Patel
#
# GPLv3
#
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
from subprocess import call
#
# The primary bus name we grab *must* match what we specify in our .lens file
#
BUS_NAME = "net.launchpad.Unity.Lens.VBox"
class Daemon:
def __init__(self):
# The path for the Lens *must* also match the one in our .lens file
self._lens = Unity.Lens.new("/net/launchpad/unity/lens/vbox", "vbox")
self._lens.props.search_hint = "Search VirtualBox VMs"
self._lens.props.visible = True
self._lens.props.search_in_global = True
self.populate_categories()
self.populate_filters()
# The Lens infrastructure is capable of having a Lens daemon run independantly
# of it's Scopes, so you can have the Music Lens that does nothing but
# add some filters and categories, but then, depending on which music player
# the user uses, they can install the right scope and it'll all work.
#
# However, many times you want to ship something useful with your Lens, which
# means you'll want to ship a scope to make things work. Instead of having to
# create a separate daemon and jump through a bunch of hoops to make all that
# work, you can create a Scope(s) in the same process and use the
# lens.add_local_scope() method to let the Lens know that it exists.
self._scope = UserScope()
self._lens.add_local_scope(self._scope.get_scope())
# Now we've set the categories and filters, export the Scope.
self._lens.export()
def populate_filters(self):
filters = []
# Radiooption is my favourite filter as it only allows the user to select on
# option at a time, which does wonders for reducing the complexity of the code!
# The first argument is the most important, it's an "id" that when a scope
# queries the filter for the active option, it will receive back one of these
# ids (i.e. the scope may receive "forms"). If it receives None, then that
# means the user hasn't selected anything.
f = Unity.RadioOptionFilter.new("type", "Type", Gio.ThemedIcon.new(""), False)
f.add_option("all", "All", None)
f.add_option("running", "Running", None)
filters.append(f)
self._lens.props.filters = filters
def populate_categories(self):
# Ideally we'd have more pertinant icons for our Lens, but for now we'll
# steal Unitys :)
icon_loc = "/usr/share/icons/unity-icon-theme/places/svg/group-recent.svg"
cats = []
# You should appent categories in the order you'd like them displayed
# When you append a result to the model, the third integer argument is
# actually telling Unity which Category you'd like the result displayed in.
#
# For example: To add something to "Modified Today", you would send '0' when
# adding that result. For "Modified Earilier This Month", you would send '3'.
#
# The third, CategoryRenderer, argument allows you to hint to Unity how you
# would like the results for that Category to be displayed.
cats.append(Unity.Category.new("All",
Gio.ThemedIcon.new(icon_loc),
Unity.CategoryRenderer.VERTICAL_TILE))
cats.append(Unity.Category.new("Running",
Gio.ThemedIcon.new(icon_loc),
Unity.CategoryRenderer.VERTICAL_TILE))
self._lens.props.categories = cats
# Encapsulates searching a single user's GDocs
class UserScope:
def __init__(self):
self._scope = Unity.Scope.new("/net/launchpad/unity/scope/vbox")
# Listen for changes and requests
self._scope.connect("notify::active-search", self._on_search_changed)
self._scope.connect("notify::active-global-search", self._on_global_search_changed)
self._scope.connect("activate_uri", self._on_uri_activated)
# This allows us to re-do the search if any parameter on a filter has changed
# Though it's possible to connect to a more-specific changed signal on each
# Fitler, as we re-do the search anyway, this catch-all signal is perfect for
# us.
self._scope.connect("filters-changed", self._on_search_changed)
self._scope.export()
def get_scope(self):
return self._scope
def get_search_string(self):
search = self._scope.props.active_search
return search.props.search_string if search else None
def get_global_search_string(self):
search = self._scope.props.active_global_search
return search.props.search_string if search else None
def search_finished(self):
search = self._scope.props.active_search
if search:
search.emit("finished")
def global_search_finished(self):
search = self._scope.props.active_global_search
if search:
search.emit("finished")
def _on_search_changed(self, scope, param_spec=None):
search = self.get_search_string()
results = scope.props.results_model
print "Search changed to: '%s'" % search
self._update_results_model(search, results)
self.search_finished()
def _on_global_search_changed(self, scope, param_spec):
search = self.get_global_search_string()
results = scope.props.global_results_model
print "Global search changed to: '%s'" % search
self._update_results_model(search, results, True)
self.global_search_finished()
def _update_results_model(self, search, model, is_global=False):
# Clear out the current results in the model as we'll get a new list
# NOTE: We could be clever and only remove/add things entries that have
# changed, however the cost of doing that for a small result set probably
# outweighs the gains. Especially if you consider the complexity,
model.clear()
# Get the list of documents
vboxlist = self.get_vbox_list(search, is_global)
for entry in vboxlist:
model.append("VBoxManage startvm %s" % entry['uuid'],
"virtualbox",
2,
"application-x-desktop",
entry['title'].encode("UTF-8"),
"All",
"")
# This is where we do the actual search for documents
def get_vbox_list(self, search, is_global):
# stdout_handle = Popen("VBoxManage list vms", shell=True, bufsize=1024, stdout=subprocess.PIPE).stdout
stdout_handle = os.popen("VBoxManage list vms", "r")
result = []
for line in stdout_handle.readlines():
[title, uuid] = line.rsplit(' ', 1)
if not search or title.find(search.strip()) != -1:
result.append({'title': title, 'uuid': uuid})
return result
def apply_filters(self, uri):
# Try and grab a known filter and check whether or not it has an active
# option. We've been clever and updated our filter option id's to match
# what google expect in the request, so we can just use the id's when
# constructing the uri :)
f = self._scope.get_filter("type")
if f != None:
o = f.get_active_option()
if o != None:
uri += "/-/" + o.props.id
f = self._scope.get_filter("ownership")
if f != None:
o = f.get_active_option()
if o != None:
uri += "/mine"
return uri
# Send back a useful icon depending on the document type
def icon_for_type(self, doc_type):
ret = "text-x-preview"
if doc_type == "pdf":
ret = "gnome-mime-application-pdf"
elif doc_type == "drawing":
ret = "x-office-drawing"
elif doc_type == "document":
ret = "x-office-document"
elif doc_type == "presentation":
ret = "libreoffice-oasis-presentation"
elif doc_type == "spreadsheet" or doc_type == "text/xml":
ret = "x-office-spreadsheet"
elif doc_type == "folder":
ret = "folder"
else:
print "Unhandled icon type: ", doc_type
return ret
def _on_uri_activated(self, scope, uri):
call(uri, shell=True)
return Unity.ActivationResponse.new(Unity.HandledType.HIDE_DASH)
if __name__ == "__main__":
# NOTE: If we used the normal 'dbus' module for Python we'll get
# slightly odd results because it uses a default connection
# to the session bus that is different from the default connection
# GDBus (hence libunity) will use. Meaning that the daemon name
# will be owned by a connection different from the one all our
# Dee + Unity magic is working on...
# Still waiting for nice GDBus bindings to land:
# http://www.piware.de/2011/01/na-zdravi-pygi/
session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
session_bus = Gio.DBusProxy.new_sync(session_bus_connection, 0, None,
'org.freedesktop.DBus',
'/org/freedesktop/DBus',
'org.freedesktop.DBus', None)
result = session_bus.call_sync('RequestName',
GLib.Variant("(su)", (BUS_NAME, 0x4)),
0, -1, None)
# Unpack variant response with signature "(u)". 1 means we got it.
result = result.unpack()[0]
if result != 1:
print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
raise SystemExit(1)
daemon = Daemon()
GObject.MainLoop().run()