diff --git a/main.py b/main.py new file mode 100644 index 0000000..cf76a8b --- /dev/null +++ b/main.py @@ -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)