+void CookiesDialog::addCookieFromDialog(const QNetworkCookie &cookie, const bool &isDurable) const
+{
+ // Add the cookie to the cookie list and the cookie store.
+ emit addCookie(cookie);
+
+ // Get the new domain string.
+ QString newDomain = cookie.domain();
+
+ // Check to see if the domain already exists in the model.
+ QList<QStandardItem*> currentDomainItemList = treeModelPointer->findItems(newDomain);
+
+ // Create a domain item pointer.
+ QStandardItem *domainNameItemPointer;
+
+ // Prepare the domain item pointer.
+ if (currentDomainItemList.isEmpty()) // The domain doesn't currently exist in the tree.
+ {
+ // Create the domain name item.
+ domainNameItemPointer = new QStandardItem(newDomain);
+
+ // Create the insert domain row number.
+ int insertDomainRowNumber = 0;
+
+ // Get the number of domains in the tree.
+ int numberOfDomains = treeModelPointer->invisibleRootItem()->rowCount();
+
+ // Get the new domain strings.
+ QString newDomainTopLevelDomain = newDomain.section('.', -1);
+ QString newDomainSecondLevelDomain = newDomain.section('.', -2);
+ QString newDomainThirdLevelDomain = newDomain.section('.', -3);
+
+ // Iterate through all the domains.
+ for (int i = 0; i < numberOfDomains; ++i)
+ {
+ // Get the current domain strings.
+ QString currentDomain = treeModelPointer->invisibleRootItem()->child(i, 0)->index().data().toString();
+ QString currentDomainTopLevelDomain = currentDomain.section('.', -1);
+ QString currentDomainSecondLevelDomain = currentDomain.section('.', -2);
+ QString currentDomainThirdLevelDomain = currentDomain.section('.', -3);
+
+ // Check to see if the new domain should be inserted after the current domain.
+ if (newDomainTopLevelDomain > currentDomainTopLevelDomain)
+ {
+ // Insert the new domain after the current domain.
+ insertDomainRowNumber = i + 1;
+ }
+ else if ((newDomainTopLevelDomain == currentDomainTopLevelDomain) && (newDomainSecondLevelDomain > currentDomainSecondLevelDomain))
+ {
+ // Insert the new domain after the current domain.
+ insertDomainRowNumber = i + 1;
+ }
+ else if ((newDomainSecondLevelDomain == currentDomainSecondLevelDomain) && (newDomainThirdLevelDomain > currentDomainThirdLevelDomain))
+ {
+ // Insert the new domain after the current domain.
+ insertDomainRowNumber = i + 1;
+ }
+ else if ((newDomainThirdLevelDomain == currentDomainThirdLevelDomain) && (newDomain > currentDomain))
+ {
+ // Insert the new domain after the current domain.
+ insertDomainRowNumber = i + 1;
+ }
+ }
+
+ // Add the domain to the tree.
+ treeModelPointer->invisibleRootItem()->insertRow(insertDomainRowNumber, domainNameItemPointer);
+ }
+ else // The domain already exists in the tree.
+ {
+ // Use the current domain standard item.
+ domainNameItemPointer = currentDomainItemList[0];
+ }
+
+ // Get strings for the new cookie name and path (used later in the placement of the row).
+ QString newCookieName = QString(cookie.name());
+ QString newCookiePath = QString(cookie.path());
+
+ // Create a cookie item list.
+ QList<QStandardItem*> cookieItemList;
+
+ // Create the cookie items.
+ QStandardItem *nameItemPointer = new QStandardItem(newCookieName);
+ QStandardItem *durableItemPointer = new QStandardItem(QString(isDurable ? i18n("yes") : i18n("no")));
+ QStandardItem *pathItemPointer = new QStandardItem(newCookiePath);
+ QStandardItem *expirationDateItemPointer = new QStandardItem(QString(cookie.expirationDate().toString()));
+ QStandardItem *isHttpOnlyItemPointer = new QStandardItem(QString(cookie.isHttpOnly() ? i18n("yes") : i18n("no")));
+ QStandardItem *isSecureItemPointer = new QStandardItem(QString(cookie.isSecure() ? i18n("yes") : i18n("no")));
+ QStandardItem *valueItemPointer = new QStandardItem(QString(cookie.value()));
+
+ // Populate the cookie item list.
+ cookieItemList.append(nameItemPointer);
+ cookieItemList.append(durableItemPointer);
+ cookieItemList.append(pathItemPointer);
+ cookieItemList.append(expirationDateItemPointer);
+ cookieItemList.append(isHttpOnlyItemPointer);
+ cookieItemList.append(isSecureItemPointer);
+ cookieItemList.append(valueItemPointer);
+
+ // Create the insert cookie row number.
+ int insertCookieRowNumber = 0;
+
+ // Create the remove existing row tracker.
+ bool removeExistingRow = false;
+
+ // Get the number of cookies in the domain.
+ int numberOfCookies = domainNameItemPointer->rowCount();
+
+ // Iterate through the cookies for this domain.
+ for (int i = 0; i < numberOfCookies; ++i)
+ {
+ // Get the current cookie name and path at the indicated row.
+ QString currentCookieName = domainNameItemPointer->child(i, 0)->index().data().toString();
+ QString currentCookiePath = domainNameItemPointer->child(i, 2)->index().data().toString();
+
+ // Check to see if the new cookie should be inserted after the current cookie.
+ if (newCookieName > currentCookieName) // The new cookie name comes after the current cookie name.
+ {
+ // Insert the new cookie after the current cookie.
+ insertCookieRowNumber = i + 1;
+ }
+ else if ((newCookieName == currentCookieName) && (newCookiePath > currentCookiePath)) // The names are the same, but the new cookie path comes after the current cookie path.
+ {
+ // Insert the new cookie after the current cookie.
+ insertCookieRowNumber = i + 1;
+ }
+ else if ((newCookieName == currentCookieName) && (newCookiePath == currentCookiePath)) // The cookies are the same.
+ {
+ // Remove the existing cookie in this row.
+ removeExistingRow = true;
+
+ // Insert the cookie in it's place.
+ insertCookieRowNumber = i;
+ }
+ }
+
+ // Remove the existing row if it is being edited.
+ if (removeExistingRow)
+ domainNameItemPointer->removeRow(insertCookieRowNumber);
+
+ // Add the cookie to the tree model.
+ domainNameItemPointer->insertRow(insertCookieRowNumber, cookieItemList);
+
+ // Get the new cookie model index.
+ QModelIndex newCookieIndex = nameItemPointer->index();
+
+ // Set the new cookie to be the current index.
+ treeViewPointer->setCurrentIndex(newCookieIndex);
+
+ // Expand the parent of the new cookie.
+ treeViewPointer->expand(newCookieIndex.parent());
+}
+
+void CookiesDialog::deleteCookie(const QModelIndex &modelIndex, const bool &deleteDurableCookies) const
+{
+ // Create a partial cookie.
+ QNetworkCookie partialCookie;
+
+ // Populate the partial cookie from the current model index.
+ partialCookie.setDomain(modelIndex.parent().siblingAtColumn(0).data().toString());
+ partialCookie.setName(modelIndex.siblingAtColumn(0).data().toString().toUtf8());
+ partialCookie.setPath(modelIndex.siblingAtColumn(2).data().toString());
+
+ // Create a cookie to delete.
+ QNetworkCookie cookieToDelete;
+
+ // Check if the cookie is durable.
+ bool isDurable = CookiesDatabase::isDurable(partialCookie);
+
+ // Only delete durable cookies if directed.
+ if (deleteDurableCookies || !isDurable)
+ {
+ // Search for the partial cookie in the cookie list.
+ for (QNetworkCookie cookie : *cookieListPointer)
+ {
+ // Store the cookie to delete if it has the same identifier as the partial cookie.
+ if (cookie.hasSameIdentifier(partialCookie))
+ cookieToDelete = cookie;
+ }
+
+ // Remove the cookie from the tree model.
+ treeModelPointer->removeRow(modelIndex.row(), modelIndex.parent());
+
+ // Delete the cookie from the cookie list and cookie store.
+ emit deleteCookie(cookieToDelete);
+
+ // Delete the cookie from the durable cookies database.
+ if (isDurable)
+ CookiesDatabase::deleteCookie(cookieToDelete);
+ }
+}
+
+void CookiesDialog::deleteDomain(const QModelIndex &modelIndex, const bool &deleteDurableCookies) const
+{
+ // Get the parent index.
+ QModelIndex parentIndex = modelIndex.parent();
+
+ // Get the number of cookies in the domain.
+ int numberOfCookies = treeModelPointer->rowCount(modelIndex);
+
+ // Delete each child cookie, starting from the bottom.
+ for (int i = numberOfCookies; i > 0; --i)
+ deleteCookie(treeModelPointer->index(i-1, 0, modelIndex), deleteDurableCookies);
+
+ // Remove the domain if all the cookies have been deleted.
+ if (treeModelPointer->rowCount(modelIndex) == 0)
+ treeModelPointer->removeRow(modelIndex.row(), parentIndex);
+}
+
+
+void CookiesDialog::deleteCookieFromDialog(const QNetworkCookie &cookie) const
+{
+ // Get the current model index.
+ QModelIndex currentIndex = treeSelectionModelPointer->currentIndex();
+
+ // Get the parent index.
+ QModelIndex parentIndex = currentIndex.parent();
+
+ // Remove the cookie from the tree model.
+ treeModelPointer->removeRow(currentIndex.row(), parentIndex);
+
+ // Remove the domain from the tree model if its only cookie has been deleted.
+ if (treeModelPointer->rowCount(parentIndex) == 0)
+ treeModelPointer->removeRow(parentIndex.row(), parentIndex.parent());
+
+ // Delete the cookie from the cookie list and cookie store.
+ emit deleteCookie(cookie);
+}
+
+void CookiesDialog::showAddCookieDialog() const
+{
+ // Instantiate an add cookie dialog.
+ QDialog *addCookieDialogPointer = new AddOrEditCookieDialog(AddOrEditCookieDialog::AddCookie);
+
+ // Show the dialog.
+ addCookieDialogPointer->show();
+
+ // Add the cookie if directed.
+ connect(addCookieDialogPointer, SIGNAL(addCookie(QNetworkCookie, bool)), this, SLOT(addCookieFromDialog(QNetworkCookie, bool)));
+}
+