~diegosarmentero/+junk/u1search

« back to all changes in this revision

Viewing changes to u1search/__init__.py

  • Committer: Diego Sarmentero
  • Date: 2013-03-28 15:17:14 UTC
  • Revision ID: diego.sarmentero@gmail.com-20130328151714-16nbf2ml4etasnu2
scope working

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import logging
2
 
import optparse
3
 
 
4
 
import locale
5
 
from locale import gettext as _
6
 
locale.textdomain('u1search')
7
 
 
8
 
from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory
 
1
# -*- coding: utf-8 -*-
 
2
import os
 
3
 
 
4
from gi.repository import Gio
 
5
from dbus.mainloop.glib import DBusGMainLoop
 
6
from twisted.internet import defer
 
7
 
 
8
from singlet.lens import SingleScopeLens, IconViewCategory
9
9
 
10
10
from u1search import u1searchconfig
11
11
 
 
12
from ubuntuone import controlpanel
 
13
controlpanel.WEBSERVICE_BASE_URL = u"https://staging.one.ubuntu.com/api/"
 
14
 
 
15
from ubuntuone.controlpanel import backend
 
16
 
 
17
 
 
18
DBusGMainLoop(set_as_default=True)
 
19
 
 
20
SEARCH_API = u"file_storage/v1/search_names?q=%s"
 
21
 
 
22
 
 
23
def get_icon_names_for_path_or_uri(path_or_uri, content=None, *args, **kwargs):
 
24
    """
 
25
       Takes a file path or URI and its content if available, guesses the
 
26
       content type, and returns a list of icon names.
 
27
 
 
28
       For example:
 
29
 
 
30
       >>> get_icon_names_for_path_or_uri('/tmp/foo.png')
 
31
       ['image-png', 'gnome-mime-image-png', 'image-x-generic']
 
32
    """
 
33
 
 
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:
 
40
        return icon_name[1]
 
41
    else:
 
42
        return 'ubuntuone'
 
43
 
 
44
 
12
45
class U1searchLens(SingleScopeLens):
13
46
 
 
47
    """Ubuntu One Scope to search u1 files in the server."""
 
48
 
14
49
    class Meta:
15
50
        name = 'u1search'
16
51
        description = 'U1search Lens'
17
 
        search_hint = 'Search U1search'
 
52
        search_hint = 'Search Ubuntu One files'
18
53
        icon = 'u1search.svg'
19
 
        search_on_blank=True
20
 
 
21
 
    # TODO: Add your categories
22
 
    example_category = ListViewCategory("Examples", 'help')
23
 
 
 
54
        search_on_blank = False
 
55
 
 
56
    backend = backend.ControlBackend()
 
57
    search_category = IconViewCategory("Files", 'ubuntuone')
 
58
 
 
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',
27
 
                         'ubuntu-logo',
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:
 
64
            results.append(url,
 
65
                         icon,
 
66
                         self.search_category,
29
67
                         "text/html",
30
 
                         'Learn More',
31
 
                         'Find out how to write your Unity Lens',
32
 
                         'https://wiki.ubuntu.com/Unity/Lenses/Singlet')
33
 
        pass
 
68
                         name,
 
69
                         '',
 
70
                         url)
 
71
 
 
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)
 
76
        if not keywords:
 
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,
 
85
                icon))
 
86
 
 
87
        defer.returnValue(data)
 
 
b'\\ No newline at end of file'