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.QStringList;
|
|
48 import qt.core.QDateTime;
|
|
49 import qt.core.QDir;
|
|
50 import qt.core.QFile;
|
|
51 import qt.core.QMetaEnum;
|
|
52 import qt.core.QSettings;
|
|
53 import qt.core.QUrl;
|
45
|
54
|
73
|
55 import qt.gui.QDialog;
|
|
56 import qt.gui.QTableView;
|
|
57 import qt.gui.QCompleter;
|
|
58 import qt.gui.QDesktopServices;
|
|
59 import qt.gui.QFont;
|
|
60 import qt.gui.QFontMetrics;
|
|
61 import qt.gui.QHeaderView;
|
|
62 import qt.gui.QKeyEvent;
|
|
63 import qt.gui.QSortFilterProxyModel;
|
45
|
64
|
73
|
65 import QtWebKit.QWebSettings;
|
45
|
66
|
67
|
67 import cookiejar;
|
|
68 import autosaver;
|
45
|
69
|
|
70
|
74
|
71 static const uint JAR_VERSION = 23;
|
45
|
72
|
74
|
73 QDataStream operator<<(QDataStream stream, QNetworkCookie[] list)
|
45
|
74 {
|
67
|
75 stream << JAR_VERSION;
|
|
76 stream << quint32(list.size());
|
|
77 for (int i = 0; i < list.size(); ++i)
|
|
78 stream << list.at(i).toRawForm();
|
|
79 return stream;
|
45
|
80 }
|
|
81
|
74
|
82 QDataStream operator>>(QDataStream stream, QNetworkCookie[] list)
|
45
|
83 {
|
67
|
84 list.clear();
|
45
|
85
|
73
|
86 quint32 version_;
|
|
87 stream >> version_;
|
45
|
88
|
67
|
89 if (version != JAR_VERSION)
|
|
90 return stream;
|
45
|
91
|
67
|
92 quint32 count;
|
|
93 stream >> count;
|
|
94 for(quint32 i = 0; i < count; ++i)
|
|
95 {
|
|
96 QByteArray value;
|
|
97 stream >> value;
|
74
|
98 QNetworkCookie[] newCookies = QNetworkCookie.parseCookies(value);
|
67
|
99 if (newCookies.count() == 0 && value.length() != 0) {
|
|
100 qWarning() << "CookieJar: Unable to parse saved cookie:" << value;
|
|
101 }
|
|
102
|
|
103 for (int j = 0; j < newCookies.count(); ++j)
|
|
104 list.append(newCookies.at(j));
|
|
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 {
|
|
147 CookieJar that = const_cast<CookieJar>(this);
|
|
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
|
67
|
169 QString host = url.host();
|
|
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);
|
|
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();
|
|
207 emit cookiesChanged();
|
|
208 }
|
|
209 return addedCookies;
|
|
210 }
|
45
|
211
|
|
212 AcceptPolicy acceptPolicy()
|
|
213 {
|
|
214 if (!m_loaded)
|
67
|
215 (const_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)
|
67
|
232 (const_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
|
|
247 QStringList blockedCookies()
|
|
248 {
|
|
249 if (!m_loaded)
|
67
|
250 (const_cast<CookieJar>(this)).load();
|
45
|
251 return m_exceptions_block;
|
|
252 }
|
|
253
|
|
254 QStringList allowedCookies()
|
|
255 {
|
|
256 if (!m_loaded)
|
67
|
257 (const_cast<CookieJar>(this)).load();
|
45
|
258 return m_exceptions_allow;
|
|
259 }
|
|
260
|
|
261 QStringList allowForSessionCookies()
|
|
262 {
|
|
263 if (!m_loaded)
|
67
|
264 (const_cast<CookieJar>(this)).load();
|
45
|
265 return m_exceptions_allowForSession;
|
|
266 }
|
|
267
|
67
|
268 void setBlockedCookies(QStringList 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
|
67
|
277 void setAllowedCookies(QStringList 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
|
67
|
286 void setAllowForSessionCookies(QStringList 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();
|
|
301 emit cookiesChanged();
|
|
302 }
|
|
303
|
|
304 void loadSettings()
|
|
305 {
|
|
306 QSettings settings;
|
|
307 settings.beginGroup(QLatin1String("cookies"));
|
|
308 QByteArray value = settings.value(QLatin1String("acceptCookies"),
|
|
309 QLatin1String("AcceptOnlyFromSitesNavigatedTo")).toByteArray();
|
|
310 QMetaEnum acceptPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("AcceptPolicy"));
|
|
311 m_acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ?
|
|
312 AcceptOnlyFromSitesNavigatedTo :
|
|
313 static_cast<AcceptPolicy>(acceptPolicyEnum.keyToValue(value));
|
|
314
|
|
315 value = settings.value(QLatin1String("keepCookiesUntil"), QLatin1String("KeepUntilExpire")).toByteArray();
|
|
316 QMetaEnum keepPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("KeepPolicy"));
|
|
317 m_keepCookies = keepPolicyEnum.keyToValue(value) == -1 ?
|
|
318 KeepUntilExpire :
|
|
319 static_cast<KeepPolicy>(keepPolicyEnum.keyToValue(value));
|
|
320
|
|
321 if (m_keepCookies == KeepUntilExit)
|
74
|
322 setAllCookies(QNetworkCookie[]());
|
45
|
323
|
|
324 m_loaded = true;
|
|
325 emit cookiesChanged();
|
|
326 }
|
|
327
|
67
|
328 private:
|
|
329 void save()
|
|
330 {
|
|
331 if (!m_loaded)
|
|
332 return;
|
|
333 purgeOldCookies();
|
|
334 QString directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation);
|
|
335 if (directory.isEmpty())
|
|
336 directory = QDir.homePath() + QLatin1String("/.") + QCoreApplication.applicationName();
|
|
337 if (!QFile.exists(directory)) {
|
|
338 QDir dir;
|
|
339 dir.mkpath(directory);
|
|
340 }
|
|
341 QSettings cookieSettings(directory + QLatin1String("/cookies.ini"), QSettings.IniFormat);
|
74
|
342 QNetworkCookie[] cookies = allCookies();
|
67
|
343 for (int i = cookies.count() - 1; i >= 0; --i) {
|
|
344 if (cookies.at(i).isSessionCookie())
|
|
345 cookies.removeAt(i);
|
|
346 }
|
74
|
347 cookieSettings.setValue(QLatin1String("cookies"), qVariantFromValue<QNetworkCookie[] >(cookies));
|
67
|
348 cookieSettings.beginGroup(QLatin1String("Exceptions"));
|
|
349 cookieSettings.setValue(QLatin1String("block"), m_exceptions_block);
|
|
350 cookieSettings.setValue(QLatin1String("allow"), m_exceptions_allow);
|
|
351 cookieSettings.setValue(QLatin1String("allowForSession"), m_exceptions_allowForSession);
|
45
|
352
|
67
|
353 // save cookie settings
|
|
354 QSettings settings;
|
|
355 settings.beginGroup(QLatin1String("cookies"));
|
|
356 QMetaEnum acceptPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("AcceptPolicy"));
|
|
357 settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(m_acceptCookies)));
|
45
|
358
|
67
|
359 QMetaEnum keepPolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("KeepPolicy"));
|
|
360 settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(m_keepCookies)));
|
|
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();
|
|
372 for (int i = cookies.count() - 1; i >= 0; --i) {
|
|
373 if (!cookies.at(i).isSessionCookie() && cookies.at(i).expirationDate() < now)
|
|
374 cookies.removeAt(i);
|
|
375 }
|
|
376 if (oldCount == cookies.count())
|
|
377 return;
|
|
378 setAllCookies(cookies);
|
|
379 emit cookiesChanged();
|
|
380 }
|
45
|
381
|
|
382 void load()
|
|
383 {
|
|
384 if (m_loaded)
|
|
385 return;
|
|
386 // load cookies and exceptions
|
74
|
387 qRegisterMetaTypeStreamOperators<QNetworkCookie[] >("QNetworkCookie[]");
|
67
|
388 auto cookieSettings = new QSettings(QDesktopServices.storageLocation(QDesktopServices.DataLocation) + QLatin1String("/cookies.ini"), QSettings.IniFormat);
|
74
|
389 setAllCookies(qvariant_cast<QNetworkCookie[] >(cookieSettings.value(QLatin1String("cookies"))));
|
45
|
390 cookieSettings.beginGroup(QLatin1String("Exceptions"));
|
|
391 m_exceptions_block = cookieSettings.value(QLatin1String("block")).toStringList();
|
|
392 m_exceptions_allow = cookieSettings.value(QLatin1String("allow")).toStringList();
|
|
393 m_exceptions_allowForSession = cookieSettings.value(QLatin1String("allowForSession")).toStringList();
|
|
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
|
|
407 QStringList m_exceptions_block;
|
|
408 QStringList m_exceptions_allow;
|
|
409 QStringList m_exceptions_allowForSession;
|
|
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) {
|
|
428 QFont font;
|
|
429 font.setPointSize(10);
|
|
430 QFontMetrics fm(font);
|
|
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:
|
|
454 return QVariant();
|
|
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
|
45
|
467 if (index.row() < 0 || index.row() >= lst.size())
|
67
|
468 return QVariant();
|
45
|
469
|
|
470 switch (role) {
|
|
471 case Qt.DisplayRole:
|
|
472 case Qt.EditRole: {
|
|
473 QNetworkCookie cookie = lst.at(index.row());
|
|
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:{
|
|
490 QFont font;
|
|
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
|
|
525 private slots:
|
|
526
|
|
527 void cookiesChanged()
|
|
528 {
|
|
529 reset();
|
|
530 }
|
|
531
|
|
532 private:
|
67
|
533 CookieJar m_cookieJar;
|
45
|
534 }
|
|
535
|
67
|
536
|
|
537 import ui_cookies;
|
|
538 import ui_cookiesexceptions;
|
|
539
|
45
|
540
|
|
541 class CookiesDialog : public QDialog, public Ui_CookiesDialog
|
|
542 {
|
|
543 public:
|
|
544
|
67
|
545 this(CookieJar cookieJar, QWidget parent = this) : QDialog(parent)
|
45
|
546 {
|
|
547 setupUi(this);
|
|
548 setWindowFlags(Qt.Sheet);
|
67
|
549 CookieModel model = new CookieModel(cookieJar, this);
|
45
|
550 m_proxyModel = new QSortFilterProxyModel(this);
|
67
|
551 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
|
552 removeButton.clicked.connect(&cookiesTable.removeOne);
|
|
553 removeAllButton.clicked.connect(&cookiesTable.removeAll);
|
45
|
554 m_proxyModel.setSourceModel(model);
|
|
555 cookiesTable.verticalHeader().hide();
|
67
|
556 cookiesTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
557 cookiesTable.setModel(m_proxyModel);
|
|
558 cookiesTable.setAlternatingRowColors(true);
|
|
559 cookiesTable.setTextElideMode(Qt.ElideMiddle);
|
|
560 cookiesTable.setShowGrid(false);
|
|
561 cookiesTable.setSortingEnabled(true);
|
|
562 QFont f = font();
|
|
563 f.setPointSize(10);
|
|
564 QFontMetrics fm(f);
|
|
565 int height = fm.height() + fm.height()/3;
|
|
566 cookiesTable.verticalHeader().setDefaultSectionSize(height);
|
|
567 cookiesTable.verticalHeader().setMinimumSectionSize(-1);
|
|
568 for (int i = 0; i < model.columnCount(); ++i){
|
|
569 int header = cookiesTable.horizontalHeader().sectionSizeHint(i);
|
|
570 switch (i) {
|
|
571 case 0:
|
|
572 header = fm.width(QLatin1String("averagehost.domain.com"));
|
|
573 break;
|
|
574 case 1:
|
|
575 header = fm.width(QLatin1String("_session_id"));
|
|
576 break;
|
|
577 case 4:
|
67
|
578 header = fm.width(QDateTime.currentDateTime().toString(Qt.LocalDate));
|
45
|
579 break;
|
|
580 }
|
|
581 int buffer = fm.width(QLatin1String("xx"));
|
|
582 header += buffer;
|
|
583 cookiesTable.horizontalHeader().resizeSection(i, header);
|
|
584 }
|
|
585 cookiesTable.horizontalHeader().setStretchLastSection(true);
|
|
586 }
|
|
587
|
|
588 private:
|
|
589
|
67
|
590 QSortFilterProxyModel m_proxyModel;
|
45
|
591 }
|
|
592
|
|
593 class CookieExceptionsModel : public QAbstractTableModel
|
|
594 {
|
67
|
595 public:
|
45
|
596
|
67
|
597 this(CookieJar cookieJar, QObject parent = null)
|
45
|
598 {
|
|
599 super(parent);
|
|
600 m_cookieJar = cookiejar;
|
|
601 m_allowedCookies = m_cookieJar.allowedCookies();
|
|
602 m_blockedCookies = m_cookieJar.blockedCookies();
|
|
603 m_sessionCookies = m_cookieJar.allowForSessionCookies();
|
|
604 }
|
|
605
|
|
606 QVariant headerData(int section, Qt.Orientation orientation, int role)
|
|
607 {
|
|
608 if (role == Qt.SizeHintRole) {
|
|
609 QFont font;
|
|
610 font.setPointSize(10);
|
|
611 QFontMetrics fm(font);
|
|
612 int height = fm.height() + fm.height()/3;
|
|
613 int width = fm.width(headerData(section, orientation, Qt.DisplayRole).toString());
|
|
614 return QSize(width, height);
|
|
615 }
|
|
616
|
|
617 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) {
|
|
618 switch (section) {
|
|
619 case 0:
|
|
620 return tr("Website");
|
|
621 case 1:
|
|
622 return tr("Status");
|
|
623 }
|
|
624 }
|
67
|
625 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
626 }
|
|
627
|
67
|
628 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
629 {
|
|
630 if (index.row() < 0 || index.row() >= rowCount())
|
67
|
631 return QVariant();
|
45
|
632
|
|
633 switch (role) {
|
|
634 case Qt.DisplayRole:
|
|
635 case Qt.EditRole: {
|
67
|
636 int row = index.row();
|
|
637 if (row < m_allowedCookies.count()) {
|
|
638 switch (index.column()) {
|
|
639 case 0:
|
|
640 return m_allowedCookies.at(row);
|
|
641 case 1:
|
|
642 return tr("Allow");
|
|
643 }
|
|
644 }
|
|
645 row = row - m_allowedCookies.count();
|
|
646 if (row < m_blockedCookies.count()) {
|
|
647 switch (index.column()) {
|
|
648 case 0:
|
|
649 return m_blockedCookies.at(row);
|
|
650 case 1:
|
|
651 return tr("Block");
|
|
652 }
|
|
653 }
|
|
654 row = row - m_blockedCookies.count();
|
|
655 if (row < m_sessionCookies.count()) {
|
|
656 switch (index.column()) {
|
|
657 case 0:
|
|
658 return m_sessionCookies.at(row);
|
|
659 case 1:
|
|
660 return tr("Allow For Session");
|
|
661 }
|
45
|
662 }
|
|
663 }
|
|
664 case Qt.FontRole:{
|
67
|
665 QFont font;
|
|
666 font.setPointSize(10);
|
|
667 return font;
|
45
|
668 }
|
|
669 }
|
|
670 return QVariant();
|
|
671 }
|
|
672
|
67
|
673 int columnCount(QModelIndex parent = QModelIndex());
|
45
|
674 {
|
|
675 return (parent.isValid()) ? 0 : 2;
|
|
676 }
|
|
677
|
67
|
678 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
679 {
|
|
680 return (parent.isValid() || !m_cookieJar) ? 0 : m_allowedCookies.count() + m_blockedCookies.count() + m_sessionCookies.count();
|
|
681 }
|
|
682
|
67
|
683 bool removeRows(int row, int count, QModelIndex parent)
|
45
|
684 {
|
|
685 if (parent.isValid() || !m_cookieJar)
|
|
686 return false;
|
|
687
|
|
688 int lastRow = row + count - 1;
|
|
689 beginRemoveRows(parent, row, lastRow);
|
|
690 for (int i = lastRow; i >= row; --i) {
|
|
691 if (i < m_allowedCookies.count()) {
|
|
692 m_allowedCookies.removeAt(row);
|
|
693 continue;
|
|
694 }
|
|
695 i = i - m_allowedCookies.count();
|
|
696 if (i < m_blockedCookies.count()) {
|
|
697 m_blockedCookies.removeAt(row);
|
|
698 continue;
|
|
699 }
|
|
700 i = i - m_blockedCookies.count();
|
|
701 if (i < m_sessionCookies.count()) {
|
|
702 m_sessionCookies.removeAt(row);
|
|
703 continue;
|
|
704 }
|
|
705 }
|
|
706
|
|
707 m_cookieJar.setAllowedCookies(m_allowedCookies);
|
|
708 m_cookieJar.setBlockedCookies(m_blockedCookies);
|
|
709 m_cookieJar.setAllowForSessionCookies(m_sessionCookies);
|
|
710 endRemoveRows();
|
|
711 return true;
|
|
712 }
|
67
|
713
|
45
|
714 private:
|
67
|
715
|
|
716 CookieJar m_cookieJar;
|
45
|
717
|
|
718 // Domains we allow, Domains we block, Domains we allow for this session
|
|
719 QStringList m_allowedCookies;
|
|
720 QStringList m_blockedCookies;
|
|
721 QStringList m_sessionCookies;
|
|
722 }
|
|
723
|
67
|
724
|
45
|
725 class CookiesExceptionsDialog : public QDialog, public Ui_CookiesExceptionsDialog
|
|
726 {
|
|
727
|
|
728 public:
|
67
|
729
|
|
730 this(CookieJar cookieJar, QWidget parent = null)
|
45
|
731 : QDialog(parent)
|
|
732 {
|
|
733 m_cookieJar = cookieJar;
|
|
734 setupUi(this);
|
|
735 setWindowFlags(Qt.Sheet);
|
67
|
736 removeButton.clicked.connect(&exceptionTable.removeOne);
|
|
737 removeAllButton.clicked.connect(&exceptionTable.removeAll);
|
45
|
738 exceptionTable.verticalHeader().hide();
|
67
|
739 exceptionTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
740 exceptionTable.setAlternatingRowColors(true);
|
|
741 exceptionTable.setTextElideMode(Qt.ElideMiddle);
|
|
742 exceptionTable.setShowGrid(false);
|
|
743 exceptionTable.setSortingEnabled(true);
|
|
744 m_exceptionsModel = new CookieExceptionsModel(cookieJar, this);
|
|
745 m_proxyModel = new QSortFilterProxyModel(this);
|
|
746 m_proxyModel.setSourceModel(m_exceptionsModel);
|
67
|
747 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
45
|
748 exceptionTable.setModel(m_proxyModel);
|
|
749
|
67
|
750 CookieModel cookieModel = new CookieModel(cookieJar, this);
|
45
|
751 domainLineEdit.setCompleter(new QCompleter(cookieModel, domainLineEdit));
|
|
752
|
67
|
753 domainLineEdit.textChanged.connect(&this.textChanged);
|
|
754 blockButton.clicked.connect(&this.block);
|
|
755 allowButton.clicked.connect(&this.allow);
|
|
756 allowForSessionButton.clicked.connect(&this.allowForSession);
|
45
|
757
|
|
758 QFont f = font();
|
|
759 f.setPointSize(10);
|
|
760 QFontMetrics fm(f);
|
|
761 int height = fm.height() + fm.height()/3;
|
|
762 exceptionTable.verticalHeader().setDefaultSectionSize(height);
|
|
763 exceptionTable.verticalHeader().setMinimumSectionSize(-1);
|
67
|
764 for (int i = 0; i < m_exceptionsModel.columnCount(); ++i) {
|
45
|
765 int header = exceptionTable.horizontalHeader().sectionSizeHint(i);
|
|
766 switch (i) {
|
|
767 case 0:
|
|
768 header = fm.width(QLatin1String("averagebiglonghost.domain.com"));
|
|
769 break;
|
|
770 case 1:
|
|
771 header = fm.width(QLatin1String("Allow For Session"));
|
|
772 break;
|
|
773 }
|
|
774 int buffer = fm.width(QLatin1String("xx"));
|
|
775 header += buffer;
|
|
776 exceptionTable.horizontalHeader().resizeSection(i, header);
|
|
777 }
|
|
778 }
|
|
779
|
67
|
780 private:
|
|
781
|
45
|
782 void block()
|
|
783 {
|
|
784 if (domainLineEdit.text().isEmpty())
|
|
785 return;
|
|
786 m_exceptionsModel.m_blockedCookies.append(domainLineEdit.text());
|
|
787 m_cookieJar.setBlockedCookies(m_exceptionsModel.m_blockedCookies);
|
|
788 m_exceptionsModel.reset();
|
|
789 }
|
|
790
|
|
791 void allow()
|
|
792 {
|
|
793 if (domainLineEdit.text().isEmpty())
|
|
794 return;
|
|
795 m_exceptionsModel.m_allowedCookies.append(domainLineEdit.text());
|
|
796 m_cookieJar.setAllowedCookies(m_exceptionsModel.m_allowedCookies);
|
|
797 m_exceptionsModel.reset();
|
|
798 }
|
|
799 void allowForSession()
|
|
800 {
|
|
801 if (domainLineEdit.text().isEmpty())
|
|
802 return;
|
|
803 m_exceptionsModel.m_sessionCookies.append(domainLineEdit.text());
|
|
804 m_cookieJar.setAllowForSessionCookies(m_exceptionsModel.m_sessionCookies);
|
|
805 m_exceptionsModel.reset();
|
|
806 }
|
|
807
|
67
|
808 void textChanged(QString text)
|
45
|
809 {
|
|
810 bool enabled = !text.isEmpty();
|
|
811 blockButton.setEnabled(enabled);
|
|
812 allowButton.setEnabled(enabled);
|
|
813 allowForSessionButton.setEnabled(enabled);
|
|
814 }
|
|
815
|
|
816 private:
|
|
817
|
67
|
818 CookieExceptionsModel m_exceptionsModel;
|
|
819 QSortFilterProxyModel m_proxyModel;
|
|
820 CookieJar m_cookieJar;
|
45
|
821 }
|