From 6894033f8dd9ed4f370c8c2d03fa791e0b4cddd3 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 23 Jan 2016 22:35:19 +0000 Subject: [PATCH 1/2] lineparser: Add clear() method. The lineparser clear method, implemented for all lineparser subclasses, clears the underlying file and also empties any lineparser data structures. --- qutebrowser/misc/lineparser.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/qutebrowser/misc/lineparser.py b/qutebrowser/misc/lineparser.py index fd8ef2589..59eba66e0 100644 --- a/qutebrowser/misc/lineparser.py +++ b/qutebrowser/misc/lineparser.py @@ -125,6 +125,10 @@ class BaseLineParser(QObject): """Save the history to disk.""" raise NotImplementedError + def clear(self): + """Clear the contents of the file.""" + raise NotImplementedError + class AppendLineParser(BaseLineParser): @@ -183,6 +187,15 @@ class AppendLineParser(BaseLineParser): self.new_data = [] self._after_save() + def clear(self): + do_save = self._prepare_save() + if not do_save: + return + with self._open('w') as f: + pass + self.new_data = [] + self._after_save() + class LineParser(BaseLineParser): @@ -237,6 +250,9 @@ class LineParser(BaseLineParser): self._opened = False self._after_save() + def clear(self): + self.data = [] + self.save() class LimitLineParser(LineParser): @@ -289,3 +305,7 @@ class LimitLineParser(LineParser): with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data[-limit:]) self._after_save() + + def clear(self): + self.data = [] + self.save() From 399aaa2b70f63a1cdfa17abad1bd0bac0040f7aa Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 23 Jan 2016 22:36:24 +0000 Subject: [PATCH 2/2] history: Add clear() method and history-clear command WebHistory now has a clear() method which is also a command (history-clear) which clears the qutebrowser history using the new lineparser clear() method and emits a cleared signal. The completion model urlmodel connects to the WebHistory.cleared signal and clears its history category completion list. I am adding this as a temporary fix before #58 or #1051 get implemented. --- qutebrowser/browser/history.py | 13 +++++++++++++ qutebrowser/completion/models/urlmodel.py | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/qutebrowser/browser/history.py b/qutebrowser/browser/history.py index 050220d47..b078b4ba8 100644 --- a/qutebrowser/browser/history.py +++ b/qutebrowser/browser/history.py @@ -25,6 +25,7 @@ import collections from PyQt5.QtCore import pyqtSignal, QUrl from PyQt5.QtWebKit import QWebHistoryInterface +from qutebrowser.commands import cmdutils from qutebrowser.utils import utils, objreg, standarddir, log from qutebrowser.config import config from qutebrowser.misc import lineparser @@ -72,10 +73,12 @@ class WebHistory(QWebHistoryInterface): arg: The new HistoryEntry. item_added: Emitted after a new HistoryEntry is added. arg: The new HistoryEntry. + cleared: Emitted after the history is cleared. """ add_completion_item = pyqtSignal(HistoryEntry) item_added = pyqtSignal(HistoryEntry) + cleared = pyqtSignal() async_read_done = pyqtSignal() def __init__(self, parent=None): @@ -169,6 +172,16 @@ class WebHistory(QWebHistoryInterface): self._lineparser.save() self._saved_count = len(self._new_history) + @cmdutils.register(name='history-clear', instance='web-history') + def clear(self): + """Clear all history entries.""" + self._lineparser.clear() + self._history_dict.clear() + self._temp_history.clear() + self._new_history.clear() + self._saved_count = 0 + self.cleared.emit() + def addHistoryEntry(self, url_string): """Called by WebKit when an URL should be added to the history. diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index b31ab9809..9e125cb7c 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -76,6 +76,7 @@ class UrlCompletionModel(base.BaseCompletionModel): self._add_history_entry(entry) self._history.add_completion_item.connect( self.on_history_item_added) + self._history.cleared.connect(self.on_history_cleared) objreg.get('config').changed.connect(self.reformat_timestamps) @@ -130,6 +131,10 @@ class UrlCompletionModel(base.BaseCompletionModel): else: self._add_history_entry(entry) + @pyqtSlot() + def on_history_cleared(self): + self._history_cat.removeRows(0, self._history_cat.rowCount()) + def _remove_item(self, data, category, column): """Helper function for on_quickmark_removed and on_bookmark_removed.