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