comparison demos/browser/cookiejar.d @ 67:b5d10b2218da

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