eric6/Helpviewer/CookieJar/CookiesDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 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 model = self.cookiesTable.model()
89 row = index.row()
90
91 domain = model.data(model.index(row, 0))
92 name = model.data(model.index(row, 1))
93 path = model.data(model.index(row, 2))
94 secure = model.data(model.index(row, 3))
95 expires = model.data(model.index(row, 4)).toString("yyyy-MM-dd hh:mm")
96 value = bytes(
97 QByteArray.fromPercentEncoding(
98 model.data(model.index(row, 5)))).decode()
99
100 if self.__detailsDialog is None:
101 from .CookieDetailsDialog import CookieDetailsDialog
102 self.__detailsDialog = CookieDetailsDialog(self)
103 self.__detailsDialog.setData(domain, name, path, secure, expires,
104 value)
105 self.__detailsDialog.show()
106
107 @pyqtSlot()
108 def on_addButton_clicked(self):
109 """
110 Private slot to add a new exception.
111 """
112 selection = self.cookiesTable.selectionModel().selectedRows()
113 if len(selection) == 0:
114 return
115
116 from .CookiesExceptionsDialog import CookiesExceptionsDialog
117
118 firstSelected = selection[0]
119 domainSelection = firstSelected.sibling(firstSelected.row(), 0)
120 domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole)
121 dlg = CookiesExceptionsDialog(self.__cookieJar, self)
122 dlg.setDomainName(domain)
123 dlg.exec_()
124
125 def __tableSelectionChanged(self, selected, deselected):
126 """
127 Private slot to handle a change of selected items.
128
129 @param selected selected indexes (QItemSelection)
130 @param deselected deselected indexes (QItemSelection)
131 """
132 self.addButton.setEnabled(len(selected.indexes()) > 0)
133
134 def __tableModelReset(self):
135 """
136 Private slot to handle a reset of the cookies table.
137 """
138 self.addButton.setEnabled(False)

eric ide

mercurial