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