File indexing completed on 2023-10-01 08:39:30
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 SQLiteStore::SQLiteStore(const QString &database) 0025 : TransferHistoryStore() 0026 , m_dbName(database) 0027 , m_sql() 0028 { 0029 } 0030 0031 SQLiteStore::~SQLiteStore() 0032 { 0033 if (m_sql.isOpen()) { 0034 m_sql.close(); 0035 } 0036 0037 deleteExpiredItems(); 0038 } 0039 0040 void SQLiteStore::load() 0041 { 0042 m_items.clear(); 0043 if (sql().open()) { 0044 if (!sql().tables().contains("transfer_history_item")) { 0045 createTables(); 0046 } 0047 0048 QSqlQuery query = sql().exec("SELECT * FROM transfer_history_item"); 0049 0050 if (query.lastError().isValid()) { 0051 qCDebug(KGET_DEBUG) << query.lastError().text(); 0052 } else { 0053 QSqlRecord rec = query.record(); 0054 0055 while (query.next()) { 0056 TransferHistoryItem item; 0057 item.setDest(query.value(rec.indexOf("dest")).toString()); 0058 item.setSource(query.value(rec.indexOf("source")).toString()); 0059 item.setState(query.value(rec.indexOf("state")).toInt()); 0060 item.setDateTime(QDateTime::fromSecsSinceEpoch(query.value(rec.indexOf("time")).toUInt())); 0061 item.setSize(query.value(rec.indexOf("size")).toInt()); 0062 0063 // do not load expired items 0064 if (item.isExpired(expiryAge())) { 0065 continue; 0066 } 0067 0068 m_items << item; 0069 Q_EMIT elementLoaded(query.at(), query.size(), item); 0070 } 0071 } 0072 } 0073 0074 sql().close(); 0075 0076 Q_EMIT loadFinished(); 0077 } 0078 0079 void SQLiteStore::clear() 0080 { 0081 QFile::remove(m_dbName); 0082 } 0083 0084 void SQLiteStore::saveItem(const TransferHistoryItem &item) 0085 { 0086 saveItems(QList<TransferHistoryItem>() << item); 0087 } 0088 0089 void SQLiteStore::saveItems(const QList<TransferHistoryItem> &items) 0090 { 0091 if (sql().open()) { 0092 if (!sql().tables().contains("transfer_history_item")) { 0093 createTables(); 0094 } 0095 0096 if (!sql().transaction()) { 0097 qCWarning(KGET_DEBUG) << "Could not establish a transaction, might be slow."; 0098 } 0099 0100 foreach (const TransferHistoryItem &item, items) { 0101 QSqlQuery query = sql().exec( 0102 "insert into transfer_history_item(source, dest, size, time, state)" 0103 "values ('" 0104 + item.source() + "', '" + item.dest() + "', " + QString::number(item.size()) + ", " + QString::number(item.dateTime().toSecsSinceEpoch()) 0105 + ", '" + QString::number(item.state()) + "')"); 0106 0107 if (query.lastError().isValid()) { 0108 qCDebug(KGET_DEBUG) << query.lastError().text(); 0109 } 0110 m_items << item; 0111 } 0112 0113 if (!sql().commit()) { 0114 qCWarning(KGET_DEBUG) << "Could not commit changes."; 0115 } 0116 } 0117 sql().close(); 0118 0119 Q_EMIT saveFinished(); 0120 } 0121 0122 void SQLiteStore::deleteItem(const TransferHistoryItem &item) 0123 { 0124 if (sql().open()) { 0125 if (!sql().tables().contains("transfer_history_item")) { 0126 createTables(); 0127 } 0128 0129 QSqlQuery query = sql().exec( 0130 "delete from transfer_history_item where " 0131 " source = '" 0132 + item.source() + "';"); 0133 0134 if (query.lastError().isValid()) { 0135 qCDebug(KGET_DEBUG) << query.lastError().text(); 0136 } 0137 0138 sql().commit(); 0139 m_items.removeAll(item); 0140 } 0141 sql().close(); 0142 0143 Q_EMIT deleteFinished(); 0144 } 0145 0146 QSqlDatabase SQLiteStore::sql() 0147 { 0148 if (!m_sql.isValid()) { 0149 m_sql = QSqlDatabase::addDatabase("QSQLITE"); 0150 m_sql.setDatabaseName(m_dbName); 0151 } 0152 0153 return m_sql; 0154 } 0155 0156 void SQLiteStore::createTables() 0157 { 0158 QSqlQuery query = sql().exec( 0159 "CREATE TABLE transfer_history_item(dest VARCHAR NOT NULL, " 0160 "source VARCHAR NOT NULL, size int NOT NULL, time int not null, " 0161 "state int, PRIMARY KEY(dest, source));"); 0162 0163 if (query.lastError().isValid()) { 0164 qCDebug(KGET_DEBUG) << query.lastError().text(); 0165 } 0166 } 0167 0168 #endif 0169 0170 #include "moc_transferhistorystore_sqlite_p.cpp"