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