mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #943025 (Add file filter for automaticaly adding books)
This commit is contained in:
parent
212a6cffab
commit
73427c2d4b
@ -104,6 +104,7 @@ gprefs.defaults['worker_max_time'] = 0
|
|||||||
gprefs.defaults['show_files_after_save'] = True
|
gprefs.defaults['show_files_after_save'] = True
|
||||||
gprefs.defaults['auto_add_path'] = None
|
gprefs.defaults['auto_add_path'] = None
|
||||||
gprefs.defaults['auto_add_check_for_duplicates'] = False
|
gprefs.defaults['auto_add_check_for_duplicates'] = False
|
||||||
|
gprefs.defaults['blocked_auto_formats'] = []
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
NONE = QVariant() #: Null value to return from the data function of item models
|
NONE = QVariant() #: Null value to return from the data function of item models
|
||||||
|
@ -17,6 +17,8 @@ from calibre.ptempfile import PersistentTemporaryDirectory
|
|||||||
from calibre.ebooks import BOOK_EXTENSIONS
|
from calibre.ebooks import BOOK_EXTENSIONS
|
||||||
from calibre.gui2 import question_dialog, gprefs
|
from calibre.gui2 import question_dialog, gprefs
|
||||||
|
|
||||||
|
AUTO_ADDED = frozenset(BOOK_EXTENSIONS) - {'pdr', 'mbp', 'tan'}
|
||||||
|
|
||||||
class Worker(Thread):
|
class Worker(Thread):
|
||||||
|
|
||||||
def __init__(self, path, callback):
|
def __init__(self, path, callback):
|
||||||
@ -26,7 +28,7 @@ class Worker(Thread):
|
|||||||
self.wake_up = Event()
|
self.wake_up = Event()
|
||||||
self.path, self.callback = path, callback
|
self.path, self.callback = path, callback
|
||||||
self.staging = set()
|
self.staging = set()
|
||||||
self.be = frozenset(BOOK_EXTENSIONS) - {'pdr', 'mbp', 'tan'}
|
self.allowed = AUTO_ADDED - frozenset(gprefs['blocked_auto_formats'])
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.tdir = PersistentTemporaryDirectory('_auto_adder')
|
self.tdir = PersistentTemporaryDirectory('_auto_adder')
|
||||||
@ -56,7 +58,7 @@ class Worker(Thread):
|
|||||||
# Must have read and write permissions
|
# Must have read and write permissions
|
||||||
and os.access(os.path.join(self.path, x), os.R_OK|os.W_OK)
|
and os.access(os.path.join(self.path, x), os.R_OK|os.W_OK)
|
||||||
# Must be a known ebook file type
|
# Must be a known ebook file type
|
||||||
and os.path.splitext(x)[1][1:].lower() in self.be
|
and os.path.splitext(x)[1][1:].lower() in self.allowed
|
||||||
]
|
]
|
||||||
data = {}
|
data = {}
|
||||||
# Give any in progress copies time to complete
|
# Give any in progress copies time to complete
|
||||||
|
@ -7,11 +7,14 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from PyQt4.Qt import Qt
|
||||||
|
|
||||||
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, \
|
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, \
|
||||||
CommaSeparatedList, AbortCommit
|
CommaSeparatedList, AbortCommit
|
||||||
from calibre.gui2.preferences.adding_ui import Ui_Form
|
from calibre.gui2.preferences.adding_ui import Ui_Form
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
from calibre.gui2.widgets import FilenamePattern
|
from calibre.gui2.widgets import FilenamePattern
|
||||||
|
from calibre.gui2.auto_add import AUTO_ADDED
|
||||||
from calibre.gui2 import gprefs, choose_dir, error_dialog, question_dialog
|
from calibre.gui2 import gprefs, choose_dir, error_dialog, question_dialog
|
||||||
|
|
||||||
class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||||
@ -38,6 +41,9 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
self.metadata_box.layout().insertWidget(0, self.filename_pattern)
|
self.metadata_box.layout().insertWidget(0, self.filename_pattern)
|
||||||
self.filename_pattern.changed_signal.connect(self.changed_signal.emit)
|
self.filename_pattern.changed_signal.connect(self.changed_signal.emit)
|
||||||
self.auto_add_browse_button.clicked.connect(self.choose_aa_path)
|
self.auto_add_browse_button.clicked.connect(self.choose_aa_path)
|
||||||
|
for signal in ('Activated', 'Changed', 'DoubleClicked', 'Clicked'):
|
||||||
|
signal = getattr(self.opt_blocked_auto_formats, 'item'+signal)
|
||||||
|
signal.connect(self.blocked_auto_formats_changed)
|
||||||
|
|
||||||
def choose_aa_path(self):
|
def choose_aa_path(self):
|
||||||
path = choose_dir(self, 'auto add path choose',
|
path = choose_dir(self, 'auto add path choose',
|
||||||
@ -50,11 +56,47 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
self.filename_pattern.blockSignals(True)
|
self.filename_pattern.blockSignals(True)
|
||||||
self.filename_pattern.initialize()
|
self.filename_pattern.initialize()
|
||||||
self.filename_pattern.blockSignals(False)
|
self.filename_pattern.blockSignals(False)
|
||||||
|
self.init_blocked_auto_formats()
|
||||||
self.opt_automerge.setEnabled(self.opt_add_formats_to_existing.isChecked())
|
self.opt_automerge.setEnabled(self.opt_add_formats_to_existing.isChecked())
|
||||||
|
|
||||||
|
# Blocked auto formats {{{
|
||||||
|
def blocked_auto_formats_changed(self, *args):
|
||||||
|
fmts = self.current_blocked_auto_formats
|
||||||
|
old = gprefs['blocked_auto_formats']
|
||||||
|
if set(fmts) != set(old):
|
||||||
|
self.changed_signal.emit()
|
||||||
|
|
||||||
|
def init_blocked_auto_formats(self, defaults=False):
|
||||||
|
if defaults:
|
||||||
|
fmts = gprefs.defaults['blocked_auto_formats']
|
||||||
|
else:
|
||||||
|
fmts = gprefs['blocked_auto_formats']
|
||||||
|
viewer = self.opt_blocked_auto_formats
|
||||||
|
viewer.blockSignals(True)
|
||||||
|
exts = set(AUTO_ADDED)
|
||||||
|
viewer.clear()
|
||||||
|
for ext in sorted(exts):
|
||||||
|
viewer.addItem(ext)
|
||||||
|
item = viewer.item(viewer.count()-1)
|
||||||
|
item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable)
|
||||||
|
item.setCheckState(Qt.Checked if
|
||||||
|
ext in fmts else Qt.Unchecked)
|
||||||
|
viewer.blockSignals(False)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_blocked_auto_formats(self):
|
||||||
|
fmts = []
|
||||||
|
viewer = self.opt_blocked_auto_formats
|
||||||
|
for i in range(viewer.count()):
|
||||||
|
if viewer.item(i).checkState() == Qt.Checked:
|
||||||
|
fmts.append(unicode(viewer.item(i).text()))
|
||||||
|
return fmts
|
||||||
|
# }}}
|
||||||
|
|
||||||
def restore_defaults(self):
|
def restore_defaults(self):
|
||||||
ConfigWidgetBase.restore_defaults(self)
|
ConfigWidgetBase.restore_defaults(self)
|
||||||
self.filename_pattern.initialize(defaults=True)
|
self.filename_pattern.initialize(defaults=True)
|
||||||
|
self.init_blocked_auto_formats(defaults=True)
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
path = unicode(self.opt_auto_add_path.text()).strip()
|
path = unicode(self.opt_auto_add_path.text()).strip()
|
||||||
@ -80,7 +122,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
return
|
return
|
||||||
pattern = self.filename_pattern.commit()
|
pattern = self.filename_pattern.commit()
|
||||||
prefs['filename_pattern'] = pattern
|
prefs['filename_pattern'] = pattern
|
||||||
return ConfigWidgetBase.commit(self)
|
fmts = self.current_blocked_auto_formats
|
||||||
|
old = gprefs['blocked_auto_formats']
|
||||||
|
changed = set(fmts) != set(old)
|
||||||
|
if changed:
|
||||||
|
gprefs['blocked_auto_formats'] = self.current_blocked_auto_formats
|
||||||
|
ret = ConfigWidgetBase.commit(self)
|
||||||
|
return changed or ret
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from PyQt4.Qt import QApplication
|
from PyQt4.Qt import QApplication
|
||||||
|
@ -150,8 +150,8 @@ Author matching is exact.</string>
|
|||||||
<attribute name="title">
|
<attribute name="title">
|
||||||
<string>&Automatic Adding</string>
|
<string>&Automatic Adding</string>
|
||||||
</attribute>
|
</attribute>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
<layout class="QGridLayout" name="gridLayout_3">
|
||||||
<item>
|
<item row="0" column="0" colspan="2">
|
||||||
<widget class="QLabel" name="label">
|
<widget class="QLabel" name="label">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Specify a folder. Any files you put into this folder will be automatically added to calibre (restart required).</string>
|
<string>Specify a folder. Any files you put into this folder will be automatically added to calibre (restart required).</string>
|
||||||
@ -161,7 +161,46 @@ Author matching is exact.</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="2" column="0" colspan="2">
|
||||||
|
<widget class="QLabel" name="label_2">
|
||||||
|
<property name="text">
|
||||||
|
<string><b>WARNING:</b> Files in the above folder will be deleted after being added to calibre.</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="4" column="0">
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="title">
|
||||||
|
<string>Ignore files with the following extensions when automatically adding </string>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||||
<item>
|
<item>
|
||||||
|
<widget class="QListWidget" name="opt_blocked_auto_formats">
|
||||||
|
<property name="alternatingRowColors">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="selectionMode">
|
||||||
|
<enum>QAbstractItemView::NoSelection</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="4" column="1">
|
||||||
|
<spacer name="horizontalSpacer_2">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>272</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" colspan="2">
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLineEdit" name="opt_auto_add_path">
|
<widget class="QLineEdit" name="opt_auto_add_path">
|
||||||
@ -179,21 +218,14 @@ Author matching is exact.</string>
|
|||||||
<string>...</string>
|
<string>...</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="icon">
|
<property name="icon">
|
||||||
<iconset resource="../../../work/calibre/resources/images.qrc">
|
<iconset resource="../../../../resources/images.qrc">
|
||||||
<normaloff>:/images/document_open.png</normaloff>:/images/document_open.png</iconset>
|
<normaloff>:/images/document_open.png</normaloff>:/images/document_open.png</iconset>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item row="3" column="0" colspan="2">
|
||||||
<widget class="QLabel" name="label_2">
|
|
||||||
<property name="text">
|
|
||||||
<string><b>WARNING:</b> Files in the above folder will be deleted after being added to calibre.</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QCheckBox" name="opt_auto_add_check_for_duplicates">
|
<widget class="QCheckBox" name="opt_auto_add_check_for_duplicates">
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
<string>If set, this option will causes calibre to check if a file
|
<string>If set, this option will causes calibre to check if a file
|
||||||
@ -206,19 +238,6 @@ Author matching is exact.</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
|
||||||
<spacer name="verticalSpacer_2">
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Vertical</enum>
|
|
||||||
</property>
|
|
||||||
<property name="sizeHint" stdset="0">
|
|
||||||
<size>
|
|
||||||
<width>20</width>
|
|
||||||
<height>40</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
</spacer>
|
|
||||||
</item>
|
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
@ -226,7 +245,7 @@ Author matching is exact.</string>
|
|||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
<resources>
|
<resources>
|
||||||
<include location="../../../work/calibre/resources/images.qrc"/>
|
<include location="../../../../resources/images.qrc"/>
|
||||||
</resources>
|
</resources>
|
||||||
<connections>
|
<connections>
|
||||||
<connection>
|
<connection>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user