6 """ |
6 """ |
7 Module implementing the bookmarks manager. |
7 Module implementing the bookmarks manager. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
|
11 import contextlib |
11 |
12 |
12 from PyQt5.QtCore import ( |
13 from PyQt5.QtCore import ( |
13 pyqtSignal, QT_TRANSLATE_NOOP, QObject, QFile, QIODevice, QXmlStreamReader, |
14 pyqtSignal, QT_TRANSLATE_NOOP, QObject, QFile, QIODevice, QXmlStreamReader, |
14 QDateTime, QFileInfo, QUrl, QCoreApplication |
15 QDateTime, QFileInfo, QUrl, QCoreApplication |
15 ) |
16 ) |
53 """ |
54 """ |
54 Constructor |
55 Constructor |
55 |
56 |
56 @param parent reference to the parent object (QObject) |
57 @param parent reference to the parent object (QObject) |
57 """ |
58 """ |
58 super(BookmarksManager, self).__init__(parent) |
59 super().__init__(parent) |
59 |
60 |
60 self.__saveTimer = AutoSaver(self, self.save) |
61 self.__saveTimer = AutoSaver(self, self.save) |
61 self.entryAdded.connect(self.__saveTimer.changeOccurred) |
62 self.entryAdded.connect(self.__saveTimer.changeOccurred) |
62 self.entryRemoved.connect(self.__saveTimer.changeOccurred) |
63 self.entryRemoved.connect(self.__saveTimer.changeOccurred) |
63 self.entryChanged.connect(self.__saveTimer.changeOccurred) |
64 self.entryChanged.connect(self.__saveTimer.changeOccurred) |
321 Public method to set the visit count of a bookmark. |
322 Public method to set the visit count of a bookmark. |
322 |
323 |
323 @param node reference to the node to be changed (BookmarkNode) |
324 @param node reference to the node to be changed (BookmarkNode) |
324 @param count visit count to be set (int or str) |
325 @param count visit count to be set (int or str) |
325 """ |
326 """ |
326 try: |
327 with contextlib.suppress(ValueError): |
327 node.visitCount = int(count) |
328 node.visitCount = int(count) |
328 self.__saveTimer.changeOccurred() |
329 self.__saveTimer.changeOccurred() |
329 except ValueError: |
|
330 # ignore invalid values |
|
331 pass |
|
332 |
330 |
333 def bookmarks(self): |
331 def bookmarks(self): |
334 """ |
332 """ |
335 Public method to get a reference to the root bookmark node. |
333 Public method to get a reference to the root bookmark node. |
336 |
334 |
465 """ |
463 """ |
466 bm = None |
464 bm = None |
467 for node in startNode.children(): |
465 for node in startNode.children(): |
468 if node.type() == BookmarkNode.Folder: |
466 if node.type() == BookmarkNode.Folder: |
469 bm = self.__searchBookmark(url, node) |
467 bm = self.__searchBookmark(url, node) |
470 elif node.type() == BookmarkNode.Bookmark: |
468 elif ( |
471 if node.url == url: |
469 node.type() == BookmarkNode.Bookmark and |
472 bm = node |
470 node.url == url |
|
471 ): |
|
472 bm = node |
473 if bm is not None: |
473 if bm is not None: |
474 return bm |
474 return bm |
475 return None |
475 return None |
476 |
476 |
477 def bookmarksForUrl(self, url, start=StartRoot): |
477 def bookmarksForUrl(self, url, start=StartRoot): |
508 """ |
508 """ |
509 bm = [] |
509 bm = [] |
510 for node in startNode.children(): |
510 for node in startNode.children(): |
511 if node.type() == BookmarkNode.Folder: |
511 if node.type() == BookmarkNode.Folder: |
512 bm.extend(self.__searchBookmarks(url, node)) |
512 bm.extend(self.__searchBookmarks(url, node)) |
513 elif node.type() == BookmarkNode.Bookmark: |
513 elif ( |
514 if node.url == url: |
514 node.type() == BookmarkNode.Bookmark and |
515 bm.append(node) |
515 node.url == url |
|
516 ): |
|
517 bm.append(node) |
516 return bm |
518 return bm |
517 |
519 |
518 |
520 |
519 class RemoveBookmarksCommand(QUndoCommand): |
521 class RemoveBookmarksCommand(QUndoCommand): |
520 """ |
522 """ |
527 @param bookmarksManager reference to the bookmarks manager |
529 @param bookmarksManager reference to the bookmarks manager |
528 (BookmarksManager) |
530 (BookmarksManager) |
529 @param parent reference to the parent node (BookmarkNode) |
531 @param parent reference to the parent node (BookmarkNode) |
530 @param row row number of bookmark (integer) |
532 @param row row number of bookmark (integer) |
531 """ |
533 """ |
532 super(RemoveBookmarksCommand, self).__init__( |
534 super().__init__( |
533 QCoreApplication.translate("BookmarksManager", "Remove Bookmark")) |
535 QCoreApplication.translate("BookmarksManager", "Remove Bookmark")) |
534 |
536 |
535 self._row = row |
537 self._row = row |
536 self._bookmarksManager = bookmarksManager |
538 self._bookmarksManager = bookmarksManager |
537 try: |
539 try: |
601 @param node reference to the node to be changed (BookmarkNode) |
603 @param node reference to the node to be changed (BookmarkNode) |
602 @param newValue new value to be set (string) |
604 @param newValue new value to be set (string) |
603 @param title flag indicating a change of the title (True) or |
605 @param title flag indicating a change of the title (True) or |
604 the URL (False) (boolean) |
606 the URL (False) (boolean) |
605 """ |
607 """ |
606 super(ChangeBookmarkCommand, self).__init__() |
608 super().__init__() |
607 |
609 |
608 self._bookmarksManager = bookmarksManager |
610 self._bookmarksManager = bookmarksManager |
609 self._title = title |
611 self._title = title |
610 self._newValue = newValue |
612 self._newValue = newValue |
611 self._node = node |
613 self._node = node |