~ps-jenkins/unity-scope-firefoxbookmarks/latestsnapshot-0.1daily13.06.05-0ubuntu1

« back to all changes in this revision

Viewing changes to src/unity_firefoxbookmarks_daemon.py

  • Committer: David Callé
  • Date: 2013-03-14 15:11:26 UTC
  • Revision ID: davidc@framli.eu-20130314151126-6rjtv2gcj948emn2
Depends on unity-scopes-runner

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
24
23
 
25
24
APP_NAME = 'unity-scope-firefoxbookmarks'
26
25
LOCAL_PATH = '/usr/share/locale/'
36
35
PROVIDER_CREDITS = _('')
37
36
SVG_DIR = '/usr/share/icons/unity-icon-theme/places/svg/'
38
37
PROVIDER_ICON = SVG_DIR + 'service-firefoxbookmarks.svg'
39
 
DEFAULT_RESULT_ICON = 'gtk-about'
 
38
DEFAULT_RESULT_ICON = SVG_DIR + 'result-help.svg'
40
39
DEFAULT_RESULT_MIMETYPE = 'text/html'
41
40
DEFAULT_RESULT_TYPE = Unity.ResultType.DEFAULT
42
 
FIREFOX_EXECUTABLE = '/usr/bin/firefox'
43
41
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;'''
53
42
 
54
43
c1 = {'id': 'bookmarks',
55
44
      'name': _('Bookmarks'),
62
51
EXTRA_METADATA = []
63
52
 
64
53
 
65
 
def get_bookmarks_from_firefox(path, search):
66
 
    '''
67
 
     Gets a list of bookmarks from the user's firefox profiles
68
 
    '''
69
 
    # Build Firefox's profile paths
70
 
    firefox_dbfiles = []
 
54
def get_bookmarks_from_firefox(path):
 
55
    '''
 
56
     Gets a list of bookmarks from the user's firefox profile
 
57
    '''
 
58
    # Build Firefox's profile path
 
59
    firefox_dbfile = ""
71
60
    bookmarks = []
72
61
    for folder in os.listdir(path):
73
 
        if '.' in folder:
74
 
            firefox_dbfiles.append(path + folder + "/places.sqlite")
75
 
 
76
 
    for dbfile in firefox_dbfiles:
77
 
        if os.path.exists(dbfile):
78
 
            try:
79
 
                filename = dbfile.replace('/places.sqlite', '')
80
 
                file_name, profile_name = os.path.splitext(filename)
81
 
                profile_name = profile_name[1:]
82
 
                sqlite_query = BOOKMARKS_QUERY % (profile_name, dbfile, search, search)
83
 
 
84
 
                conn = sqlite3.connect(dbfile)
85
 
                connection = conn.cursor()
86
 
                connection.execute(sqlite_query)
87
 
                profile_bookmarks = connection.fetchall()
88
 
                for bookmark in profile_bookmarks:
89
 
                    bookmarks.append(bookmark)
90
 
                connection.close()
91
 
            except sqlite3.DatabaseError:
92
 
                pass
 
62
        if folder.endswith(".default"):
 
63
            firefox_dbfile = path + folder + "/places.sqlite"
 
64
 
 
65
    if os.path.exists(firefox_dbfile):
 
66
        try:
 
67
            sqlite_query = '''SELECT moz_bookmarks.title, moz_places.url, moz_favicons.url
 
68
                           FROM moz_bookmarks, moz_places, moz_favicons
 
69
                           WHERE moz_places.id = moz_bookmarks.fk
 
70
                               AND moz_places.favicon_id = moz_favicons.id
 
71
                               AND moz_bookmarks.title is not null
 
72
                               AND moz_places.favicon_id is not null
 
73
                               AND moz_bookmarks.type = 1
 
74
                           ORDER BY moz_bookmarks.lastModified;'''
 
75
 
 
76
            conn = sqlite3.connect(firefox_dbfile)
 
77
            connection = conn.cursor()
 
78
            connection.execute(sqlite_query)
 
79
            bookmarks = connection.fetchall()
 
80
            connection.close()
 
81
        except:
 
82
            pass
93
83
    return bookmarks
94
84
 
95
85
 
98
88
    Search for help documents matching the search string
99
89
    '''
100
90
    results = []
101
 
    bookmarks = get_bookmarks_from_firefox(BOOKMARKS_PATH, search)
 
91
    bookmarks = get_bookmarks_from_firefox(BOOKMARKS_PATH)
102
92
 
103
93
    for bookmark in bookmarks:
104
94
        # Stop Firefox's smart bookmark folders from showing up
105
95
        if not bookmark[1].find("place:") == -1:
106
96
            continue
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])})
 
97
 
 
98
        if bookmark[2].startswith("chrome:"):
 
99
            icon = Gio.ThemedIcon.new("firefox").to_string()
 
100
        else:
 
101
            icon = bookmark[2]
 
102
 
 
103
        # Search bookmark names for matches
 
104
        if not bookmark[0].lower().find(search.lower()) == -1:
 
105
            results.append({'uri': bookmark[1],
 
106
                            'icon': icon,
 
107
                            'category': 0,
 
108
                            'title': bookmark[0],
 
109
                            'comment': bookmark[1]})
 
110
            continue
 
111
 
 
112
        # Search bookmark urls for matches
 
113
        if not bookmark[1].lower().find(search.lower()) == -1:
 
114
            results.append({'uri': bookmark[1],
 
115
                            'icon': icon,
 
116
                            'category': 0,
 
117
                            'title': bookmark[0],
 
118
                            'comment': bookmark[1]})
116
119
    return results
117
120
 
118
121
 
119
 
def activate(result, metadata, id):
 
122
def activate(scope, uri):
120
123
    '''
121
124
    Open the url in the default webbrowser
122
125
    Args:
123
126
      uri: The url to be opened
124
127
    '''
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
 
128
    webbrowser.open(uri)
 
129
    return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri='')
 
130
 
148
131
 
149
132
# Classes below this point establish communication
150
133
# with Unity, you probably shouldn't modify them.
179
162
                    i['comment'] = ''
180
163
                if not 'dnd_uri' in i or not i['dnd_uri'] or i['dnd_uri'] == '':
181
164
                    i['dnd_uri'] = i['uri']
182
 
                i['provider_credits'] = GLib.Variant('s', PROVIDER_CREDITS)
183
 
                result_set.add_result(**i)
 
165
                i['metadata'] = {}
 
166
                if EXTRA_METADATA:
 
167
                    for e in i:
 
168
                        for m in EXTRA_METADATA:
 
169
                            if m['id'] == e:
 
170
                                i['metadata'][e] = i[e]
 
171
                i['metadata']['provider_credits'] = GLib.Variant('s', PROVIDER_CREDITS)
 
172
                result = Unity.ScopeResult.create(str(i['uri']), str(i['icon']),
 
173
                                                  i['category'], i['result_type'],
 
174
                                                  str(i['mimetype']), str(i['title']),
 
175
                                                  str(i['comment']), str(i['dnd_uri']),
 
176
                                                  i['metadata'])
 
177
                result_set.add_result(result)
184
178
        except Exception as error:
185
179
            print(error)
186
180
 
236
230
        se = MySearch(search_context)
237
231
        return se
238
232
 
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
 
 
251
233
 
252
234
def load_scope():
253
235
    return Scope()