Note: This action is now available as part of the builtin Single Field Edit Action.
Code:
from qt.core import (Qt, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QFrame,
QGroupBox, QCheckBox, QLineEdit, QRadioButton)
from calibre.gui2.widgets2 import Dialog
from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre_plugins.action_chains.actions.base import ChainAction
class ConfigWidget(QWidget):
def __init__(self, plugin_action):
QWidget.__init__(self)
self.plugin_action = plugin_action
self.gui = plugin_action.gui
self.db = self.gui.current_db
self._init_controls()
def _init_controls(self):
l = self.l = QVBoxLayout(self)
self.setLayout(l)
line = QFrame(self)
line.setFrameShape(QFrame.HLine)
line.setFrameShadow(QFrame.Sunken)
l.addWidget(line)
group_box = QGroupBox(_('Formats options'))
group_box_l = QVBoxLayout()
group_box.setLayout(group_box_l)
l.addWidget(group_box)
self.all_opt = QRadioButton(_('Remove all formats'))
self.all_opt.setChecked(True)
group_box_l.addWidget(self.all_opt)
self.include_opt = QRadioButton(_('Remove only specified formats (comma separated list)'))
self.include_edit = QLineEdit()
group_box_l.addWidget(self.include_opt)
group_box_l.addWidget(self.include_edit)
self.exclude_opt = QRadioButton(_('Remove all formats except spcefied (comma separated list)'))
self.exclude_edit = QLineEdit()
group_box_l.addWidget(self.exclude_opt)
group_box_l.addWidget(self.exclude_edit)
l.addStretch(1)
def load_settings(self, settings):
if settings:
if settings['opt'] == 'all':
self.all_opt.setChecked(True)
elif settings['opt'] == 'include':
self.include_opt.setChecked(True)
self.include_edit.setText(settings['include'])
elif settings['opt'] == 'exclude':
self.exclude_opt.setChecked(True)
self.exclude_edit.setText(settings['exclude'])
def save_settings(self):
settings = {}
if self.all_opt.isChecked():
settings['opt'] = 'all'
elif self.include_opt.isChecked():
settings['opt'] = 'include'
settings['include'] = self.include_edit.text()
elif self.exclude_opt.isChecked():
settings['opt'] = 'exclude'
settings['exclude'] = self.exclude_edit.text()
return settings
class RemoveFormats(ChainAction):
name = 'Remove Formats'
support_scopes = True
def run(self, gui, settings, chain):
db = gui.current_db
book_ids = chain.scope().get_book_ids()
for book_id in book_ids:
fmts_to_delete = set()
fmts_string = db.formats(book_id, index_is_id=True)
if fmts_string:
available_fmts = [ fmt.strip().upper() for fmt in fmts_string.split(',') ]
else:
available_fmts = []
if settings['remove_opt'] == 'all':
if available_fmts:
fmts_to_delete = available_fmts
elif settings['remove_opt'] == 'include':
fmts_to_delete = [ fmt.strip().upper() for fmt in settings.get('include', '').split(',') ]
elif settings['remove_opt'] == 'exclude':
fmts_to_keep = set([ fmt.strip().upper() for fmt in settings.get('exclude', '').split(',') ])
fmts_to_delete = set(available_fmts).difference(fmts_to_keep)
for fmt in fmts_to_delete:
fmt = fmt.upper()
if fmt in available_fmts:
db.remove_format(book_id, fmt, index_is_id=True, notify=False)
def default_settings(self):
return {'opt': 'all'}
def config_widget(self):
return ConfigWidget