From 087f3d855e26d0e9540f21ad6e7e909d994a54c0 Mon Sep 17 00:00:00 2001 From: Terisa de morgan Date: Mon, 6 Nov 2017 12:45:49 +0100 Subject: [PATCH] Configuration stored in database for bib, csv, and xml catalog --- src/calibre/gui2/catalog/catalog_bibtex.py | 57 +++++++--- src/calibre/gui2/catalog/catalog_csv_xml.py | 43 +++++++- src/calibre/gui2/catalog/config.py | 161 ++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+), 17 deletions(-) create mode 100644 src/calibre/gui2/catalog/config.py diff --git a/src/calibre/gui2/catalog/catalog_bibtex.py b/src/calibre/gui2/catalog/catalog_bibtex.py index 7d3455827d..a75b39c5b6 100644 --- a/src/calibre/gui2/catalog/catalog_bibtex.py +++ b/src/calibre/gui2/catalog/catalog_bibtex.py @@ -8,33 +8,39 @@ __docformat__ = 'restructuredtext en' from calibre.gui2 import gprefs +from calibre.gui2.ui import get_gui from calibre.gui2.catalog.catalog_bibtex_ui import Ui_Form from PyQt5.Qt import QWidget, QListWidgetItem - +import calibre.gui2.catalog.config as cfg class PluginWidget(QWidget, Ui_Form): TITLE = _('BibTeX options') HELP = _('Options specific to')+' BibTeX '+_('output') - OPTION_FIELDS = [('bib_cit','{authors}{id}'), - ('bib_entry', 0), # mixed - ('bibfile_enc', 0), # utf-8 - ('bibfile_enctag', 0), # strict - ('impcit', True), - ('addfiles', False), + OPTION_FIELDS = [('bib_cit','{authors}{id}'), # Citation template + ('bib_entry', 0), # mixed, Entry type + ('bibfile_enc', 0), # utf-8, Bibtex file encoding output + ('bibfile_enctag', 0), # strict, Bibtex file encofing flag + ('impcit', True), # Create citation + ('addfiles', False) # Add files path ] sync_enabled = False formats = set(['bib']) + library_config = None def __init__(self, parent=None): QWidget.__init__(self, parent) self.setupUi(self) + self.library_config = None - def initialize(self, name, db): # not working properly to update + def initialize(self, name, db): from calibre.library.catalogs import FIELDS self.all_fields = [x for x in FIELDS if x != 'all'] + db = get_gui().current_db + self.library_config = cfg.get_library_config (db) + # add custom columns for x in sorted(db.custom_field_keys()): self.all_fields.append(x) @@ -45,7 +51,8 @@ class PluginWidget(QWidget, Ui_Form): QListWidgetItem(x, self.db_fields) self.name = name - fields = gprefs.get(name+'_db_fields', self.all_fields) + #fields = gprefs.get(name+'_db_fields', self.all_fields) + fields = cfg.get_fields (self.library_config, self.name, self.all_fields) # Restore the activated db_fields from last use for x in xrange(self.db_fields.count()): item = self.db_fields.item(x) @@ -59,7 +66,9 @@ class PluginWidget(QWidget, Ui_Form): self.bib_entry.addItems(['mixed', 'misc', 'book']) # Update dialog fields from stored options for opt in self.OPTION_FIELDS: - opt_value = gprefs.get(self.name + '_' + opt[0], opt[1]) + #opt_value = gprefs.get(self.name + '_' + opt[0], opt[1]) # check + #opt_value = cfg.get_value (self.library_config, self.name, opt[0], opt[1]) + opt_value = self.get_value (opt[0], opt[1]) if opt[0] in ['bibfile_enc', 'bibfile_enctag', 'bib_entry']: getattr(self, opt[0]).setCurrentIndex(opt_value) elif opt[0] in ['impcit', 'addfiles'] : @@ -70,12 +79,15 @@ class PluginWidget(QWidget, Ui_Form): def options(self): # Save the currently activated fields + db = get_gui().current_db + self.library_config = cfg.get_library_config (db) fields = [] for x in xrange(self.db_fields.count()): item = self.db_fields.item(x) if item.isSelected(): fields.append(unicode(item.text())) - gprefs.set(self.name+'_db_fields', fields) + # gprefs.set(self.name+'_db_fields', fields) + cfg.set_fields (db, self.library_config, self.name, fields) # Dictionary currently activated fields if len(self.db_fields.selectedItems()): @@ -93,8 +105,29 @@ class PluginWidget(QWidget, Ui_Form): opt_value = getattr(self, opt[0]).isChecked() else : opt_value = unicode(getattr(self, opt[0]).text()) - gprefs.set(self.name + '_' + opt[0], opt_value) + #gprefs.set(self.name + '_' + opt[0], opt_value) # check + #cfg.set_value (db, self.library_config, self.name, opt[0], opt_value) + self.set_value (db, opt[0], opt_value) opts_dict[opt[0]] = opt_value return opts_dict + + def get_value (self, attr, val_def): + val = None + + try: + val = None + val = self.library_config[cfg.CATALOG_FIELDS][self.name+'_'+attr] + + if (val == None): + val = val_def + except: + val = gprefs.get(self.name+'_'+attr, val_def) + + return val + + def set_value (self, db, attr, value): + if (self.library_config): + self.library_config[cfg.CATALOG_FIELDS][self.name + '_' + attr] = value + cfg.set_library_config (db, self.library_config) \ No newline at end of file diff --git a/src/calibre/gui2/catalog/catalog_csv_xml.py b/src/calibre/gui2/catalog/catalog_csv_xml.py index 627f6ec51f..9f10904c84 100644 --- a/src/calibre/gui2/catalog/catalog_csv_xml.py +++ b/src/calibre/gui2/catalog/catalog_csv_xml.py @@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en' from calibre.gui2 import gprefs from calibre.gui2.ui import get_gui from PyQt5.Qt import QWidget, QListWidgetItem, Qt, QVBoxLayout, QLabel, QListWidget - +import calibre.gui2.catalog.config as cfg class PluginWidget(QWidget): @@ -17,6 +17,7 @@ class PluginWidget(QWidget): HELP = _('Options specific to')+' CSV/XML '+_('output') sync_enabled = False formats = set(['csv', 'xml']) + library_config = None handles_scrolling = True def __init__(self, parent=None): @@ -39,8 +40,10 @@ class PluginWidget(QWidget): self.name = catalog_name from calibre.library.catalogs import FIELDS db = get_gui().current_db + self.library_config = cfg.get_library_config(db) self.all_fields = {x for x in FIELDS if x != 'all'} | set(db.custom_field_keys()) - sort_order = gprefs.get(self.name + '_db_fields_sort_order', {}) + sort_order = self.get_sort_order (db) +# sort_order = gprefs.get(self.name + '_db_fields_sort_order', {}) fm = db.field_metadata def name(x): @@ -63,24 +66,54 @@ class PluginWidget(QWidget): QListWidgetItem(name(x) + ' (%s)' % x, self.db_fields).setData(Qt.UserRole, x) # Restore the activated fields from last use - fields = frozenset(gprefs.get(self.name+'_db_fields', self.all_fields)) + #fields = frozenset(self.get_fields(db)) + fields = frozenset(cfg.get_fields (self.library_config, self.name, self.all_fields)) + #fields = frozenset(gprefs.get(self.name+'_db_fields', self.all_fields)) for x in range(self.db_fields.count()): item = self.db_fields.item(x) item.setCheckState(Qt.Checked if unicode(item.data(Qt.UserRole)) in fields else Qt.Unchecked) def options(self): # Save the currently activated fields + db = get_gui().current_db fields, all_fields = [], [] for x in xrange(self.db_fields.count()): item = self.db_fields.item(x) all_fields.append(unicode(item.data(Qt.UserRole))) if item.checkState() == Qt.Checked: fields.append(unicode(item.data(Qt.UserRole))) - gprefs.set(self.name+'_db_fields', fields) - gprefs.set(self.name + '_db_fields_sort_order', {x:i for i, x in enumerate(all_fields)}) + + #gprefs.set(self.name+'_db_fields', fields) + #gprefs.set(self.name + '_db_fields_sort_order', {x:i for i, x in enumerate(all_fields)}) + + #self.set_fields (db, fields) + cfg.set_fields (db, self.library_config, self.name, fields) + self.set_sort_order(db, {x:i for i, x in enumerate(all_fields)}) # Return a dictionary with current options for this widget if len(fields): return {'fields':fields} else: return {'fields':['all']} + + def get_sort_order (self, db): + if not(self.library_config): + self.library_config = cfg.get_library_config(db) + + if (self.library_config): + sort_order = self.library_config[cfg.CATALOG_ORDER][self.name + '_db_fields_sort_order'] + else: + sort_order = None + + if not sort_order: + sort_order = gprefs.get(self.name + '_db_fields_sort_order', {}) + + return sort_order + + def set_sort_order (self, db, order): + if not (self.library_config): + self.library_config = cfg.get_library_config (db) + + if (self.library_config): + self.library_config[cfg.CATALOG_ORDER][self.name + '_db_fields_sort_order'] = order + cfg.set_library_config (db, self.library_config) \ No newline at end of file diff --git a/src/calibre/gui2/catalog/config.py b/src/calibre/gui2/catalog/config.py new file mode 100644 index 0000000000..272ddf8062 --- /dev/null +++ b/src/calibre/gui2/catalog/config.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2011, Grant Drake ' +__docformat__ = 'restructuredtext en' + +import copy, traceback + +try: + from PyQt5 import QtCore + from PyQt5 import QtWidgets as QtGui + from PyQt5.Qt import (Qt, QWidget, QGridLayout, QLabel, QPushButton, QVBoxLayout, QSpinBox, + QGroupBox, QCheckBox, QLineEdit, QTabWidget, QTableWidget, QAbstractItemView, + QHBoxLayout, QIcon, QInputDialog, QComboBox) +except ImportError: + from PyQt4 import QtGui, QtCore + from PyQt4.Qt import (Qt, QWidget, QGridLayout, QLabel, QPushButton, QVBoxLayout, QSpinBox, + QGroupBox, QCheckBox, QLineEdit, QTabWidget, QTableWidget, QAbstractItemView, + QHBoxLayout, QIcon, QInputDialog, QComboBox) + +from calibre.gui2 import open_url, choose_dir, error_dialog, question_dialog, gprefs +from calibre.gui2.dialogs.confirm_delete import confirm +from calibre.utils.config import JSONConfig +from calibre.constants import DEBUG as _DEBUG + +from calibre_plugins.koboutilities.common_utils import (get_library_uuid, debug_print, get_icon, + CustomColumnComboBox, KeyboardConfigDialog, KeyComboBox, ImageTitleLayout, + PrefsViewerDialog, ReadOnlyTextIconWidgetItem, CheckableTableWidgetItem, + ReadOnlyTableWidgetItem, ProfileComboBox, + convert_qvariant) + +# Redefine the debug here so the jobs can see it. +DEBUG = _DEBUG + +PREFS_NAMESPACE = 'catalog' +PREFS_KEY_SETTINGS = 'settings' + +KEY_SCHEMA_VERSION = 'SchemaVersion' +DEFAULT_SCHEMA_VERSION = 0.1 + +CATALOG_FIELDS = 'catalogFields' +CATALOG_ORDER = 'catalogOrder' + +KEY_CSV = 'catalog_csv_xml_db_fields' +KEY_CSV_ORDER = 'catalog_csv_xml_db_fields_sort_order' + +DEFAULT_FIELD_VALUES = { + KEY_CSV: {} + } + +DEFAULT_ORDER_VALUES = { + KEY_CSV_ORDER: {} + } + +DEFAULT_LIBRARY_VALUES = { + CATALOG_FIELDS: DEFAULT_FIELD_VALUES, + CATALOG_ORDER: DEFAULT_ORDER_VALUES + } + + +# This is where all preferences for this plugin will be stored +plugin_prefs = JSONConfig('catalog') + +# Set defaults +plugin_prefs.defaults[CATALOG_FIELDS] = DEFAULT_FIELD_VALUES +plugin_prefs.defaults[CATALOG_ORDER] = DEFAULT_ORDER_VALUES + +def get_plugin_pref(store_name, option): + debug_print("get_plugin_pref - start - store_name='%s', option='%s'" % (store_name, option)) + c = plugin_prefs[store_name] + + default_value = plugin_prefs.defaults[store_name][option] + + return c.get(option, default_value) + +def get_plugin_prefs(store_name, fill_defaults=False): + if fill_defaults: + c = get_prefs(plugin_prefs, store_name) + else: + c = plugin_prefs[store_name] + return c + +def get_prefs(prefs_store, store_name): + debug_print("get_prefs - start - store_name='%s'" % (store_name, )) +# debug_print("get_prefs - start - prefs_store='%s'" % (prefs_store,)) + store = {} + if prefs_store and prefs_store[store_name]: + for key in plugin_prefs.defaults[store_name].keys(): + store[key] = prefs_store[store_name].get(key, plugin_prefs.defaults[store_name][key]) + else: + store = plugin_prefs.defaults[store_name] + return store + +def get_pref(store, store_name, option, defaults=None): + debug_print("get_pref - start - store_name='%s'" % (store_name, )) +# debug_print("get_pref - start - option='%s'" % (option,)) + if defaults: + default_value = defaults[option] + else: + default_value = plugin_prefs.defaults[store_name][option] + return store.get(option, default_value) + +def get_library_config(db): + library_config = None + + if library_config is None: + library_config = db.prefs.get_namespaced(PREFS_NAMESPACE, PREFS_KEY_SETTINGS, + copy.deepcopy(DEFAULT_LIBRARY_VALUES)) + #debug_print("get_library_config - library_config:", library_config) + return library_config + +def set_library_config(db, library_config): + #debug_print("set_library_config - library_config:", library_config) + db.prefs.set_namespaced(PREFS_NAMESPACE, PREFS_KEY_SETTINGS, library_config) + +def get_fields (library_config, name, all_fields): + + try: + fields = library_config[CATALOG_FIELDS][name+'_db_fields'] + if not (fields): + fields = gprefs.get(name+'_db_fields', all_fields) + except: + fields = gprefs.get(name+'_db_fields', all_fields) + + return fields + + +def set_fields (db, library_config, name, fields): + if (library_config): + library_config[CATALOG_FIELDS][name+'_db_fields'] = fields + set_library_config (db, library_config) + +def get_value (library_config, name, attr, val_def): + val = None + + if (library_config): + try: + val = library_config[CATALOG_FIELDS][name+'_'+attr] + + if not (val): + val = gprefs.get(name+'_'+attr, val_def) + except: + val = gprefs.get(name+'_'+attr, val_def) + else: + val = gprefs.get(name+'_'+attr, val_def) + + if (not val): + val = val_def + + return val + +def set_value (db, library_config, name, attr, value): + + if (library_config): + library_config[CATALOG_FIELDS][name + '_' + attr] = value + set_library_config (db, library_config) + + -- 2.14.2