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 #ifndef COOKIEJAR_H 35 #define COOKIEJAR_H 36 37 #include <QtNetwork/QNetworkCookieJar> 38 39 #include <QtCore/QAbstractItemModel> 40 #include <QtCore/QStringList> 41 42 #include <QtWidgets/QDialog> 43 #include <QtWidgets/QTableView> 44 45 QT_BEGIN_NAMESPACE 46 class QSortFilterProxyModel; 47 class QKeyEvent; 48 QT_END_NAMESPACE 49 50 class AutoSaver; 51 52 class CookieJar : public QNetworkCookieJar 53 { 54 friend class CookieModel; 55 Q_OBJECT 56 Q_PROPERTY(AcceptPolicy acceptPolicy READ acceptPolicy WRITE setAcceptPolicy) 57 Q_PROPERTY(KeepPolicy keepPolicy READ keepPolicy WRITE setKeepPolicy) 58 Q_PROPERTY(QStringList blockedCookies READ blockedCookies WRITE setBlockedCookies) 59 Q_PROPERTY(QStringList allowedCookies READ allowedCookies WRITE setAllowedCookies) 60 Q_PROPERTY(QStringList allowForSessionCookies READ allowForSessionCookies WRITE setAllowForSessionCookies) 61 Q_ENUMS(KeepPolicy) 62 Q_ENUMS(AcceptPolicy) 63 64 signals: 65 void cookiesChanged(); 66 67 public: 68 enum AcceptPolicy { 69 AcceptAlways, 70 AcceptNever, 71 AcceptOnlyFromSitesNavigatedTo 72 }; 73 74 enum KeepPolicy { 75 KeepUntilExpire, 76 KeepUntilExit, 77 KeepUntilTimeLimit 78 }; 79 80 CookieJar(QObject *parent = 0); 81 ~CookieJar(); 82 83 QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const; 84 bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url); 85 86 AcceptPolicy acceptPolicy() const; 87 void setAcceptPolicy(AcceptPolicy policy); 88 89 KeepPolicy keepPolicy() const; 90 void setKeepPolicy(KeepPolicy policy); 91 92 QStringList blockedCookies() const; 93 QStringList allowedCookies() const; 94 QStringList allowForSessionCookies() const; 95 96 void setBlockedCookies(const QStringList &list); 97 void setAllowedCookies(const QStringList &list); 98 void setAllowForSessionCookies(const QStringList &list); 99 100 public slots: 101 void clear(); 102 void loadSettings(); 103 104 private slots: 105 void save(); 106 107 private: 108 void purgeOldCookies(); 109 void load(); 110 bool m_loaded; 111 AutoSaver *m_saveTimer; 112 113 AcceptPolicy m_acceptCookies; 114 KeepPolicy m_keepCookies; 115 116 QStringList m_exceptions_block; 117 QStringList m_exceptions_allow; 118 QStringList m_exceptions_allowForSession; 119 }; 120 121 class CookieModel : public QAbstractTableModel 122 { 123 Q_OBJECT 124 125 public: 126 CookieModel(CookieJar *jar, QObject *parent = 0); 127 QVariant headerData(int section, Qt::Orientation orientation, int role) const; 128 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 129 int columnCount(const QModelIndex &parent = QModelIndex()) const; 130 int rowCount(const QModelIndex &parent = QModelIndex()) const; 131 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); 132 133 private slots: 134 void cookiesChanged(); 135 136 private: 137 CookieJar *m_cookieJar; 138 }; 139 140 #include "ui_cookies.h" 141 #include "ui_cookiesexceptions.h" 142 143 class CookiesDialog : public QDialog, public Ui_CookiesDialog 144 { 145 Q_OBJECT 146 147 public: 148 CookiesDialog(CookieJar *cookieJar, QWidget *parent = 0); 149 150 private: 151 QSortFilterProxyModel *m_proxyModel; 152 }; 153 154 class CookieExceptionsModel : public QAbstractTableModel 155 { 156 Q_OBJECT 157 friend class CookiesExceptionsDialog; 158 159 public: 160 CookieExceptionsModel(CookieJar *cookieJar, QObject *parent = 0); 161 QVariant headerData(int section, Qt::Orientation orientation, int role) const; 162 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 163 int columnCount(const QModelIndex &parent = QModelIndex()) const; 164 int rowCount(const QModelIndex &parent = QModelIndex()) const; 165 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); 166 167 private: 168 CookieJar *m_cookieJar; 169 170 // Domains we allow, Domains we block, Domains we allow for this session 171 QStringList m_allowedCookies; 172 QStringList m_blockedCookies; 173 QStringList m_sessionCookies; 174 }; 175 176 class CookiesExceptionsDialog : public QDialog, public Ui_CookiesExceptionsDialog 177 { 178 Q_OBJECT 179 180 public: 181 CookiesExceptionsDialog(CookieJar *cookieJar, QWidget *parent = 0); 182 183 private slots: 184 void block(); 185 void allow(); 186 void allowForSession(); 187 void textChanged(const QString &text); 188 189 private: 190 CookieExceptionsModel *m_exceptionsModel; 191 QSortFilterProxyModel *m_proxyModel; 192 CookieJar *m_cookieJar; 193 }; 194 195 #endif // COOKIEJAR_H 196 197