Line data Source code
1 : #include "InsertFolderOperation.h"
2 : #include "../utilities/Utilities.h"
3 : #include "../utilities/ErrorHandling.h"
4 : #include "../utilities/FangLogging.h"
5 : #include "../utilities/UnreadCountReader.h"
6 : #include "../models/FolderFeedItem.h"
7 :
8 5 : InsertFolderOperation::InsertFolderOperation(OperationManager *parent, int newIndex, QString name, ListModel *feedList)
9 : : DBOperation(parent),
10 5 : newIndex(newIndex),
11 5 : name(name),
12 5 : feedList(feedList),
13 5 : newItem(nullptr)
14 : {
15 5 : }
16 :
17 5 : InsertFolderOperation::~InsertFolderOperation()
18 : {
19 :
20 5 : }
21 :
22 5 : void InsertFolderOperation::execute()
23 : {
24 5 : qint64 insertID = -1;
25 :
26 : // Make sure we have a realistic newIndex before we get started.
27 5 : if (newIndex < 0 || newIndex >= feedList->count()) {
28 4 : qCCritical(logOperation) << "InsertFolderOperation: Invalid newIndex" << newIndex
29 2 : << "for feedList with count" << feedList->count();
30 2 : return;
31 : }
32 :
33 : // Grab next two ids in the list.
34 3 : QVector<qint64> nextIDs;
35 3 : QVector<FeedItem *> nextItems;
36 9 : for (int i = newIndex; i < newIndex + 2; i++) {
37 6 : FeedItem* next = qobject_cast<FeedItem*>(feedList->row(i));
38 6 : if (!next) {
39 0 : qCCritical(logOperation) << "InsertFolderOperation: Feed item at index" << i << "is null";
40 0 : return;
41 : }
42 6 : nextIDs.append(next->getDbID());
43 6 : nextItems.append(next);
44 : }
45 :
46 : // DB: Insert the new folder into the db and get it's new ID.
47 3 : db().transaction();
48 :
49 : {
50 3 : QSqlQuery query(db());
51 3 : query.prepare("INSERT INTO FeedItemTable (title, ordinal, is_folder, subtitle, "
52 : "url, siteURL) VALUES "
53 : "(:title, :ordinal, :isFolder, '', '', '')");
54 3 : query.bindValue(":title", name);
55 3 : query.bindValue(":ordinal", newIndex);
56 3 : query.bindValue(":isFolder", true);
57 :
58 3 : if (!query.exec()) {
59 0 : reportSQLError(query, "Could not add new folder");
60 0 : db().rollback();
61 :
62 0 : return;
63 : }
64 :
65 : // Our new ID.
66 3 : insertID = query.lastInsertId().toLongLong();
67 3 : }
68 :
69 3 : if (insertID <= -1) {
70 0 : qCCritical(logOperation) << "InsertFolderOperation: Invalid insert ID" << insertID;
71 0 : db().rollback();
72 0 : return;
73 : }
74 :
75 : {
76 3 : QString ids = Utilities::commaSeparatedStringList(nextIDs);
77 :
78 3 : QSqlQuery query(db());
79 3 : query.prepare("UPDATE FeedItemTable SET parent_folder = :parentFolder "
80 6 : "WHERE id IN (" + ids + ")");
81 3 : query.bindValue(":parentFolder", insertID);
82 3 : if (!query.exec()) {
83 0 : reportSQLError(query, "Could not set children for new folder");
84 0 : db().rollback();
85 :
86 0 : return;
87 : }
88 3 : }
89 :
90 3 : db().commit();
91 :
92 :
93 : // Update our model.
94 3 : FolderFeedItem* folder = new FolderFeedItem(insertID, newIndex, name, feedList);
95 9 : for (FeedItem* item: nextItems) {
96 6 : item->setParentFolder(insertID);
97 : }
98 3 : feedList->insertRow(newIndex, folder);
99 :
100 : // Get the initial unread count based on the feeds within this folder.
101 3 : UnreadCountReader::update(db(), folder);
102 3 : }
|