+ // 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);
+
+ // Get the number of domains in the tree.
+ int numberOfDomains = treeModelPointer->invisibleRootItem()->rowCount();
+
+ // Create the insert domain row number and initially set it to be at the end of the list.
+ int insertDomainRowNumber = numberOfDomains;
+
+ // Create the new domain strings.
+ QString newDomainTopLevelDomain;
+ QString newDomainSecondLevelDomain;
+ QString newDomainThirdLevelDomain;
+
+ // Get the number of dots in the new domain string.
+ int newDomainDots = newDomain.count(QLatin1Char('.'));
+
+ // Get the new top level domain.
+ newDomainTopLevelDomain = newDomain.section('.', -1);
+
+ // Get the new second level domain if it contains at least one dot.
+ if (newDomainDots >= 1)
+ newDomainSecondLevelDomain = newDomain.section('.', -2);
+
+ // Get the new third level domain if it contains at least two dots.
+ if (newDomainDots >= 2)
+ newDomainThirdLevelDomain = newDomain.section('.', -3);
+
+ // Create while loop trackers.
+ bool locationFound = false;
+ int currentRow = 0;
+
+ // Check to see if the new domain should be inserted after an existing domain.
+ while (!locationFound && (currentRow < numberOfDomains)) {
+ // Get the current domain string.
+ QString currentDomain = treeModelPointer->invisibleRootItem()->child(currentRow, 0)->index().data().toString();
+
+ // Create the current domain strings.
+ QString currentDomainTopLevelDomain;
+ QString currentDomainSecondLevelDomain;
+ QString currentDomainThirdLevelDomain;
+
+ // Get the number of dots in the current domain string.
+ int currentDomainDots = currentDomain.count(QLatin1Char('.'));
+
+ // Get the current top level domain.
+ currentDomainTopLevelDomain = currentDomain.section('.', -1);
+
+ // Get the current second level domain if it contains at least one dot.
+ if (currentDomainDots >= 1)
+ currentDomainSecondLevelDomain = currentDomain.section('.', -2);
+
+ // Get the current third level domain if it contains at least two dots.
+ if (currentDomainDots >= 2)
+ currentDomainThirdLevelDomain = currentDomain.section('.', -3);
+
+ // Check to see if the new domain should be inserted after the current domain.
+ // Segments, like third level domains, that do not exist will be blank, which will cause the sorting to group similar domains.
+ if ((newDomainTopLevelDomain < currentDomainTopLevelDomain) || // The new top level domain `.com` is less than the current top level domain `.org`.
+ ((newDomainTopLevelDomain == currentDomainTopLevelDomain) && ((newDomainSecondLevelDomain < currentDomainSecondLevelDomain) || // `jessica.com` is less than `stoutner.com`.
+ ((newDomainSecondLevelDomain == currentDomainSecondLevelDomain) && ((newDomainThirdLevelDomain < currentDomainThirdLevelDomain) || // `apache.stoutner.com` < `www.stoutner.com`.
+ ((newDomainThirdLevelDomain == currentDomainThirdLevelDomain) && (newDomain < currentDomain))))))) // `first.www.stoutner.com` < `second.www.stoutner.com`.
+ {
+ // Insert the domain at the current row (because the rows are 0 based, this will insert it before the first domain where all the above checks fail).
+ insertDomainRowNumber = currentRow;
+
+ // Mark the location as found.
+ locationFound = true;
+ }
+
+ // Move to the next row.
+ ++currentRow;
+ }
+
+ // 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);
+
+ // Get the number of cookies in the domain.
+ int numberOfCookies = domainNameItemPointer->rowCount();
+
+ // Create the insert cookie row number and initially set it to be at the end of the list.
+ int insertCookieRowNumber = numberOfCookies;
+
+ // Create the trackers.
+ bool removeExistingRow = false;
+ bool rowFound = false;
+ int currentRow = 0;
+
+ while (!rowFound && currentRow < numberOfCookies)
+ {
+ // Get the current cookie name and path at the indicated row.
+ QString currentCookieName = domainNameItemPointer->child(currentRow, 0)->index().data().toString();
+ QString currentCookiePath = domainNameItemPointer->child(currentRow, 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 before the current cookie name.
+ ((newCookieName == currentCookieName) && ((newCookiePath < currentCookiePath) || // The names are the same, but the new cookie path comes before the current cookie path.
+ (newCookiePath == currentCookiePath)))) // The core attributes of the cookies are the same.
+ {
+ // Remove the existing cookie if the core attributes are the same.
+ if ((newCookieName == currentCookieName) && (newCookiePath == currentCookiePath))
+ removeExistingRow = true;
+
+ // Insert the new cookie at this row.
+ insertCookieRowNumber = currentRow;
+
+ // Mark the row as found.
+ rowFound = true;
+ }
+
+ // Move to the next row.
+ ++currentRow;
+ }
+
+ // 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());