+QString BookmarksDatabase::getFavoriteIconBase64String(const QIcon &favoriteIcon)
+{
+ // Get a favorite icon pixmap.
+ QPixmap favoriteIconPixmap = favoriteIcon.pixmap(32, 32);
+
+ // Create a favorite icon byte array.
+ QByteArray favoriteIconByteArray;
+
+ // Create a favorite icon buffer.
+ QBuffer favoriteIconBuffer(&favoriteIconByteArray);
+
+ // Open the buffer.
+ favoriteIconBuffer.open(QIODevice::WriteOnly);
+
+ // Convert the favorite icon pixmap into a byte array in PNG format.
+ favoriteIconPixmap.save(&favoriteIconBuffer, "PNG");
+
+ // Close the buffer.
+ favoriteIconBuffer.close();
+
+ // Convert the favorite icon byte array to a base 64 string.
+ QString favoriteIconBase64String = favoriteIconByteArray.toBase64();
+
+ // Return the favorite icon base 64 string.
+ return favoriteIconBase64String;
+}
+
+QList<BookmarkStruct>* BookmarksDatabase::getFolderContents(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder contents query.
+ QSqlQuery folderContentsQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderContentsQuery.setForwardOnly(true);
+
+ // Prepare the folder contents query.
+ folderContentsQuery.prepare("SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER_ID + " = :parent_folder_id ORDER BY " + DISPLAY_ORDER + " ASC");
+
+ // Bind the query values.
+ folderContentsQuery.bindValue(":parent_folder_id", folderId);
+
+ // Execute the query.
+ folderContentsQuery.exec();
+
+ // Create a folder contents list.
+ QList<BookmarkStruct> *folderContentsListPointer = new QList<BookmarkStruct>;
+
+ // Populate the folder contents list.
+ while (folderContentsQuery.next())
+ {
+ // Create a bookmark struct.
+ struct BookmarkStruct bookmarkStruct;
+
+ // Get the favorite icon base 64 byte array.
+ QByteArray favoriteIconByteArray = QByteArray::fromBase64(folderContentsQuery.value(FAVORITE_ICON).toByteArray());
+
+ // Create a favorite icon pixmap.
+ QPixmap favoriteIconPixmap;
+
+ // Load the pixmap from byte array.
+ favoriteIconPixmap.loadFromData(favoriteIconByteArray);
+
+ // Populate the bookmark struct.
+ bookmarkStruct.databaseId = folderContentsQuery.value(ID).toInt();
+ bookmarkStruct.name = folderContentsQuery.value(BOOKMARK_NAME).toString();
+ bookmarkStruct.url = folderContentsQuery.value(BOOKMARK_URL).toString();
+ bookmarkStruct.parentFolderId = folderContentsQuery.value(PARENT_FOLDER_ID).toDouble();
+ bookmarkStruct.displayOrder = folderContentsQuery.value(DISPLAY_ORDER).toInt();
+ bookmarkStruct.isFolder = folderContentsQuery.value(IS_FOLDER).toBool();
+ bookmarkStruct.folderId = folderContentsQuery.value(FOLDER_ID).toDouble();
+ bookmarkStruct.favoriteIcon = QIcon(favoriteIconPixmap);
+
+ // Add the item to the list.
+ folderContentsListPointer->append(bookmarkStruct);
+ }
+
+ // Return the folder contents list.
+ return folderContentsListPointer;
+}
+
+QList<int>* BookmarksDatabase::getFolderContentsDatabaseIds(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder contents query.
+ QSqlQuery folderContentsQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderContentsQuery.setForwardOnly(true);
+
+ // Prepare the folder contents query.
+ folderContentsQuery.prepare("SELECT " + ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER_ID + " = :parent_folder_id");
+
+ // Bind the query values.
+ folderContentsQuery.bindValue(":parent_folder_id", folderId);
+
+ // Execute the query.
+ folderContentsQuery.exec();
+
+ // Create a folder contents database ID list.
+ QList<int> *folderContentsDatabaseIdsListPointer = new QList<int>;
+
+ // Populate the folder contents list.
+ while (folderContentsQuery.next())
+ {
+ // Add the database ID to the list.
+ folderContentsDatabaseIdsListPointer->append(folderContentsQuery.value(ID).toInt());
+ }
+
+ // Return the folder contents database ID list.
+ return folderContentsDatabaseIdsListPointer;
+}
+
+QList<int> *BookmarksDatabase::getFolderContentsDatabaseIdsRecursively(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder contents query.
+ QSqlQuery folderContentsQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderContentsQuery.setForwardOnly(true);
+
+ // Prepare the folder contents query.
+ folderContentsQuery.prepare("SELECT " + ID + ", " + IS_FOLDER + ", " + FOLDER_ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER_ID + " = :parent_folder_id");
+
+ // Bind the query values.
+ folderContentsQuery.bindValue(":parent_folder_id", folderId);
+
+ // Execute the query.
+ folderContentsQuery.exec();
+
+ // Create a folder contents database ID list.
+ QList<int> *folderContentsDatabaseIdsListPointer = new QList<int>;
+
+ // Populate the folder contents list.
+ while (folderContentsQuery.next())
+ {
+ // Add the database ID to the list.
+ folderContentsDatabaseIdsListPointer->append(folderContentsQuery.value(ID).toInt());
+
+ // Recursively get the contents if this is a subfolder.
+ if (folderContentsQuery.value(IS_FOLDER).toBool())
+ folderContentsDatabaseIdsListPointer->append(*getFolderContentsDatabaseIdsRecursively(folderContentsQuery.value(FOLDER_ID).toDouble()));
+ }
+
+ // Return the folder contents database ID list.
+ return folderContentsDatabaseIdsListPointer;
+}
+
+int BookmarksDatabase::getFolderDatabaseId(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder database ID query.
+ QSqlQuery folderDatabaseIdQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderDatabaseIdQuery.setForwardOnly(true);
+
+ // Prepare the folder database ID query.
+ folderDatabaseIdQuery.prepare("SELECT " + ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + FOLDER_ID + " = :folder_id");
+
+ // Bind the query values.
+ folderDatabaseIdQuery.bindValue(":folder_id", folderId);
+
+ // Execute the query.
+ folderDatabaseIdQuery.exec();
+
+ // Move to the first entry.
+ folderDatabaseIdQuery.first();
+
+ // Return the folder database ID.
+ return folderDatabaseIdQuery.value(ID).toInt();
+}
+
+double BookmarksDatabase::getFolderId(const int databaseId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder ID query.
+ QSqlQuery folderIdQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderIdQuery.setForwardOnly(true);
+
+ // Prepare the folder ID query.
+ folderIdQuery.prepare("SELECT " + FOLDER_ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + ID + " = :database_id");
+
+ // Bind the query values.
+ folderIdQuery.bindValue(":database_id", databaseId);
+
+ // Execute the query.
+ folderIdQuery.exec();
+
+ // Move to the first entry.
+ folderIdQuery.first();
+
+ // Return the folder ID.
+ return folderIdQuery.value(FOLDER_ID).toDouble();
+}
+
+int BookmarksDatabase::getFolderItemCount(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a folder contents query.
+ QSqlQuery folderContentsQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ folderContentsQuery.setForwardOnly(true);
+
+ // Prepare the folder contents query.
+ folderContentsQuery.prepare("SELECT " + ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER_ID + " = :parent_folder_id");
+
+ // Bind the query values.
+ folderContentsQuery.bindValue(":parent_folder_id", folderId);
+
+ // Execute the query.
+ folderContentsQuery.exec();
+
+ // Move to the last row.
+ folderContentsQuery.last();
+
+ // Initialize an item count variable.
+ int itemCount = 0;
+
+ // Check to see if the query is valid (there is at least one item).
+ if (folderContentsQuery.isValid())
+ {
+ // Get the number of rows (which is zero based) and add one to calculate the number of bookmarks.
+ itemCount = folderContentsQuery.at() + 1;
+ }
+
+ // Return the item count.
+ return itemCount;
+}
+
+double BookmarksDatabase::getParentFolderId(const int databaseId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a parent folder ID query.
+ QSqlQuery parentFolderIdQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ parentFolderIdQuery.setForwardOnly(true);
+
+ // Prepare the parent folder ID query.
+ parentFolderIdQuery.prepare("SELECT " + PARENT_FOLDER_ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + ID + " = :database_id");
+
+ // Bind the query values.
+ parentFolderIdQuery.bindValue(":database_id", databaseId);
+
+ // Execute the query.
+ parentFolderIdQuery.exec();
+
+ // Move to the first entry.
+ parentFolderIdQuery.first();
+
+ // Return the parent folder ID.
+ return parentFolderIdQuery.value(PARENT_FOLDER_ID).toDouble();
+}
+
+QList<BookmarkStruct>* BookmarksDatabase::getSubfolders(const double folderId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate a subfolders query.
+ QSqlQuery subfoldersQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ subfoldersQuery.setForwardOnly(true);
+
+ // Prepare the subfolders query.
+ subfoldersQuery.prepare("SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = 1 AND " + PARENT_FOLDER_ID + " = :parent_folder_id ORDER BY " + DISPLAY_ORDER + " ASC");
+
+ // Bind the query values.
+ subfoldersQuery.bindValue(":parent_folder_id", folderId);
+
+ // Execute the query.
+ subfoldersQuery.exec();
+
+ // Create a subfolder list.
+ QList<BookmarkStruct> *subfoldersListPointer = new QList<BookmarkStruct>;
+
+ // Populate the subfolder list.
+ while (subfoldersQuery.next())
+ {
+ // Create a bookmark struct.
+ struct BookmarkStruct bookmarkStruct;
+
+ // Get the favorite icon base 64 byte array.
+ QByteArray favoriteIconByteArray = QByteArray::fromBase64(subfoldersQuery.value(FAVORITE_ICON).toByteArray());
+
+ // Create a favorite icon pixmap.
+ QPixmap favoriteIconPixmap;
+
+ // Load the pixmap from byte array.
+ favoriteIconPixmap.loadFromData(favoriteIconByteArray);
+
+ // Populate the bookmark struct.
+ bookmarkStruct.databaseId = subfoldersQuery.value(ID).toInt();
+ bookmarkStruct.name = subfoldersQuery.value(BOOKMARK_NAME).toString();
+ bookmarkStruct.parentFolderId = subfoldersQuery.value(PARENT_FOLDER_ID).toDouble();
+ bookmarkStruct.displayOrder = subfoldersQuery.value(DISPLAY_ORDER).toInt();
+ bookmarkStruct.isFolder = subfoldersQuery.value(IS_FOLDER).toBool();
+ bookmarkStruct.folderId = subfoldersQuery.value(FOLDER_ID).toDouble();
+ bookmarkStruct.favoriteIcon = QIcon(favoriteIconPixmap);
+
+ // Add the subfolder to the list.
+ subfoldersListPointer->append(bookmarkStruct);
+ }
+
+ // Return the subfolders list.
+ return subfoldersListPointer;
+}
+
+bool BookmarksDatabase::isBookmarked(const QString url)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate an is bookmarked query.
+ QSqlQuery isBookmarkedQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ isBookmarkedQuery.setForwardOnly(true);
+
+ // Prepare the is bookmarked query.
+ isBookmarkedQuery.prepare("SELECT " + ID + " FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = 0 AND " + BOOKMARK_URL + " = :url");
+
+ // Bind the query values.
+ isBookmarkedQuery.bindValue(":url", url);
+
+ // Execute the query.
+ isBookmarkedQuery.exec();
+
+ // Move to the first entry.
+ isBookmarkedQuery.first();
+
+ // Return true if the query is valid (there is at least one item).
+ return isBookmarkedQuery.isValid();
+}
+
+bool BookmarksDatabase::isFolder(const int databaseId)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate an is folder query.
+ QSqlQuery isFolderQuery(bookmarksDatabase);
+
+ // Set the query to be forward only, which is more performant.
+ isFolderQuery.setForwardOnly(true);
+
+ // Prepare the is folder query.
+ isFolderQuery.prepare("SELECT " + IS_FOLDER + " FROM " + BOOKMARKS_TABLE + " WHERE " + ID + " = :id");
+
+ // Bind the query values.
+ isFolderQuery.bindValue(":id", databaseId);
+
+ // Execute the query.
+ isFolderQuery.exec();
+
+ // Move to the first entry.
+ isFolderQuery.first();
+
+ // Return the folder status.
+ return isFolderQuery.value(IS_FOLDER).toBool();
+}
+
+void BookmarksDatabase::updateBookmark(const BookmarkStruct *bookmarkStructPointer)
+{
+ // Get a handle for the bookmarks database.
+ QSqlDatabase bookmarksDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+ // Instantiate an update bookmark query.
+ QSqlQuery updateBookmarkQuery(bookmarksDatabase);
+
+ // Prepare the update bookmark query.
+ updateBookmarkQuery.prepare("UPDATE " + BOOKMARKS_TABLE + " SET " +
+ BOOKMARK_NAME + " = :bookmark_name, " +
+ BOOKMARK_URL + " = :bookmark_url, " +
+ PARENT_FOLDER_ID + " = :parent_folder_id, " +
+ DISPLAY_ORDER + " = :display_order, " +
+ FAVORITE_ICON + "= :favorite_icon " +
+ "WHERE " + ID + " = :id");
+
+ // Bind the query values.
+ updateBookmarkQuery.bindValue(":bookmark_name", bookmarkStructPointer->name);
+ updateBookmarkQuery.bindValue(":bookmark_url", bookmarkStructPointer->url);
+ updateBookmarkQuery.bindValue(":parent_folder_id", bookmarkStructPointer->parentFolderId);
+ updateBookmarkQuery.bindValue(":display_order", bookmarkStructPointer->displayOrder);
+ updateBookmarkQuery.bindValue(":favorite_icon", getFavoriteIconBase64String(bookmarkStructPointer->favoriteIcon));
+ updateBookmarkQuery.bindValue(":id", bookmarkStructPointer->databaseId);
+
+ // Execute the query.
+ updateBookmarkQuery.exec();
+}
+
+void BookmarksDatabase::updateBookmarkName(const int databaseId, const QString &bookmarkName)