From 83e7c484d2440bfff54e8d02b2d532c2aba755ef Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 22 Jun 2022 12:02:44 -0700 Subject: [PATCH] Replace the for loops in CookiesDialog with while loops. https://redmine.stoutner.com/issues/848 --- src/databases/CookiesDatabase.cpp | 19 ++- src/dialogs/CookiesDialog.cpp | 184 ++++++++++++++++++------------ src/windows/BrowserWindow.cpp | 4 +- 3 files changed, 129 insertions(+), 78 deletions(-) diff --git a/src/databases/CookiesDatabase.cpp b/src/databases/CookiesDatabase.cpp index a992909..8464c06 100644 --- a/src/databases/CookiesDatabase.cpp +++ b/src/databases/CookiesDatabase.cpp @@ -163,11 +163,18 @@ int CookiesDatabase::cookieCount() // Execute the query. countCookiesQuery.exec(); - // Move to the last row. - countCookiesQuery.last(); + // Initialize a number of cookies variable. + int numberOfCookies = 0; - // Get the number of rows (which is zero based). - int numberOfCookies = countCookiesQuery.at() + 1; + // Check to see if the query is valid (there is at least one cookie). + if (countCookiesQuery.isValid()) + { + // Move to the last row. + countCookiesQuery.last(); + + // Get the number of rows (which is zero based) and add one to calculate the number of cookies. + numberOfCookies = countCookiesQuery.at() + 1; + } // Return the number of cookies. return numberOfCookies; @@ -351,14 +358,14 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie) (isUpdateQuery.value(2).toBool() != cookie.isSecure()) || (isUpdateQuery.value(3).toString().toUtf8() != cookie.value())) // The cookies data has changed. { - qDebug() << "The durable cookie data has changed."; + //qDebug() << "The durable cookie data has changed."; // Return true. return true; } else // The cookie data has not changed. { - qDebug() << "The durable cookie data is unchanged."; + //qDebug() << "The durable cookie data is unchanged."; // Return false. return false; diff --git a/src/dialogs/CookiesDialog.cpp b/src/dialogs/CookiesDialog.cpp index 612888d..cfd8f8b 100644 --- a/src/dialogs/CookiesDialog.cpp +++ b/src/dialogs/CookiesDialog.cpp @@ -57,19 +57,35 @@ bool cookieSortPredicate(const QNetworkCookie &leftHandCookie, const QNetworkCoo QString leftHandDomain = leftHandCookie.domain(); QString rightHandDomain = rightHandCookie.domain(); - // Get the top level domains. - QString leftHandTopLevelDomain = leftHandDomain.section('.', -1); - QString rightHandTopLevelDomain = rightHandDomain.section('.', -1); - - // Get the second level domains. - QString leftHandSecondLevelDomain = leftHandDomain.section('.', -2); - QString rightHandSecondLevelDomain = rightHandDomain.section('.', -2); - - // Get the third level domains. - QString leftHandThirdLevelDomain = leftHandDomain.section('.', -3); - QString rightHandThirdLevelDomain = rightHandDomain.section('.', -3); + // Create the strings. + QString leftHandTopLevelDomain; + QString rightHandTopLevelDomain; + QString leftHandSecondLevelDomain; + QString rightHandSecondLevelDomain; + QString leftHandThirdLevelDomain; + QString rightHandThirdLevelDomain; + + // Get the numer of dots in the strings. + int leftHandDots = leftHandDomain.count(QLatin1Char('.')); + int rightHandDots = rightHandDomain.count(QLatin1Char('.')); - // Check to see if the top level domains are the same. + // Get the top level domains. + leftHandTopLevelDomain = leftHandDomain.section('.', -1); + rightHandTopLevelDomain = rightHandDomain.section('.', -1); + + // Get the second level domains if they contain at least one dot. + if (leftHandDots >= 1) + leftHandSecondLevelDomain = leftHandDomain.section('.', -2); + if (rightHandDots >= 1) + rightHandSecondLevelDomain = rightHandDomain.section('.', -2); + + // Get the third level domains if they contain at least two dots. + if (leftHandDots >= 2) + leftHandThirdLevelDomain = leftHandDomain.section('.', -3); + if (rightHandDots >= 2) + rightHandThirdLevelDomain = rightHandDomain.section('.', -3); + + // Check to see if the top level domains are the same. Segments, like third level domains, that don't exist will be blank, which will cause the sorting to group similar domains. if (leftHandTopLevelDomain == rightHandTopLevelDomain) { // Check to see if the second level domains are the same. @@ -301,47 +317,75 @@ void CookiesDialog::addCookieFromDialog(const QNetworkCookie &cookie, const bool // 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); + // Create the insert domain row number and initially set it to be at the end of the list. + int insertDomainRowNumber = numberOfDomains; - // 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); + // 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. - if (newDomainTopLevelDomain > currentDomainTopLevelDomain) - { - // Insert the new domain after the current domain. - insertDomainRowNumber = i + 1; - } - else if ((newDomainTopLevelDomain == currentDomainTopLevelDomain) && (newDomainSecondLevelDomain > currentDomainSecondLevelDomain)) + // 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 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; + // 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. @@ -378,41 +422,41 @@ void CookiesDialog::addCookieFromDialog(const QNetworkCookie &cookie, const bool 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) + // 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(i, 0)->index().data().toString(); - QString currentCookiePath = domainNameItemPointer->child(i, 2)->index().data().toString(); + 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 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. + 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 in this row. - removeExistingRow = true; + // Remove the existing cookie if the core attributes are the same. + if ((newCookieName == currentCookieName) && (newCookiePath == currentCookiePath)) + removeExistingRow = true; - // Insert the cookie in it's place. - insertCookieRowNumber = i; + // 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. diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 4edb35a..d3800ad 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -279,7 +279,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const { - qDebug() << "Add cookie: " << newCookie.toRawForm(); + //qDebug() << "Add cookie: " << newCookie.toRawForm(); // Add the new cookie to the list. cookieListPointer->push_front(newCookie); @@ -440,7 +440,7 @@ void BrowserWindow::refresh() const void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const { - qDebug() << "Remove cookie: " << cookie.toRawForm(); + //qDebug() << "Remove cookie: " << cookie.toRawForm(); // Remove the cookie from the list. cookieListPointer->remove(cookie); -- 2.45.2