|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog for the configuration of cookie exceptions. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, QSortFilterProxyModel |
|
11 from PyQt5.QtGui import QFont, QFontMetrics |
|
12 from PyQt5.QtWidgets import QDialog, QCompleter |
|
13 |
|
14 from .CookieExceptionsModel import CookieExceptionsModel |
|
15 |
|
16 from .Ui_CookiesExceptionsDialog import Ui_CookiesExceptionsDialog |
|
17 |
|
18 |
|
19 class CookiesExceptionsDialog(QDialog, Ui_CookiesExceptionsDialog): |
|
20 """ |
|
21 Class implementing a dialog for the configuration of cookie exceptions. |
|
22 """ |
|
23 def __init__(self, cookieJar, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param cookieJar reference to the cookie jar (CookieJar) |
|
28 @param parent reference to the parent widget (QWidget) |
|
29 """ |
|
30 super().__init__(parent) |
|
31 self.setupUi(self) |
|
32 |
|
33 self.__cookieJar = cookieJar |
|
34 |
|
35 self.removeButton.clicked.connect( |
|
36 self.exceptionsTable.removeSelected) |
|
37 self.removeAllButton.clicked.connect( |
|
38 self.exceptionsTable.removeAll) |
|
39 |
|
40 self.exceptionsTable.verticalHeader().hide() |
|
41 self.__exceptionsModel = CookieExceptionsModel(cookieJar) |
|
42 self.__proxyModel = QSortFilterProxyModel(self) |
|
43 self.__proxyModel.setSourceModel(self.__exceptionsModel) |
|
44 self.searchEdit.textChanged.connect( |
|
45 self.__proxyModel.setFilterFixedString) |
|
46 self.exceptionsTable.setModel(self.__proxyModel) |
|
47 |
|
48 self.domainEdit.setCompleter( |
|
49 QCompleter(cookieJar.cookieDomains(), self.domainEdit)) |
|
50 |
|
51 f = QFont() |
|
52 f.setPointSize(10) |
|
53 fm = QFontMetrics(f) |
|
54 height = fm.height() + fm.height() // 3 |
|
55 self.exceptionsTable.verticalHeader().setDefaultSectionSize(height) |
|
56 self.exceptionsTable.verticalHeader().setMinimumSectionSize(-1) |
|
57 for section in range(self.__exceptionsModel.columnCount()): |
|
58 header = self.exceptionsTable.horizontalHeader().sectionSizeHint( |
|
59 section) |
|
60 if section == 0: |
|
61 try: |
|
62 header = fm.horizontalAdvance( |
|
63 "averagebiglonghost.averagedomain.info") |
|
64 except AttributeError: |
|
65 header = fm.width( |
|
66 "averagebiglonghost.averagedomain.info") |
|
67 elif section == 1: |
|
68 try: |
|
69 header = fm.horizontalAdvance(self.tr("Allow For Session")) |
|
70 except AttributeError: |
|
71 header = fm.width(self.tr("Allow For Session")) |
|
72 try: |
|
73 buffer = fm.horizontalAdvance("mm") |
|
74 except AttributeError: |
|
75 buffer = fm.width("mm") |
|
76 header += buffer |
|
77 self.exceptionsTable.horizontalHeader().resizeSection( |
|
78 section, header) |
|
79 |
|
80 def setDomainName(self, domain): |
|
81 """ |
|
82 Public method to set the domain to be displayed. |
|
83 |
|
84 @param domain domain name to be displayed (string) |
|
85 """ |
|
86 self.domainEdit.setText(domain) |
|
87 |
|
88 @pyqtSlot(str) |
|
89 def on_domainEdit_textChanged(self, txt): |
|
90 """ |
|
91 Private slot to handle a change of the domain edit text. |
|
92 |
|
93 @param txt current text of the edit (string) |
|
94 """ |
|
95 enabled = txt != "" |
|
96 self.blockButton.setEnabled(enabled) |
|
97 self.allowButton.setEnabled(enabled) |
|
98 self.allowForSessionButton.setEnabled(enabled) |
|
99 |
|
100 @pyqtSlot() |
|
101 def on_blockButton_clicked(self): |
|
102 """ |
|
103 Private slot to block cookies of a domain. |
|
104 """ |
|
105 from .CookieJar import CookieJar |
|
106 self.__exceptionsModel.addRule(self.domainEdit.text(), CookieJar.Block) |
|
107 self.domainEdit.clear() |
|
108 |
|
109 @pyqtSlot() |
|
110 def on_allowForSessionButton_clicked(self): |
|
111 """ |
|
112 Private slot to allow cookies of a domain for the current session only. |
|
113 """ |
|
114 from .CookieJar import CookieJar |
|
115 self.__exceptionsModel.addRule(self.domainEdit.text(), |
|
116 CookieJar.AllowForSession) |
|
117 self.domainEdit.clear() |
|
118 |
|
119 @pyqtSlot() |
|
120 def on_allowButton_clicked(self): |
|
121 """ |
|
122 Private slot to allow cookies of a domain. |
|
123 """ |
|
124 from .CookieJar import CookieJar |
|
125 self.__exceptionsModel.addRule(self.domainEdit.text(), CookieJar.Allow) |
|
126 self.domainEdit.clear() |