|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class to manage closed tabs. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSignal, QUrl, QObject |
|
11 |
|
12 |
|
13 class ClosedTab: |
|
14 """ |
|
15 Class implementing a structure to store data about a closed tab. |
|
16 """ |
|
17 def __init__(self, url=None, title="", position=-1): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param url URL of the closed tab (QUrl) |
|
22 @param title title of the closed tab (string) |
|
23 @param position index of the closed tab (integer) |
|
24 """ |
|
25 self.url = QUrl() if url is None else QUrl(url) |
|
26 self.title = title |
|
27 self.position = position |
|
28 |
|
29 def __eq__(self, other): |
|
30 """ |
|
31 Special method implementing the equality operator. |
|
32 |
|
33 @param other reference to the object to compare against (ClosedTab) |
|
34 @return flag indicating equality of the tabs (boolean) |
|
35 """ |
|
36 return ( |
|
37 self.url == other.url and |
|
38 self.title == other.title and |
|
39 self.position == other.position |
|
40 ) |
|
41 |
|
42 |
|
43 class ClosedTabsManager(QObject): |
|
44 """ |
|
45 Class implementing a manager for closed tabs. |
|
46 |
|
47 @signal closedTabAvailable(boolean) emitted to signal a change of |
|
48 availability of closed tabs |
|
49 """ |
|
50 closedTabAvailable = pyqtSignal(bool) |
|
51 |
|
52 def __init__(self, parent=None): |
|
53 """ |
|
54 Constructor |
|
55 |
|
56 @param parent reference to the parent object (QObject) |
|
57 """ |
|
58 super().__init__() |
|
59 |
|
60 self.__closedTabs = [] |
|
61 |
|
62 def recordBrowser(self, browser, position): |
|
63 """ |
|
64 Public method to record the data of a browser about to be closed. |
|
65 |
|
66 @param browser reference to the browser to be closed (HelpBrowser) |
|
67 @param position index of the tab to be closed (integer) |
|
68 """ |
|
69 import WebBrowser.WebBrowserWindow |
|
70 if WebBrowser.WebBrowserWindow.WebBrowserWindow.isPrivate(): |
|
71 return |
|
72 |
|
73 if browser.url().isEmpty(): |
|
74 return |
|
75 |
|
76 tab = ClosedTab(browser.url(), browser.title(), position) |
|
77 self.__closedTabs.insert(0, tab) |
|
78 self.closedTabAvailable.emit(True) |
|
79 |
|
80 def getClosedTabAt(self, index): |
|
81 """ |
|
82 Public method to get the indexed closed tab. |
|
83 |
|
84 @param index index of the tab to return (integer) |
|
85 @return requested tab (ClosedTab) |
|
86 """ |
|
87 tab = ( |
|
88 self.__closedTabs.pop(index) |
|
89 if (len(self.__closedTabs) > 0 and |
|
90 len(self.__closedTabs) > index) else |
|
91 ClosedTab() |
|
92 ) |
|
93 self.closedTabAvailable.emit(len(self.__closedTabs) > 0) |
|
94 return tab |
|
95 |
|
96 def isClosedTabAvailable(self): |
|
97 """ |
|
98 Public method to check for closed tabs. |
|
99 |
|
100 @return flag indicating the availability of closed tab data (boolean) |
|
101 """ |
|
102 return len(self.__closedTabs) > 0 |
|
103 |
|
104 def clearList(self): |
|
105 """ |
|
106 Public method to clear the list of closed tabs. |
|
107 """ |
|
108 self.__closedTabs = [] |
|
109 self.closedTabAvailable.emit(False) |
|
110 |
|
111 def allClosedTabs(self): |
|
112 """ |
|
113 Public method to get a list of all closed tabs. |
|
114 |
|
115 @return list of closed tabs (list of ClosedTab) |
|
116 """ |
|
117 return self.__closedTabs |