vvave.cpp 4.58 KB
Newer Older
Camilo higuita's avatar
fixes  
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
fixes  
Camilo higuita committed
26 27 28 29


vvave::vvave(QObject *parent) : QObject(parent)
{
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    this->db = CollectionDB::getInstance();
    for(const auto &path : {BAE::CachePath, BAE::YoutubeCachePath})
    {
        QDir dirPath(path);
        if (!dirPath.exists())
            dirPath.mkpath(".");
    }

#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");

    this->notify = new Notify(this);
    connect(this->notify, &Notify::babeSong, [this]()
    {
        //        emit this->babeIt();
    });

    connect(this->notify, &Notify::skipSong, [this]()
    {
        //        emit this->skipTrack();
    });

#endif
}

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
polish  
camilo higuita committed
68
    const auto func = [=]() -> uint
69
    {
70
        auto newPaths = paths;
71

72
        for(auto path : newPaths)
73
            if(path.startsWith("file://"))
74
                path.replace("file://", "");
75

76 77
        return FLoader::getTracks(newPaths);
    };
78 79 80 81 82 83

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

//// PUBLIC SLOTS
Camilo higuita's avatar
fixes  
Camilo higuita committed
84
QVariantList vvave::sourceFolders()
85 86 87 88 89 90 91 92
{
    const auto sources = this->db->getDBData("select * from sources");

    QVariantList res;
    for(const auto &item : sources)
        res << FMH::getDirInfo(item[FMH::MODEL_KEY::URL]);
    return res;
}
Camilo higuita's avatar
fixes  
Camilo higuita committed
93

94 95 96 97 98 99 100 101
bool vvave::removeSource(const QString &source)
{
    if(!this->getSourceFolders().contains(source))
        return false;

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

102 103 104 105 106
QString vvave::moodColor(const int &index)
{
    if(index < BAE::MoodColors.size() && index > -1)
        return BAE::MoodColors.at(index);
    else return "";
Camilo higuita's avatar
fixes  
Camilo higuita committed
107
}
108

Camilo higuita's avatar
fixes  
Camilo higuita committed
109
void vvave::scanDir(const QStringList &paths)
110
{
111
    this->checkCollection(paths, [=](uint size) {emit this->refreshTables(size);});
112 113 114 115
}

QStringList vvave::getSourceFolders()
{
Camilo higuita's avatar
Camilo higuita committed
116 117 118 119 120 121 122 123 124 125
    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
126
    for(const auto &url : urls)
Camilo higuita's avatar
Camilo higuita committed
127 128
        if(db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url))
        {
camilo higuita's avatar
fixes  
camilo higuita committed
129
            data << FM::toMap(this->db->getDBData(QStringList() << url).first());
camilo higuita's avatar
camilo higuita committed
130
        }else
Camilo higuita's avatar
Camilo higuita committed
131 132 133
        {
            if(info.feed(url))
            {
camilo higuita's avatar
camilo higuita committed
134 135 136 137 138 139 140 141
                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();
                const auto sourceUrl = QFileInfo(url).dir().path();
                const auto duration = info.getDuration();
                const auto year = info.getYear();
Camilo higuita's avatar
Camilo higuita committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

                data << QVariantMap({
                                        {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)}
                                    });
            }
        }

    emit this->openFiles(data);
159 160
}

161 162