Merge lp:~cmulk/cardapio/firefox-bookmarks into lp:cardapio

Proposed by cmulk
Status: Merged
Merged at revision: 468
Proposed branch: lp:~cmulk/cardapio/firefox-bookmarks
Merge into: lp:cardapio
Diff against target: 138 lines (+134/-0)
1 file modified
src/plugins/firefox_bookmarks.py (+134/-0)
To merge this branch: bzr merge lp:~cmulk/cardapio/firefox-bookmarks
Reviewer Review Type Date Requested Status
Thiago Teixeira Pending
Review via email: mp+35899@code.launchpad.net
To post a comment you must log in.
lp:~cmulk/cardapio/firefox-bookmarks updated
422. By me <me@me-laptop>

Fixed handling of file paths using os.path.join()

423. By me <me@me-laptop>

Added updating of bookmark list in plugin whenever Firefox is closed

424. By Clifton Mulkey

Fixed bug that occurs when firefox default profile folder is not explicitly set.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/plugins/firefox_bookmarks.py'
2--- src/plugins/firefox_bookmarks.py 1970-01-01 00:00:00 +0000
3+++ src/plugins/firefox_bookmarks.py 2010-10-01 19:51:02 +0000
4@@ -0,0 +1,134 @@
5+import_error = None
6+try:
7+ import os
8+ import sqlite3
9+ import shutil
10+ import gio
11+
12+except Exception, exception:
13+ import_error = exception
14+
15+
16+class CardapioPlugin (CardapioPluginInterface):
17+ author = 'Cardapio Team'
18+ name = _('Firefox Bookmarks')
19+ description = _('Search for Firefox Bookmarks')
20+
21+ url = ''
22+ help_text = ''
23+ version = '1.0'
24+
25+ plugin_api_version = 1.39
26+
27+ search_delay_type = None
28+ category_name = _('Firefox Bookmarks')
29+ category_icon = 'firefox'
30+ category_tooltip = _('Web bookmarks in Firefox')
31+
32+ fallback_icon = 'html'
33+
34+ hide_from_sidebar = True
35+
36+
37+ def __init__(self, cardapio_proxy):
38+ '''
39+ This method is called when the plugin is enabled.
40+ Nothing much to be done here except initialize variables and set loaded to True
41+ '''
42+ self.c = cardapio_proxy
43+
44+ if import_error:
45+ self.c.write_to_log(self, 'Could not import certain modules', is_error = True)
46+ self.c.write_to_log(self, import_error, is_error = True)
47+ self.loaded = False
48+ return
49+
50+
51+ self.build_bookmark_list()
52+ lock_path = os.path.join(self.prof_path,'lock')
53+
54+ #The lock file may not exist at first, but the monitor will
55+ #detect when it is created and deleted
56+ self.package_monitor = gio.File(lock_path).monitor_file()
57+ self.package_monitor.connect('changed', self.on_lock_changed)
58+
59+ self.loaded = True # set to true if everything goes well
60+
61+ def search(self, text, result_limit):
62+ results = []
63+ self.current_query = text
64+ text = text.lower()
65+
66+ for item in self.item_list:
67+ if len(results) >= result_limit: break
68+
69+ if item['name'].lower().find(text) != -1:
70+ results.append(item)
71+
72+ self.c.handle_search_result(self, results, self.current_query)
73+
74+ def build_bookmark_list(self):
75+
76+ firefox_path = os.path.join(os.environ['HOME'],".mozilla/firefox")
77+ ini_file = open(os.path.join(firefox_path,'profiles.ini'))
78+ prof_list = ini_file.read().split()
79+ ini_file.close()
80+
81+ try:
82+ prof_folder = prof_list[prof_list.index('Default=1') - 1].split('=')[1]
83+
84+ except ValueError:
85+
86+ try:
87+ prof_folder = prof_list[prof_list.index('Name=default') + 2].split('=')[1]
88+
89+ except ValueError:
90+ self.c.write_to_log(self, 'Could not determine firefox profile folder',
91+ is_error = True)
92+ return
93+
94+ self.prof_path = os.path.join(firefox_path,prof_folder)
95+
96+ db_path = os.path.join(self.prof_path, 'places.sqlite')
97+
98+ if not os.path.exists(db_path):
99+ self.c.write_to_log(self, 'Could not find the bookmarks database', is_error = True)
100+ return
101+
102+ #places.sqlite is locked when firefox is running, so we must make
103+ #a temporary copy to read the bookmarks from
104+ db_copy_path = '%s.copy' % db_path
105+ shutil.copy(db_path, db_copy_path)
106+
107+ sql_conn = sqlite3.connect(db_copy_path)
108+
109+ sql_query = "SELECT moz_bookmarks.title, moz_places.url \
110+ FROM moz_bookmarks, moz_places \
111+ WHERE moz_bookmarks.fk = moz_places.id AND moz_places.url NOT LIKE 'place:%'"
112+
113+ c = sql_conn.execute(sql_query)
114+
115+ self.item_list = []
116+ for row in c:
117+ item = {
118+ 'name' : _('%s') % row[0],
119+ 'tooltip' : _('Go To \"%s\"') % row[0] ,
120+ 'icon name' : 'html',
121+ 'type' : 'xdg',
122+ 'command' : '%s' % row[1],
123+ 'context menu' : None,
124+ }
125+ self.item_list.append(item)
126+
127+ sql_conn.close()
128+ os.remove(db_copy_path)
129+
130+ #TODO: Find a way to update plugin when bookmarks change in Firefox
131+
132+ def on_lock_changed(self, monitor, file, other_file, event):
133+ #Happens whenever Firefox is closed
134+ if event == gio.FILE_MONITOR_EVENT_DELETED:
135+ self.c.ask_for_reload_permission(self)
136+
137+ def on_reload_permission_granted(self):
138+ self.build_bookmark_list()

Subscribers

People subscribed via source and target branches