2015-02-01 17:34:16 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-02-06 00:21:57 +01:00
|
|
|
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-02-01 17:34:16 +01:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Simple history which gets written to disk."""
|
|
|
|
|
|
|
|
import time
|
2015-03-13 19:44:15 +01:00
|
|
|
import collections
|
2015-02-01 17:34:16 +01:00
|
|
|
|
2015-03-16 07:03:01 +01:00
|
|
|
from PyQt5.QtCore import pyqtSignal, QUrl
|
2015-02-01 17:34:16 +01:00
|
|
|
from PyQt5.QtWebKit import QWebHistoryInterface
|
|
|
|
|
2015-04-10 06:40:48 +02:00
|
|
|
from qutebrowser.utils import utils, objreg, standarddir, log
|
2015-02-01 17:34:16 +01:00
|
|
|
from qutebrowser.config import config
|
2015-03-02 20:07:26 +01:00
|
|
|
from qutebrowser.misc import lineparser
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryEntry:
|
|
|
|
|
|
|
|
"""A single entry in the web history.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
atime: The time the page was accessed.
|
2015-03-16 07:03:01 +01:00
|
|
|
url: The URL which was accessed as QUrl.
|
|
|
|
url_string: The URL which was accessed as string.
|
2015-02-01 17:34:16 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, atime, url):
|
2015-03-12 08:07:40 +01:00
|
|
|
self.atime = float(atime)
|
2015-03-16 07:03:01 +01:00
|
|
|
self.url = QUrl(url)
|
|
|
|
self.url_string = url
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return utils.get_repr(self, constructor=True, atime=self.atime,
|
2015-03-16 07:03:01 +01:00
|
|
|
url=self.url.toDisplayString())
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-03-16 07:03:01 +01:00
|
|
|
return '{} {}'.format(int(self.atime), self.url_string)
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_str(cls, s):
|
|
|
|
"""Get a history based on a 'TIME URL' string."""
|
2015-02-13 18:58:44 +01:00
|
|
|
return cls(*s.split(' ', maxsplit=1))
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WebHistory(QWebHistoryInterface):
|
|
|
|
|
2015-03-02 22:44:43 +01:00
|
|
|
"""A QWebHistoryInterface which supports being written to disk.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_lineparser: The AppendLineParser used to save the history.
|
2015-03-13 19:44:15 +01:00
|
|
|
_history_dict: An OrderedDict of URLs read from the on-disk history.
|
2015-03-02 22:44:43 +01:00
|
|
|
_new_history: A list of HistoryEntry items of the current session.
|
|
|
|
_saved_count: How many HistoryEntries have been written to disk.
|
2015-03-15 21:16:45 +01:00
|
|
|
|
|
|
|
Signals:
|
|
|
|
item_about_to_be_added: Emitted before a new HistoryEntry is added.
|
|
|
|
arg: The new HistoryEntry.
|
|
|
|
item_added: Emitted after a new HistoryEntry is added.
|
|
|
|
arg: The new HistoryEntry.
|
2015-03-02 22:44:43 +01:00
|
|
|
"""
|
2015-02-01 17:34:16 +01:00
|
|
|
|
2015-03-15 21:16:45 +01:00
|
|
|
item_about_to_be_added = pyqtSignal(HistoryEntry)
|
2015-03-09 03:33:05 +01:00
|
|
|
item_added = pyqtSignal(HistoryEntry)
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
2015-03-02 22:44:43 +01:00
|
|
|
self._lineparser = lineparser.AppendLineParser(
|
2015-02-26 07:01:22 +01:00
|
|
|
standarddir.data(), 'history', parent=self)
|
2015-03-13 19:44:15 +01:00
|
|
|
self._history_dict = collections.OrderedDict()
|
2015-03-02 22:44:43 +01:00
|
|
|
with self._lineparser.open():
|
|
|
|
for line in self._lineparser:
|
2015-03-17 06:16:26 +01:00
|
|
|
data = line.rstrip().split(maxsplit=1)
|
2015-03-16 10:45:50 +01:00
|
|
|
if not data:
|
|
|
|
# empty line
|
|
|
|
continue
|
2015-04-10 06:40:48 +02:00
|
|
|
elif len(data) != 2:
|
|
|
|
# other malformed line
|
|
|
|
log.init.warning("Invalid history entry {!r}!".format(
|
|
|
|
line))
|
|
|
|
continue
|
2015-03-17 06:16:26 +01:00
|
|
|
atime, url = data
|
2015-05-13 23:46:22 +02:00
|
|
|
if atime.startswith('\0'):
|
|
|
|
log.init.warning(
|
|
|
|
"Removing NUL bytes from entry {!r} - see "
|
|
|
|
"https://github.com/The-Compiler/qutebrowser/issues/"
|
|
|
|
"670".format(data))
|
|
|
|
atime = atime.lstrip('\0')
|
2015-03-12 08:12:59 +01:00
|
|
|
# This de-duplicates history entries; only the latest
|
|
|
|
# entry for each URL is kept. If you want to keep
|
|
|
|
# information about previous hits change the items in
|
|
|
|
# old_urls to be lists or change HistoryEntry to have a
|
|
|
|
# list of atimes.
|
2015-03-12 09:35:56 +01:00
|
|
|
self._history_dict[url] = HistoryEntry(atime, url)
|
2015-03-16 07:54:39 +01:00
|
|
|
self._history_dict.move_to_end(url)
|
2015-03-02 22:44:43 +01:00
|
|
|
self._new_history = []
|
|
|
|
self._saved_count = 0
|
|
|
|
objreg.get('save-manager').add_saveable(
|
2015-03-09 03:33:05 +01:00
|
|
|
'history', self.save, self.item_added)
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2015-03-13 19:45:43 +01:00
|
|
|
return utils.get_repr(self, length=len(self))
|
2015-02-01 17:34:16 +01:00
|
|
|
|
2015-02-01 23:12:02 +01:00
|
|
|
def __getitem__(self, key):
|
2015-03-02 22:44:43 +01:00
|
|
|
return self._new_history[key]
|
|
|
|
|
2015-03-05 04:44:46 +01:00
|
|
|
def __iter__(self):
|
2015-03-12 09:35:56 +01:00
|
|
|
return iter(self._history_dict.values())
|
2015-03-05 04:44:46 +01:00
|
|
|
|
2015-03-13 19:45:43 +01:00
|
|
|
def __len__(self):
|
|
|
|
return len(self._history_dict)
|
|
|
|
|
2015-03-02 22:44:43 +01:00
|
|
|
def get_recent(self):
|
|
|
|
"""Get the most recent history entries."""
|
|
|
|
old = self._lineparser.get_recent()
|
|
|
|
return old + [str(e) for e in self._new_history]
|
2015-02-01 23:12:02 +01:00
|
|
|
|
2015-02-01 17:34:16 +01:00
|
|
|
def save(self):
|
|
|
|
"""Save the history to disk."""
|
2015-03-02 22:44:43 +01:00
|
|
|
new = (str(e) for e in self._new_history[self._saved_count:])
|
|
|
|
self._lineparser.new_data = new
|
2015-03-02 20:07:26 +01:00
|
|
|
self._lineparser.save()
|
2015-03-02 22:44:43 +01:00
|
|
|
self._saved_count = len(self._new_history)
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def addHistoryEntry(self, url_string):
|
|
|
|
"""Called by WebKit when an URL should be added to the history.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url_string: An url as string to add to the history.
|
|
|
|
"""
|
2015-04-02 09:09:17 +02:00
|
|
|
if not url_string:
|
|
|
|
return
|
2015-02-01 17:34:16 +01:00
|
|
|
if not config.get('general', 'private-browsing'):
|
|
|
|
entry = HistoryEntry(time.time(), url_string)
|
2015-03-15 21:16:45 +01:00
|
|
|
self.item_about_to_be_added.emit(entry)
|
2015-03-02 22:44:43 +01:00
|
|
|
self._new_history.append(entry)
|
2015-03-12 09:35:56 +01:00
|
|
|
self._history_dict[url_string] = entry
|
2015-03-13 19:44:15 +01:00
|
|
|
self._history_dict.move_to_end(url_string)
|
2015-03-09 03:33:05 +01:00
|
|
|
self.item_added.emit(entry)
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
def historyContains(self, url_string):
|
|
|
|
"""Called by WebKit to determine if an URL is contained in the history.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url_string: The URL (as string) to check for.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if the url is in the history, False otherwise.
|
|
|
|
"""
|
2015-03-12 09:35:56 +01:00
|
|
|
return url_string in self._history_dict
|
2015-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
|
2015-04-06 00:10:37 +02:00
|
|
|
def init(parent=None):
|
|
|
|
"""Initialize the web history.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
parent: The parent to use for WebHistory.
|
|
|
|
"""
|
|
|
|
history = WebHistory(parent)
|
2015-02-01 17:34:16 +01:00
|
|
|
objreg.register('web-history', history)
|
|
|
|
QWebHistoryInterface.setDefaultInterface(history)
|