Add titles to history entry.

Adds a title to the HistoryEntry class and includes it in the serialization
stuff. Not currently set from anywhere.

Not sure if anything more needs to be done to support non-ascii characters.
Everything works fine for me with unicode chars in url and title but
everything in my stack is utf-8.
This commit is contained in:
Jimmy 2016-03-20 13:03:14 +13:00
parent 0c1e82a103
commit 1524f29f71
2 changed files with 18 additions and 11 deletions

View File

@ -40,18 +40,18 @@ class HistoryEntry:
url_string: The URL which was accessed as string.
"""
def __init__(self, atime, url):
def __init__(self, atime, url, title):
self.atime = float(atime)
self.url = QUrl(url)
self.url_string = url
self.title = title
def __repr__(self):
return utils.get_repr(self, constructor=True, atime=self.atime,
url=self.url.toDisplayString())
url=self.url.toDisplayString(), title=self.title)
def __str__(self):
return '{} {}'.format(int(self.atime), self.url_string)
return '{} {} {}'.format(int(self.atime), self.url_string, self.title)
class WebHistory(QWebHistoryInterface):
@ -118,16 +118,20 @@ class WebHistory(QWebHistoryInterface):
with self._lineparser.open():
for line in self._lineparser:
yield
data = line.rstrip().split(maxsplit=1)
data = line.rstrip().split(maxsplit=2)
if not data:
# empty line
continue
elif len(data) != 2:
elif len(data) == 2:
atime, url = data
title = ""
elif len(data) == 3:
atime, url, title = data
else:
# other malformed line
log.init.warning("Invalid history entry {!r}!".format(
line))
continue
atime, url = data
if atime.startswith('\0'):
log.init.debug(
"Removing NUL bytes from entry {!r} - see "
@ -139,7 +143,7 @@ class WebHistory(QWebHistoryInterface):
# information about previous hits change the items in
# old_urls to be lists or change HistoryEntry to have a
# list of atimes.
entry = HistoryEntry(atime, url)
entry = HistoryEntry(atime, url, title)
self._add_entry(entry)
self._initial_read_done = True
@ -169,7 +173,7 @@ class WebHistory(QWebHistoryInterface):
self._lineparser.save()
self._saved_count = len(self._new_history)
def addHistoryEntry(self, url_string):
def addHistoryEntry(self, url_string, title=""):
"""Called by WebKit when an URL should be added to the history.
Args:
@ -179,7 +183,7 @@ class WebHistory(QWebHistoryInterface):
return
if config.get('general', 'private-browsing'):
return
entry = HistoryEntry(time.time(), url_string)
entry = HistoryEntry(time.time(), url_string, title)
if self._initial_read_done:
self.add_completion_item.emit(entry)
self._new_history.append(entry)

View File

@ -99,7 +99,8 @@ class UrlCompletionModel(base.BaseCompletionModel):
def _add_history_entry(self, entry):
"""Add a new history entry to the completion."""
self.new_item(self._history_cat, entry.url.toDisplayString(), "",
self.new_item(self._history_cat, entry.url.toDisplayString(),
entry.title,
self._fmt_atime(entry.atime), sort=int(entry.atime),
userdata=entry.url)
@ -122,9 +123,11 @@ class UrlCompletionModel(base.BaseCompletionModel):
for i in range(self._history_cat.rowCount()):
url_item = self._history_cat.child(i, self.URL_COLUMN)
atime_item = self._history_cat.child(i, self.TIME_COLUMN)
title_item = self._history_cat.child(i, self.TEXT_COLUMN)
url = url_item.data(base.Role.userdata)
if url == entry.url:
atime_item.setText(self._fmt_atime(entry.atime))
title_item.setText(entry.title)
url_item.setData(int(entry.atime), base.Role.sort)
break
else: