Debian Codegraph @

manual/plugin_examples/interface_demo/main.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
3
4
5 __license__ = 'GPL v3'
6 __copyright__ = '2011, Kovid Goyal <[email protected]>'
7 __docformat__ = 'restructuredtext en'
8
9 if False:
10 # This is here to keep my python error checker from complaining about
11 # the builtin functions that will be defined by the plugin loading system
12 # You do not need this code in your plugins
13 get_icons = get_resources = None
14
15 from qt.core import QDialog, QLabel, QMessageBox, QPushButton, QVBoxLayout
16
17 from calibre_plugins.interface_demo.config import prefs
18
19
20 class DemoDialog(QDialog):
21 def __init__(self, gui, icon, do_user_config):
22 QDialog.__init__(self, gui)
23 self.gui = gui
24 self.do_user_config = do_user_config
25
26 # The current database shown in the GUI
27 # db is an instance of the class LibraryDatabase from db/legacy.py
28 # This class has many, many methods that allow you to do a lot of
29 # things. For most purposes you should use db.new_api, which has
30 # a much nicer interface from db/cache.py
31 self.db = gui.current_db
32
33 self.l = QVBoxLayout()
34 self.setLayout(self.l)
35
36 self.label = QLabel(prefs['hello_world_msg'])
37 self.l.addWidget(self.label)
38
39 self.setWindowTitle('Interface Plugin Demo')
40 self.setWindowIcon(icon)
41
42 self.about_button = QPushButton('About', self)
43 self.about_button.clicked.connect(self.about)
44 self.l.addWidget(self.about_button)
45
46 self.marked_button = QPushButton('Show books with only one format in the calibre GUI', self)
47 self.marked_button.clicked.connect(self.marked)
48 self.l.addWidget(self.marked_button)
49
50 self.view_button = QPushButton('View the most recently added book', self)
51 self.view_button.clicked.connect(self.view)
52 self.l.addWidget(self.view_button)
53
54 self.update_metadata_button = QPushButton("Update metadata in a book's files", self)
55 self.update_metadata_button.clicked.connect(self.update_metadata)
56 self.l.addWidget(self.update_metadata_button)
57
58 self.conf_button = QPushButton('Configure this plugin', self)
59 self.conf_button.clicked.connect(self.config)
60 self.l.addWidget(self.conf_button)
61
62 self.resize(self.sizeHint())
63
64 def about(self):
65 # Get the about text from a file inside the plugin zip file
66 # The get_resources function is a builtin function defined for all your
67 # plugin code. It loads files from the plugin zip file. It returns
68 # the bytes from the specified file.
69 #
70 # Note that if you are loading more than one file, for performance, you
71 # should pass a list of names to get_resources. In this case,
72 # get_resources will return a dictionary mapping names to bytes. Names that
73 # are not found in the zip file will not be in the returned dictionary.
74 text = get_resources('about.txt')
75 QMessageBox.about(self, 'About the Interface Plugin Demo', text.decode('utf-8'))
76
77 def marked(self):
78 """Show books with only one format"""
79 db = self.db.new_api
80 matched_ids = {book_id for book_id in db.all_book_ids() if len(db.formats(book_id)) == 1}
81 # Mark the records with the matching ids
82 # new_api does not know anything about marked books, so we use the full
83 # db object
84 self.db.set_marked_ids(matched_ids)
85
86 # Tell the GUI to search for all marked records
87 self.gui.search.setEditText('marked:true')
88 self.gui.search.do_search()
89
90 def view(self):
91 """View the most recently added book"""
92 most_recent = most_recent_id = None
93 db = self.db.new_api
94 for book_id, timestamp in db.all_field_for('timestamp', db.all_book_ids()).items():
95 if most_recent is None or timestamp > most_recent:
96 most_recent = timestamp
97 most_recent_id = book_id
98
99 if most_recent_id is not None:
100 # Get a reference to the View plugin
101 view_plugin = self.gui.iactions['View']
102 # Ask the view plugin to launch the viewer for row_number
103 view_plugin._view_calibre_books([most_recent_id])
104
105 def update_metadata(self):
106 """
107 Set the metadata in the files in the selected book's record to
108 match the current metadata in the database.
109 """
110 from calibre.ebooks.metadata.meta import set_metadata
111 from calibre.gui2 import error_dialog, info_dialog
112
113 # Get currently selected books
114 rows = self.gui.library_view.selectionModel().selectedRows()
115 if not rows or len(rows) == 0:
116 return error_dialog(self.gui, 'Cannot update metadata', 'No books selected', show=True)
117 # Map the rows to book ids
118 ids = list(map(self.gui.library_view.model().id, rows))
119 db = self.db.new_api
120 for book_id in ids:
121 # Get the current metadata for this book from the db
122 mi = db.get_metadata(book_id, get_cover=True, cover_as_data=True)
123 fmts = db.formats(book_id)
124 if not fmts:
125 continue
126 for fmt in fmts:
127 fmt = fmt.lower()
128 # Get a python file object for the format. This will be either
129 # an in memory file or a temporary on disk file
130 ffile = db.format(book_id, fmt, as_file=True)
131 ffile.seek(0)
132 # Set metadata in the format
133 set_metadata(ffile, mi, fmt)
134 ffile.seek(0)
135 # Now replace the file in the calibre library with the updated
136 # file. We don't use add_format_with_hooks as the hooks were
137 # already run when the file was first added to calibre.
138 db.add_format(book_id, fmt, ffile, run_hooks=False)
139
140 info_dialog(self, 'Updated files', f'Updated the metadata in the files of {len(ids)} book(s)', show=True)
141
142 def config(self):
143 self.do_user_config(parent=self)
144 # Apply the changes
145 self.label.setText(prefs['hello_world_msg'])