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
|
|
71 static const unsigned int JAR_VERSION = 23;
|
|
72
|
67
|
73 QDataStream &operator<<(QDataStream stream, QList<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
|
67
|
82 QDataStream &operator>>(QDataStream stream, QList<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;
|
|
98 QList<QNetworkCookie> newCookies = QNetworkCookie.parseCookies(value);
|
|
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
|
67
|
145 QList<QNetworkCookie> cookiesForUrl(QUrl url)
|
|
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)) {
|
|
153 QList<QNetworkCookie> noCookies;
|
|
154 return noCookies;
|
|
155 }
|
45
|
156
|
67
|
157 return QNetworkCookieJar.cookiesForUrl(url);
|
|
158 }
|
|
159
|
|
160 bool setCookiesFromUrl(QList<QNetworkCookie> cookieList, QUrl url)
|
|
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) {
|
|
182 QList<QNetworkCookie> lst;
|
|
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) {
|
|
192 QList<QNetworkCookie> cookies = allCookies();
|
|
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 {
|
|
299 setAllCookies(QList<QNetworkCookie>());
|
|
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)
|
|
322 setAllCookies(QList<QNetworkCookie>());
|
|
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);
|
|
342 QList<QNetworkCookie> cookies = allCookies();
|
|
343 for (int i = cookies.count() - 1; i >= 0; --i) {
|
|
344 if (cookies.at(i).isSessionCookie())
|
|
345 cookies.removeAt(i);
|
|
346 }
|
|
347 cookieSettings.setValue(QLatin1String("cookies"), qVariantFromValue<QList<QNetworkCookie> >(cookies));
|
|
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 {
|
|
367 QList<QNetworkCookie> cookies = allCookies();
|
|
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
|
|
387 qRegisterMetaTypeStreamOperators<QList<QNetworkCookie> >("QList<QNetworkCookie>");
|
67
|
388 auto cookieSettings = new QSettings(QDesktopServices.storageLocation(QDesktopServices.DataLocation) + QLatin1String("/cookies.ini"), QSettings.IniFormat);
|
45
|
389 setAllCookies(qvariant_cast<QList<QNetworkCookie> >(cookieSettings.value(QLatin1String("cookies"))));
|
|
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 {
|
|
462 QList<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
|
|
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);
|
|
517 QList<QNetworkCookie> lst = m_cookieJar.allCookies();
|
|
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
|
|
526 private slots:
|
|
527
|
|
528 void cookiesChanged()
|
|
529 {
|
|
530 reset();
|
|
531 }
|
|
532
|
|
533 private:
|
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
|
67
|
546 this(CookieJar cookieJar, QWidget parent = this) : QDialog(parent)
|
45
|
547 {
|
|
548 setupUi(this);
|
|
549 setWindowFlags(Qt.Sheet);
|
67
|
550 CookieModel model = new CookieModel(cookieJar, this);
|
45
|
551 m_proxyModel = new QSortFilterProxyModel(this);
|
67
|
552 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
|
553 removeButton.clicked.connect(&cookiesTable.removeOne);
|
|
554 removeAllButton.clicked.connect(&cookiesTable.removeAll);
|
45
|
555 m_proxyModel.setSourceModel(model);
|
|
556 cookiesTable.verticalHeader().hide();
|
67
|
557 cookiesTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
558 cookiesTable.setModel(m_proxyModel);
|
|
559 cookiesTable.setAlternatingRowColors(true);
|
|
560 cookiesTable.setTextElideMode(Qt.ElideMiddle);
|
|
561 cookiesTable.setShowGrid(false);
|
|
562 cookiesTable.setSortingEnabled(true);
|
|
563 QFont f = font();
|
|
564 f.setPointSize(10);
|
|
565 QFontMetrics fm(f);
|
|
566 int height = fm.height() + fm.height()/3;
|
|
567 cookiesTable.verticalHeader().setDefaultSectionSize(height);
|
|
568 cookiesTable.verticalHeader().setMinimumSectionSize(-1);
|
|
569 for (int i = 0; i < model.columnCount(); ++i){
|
|
570 int header = cookiesTable.horizontalHeader().sectionSizeHint(i);
|
|
571 switch (i) {
|
|
572 case 0:
|
|
573 header = fm.width(QLatin1String("averagehost.domain.com"));
|
|
574 break;
|
|
575 case 1:
|
|
576 header = fm.width(QLatin1String("_session_id"));
|
|
577 break;
|
|
578 case 4:
|
67
|
579 header = fm.width(QDateTime.currentDateTime().toString(Qt.LocalDate));
|
45
|
580 break;
|
|
581 }
|
|
582 int buffer = fm.width(QLatin1String("xx"));
|
|
583 header += buffer;
|
|
584 cookiesTable.horizontalHeader().resizeSection(i, header);
|
|
585 }
|
|
586 cookiesTable.horizontalHeader().setStretchLastSection(true);
|
|
587 }
|
|
588
|
|
589 private:
|
|
590
|
67
|
591 QSortFilterProxyModel m_proxyModel;
|
45
|
592 }
|
|
593
|
|
594 class CookieExceptionsModel : public QAbstractTableModel
|
|
595 {
|
67
|
596 public:
|
45
|
597
|
67
|
598 this(CookieJar cookieJar, QObject parent = null)
|
45
|
599 {
|
|
600 super(parent);
|
|
601 m_cookieJar = cookiejar;
|
|
602 m_allowedCookies = m_cookieJar.allowedCookies();
|
|
603 m_blockedCookies = m_cookieJar.blockedCookies();
|
|
604 m_sessionCookies = m_cookieJar.allowForSessionCookies();
|
|
605 }
|
|
606
|
|
607 QVariant headerData(int section, Qt.Orientation orientation, int role)
|
|
608 {
|
|
609 if (role == Qt.SizeHintRole) {
|
|
610 QFont font;
|
|
611 font.setPointSize(10);
|
|
612 QFontMetrics fm(font);
|
|
613 int height = fm.height() + fm.height()/3;
|
|
614 int width = fm.width(headerData(section, orientation, Qt.DisplayRole).toString());
|
|
615 return QSize(width, height);
|
|
616 }
|
|
617
|
|
618 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) {
|
|
619 switch (section) {
|
|
620 case 0:
|
|
621 return tr("Website");
|
|
622 case 1:
|
|
623 return tr("Status");
|
|
624 }
|
|
625 }
|
67
|
626 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
627 }
|
|
628
|
67
|
629 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
630 {
|
|
631 if (index.row() < 0 || index.row() >= rowCount())
|
67
|
632 return QVariant();
|
45
|
633
|
|
634 switch (role) {
|
|
635 case Qt.DisplayRole:
|
|
636 case Qt.EditRole: {
|
67
|
637 int row = index.row();
|
|
638 if (row < m_allowedCookies.count()) {
|
|
639 switch (index.column()) {
|
|
640 case 0:
|
|
641 return m_allowedCookies.at(row);
|
|
642 case 1:
|
|
643 return tr("Allow");
|
|
644 }
|
|
645 }
|
|
646 row = row - m_allowedCookies.count();
|
|
647 if (row < m_blockedCookies.count()) {
|
|
648 switch (index.column()) {
|
|
649 case 0:
|
|
650 return m_blockedCookies.at(row);
|
|
651 case 1:
|
|
652 return tr("Block");
|
|
653 }
|
|
654 }
|
|
655 row = row - m_blockedCookies.count();
|
|
656 if (row < m_sessionCookies.count()) {
|
|
657 switch (index.column()) {
|
|
658 case 0:
|
|
659 return m_sessionCookies.at(row);
|
|
660 case 1:
|
|
661 return tr("Allow For Session");
|
|
662 }
|
45
|
663 }
|
|
664 }
|
|
665 case Qt.FontRole:{
|
67
|
666 QFont font;
|
|
667 font.setPointSize(10);
|
|
668 return font;
|
45
|
669 }
|
|
670 }
|
|
671 return QVariant();
|
|
672 }
|
|
673
|
67
|
674 int columnCount(QModelIndex parent = QModelIndex());
|
45
|
675 {
|
|
676 return (parent.isValid()) ? 0 : 2;
|
|
677 }
|
|
678
|
67
|
679 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
680 {
|
|
681 return (parent.isValid() || !m_cookieJar) ? 0 : m_allowedCookies.count() + m_blockedCookies.count() + m_sessionCookies.count();
|
|
682 }
|
|
683
|
67
|
684 bool removeRows(int row, int count, QModelIndex parent)
|
45
|
685 {
|
|
686 if (parent.isValid() || !m_cookieJar)
|
|
687 return false;
|
|
688
|
|
689 int lastRow = row + count - 1;
|
|
690 beginRemoveRows(parent, row, lastRow);
|
|
691 for (int i = lastRow; i >= row; --i) {
|
|
692 if (i < m_allowedCookies.count()) {
|
|
693 m_allowedCookies.removeAt(row);
|
|
694 continue;
|
|
695 }
|
|
696 i = i - m_allowedCookies.count();
|
|
697 if (i < m_blockedCookies.count()) {
|
|
698 m_blockedCookies.removeAt(row);
|
|
699 continue;
|
|
700 }
|
|
701 i = i - m_blockedCookies.count();
|
|
702 if (i < m_sessionCookies.count()) {
|
|
703 m_sessionCookies.removeAt(row);
|
|
704 continue;
|
|
705 }
|
|
706 }
|
|
707
|
|
708 m_cookieJar.setAllowedCookies(m_allowedCookies);
|
|
709 m_cookieJar.setBlockedCookies(m_blockedCookies);
|
|
710 m_cookieJar.setAllowForSessionCookies(m_sessionCookies);
|
|
711 endRemoveRows();
|
|
712 return true;
|
|
713 }
|
67
|
714
|
45
|
715 private:
|
67
|
716
|
|
717 CookieJar m_cookieJar;
|
45
|
718
|
|
719 // Domains we allow, Domains we block, Domains we allow for this session
|
|
720 QStringList m_allowedCookies;
|
|
721 QStringList m_blockedCookies;
|
|
722 QStringList m_sessionCookies;
|
|
723 }
|
|
724
|
67
|
725
|
45
|
726 class CookiesExceptionsDialog : public QDialog, public Ui_CookiesExceptionsDialog
|
|
727 {
|
|
728
|
|
729 public:
|
67
|
730
|
|
731 this(CookieJar cookieJar, QWidget parent = null)
|
45
|
732 : QDialog(parent)
|
|
733 {
|
|
734 m_cookieJar = cookieJar;
|
|
735 setupUi(this);
|
|
736 setWindowFlags(Qt.Sheet);
|
67
|
737 removeButton.clicked.connect(&exceptionTable.removeOne);
|
|
738 removeAllButton.clicked.connect(&exceptionTable.removeAll);
|
45
|
739 exceptionTable.verticalHeader().hide();
|
67
|
740 exceptionTable.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
741 exceptionTable.setAlternatingRowColors(true);
|
|
742 exceptionTable.setTextElideMode(Qt.ElideMiddle);
|
|
743 exceptionTable.setShowGrid(false);
|
|
744 exceptionTable.setSortingEnabled(true);
|
|
745 m_exceptionsModel = new CookieExceptionsModel(cookieJar, this);
|
|
746 m_proxyModel = new QSortFilterProxyModel(this);
|
|
747 m_proxyModel.setSourceModel(m_exceptionsModel);
|
67
|
748 search.textChanged.connect(&m_proxyModel.setFilterFixedString);
|
45
|
749 exceptionTable.setModel(m_proxyModel);
|
|
750
|
67
|
751 CookieModel cookieModel = new CookieModel(cookieJar, this);
|
45
|
752 domainLineEdit.setCompleter(new QCompleter(cookieModel, domainLineEdit));
|
|
753
|
67
|
754 domainLineEdit.textChanged.connect(&this.textChanged);
|
|
755 blockButton.clicked.connect(&this.block);
|
|
756 allowButton.clicked.connect(&this.allow);
|
|
757 allowForSessionButton.clicked.connect(&this.allowForSession);
|
45
|
758
|
|
759 QFont f = font();
|
|
760 f.setPointSize(10);
|
|
761 QFontMetrics fm(f);
|
|
762 int height = fm.height() + fm.height()/3;
|
|
763 exceptionTable.verticalHeader().setDefaultSectionSize(height);
|
|
764 exceptionTable.verticalHeader().setMinimumSectionSize(-1);
|
67
|
765 for (int i = 0; i < m_exceptionsModel.columnCount(); ++i) {
|
45
|
766 int header = exceptionTable.horizontalHeader().sectionSizeHint(i);
|
|
767 switch (i) {
|
|
768 case 0:
|
|
769 header = fm.width(QLatin1String("averagebiglonghost.domain.com"));
|
|
770 break;
|
|
771 case 1:
|
|
772 header = fm.width(QLatin1String("Allow For Session"));
|
|
773 break;
|
|
774 }
|
|
775 int buffer = fm.width(QLatin1String("xx"));
|
|
776 header += buffer;
|
|
777 exceptionTable.horizontalHeader().resizeSection(i, header);
|
|
778 }
|
|
779 }
|
|
780
|
67
|
781 private:
|
|
782
|
45
|
783 void block()
|
|
784 {
|
|
785 if (domainLineEdit.text().isEmpty())
|
|
786 return;
|
|
787 m_exceptionsModel.m_blockedCookies.append(domainLineEdit.text());
|
|
788 m_cookieJar.setBlockedCookies(m_exceptionsModel.m_blockedCookies);
|
|
789 m_exceptionsModel.reset();
|
|
790 }
|
|
791
|
|
792 void allow()
|
|
793 {
|
|
794 if (domainLineEdit.text().isEmpty())
|
|
795 return;
|
|
796 m_exceptionsModel.m_allowedCookies.append(domainLineEdit.text());
|
|
797 m_cookieJar.setAllowedCookies(m_exceptionsModel.m_allowedCookies);
|
|
798 m_exceptionsModel.reset();
|
|
799 }
|
|
800 void allowForSession()
|
|
801 {
|
|
802 if (domainLineEdit.text().isEmpty())
|
|
803 return;
|
|
804 m_exceptionsModel.m_sessionCookies.append(domainLineEdit.text());
|
|
805 m_cookieJar.setAllowForSessionCookies(m_exceptionsModel.m_sessionCookies);
|
|
806 m_exceptionsModel.reset();
|
|
807 }
|
|
808
|
67
|
809 void textChanged(QString text)
|
45
|
810 {
|
|
811 bool enabled = !text.isEmpty();
|
|
812 blockButton.setEnabled(enabled);
|
|
813 allowButton.setEnabled(enabled);
|
|
814 allowForSessionButton.setEnabled(enabled);
|
|
815 }
|
|
816
|
|
817 private:
|
|
818
|
67
|
819 CookieExceptionsModel m_exceptionsModel;
|
|
820 QSortFilterProxyModel m_proxyModel;
|
|
821 CookieJar m_cookieJar;
|
45
|
822 }
|