File indexing completed on 2025-01-05 04:29:52

0001 /**
0002  * SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "models/errorlogmodel.h"
0008 
0009 #include <QDebug>
0010 #include <QSqlQuery>
0011 
0012 #include "database.h"
0013 #include "datamanager.h"
0014 
0015 ErrorLogModel::ErrorLogModel()
0016     : QAbstractListModel(nullptr)
0017 {
0018     QSqlQuery query;
0019     query.prepare(QStringLiteral("SELECT * FROM Errors ORDER BY date DESC;"));
0020     Database::instance().execute(query);
0021     while (query.next()) {
0022         Error *error = new Error(Error::dbToType(query.value(QStringLiteral("type")).toInt()),
0023                                  query.value(QStringLiteral("url")).toString(),
0024                                  query.value(QStringLiteral("id")).toString(),
0025 
0026                                  query.value(QStringLiteral("code")).toInt(),
0027                                  query.value(QStringLiteral("message")).toString(),
0028                                  QDateTime::fromSecsSinceEpoch(query.value(QStringLiteral("date")).toInt()),
0029                                  query.value(QStringLiteral("title")).toString());
0030         m_errors += error;
0031     }
0032 }
0033 
0034 QVariant ErrorLogModel::data(const QModelIndex &index, int role) const
0035 {
0036     if (role != 0) {
0037         return QVariant();
0038     }
0039     return QVariant::fromValue(m_errors[index.row()]);
0040 }
0041 
0042 QHash<int, QByteArray> ErrorLogModel::roleNames() const
0043 {
0044     QHash<int, QByteArray> roleNames;
0045     roleNames[0] = "error";
0046     return roleNames;
0047 }
0048 
0049 int ErrorLogModel::rowCount(const QModelIndex &parent) const
0050 {
0051     Q_UNUSED(parent)
0052     return m_errors.count();
0053 }
0054 
0055 void ErrorLogModel::monitorErrorMessages(const Error::Type type,
0056                                          const QString &url,
0057                                          const QString &id,
0058                                          const int errorCode,
0059                                          const QString &errorString,
0060                                          const QString &title)
0061 {
0062     qDebug() << "Error happened:" << type << url << id << errorCode << errorString;
0063 
0064     Error *error = new Error(type, url, id, errorCode, errorString, QDateTime::currentDateTime(), title);
0065     beginInsertRows(QModelIndex(), 0, 0);
0066     m_errors.prepend(error);
0067     endInsertRows();
0068 
0069     // Also add error to database
0070     QSqlQuery query;
0071     query.prepare(QStringLiteral("INSERT INTO Errors VALUES (:type, :url, :id, :code, :message, :date, :title);"));
0072     query.bindValue(QStringLiteral(":type"), Error::typeToDb(type));
0073     query.bindValue(QStringLiteral(":url"), url);
0074     query.bindValue(QStringLiteral(":id"), id);
0075     query.bindValue(QStringLiteral(":code"), errorCode);
0076     query.bindValue(QStringLiteral(":message"), errorString);
0077     query.bindValue(QStringLiteral(":date"), error->date.toSecsSinceEpoch());
0078     query.bindValue(QStringLiteral(":title"), title);
0079     Database::instance().execute(query);
0080 
0081     // Send signal to display inline error message
0082     Q_EMIT newErrorLogged(error);
0083 }
0084 
0085 void ErrorLogModel::clearAll()
0086 {
0087     beginResetModel();
0088     for (auto &error : m_errors) {
0089         delete error;
0090     }
0091     m_errors.clear();
0092     endResetModel();
0093 
0094     // Also clear errors from database
0095     QSqlQuery query;
0096     query.prepare(QStringLiteral("DELETE FROM Errors;"));
0097     Database::instance().execute(query);
0098 }