Line data Source code
1 : #include "ExpireNewsOperation.h"
2 :
3 : #include "../models/FeedItem.h"
4 : #include "../models/NewsItem.h"
5 :
6 5 : ExpireNewsOperation::ExpireNewsOperation(OperationManager *parent, ListModel *feedList, QDateTime olderThan, qint32 saveLast) :
7 : DBOperation(parent),
8 5 : feedList(feedList),
9 5 : olderThan(olderThan),
10 5 : saveLast(saveLast)
11 : {
12 :
13 5 : }
14 :
15 5 : void ExpireNewsOperation::execute()
16 : {
17 5 : bool performVacuum = false;
18 :
19 : {
20 5 : db().transaction();
21 :
22 : // SQL can't easily do loops, so we need a little C++ magic.
23 11 : for (int i = 0; i < feedList->rowCount(); i++) {
24 6 : FeedItem* feed = qobject_cast<FeedItem*>(feedList->row(i));
25 6 : if (feed->isSpecialFeed()) {
26 : // Can't delete a special feed, DUH.
27 1 : continue;
28 : }
29 :
30 5 : QSqlQuery query(db());
31 5 : query.prepare("DELETE FROM NewsItemTable WHERE feed_id = :feed_id AND NOT pinned "
32 : "AND id < ( SELECT bookmark_id FROM FeedItemTable WHERE id = :feed_id2 LIMIT 1 ) "
33 : "AND timestamp < :olderThan AND id NOT IN "
34 : "( SELECT id FROM NewsItemTable WHERE feed_id = :feed_id3 ORDER BY timestamp DESC LIMIT :saveLast )");
35 5 : query.bindValue(":feed_id", feed->getDbID());
36 5 : query.bindValue(":feed_id2", feed->getDbID());
37 5 : query.bindValue(":olderThan", olderThan.toMSecsSinceEpoch());
38 5 : query.bindValue(":feed_id3", feed->getDbID());
39 5 : query.bindValue(":saveLast", saveLast);
40 :
41 5 : if (!query.exec()) {
42 0 : reportSQLError(query, "Unable to remove old feed.");
43 0 : db().rollback();
44 :
45 0 : return;
46 : }
47 :
48 5 : if (query.numRowsAffected() > 0) {
49 3 : performVacuum = true;
50 : }
51 5 : }
52 :
53 5 : db().commit();
54 : }
55 :
56 : // If our delete accomplished anything, let's reclaim lost disk space with SQLite's VACUUM command.
57 : // According to the SQLite docs, you can't have an open transaction while vacuuming.
58 5 : if (performVacuum) {
59 3 : QSqlQuery query(db());
60 3 : query.exec("VACUUM");
61 3 : }
62 : }
|