Mercurial > projects > qtd
comparison demos/browser/cookiejar.d @ 74:37caa90ce503
more porting
author | mandel |
---|---|
date | Fri, 22 May 2009 23:43:58 +0000 |
parents | 7bfd46c330dc |
children | 0654fc9bac95 |
comparison
equal
deleted
inserted
replaced
73:7bfd46c330dc | 74:37caa90ce503 |
---|---|
66 | 66 |
67 import cookiejar; | 67 import cookiejar; |
68 import autosaver; | 68 import autosaver; |
69 | 69 |
70 | 70 |
71 static const unsigned int JAR_VERSION = 23; | 71 static const uint JAR_VERSION = 23; |
72 | 72 |
73 QDataStream &operator<<(QDataStream stream, QList<QNetworkCookie> list) | 73 QDataStream operator<<(QDataStream stream, QNetworkCookie[] list) |
74 { | 74 { |
75 stream << JAR_VERSION; | 75 stream << JAR_VERSION; |
76 stream << quint32(list.size()); | 76 stream << quint32(list.size()); |
77 for (int i = 0; i < list.size(); ++i) | 77 for (int i = 0; i < list.size(); ++i) |
78 stream << list.at(i).toRawForm(); | 78 stream << list.at(i).toRawForm(); |
79 return stream; | 79 return stream; |
80 } | 80 } |
81 | 81 |
82 QDataStream &operator>>(QDataStream stream, QList<QNetworkCookie> list) | 82 QDataStream operator>>(QDataStream stream, QNetworkCookie[] list) |
83 { | 83 { |
84 list.clear(); | 84 list.clear(); |
85 | 85 |
86 quint32 version_; | 86 quint32 version_; |
87 stream >> version_; | 87 stream >> version_; |
93 stream >> count; | 93 stream >> count; |
94 for(quint32 i = 0; i < count; ++i) | 94 for(quint32 i = 0; i < count; ++i) |
95 { | 95 { |
96 QByteArray value; | 96 QByteArray value; |
97 stream >> value; | 97 stream >> value; |
98 QList<QNetworkCookie> newCookies = QNetworkCookie.parseCookies(value); | 98 QNetworkCookie[] newCookies = QNetworkCookie.parseCookies(value); |
99 if (newCookies.count() == 0 && value.length() != 0) { | 99 if (newCookies.count() == 0 && value.length() != 0) { |
100 qWarning() << "CookieJar: Unable to parse saved cookie:" << value; | 100 qWarning() << "CookieJar: Unable to parse saved cookie:" << value; |
101 } | 101 } |
102 | 102 |
103 for (int j = 0; j < newCookies.count(); ++j) | 103 for (int j = 0; j < newCookies.count(); ++j) |
140 if (m_keepCookies == KeepUntilExit) | 140 if (m_keepCookies == KeepUntilExit) |
141 clear(); | 141 clear(); |
142 m_saveTimer.saveIfNeccessary(); | 142 m_saveTimer.saveIfNeccessary(); |
143 } | 143 } |
144 | 144 |
145 QList<QNetworkCookie> cookiesForUrl(QUrl url) | 145 QNetworkCookie[] cookiesForUrl(QUrl url) |
146 { | 146 { |
147 CookieJar that = const_cast<CookieJar>(this); | 147 CookieJar that = const_cast<CookieJar>(this); |
148 if (!m_loaded) | 148 if (!m_loaded) |
149 that.load(); | 149 that.load(); |
150 | 150 |
151 QWebSettings globalSettings = QWebSettings.globalSettings(); | 151 QWebSettings globalSettings = QWebSettings.globalSettings(); |
152 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled)) { | 152 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled)) { |
153 QList<QNetworkCookie> noCookies; | 153 QNetworkCookie[] noCookies; |
154 return noCookies; | 154 return noCookies; |
155 } | 155 } |
156 | 156 |
157 return QNetworkCookieJar.cookiesForUrl(url); | 157 return QNetworkCookieJar.cookiesForUrl(url); |
158 } | 158 } |
159 | 159 |
160 bool setCookiesFromUrl(QList<QNetworkCookie> cookieList, QUrl url) | 160 bool setCookiesFromUrl(QNetworkCookie[] cookieList, QUrl url) |
161 { | 161 { |
162 if (!m_loaded) | 162 if (!m_loaded) |
163 load(); | 163 load(); |
164 | 164 |
165 QWebSettings globalSettings = QWebSettings.globalSettings(); | 165 QWebSettings globalSettings = QWebSettings.globalSettings(); |
177 if ((acceptInitially && !eBlock) || (!acceptInitially && (eAllow || eAllowSession))) { | 177 if ((acceptInitially && !eBlock) || (!acceptInitially && (eAllow || eAllowSession))) { |
178 // pass url domain == cookie domain | 178 // pass url domain == cookie domain |
179 QDateTime soon = QDateTime.currentDateTime(); | 179 QDateTime soon = QDateTime.currentDateTime(); |
180 soon = soon.addDays(90); | 180 soon = soon.addDays(90); |
181 foreach(QNetworkCookie cookie, cookieList) { | 181 foreach(QNetworkCookie cookie, cookieList) { |
182 QList<QNetworkCookie> lst; | 182 QNetworkCookie[] lst; |
183 if (m_keepCookies == KeepUntilTimeLimit && !cookie.isSessionCookie() && cookie.expirationDate() > soon) { | 183 if (m_keepCookies == KeepUntilTimeLimit && !cookie.isSessionCookie() && cookie.expirationDate() > soon) { |
184 cookie.setExpirationDate(soon); | 184 cookie.setExpirationDate(soon); |
185 } | 185 } |
186 lst += cookie; | 186 lst += cookie; |
187 if (QNetworkCookieJar.setCookiesFromUrl(lst, url)) { | 187 if (QNetworkCookieJar.setCookiesFromUrl(lst, url)) { |
188 addedCookies = true; | 188 addedCookies = true; |
189 } else { | 189 } else { |
190 // finally force it in if wanted | 190 // finally force it in if wanted |
191 if (m_acceptCookies == AcceptAlways) { | 191 if (m_acceptCookies == AcceptAlways) { |
192 QList<QNetworkCookie> cookies = allCookies(); | 192 QNetworkCookie[] cookies = allCookies(); |
193 cookies += cookie; | 193 cookies += cookie; |
194 setAllCookies(cookies); | 194 setAllCookies(cookies); |
195 addedCookies = true; | 195 addedCookies = true; |
196 } | 196 } |
197 /* | 197 /* |
294 | 294 |
295 public: | 295 public: |
296 | 296 |
297 void clear() | 297 void clear() |
298 { | 298 { |
299 setAllCookies(QList<QNetworkCookie>()); | 299 setAllCookies(QNetworkCookie[]()); |
300 m_saveTimer.changeOccurred(); | 300 m_saveTimer.changeOccurred(); |
301 emit cookiesChanged(); | 301 emit cookiesChanged(); |
302 } | 302 } |
303 | 303 |
304 void loadSettings() | 304 void loadSettings() |
317 m_keepCookies = keepPolicyEnum.keyToValue(value) == -1 ? | 317 m_keepCookies = keepPolicyEnum.keyToValue(value) == -1 ? |
318 KeepUntilExpire : | 318 KeepUntilExpire : |
319 static_cast<KeepPolicy>(keepPolicyEnum.keyToValue(value)); | 319 static_cast<KeepPolicy>(keepPolicyEnum.keyToValue(value)); |
320 | 320 |
321 if (m_keepCookies == KeepUntilExit) | 321 if (m_keepCookies == KeepUntilExit) |
322 setAllCookies(QList<QNetworkCookie>()); | 322 setAllCookies(QNetworkCookie[]()); |
323 | 323 |
324 m_loaded = true; | 324 m_loaded = true; |
325 emit cookiesChanged(); | 325 emit cookiesChanged(); |
326 } | 326 } |
327 | 327 |
337 if (!QFile.exists(directory)) { | 337 if (!QFile.exists(directory)) { |
338 QDir dir; | 338 QDir dir; |
339 dir.mkpath(directory); | 339 dir.mkpath(directory); |
340 } | 340 } |
341 QSettings cookieSettings(directory + QLatin1String("/cookies.ini"), QSettings.IniFormat); | 341 QSettings cookieSettings(directory + QLatin1String("/cookies.ini"), QSettings.IniFormat); |
342 QList<QNetworkCookie> cookies = allCookies(); | 342 QNetworkCookie[] cookies = allCookies(); |
343 for (int i = cookies.count() - 1; i >= 0; --i) { | 343 for (int i = cookies.count() - 1; i >= 0; --i) { |
344 if (cookies.at(i).isSessionCookie()) | 344 if (cookies.at(i).isSessionCookie()) |
345 cookies.removeAt(i); | 345 cookies.removeAt(i); |
346 } | 346 } |
347 cookieSettings.setValue(QLatin1String("cookies"), qVariantFromValue<QList<QNetworkCookie> >(cookies)); | 347 cookieSettings.setValue(QLatin1String("cookies"), qVariantFromValue<QNetworkCookie[] >(cookies)); |
348 cookieSettings.beginGroup(QLatin1String("Exceptions")); | 348 cookieSettings.beginGroup(QLatin1String("Exceptions")); |
349 cookieSettings.setValue(QLatin1String("block"), m_exceptions_block); | 349 cookieSettings.setValue(QLatin1String("block"), m_exceptions_block); |
350 cookieSettings.setValue(QLatin1String("allow"), m_exceptions_allow); | 350 cookieSettings.setValue(QLatin1String("allow"), m_exceptions_allow); |
351 cookieSettings.setValue(QLatin1String("allowForSession"), m_exceptions_allowForSession); | 351 cookieSettings.setValue(QLatin1String("allowForSession"), m_exceptions_allowForSession); |
352 | 352 |
362 | 362 |
363 private: | 363 private: |
364 | 364 |
365 void purgeOldCookies() | 365 void purgeOldCookies() |
366 { | 366 { |
367 QList<QNetworkCookie> cookies = allCookies(); | 367 QNetworkCookie[] cookies = allCookies(); |
368 if (cookies.isEmpty()) | 368 if (cookies.isEmpty()) |
369 return; | 369 return; |
370 int oldCount = cookies.count(); | 370 int oldCount = cookies.count(); |
371 QDateTime now = QDateTime.currentDateTime(); | 371 QDateTime now = QDateTime.currentDateTime(); |
372 for (int i = cookies.count() - 1; i >= 0; --i) { | 372 for (int i = cookies.count() - 1; i >= 0; --i) { |
382 void load() | 382 void load() |
383 { | 383 { |
384 if (m_loaded) | 384 if (m_loaded) |
385 return; | 385 return; |
386 // load cookies and exceptions | 386 // load cookies and exceptions |
387 qRegisterMetaTypeStreamOperators<QList<QNetworkCookie> >("QList<QNetworkCookie>"); | 387 qRegisterMetaTypeStreamOperators<QNetworkCookie[] >("QNetworkCookie[]"); |
388 auto cookieSettings = new QSettings(QDesktopServices.storageLocation(QDesktopServices.DataLocation) + QLatin1String("/cookies.ini"), QSettings.IniFormat); | 388 auto cookieSettings = new QSettings(QDesktopServices.storageLocation(QDesktopServices.DataLocation) + QLatin1String("/cookies.ini"), QSettings.IniFormat); |
389 setAllCookies(qvariant_cast<QList<QNetworkCookie> >(cookieSettings.value(QLatin1String("cookies")))); | 389 setAllCookies(qvariant_cast<QNetworkCookie[] >(cookieSettings.value(QLatin1String("cookies")))); |
390 cookieSettings.beginGroup(QLatin1String("Exceptions")); | 390 cookieSettings.beginGroup(QLatin1String("Exceptions")); |
391 m_exceptions_block = cookieSettings.value(QLatin1String("block")).toStringList(); | 391 m_exceptions_block = cookieSettings.value(QLatin1String("block")).toStringList(); |
392 m_exceptions_allow = cookieSettings.value(QLatin1String("allow")).toStringList(); | 392 m_exceptions_allow = cookieSettings.value(QLatin1String("allow")).toStringList(); |
393 m_exceptions_allowForSession = cookieSettings.value(QLatin1String("allowForSession")).toStringList(); | 393 m_exceptions_allowForSession = cookieSettings.value(QLatin1String("allowForSession")).toStringList(); |
394 qSort(m_exceptions_block.begin(), m_exceptions_block.end()); | 394 qSort(m_exceptions_block.begin(), m_exceptions_block.end()); |
457 return QAbstractTableModel.headerData(section, orientation, role); | 457 return QAbstractTableModel.headerData(section, orientation, role); |
458 } | 458 } |
459 | 459 |
460 QVariant data(QModelIndex index, int role = Qt.DisplayRole) | 460 QVariant data(QModelIndex index, int role = Qt.DisplayRole) |
461 { | 461 { |
462 QList<QNetworkCookie> lst; | 462 QNetworkCookie[] lst; |
463 | 463 |
464 if (m_cookieJar) | 464 if (m_cookieJar) |
465 lst = m_cookieJar.allCookies(); | 465 lst = m_cookieJar.allCookies(); |
466 | 466 |
467 if (index.row() < 0 || index.row() >= lst.size()) | 467 if (index.row() < 0 || index.row() >= lst.size()) |
505 int rowCount(QModelIndex parent = QModelIndex()) | 505 int rowCount(QModelIndex parent = QModelIndex()) |
506 { | 506 { |
507 return (parent.isValid() || !m_cookieJar) ? 0 : m_cookieJar.allCookies().count(); | 507 return (parent.isValid() || !m_cookieJar) ? 0 : m_cookieJar.allCookies().count(); |
508 } | 508 } |
509 | 509 |
510 | |
511 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) | 510 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) |
512 { | 511 { |
513 if (parent.isValid() || !m_cookieJar) | 512 if (parent.isValid() || !m_cookieJar) |
514 return false; | 513 return false; |
515 int lastRow = row + count - 1; | 514 int lastRow = row + count - 1; |
516 beginRemoveRows(parent, row, lastRow); | 515 beginRemoveRows(parent, row, lastRow); |
517 QList<QNetworkCookie> lst = m_cookieJar.allCookies(); | 516 QNetworkCookie[] lst = m_cookieJar.allCookies(); |
518 for (int i = lastRow; i >= row; --i) { | 517 for (int i = lastRow; i >= row; --i) { |
519 lst.removeAt(i); | 518 lst.removeAt(i); |
520 } | 519 } |
521 m_cookieJar.setAllCookies(lst); | 520 m_cookieJar.setAllCookies(lst); |
522 endRemoveRows(); | 521 endRemoveRows(); |