Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

babe.cpp 15.5 KB
Newer Older
1 2 3 4 5
#include "babe.h"

#include <QPalette>
#include <QWidget>
#include <QColor>
Camilo Higuita's avatar
Camilo Higuita committed
6
#include <QIcon>
7 8 9
#include "db/collectionDB.h"
#include "settings/settings.h"
#include "pulpo/pulpo.h"
Camilo Higuita's avatar
Camilo Higuita committed
10 11
#include <QApplication>
#include <QDesktopWidget>
12
#include <QDirIterator>
13
#include <QtQml>
14 15 16 17
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
#include "kde/notify.h"
#endif

18 19 20 21
using namespace BAE;

Babe::Babe(QObject *parent) : QObject(parent)
{    
22
    qDebug()<<"CONSTRUCTING ABE INTERFACE";
23 24 25 26 27 28 29 30
    this->con = new CollectionDB(this);

    this->set = new settings(this);

    connect(set, &settings::refreshTables, [this](QVariantMap tables)
    {
        emit this->refreshTables(tables);
    });
Camilo Higuita's avatar
Camilo Higuita committed
31

Camilo Higuita's avatar
Camilo Higuita committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    this->nof = new Notify(this);
    connect(this->nof,&Notify::babeSong,[this](const BAE::DB &track)
    {
        qDebug()<<"BABETRACKKKK";
        Q_UNUSED(track);
        emit this->babeIt();
    });

    connect(this->nof,&Notify::skipSong,[this]()
    {
        emit this->skipTrack();
    });
#endif

47 48
}

49 50
Babe::~Babe()
{
Camilo Higuita's avatar
Camilo Higuita committed
51

52 53
}

54 55
QVariantList Babe::get(const QString &queryTxt)
{
56
    return this->con->getDBDataQML(queryTxt);
57
}
58

59 60 61
QVariantList Babe::getList(const QStringList &urls)
{
    return Babe::transformData(this->con->getDBData(urls));
62 63
}

64
void Babe::trackLyrics(const QString &url)
65 66
{
    auto track = this->con->getDBData(QString("SELECT * FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::TRACKS],
67
                                      KEYMAP[KEY::URL], url));
68

69 70
    if(track.isEmpty()) return;
    this->fetchTrackLyrics(track.first());
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

bool Babe::trackBabe(const QString &path)
{
    auto babe = this->con->getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::BABE],
                                     TABLEMAP[TABLE::TRACKS],
            KEYMAP[KEY::URL],path));

    if(!babe.isEmpty())
        return babe.first()[KEY::BABE].toInt();

    return false;
}

QString Babe::artistArt(const QString &artist)
{
    auto artwork = this->con->getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::ARTWORK],
                                        TABLEMAP[TABLE::ARTISTS],
            KEYMAP[KEY::ARTIST],artist));

    if(!artwork.isEmpty())
        if(!artwork.first()[KEY::ARTWORK].isEmpty() && artwork.first()[KEY::ARTWORK] != SLANG[W::NONE])
            return artwork.first()[KEY::ARTWORK];

    return "";
}

QString Babe::artistWiki(const QString &artist)
{
    auto wiki = this->con->getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::WIKI],
                                     TABLEMAP[TABLE::ARTISTS],
            KEYMAP[KEY::ARTIST],artist));

    if(!wiki.isEmpty())
        return wiki.first()[KEY::WIKI];

    return "";
}

QString Babe::albumArt(const QString &album, const QString &artist)
{
    auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\" AND %5 = \"%6\"").arg(KEYMAP[KEY::ARTWORK],
            TABLEMAP[TABLE::ALBUMS],
            KEYMAP[KEY::ALBUM],album,
            KEYMAP[KEY::ARTIST],artist);
    auto albumCover = this->con->getDBData(queryStr);

    if(!albumCover.isEmpty())
        if(!albumCover.first()[KEY::ARTWORK].isEmpty() && albumCover.first()[KEY::ARTWORK] != SLANG[W::NONE])
            return albumCover.first()[KEY::ARTWORK];

    return "";
}

125
void Babe::fetchTrackLyrics(DB &song)
126 127 128 129 130 131 132 133 134 135 136 137 138
{
    Pulpo pulpo;
    pulpo.registerServices({SERVICES::LyricWikia, SERVICES::Genius});
    pulpo.setOntology(PULPO::ONTOLOGY::TRACK);
    pulpo.setInfo(PULPO::INFO::LYRICS);

    connect(&pulpo, &Pulpo::infoReady, [&](const BAE::DB &track, const PULPO::RESPONSE  &res)
    {
        if(!res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS].isEmpty())
        {
            auto lyrics = res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS][PULPO::CONTEXT::LYRIC].toString();
            this->con->lyricsTrack(track, lyrics);
            song.insert(KEY::LYRICS, lyrics);
139
            emit this->trackLyricsReady(song[KEY::LYRICS], song[KEY::URL]);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        }
    });

    pulpo.feed(song, PULPO::RECURSIVE::OFF);


}

QString Babe::albumWiki(const QString &album, const QString &artist)
{
    auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\" AND %5 = \"%6\"").arg(KEYMAP[KEY::WIKI],
            TABLEMAP[TABLE::ALBUMS],
            KEYMAP[KEY::ALBUM],album,
            KEYMAP[KEY::ARTIST],artist);
    auto wiki = this->con->getDBData(queryStr);

    if(!wiki.isEmpty())
        return wiki.first()[KEY::WIKI];

    return "";
}

bool Babe::babeTrack(const QString &path, const bool &value)
{
    if(this->con->update(TABLEMAP[TABLE::TRACKS],
                         KEYMAP[KEY::BABE],
                         value ? 1 : 0,
                         KEYMAP[KEY::URL],
                         path)) return true;

    return false;
}

173 174 175 176 177 178 179
bool Babe::rateTrack(const QString &path, const int &value)
{
    return this->con->rateTrack(path, value);
}

int Babe::trackRate(const QString &path)
{
Camilo Higuita's avatar
Camilo Higuita committed
180
    return this->con->getTrackStars(path);
181 182
}

183 184 185 186 187 188
bool Babe::moodTrack(const QString &path, const QString &color)
{
    qDebug()<<path<<color;
    return this->con->artTrack(path, color);
}

189 190 191 192 193 194 195 196 197 198 199 200
void Babe::notify(const QString &title, const QString &body)
{

#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    Babe::nof->notify(title, body);
#else
    Q_UNUSED(title);
    Q_UNUSED(body);
#endif

}

Camilo Higuita's avatar
Camilo Higuita committed
201 202 203 204 205 206 207
void Babe::notifySong(const QString &url)
{
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    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);
    auto track = this->con->getDBData(query);
    Babe::nof->notifySong(track.first());
#else
Camilo Higuita's avatar
Camilo Higuita committed
208
    Q_UNUSED(url);
Camilo Higuita's avatar
Camilo Higuita committed
209 210 211
#endif
}

212 213
void Babe::scanDir(const QString &url)
{
Camilo Higuita's avatar
Camilo Higuita committed
214
    emit this->set->collectionPathChanged({url});
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
void Babe::brainz(const bool &on)
{
    this->set->checkCollectionBrainz(on);
}

QVariant Babe::loadSetting(const QString &key, const QString &group, const QVariant &defaultValue)
{
    auto res = BAE::loadSettings(key, group, defaultValue);
    qDebug()<<res<<"LOADSET RES";
    return res;
}

void Babe::saveSetting(const QString &key, const QVariant &value, const QString &group)
{
    qDebug()<<key<<value<<group;
    BAE::saveSettings(key, value, group);
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
void Babe::savePlaylist(const QStringList &list)
{
    BAE::saveSettings("PLAYLIST", list, "MAINWINDOW");
}

QStringList Babe::lastPlaylist()
{
    return BAE::loadSettings("PLAYLIST","MAINWINDOW",{}).toStringList();
}

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

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

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
QString Babe::baseColor()
{
#if defined(Q_OS_ANDROID)
    return "#24282c";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Base).name();
#elif defined(Q_OS_WIN32)
    return "#24282c";
#endif
}

QString Babe::darkColor()
{
#if defined(Q_OS_ANDROID)
    return "#24282c";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Dark).name();
#elif defined(Q_OS_WIN32)
    return "#24282c";
#endif
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292
QString Babe::backgroundColor()
{
#if defined(Q_OS_ANDROID)
    return "#31363b";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Background).name();
#elif defined(Q_OS_WIN32)
    return "#31363b";
#endif
}

QString Babe::foregroundColor()
{
293 294 295 296 297 298 299 300 301 302 303 304
#if defined(Q_OS_ANDROID)
    return "#FFF";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Foreground).name();
#elif defined(Q_OS_WIN32)
    return "#FFF";
#endif
}

QString Babe::textColor()
{
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
#if defined(Q_OS_ANDROID)
    return "#FFF";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Text).name();
#elif defined(Q_OS_WIN32)
    return "#FFF";
#endif
}

QString Babe::hightlightColor()
{
#if defined(Q_OS_ANDROID)
    return "";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Highlight).name();
#elif defined(Q_OS_WIN32)
    return "";
#endif
}

QString Babe::midColor()
{
329 330 331 332 333 334 335 336 337 338 339 340
#if defined(Q_OS_ANDROID)
    return "#3e444b";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Mid).name();
#elif defined(Q_OS_WIN32)
    return "#3e444b";
#endif
}

QString Babe::midLightColor()
{
341
#if defined(Q_OS_ANDROID)
342
    return "#3e444b";
343 344 345 346
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Midlight).name();
#elif defined(Q_OS_WIN32)
347
    return "#3e444b";
348 349 350
#endif
}

351 352 353 354 355 356 357 358 359 360 361 362
QString Babe::shadowColor()
{
#if defined(Q_OS_ANDROID)
    return "#3e444b";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Shadow).name();
#elif defined(Q_OS_WIN32)
    return "#3e444b";
#endif
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
QString Babe::altColor()
{
#if defined(Q_OS_ANDROID)
    return "#232629";
#elif defined(Q_OS_LINUX)
    QWidget widget;
    return widget.palette().color(QPalette::Base).name();
#elif defined(Q_OS_WIN32)
    return "#232629";
#endif
}

QString Babe::babeColor()
{
    return "#E91E63";
}

380 381
bool Babe::isMobile()
{
Camilo Higuita's avatar
Camilo Higuita committed
382
    return BAE::isMobile();
383 384
}

Camilo Higuita's avatar
Camilo Higuita committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
int Babe::screenGeometry(QString &side)
{
    side = side.toLower();
    auto geo = QApplication::desktop()->screenGeometry();

    if(side == "width")
        return geo.width();
    else if(side == "height")
        return geo.height();
    else return 0;
}

int Babe::cursorPos(QString &axis)
{
    axis = axis.toLower();
    auto pos = QCursor::pos();
    if(axis == "x")
        return pos.x();
    else if(axis == "y")
        return pos.y();
    else return 0;
}

408 409 410 411 412 413 414
QString Babe::moodColor(const int &pos)
{
    if(pos < BAE::MoodColors.size())
        return BAE::MoodColors.at(pos);
    else return "";
}

415 416 417 418 419
QString Babe::homeDir()
{
    return BAE::MusicPath;
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
QVariantList Babe::getDirs(const QString &pathUrl)
{
    auto path = pathUrl;
    if(path.startsWith("file://"))
        path.replace("file://", "");
    qDebug()<<"DIRECTRORY"<<path;
    QVariantList paths;

    if (QFileInfo(path).isDir())
    {
        QDirIterator it(path, QDir::Dirs, QDirIterator::NoIteratorFlags);
        while (it.hasNext())
        {
            auto url = it.next();
            auto name = QDir(url).dirName();
            qDebug()<<name<<url;
            QVariantMap map = { {"url", url }, {"name", name} };
            paths << map;
        }
    }

    return paths;
}

QVariantMap Babe::getParentDir(const QString &path)
{
446 447 448
    auto dir = QDir(path);
    dir.cdUp();
    auto dirPath = dir.absolutePath();
449

450 451
    if(dir.isReadable() && !dir.isRoot() && dir.exists())
        return {{"url", dirPath}, {"name", dir.dirName()}};
452
    else
453
        return {{"url", path}, {"name", QFileInfo(path).dir().dirName()}};
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

void Babe::registerTypes()
{
    qmlRegisterUncreatableType<Babe>("Babe", 1, 0, "Babe", "ERROR ABE");
}


uint Babe::sizeHint(const uint &hint)
{
    if(hint>=BAE::BIG_ALBUM_FACTOR)
        return BAE::getWidgetSizeHint(BAE::AlbumSizeHint::BIG_ALBUM);
    else if(hint>=BAE::MEDIUM_ALBUM_FACTOR)
        return BAE::getWidgetSizeHint(BAE::AlbumSizeHint::MEDIUM_ALBUM);
    else if(hint>=BAE::SMALL_ALBUM_FACTOR)
        return BAE::getWidgetSizeHint(BAE::AlbumSizeHint::SMALL_ALBUM);
    else return hint;
471 472
}

Camilo Higuita's avatar
Camilo Higuita committed
473 474 475 476 477 478 479
QString Babe::icon(const QString &icon, const int &size)
{
   auto pix = QIcon::fromTheme(icon).pixmap(QSize(size, size), QIcon::Mode::Normal, QIcon::State::On);

   return "";
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493
QString Babe::loadCover(const QString &url)
{
    auto map = this->con->getDBData(QStringList() << url);

    if(map.isEmpty()) return "";

    auto track = map.first();
    auto artist = track[KEY::ARTIST];
    auto album = track[KEY::ALBUM];
    auto title = track[KEY::TITLE];

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

494
    if(!albumImg.isEmpty() && albumImg != BAE::SLANG[W::NONE])
495
        return albumImg;
496
    else if (!artistImg.isEmpty() && artistImg != BAE::SLANG[W::NONE])
497 498 499 500 501
        return artistImg;
    else
        return this->fetchCoverArt(track);
}

Camilo Higuita's avatar
Camilo Higuita committed
502 503
QVariantList Babe::searchFor(const QStringList &queries)
{
504 505
    QVariantList mapList;
    bool hasKey = false;
Camilo Higuita's avatar
Camilo Higuita committed
506 507 508 509 510 511 512 513 514 515 516

    for(auto searchQuery : queries)
    {
        if(searchQuery.contains(BAE::SearchTMap[BAE::SearchT::LIKE]+":") || searchQuery.startsWith("#"))
        {
            if(searchQuery.startsWith("#"))
                searchQuery=searchQuery.replace("#","").trimmed();
            else
                searchQuery=searchQuery.replace(BAE::SearchTMap[BAE::SearchT::LIKE]+":","").trimmed();


517
            searchQuery = searchQuery.trimmed();
Camilo Higuita's avatar
Camilo Higuita committed
518 519 520 521 522 523 524 525 526 527 528 529
            if(!searchQuery.isEmpty())
            {
                mapList += this->con->getSearchedTracks(BAE::KEY::WIKI, searchQuery);
                mapList += this->con->getSearchedTracks(BAE::KEY::TAG, searchQuery);
                mapList += this->con->getSearchedTracks(BAE::KEY::LYRICS, searchQuery);
            }

        }else if(searchQuery.contains((BAE::SearchTMap[BAE::SearchT::SIMILAR]+":")))
        {
            searchQuery=searchQuery.replace(BAE::SearchTMap[BAE::SearchT::SIMILAR]+":","").trimmed();
            searchQuery=searchQuery.trimmed();
            if(!searchQuery.isEmpty())
530
                mapList += this->con->getSearchedTracks(BAE::KEY::TAG, searchQuery);
Camilo Higuita's avatar
Camilo Higuita committed
531 532 533 534 535 536 537 538 539 540 541 542 543

        }else
        {
            BAE::KEY key;

            QMapIterator<BAE::KEY, QString> k(BAE::KEYMAP);
            while (k.hasNext())
            {
                k.next();
                if(searchQuery.contains(QString(k.value()+":")))
                {
                    hasKey=true;
                    key=k.key();
544
                    searchQuery = searchQuery.replace(k.value()+":","").trimmed();
Camilo Higuita's avatar
Camilo Higuita committed
545 546 547 548 549 550 551 552 553 554 555 556 557
                }
            }

            searchQuery = searchQuery.trimmed();
            qDebug()<<"Searching for: "<<searchQuery;

            if(!searchQuery.isEmpty())
            {
                if(hasKey)
                    mapList += this->con->getSearchedTracks(key, searchQuery);
                else
                {
                    auto queryTxt = QString("SELECT * FROM tracks WHERE title LIKE \"%"+searchQuery+"%\" OR artist LIKE \"%"+searchQuery+"%\" OR album LIKE \"%"+searchQuery+"%\"OR genre LIKE \"%"+searchQuery+"%\"OR url LIKE \"%"+searchQuery+"%\" LIMIT 1000");
558
                    mapList += this->con->getDBDataQML(queryTxt);
Camilo Higuita's avatar
Camilo Higuita committed
559 560 561 562 563
                }
            }
        }
    }

564
    return  mapList;
Camilo Higuita's avatar
Camilo Higuita committed
565 566
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
QString Babe::fetchCoverArt(DB &song)
{
    if(BAE::artworkCache(song, KEY::ALBUM)) return song[KEY::ARTWORK];
    if(BAE::artworkCache(song, KEY::ARTIST)) return song[KEY::ARTWORK];

    Pulpo pulpo;
    pulpo.registerServices({SERVICES::LastFm, SERVICES::Spotify});
    pulpo.setOntology(PULPO::ONTOLOGY::ALBUM);
    pulpo.setInfo(PULPO::INFO::ARTWORK);

    QEventLoop loop;

    QTimer timer;
    timer.setSingleShot(true);
    timer.setInterval(1000);

    connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);

    connect(&pulpo, &Pulpo::infoReady, [&](const BAE::DB &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);
        }
        loop.quit();
    });

    pulpo.feed(song, PULPO::RECURSIVE::OFF);

    timer.start();
    loop.exec();
    timer.stop();

    return  song[KEY::ARTWORK];
}

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
QVariantList Babe::transformData(const DB_LIST &dbList)
{
    QVariantList res;

    for(auto data : dbList)
    {
        QVariantMap map;
        for(auto key : data.keys())
            map[BAE::KEYMAP[key]] = data[key];

        res << map;
    }

    return res;
}