Helpviewer/History/HistoryManager.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2599
2373006616da
parent 3002
6ffc581f00f1
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
9 9
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 11
12 import os 12 import os
13 13
14 from PyQt4.QtCore import pyqtSignal, QFileInfo, QDateTime, QDate, QTime, QUrl, QTimer, \ 14 from PyQt4.QtCore import pyqtSignal, QFileInfo, QDateTime, QDate, QTime, \
15 QFile, QIODevice, QByteArray, QDataStream, QTemporaryFile 15 QUrl, QTimer, QFile, QIODevice, QByteArray, QDataStream, QTemporaryFile
16 from PyQt4.QtWebKit import QWebHistoryInterface, QWebSettings 16 from PyQt4.QtWebKit import QWebHistoryInterface, QWebSettings
17 17
18 from E5Gui import E5MessageBox 18 from E5Gui import E5MessageBox
19 19
20 from Utilities.AutoSaver import AutoSaver 20 from Utilities.AutoSaver import AutoSaver
42 42
43 def __eq__(self, other): 43 def __eq__(self, other):
44 """ 44 """
45 Special method determining equality. 45 Special method determining equality.
46 46
47 @param other reference to the history entry to compare against (HistoryEntry) 47 @param other reference to the history entry to compare against
48 (HistoryEntry)
48 @return flag indicating equality (boolean) 49 @return flag indicating equality (boolean)
49 """ 50 """
50 return other.title == self.title and \ 51 return other.title == self.title and \
51 other.url == self.url and \ 52 other.url == self.url and \
52 other.dateTime == self.dateTime 53 other.dateTime == self.dateTime
55 """ 56 """
56 Special method determining less relation. 57 Special method determining less relation.
57 58
58 Note: History is sorted in reverse order by date and time 59 Note: History is sorted in reverse order by date and time
59 60
60 @param other reference to the history entry to compare against (HistoryEntry) 61 @param other reference to the history entry to compare against
62 (HistoryEntry)
61 @return flag indicating less (boolean) 63 @return flag indicating less (boolean)
62 """ 64 """
63 return self.dateTime > other.dateTime 65 return self.dateTime > other.dateTime
64 66
65 def userTitle(self): 67 def userTitle(self):
80 """ 82 """
81 Class implementing the history manager. 83 Class implementing the history manager.
82 84
83 @signal historyCleared() emitted after the history has been cleared 85 @signal historyCleared() emitted after the history has been cleared
84 @signal historyReset() emitted after the history has been reset 86 @signal historyReset() emitted after the history has been reset
85 @signal entryAdded(HistoryEntry) emitted after a history entry has been added 87 @signal entryAdded(HistoryEntry) emitted after a history entry has been
86 @signal entryRemoved(HistoryEntry) emitted after a history entry has been removed 88 added
89 @signal entryRemoved(HistoryEntry) emitted after a history entry has been
90 removed
87 @signal entryUpdated(int) emitted after a history entry has been updated 91 @signal entryUpdated(int) emitted after a history entry has been updated
88 @signal historySaved() emitted after the history was saved 92 @signal historySaved() emitted after the history was saved
89 """ 93 """
90 historyCleared = pyqtSignal() 94 historyCleared = pyqtSignal()
91 historyReset = pyqtSignal() 95 historyReset = pyqtSignal()
123 from .HistoryModel import HistoryModel 127 from .HistoryModel import HistoryModel
124 from .HistoryFilterModel import HistoryFilterModel 128 from .HistoryFilterModel import HistoryFilterModel
125 from .HistoryTreeModel import HistoryTreeModel 129 from .HistoryTreeModel import HistoryTreeModel
126 130
127 self.__historyModel = HistoryModel(self, self) 131 self.__historyModel = HistoryModel(self, self)
128 self.__historyFilterModel = HistoryFilterModel(self.__historyModel, self) 132 self.__historyFilterModel = \
129 self.__historyTreeModel = HistoryTreeModel(self.__historyFilterModel, self) 133 HistoryFilterModel(self.__historyModel, self)
134 self.__historyTreeModel = \
135 HistoryTreeModel(self.__historyFilterModel, self)
130 136
131 super(HistoryManager, self).setDefaultInterface(self) 137 super(HistoryManager, self).setDefaultInterface(self)
132 self.__startFrequencyTimer() 138 self.__startFrequencyTimer()
133 139
134 def close(self): 140 def close(self):
152 """ 158 """
153 Public method to set a new history. 159 Public method to set a new history.
154 160
155 @param history reference to the list of history entries to be set 161 @param history reference to the list of history entries to be set
156 (list of HistoryEntry) 162 (list of HistoryEntry)
157 @param loadedAndSorted flag indicating that the list is sorted (boolean) 163 @param loadedAndSorted flag indicating that the list is sorted
164 (boolean)
158 """ 165 """
159 self.__history = history[:] 166 self.__history = history[:]
160 if not loadedAndSorted: 167 if not loadedAndSorted:
161 self.__history.sort() 168 self.__history.sort()
162 169
217 if cleanurl.password(): 224 if cleanurl.password():
218 # don't save the password in the history 225 # don't save the password in the history
219 cleanurl.setPassword("") 226 cleanurl.setPassword("")
220 if cleanurl.host(): 227 if cleanurl.host():
221 cleanurl.setHost(cleanurl.host().lower()) 228 cleanurl.setHost(cleanurl.host().lower())
222 itm = HistoryEntry(cleanurl.toString(), QDateTime.currentDateTime()) 229 itm = HistoryEntry(cleanurl.toString(),
230 QDateTime.currentDateTime())
223 self._addHistoryEntry(itm) 231 self._addHistoryEntry(itm)
224 232
225 def updateHistoryEntry(self, url, title): 233 def updateHistoryEntry(self, url, title):
226 """ 234 """
227 Public method to update a history entry. 235 Public method to update a history entry.
287 now = QDateTime.currentDateTime() 295 now = QDateTime.currentDateTime()
288 nextTimeout = 0 296 nextTimeout = 0
289 297
290 while self.__history: 298 while self.__history:
291 checkForExpired = QDateTime(self.__history[-1].dateTime) 299 checkForExpired = QDateTime(self.__history[-1].dateTime)
292 checkForExpired.setDate(checkForExpired.date().addDays(self.__daysToExpire)) 300 checkForExpired.setDate(
301 checkForExpired.date().addDays(self.__daysToExpire))
293 if now.daysTo(checkForExpired) > 7: 302 if now.daysTo(checkForExpired) > 7:
294 nextTimeout = 7 * 86400 303 nextTimeout = 7 * 86400
295 else: 304 else:
296 nextTimeout = now.secsTo(checkForExpired) 305 nextTimeout = now.secsTo(checkForExpired)
297 if nextTimeout > 0: 306 if nextTimeout > 0:
342 self.__history = [] 351 self.__history = []
343 self.historyReset.emit() 352 self.historyReset.emit()
344 else: 353 else:
345 breakMS = QDateTime.currentMSecsSinceEpoch() - period 354 breakMS = QDateTime.currentMSecsSinceEpoch() - period
346 while self.__history and \ 355 while self.__history and \
347 QDateTime(self.__history[0].dateTime).toMSecsSinceEpoch() > breakMS: 356 (QDateTime(self.__history[0].dateTime).toMSecsSinceEpoch() >
357 breakMS):
348 itm = self.__history.pop(0) 358 itm = self.__history.pop(0)
349 self.entryRemoved.emit(itm) 359 self.entryRemoved.emit(itm)
350 self.__lastSavedUrl = "" 360 self.__lastSavedUrl = ""
351 self.__saveTimer.changeOccurred() 361 self.__saveTimer.changeOccurred()
352 self.__saveTimer.saveIfNeccessary() 362 self.__saveTimer.saveIfNeccessary()
374 if not historyFile.exists(): 384 if not historyFile.exists():
375 return 385 return
376 if not historyFile.open(QIODevice.ReadOnly): 386 if not historyFile.open(QIODevice.ReadOnly):
377 E5MessageBox.warning(None, 387 E5MessageBox.warning(None,
378 self.trUtf8("Loading History"), 388 self.trUtf8("Loading History"),
379 self.trUtf8("""<p>Unable to open history file <b>{0}</b>.<br/>""" 389 self.trUtf8(
380 """Reason: {1}</p>""")\ 390 """<p>Unable to open history file <b>{0}</b>.<br/>"""
391 """Reason: {1}</p>""")\
381 .format(historyFile.fileName, historyFile.errorString())) 392 .format(historyFile.fileName, historyFile.errorString()))
382 return 393 return
383 394
384 history = [] 395 history = []
385 396
452 opened = f.open(QIODevice.Append) 463 opened = f.open(QIODevice.Append)
453 464
454 if not opened: 465 if not opened:
455 E5MessageBox.warning(None, 466 E5MessageBox.warning(None,
456 self.trUtf8("Saving History"), 467 self.trUtf8("Saving History"),
457 self.trUtf8("""<p>Unable to open history file <b>{0}</b>.<br/>""" 468 self.trUtf8(
458 """Reason: {1}</p>""")\ 469 """<p>Unable to open history file <b>{0}</b>.<br/>"""
470 """Reason: {1}</p>""")\
459 .format(f.fileName(), f.errorString())) 471 .format(f.fileName(), f.errorString()))
460 return 472 return
461 473
462 for index in range(first, -1, -1): 474 for index in range(first, -1, -1):
463 data = QByteArray() 475 data = QByteArray()
473 f.close() 485 f.close()
474 if saveAll: 486 if saveAll:
475 if historyFile.exists() and not historyFile.remove(): 487 if historyFile.exists() and not historyFile.remove():
476 E5MessageBox.warning(None, 488 E5MessageBox.warning(None,
477 self.trUtf8("Saving History"), 489 self.trUtf8("Saving History"),
478 self.trUtf8("""<p>Error removing old history file <b>{0}</b>.""" 490 self.trUtf8(
479 """<br/>Reason: {1}</p>""")\ 491 """<p>Error removing old history file <b>{0}</b>."""
480 .format(historyFile.fileName(), historyFile.errorString())) 492 """<br/>Reason: {1}</p>""")\
493 .format(historyFile.fileName(),
494 historyFile.errorString()))
481 if not f.copy(historyFile.fileName()): 495 if not f.copy(historyFile.fileName()):
482 E5MessageBox.warning(None, 496 E5MessageBox.warning(None,
483 self.trUtf8("Saving History"), 497 self.trUtf8("Saving History"),
484 self.trUtf8("""<p>Error moving new history file over old one """ 498 self.trUtf8(
485 """(<b>{0}</b>).<br/>Reason: {1}</p>""")\ 499 """<p>Error moving new history file over old one """
500 """(<b>{0}</b>).<br/>Reason: {1}</p>""")\
486 .format(historyFile.fileName(), f.errorString())) 501 .format(historyFile.fileName(), f.errorString()))
487 self.historySaved.emit() 502 self.historySaved.emit()
488 try: 503 try:
489 self.__lastSavedUrl = self.__history[0].url 504 self.__lastSavedUrl = self.__history[0].url
490 except IndexError: 505 except IndexError:
500 def __startFrequencyTimer(self): 515 def __startFrequencyTimer(self):
501 """ 516 """
502 Private method to start the timer to recalculate the frequencies. 517 Private method to start the timer to recalculate the frequencies.
503 """ 518 """
504 tomorrow = QDateTime(QDate.currentDate().addDays(1), QTime(3, 0)) 519 tomorrow = QDateTime(QDate.currentDate().addDays(1), QTime(3, 0))
505 self.__frequencyTimer.start(QDateTime.currentDateTime().secsTo(tomorrow) * 1000) 520 self.__frequencyTimer.start(
521 QDateTime.currentDateTime().secsTo(tomorrow) * 1000)

eric ide

mercurial