2020-03-16 22:37:04 +01:00
/**
* Copyright 2020 Tobias Fella < fella @ posteo . de >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License or ( at your option ) version 3 or any later version
* accepted by the membership of KDE e . V . ( or its successor approved
* by the membership of KDE e . V . ) , which shall act as a proxy
* defined in Section 14 of version 3 of the license .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
*/
# include <QDir>
# include <QStandardPaths>
# include <QSqlError>
# include <QSqlDatabase>
2020-03-26 20:24:28 +01:00
# include <QDateTime>
2020-03-16 22:37:04 +01:00
# include "database.h"
2020-03-26 20:24:28 +01:00
# include "alligatorsettings.h"
2020-03-16 22:37:04 +01:00
# define TRUE_OR_RETURN(x) if(!x) return false;
Database : : Database ( )
{
QSqlDatabase db = QSqlDatabase : : addDatabase ( QStringLiteral ( " QSQLITE " ) ) ;
QString databasePath = QStandardPaths : : writableLocation ( QStandardPaths : : AppDataLocation ) ;
QDir ( databasePath ) . mkpath ( databasePath ) ;
db . setDatabaseName ( databasePath + QStringLiteral ( " /database.db3 " ) ) ;
2020-04-11 23:47:49 +02:00
qDebug ( ) < < " Opening database " < < databasePath + " /database.db3 " ;
2020-03-16 22:37:04 +01:00
db . open ( ) ;
if ( ! migrate ( ) ) {
2020-04-10 17:28:26 +02:00
qCritical ( ) < < " Failed to migrate the database " ;
2020-03-16 22:37:04 +01:00
}
2020-03-26 20:24:28 +01:00
cleanup ( ) ;
2020-03-16 22:37:04 +01:00
}
bool Database : : migrate ( ) {
2020-04-10 17:28:26 +02:00
qDebug ( ) < < " Migrating database " ;
2020-03-16 22:37:04 +01:00
if ( version ( ) < 1 ) TRUE_OR_RETURN ( migrateTo1 ( ) ) ;
return true ;
}
bool Database : : migrateTo1 ( ) {
QSqlQuery query ( QSqlDatabase : : database ( ) ) ;
2020-03-29 18:07:28 +02:00
TRUE_OR_RETURN ( execute ( QStringLiteral ( " CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT, image TEXT); " ) ) ) ;
2020-03-19 00:40:57 +01:00
TRUE_OR_RETURN ( execute ( QStringLiteral ( " CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT, created INTEGER, updated INTEGER); " ) ) ) ;
2020-03-16 22:37:04 +01:00
TRUE_OR_RETURN ( execute ( QStringLiteral ( " CREATE TABLE IF NOT EXISTS Authors (id TEXT, name TEXT, uri TEXT, email TEXT); " ) ) ) ;
TRUE_OR_RETURN ( execute ( QStringLiteral ( " PRAGMA user_version = 1; " ) ) ) ;
return true ;
}
bool Database : : execute ( QString query ) {
QSqlQuery q ;
q . prepare ( query ) ;
return execute ( q ) ;
}
bool Database : : execute ( QSqlQuery & query ) {
if ( ! query . exec ( ) ) {
2020-04-10 17:28:26 +02:00
qWarning ( ) < < " Failed to execute SQL Query " ;
qWarning ( ) < < query . lastQuery ( ) ;
qWarning ( ) < < query . lastError ( ) ;
2020-03-16 22:37:04 +01:00
return false ;
}
return true ;
}
int Database : : version ( ) {
QSqlQuery query ;
query . prepare ( QStringLiteral ( " PRAGMA user_version; " ) ) ;
execute ( query ) ;
if ( query . next ( ) ) {
bool ok ;
int value = query . value ( 0 ) . toInt ( & ok ) ;
2020-04-10 17:28:26 +02:00
qDebug ( ) < < " Database version " < < value ;
2020-03-16 22:37:04 +01:00
if ( ok ) return value ;
} else {
2020-04-10 17:28:26 +02:00
qCritical ( ) < < " Failed to check database version " ;
2020-03-16 22:37:04 +01:00
}
return - 1 ;
}
2020-03-26 20:24:28 +01:00
void Database : : cleanup ( ) {
AlligatorSettings settings ;
int count = settings . deleteAfterCount ( ) ;
int type = settings . deleteAfterType ( ) ;
if ( type = = 0 ) { // Delete after <count> posts per feed
//TODO
} else {
QDateTime dateTime = QDateTime : : currentDateTime ( ) ;
2020-03-27 16:41:29 +01:00
if ( type = = 1 ) dateTime = dateTime . addDays ( - count ) ;
else if ( type = = 2 ) dateTime = dateTime . addDays ( - 7 * count ) ;
else if ( type = = 3 ) dateTime = dateTime . addMonths ( - count ) ;
2020-03-26 20:24:28 +01:00
qint64 sinceEpoch = dateTime . toSecsSinceEpoch ( ) ;
QSqlQuery query ;
query . prepare ( " DELETE FROM Entries WHERE updated < :sinceEpoch; " ) ;
query . bindValue ( " :sinceEpoch " , sinceEpoch ) ;
execute ( query ) ;
}
}