View Single Post
Old 06-01-2011, 12:26 AM   #1
rozen
Member
rozen began at the beginning.
 
Posts: 19
Karma: 10
Join Date: Dec 2010
Location: CA
Device: nook
A Tiny Database Hack

Hi,

Since I often have several editions of the New York Times on my Nook where I can't readily tell the most recent one, I decided to try writing a simple plugin which would attach a date string to the title. I started with the Interface Demo Plugin and hacked away. I am running on Ubuntu 10.10.

Well I got my AddDate plugin to work but I had to make a hack to the database code. I feel that I should not have done that, so I am asking your guidance. Again, I think that I am missing something important.

The important code for my plug is the following:

def view(self):
rows = self.gui.library_view.selectionModel().selectedRow s()
previous = self.gui.library_view.currentIndex()

if not rows or len(rows) != 1:
d = error_dialog(self.gui, _('Cannot edit metadata'),
_('Please select exactly one row.'))
d.exec_()
return

row_list = [r.row() for r in rows]
current_row = 0

if len(row_list) == 1:
cr = row_list[0]
row_list = \
list(range(
self.gui.library_view.model().rowCount(QModelIndex ())))
current_row = row_list.index(cr)

r_db = self.gui.library_view.model().db
r_id = r_db.id(current_row)
r_metadata = r_db.get_metadata(current_row)
r_title = r_metadata.title
metadata_timestamp = r_metadata.timestamp
metadata_timestamp = metadata_timestamp.timetuple()

import time
date_addition = time.strftime("%a, %d %b %Y", metadata_timestamp)
if r_title.find(date_addition) > -1:
self.close()
return
new_title = r_title + ' [' + date_addition + ']'
r_metadata.title = new_title

r_db.set_metadata(r_id, r_metadata)

changed = set([r_id])
rows_to_refresh = set([r_id])

m = self.gui.library_view.model()

if rows_to_refresh:
m.refresh_rows(rows_to_refresh)

if changed:
m.refresh_ids(list(changed))
current = self.gui.library_view.currentIndex()
if self.gui.cover_flow:
self.gui.cover_flow.dataChanged()
m.current_changed(current, previous)
self.gui.tags_view.recount()

self.close()


When Executing the statement:

r_db.set_metadata(r_id, r_metadata)

I get an error from _set_title in database2.py. Specifically, the line

only_case_change = icu_lower(old_title) == icu_lower(title)

burps because old_title is None.

So I replaced that statement with:

only_case_change = 0
if old_title: # Added by Rozen.
only_case_change = icu_lower(old_title) == icu_lower(title)

For completeness I have attached my plugin.

Since I feel that I have done something evil in ignorance, I would appreciate any suggestion.

Thanks in Advance,
Don
Attached Files
File Type: zip add_date_plugin.zip (15.7 KB, 318 views)
rozen is offline   Reply With Quote