Helpviewer/History/HistoryManager.py

changeset 7
c679fb30c8f3
parent 0
de9c2efb9d02
child 9
911a9f7aef1e
equal deleted inserted replaced
6:52e8c820d0dd 7:c679fb30c8f3
339 """Reason: {1}""")\ 339 """Reason: {1}""")\
340 .format(historyFile.fileName, historyFile.errorString())) 340 .format(historyFile.fileName, historyFile.errorString()))
341 return 341 return
342 342
343 history = [] 343 history = []
344 historyStream = QDataStream(historyFile)
345 344
346 # double check, that the history file is sorted as it is read 345 # double check, that the history file is sorted as it is read
347 needToSort = False 346 needToSort = False
348 lastInsertedItem = HistoryEntry() 347 lastInsertedItem = HistoryEntry()
349 data = QByteArray() 348 data = QByteArray(historyFile.readAll())
350 stream = QDataStream() 349 stream = QDataStream(data, QIODevice.ReadOnly)
351 buffer = QBuffer() 350 while not stream.atEnd():
352 stream.setDevice(buffer)
353 while not historyFile.atEnd():
354 historyStream >> data
355 buffer.close()
356 buffer.setBuffer(data)
357 buffer.open(QIODevice.ReadOnly)
358 ver = stream.readUInt32() 351 ver = stream.readUInt32()
359 if ver != HISTORY_VERSION: 352 if ver != HISTORY_VERSION:
360 continue 353 continue
361 itm = HistoryEntry() 354 itm = HistoryEntry()
362 stream.readString(itm.url) 355 itm.url = stream.readString()
363 stream.readString(itm.dateTime) 356 stream >> itm.dateTime
364 stream.readString(itm.title) 357 itm.title = stream.readString()
365 358
366 if not itm.dateTime.isValid(): 359 if not itm.dateTime.isValid():
367 continue 360 continue
368 361
369 if itm == lastInsertedItem: 362 if itm == lastInsertedItem:
393 """ 386 """
394 historyFile = QFile(Utilities.getConfigDir() + "/browser/history") 387 historyFile = QFile(Utilities.getConfigDir() + "/browser/history")
395 if not historyFile.exists(): 388 if not historyFile.exists():
396 self.__lastSavedUrl = "" 389 self.__lastSavedUrl = ""
397 390
398 saveAll = self.__lastSavedUrl = "" 391 saveAll = self.__lastSavedUrl == ""
399 first = len(self.__history) - 1 392 first = len(self.__history) - 1
400 if not saveAll: 393 if not saveAll:
401 # find the first one to save 394 # find the first one to save
402 for index in range(len(self.__history)): 395 for index in range(len(self.__history)):
403 if self.__history[index].url == self.__lastSavedUrl: 396 if self.__history[index].url == self.__lastSavedUrl:
404 first = index - 1 397 first = index - 1
405 break 398 break
406 if first == len(self.__history) - 1: 399 if first == len(self.__history) - 1:
407 saveAll = True 400 saveAll = True
408 401
409 # use a temporary file when saving everything
410 tempFile = QTemporaryFile()
411 if saveAll: 402 if saveAll:
412 opened = tempFile.open() 403 # use a temporary file when saving everything
404 f = QTemporaryFile()
405 opened = f.open()
413 else: 406 else:
414 opened = historyFile.open(QIODevice.Append) 407 f = historyFile
408 opened = f.open(QIODevice.Append)
415 409
416 if not opened: 410 if not opened:
417 if saveAll:
418 f = tempFile
419 else:
420 f = historyFile
421 QMessageBox.warning(None, 411 QMessageBox.warning(None,
422 self.trUtf8("Saving History"), 412 self.trUtf8("Saving History"),
423 self.trUtf8("""Unable to open history file <b>{0}</b>.<br/>""" 413 self.trUtf8("""Unable to open history file <b>{0}</b>.<br/>"""
424 """Reason: {1}""")\ 414 """Reason: {1}""")\
425 .format(f.fileName, f.errorString())) 415 .format(f.fileName(), f.errorString()))
426 return 416 return
427 417
428 if saveAll:
429 historyStream = QDataStream(tempFile)
430 else:
431 historyStream = QDataStream(historyFile)
432 for index in range(first, -1, -1): 418 for index in range(first, -1, -1):
433 data = QByteArray() 419 data = QByteArray()
434 stream = QDataStream(data, QIODevice.WriteOnly) 420 stream = QDataStream(data, QIODevice.WriteOnly)
435 itm = self.__history[index] 421 itm = self.__history[index]
436 stream.writeUInt32(HISTORY_VERSION) 422 stream.writeUInt32(HISTORY_VERSION)
437 stream.writeString(itm.url) 423 stream.writeString(itm.url)
438 stream << itm.dateTime 424 stream << itm.dateTime
439 stream.writeString(itm.title) 425 stream.writeString(itm.title)
440 historyStream << data 426 f.write(data)
441 427
428 f.close()
442 if saveAll: 429 if saveAll:
443 tempFile.close()
444 if historyFile.exists() and not historyFile.remove(): 430 if historyFile.exists() and not historyFile.remove():
445 QMessageBox.warning(None, 431 QMessageBox.warning(None,
446 self.trUtf8("Saving History"), 432 self.trUtf8("Saving History"),
447 self.trUtf8("""Error removing old history file <b>{0}</b>.""" 433 self.trUtf8("""Error removing old history file <b>{0}</b>."""
448 """<br/>Reason: {1}""")\ 434 """<br/>Reason: {1}""")\
449 .format(historyFile.fileName, historyFile.errorString())) 435 .format(historyFile.fileName(), historyFile.errorString()))
450 if not tempFile.copy(historyFile.fileName()): 436 if not f.copy(historyFile.fileName()):
451 QMessageBox.warning(None, 437 QMessageBox.warning(None,
452 self.trUtf8("Saving History"), 438 self.trUtf8("Saving History"),
453 self.trUtf8("""Error moving new history file over old one """ 439 self.trUtf8("""Error moving new history file over old one """
454 """(<b>{0}</b>).<br/>Reason: {1}""")\ 440 """(<b>{0}</b>).<br/>Reason: {1}""")\
455 .format(historyFile.fileName(), tempFile.errorString())) 441 .format(historyFile.fileName(), tempFile.errorString()))
456 else:
457 historyFile.close()
458 442
459 try: 443 try:
460 self.__lastSavedUrl = self.__history[0].url 444 self.__lastSavedUrl = self.__history[0].url
461 except IndexError: 445 except IndexError:
462 self.__lastSavedUrl = "" 446 self.__lastSavedUrl = ""

eric ide

mercurial