File indexing completed on 2024-02-18 04:58:19

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2007 by Lukas Appelhans <l.appelhans@gmx.de>
0004    Copyright (C) 2008 by Javier Goday <jgoday@gmail.com>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #ifdef HAVE_SQLITE
0013 #include "core/transferhistorystore_sqlite_p.h"
0014 #include <QSqlDatabase>
0015 #include <QSqlError>
0016 #include <QSqlQuery>
0017 #include <QSqlRecord>
0018 
0019 #include <QFile>
0020 
0021 #include "kget_debug.h"
0022 #include <QDebug>
0023 
0024 using namespace Qt::Literals::StringLiterals;
0025 
0026 SQLiteStore::SQLiteStore(const QString &database)
0027     : TransferHistoryStore()
0028     , m_dbName(database)
0029     , m_sql()
0030 {
0031 }
0032 
0033 SQLiteStore::~SQLiteStore()
0034 {
0035     if (m_sql.isOpen()) {
0036         m_sql.close();
0037     }
0038 
0039     deleteExpiredItems();
0040 }
0041 
0042 void SQLiteStore::load()
0043 {
0044     m_items.clear();
0045     if (sql().open()) {
0046         if (!sql().tables().contains("transfer_history_item")) {
0047             createTables();
0048         }
0049 
0050         QSqlQuery query(u"SELECT * FROM transfer_history_item"_s, sql());
0051         query.exec();
0052 
0053         if (query.lastError().isValid()) {
0054             qCDebug(KGET_DEBUG) << query.lastError().text();
0055         } else {
0056             QSqlRecord rec = query.record();
0057 
0058             while (query.next()) {
0059                 TransferHistoryItem item;
0060                 item.setDest(query.value(rec.indexOf("dest")).toString());
0061                 item.setSource(query.value(rec.indexOf("source")).toString());
0062                 item.setState(query.value(rec.indexOf("state")).toInt());
0063                 item.setDateTime(QDateTime::fromSecsSinceEpoch(query.value(rec.indexOf("time")).toUInt()));
0064                 item.setSize(query.value(rec.indexOf("size")).toInt());
0065 
0066                 // do not load expired items
0067                 if (item.isExpired(expiryAge())) {
0068                     continue;
0069                 }
0070 
0071                 m_items << item;
0072                 Q_EMIT elementLoaded(query.at(), query.size(), item);
0073             }
0074         }
0075     }
0076 
0077     sql().close();
0078 
0079     Q_EMIT loadFinished();
0080 }
0081 
0082 void SQLiteStore::clear()
0083 {
0084     QFile::remove(m_dbName);
0085 }
0086 
0087 void SQLiteStore::saveItem(const TransferHistoryItem &item)
0088 {
0089     saveItems(QList<TransferHistoryItem>() << item);
0090 }
0091 
0092 void SQLiteStore::saveItems(const QList<TransferHistoryItem> &items)
0093 {
0094     if (sql().open()) {
0095         if (!sql().tables().contains("transfer_history_item")) {
0096             createTables();
0097         }
0098 
0099         if (!sql().transaction()) {
0100             qCWarning(KGET_DEBUG) << "Could not establish a transaction, might be slow.";
0101         }
0102 
0103         foreach (const TransferHistoryItem &item, items) {
0104             QSqlQuery query = sql().exec(
0105                 "insert into transfer_history_item(source, dest, size, time, state)"
0106                 "values ('"
0107                 + item.source() + "', '" + item.dest() + "', " + QString::number(item.size()) + ", " + QString::number(item.dateTime().toSecsSinceEpoch())
0108                 + ", '" + QString::number(item.state()) + "')");
0109 
0110             if (query.lastError().isValid()) {
0111                 qCDebug(KGET_DEBUG) << query.lastError().text();
0112             }
0113             m_items << item;
0114         }
0115 
0116         if (!sql().commit()) {
0117             qCWarning(KGET_DEBUG) << "Could not commit changes.";
0118         }
0119     }
0120     sql().close();
0121 
0122     Q_EMIT saveFinished();
0123 }
0124 
0125 void SQLiteStore::deleteItem(const TransferHistoryItem &item)
0126 {
0127     if (sql().open()) {
0128         if (!sql().tables().contains("transfer_history_item")) {
0129             createTables();
0130         }
0131 
0132         QSqlQuery query = sql().exec(
0133             "delete from transfer_history_item where "
0134             " source = '"
0135             + item.source() + "';");
0136 
0137         if (query.lastError().isValid()) {
0138             qCDebug(KGET_DEBUG) << query.lastError().text();
0139         }
0140 
0141         sql().commit();
0142         m_items.removeAll(item);
0143     }
0144     sql().close();
0145 
0146     Q_EMIT deleteFinished();
0147 }
0148 
0149 QSqlDatabase SQLiteStore::sql()
0150 {
0151     if (!m_sql.isValid()) {
0152         m_sql = QSqlDatabase::addDatabase("QSQLITE");
0153         m_sql.setDatabaseName(m_dbName);
0154     }
0155 
0156     return m_sql;
0157 }
0158 
0159 void SQLiteStore::createTables()
0160 {
0161     QSqlQuery query = sql().exec(
0162         "CREATE TABLE transfer_history_item(dest VARCHAR NOT NULL, "
0163         "source VARCHAR NOT NULL, size int NOT NULL, time int not null, "
0164         "state int, PRIMARY KEY(dest, source));");
0165 
0166     if (query.lastError().isValid()) {
0167         qCDebug(KGET_DEBUG) << query.lastError().text();
0168     }
0169 }
0170 
0171 #endif
0172 
0173 #include "moc_transferhistorystore_sqlite_p.cpp"