WebBrowser/CookieJar/CookiesDialog.py

branch
QtWebEngine
changeset 4845
2d22ff71c005
parent 4631
5c1a96925da4
child 5028
f7ea46dd1b7e
equal deleted inserted replaced
4840:69ee7965ba27 4845:2d22ff71c005
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to show all cookies.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, Qt, QDateTime, QByteArray, \
13 QSortFilterProxyModel
14 from PyQt5.QtGui import QFont, QFontMetrics
15 from PyQt5.QtWidgets import QDialog
16
17 from .CookieModel import CookieModel
18
19 from .Ui_CookiesDialog import Ui_CookiesDialog
20
21
22 class CookiesDialog(QDialog, Ui_CookiesDialog):
23 """
24 Class implementing a dialog to show all cookies.
25 """
26 def __init__(self, cookieJar, parent=None):
27 """
28 Constructor
29
30 @param cookieJar reference to the cookie jar (CookieJar)
31 @param parent reference to the parent widget (QWidget)
32 """
33 super(CookiesDialog, self).__init__(parent)
34 self.setupUi(self)
35
36 self.addButton.setEnabled(False)
37
38 self.__cookieJar = cookieJar
39
40 self.removeButton.clicked.connect(self.cookiesTable.removeSelected)
41 self.removeAllButton.clicked.connect(self.cookiesTable.removeAll)
42
43 self.cookiesTable.verticalHeader().hide()
44 model = CookieModel(cookieJar, self)
45 self.__proxyModel = QSortFilterProxyModel(self)
46 self.__proxyModel.setSourceModel(model)
47 self.searchEdit.textChanged.connect(
48 self.__proxyModel.setFilterFixedString)
49 self.cookiesTable.setModel(self.__proxyModel)
50 self.cookiesTable.doubleClicked.connect(self.__showCookieDetails)
51 self.cookiesTable.selectionModel().selectionChanged.connect(
52 self.__tableSelectionChanged)
53 self.cookiesTable.model().modelReset.connect(self.__tableModelReset)
54
55 fm = QFontMetrics(QFont())
56 height = fm.height() + fm.height() // 3
57 self.cookiesTable.verticalHeader().setDefaultSectionSize(height)
58 self.cookiesTable.verticalHeader().setMinimumSectionSize(-1)
59 for section in range(model.columnCount()):
60 header = self.cookiesTable.horizontalHeader()\
61 .sectionSizeHint(section)
62 if section == 0:
63 header = fm.width("averagebiglonghost.averagedomain.info")
64 elif section == 1:
65 header = fm.width("_session_id")
66 elif section == 4:
67 header = fm.width(
68 QDateTime.currentDateTime().toString(Qt.LocalDate))
69 buffer = fm.width("mm")
70 header += buffer
71 self.cookiesTable.horizontalHeader().resizeSection(section, header)
72 self.cookiesTable.horizontalHeader().setStretchLastSection(True)
73 self.cookiesTable.model().sort(
74 self.cookiesTable.horizontalHeader().sortIndicatorSection(),
75 Qt.AscendingOrder)
76
77 self.__detailsDialog = None
78
79 def __showCookieDetails(self, index):
80 """
81 Private slot to show a dialog with the cookie details.
82
83 @param index index of the entry to show (QModelIndex)
84 """
85 if not index.isValid():
86 return
87
88 cookiesTable = self.sender()
89 if cookiesTable is None:
90 return
91
92 model = cookiesTable.model()
93 row = index.row()
94
95 domain = model.data(model.index(row, 0))
96 name = model.data(model.index(row, 1))
97 path = model.data(model.index(row, 2))
98 secure = model.data(model.index(row, 3))
99 expires = model.data(model.index(row, 4)).toString("yyyy-MM-dd hh:mm")
100 value = bytes(
101 QByteArray.fromPercentEncoding(
102 model.data(model.index(row, 5)))).decode()
103
104 if self.__detailsDialog is None:
105 from .CookieDetailsDialog import CookieDetailsDialog
106 self.__detailsDialog = CookieDetailsDialog(self)
107 self.__detailsDialog.setData(domain, name, path, secure, expires,
108 value)
109 self.__detailsDialog.show()
110
111 @pyqtSlot()
112 def on_addButton_clicked(self):
113 """
114 Private slot to add a new exception.
115 """
116 selection = self.cookiesTable.selectionModel().selectedRows()
117 if len(selection) == 0:
118 return
119
120 from .CookiesExceptionsDialog import CookiesExceptionsDialog
121
122 firstSelected = selection[0]
123 domainSelection = firstSelected.sibling(firstSelected.row(), 0)
124 domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole)
125 dlg = CookiesExceptionsDialog(self.__cookieJar, self)
126 dlg.setDomainName(domain)
127 dlg.exec_()
128
129 def __tableSelectionChanged(self, selected, deselected):
130 """
131 Private slot to handle a change of selected items.
132
133 @param selected selected indexes (QItemSelection)
134 @param deselected deselected indexes (QItemSelection)
135 """
136 self.addButton.setEnabled(len(selected.indexes()) > 0)
137
138 def __tableModelReset(self):
139 """
140 Private slot to handle a reset of the cookies table.
141 """
142 self.addButton.setEnabled(False)

eric ide

mercurial