fix filter saving failing when savedFilters is not set

This commit is contained in:
Damien Elmes 2021-02-05 22:38:44 +10:00
parent efd8c527a6
commit 5662939b70

View File

@ -551,7 +551,7 @@ class SidebarTreeView(QTreeView):
def _saved_searches_tree(self, root: SidebarItem) -> None: def _saved_searches_tree(self, root: SidebarItem) -> None:
icon = ":/icons/heart.svg" icon = ":/icons/heart.svg"
saved = self.col.get_config("savedFilters", {}) saved = self._get_saved_searches()
root = self._section_root( root = self._section_root(
root=root, root=root,
@ -1002,18 +1002,29 @@ class SidebarTreeView(QTreeView):
self.browser.model.beginReset() self.browser.model.beginReset()
self.mw.taskman.run_in_background(do_delete, on_done) self.mw.taskman.run_in_background(do_delete, on_done)
# Saved searches
##################
_saved_searches_key = "savedFilters"
def _get_saved_searches(self) -> Dict[str, str]:
return self.col.get_config(self._saved_searches_key, {})
def _set_saved_searches(self, searches: Dict[str, str]) -> None:
self.col.set_config(self._saved_searches_key, searches)
def remove_saved_search(self, item: SidebarItem) -> None: def remove_saved_search(self, item: SidebarItem) -> None:
name = item.name name = item.name
if not askUser(tr(TR.BROWSING_REMOVE_FROM_YOUR_SAVED_SEARCHES, val=name)): if not askUser(tr(TR.BROWSING_REMOVE_FROM_YOUR_SAVED_SEARCHES, val=name)):
return return
conf = self.col.get_config("savedFilters") conf = self._get_saved_searches()
del conf[name] del conf[name]
self.col.set_config("savedFilters", conf) self._set_saved_searches(conf)
self.refresh() self.refresh()
def rename_saved_search(self, item: SidebarItem) -> None: def rename_saved_search(self, item: SidebarItem) -> None:
old = item.name old = item.name
conf = self.col.get_config("savedFilters") conf = self._get_saved_searches()
try: try:
filt = conf[old] filt = conf[old]
except KeyError: except KeyError:
@ -1023,7 +1034,7 @@ class SidebarTreeView(QTreeView):
return return
conf[new] = filt conf[new] = filt
del conf[old] del conf[old]
self.col.set_config("savedFilters", conf) self._set_saved_searches(conf)
self.refresh() self.refresh()
def save_current_search(self, _item: Any = None) -> None: def save_current_search(self, _item: Any = None) -> None:
@ -1037,9 +1048,9 @@ class SidebarTreeView(QTreeView):
name = getOnlyText(tr(TR.BROWSING_PLEASE_GIVE_YOUR_FILTER_A_NAME)) name = getOnlyText(tr(TR.BROWSING_PLEASE_GIVE_YOUR_FILTER_A_NAME))
if not name: if not name:
return return
conf = self.col.get_config("savedFilters") conf = self._get_saved_searches()
conf[name] = filt conf[name] = filt
self.col.set_config("savedFilters", conf) self._set_saved_searches(conf)
self.refresh() self.refresh()
def manage_notetype(self, item: SidebarItem) -> None: def manage_notetype(self, item: SidebarItem) -> None: