QVariant CookieModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= m_cookies.size()) return QVariant(); switch (role) { case Qt::DisplayRole: case Qt::EditRole: { QNetworkCookie cookie = m_cookies.at(index.row()); switch (index.column()) { case 0: return cookie.domain(); case 1: return cookie.name(); case 2: return cookie.path(); case 3: return cookie.isSecure() ? tr("true") : tr("false"); case 4: return cookie.isSessionCookie() ? tr("Session cookie") : cookie.expirationDate().toString(); case 5: return cookie.value(); } } case Qt::FontRole: { QFont font; font.setPointSize(10); return font; } } return QVariant(); }
CookieDialog::CookieDialog(const QNetworkCookie &cookie, QWidget *parent): QDialog(parent) { setupUi(this); m_nameLineEdit->setText(cookie.name()); m_domainLineEdit->setText(cookie.domain()); m_valueLineEdit->setText(cookie.value()); m_pathLineEdit->setText(cookie.path()); m_dateEdit->setDate(cookie.expirationDate().date()); m_isSecureComboBox->addItem(cookie.isSecure() ? tr("yes") : tr("no")); m_isHttpOnlyComboBox->addItem(cookie.isHttpOnly() ? tr("yes") : tr("no")); m_addButton->setVisible(false); m_cancelButton->setText(tr("Close")); }
bool CookieStore::insertCookie(const QNetworkCookie& cookie, const QUrl& firstUrl) { sqlite3_stmt* stmt; int rc; QByteArray domain = cookie.domain().toLatin1(); QByteArray path = cookie.path().toLatin1(); QByteArray name = cookie.name(); QByteArray fdomain = firstUrl.host().toLower().toLatin1(); QByteArray expiration; if (cookie.isSessionCookie()) { expiration = "0"; } else { expiration = cookie.expirationDate().toString().toLatin1(); } QByteArray content = cookie.toRawForm(); rc = sqlite3_prepare(m_db, "insert into thirdPartyCookies (domain, path, name, fdomain, expiration, content) values (?, ?, ?, ?, ?, ?)", -1, &stmt, 0); if (rc != SQLITE_OK) { qDebug() << "insertCookie prepare fail:" << rc; return false; } QByteArray text[6] = {domain, path, name, fdomain, expiration, content}; for (int i = 0; i < 6; ++i) { rc = sqlite3_bind_text(stmt, i + 1, text[i].constData(), text[i].size(), SQLITE_TRANSIENT); if (rc != SQLITE_OK) { qDebug() << "insertCookie bind fail:" << i; sqlite3_finalize(stmt); return false; } } rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { qDebug() << "insertCookie step (execute) fail"; sqlite3_finalize(stmt); return false; } sqlite3_finalize(stmt); return true; }
bool CookieJar::hasCookie(const QNetworkCookie &cookie) const { QUrl url; url.setScheme(cookie.isSecure() ? QLatin1String("https") : QLatin1String("http")); url.setHost(cookie.domain().startsWith(QLatin1Char('.')) ? cookie.domain().mid(1) : cookie.domain()); url.setPath(cookie.path()); const QList<QNetworkCookie> cookies(getCookiesForUrl(url)); for (int i = 0; i < cookies.count(); ++i) { if (cookies.at(i).domain() == cookie.domain() && cookies.at(i).name() == cookie.name()) { return true; } } return false; }
bool CookieStore::deleteCookie(const QNetworkCookie& cookie, const QUrl& firstUrl) { sqlite3_stmt *stmt; int rc; QByteArray domain = cookie.domain().toLatin1(); QByteArray path = cookie.path().toLatin1(); QByteArray name = cookie.name(); QByteArray fdomain = firstUrl.host().toLower().toLatin1(); QByteArray text[4] = {domain, path, name, fdomain}; rc = sqlite3_prepare(m_db, "delete from thirdPartyCookies where (domain = ? and path = ? and name = ? and fdomain = ?)", -1, &stmt, 0); if (rc != SQLITE_OK) { qDebug() << "deleteCookie prepare fail."; return false; } for (int i = 0; i < 4; ++i) { rc = sqlite3_bind_text(stmt, i + 1, text[i].constData(), text[i].size(), SQLITE_TRANSIENT); if (rc != SQLITE_OK) { qDebug() << "deleteCookie bind fail."; sqlite3_finalize(stmt); return false; } } rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { qDebug() << "deleteCookie step (execute) fail"; sqlite3_finalize(stmt); return false; } sqlite3_finalize(stmt); return true; }
void CookieJar::saveCookies() { QString fName = m_appPath; fName += "cookies"; QSettings cnFile(fName, QSettings::IniFormat); cnFile.clear(); cnFile.setValue("login", m_sVkLogin); QList<QNetworkCookie> cookies = allCookies(); cnFile.beginWriteArray("cookies"); for(int i=0; i<cookies.size(); i++) { QNetworkCookie cook = cookies.at(i); cnFile.setArrayIndex(i); cnFile.setValue("name", cook.name()); cnFile.setValue("value", cook.value()); cnFile.setValue("domain", cook.domain()); cnFile.setValue("path", cook.path()); } cnFile.endArray(); }
void CookiesContentsWidget::addCookie(const QNetworkCookie &cookie) { const QString domain = (cookie.domain().startsWith('.') ? cookie.domain().mid(1) : cookie.domain()); QStandardItem *domainItem = findDomain(domain); if (domainItem) { for (int i = 0; i < domainItem->rowCount(); ++i) { if (domainItem->child(i, 0)->text() == cookie.name() && domainItem->child(i, 0)->data(Qt::UserRole).toString() == cookie.path()) { return; } } } else { domainItem = new QStandardItem(HistoryManager::getIcon(QUrl(QStringLiteral("http://%1/").arg(domain))), domain); domainItem->setToolTip(domain); m_model->appendRow(domainItem); if (sender()) { m_model->sort(0); } } QStandardItem *cookieItem = new QStandardItem(QString(cookie.name())); cookieItem->setData(cookie.path(), Qt::UserRole); cookieItem->setData(cookie.domain(), (Qt::UserRole + 1)); cookieItem->setToolTip(cookie.name()); cookieItem->setFlags(cookieItem->flags() | Qt::ItemNeverHasChildren); domainItem->appendRow(cookieItem); domainItem->setText(QStringLiteral("%1 (%2)").arg(domain).arg(domainItem->rowCount())); }
void CookiesContentsWidget::removeCookie(const QNetworkCookie &cookie) { const QString domain = (cookie.domain().startsWith('.') ? cookie.domain().mid(1) : cookie.domain()); QStandardItem *domainItem = findDomain(domain); if (domainItem) { QPoint point; for (int j = 0; j < domainItem->rowCount(); ++j) { if (domainItem->child(j, 0)->text() == cookie.name() && domainItem->child(j, 0)->data(Qt::UserRole).toString() == cookie.path()) { point = m_ui->cookiesView->visualRect(domainItem->child(j, 0)->index()).center(); domainItem->removeRow(j); break; } } if (domainItem->rowCount() == 0) { m_model->invisibleRootItem()->removeRow(domainItem->row()); } else { domainItem->setText(QStringLiteral("%1 (%2)").arg(domain).arg(domainItem->rowCount())); } if (!point.isNull()) { const QModelIndex index = m_ui->cookiesView->indexAt(point); m_ui->cookiesView->setCurrentIndex(index); m_ui->cookiesView->selectionModel()->select(index, QItemSelectionModel::Select); } } }
inline static bool shorterPaths(const QNetworkCookie &c1, const QNetworkCookie &c2) { return c2.path().length() < c1.path().length(); }