|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the bookmark node. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QDateTime |
|
13 |
|
14 |
|
15 class BookmarkNode(object): |
|
16 """ |
|
17 Class implementing the bookmark node type. |
|
18 """ |
|
19 # possible bookmark node types |
|
20 Root = 0 |
|
21 Folder = 1 |
|
22 Bookmark = 2 |
|
23 Separator = 3 |
|
24 |
|
25 # possible timestamp types |
|
26 TsAdded = 0 |
|
27 TsModified = 1 |
|
28 TsVisited = 2 |
|
29 |
|
30 def __init__(self, type_=Root, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param type_ type of the bookmark node (BookmarkNode.Type) |
|
35 @param parent reference to the parent node (BookmarkNode) |
|
36 """ |
|
37 self.url = "" |
|
38 self.title = "" |
|
39 self.desc = "" |
|
40 self.expanded = False |
|
41 self.added = QDateTime() |
|
42 self.modified = QDateTime() |
|
43 self.visited = QDateTime() |
|
44 |
|
45 self._children = [] |
|
46 self._parent = parent |
|
47 self._type = type_ |
|
48 |
|
49 if parent is not None: |
|
50 parent.add(self) |
|
51 |
|
52 def type(self): |
|
53 """ |
|
54 Public method to get the bookmark's type. |
|
55 |
|
56 @return bookmark type (BookmarkNode.Type) |
|
57 """ |
|
58 return self._type |
|
59 |
|
60 def setType(self, type_): |
|
61 """ |
|
62 Public method to set the bookmark's type. |
|
63 |
|
64 @param type_ type of the bookmark node (BookmarkNode.Type) |
|
65 """ |
|
66 self._type = type_ |
|
67 |
|
68 def children(self): |
|
69 """ |
|
70 Public method to get the list of child nodes. |
|
71 |
|
72 @return list of all child nodes (list of BookmarkNode) |
|
73 """ |
|
74 return self._children[:] |
|
75 |
|
76 def parent(self): |
|
77 """ |
|
78 Public method to get a reference to the parent node. |
|
79 |
|
80 @return reference to the parent node (BookmarkNode) |
|
81 """ |
|
82 return self._parent |
|
83 |
|
84 def add(self, child, offset=-1): |
|
85 """ |
|
86 Public method to add/insert a child node. |
|
87 |
|
88 @param child reference to the node to add (BookmarkNode) |
|
89 @param offset position where to insert child (integer, -1 = append) |
|
90 """ |
|
91 if child._type == BookmarkNode.Root: |
|
92 return |
|
93 |
|
94 if child._parent is not None: |
|
95 child._parent.remove(child) |
|
96 |
|
97 child._parent = self |
|
98 if offset == -1: |
|
99 self._children.append(child) |
|
100 else: |
|
101 self._children.insert(offset, child) |
|
102 |
|
103 def remove(self, child): |
|
104 """ |
|
105 Public method to remove a child node. |
|
106 |
|
107 @param child reference to the child node (BookmarkNode) |
|
108 """ |
|
109 child._parent = None |
|
110 if child in self._children: |
|
111 self._children.remove(child) |