|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a model for user agent management. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 |
|
12 from PyQt5.QtCore import Qt, QModelIndex, QAbstractTableModel |
|
13 |
|
14 |
|
15 class UserAgentModel(QAbstractTableModel): |
|
16 """ |
|
17 Class implementing a model for user agent management. |
|
18 """ |
|
19 def __init__(self, manager, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param manager reference to the user agent manager (UserAgentManager) |
|
24 @param parent reference to the parent object (QObject) |
|
25 """ |
|
26 super().__init__(parent) |
|
27 |
|
28 self.__manager = manager |
|
29 self.__manager.changed.connect(self.__userAgentsChanged) |
|
30 |
|
31 self.__headers = [ |
|
32 self.tr("Host"), |
|
33 self.tr("User Agent String"), |
|
34 ] |
|
35 |
|
36 def __userAgentsChanged(self): |
|
37 """ |
|
38 Private slot handling a change of the registered user agent strings. |
|
39 """ |
|
40 self.beginResetModel() |
|
41 self.endResetModel() |
|
42 |
|
43 def removeRows(self, row, count, parent=None): |
|
44 """ |
|
45 Public method to remove entries from the model. |
|
46 |
|
47 @param row start row (integer) |
|
48 @param count number of rows to remove (integer) |
|
49 @param parent parent index (QModelIndex) |
|
50 @return flag indicating success (boolean) |
|
51 """ |
|
52 if parent is None: |
|
53 parent = QModelIndex() |
|
54 |
|
55 if parent.isValid(): |
|
56 return False |
|
57 |
|
58 if count <= 0: |
|
59 return False |
|
60 |
|
61 lastRow = row + count - 1 |
|
62 |
|
63 self.beginRemoveRows(parent, row, lastRow) |
|
64 |
|
65 hostsList = self.__manager.allHostNames() |
|
66 for index in range(row, lastRow + 1): |
|
67 self.__manager.removeUserAgent(hostsList[index]) |
|
68 |
|
69 return True |
|
70 |
|
71 def rowCount(self, parent=None): |
|
72 """ |
|
73 Public method to get the number of rows of the model. |
|
74 |
|
75 @param parent parent index (QModelIndex) |
|
76 @return number of rows (integer) |
|
77 """ |
|
78 if parent is None: |
|
79 parent = QModelIndex() |
|
80 |
|
81 if parent.isValid(): |
|
82 return 0 |
|
83 else: |
|
84 return self.__manager.hostsCount() |
|
85 |
|
86 def columnCount(self, parent=None): |
|
87 """ |
|
88 Public method to get the number of columns of the model. |
|
89 |
|
90 @param parent parent index (QModelIndex) (Unused) |
|
91 @return number of columns (integer) |
|
92 """ |
|
93 return len(self.__headers) |
|
94 |
|
95 def data(self, index, role): |
|
96 """ |
|
97 Public method to get data from the model. |
|
98 |
|
99 @param index index to get data for (QModelIndex) |
|
100 @param role role of the data to retrieve (integer) |
|
101 @return requested data |
|
102 """ |
|
103 if index.row() >= self.__manager.hostsCount() or index.row() < 0: |
|
104 return None |
|
105 |
|
106 host = self.__manager.allHostNames()[index.row()] |
|
107 userAgent = self.__manager.userAgent(host) |
|
108 |
|
109 if userAgent is None: |
|
110 return None |
|
111 |
|
112 if role == Qt.ItemDataRole.DisplayRole: |
|
113 if index.column() == 0: |
|
114 return host |
|
115 elif index.column() == 1: |
|
116 return userAgent |
|
117 |
|
118 return None |
|
119 |
|
120 def headerData(self, section, orientation, |
|
121 role=Qt.ItemDataRole.DisplayRole): |
|
122 """ |
|
123 Public method to get the header data. |
|
124 |
|
125 @param section section number (integer) |
|
126 @param orientation header orientation (Qt.Orientation) |
|
127 @param role data role (Qt.ItemDataRole) |
|
128 @return header data |
|
129 """ |
|
130 if ( |
|
131 orientation == Qt.Orientation.Horizontal and |
|
132 role == Qt.ItemDataRole.DisplayRole |
|
133 ): |
|
134 with contextlib.suppress(IndexError): |
|
135 return self.__headers[section] |
|
136 |
|
137 return None |