~ps-jenkins/unity-scope-firefoxbookmarks/latestsnapshot-0.1+13.10.20130723-0ubuntu1

« back to all changes in this revision

Viewing changes to src/unity_firefoxbookmarks_daemon.py

Add previews.

Approved by David Callé, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import os
21
21
import webbrowser
22
22
import sqlite3
 
23
import hashlib
23
24
 
24
25
APP_NAME = 'unity-scope-firefoxbookmarks'
25
26
LOCAL_PATH = '/usr/share/locale/'
35
36
PROVIDER_CREDITS = _('')
36
37
SVG_DIR = '/usr/share/icons/unity-icon-theme/places/svg/'
37
38
PROVIDER_ICON = SVG_DIR + 'service-firefoxbookmarks.svg'
38
 
DEFAULT_RESULT_ICON = SVG_DIR + 'result-help.svg'
 
39
DEFAULT_RESULT_ICON = 'gtk-about'
39
40
DEFAULT_RESULT_MIMETYPE = 'text/html'
40
41
DEFAULT_RESULT_TYPE = Unity.ResultType.DEFAULT
 
42
FIREFOX_EXECUTABLE = '/usr/bin/firefox'
41
43
BOOKMARKS_PATH = os.getenv("HOME") + "/.mozilla/firefox/"
 
44
BOOKMARKS_QUERY = '''SELECT moz_bookmarks.title, moz_places.url, moz_favicons.url, '%s', '%s'
 
45
                     FROM moz_bookmarks, moz_places, moz_favicons
 
46
                     WHERE moz_places.id = moz_bookmarks.fk
 
47
                         AND moz_places.favicon_id = moz_favicons.id
 
48
                         AND moz_bookmarks.title is not null
 
49
                         AND moz_places.favicon_id is not null
 
50
                         AND moz_bookmarks.type = 1
 
51
                         AND (moz_bookmarks.title LIKE '%%%s%%' OR moz_places.url LIKE '%%%s%%')
 
52
                     ORDER BY moz_bookmarks.lastModified;'''
42
53
 
43
54
c1 = {'id': 'bookmarks',
44
55
      'name': _('Bookmarks'),
51
62
EXTRA_METADATA = []
52
63
 
53
64
 
54
 
def get_bookmarks_from_firefox(path):
 
65
def get_bookmarks_from_firefox(path, search):
55
66
    '''
56
67
     Gets a list of bookmarks from the user's firefox profiles
57
68
    '''
68
79
                filename = dbfile.replace('/places.sqlite', '')
69
80
                file_name, profile_name = os.path.splitext(filename)
70
81
                profile_name = profile_name[1:]
71
 
                sqlite_query = '''SELECT moz_bookmarks.title, moz_places.url, moz_favicons.url, '%s'
72
 
                               FROM moz_bookmarks, moz_places, moz_favicons
73
 
                               WHERE moz_places.id = moz_bookmarks.fk
74
 
                                   AND moz_places.favicon_id = moz_favicons.id
75
 
                                   AND moz_bookmarks.title is not null
76
 
                                   AND moz_places.favicon_id is not null
77
 
                                   AND moz_bookmarks.type = 1
78
 
                               ORDER BY moz_bookmarks.lastModified;''' % profile_name
79
 
    
 
82
                sqlite_query = BOOKMARKS_QUERY % (profile_name, dbfile, search, search)
 
83
 
80
84
                conn = sqlite3.connect(dbfile)
81
85
                connection = conn.cursor()
82
86
                connection.execute(sqlite_query)
94
98
    Search for help documents matching the search string
95
99
    '''
96
100
    results = []
97
 
    bookmarks = get_bookmarks_from_firefox(BOOKMARKS_PATH)
 
101
    bookmarks = get_bookmarks_from_firefox(BOOKMARKS_PATH, search)
98
102
 
99
103
    for bookmark in bookmarks:
100
104
        # Stop Firefox's smart bookmark folders from showing up
101
105
        if not bookmark[1].find("place:") == -1:
102
106
            continue
103
 
 
104
 
        if bookmark[2].startswith("chrome:"):
105
 
            icon = Gio.ThemedIcon.new("firefox").to_string()
106
 
        else:
107
 
            icon = bookmark[2]
108
 
 
109
 
        # Search bookmark names for matches
110
 
        if not bookmark[0].lower().find(search.lower()) == -1:
111
 
            results.append({'uri': bookmark[1],
112
 
                            'icon': icon,
113
 
                            'category': 0,
114
 
                            'title': bookmark[0],
115
 
                            'comment': bookmark[1],
116
 
                            'user':GLib.Variant('s', bookmark[3])})
117
 
            continue
118
 
 
119
 
        # Search bookmark urls for matches
120
 
        if not bookmark[1].lower().find(search.lower()) == -1:
121
 
            results.append({'uri': bookmark[1],
122
 
                            'icon': icon,
123
 
                            'category': 0,
124
 
                            'title': bookmark[0],
125
 
                            'comment': bookmark[1],
126
 
                            'user':GLib.Variant('s', bookmark[3])})
 
107
        path = bookmark[4].replace('places.sqlite', 'thumbnails/')
 
108
        icon = '%s%s.png' % (path, hashlib.md5(bookmark[1].encode()).hexdigest())
 
109
        if not os.path.exists(icon):
 
110
            icon = 'gtk-about'
 
111
        results.append({'uri': bookmark[1],
 
112
                        'icon': icon,
 
113
                        'category': 0,
 
114
                        'title': bookmark[0],
 
115
                        'user': GLib.Variant('s', bookmark[3])})
127
116
    return results
128
117
 
129
118
 
130
 
def activate(scope, uri):
 
119
def activate(result, metadata, id):
131
120
    '''
132
121
    Open the url in the default webbrowser
133
122
    Args:
134
123
      uri: The url to be opened
135
124
    '''
136
 
    webbrowser.open(uri)
137
 
    return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri='')
138
 
 
 
125
    parameters = [FIREFOX_EXECUTABLE, result.uri]
 
126
    GLib.spawn_async(parameters)
 
127
    return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri=None)
 
128
 
 
129
 
 
130
class Preview(Unity.ResultPreviewer):
 
131
    '''
 
132
    Creates the preview for the result
 
133
    '''
 
134
    def do_run(self):
 
135
        '''
 
136
        Create a preview and return it
 
137
        '''
 
138
        preview = Unity.GenericPreview.new(self.result.title, '', None)
 
139
        preview.props.subtitle = self.result.uri
 
140
        
 
141
        if os.path.exists(self.result.icon_hint):
 
142
            preview.props.image_source_uri = 'file://' + self.result.icon_hint
 
143
        else:
 
144
            preview.props.image = Gio.ThemedIcon.new('gtk-about')
 
145
        show_action = Unity.PreviewAction.new("show", _("Show"), None)
 
146
        preview.add_action(show_action)
 
147
        return preview
139
148
 
140
149
# Classes below this point establish communication
141
150
# with Unity, you probably shouldn't modify them.
170
179
                    i['comment'] = ''
171
180
                if not 'dnd_uri' in i or not i['dnd_uri'] or i['dnd_uri'] == '':
172
181
                    i['dnd_uri'] = i['uri']
173
 
                i['metadata'] = {}
174
 
                if EXTRA_METADATA:
175
 
                    for e in i:
176
 
                        for m in EXTRA_METADATA:
177
 
                            if m['id'] == e:
178
 
                                i['metadata'][e] = i[e]
179
 
                i['metadata']['provider_credits'] = GLib.Variant('s', PROVIDER_CREDITS)
180
 
                result = Unity.ScopeResult.create(str(i['uri']), str(i['icon']),
181
 
                                                  i['category'], i['result_type'],
182
 
                                                  str(i['mimetype']), str(i['title']),
183
 
                                                  str(i['comment']), str(i['dnd_uri']),
184
 
                                                  i['metadata'])
185
 
                result_set.add_result(result)
 
182
                i['provider_credits'] = GLib.Variant('s', PROVIDER_CREDITS)
 
183
                result_set.add_result(**i)
186
184
        except Exception as error:
187
185
            print(error)
188
186
 
238
236
        se = MySearch(search_context)
239
237
        return se
240
238
 
 
239
    def do_activate(self, result, metadata, id):
 
240
        return activate(result, metadata, id)
 
241
 
 
242
    def do_create_previewer(self, result, metadata):
 
243
        '''
 
244
        Creates a preview when a resut is right-clicked
 
245
        '''
 
246
        result_preview = Preview()
 
247
        result_preview.set_scope_result(result)
 
248
        result_preview.set_search_metadata(metadata)
 
249
        return result_preview
 
250
 
241
251
 
242
252
def load_scope():
243
253
    return Scope()