File indexing completed on 2025-01-05 04:24:22

0001 #include "dbactions.h"
0002 
0003 #include <QDateTime>
0004 #include <QSqlQuery>
0005 #include <QSqlRecord>
0006 #include <QSqlError>
0007 
0008 DBActions *DBActions::m_instance = nullptr;
0009 
0010 void DBActions::addToHistory(const UrlData &data)
0011 {
0012     auto mData = data.toMap();
0013     mData.insert("adddate",  QDateTime::currentDateTime().toString(Qt::TextDate));
0014     if(this->insert("HISTORY", mData))
0015     {
0016         emit this->historyUrlInserted(data);
0017     }
0018 }
0019 
0020 void DBActions::addBookmark(const UrlData &data)
0021 {
0022     auto mData = data.toMap();
0023     mData.insert("adddate",  QDateTime::currentDateTime().toString(Qt::TextDate));
0024     if(this->insert("BOOKMARKS", mData))
0025     {
0026         emit this->bookmarkInserted(data);
0027     }
0028 }
0029 
0030 void DBActions::urlIcon(const QUrl &url, const QString &icon)
0031 {
0032     if(!this->insert("ICONS", {{"url", url.toString()}, {"icon", icon}}))
0033     {
0034         this->update("ICONS", {{FMH::MODEL_KEY::ICON, icon}}, {{"url", url}});
0035     }
0036 
0037     emit this->iconInserted(url, icon);
0038 }
0039 
0040 FMH::MODEL_LIST DBActions::getHistory() const
0041 {
0042 return FMH::toModelList(this->get("select * from HISTORY h inner join ICONS i where i.url = h.url"));
0043 }
0044 
0045 FMH::MODEL_LIST DBActions::getBookmarks() const
0046 {
0047     return FMH::toModelList(this->get("select * from BOOKMARKS b inner join ICONS i where i.url = b.url"));
0048 }
0049 
0050 bool DBActions::isBookmark(const QUrl &url)
0051 {
0052     return checkExistance("BOOKMARKS", "url", url.toString());
0053 }
0054 
0055 DBActions::DBActions(QObject *parent) : DB(parent)
0056 {
0057     connect(qApp, &QCoreApplication::aboutToQuit, []()
0058     {
0059         qDebug() << "Lets remove Tagging singleton instance";
0060         delete m_instance;
0061         m_instance = nullptr;
0062     });
0063 }
0064 
0065 const QVariantList DBActions::get(const QString &queryTxt, std::function<bool(QVariantMap &item)> modifier) const
0066 {
0067     QVariantList mapList;
0068 
0069     auto query = this->getQuery(queryTxt);
0070 
0071     if (query.exec()) {
0072         const auto keys = FMH::MODEL_NAME.keys();
0073 
0074         while (query.next()) {
0075             QVariantMap data;
0076             for (const auto &key : keys) {
0077 
0078                 if (query.record().indexOf(FMH::MODEL_NAME[key]) > -1) {
0079                     data[FMH::MODEL_NAME[key]] = query.value(FMH::MODEL_NAME[key]).toString();
0080                 }
0081             }
0082 
0083             if (modifier) {
0084                 if (!modifier(data))
0085                 {
0086                     continue;
0087                 }
0088             }
0089 
0090             mapList << data;
0091         }
0092 
0093     } else
0094     {
0095         qDebug() << query.lastError() << query.lastQuery();
0096     }
0097 
0098     return mapList;
0099 }