45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module cookiejar;
|
|
42
|
73
|
43 import qt.network.QNetworkCookieJar;
|
|
44 import qt.core.QDebug;
|
|
45
|
|
46 import qt.core.QAbstractItemModel;
|
|
47 import qt.core.QDateTime;
|
|
48 import qt.core.QDir;
|
|
49 import qt.core.QFile;
|
|
50 import qt.core.QMetaEnum;
|
|
51 import qt.core.QSettings;
|
|
52 import qt.core.QUrl;
|
45
|
53
|
73
|
54 import qt.gui.QDialog;
|
|
55 import qt.gui.QTableView;
|
|
56 import qt.gui.QCompleter;
|
|
57 import qt.gui.QDesktopServices;
|
|
58 import qt.gui.QFont;
|
|
59 import qt.gui.QFontMetrics;
|
|
60 import qt.gui.QHeaderView;
|
|
61 import qt.gui.QKeyEvent;
|
|
62 import qt.gui.QSortFilterProxyModel;
|
45
|
63
|
73
|
64 import QtWebKit.QWebSettings;
|
45
|
65
|
67
|
66 import cookiejar;
|
|
67 import autosaver;
|
45
|
68
|
|
69
|
74
|
70 static const uint JAR_VERSION = 23;
|
45
|
71
|
80
|
72 QDataStream opShl(QDataStream stream, QNetworkCookie[] list)
|
45
|
73 {
|
67
|
74 stream << JAR_VERSION;
|
77
|
75 stream << cast(uint) list.length;
|
|
76 for (int i = 0; i < list.length; ++i)
|
|
77 stream << list[i].toRawForm();
|
67
|
78 return stream;
|
45
|
79 }
|
|
80
|
80
|
81 QDataStream opShr(QDataStream stream, ref QNetworkCookie[] list)
|
45
|
82 {
|
67
|
83 list.clear();
|
45
|
84
|
77
|
85 uint version_;
|
73
|
86 stream >> version_;
|
45
|
87
|
80
|
88 if (version_ != JAR_VERSION)
|
67
|
89 return stream;
|
45
|
90
|
77
|
91 uint count;
|
67
|
92 stream >> count;
|
77
|
93 for(uint i = 0; i < count; ++i)
|
67
|
94 {
|
|
95 QByteArray value;
|
|
96 stream >> value;
|
74
|
97 QNetworkCookie[] newCookies = QNetworkCookie.parseCookies(value);
|
77
|
98 if (newCookies.length == 0 && value.length() != 0) {
|
67
|
99 qWarning() << "CookieJar: Unable to parse saved cookie:" << value;
|
|
100 }
|
|
101
|
77
|
102 for (int j = 0; j < newCookies.length; ++j)
|
|
103 list ~= newCookies[j];
|
67
|
104
|
|
105 if (stream.atEnd())
|
|
106 break;
|
|
107 }
|
|
108 return stream;
|
45
|
109 }
|
|
110
|
|
111 class CookieJar : public QNetworkCookieJar
|
|
112 {
|
67
|
113 mixin Signal!("cookiesChanged");
|
45
|
114
|
|
115 public:
|
67
|
116
|
45
|
117 enum AcceptPolicy {
|
|
118 AcceptAlways,
|
|
119 AcceptNever,
|
|
120 AcceptOnlyFromSitesNavigatedTo
|
67
|
121 }
|
45
|
122
|
|
123 enum KeepPolicy {
|
|
124 KeepUntilExpire,
|
|
125 KeepUntilExit,
|
|
126 KeepUntilTimeLimit
|
67
|
127 }
|
45
|
128
|
67
|
129 this(QObject parent = null)
|
45
|
130 {
|
|
131 super(parent);
|
|
132 m_loaded = false;
|
|
133 m_saveTimer = new AutoSaver(this);
|
|
134 m_acceptCookies = AcceptOnlyFromSitesNavigatedTo;
|
|
135 }
|
67
|
136
|
45
|
137 ~this()
|
|
138 {
|
|
139 if (m_keepCookies == KeepUntilExit)
|
|
140 clear();
|
|
141 m_saveTimer.saveIfNeccessary();
|
|
142 }
|
|
143
|
74
|
144 QNetworkCookie[] cookiesForUrl(QUrl url)
|
67
|
145 {
|
77
|
146 CookieJar that = cast(CookieJar) this;
|
67
|
147 if (!m_loaded)
|
|
148 that.load();
|
45
|
149
|
67
|
150 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
151 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled)) {
|
74
|
152 QNetworkCookie[] noCookies;
|
67
|
153 return noCookies;
|
|
154 }
|
45
|
155
|
67
|
156 return QNetworkCookieJar.cookiesForUrl(url);
|
|
157 }
|
|
158
|
74
|
159 bool setCookiesFromUrl(QNetworkCookie[] cookieList, QUrl url)
|
67
|
160 {
|
|
161 if (!m_loaded)
|
|
162 load();
|
45
|
163
|
67
|
164 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
165 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled))
|
|
166 return false;
|
45
|
167
|
77
|
168 string host = url.host();
|
67
|
169 bool eBlock = qBinaryFind(m_exceptions_block.begin(), m_exceptions_block.end(), host) != m_exceptions_block.end();
|
|
170 bool eAllow = qBinaryFind(m_exceptions_allow.begin(), m_exceptions_allow.end(), host) != m_exceptions_allow.end();
|
|
171 bool eAllowSession = qBinaryFind(m_exceptions_allowForSession.begin(), m_exceptions_allowForSession.end(), host) != m_exceptions_allowForSession.end();
|
45
|
172
|
67
|
173 bool addedCookies = false;
|
|
174 // pass exceptions
|
|
175 bool acceptInitially = (m_acceptCookies != AcceptNever);
|
|
176 if ((acceptInitially && !eBlock) || (!acceptInitially && (eAllow || eAllowSession))) {
|
|
177 // pass url domain == cookie domain
|
|
178 QDateTime soon = QDateTime.currentDateTime();
|
|
179 soon = soon.addDays(90);
|
77
|
180 foreach(QNetworkCookie cookie; cookieList) {
|
74
|
181 QNetworkCookie[] lst;
|
67
|
182 if (m_keepCookies == KeepUntilTimeLimit && !cookie.isSessionCookie() && cookie.expirationDate() > soon) {
|
|
183 cookie.setExpirationDate(soon);
|
|
184 }
|
|
185 lst += cookie;
|
|
186 if (QNetworkCookieJar.setCookiesFromUrl(lst, url)) {
|
|
187 addedCookies = true;
|
|
188 } else {
|
|
189 // finally force it in if wanted
|
|
190 if (m_acceptCookies == AcceptAlways) {
|
74
|
191 QNetworkCookie[] cookies = allCookies();
|
67
|
192 cookies += cookie;
|
|
193 setAllCookies(cookies);
|
|
194 addedCookies = true;
|
|
195 }
|
|
196 /*
|
|
197 else
|
|
198 qWarning() << "setCookiesFromUrl failed" << url << cookieList.value(0).toRawForm();
|
|
199 */
|
|
200 }
|
|
201 }
|
|
202 }
|
45
|
203
|
67
|
204 if (addedCookies) {
|
|
205 m_saveTimer.changeOccurred();
|
77
|
206 cookiesChanged.emit();
|
67
|
207 }
|
|
208 return addedCookies;
|
|
209 }
|
45
|
210
|
|
211 AcceptPolicy acceptPolicy()
|
|
212 {
|
|
213 if (!m_loaded)
|
77
|
214 (cast(CookieJar) this).load();
|
45
|
215 return m_acceptCookies;
|
|
216 }
|
|
217
|
|
218 void setAcceptPolicy(AcceptPolicy policy)
|
|
219 {
|
|
220 if (!m_loaded)
|
|
221 load();
|
|
222 if (policy == m_acceptCookies)
|
|
223 return;
|
|
224 m_acceptCookies = policy;
|
|
225 m_saveTimer.changeOccurred();
|
|
226 }
|
|
227
|
|
228 KeepPolicy keepPolicy()
|
|
229 {
|
|
230 if (!m_loaded)
|
77
|
231 (cast(CookieJar) this).load();
|
45
|
232 return m_keepCookies;
|
|
233 }
|
|
234
|
|
235 void setKeepPolicy(KeepPolicy policy)
|
|
236 {
|
|
237 if (!m_loaded)
|
|
238 load();
|
|
239 if (policy == m_keepCookies)
|
|
240 return;
|
|
241 m_keepCookies = policy;
|
|
242 m_saveTimer.changeOccurred();
|
|
243 }
|
|
244
|
|
245
|
77
|
246 string[] blockedCookies()
|
45
|
247 {
|
|
248 if (!m_loaded)
|
77
|
249 (cast(CookieJar) this).load();
|
45
|
250 return m_exceptions_block;
|
|
251 }
|
|
252
|
77
|
253 string[] allowedCookies()
|
45
|
254 {
|
|
255 if (!m_loaded)
|
77
|
256 (cast(CookieJar) this).load();
|
45
|
257 return m_exceptions_allow;
|
|
258 }
|
|
259
|
77
|
260 string[] allowForSessionCookies()
|
45
|
261 {
|
|
262 if (!m_loaded)
|
77
|
263 (cast(CookieJar) this).load();
|
45
|
264 return m_exceptions_allowForSession;
|
|
265 }
|
|
266
|
77
|
267 void setBlockedCookies(string[] list)
|
45
|
268 {
|
|
269 if (!m_loaded)
|
|
270 load();
|
|
271 m_exceptions_block = list;
|
|
272 qSort(m_exceptions_block.begin(), m_exceptions_block.end());
|
|
273 m_saveTimer.changeOccurred();
|
|
274 }
|
|
275
|
77
|
276 void setAllowedCookies(string[] list)
|
45
|
277 {
|
|
278 if (!m_loaded)
|
|
279 load();
|
|
280 m_exceptions_allow = list;
|
|
281 qSort(m_exceptions_allow.begin(), m_exceptions_allow.end());
|
|
282 m_saveTimer.changeOccurred();
|
|
283 }
|
|
284
|
77
|
285 void setAllowForSessionCookies(string[] list)
|
45
|
286 {
|
|
287 if (!m_loaded)
|
|
288 load();
|
|
289 m_exceptions_allowForSession = list;
|
|
290 qSort(m_exceptions_allowForSession.begin(), m_exceptions_allowForSession.end());
|
|
291 m_saveTimer.changeOccurred();
|
|
292 }
|
|
293
|
73
|
294 public:
|
45
|
295
|
|
296 void clear()
|
|
297 {
|
74
|
298 setAllCookies(QNetworkCookie[]());
|
45
|
299 m_saveTimer.changeOccurred();
|
77
|
300 cookiesChanged.emit();
|
45
|
301 }
|
|
302
|
|
303 void loadSettings()
|
|
304 {
|
|
305 QSettings settings;
|
80
|
306 settings.beginGroup("cookies");
|
|
307 QByteArray value = settings.value("acceptCookies",
|
|
308 "AcceptOnlyFromSitesNavigatedTo").toByteArray();
|
45
|
309 QMetaEnum acceptPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("AcceptPolicy"));
|
|
310 m_acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ?
|
|
311 AcceptOnlyFromSitesNavigatedTo :
|
77
|
312 cast(AcceptPolicy) acceptPolicyEnum.keyToValue(value);
|
45
|
313
|
80
|
314 value = settings.value("keepCookiesUntil", "KeepUntilExpire").toByteArray();
|
45
|
315 QMetaEnum keepPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("KeepPolicy"));
|
|
316 m_keepCookies = keepPolicyEnum.keyToValue(value) == -1 ?
|
|
317 KeepUntilExpire :
|
77
|
318 cast(KeepPolicy) keepPolicyEnum.keyToValue(value);
|
45
|
319
|
|
320 if (m_keepCookies == KeepUntilExit)
|
77
|
321 setAllCookies(null);
|
45
|
322
|
|
323 m_loaded = true;
|
77
|
324 cookiesChanged.emit();
|
45
|
325 }
|
|
326
|
67
|
327 private:
|
77
|
328
|
67
|
329 void save()
|
|
330 {
|
|
331 if (!m_loaded)
|
|
332 return;
|
|
333 purgeOldCookies();
|
77
|
334 string directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation);
|
67
|
335 if (directory.isEmpty())
|
80
|
336 directory = QDir.homePath() ~ "/." ~ QCoreApplication.applicationName();
|
67
|
337 if (!QFile.exists(directory)) {
|
|
338 QDir dir;
|
|
339 dir.mkpath(directory);
|
|
340 }
|
80
|
341 auto cookieSettings = new QSettings(directory ~ "/cookies.ini", QSettings.IniFormat);
|
74
|
342 QNetworkCookie[] cookies = allCookies();
|
67
|
343 for (int i = cookies.count() - 1; i >= 0; --i) {
|
77
|
344 if (cookies[i].isSessionCookie())
|
67
|
345 cookies.removeAt(i);
|
|
346 }
|
80
|
347 cookieSettings.setValue("cookies", qVariantFromValue<QNetworkCookie[] >(cookies));
|
|
348 cookieSettings.beginGroup("Exceptions");
|
|
349 cookieSettings.setValue("block", m_exceptions_block);
|
|
350 cookieSettings.setValue("allow", m_exceptions_allow);
|
|
351 cookieSettings.setValue("allowForSession", m_exceptions_allowForSession);
|
45
|
352
|
67
|
353 // save cookie settings
|
|
354 QSettings settings;
|
80
|
355 settings.beginGroup("cookies");
|
67
|
356 QMetaEnum acceptPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("AcceptPolicy"));
|
80
|
357 settings.setValue("acceptCookies", acceptPolicyEnum.valueToKey(m_acceptCookies));
|
45
|
358
|
67
|
359 QMetaEnum keepPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("KeepPolicy"));
|
80
|
360 settings.setValue("keepCookiesUntil", keepPolicyEnum.valueToKey(m_keepCookies));
|
67
|
361 }
|
45
|
362
|
|
363 private:
|
67
|
364
|
|
365 void purgeOldCookies()
|
|
366 {
|
74
|
367 QNetworkCookie[] cookies = allCookies();
|
67
|
368 if (cookies.isEmpty())
|
|
369 return;
|
|
370 int oldCount = cookies.count();
|
|
371 QDateTime now = QDateTime.currentDateTime();
|
77
|
372 for (int i = cookies.length - 1; i >= 0; --i) {
|
|
373 if (!cookies[i].isSessionCookie() && cookies[i].expirationDate() < now)
|
67
|
374 cookies.removeAt(i);
|
|
375 }
|
77
|
376 if (oldCount == cookies.length)
|
67
|
377 return;
|
|
378 setAllCookies(cookies);
|
77
|
379 cookiesChanged.emit();
|
67
|
380 }
|
45
|
381
|
|
382 void load()
|
|
383 {
|
|
384 if (m_loaded)
|
|
385 return;
|
|
386 // load cookies and exceptions
|
77
|
387 qRegisterMetaTypeStreamOperators!(QNetworkCookie[])("QNetworkCookie[]");
|
80
|
388 auto cookieSettings = new QSettings(QDesktopServices.storageLocation(QDesktopServices.DataLocation) ~ "/cookies.ini", QSettings.IniFormat);
|
|
389 setAllCookies(cast(QNetworkCookie[]) (cookieSettings.value("cookies")));
|
|
390 cookieSettings.beginGroup("Exceptions");
|
|
391 m_exceptions_block = cookieSettings.value("block").toStringList();
|
|
392 m_exceptions_allow = cookieSettings.value("allow").toStringList();
|
|
393 m_exceptions_allowForSession = cookieSettings.value("allowForSession").toStringList();
|
45
|
394 qSort(m_exceptions_block.begin(), m_exceptions_block.end());
|
|
395 qSort(m_exceptions_allow.begin(), m_exceptions_allow.end());
|
|
396 qSort(m_exceptions_allowForSession.begin(), m_exceptions_allowForSession.end());
|
|
397
|
|
398 loadSettings();
|
|
399 }
|
|
400
|
|
401 bool m_loaded;
|
67
|
402 AutoSaver m_saveTimer;
|
45
|
403
|
|
404 AcceptPolicy m_acceptCookies;
|
|
405 KeepPolicy m_keepCookies;
|
|
406
|
77
|
407 string[] m_exceptions_block;
|
|
408 string[] m_exceptions_allow;
|
|
409 string[] m_exceptions_allowForSession;
|
45
|
410 }
|
|
411
|
|
412 class CookieModel : public QAbstractTableModel
|
|
413 {
|
|
414
|
|
415 public:
|
67
|
416
|
|
417 this(CookieJar jar, QObject parent = null)
|
45
|
418 {
|
|
419 super(parent);
|
|
420 m_cookieJar = cookieJar;
|
67
|
421 m_cookieJar.cookiesChanged.connect(&this.cookiesChanged);
|
45
|
422 m_cookieJar.load();
|
|
423 }
|
|
424
|
67
|
425 QVariant headerData(int section, Qt.Orientation orientation, int role)
|
|
426 {
|
|
427 if (role == Qt.SizeHintRole) {
|
77
|
428 auto font = new QFont;
|
67
|
429 font.setPointSize(10);
|
77
|
430 auto fm = new QFontMetrics(font);
|
67
|
431 int height = fm.height() + fm.height()/3;
|
|
432 int width = fm.width(headerData(section, orientation, Qt.DisplayRole).toString());
|
|
433 return QSize(width, height);
|
|
434 }
|
45
|
435
|
67
|
436 if (orientation == Qt.Horizontal) {
|
|
437 if (role != Qt.DisplayRole)
|
|
438 return QVariant();
|
45
|
439
|
67
|
440 switch (section) {
|
|
441 case 0:
|
|
442 return tr("Website");
|
|
443 case 1:
|
|
444 return tr("Name");
|
|
445 case 2:
|
|
446 return tr("Path");
|
|
447 case 3:
|
|
448 return tr("Secure");
|
|
449 case 4:
|
|
450 return tr("Expires");
|
|
451 case 5:
|
|
452 return tr("Contents");
|
|
453 default:
|
77
|
454 return new QVariant();
|
67
|
455 }
|
|
456 }
|
|
457 return QAbstractTableModel.headerData(section, orientation, role);
|
|
458 }
|
45
|
459
|
67
|
460 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
461 {
|
74
|
462 QNetworkCookie[] lst;
|
67
|
463
|
45
|
464 if (m_cookieJar)
|
67
|
465 lst = m_cookieJar.allCookies();
|
|
466
|
77
|
467 if (index.row() < 0 || index.row() >= lst.length)
|
|
468 return new QVariant();
|
45
|
469
|
|
470 switch (role) {
|
|
471 case Qt.DisplayRole:
|
|
472 case Qt.EditRole: {
|
77
|
473 QNetworkCookie cookie = lst[index.row];
|
45
|
474 switch (index.column()) {
|
|
475 case 0:
|
|
476 return cookie.domain();
|
|
477 case 1:
|
|
478 return cookie.name();
|
|
479 case 2:
|
|
480 return cookie.path();
|
|
481 case 3:
|
|
482 return cookie.isSecure();
|
|
483 case 4:
|
|
484 return cookie.expirationDate();
|
|
485 case 5:
|
|
486 return cookie.value();
|
|
487 }
|
|
488 }
|
|
489 case Qt.FontRole:{
|
77
|
490 auto font = new QFont;
|
45
|
491 font.setPointSize(10);
|
|
492 return font;
|
|
493 }
|
|
494 }
|
|
495
|
|
496 return QVariant();
|
|
497 }
|
|
498
|
67
|
499 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
500 {
|
|
501 return (parent.isValid()) ? 0 : 6;
|
|
502 }
|
|
503
|
|
504
|
67
|
505 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
506 {
|
|
507 return (parent.isValid() || !m_cookieJar) ? 0 : m_cookieJar.allCookies().count();
|
|
508 }
|
|
509
|
67
|
510 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
511 {
|
|
512 if (parent.isValid() || !m_cookieJar)
|
|
513 return false;
|
|
514 int lastRow = row + count - 1;
|
|
515 beginRemoveRows(parent, row, lastRow);
|
74
|
516 QNetworkCookie[] lst = m_cookieJar.allCookies();
|
45
|
517 for (int i = lastRow; i >= row; --i) {
|
|
518 lst.removeAt(i);
|
|
519 }
|
|
520 m_cookieJar.setAllCookies(lst);
|
|
521 endRemoveRows();
|
|
522 return true;
|
|
523 }
|
|
524
|
77
|
525 private:
|
45
|
526
|
|
527 void cookiesChanged()
|
|
528 {
|
|
529 reset();
|
|
530 }
|
|
531
|
|
532 private:
|
77
|
533
|
67
|
534 CookieJar m_cookieJar;
|
45
|
535 }
|
|
536
|
67
|
537
|
|
538 import ui_cookies;
|
|
539 import ui_cookiesexceptions;
|
|
540
|
45
|
541
|
|
542 class CookiesDialog : public QDialog, public Ui_CookiesDialog
|
|
543 {
|
|
544 public:
|
|
545
|
77
|
546 this(CookieJar cookieJar, QWidget parent = this)
|
|
547 //: QDialog(parent)
|
45
|
548 {
|
|
549 setupUi(this);
|
|
550 setWindowFlags(Qt.Sheet);
|
67
|
551 CookieModel model = new CookieModel(cookieJar, this);
|
45
|
552 m_proxyModel = new QSortFilterProxyModel(this);
|
67
|
553 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
|
554 removeButton.clicked.connect(&cookiesTable.removeOne);
|
|
555 removeAllButton.clicked.connect(&cookiesTable.removeAll);
|
45
|
556 m_proxyModel.setSourceModel(model);
|
|
557 cookiesTable.verticalHeader().hide();
|
67
|
558 cookiesTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
559 cookiesTable.setModel(m_proxyModel);
|
|
560 cookiesTable.setAlternatingRowColors(true);
|
|
561 cookiesTable.setTextElideMode(Qt.ElideMiddle);
|
|
562 cookiesTable.setShowGrid(false);
|
|
563 cookiesTable.setSortingEnabled(true);
|
|
564 QFont f = font();
|
|
565 f.setPointSize(10);
|
|
566 QFontMetrics fm(f);
|
|
567 int height = fm.height() + fm.height()/3;
|
|
568 cookiesTable.verticalHeader().setDefaultSectionSize(height);
|
|
569 cookiesTable.verticalHeader().setMinimumSectionSize(-1);
|
|
570 for (int i = 0; i < model.columnCount(); ++i){
|
|
571 int header = cookiesTable.horizontalHeader().sectionSizeHint(i);
|
|
572 switch (i) {
|
|
573 case 0:
|
80
|
574 header = fm.width("averagehost.domain.com");
|
45
|
575 break;
|
|
576 case 1:
|
80
|
577 header = fm.width("_session_id");
|
45
|
578 break;
|
|
579 case 4:
|
67
|
580 header = fm.width(QDateTime.currentDateTime().toString(Qt.LocalDate));
|
45
|
581 break;
|
|
582 }
|
80
|
583 int buffer = fm.width("xx");
|
45
|
584 header += buffer;
|
|
585 cookiesTable.horizontalHeader().resizeSection(i, header);
|
|
586 }
|
|
587 cookiesTable.horizontalHeader().setStretchLastSection(true);
|
|
588 }
|
|
589
|
|
590 private:
|
|
591
|
67
|
592 QSortFilterProxyModel m_proxyModel;
|
45
|
593 }
|
|
594
|
|
595 class CookieExceptionsModel : public QAbstractTableModel
|
|
596 {
|
67
|
597 public:
|
45
|
598
|
67
|
599 this(CookieJar cookieJar, QObject parent = null)
|
45
|
600 {
|
|
601 super(parent);
|
|
602 m_cookieJar = cookiejar;
|
|
603 m_allowedCookies = m_cookieJar.allowedCookies();
|
|
604 m_blockedCookies = m_cookieJar.blockedCookies();
|
|
605 m_sessionCookies = m_cookieJar.allowForSessionCookies();
|
|
606 }
|
|
607
|
|
608 QVariant headerData(int section, Qt.Orientation orientation, int role)
|
|
609 {
|
|
610 if (role == Qt.SizeHintRole) {
|
77
|
611 auto font = new QFont;
|
45
|
612 font.setPointSize(10);
|
77
|
613 auto fm =new QFontMetrics(font);
|
45
|
614 int height = fm.height() + fm.height()/3;
|
|
615 int width = fm.width(headerData(section, orientation, Qt.DisplayRole).toString());
|
|
616 return QSize(width, height);
|
|
617 }
|
|
618
|
|
619 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) {
|
|
620 switch (section) {
|
|
621 case 0:
|
|
622 return tr("Website");
|
|
623 case 1:
|
|
624 return tr("Status");
|
|
625 }
|
|
626 }
|
67
|
627 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
628 }
|
|
629
|
67
|
630 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
631 {
|
|
632 if (index.row() < 0 || index.row() >= rowCount())
|
67
|
633 return QVariant();
|
45
|
634
|
|
635 switch (role) {
|
|
636 case Qt.DisplayRole:
|
|
637 case Qt.EditRole: {
|
67
|
638 int row = index.row();
|
77
|
639 if (row < m_allowedCookies.length) {
|
67
|
640 switch (index.column()) {
|
|
641 case 0:
|
77
|
642 return m_allowedCookies[row];
|
67
|
643 case 1:
|
|
644 return tr("Allow");
|
|
645 }
|
|
646 }
|
77
|
647 row = row - m_allowedCookies.length;
|
|
648 if (row < m_blockedCookies.length) {
|
67
|
649 switch (index.column()) {
|
|
650 case 0:
|
77
|
651 return m_blockedCookies[row];
|
67
|
652 case 1:
|
|
653 return tr("Block");
|
|
654 }
|
|
655 }
|
77
|
656 row = row - m_blockedCookies.length;
|
|
657 if (row < m_sessionCookies.length) {
|
67
|
658 switch (index.column()) {
|
|
659 case 0:
|
77
|
660 return m_sessionCookies[row];
|
67
|
661 case 1:
|
|
662 return tr("Allow For Session");
|
|
663 }
|
45
|
664 }
|
|
665 }
|
|
666 case Qt.FontRole:{
|
77
|
667 auto font = new QFont;
|
67
|
668 font.setPointSize(10);
|
|
669 return font;
|
45
|
670 }
|
|
671 }
|
|
672 return QVariant();
|
|
673 }
|
|
674
|
67
|
675 int columnCount(QModelIndex parent = QModelIndex());
|
45
|
676 {
|
|
677 return (parent.isValid()) ? 0 : 2;
|
|
678 }
|
|
679
|
67
|
680 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
681 {
|
77
|
682 return (parent.isValid() || !m_cookieJar) ? 0 : m_allowedCookies.length + m_blockedCookies.length + m_sessionCookies.length;
|
45
|
683 }
|
|
684
|
67
|
685 bool removeRows(int row, int count, QModelIndex parent)
|
45
|
686 {
|
|
687 if (parent.isValid() || !m_cookieJar)
|
|
688 return false;
|
|
689
|
|
690 int lastRow = row + count - 1;
|
|
691 beginRemoveRows(parent, row, lastRow);
|
|
692 for (int i = lastRow; i >= row; --i) {
|
77
|
693 if (i < m_allowedCookies.length) {
|
45
|
694 m_allowedCookies.removeAt(row);
|
|
695 continue;
|
|
696 }
|
77
|
697 i = i - m_allowedCookies.length;
|
|
698 if (i < m_blockedCookies.length) {
|
45
|
699 m_blockedCookies.removeAt(row);
|
|
700 continue;
|
|
701 }
|
77
|
702 i = i - m_blockedCookies.length;
|
|
703 if (i < m_sessionCookies.length) {
|
45
|
704 m_sessionCookies.removeAt(row);
|
|
705 continue;
|
|
706 }
|
|
707 }
|
|
708
|
|
709 m_cookieJar.setAllowedCookies(m_allowedCookies);
|
|
710 m_cookieJar.setBlockedCookies(m_blockedCookies);
|
|
711 m_cookieJar.setAllowForSessionCookies(m_sessionCookies);
|
|
712 endRemoveRows();
|
|
713 return true;
|
|
714 }
|
67
|
715
|
45
|
716 private:
|
67
|
717
|
|
718 CookieJar m_cookieJar;
|
45
|
719
|
|
720 // Domains we allow, Domains we block, Domains we allow for this session
|
77
|
721 string[] m_allowedCookies;
|
|
722 string[] m_blockedCookies;
|
|
723 string[] m_sessionCookies;
|
45
|
724 }
|
|
725
|
67
|
726
|
45
|
727 class CookiesExceptionsDialog : public QDialog, public Ui_CookiesExceptionsDialog
|
|
728 {
|
|
729
|
|
730 public:
|
67
|
731
|
|
732 this(CookieJar cookieJar, QWidget parent = null)
|
77
|
733 //: QDialog(parent)
|
45
|
734 {
|
|
735 m_cookieJar = cookieJar;
|
|
736 setupUi(this);
|
|
737 setWindowFlags(Qt.Sheet);
|
67
|
738 removeButton.clicked.connect(&exceptionTable.removeOne);
|
|
739 removeAllButton.clicked.connect(&exceptionTable.removeAll);
|
45
|
740 exceptionTable.verticalHeader().hide();
|
67
|
741 exceptionTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
742 exceptionTable.setAlternatingRowColors(true);
|
|
743 exceptionTable.setTextElideMode(Qt.ElideMiddle);
|
|
744 exceptionTable.setShowGrid(false);
|
|
745 exceptionTable.setSortingEnabled(true);
|
|
746 m_exceptionsModel = new CookieExceptionsModel(cookieJar, this);
|
|
747 m_proxyModel = new QSortFilterProxyModel(this);
|
|
748 m_proxyModel.setSourceModel(m_exceptionsModel);
|
67
|
749 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
45
|
750 exceptionTable.setModel(m_proxyModel);
|
|
751
|
67
|
752 CookieModel cookieModel = new CookieModel(cookieJar, this);
|
45
|
753 domainLineEdit.setCompleter(new QCompleter(cookieModel, domainLineEdit));
|
|
754
|
67
|
755 domainLineEdit.textChanged.connect(&this.textChanged);
|
|
756 blockButton.clicked.connect(&this.block);
|
|
757 allowButton.clicked.connect(&this.allow);
|
|
758 allowForSessionButton.clicked.connect(&this.allowForSession);
|
45
|
759
|
|
760 QFont f = font();
|
|
761 f.setPointSize(10);
|
77
|
762 auto fm = new QFontMetrics(f);
|
45
|
763 int height = fm.height() + fm.height()/3;
|
|
764 exceptionTable.verticalHeader().setDefaultSectionSize(height);
|
|
765 exceptionTable.verticalHeader().setMinimumSectionSize(-1);
|
67
|
766 for (int i = 0; i < m_exceptionsModel.columnCount(); ++i) {
|
45
|
767 int header = exceptionTable.horizontalHeader().sectionSizeHint(i);
|
|
768 switch (i) {
|
|
769 case 0:
|
80
|
770 header = fm.width("averagebiglonghost.domain.com");
|
45
|
771 break;
|
|
772 case 1:
|
80
|
773 header = fm.width("Allow For Session");
|
45
|
774 break;
|
|
775 }
|
80
|
776 int buffer = fm.width("xx");
|
45
|
777 header += buffer;
|
|
778 exceptionTable.horizontalHeader().resizeSection(i, header);
|
|
779 }
|
|
780 }
|
|
781
|
67
|
782 private:
|
|
783
|
45
|
784 void block()
|
|
785 {
|
|
786 if (domainLineEdit.text().isEmpty())
|
|
787 return;
|
77
|
788 m_exceptionsModel.m_blockedCookies ~= domainLineEdit.text();
|
45
|
789 m_cookieJar.setBlockedCookies(m_exceptionsModel.m_blockedCookies);
|
|
790 m_exceptionsModel.reset();
|
|
791 }
|
|
792
|
|
793 void allow()
|
|
794 {
|
|
795 if (domainLineEdit.text().isEmpty())
|
|
796 return;
|
77
|
797 m_exceptionsModel.m_allowedCookies ~= domainLineEdit.text();
|
45
|
798 m_cookieJar.setAllowedCookies(m_exceptionsModel.m_allowedCookies);
|
|
799 m_exceptionsModel.reset();
|
|
800 }
|
|
801 void allowForSession()
|
|
802 {
|
|
803 if (domainLineEdit.text().isEmpty())
|
|
804 return;
|
77
|
805 m_exceptionsModel.m_sessionCookie ~= domainLineEdit.text();
|
45
|
806 m_cookieJar.setAllowForSessionCookies(m_exceptionsModel.m_sessionCookies);
|
|
807 m_exceptionsModel.reset();
|
|
808 }
|
|
809
|
77
|
810 void textChanged(string text)
|
45
|
811 {
|
|
812 bool enabled = !text.isEmpty();
|
|
813 blockButton.setEnabled(enabled);
|
|
814 allowButton.setEnabled(enabled);
|
|
815 allowForSessionButton.setEnabled(enabled);
|
|
816 }
|
|
817
|
|
818 private:
|
|
819
|
67
|
820 CookieExceptionsModel m_exceptionsModel;
|
|
821 QSortFilterProxyModel m_proxyModel;
|
|
822 CookieJar m_cookieJar;
|
45
|
823 }
|