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