babe.cpp 15.2 KB
Newer Older
1 2 3
#include "babe.h"

#include "db/collectionDB.h"
Camilo higuita's avatar
Camilo higuita committed
4
//#include "db/conthread.h"
5
#include "settings/BabeSettings.h"
6 7 8
#include <QPalette>
#include <QColor>
#include <QIcon>
9
#include <QGuiApplication>
10
#include <QDirIterator>
11
#include <QtQml>
12
#include <QDesktopServices>
13
#include <QCursor>
14
#include "services/local/taginfo.h"
15
//#include "Python.h"
16 17 18 19

#ifdef STATIC_MAUIKIT
#include "fm.h"
#else
20
#include <MauiKit/fm.h>
21
#endif
22

23
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
24
#include <QWidget>
25 26 27
#include "kde/notify.h"
#endif

28 29
using namespace BAE;

30
Babe::Babe(QObject *parent) : QObject(parent)
31
{    
32
    this->settings = new BabeSettings(this);
Camilo higuita's avatar
Camilo higuita committed
33 34

    /*use another thread for the db to perfom heavy dutty actions*/
Camilo higuita's avatar
Camilo higuita committed
35
//    this->thread = new ConThread;
36
    this->db = CollectionDB::getInstance();
Camilo higuita's avatar
Camilo higuita committed
37

38 39 40
//    connect(pulpo, &Pulpo::infoReady, [&](const FMH::MODEL &track, const PULPO::RESPONSES  &res)
//    {
//        qDebug()<<"GOT THE LYRICS";
Camilo higuita's avatar
Camilo higuita committed
41

42 43 44
//        if(!res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS].isEmpty())
//        {
//            auto lyrics = res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS][PULPO::CONTEXT::LYRIC].toString();
Camilo higuita's avatar
Camilo higuita committed
45

46 47 48 49
//            this->db->lyricsTrack(track, lyrics);
//            emit this->trackLyricsReady(lyrics, track[FMH::MODEL_KEY::URL]);
//        }
//    });
Camilo higuita's avatar
Camilo higuita committed
50

51

52
    connect(settings, &BabeSettings::refreshTables, [this](int size)
53
    {
54
        emit this->refreshTables(size);
55
    });
56

57 58 59 60
    connect(settings, &BabeSettings::refreshATable, [this](BAE::TABLE table)
    {
        switch(table)
        {
61 62 63
        case BAE::TABLE::TRACKS: emit this->refreshTracks(); break;
        case BAE::TABLE::ALBUMS: emit this->refreshAlbums(); break;
        case BAE::TABLE::ARTISTS: emit this->refreshArtists(); break;
64
        default: break;
65 66 67 68
        }

    });

Camilo higuita's avatar
Camilo higuita committed
69
    /*The local streaming connection still unfinished*/
Camilo higuita's avatar
Camilo higuita committed
70
//    connect(&link, &Linking::parseAsk, this, &Babe::linkDecoder);
71 72 73 74 75 76 77 78 79 80 81
//    connect(&link, &Linking::bytesFrame, [this](QByteArray array)
//    {
//        this->player.appendBuffe(array);

//    });
//    connect(&link, &Linking::arrayReady, [this](QByteArray array)
//    {
//        qDebug()<<"trying to play the array";
//        Q_UNUSED(array);
//        this->player.playBuffer();
//    });
82

83 84
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    this->nof = new Notify(this);
85
    connect(this->nof,&Notify::babeSong,[this]()
86 87 88 89 90 91 92 93
    {
        emit this->babeIt();
    });

    connect(this->nof,&Notify::skipSong,[this]()
    {
        emit this->skipTrack();
    });
94
#elif defined (Q_OS_ANDROID)
95

96 97
#endif

98 99
}

Camilo higuita's avatar
Camilo higuita committed
100
Babe::~Babe(){}
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

//void Babe::runPy()
//{

//    QFile cat (BAE::CollectionDBPath+"cat");
//    qDebug()<<cat.exists()<<cat.permissions();
//    if(!cat.setPermissions(QFile::ExeGroup | QFile::ExeOther | QFile::ExeOther | QFile::ExeUser))
//        qDebug()<<"Faile dot give cat permissionsa";
//    qDebug()<<cat.exists()<<cat.permissions();

//    QProcess process;
//    process.setWorkingDirectory(BAE::CollectionDBPath);
//    process.start("./cat", QStringList());

//    bool finished = process.waitForFinished(-1);
//    QString p_stdout = process.readAll();
//    qDebug()<<p_stdout<<finished<<process.workingDirectory()<<process.errorString();
//}

121 122
QVariantList Babe::get(const QString &queryTxt)
{
123
    return this->db->getDBDataQML(queryTxt);
124
}
125

126 127
QVariantList Babe::getList(const QStringList &urls)
{
128
    return Babe::transformData(this->db->getDBData(urls));
129 130
}

Camilo higuita's avatar
Camilo higuita committed
131 132 133 134
//void Babe::set(const QString &table, const QVariantList &wheres)
//{
//    this->thread->start(table, wheres);
//}
135

Camilo higuita's avatar
Camilo higuita committed
136 137 138 139 140 141 142 143
//void Babe::trackPlaylist(const QStringList &urls, const QString &playlist)
//{
//    QVariantList data;
//    for(auto url : urls)
//    {
//        QVariantMap map {{FMH::MODEL_NAME[FMH::MODEL_KEY::PLAYLIST],playlist},
//                         {FMH::MODEL_NAME[FMH::MODEL_KEY::URL],url},
//                         {FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE],QDateTime::currentDateTime()}};
144

Camilo higuita's avatar
Camilo higuita committed
145 146
//        data << map;
//    }
147

Camilo higuita's avatar
Camilo higuita committed
148 149
//    this->thread->start(BAE::TABLEMAP[TABLE::TRACKS_PLAYLISTS], data);
//}
150

151
void Babe::trackLyrics(const QString &url)
152
{
153 154
    auto track = this->db->getDBData(QString("SELECT * FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::TRACKS],
                                     FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url));
155

156
    if(track.isEmpty()) return;
157

158 159 160 161
//    qDebug()<< "Getting lyrics for track"<< track.first()[FMH::MODEL_KEY::TITLE];
//    if(!track.first()[FMH::MODEL_KEY::LYRICS].isEmpty() && track.first()[FMH::MODEL_KEY::LYRICS] != SLANG[W::NONE])
//        emit this->trackLyricsReady(track.first()[FMH::MODEL_KEY::LYRICS], url);
//    else
162
        this->fetchTrackLyrics(track.first());
163 164 165 166 167
}


QString Babe::artistArt(const QString &artist)
{
168 169 170
    auto artwork = this->db->getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::ARTWORK],
                                       TABLEMAP[TABLE::ARTISTS],
            FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST],artist));
171 172

    if(!artwork.isEmpty())
173 174
        if(!artwork.first()[FMH::MODEL_KEY::ARTWORK].isEmpty() && artwork.first()[FMH::MODEL_KEY::ARTWORK] != SLANG[W::NONE])
            return artwork.first()[FMH::MODEL_KEY::ARTWORK];
175 176 177 178 179 180

    return "";
}

QString Babe::artistWiki(const QString &artist)
{
181 182 183
    auto wiki = this->db->getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::WIKI],
                                    TABLEMAP[TABLE::ARTISTS],
            FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST],artist));
184 185

    if(!wiki.isEmpty())
186
        return wiki.first()[FMH::MODEL_KEY::WIKI];
187 188 189 190 191 192

    return "";
}

QString Babe::albumArt(const QString &album, const QString &artist)
{
193
    auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\" AND %5 = \"%6\"").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::ARTWORK],
194
            TABLEMAP[TABLE::ALBUMS],
195 196
            FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM], album,
            FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST], artist);
197

198
    auto albumCover = this->db->getDBData(queryStr);
199 200

    if(!albumCover.isEmpty())
201 202
        if(!albumCover.first()[FMH::MODEL_KEY::ARTWORK].isEmpty() && albumCover.first()[FMH::MODEL_KEY::ARTWORK] != SLANG[W::NONE])
            return albumCover.first()[FMH::MODEL_KEY::ARTWORK];
203 204 205 206

    return "";
}

207
void Babe::fetchTrackLyrics(FMH::MODEL &song)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
{/*
    auto p = new Pulpo(this);

    connect(p, &Pulpo::infoReady, [=](const FMH::MODEL &track, const PULPO::RESPONSE  &res)
    {
        qDebug()<<"GOT THE LYRICS";

        if(!res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS].isEmpty())
        {
            auto lyrics = res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS][PULPO::CONTEXT::LYRIC].toString();

//            this->db->lyricsTrack(track, lyrics);
            emit this->trackLyricsReady(lyrics, track[FMH::MODEL_KEY::URL]);
            p->deleteLater();
        }
    });

    p->registerServices({SERVICES::Genius});
    p->setOntology(PULPO::ONTOLOGY::TRACK);
    p->setInfo(PULPO::INFO::LYRICS);
228

229
    qDebug()<<"STARTED FETCHING LYRICS";
230
    p->feed(song, PULPO::RECURSIVE::OFF);
231

232
    qDebug()<<"DONE FETCHING LYRICS";*/
233 234
}

Camilo higuita's avatar
Camilo higuita committed
235 236
//void Babe::linkDecoder(QString json)
//{
237

Camilo higuita's avatar
Camilo higuita committed
238 239
//    qDebug()<<"DECODING LINKER MSG"<<json;
//    auto ask = link.decode(json);
240

Camilo higuita's avatar
Camilo higuita committed
241 242
//    auto code = ask[BAE::SLANG[BAE::W::CODE]].toInt();
//    auto msg = ask[BAE::SLANG[BAE::W::MSG]].toString();
243

Camilo higuita's avatar
Camilo higuita committed
244 245 246 247 248 249 250 251 252 253 254 255 256
//    switch(static_cast<LINK::CODE>(code))
//    {
//    case LINK::CODE::CONNECTED :
//    {
//        this->link.deviceName = msg;
//        emit this->link.serverConReady(msg);
//        break;
//    }
//    case LINK::CODE::QUERY :
//    case LINK::CODE::FILTER :
//    case LINK::CODE::PLAYLISTS :
//    {
//        auto res = this->db->getDBDataQML(msg);
257
//        link.sendToClient(link.packResponse(static_cast<LINK::CODE>(code), res));
Camilo higuita's avatar
Camilo higuita committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
//        break;
//    }
//    case LINK::CODE::SEARCHFOR :
//    {
////        auto res = this->searchFor(msg.split(","));
////        link.sendToClient(link.packResponse(static_cast<LINK::CODE>(code), res));
//        break;
//    }
//    case LINK::CODE::PLAY :
//    {
//        QFile file(msg);    // sound dir
//        file.open(QIODevice::ReadOnly);
//        QByteArray arr = file.readAll();
//        qDebug()<<"Preparing track array"<<msg<<arr.size();
//        link.sendArrayToClient(arr);
//        break;
//    }
//    case LINK::CODE::COLLECT :
//    {
//        //            auto devices = getDevices();
//        //            qDebug()<<"DEVICES:"<< devices;
//        //            if(!devices.isEmpty())
//        //                sendToDevice(devices.first().toMap().value("name").toString(),
//        //                             devices.first().toMap().value("id").toString(), msg);
//        break;
283

Camilo higuita's avatar
Camilo higuita committed
284 285
//    }
//    default: break;
286

Camilo higuita's avatar
Camilo higuita committed
287 288
//    }
//}
289

290 291
QString Babe::albumWiki(const QString &album, const QString &artist)
{
292
    auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\" AND %5 = \"%6\"").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::WIKI],
293
            TABLEMAP[TABLE::ALBUMS],
294 295 296
            FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM],album,
            FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST],artist);
    auto wiki = this->db->getDBData(queryStr);
297 298

    if(!wiki.isEmpty())
299
        return wiki.first()[FMH::MODEL_KEY::WIKI];
300 301 302 303

    return "";
}

Camilo higuita's avatar
Camilo higuita committed
304 305
QVariantList Babe::getFolders()
{
306
    auto sources = this->db->getDBData("select * from sources");
Camilo higuita's avatar
Camilo higuita committed
307 308 309 310

    QVariantList res;

    for(auto item : sources)
311
        res << FMH::getDirInfo(item[FMH::MODEL_KEY::URL]);
Camilo higuita's avatar
Camilo higuita committed
312 313 314 315 316

    qDebug()<<"FOLDERS:"<< res;
    return res;
}

317 318 319 320 321
QStringList Babe::getSourceFolders()
{
    return this->db->getSourcesFolders();
}

322

323 324 325
void Babe::notify(const QString &title, const QString &body)
{
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
326
    this->nof->notify(title, body);
327
#elif defined (Q_OS_ANDROID)
328 329
    Q_UNUSED(title);
    Q_UNUSED(body);
330 331 332
#endif
}

333 334
void Babe::notifySong(const QString &url)
{
335
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))    
336
    if(!this->db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url))
337 338
        return;

339
    auto query = QString("select t.*, al.artwork from tracks t inner join albums al on al.album = t.album and al.artist = t.artist where url = \"%1\"").arg(url);
340
    auto track = this->db->getDBData(query);
341
    this->nof->notifySong(track.first());
342

343
#else
Camilo Higuita's avatar
Camilo Higuita committed
344
    Q_UNUSED(url);
345 346 347
#endif
}

348 349
void Babe::scanDir(const QString &url)
{
350
    emit this->settings->collectionPathChanged({url});
351 352
}

353
void Babe::brainz(const bool &on)
Camilo higuita's avatar
Camilo higuita committed
354 355 356
{    
    qDebug()<< "Changed vvae brainz state"<< on;
    this->settings->startBrainz(on);
357 358
}

359 360
bool Babe::brainzState()
{
Camilo higuita's avatar
Camilo higuita committed
361
    return FM::loadSettings("AUTO", "BRAINZ", false).toBool();
362 363
}

364 365 366 367 368
void Babe::refreshCollection()
{
    this->settings->refreshCollection();
}

369 370 371 372 373
void Babe::getYoutubeTrack(const QString &message)
{
    this->settings->fetchYoutubeTrack(message);
}

374 375
void Babe::savePlaylist(const QStringList &list)
{
376
    FM::saveSettings("LASTPLAYLIST", list, "PLAYLIST");
377 378 379 380
}

QStringList Babe::lastPlaylist()
{
381
    return FM::loadSettings("LASTPLAYLIST","PLAYLIST", QStringList()).toStringList();
382 383 384 385 386 387 388 389 390 391 392 393
}

void Babe::savePlaylistPos(const int &pos)
{
    BAE::saveSettings("PLAYLIST_POS", pos, "MAINWINDOW");
}

int Babe::lastPlaylistPos()
{
    return BAE::loadSettings("PLAYLIST_POS","MAINWINDOW",QVariant(0)).toInt();
}

394
void Babe::showFolder(const QStringList &urls)
Camilo higuita's avatar
Camilo higuita committed
395
{    
396
    for(auto url : urls)
Camilo higuita's avatar
Camilo higuita committed
397
        FM::openUrl(QUrl::fromLocalFile(QFileInfo(url).dir().absolutePath()).toString());
398 399
}

400 401
QString Babe::babeColor()
{
402
    return "#f84172";
403 404
}

405
void Babe::openUrls(const QStringList &urls)
Camilo Higuita's avatar
Camilo Higuita committed
406
{
407
    if(urls.isEmpty()) return;
Camilo Higuita's avatar
Camilo Higuita committed
408

409 410
    QVariantList data;
    TagInfo info;
411

412
    for(auto url : urls)
413
        if(this->db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url))
414 415
            data << this->getList({url}).first().toMap();
        else
416
        {
417 418 419 420 421 422 423 424 425 426 427 428
            if(info.feed(url))
            {
                auto album = BAE::fixString(info.getAlbum());
                auto track= info.getTrack();
                auto title = BAE::fixString(info.getTitle()); /* to fix*/
                auto artist = BAE::fixString(info.getArtist());
                auto genre = info.getGenre();
                auto sourceUrl = QFileInfo(url).dir().path();
                auto duration = info.getDuration();
                auto year = info.getYear();

                data << QVariantMap({
429 430 431 432 433 434 435 436 437 438
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::TRACK], QString::number(track)},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST], artist},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::ALBUM], album},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::DURATION],QString::number(duration)},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::GENRE], genre},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::SOURCE], sourceUrl},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::FAV],"0"},
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::RELEASEDATE], QString::number(year)}
439 440
                                    });
            }
441 442
        }

443
    qDebug()<< data;
444

445
    emit this->openFiles(data);
446 447
}

448 449 450 451 452 453 454
QString Babe::moodColor(const int &pos)
{
    if(pos < BAE::MoodColors.size())
        return BAE::MoodColors.at(pos);
    else return "";
}

455
QStringList Babe::defaultSources()
456
{
457
    return BAE::defaultSources;
458 459
}

Camilo higuita's avatar
Camilo higuita committed
460
void Babe::loadCover(const QString &url)
461
{
462
    auto map = this->db->getDBData(QStringList() << url);
463

Camilo higuita's avatar
Camilo higuita committed
464
    if(map.isEmpty()) return;
465 466

    auto track = map.first();
467 468 469
    auto artist = track[FMH::MODEL_KEY::ARTIST];
    auto album = track[FMH::MODEL_KEY::ALBUM];
    auto title = track[FMH::MODEL_KEY::TITLE];
470 471 472 473

    auto artistImg = this->artistArt(artist);
    auto albumImg = this->albumArt(album, artist);

474
    if(!albumImg.isEmpty() && albumImg != BAE::SLANG[W::NONE])
Camilo higuita's avatar
Camilo higuita committed
475
        emit this->coverReady(albumImg);
476
    else if (!artistImg.isEmpty() && artistImg != BAE::SLANG[W::NONE])
Camilo higuita's avatar
Camilo higuita committed
477
        emit this->coverReady(artistImg);
478
    else
Camilo higuita's avatar
Camilo higuita committed
479
        this->fetchCoverArt(track);
480 481
}

482

Camilo higuita's avatar
Camilo higuita committed
483
void Babe::fetchCoverArt(FMH::MODEL &song)
484
{
485
//    auto pulpo = new Pulpo;
486

487 488
//    if(BAE::artworkCache(song, FMH::MODEL_KEY::ALBUM)) emit this->coverReady(song[FMH::MODEL_KEY::ARTWORK]);
//    if(BAE::artworkCache(song, FMH::MODEL_KEY::ARTIST)) emit this->coverReady(song[FMH::MODEL_KEY::ARTWORK]);
489

490 491 492
//    pulpo->registerServices({SERVICES::LastFm, SERVICES::Spotify});
//    pulpo->setOntology(PULPO::ONTOLOGY::ALBUM);
//    pulpo->setInfo(PULPO::INFO::ARTWORK);
493

494 495 496 497 498 499 500 501 502 503
//    connect(pulpo, &Pulpo::infoReady, [&](const FMH::MODEL &track,const PULPO::RESPONSE  &res)
//    {
//        Q_UNUSED(track);
//        if(!res[PULPO::ONTOLOGY::ALBUM][PULPO::INFO::ARTWORK].isEmpty())
//        {
//            auto artwork = res[PULPO::ONTOLOGY::ALBUM][PULPO::INFO::ARTWORK][PULPO::CONTEXT::IMAGE].toByteArray();
//            BAE::saveArt(song, artwork, BAE::CachePath);
//            emit this->coverReady(song[FMH::MODEL_KEY::ARTWORK]);
//        }
//    });
504

505
//    pulpo->feed(song, PULPO::RECURSIVE::OFF);
506 507
}

508
QVariantList Babe::transformData(const FMH::MODEL_LIST &dbList)
509 510 511
{
    QVariantList res;

Camilo higuita's avatar
Camilo higuita committed
512 513 514 515 516
    for(FMH::MODEL data : dbList)
    {
        FMH::MODEL copy = data;
        res << FM::toMap(copy);
    }
517 518 519 520

    return res;
}