Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import datetime
from PyQt5.QtCore import QObject, pyqtSignal
from calibre.gui2.actions import InterfaceAction
from calibre.gui2 import error_dialog
from calibre.utils.date import format_date

# This class will emit a signal when the viewer is launched
class ViewerMonitor(QObject):
viewer_launched = pyqtSignal(list)

# The plugin class that will do the work
class AutoDatestampAndView(InterfaceAction):
name = 'Auto Datestamp and View'
action_spec = ('Date and view book', None, 'View selected book and record date', None)
action_type = 'current'

def genesis(self):
# Create an instance of our monitor
self.monitor = ViewerMonitor()
self.monitor.viewer_launched.connect(self.datestamp_book)

# Hook into the View action's functionality
orig_view_func = self.gui.iactions['View']._view_calibre_books

def new_view_func(book_ids):
# Call the original view function
orig_view_func(book_ids)
# Emit our custom signal
self.monitor.viewer_launched.emit(book_ids)

# Replace the original view function with our new, hooked function
self.gui.iactions['View']._view_calibre_books = new_view_func

def datestamp_book(self, book_ids):
if not book_ids:
return

db = self.gui.library_view.model().db
date_column = '#lastopened'

if date_column not in db.custom_field_keys():
return error_dialog(self.gui, 'Before running this plugin',
'You need to create a custom Date column with the lookup name "%s".' % date_column,
show=True)

label = db.field_metadata.key_to_label(date_column)

# Update the date for each selected book
for book_id in book_ids:
now = datetime.datetime.now()
db.set_custom(book_id, now, label=label, commit=False)

# Commit changes to the database and refresh the view
db.commit()
self.gui.library_view.model().refresh_ids(book_ids)