File indexing completed on 2024-04-14 08:31:02

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 #include "core/transferhistorystore.h"
0012 #include "core/transferhistorystore_xml_p.h"
0013 #ifdef HAVE_SQLITE
0014 #include "core/transferhistorystore_sqlite_p.h"
0015 #endif
0016 #include "core/transfer.h"
0017 #include "settings.h"
0018 
0019 #include <QDateTime>
0020 #include <QDir>
0021 #include <QThread>
0022 
0023 #include <KIO/Global>
0024 #include <QStandardPaths>
0025 
0026 TransferHistoryItem::TransferHistoryItem()
0027     : QObject()
0028 {
0029 }
0030 
0031 TransferHistoryItem::TransferHistoryItem(const Transfer &transfer)
0032     : QObject()
0033 {
0034     setDest(transfer.dest().toLocalFile());
0035     setSource(transfer.source().url());
0036     setSize(transfer.totalSize());
0037     setDateTime(QDateTime::currentDateTime());
0038 
0039     setState(transfer.status());
0040 }
0041 
0042 TransferHistoryItem::TransferHistoryItem(const TransferHistoryItem &item)
0043     : QObject()
0044 {
0045     setDest(item.dest());
0046     setSource(item.source());
0047     setState(item.state());
0048     setSize(item.size());
0049     setDateTime(item.dateTime());
0050 }
0051 
0052 bool TransferHistoryItem::isExpired(qint64 expiryAge)
0053 {
0054     if (expiryAge == -1)
0055         return false;
0056 
0057     const qint64 lifeSpan = dateTime().secsTo(QDateTime::currentDateTime());
0058     return (lifeSpan > expiryAge);
0059 }
0060 
0061 void TransferHistoryItem::setDest(const QString &dest)
0062 {
0063     m_dest = dest;
0064 }
0065 
0066 void TransferHistoryItem::setSource(const QString &source)
0067 {
0068     m_source = source;
0069 }
0070 
0071 void TransferHistoryItem::setState(int state)
0072 {
0073     m_state = state;
0074 }
0075 
0076 void TransferHistoryItem::setSize(int size)
0077 {
0078     m_size = size;
0079 }
0080 
0081 void TransferHistoryItem::setDateTime(const QDateTime &dateTime)
0082 {
0083     m_dateTime = dateTime;
0084 }
0085 
0086 QString TransferHistoryItem::dest() const
0087 {
0088     return m_dest;
0089 }
0090 
0091 QString TransferHistoryItem::source() const
0092 {
0093     return m_source;
0094 }
0095 
0096 int TransferHistoryItem::state() const
0097 {
0098     return m_state;
0099 }
0100 
0101 int TransferHistoryItem::size() const
0102 {
0103     return m_size;
0104 }
0105 
0106 QDateTime TransferHistoryItem::dateTime() const
0107 {
0108     return m_dateTime;
0109 }
0110 
0111 TransferHistoryItem &TransferHistoryItem::operator=(const TransferHistoryItem &item)
0112 {
0113     setDest(item.dest());
0114     setSource(item.source());
0115     setState(item.state());
0116     setSize(item.size());
0117     setDateTime(item.dateTime());
0118 
0119     return *this;
0120 }
0121 
0122 bool TransferHistoryItem::operator==(const TransferHistoryItem &item) const
0123 {
0124     return dest() == item.dest() && source() == item.source();
0125 }
0126 
0127 TransferHistoryStore::TransferHistoryStore()
0128     : QObject()
0129     , m_items()
0130     , m_expiryAge(getSettingsExpiryAge())
0131 {
0132 }
0133 
0134 TransferHistoryStore::~TransferHistoryStore()
0135 {
0136 }
0137 
0138 qint64 TransferHistoryStore::getSettingsExpiryAge()
0139 {
0140     if (!Settings::automaticDeletionEnabled())
0141         return -1;
0142 
0143     qint64 timeVal = static_cast<qint64>(Settings::expiryTimeValue());
0144     switch (Settings::expiryTimeType()) {
0145     case Day:
0146         timeVal *= 24;
0147         [[fallthrough]];
0148     case Hour:
0149         timeVal *= 60;
0150         [[fallthrough]];
0151     case Minute:
0152         timeVal *= 60;
0153     }
0154     return timeVal;
0155 }
0156 
0157 qint64 TransferHistoryStore::expiryAge() const
0158 {
0159     return m_expiryAge;
0160 }
0161 
0162 void TransferHistoryStore::updateExpiryAge(qint64 timeVal)
0163 {
0164     m_expiryAge = timeVal;
0165     deleteExpiredItems();
0166 }
0167 
0168 void TransferHistoryStore::deleteExpiredItems()
0169 {
0170     QList<TransferHistoryItem> &items = m_items;
0171     for (auto &item : items) {
0172         if (item.isExpired(m_expiryAge))
0173             deleteItem(item);
0174     }
0175 }
0176 
0177 QList<TransferHistoryItem> TransferHistoryStore::items() const
0178 {
0179     return m_items;
0180 }
0181 
0182 void TransferHistoryStore::settingsChanged()
0183 {
0184     updateExpiryAge(getSettingsExpiryAge());
0185 }
0186 
0187 TransferHistoryStore *TransferHistoryStore::getStore()
0188 {
0189     // make sure that the DataLocation directory exists (earlier this used to be handled by KStandardDirs)
0190     if (!QFileInfo::exists(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation))) {
0191         QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
0192     }
0193     switch (Settings::historyBackend()) {
0194     case TransferHistoryStore::SQLite:
0195 #ifdef HAVE_SQLITE
0196         return new SQLiteStore(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/transferhistory.db"));
0197         break;
0198 #endif
0199     case TransferHistoryStore::Xml:
0200     default:
0201         return new XmlStore(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/transferhistory.kgt"));
0202     }
0203 }
0204 
0205 #include "moc_transferhistorystore.cpp"