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