dbactions.cpp 8.43 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

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

29
DBActions::DBActions(QObject *parent) : DB(parent) {}
30

31 32 33
DBActions::~DBActions() {}

void DBActions::init()
34
{
35
    qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
36

37
    qDebug()<< "Starting DBActions";
38
    this->tag = Tagging::getInstance();
39 40
}

41
DBActions *DBActions::instance = nullptr;
Camilo Higuita's avatar
Camilo Higuita committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56
DBActions *DBActions::getInstance()
{
    if(!instance)
    {
        instance = new DBActions();
        qDebug() << "getInstance(): First DBActions instance\n";
        instance->init();
        return instance;
    } else
    {
        qDebug()<< "getInstance(): previous DBActions instance\n";
        return instance;
    }
}
Camilo Higuita's avatar
Camilo Higuita committed
57 58 59

bool DBActions::execQuery(const QString &queryTxt)
{
60
    auto query = this->getQuery(queryTxt);
Camilo Higuita's avatar
Camilo Higuita committed
61 62 63
    return query.exec();
}

64
bool DBActions::insertPic(const FMH::MODEL &img)
Camilo Higuita's avatar
Camilo Higuita committed
65
{
66 67 68 69 70 71 72 73
    auto url = img[FMH::MODEL_KEY::URL];
    auto title = img[FMH::MODEL_KEY::TITLE];
    auto rate = img[FMH::MODEL_KEY::RATE];
    auto fav = img[FMH::MODEL_KEY::FAV];
    auto color = img[FMH::MODEL_KEY::COLOR];
    auto sourceUrl = img[FMH::MODEL_KEY::SOURCE];
    auto picDate = img[FMH::MODEL_KEY::DATE];
    auto format = img[FMH::MODEL_KEY::FORMAT];
74 75 76

    qDebug()<< "writting to db: "<<title<<url;
    /* first needs to insert album and artist*/
77
    QVariantMap sourceMap {{FMH::MODEL_NAME[FMH::MODEL_KEY::URL], sourceUrl}};
78
    this->insert(PIX::TABLEMAP[PIX::TABLE::SOURCES], sourceMap);
79 80


81 82 83 84 85 86 87 88
    QVariantMap imgMap {{FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::SOURCE], sourceUrl},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::RATE], rate},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::FAV], fav},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR], color},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::FORMAT], format},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::DATE], picDate},
Camilo higuita's avatar
Camilo higuita committed
89 90
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::PLACE], QString()},
                        {FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE], QDateTime::currentDateTime().toString(Qt::TextDate)}};
91

92
    return this->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES], imgMap);
Camilo Higuita's avatar
Camilo Higuita committed
93 94

}
95

camilo higuita's avatar
camilo higuita committed
96 97
bool DBActions::addPic(const QString &url)
{
98
    if(!this->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url))
camilo higuita's avatar
camilo higuita committed
99 100 101 102 103 104
    {
        QFileInfo info(url);
        auto title = info.baseName();
        auto format = info.suffix();
        auto sourceUrl = info.dir().path();

105
        FMH::MODEL picMap =
camilo higuita's avatar
camilo higuita committed
106
        {
107 108 109 110 111 112
            {FMH::MODEL_KEY::URL, url},
            {FMH::MODEL_KEY::TITLE, title},
            {FMH::MODEL_KEY::FAV, "0"},
            {FMH::MODEL_KEY::RATE, "0"},
            {FMH::MODEL_KEY::COLOR, ""},
            {FMH::MODEL_KEY::SOURCE, sourceUrl},
Camilo higuita's avatar
Camilo higuita committed
113
            {FMH::MODEL_KEY::DATE, info.birthTime().toString(Qt::TextDate)},
114
            {FMH::MODEL_KEY::FORMAT, format}
camilo higuita's avatar
camilo higuita committed
115 116
        };

117 118
        return this->insertPic(picMap);
    }
119 120

    return false;
121 122 123 124 125
}

bool DBActions::removePic(const QString &url)
{
    auto queryTxt = QString("DELETE FROM images WHERE url =  \"%1\"").arg(url);
126
    auto query = this->getQuery(queryTxt);
127 128 129
    if(query.exec())
    {
        queryTxt = QString("DELETE FROM images_tags WHERE url =  \"%1\"").arg(url);
130
        this->getQuery(queryTxt).exec();
131 132

        queryTxt = QString("DELETE FROM images_albums WHERE url =  \"%1\"").arg(url);
133
        this->getQuery(queryTxt).exec();
134 135

        queryTxt = QString("DELETE FROM images_notes WHERE url =  \"%1\"").arg(url);
136
        this->getQuery(queryTxt).exec();
137 138

        return true;
camilo higuita's avatar
camilo higuita committed
139
    }
140
    return false;
camilo higuita's avatar
camilo higuita committed
141 142
}

143 144 145 146 147 148 149 150 151 152 153
bool DBActions::deletePic(const QString &url)
{
    QFile file(url);
    if(!file.exists()) return false;

    if(file.remove())
        return this->removePic(url);

    return false;
}

154 155
bool DBActions::favPic(const QString &url, const bool &fav )
{
156
    if(!this->checkExistance("images", "url", url))
157 158 159
        if(!this->addPic(url))
            return false;

160
    FMH::MODEL favedPic = {{FMH::MODEL_KEY::FAV, fav ? "1" : "0"}};
Camilo higuita's avatar
Camilo higuita committed
161
    return this->update(PIX::TABLEMAP[PIX::TABLE::IMAGES], favedPic, QVariantMap({{FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url}}) );
162 163 164 165
}

bool DBActions::isFav(const QString &url)
{
166
    auto data = this->getDBData(QString("select * from images where url = '%1'").arg(url));
167 168 169

    if (data.isEmpty()) return false;

170
    return data.first()[FMH::MODEL_KEY::FAV] == "1" ? true : false;
171 172
}

173
bool DBActions::addTag(const QString &tag)
174
{
Camilo Higuita's avatar
Camilo Higuita committed
175
    if (this->tag->tag(tag))
Camilo Higuita's avatar
Camilo Higuita committed
176 177 178 179 180 181
    {
        emit tagAdded(tag);
        return true;
    }

    return false;
182
}
183

184 185 186
bool DBActions::albumTag(const QString &tag, const QString &album)
{
    this->addTag(tag);
187

188
    return this->tag->tagAbstract(tag, FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM], album);
189 190
}

191 192
bool DBActions::removePicTag(const QString &tag, const QString &url)
{
193
    FMH::MODEL tagMap {{FMH::MODEL_KEY::URL, url}, {FMH::MODEL_KEY::TAG, tag}};
194
    return this->remove(PIX::TABLEMAP[PIX::TABLE::IMAGES_TAGS], tagMap);
195 196
}

197 198
bool DBActions::removeAlbumTag(const QString &tag, const QString &album)
{
199
    FMH::MODEL tagMap {{FMH::MODEL_KEY::TAG, tag}, {FMH::MODEL_KEY::ALBUM, album}};
200
    return this->remove(PIX::TABLEMAP[PIX::TABLE::ALBUMS_TAGS], tagMap);
201 202
}

Camilo Higuita's avatar
Camilo Higuita committed
203 204 205 206 207
bool DBActions::cleanTags()
{
    return false;
}

208 209 210 211
bool DBActions::addAlbum(const QString &album)
{
    QVariantMap albumMap
    {
212
        {FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM], album},
213
        {FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE], QDateTime::currentDateTime()}
214 215
    };

216 217 218 219 220 221 222
    if(this->insert(PIX::TABLEMAP[PIX::TABLE::ALBUMS], albumMap))
    {
        emit this->albumAdded(album);
        return true;
    }

    return false;
223 224
}

225 226 227
bool DBActions::picAlbum(const QString &album, const QString &url)
{
    qDebug()<<"Trying to add to album"<<album<<url;
228
    this->addAlbum(album);
229 230
    QVariantMap albumPic
    {
231 232
        {FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM], album},
233
        {FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE], QDateTime::currentDateTime()}
234
    };
235
    return this->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES_ALBUMS], albumPic);
236 237
}

238 239 240 241
QVariantList DBActions::searchFor(const QStringList &queries, const QString &queryTxt)
{
    QVariantList res;
    for(auto query : queries)
242
        res <<  this->get(PIX::getQuery("searchFor_").arg(query));
243 244 245 246

    return res;
}

247
FMH::MODEL_LIST DBActions::getFolders(const QString &query)
248
{
249
    FMH::MODEL_LIST res;
250
    auto data =  this->getDBData(query);
251

252
    /*Data model keys for to be used on MauiKit Icondelegate component */
253
    for(auto i : data)
254
        res << FMH::getFileInfoModel(i[FMH::MODEL_KEY::URL]);
255

256
    return res;
257
}
258

259
FMH::MODEL_LIST DBActions::getDBData(const QString &queryTxt)
260
{
261
    FMH::MODEL_LIST mapList;
262 263 264 265 266 267 268

    auto query = this->getQuery(queryTxt);

    if(query.exec())
    {
        while(query.next())
        {
269 270 271 272
            FMH::MODEL data;
            for(auto key : FMH::MODEL_NAME.keys())
                if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
                    data.insert(key, query.value(FMH::MODEL_NAME[key]).toString());
273

274
            const auto url = data[FMH::MODEL_KEY::URL];
275 276 277 278 279
            if(!url.isEmpty())
            {
                if(FMH::fileExists(url))
                    mapList<< data;
                else
280
                    this->removePic(data[FMH::MODEL_KEY::URL]);
281 282 283 284 285 286 287 288 289
            }else mapList<< data;
        }

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

    return mapList;
}

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