5
from locale import gettext as _
6
locale.textdomain('u1search')
8
from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory
1
# -*- coding: utf-8 -*-
4
from gi.repository import Gio
5
from dbus.mainloop.glib import DBusGMainLoop
6
from twisted.internet import defer
8
from singlet.lens import SingleScopeLens, IconViewCategory
10
10
from u1search import u1searchconfig
12
from ubuntuone import controlpanel
13
controlpanel.WEBSERVICE_BASE_URL = u"https://staging.one.ubuntu.com/api/"
15
from ubuntuone.controlpanel import backend
18
DBusGMainLoop(set_as_default=True)
20
SEARCH_API = u"file_storage/v1/search_names?q=%s"
23
def get_icon_names_for_path_or_uri(path_or_uri, content=None, *args, **kwargs):
25
Takes a file path or URI and its content if available, guesses the
26
content type, and returns a list of icon names.
30
>>> get_icon_names_for_path_or_uri('/tmp/foo.png')
31
['image-png', 'gnome-mime-image-png', 'image-x-generic']
34
# is_uncertain is true when no content is passed in, as the content may be
35
# different from the guessed MIME type.
36
# For example, a PNG containing JPG data.
37
(content_type, is_uncertain) = Gio.content_type_guess(path_or_uri, content)
38
icon_name = Gio.content_type_get_icon(content_type).get_names()
39
if len(icon_name) > 1:
12
45
class U1searchLens(SingleScopeLens):
47
"""Ubuntu One Scope to search u1 files in the server."""
16
51
description = 'U1search Lens'
17
search_hint = 'Search U1search'
52
search_hint = 'Search Ubuntu One files'
18
53
icon = 'u1search.svg'
21
# TODO: Add your categories
22
example_category = ListViewCategory("Examples", 'help')
54
search_on_blank = False
56
backend = backend.ControlBackend()
57
search_category = IconViewCategory("Files", 'ubuntuone')
59
@defer.inlineCallbacks
24
60
def search(self, search, results):
25
# TODO: Add your search results
26
results.append('https://wiki.ubuntu.com/Unity/Lenses/Singlet',
28
self.example_category,
61
"""Execute the proper search and load the the items in results."""
62
data = yield self.u1_files_query(search)
63
for name, url, icon in data:
31
'Find out how to write your Unity Lens',
32
'https://wiki.ubuntu.com/Unity/Lenses/Singlet')
72
@defer.inlineCallbacks
73
def u1_files_query(self, keywords):
74
"""Search for the keywords in the U1 files from the user."""
75
data = [] # (Text, Link)
77
defer.returnValue(data)
78
query = SEARCH_API % keywords
79
result = yield self.backend.wc.call_api(query)
80
for item in result['results']:
81
file_path = os.path.expanduser(item['path'])
82
icon = get_icon_names_for_path_or_uri(file_path)
83
data.append((os.path.basename(item['url']),
84
'file://' + file_path,
87
defer.returnValue(data)
b'\\ No newline at end of file'