vvave.cpp 4.45 KB
Newer Older
Camilo higuita's avatar
Camilo higuita committed
1 2 3
#include "vvave.h"

#include "db/collectionDB.h"
4 5 6 7 8 9 10 11 12 13
#include "settings/fileloader.h"
#include "utils/brain.h"

#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
#include "kde/notify.h"
#endif

#include <QtConcurrent>
#include <QFuture>

Camilo higuita's avatar
Camilo higuita committed
14 15 16 17 18 19
#ifdef STATIC_MAUIKIT
#include "fm.h"
#else
#include <MauiKit/fm.h>
#endif

20 21 22 23 24 25
/*
 * Sets upthe app default config paths
 * BrainDeamon to get collection information
 * YoutubeFetcher ?
 *
 * */
Camilo higuita's avatar
Camilo higuita committed
26 27


camilo higuita's avatar
camilo higuita committed
28 29
vvave::vvave(QObject *parent) : QObject(parent),
    db(CollectionDB::getInstance())
Camilo higuita's avatar
Camilo higuita committed
30
{
31 32 33 34 35 36 37
    for(const auto &path : {BAE::CachePath, BAE::YoutubeCachePath})
    {
        QDir dirPath(path);
        if (!dirPath.exists())
            dirPath.mkpath(".");
    }

38 39 40
//#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
//    if(!FMH::fileExists(BAE::NotifyDir+"/vvave.notifyrc"))
//        QFile::copy(":/assets/vvave.notifyrc", BAE::NotifyDir+"/vvave.notifyrc");
41

42
//#endif
43 44 45 46 47 48 49 50 51 52 53 54 55 56
}

vvave::~vvave() {}

void vvave::checkCollection(const QStringList &paths, std::function<void(uint)> cb)
{
    QFutureWatcher<uint> *watcher = new QFutureWatcher<uint>;
    connect(watcher, &QFutureWatcher<uint>::finished, [=]()
    {
        const uint newTracks = watcher->future().result();
        if(cb)
            cb(newTracks);
        watcher->deleteLater();
    });
camilo higuita's avatar
camilo higuita committed
57
    const auto func = [=]() -> uint
58
    {
59
        auto newPaths = paths;
60

61
        for(auto path : newPaths)
62
            if(path.startsWith("file://"))
63
                path.replace("file://", "");
64

65 66
        return FLoader::getTracks(newPaths);
    };
67 68 69 70 71 72

    QFuture<uint> t1 = QtConcurrent::run(func);
    watcher->setFuture(t1);
}

//// PUBLIC SLOTS
Camilo higuita's avatar
Camilo higuita committed
73
QVariantList vvave::sourceFolders()
74 75 76 77 78
{
    const auto sources = this->db->getDBData("select * from sources");

    QVariantList res;
    for(const auto &item : sources)
79
        res << FMH::getDirInfo(QUrl::fromLocalFile(item[FMH::MODEL_KEY::URL]));
80 81
    return res;
}
Camilo higuita's avatar
Camilo higuita committed
82

83 84 85 86 87 88 89 90
bool vvave::removeSource(const QString &source)
{
    if(!this->getSourceFolders().contains(source))
        return false;

    return this->db->removeSource(source);
}

91 92 93 94 95
QString vvave::moodColor(const int &index)
{
    if(index < BAE::MoodColors.size() && index > -1)
        return BAE::MoodColors.at(index);
    else return "";
Camilo higuita's avatar
Camilo higuita committed
96
}
97

Camilo higuita's avatar
Camilo higuita committed
98
void vvave::scanDir(const QStringList &paths)
99
{
100
    this->checkCollection(paths, [=](uint size) {emit this->refreshTables(size);});
101 102 103 104
}

QStringList vvave::getSourceFolders()
{
Camilo higuita's avatar
Camilo higuita committed
105 106 107 108 109 110 111 112 113 114
    return this->db->getSourcesFolders();
}

void vvave::openUrls(const QStringList &urls)
{
    if(urls.isEmpty()) return;

    QVariantList data;
    TagInfo info;

camilo higuita's avatar
camilo higuita committed
115
    for(const auto &url : urls)
116
        if(db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], QUrl(url).toLocalFile()))
Camilo higuita's avatar
Camilo higuita committed
117
        {
118
            data << FM::toMap(this->db->getDBData(QStringList() << QUrl(url).toLocalFile()).first());
camilo higuita's avatar
camilo higuita committed
119
        }else
Camilo higuita's avatar
Camilo higuita committed
120
        {
121
            if(info.feed(QUrl(url).toLocalFile()))
Camilo higuita's avatar
Camilo higuita committed
122
            {
camilo higuita's avatar
camilo higuita committed
123 124 125 126 127
                const auto album = BAE::fixString(info.getAlbum());
                const auto track= info.getTrack();
                const auto title = BAE::fixString(info.getTitle()); /* to fix*/
                const auto artist = BAE::fixString(info.getArtist());
                const auto genre = info.getGenre();
128
                const auto sourceUrl = QFileInfo(QUrl(url).toLocalFile()).dir().path();
camilo higuita's avatar
camilo higuita committed
129 130
                const auto duration = info.getDuration();
                const auto year = info.getYear();
Camilo higuita's avatar
Camilo higuita committed
131 132

                data << QVariantMap({
133
                                        {FMH::MODEL_NAME[FMH::MODEL_KEY::URL], QUrl(url).toLocalFile()},
Camilo higuita's avatar
Camilo higuita committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
                                        {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)}
                                    });
            }
        }

    emit this->openFiles(data);
148 149
}

150 151