WebBrowser/History/HistoryManager.py

changeset 5707
1273cd92a8f4
parent 5389
9b1c800daff3
child 6048
82ad8ec9548c
equal deleted inserted replaced
5706:59458b006d76 5707:1273cd92a8f4
19 19
20 from Utilities.AutoSaver import AutoSaver 20 from Utilities.AutoSaver import AutoSaver
21 import Utilities 21 import Utilities
22 import Preferences 22 import Preferences
23 23
24 HISTORY_VERSION = 42 24 HISTORY_VERSION_42 = 42
25 HISTORY_VERSION_60 = 60
26 HISTORY_VERSIONS = [HISTORY_VERSION_60, HISTORY_VERSION_42]
25 27
26 28
27 class HistoryEntry(object): 29 class HistoryEntry(object):
28 """ 30 """
29 Class implementing a history entry. 31 Class implementing a history entry.
30 """ 32 """
31 def __init__(self, url=None, dateTime=None, title=None): 33 def __init__(self, url=None, dateTime=None, title=None, visitCount=None):
32 """ 34 """
33 Constructor 35 Constructor
34 36
35 @param url URL of the history entry (string) 37 @param url URL of the history entry (string)
36 @param dateTime date and time this entry was created (QDateTime) 38 @param dateTime date and time this entry was created (QDateTime)
37 @param title title string for the history entry (string) 39 @param title title string for the history entry (string)
40 @param visitCount number of visits of this URL (int)
38 """ 41 """
39 self.url = url and url or "" 42 self.url = url and url or ""
40 self.dateTime = dateTime and dateTime or QDateTime() 43 self.dateTime = dateTime and dateTime or QDateTime()
41 self.title = title and title or "" 44 self.title = title and title or ""
45 self.visitCount = visitCount and visitCount or 0
42 46
43 def __eq__(self, other): 47 def __eq__(self, other):
44 """ 48 """
45 Special method determining equality. 49 Special method determining equality.
46 50
74 page = QFileInfo(QUrl(self.url).path()).fileName() 78 page = QFileInfo(QUrl(self.url).path()).fileName()
75 if page: 79 if page:
76 return page 80 return page
77 return self.url 81 return self.url
78 return self.title 82 return self.title
83
84 def isValid(self):
85 """
86 Public method to determine validity.
87
88 @return flag indicating validity
89 @rtype bool
90 """
91 return bool(self.url) and self.dateTime.isValid()
79 92
80 93
81 class HistoryManager(QObject): 94 class HistoryManager(QObject):
82 """ 95 """
83 Class implementing the history manager. 96 Class implementing the history manager.
176 else: 189 else:
177 self.__lastSavedUrl = "" 190 self.__lastSavedUrl = ""
178 self.__saveTimer.changeOccurred() 191 self.__saveTimer.changeOccurred()
179 self.historyReset.emit() 192 self.historyReset.emit()
180 193
194 def __findFirstHistoryEntry(self, url):
195 """
196 Private method to find the first entry for the given URL.
197
198 @param url URL to search for
199 @type str
200 @return first entry for the given URL
201 @rtype HistoryEntry
202 """
203 for index in range(len(self.__history)):
204 if url == self.__history[index].url:
205 return self.__history[index]
206
207 # not found, return an empty entry
208 return HistoryEntry()
209
210 def __updateVisitCount(self, url, count):
211 """
212 Private method to update the visit count for all entries of the
213 given URL.
214
215 @param url URL to be updated
216 @type str
217 @param count new visit count
218 @type int
219 """
220 for index in range(len(self.__history)):
221 if url == self.__history[index].url:
222 self.__history[index].visitCount = count
223
181 def addHistoryEntry(self, view): 224 def addHistoryEntry(self, view):
182 """ 225 """
183 Public method to add a history entry. 226 Public method to add a history entry.
184 227
185 @param view reference to the view to add an entry for 228 @param view reference to the view to add an entry for
190 return 233 return
191 234
192 url = view.url() 235 url = view.url()
193 title = view.title() 236 title = view.title()
194 237
195 if url.scheme() not in ["eric", "about", "data"]: 238 if url.scheme() not in ["eric", "about", "data", "chrome"]:
196 if url.password(): 239 cleanUrlStr = self.__cleanUrlStr(url)
197 # don't save the password in the history 240 firstEntry = self.__findFirstHistoryEntry(cleanUrlStr)
198 url.setPassword("") 241 if firstEntry.isValid():
199 if url.host(): 242 visitCount = firstEntry.visitCount + 1
200 url.setHost(url.host().lower()) 243 self.__updateVisitCount(cleanUrlStr, visitCount)
201 itm = HistoryEntry(url.toString(), 244 else:
245 visitCount = 1
246 itm = HistoryEntry(cleanUrlStr,
202 QDateTime.currentDateTime(), 247 QDateTime.currentDateTime(),
203 title) 248 title,
249 visitCount)
204 self.__history.insert(0, itm) 250 self.__history.insert(0, itm)
205 self.entryAdded.emit(itm) 251 self.entryAdded.emit(itm)
206 if len(self.__history) == 1: 252 if len(self.__history) == 1:
207 self.__checkForExpired() 253 self.__checkForExpired()
208 254
211 Public method to update a history entry. 257 Public method to update a history entry.
212 258
213 @param url URL of the entry to update (string) 259 @param url URL of the entry to update (string)
214 @param title title of the entry to update (string) 260 @param title title of the entry to update (string)
215 """ 261 """
216 cleanurl = QUrl(url) 262 if QUrl(url).scheme() not in ["eric", "about", "data", "chrome"]:
217 if cleanurl.scheme() not in ["eric", "about"]: 263 cleanUrlStr = self.__cleanUrlStr(QUrl(url))
218 for index in range(len(self.__history)): 264 for index in range(len(self.__history)):
219 if url == self.__history[index].url: 265 if cleanUrlStr == self.__history[index].url:
220 self.__history[index].title = title 266 self.__history[index].title = title
221 self.__saveTimer.changeOccurred() 267 self.__saveTimer.changeOccurred()
222 if not self.__lastSavedUrl: 268 if not self.__lastSavedUrl:
223 self.__lastSavedUrl = self.__history[index].url 269 self.__lastSavedUrl = self.__history[index].url
224 self.entryUpdated.emit(index) 270 self.entryUpdated.emit(index)
229 Public method to remove a history entry. 275 Public method to remove a history entry.
230 276
231 @param url URL of the entry to remove (QUrl) 277 @param url URL of the entry to remove (QUrl)
232 @param title title of the entry to remove (string) 278 @param title title of the entry to remove (string)
233 """ 279 """
234 for index in range(len(self.__history)): 280 if url.scheme() not in ["eric", "about", "data", "chrome"]:
235 if url == QUrl(self.__history[index].url) and \ 281 cleanUrlStr = self.__cleanUrlStr(url)
236 (not title or title == self.__history[index].title): 282 for index in range(len(self.__history)):
237 itm = self.__history[index] 283 if cleanUrlStr == self.__history[index].url and \
238 self.__lastSavedUrl = "" 284 (not title or title == self.__history[index].title):
239 self.__history.remove(itm) 285 itm = self.__history[index]
240 self.entryRemoved.emit(itm) 286 self.__lastSavedUrl = ""
241 break 287 self.__history.remove(itm)
288 self.entryRemoved.emit(itm)
289 break
290
291 def __cleanUrl(self, url):
292 """
293 Private method to generate a clean URL usable for the history entry.
294
295 @param url original URL
296 @type QUrl
297 @return cleaned URL
298 @rtype QUrl
299 """
300 cleanurl = QUrl(url)
301 if cleanurl.password():
302 # don't save the password in the history
303 cleanurl.setPassword("")
304 if cleanurl.host():
305 # convert host to lower case
306 cleanurl.setHost(url.host().lower())
307
308 return cleanurl
309
310 def __cleanUrlStr(self, url):
311 """
312 Private method to generate a clean URL usable for the history entry.
313
314 @param url original URL
315 @type QUrl
316 @return cleaned URL
317 @rtype str
318 """
319 cleanurl = self.__cleanUrl(url)
320 return cleanurl.toString()
242 321
243 def historyModel(self): 322 def historyModel(self):
244 """ 323 """
245 Public method to get a reference to the history model. 324 Public method to get a reference to the history model.
246 325
381 data = QByteArray(historyFile.readAll()) 460 data = QByteArray(historyFile.readAll())
382 stream = QDataStream(data, QIODevice.ReadOnly) 461 stream = QDataStream(data, QIODevice.ReadOnly)
383 stream.setVersion(QDataStream.Qt_4_6) 462 stream.setVersion(QDataStream.Qt_4_6)
384 while not stream.atEnd(): 463 while not stream.atEnd():
385 ver = stream.readUInt32() 464 ver = stream.readUInt32()
386 if ver != HISTORY_VERSION: 465 if ver not in HISTORY_VERSIONS:
387 continue 466 continue
388 itm = HistoryEntry() 467 itm = HistoryEntry()
389 itm.url = Utilities.readStringFromStream(stream) 468 itm.url = Utilities.readStringFromStream(stream)
390 stream >> itm.dateTime 469 stream >> itm.dateTime
391 itm.title = Utilities.readStringFromStream(stream) 470 itm.title = Utilities.readStringFromStream(stream)
471 if ver == HISTORY_VERSION_60:
472 itm.visitCount = stream.readUInt32()
392 473
393 if not itm.dateTime.isValid(): 474 if not itm.dateTime.isValid():
394 continue 475 continue
395 476
396 if itm == lastInsertedItem: 477 if itm == lastInsertedItem:
397 if not lastInsertedItem.title and len(history) > 0: 478 if not lastInsertedItem.title and len(history) > 0:
398 history[0].title = itm.title 479 history[0].title = itm.title
399 continue 480 continue
481
482 if ver == HISTORY_VERSION_42:
483 firstEntry = self.__findFirstHistoryEntry(itm.url)
484 if firstEntry.isValid():
485 visitCount = firstEntry.visitCount + 1
486 self.__updateVisitCount(itm.url, visitCount)
487 else:
488 visitCount = 1
489 itm.visitCount = visitCount
400 490
401 if not needToSort and history and lastInsertedItem < itm: 491 if not needToSort and history and lastInsertedItem < itm:
402 needToSort = True 492 needToSort = True
403 493
404 history.insert(0, itm) 494 history.insert(0, itm)
456 for index in range(first, -1, -1): 546 for index in range(first, -1, -1):
457 data = QByteArray() 547 data = QByteArray()
458 stream = QDataStream(data, QIODevice.WriteOnly) 548 stream = QDataStream(data, QIODevice.WriteOnly)
459 stream.setVersion(QDataStream.Qt_4_6) 549 stream.setVersion(QDataStream.Qt_4_6)
460 itm = self.__history[index] 550 itm = self.__history[index]
461 stream.writeUInt32(HISTORY_VERSION) 551 stream.writeUInt32(HISTORY_VERSION_60)
462 stream.writeString(itm.url.encode("utf-8")) 552 stream.writeString(itm.url.encode("utf-8"))
463 stream << itm.dateTime 553 stream << itm.dateTime
464 stream.writeString(itm.title.encode('utf-8')) 554 stream.writeString(itm.title.encode('utf-8'))
555 stream.writeUInt32(itm.visitCount)
465 f.write(data) 556 f.write(data)
466 557
467 f.close() 558 f.close()
468 if saveAll: 559 if saveAll:
469 if historyFile.exists() and not historyFile.remove(): 560 if historyFile.exists() and not historyFile.remove():

eric ide

mercurial