dbactions.cpp 4.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***
Pix  Copyright (C) 2018  Camilo Higuita
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

 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 3 of the License, or
(at your option) any later version.

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 <http://www.gnu.org/licenses/>.
***/

21
#include "dbactions.h"
22

23 24 25 26 27 28
#ifdef STATIC_MAUIKIT
#include "fmstatic.h"
#else
#include <MauiKit/fmstatic.h>
#endif

29
DBActions::DBActions(QObject *parent) : DB(parent)
30
{
31 32
    qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
    qDebug()<< "Starting DBActions";
33 34
}

Camilo higuita's avatar
Camilo higuita committed
35 36 37 38 39 40
DBActions *DBActions::instance = nullptr;
DBActions *DBActions::getInstance()
{
    if(!instance)
    {
        qDebug() << "getInstance(): First DBActions instance\n";
41
        instance = new DBActions();
Camilo higuita's avatar
Camilo higuita committed
42 43 44 45 46 47 48
        return instance;
    } else
    {
        qDebug()<< "getInstance(): previous DBActions instance\n";
        return instance;
    }
}
Camilo Higuita's avatar
Camilo Higuita committed
49 50 51

bool DBActions::execQuery(const QString &queryTxt)
{
52
    auto query = this->getQuery(queryTxt);
Camilo Higuita's avatar
Camilo Higuita committed
53 54 55
    return query.exec();
}

56
bool DBActions::insertPic(const FMH::MODEL &img)
Camilo Higuita's avatar
Camilo Higuita committed
57
{
58
    QVariantMap sourceMap {{FMH::MODEL_NAME[FMH::MODEL_KEY::URL], img[FMH::MODEL_KEY::SOURCE]}};
59
    this->insert(PIX::TABLEMAP[PIX::TABLE::SOURCES], sourceMap);
60
    return this->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES], FMH::toMap(img));
Camilo Higuita's avatar
Camilo Higuita committed
61
}
62

63
bool DBActions::addPic(const QUrl &url)
camilo higuita's avatar
camilo higuita committed
64
{
65
    if(!this->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url.toString()))
camilo higuita's avatar
camilo higuita committed
66
    {
67
        const QFileInfo info(url.toLocalFile());
68
        FMH::MODEL picMap =
camilo higuita's avatar
camilo higuita committed
69
        {
70 71
            {FMH::MODEL_KEY::URL, url.toString()},
            {FMH::MODEL_KEY::TITLE,  info.baseName()},
72 73
            {FMH::MODEL_KEY::RATE, "0"},
            {FMH::MODEL_KEY::COLOR, ""},
74 75
            {FMH::MODEL_KEY::SIZE, QString::number(info.size())},
            {FMH::MODEL_KEY::SOURCE, FMH::fileDir(url)},
Camilo higuita's avatar
Camilo higuita committed
76
            {FMH::MODEL_KEY::DATE, info.birthTime().toString(Qt::TextDate)},
77 78
            {FMH::MODEL_KEY::ADDDATE, QDateTime::currentDateTime().toString(Qt::TextDate)},
            {FMH::MODEL_KEY::FORMAT, info.suffix()}
camilo higuita's avatar
camilo higuita committed
79 80
        };

81 82
        return this->insertPic(picMap);
    }
83 84

    return false;
85 86 87 88 89
}

bool DBActions::removePic(const QString &url)
{
    auto queryTxt = QString("DELETE FROM images WHERE url =  \"%1\"").arg(url);
90
    auto query = this->getQuery(queryTxt);
91 92 93
    if(query.exec())
    {
        queryTxt = QString("DELETE FROM images_notes WHERE url =  \"%1\"").arg(url);
94
        this->getQuery(queryTxt).exec();
95 96

        return true;
camilo higuita's avatar
camilo higuita committed
97
    }
98
    return false;
camilo higuita's avatar
camilo higuita committed
99 100
}

101
bool DBActions::deletePic(const QUrl &url)
102
{
103 104
    if(FMStatic::removeFile(url))
        return this->removePic(url.toString());
105 106 107 108

    return false;
}

109 110 111 112
QVariantList DBActions::searchFor(const QStringList &queries, const QString &queryTxt)
{
    QVariantList res;
    for(auto query : queries)
113
        res <<  this->get(PIX::getQuery("searchFor_").arg(query));
114 115 116 117

    return res;
}

Camilo higuita's avatar
Camilo higuita committed
118
FMH::MODEL_LIST DBActions::getFolders(const QString &query)
119
{
120 121 122 123 124
    const auto data = this->getDBData(query);
    return std::accumulate(data.begin(), data.end(), FMH::MODEL_LIST(), [](FMH::MODEL_LIST &res, const FMH::MODEL &item){
        res << FMH::getFileInfoModel(item[FMH::MODEL_KEY::URL]);
        return res;
    });
125
}
Camilo higuita's avatar
Camilo higuita committed
126

127
FMH::MODEL_LIST DBActions::getDBData(const QString &queryTxt, std::function<bool(FMH::MODEL &item)> modifier)
128
{
129
    FMH::MODEL_LIST mapList;
130 131 132 133 134 135 136

    auto query = this->getQuery(queryTxt);

    if(query.exec())
    {
        while(query.next())
        {
137
            FMH::MODEL data;
138
            for(const auto &key : FMH::MODEL_NAME.keys())
139 140
                if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
                    data.insert(key, query.value(FMH::MODEL_NAME[key]).toString());
141

142
            if(modifier)
143 144 145 146 147
            {
                if(!modifier(data))
                    continue;
            }

148
            mapList << data;
149 150 151 152 153 154 155 156
        }

    }else qDebug()<< query.lastError()<< query.lastQuery();

    return mapList;
}

QVariantList DBActions::get(const QString &queryTxt)
Camilo higuita's avatar
Camilo higuita committed
157
{
Camilo higuita's avatar
Camilo higuita committed
158
    return this->getList(queryTxt);
Camilo higuita's avatar
Camilo higuita committed
159
}