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

vvave.cpp 5.68 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
    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::runBrain()
{
    QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
Camilo higuita's avatar
Camilo higuita committed
63

64 65 66 67 68
    QObject::connect(watcher, &QFutureWatcher<void>::finished, [=]()
    {
        watcher->deleteLater();
        emit this->refreshAlbums();
    });
Camilo higuita's avatar
Camilo higuita committed
69

70 71 72 73 74
    //    QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
    //    {
    //        if(watcher != nullptr)
    //            watcher->future().waitForFinished();
    //    });
75

76 77
//    auto func = [=]()
//    {
78
        // the album artworks package
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
//        BRAIN::PACKAGE albumPackage;
//        albumPackage.ontology = PULPO::ONTOLOGY::ALBUM;
//        albumPackage.info = PULPO::INFO::ARTWORK;
//        albumPackage.callback = [=]()
//        {
//            emit this->refreshAlbums();
//        };

//        BRAIN::PACKAGE artistPackage;
//        artistPackage.ontology = PULPO::ONTOLOGY::ARTIST;
//        artistPackage.info = PULPO::INFO::ARTWORK;
//        artistPackage.callback = [=]()
//        {
//            emit this->refreshArtists();
//        };

//        BRAIN::synapse(BRAIN::PACKAGES() << albumPackage /*<< artistPackage*/);
//    };

//    QFuture<void> t1 = QtConcurrent::run(func);
//    watcher->setFuture(t1);
100 101 102 103 104 105 106 107 108 109 110 111 112
}


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
113
    const auto func = [=]() -> uint
114
    {
115
        auto newPaths = paths;
116

117
        for(auto path : newPaths)
118
            if(path.startsWith("file://"))
119
                path.replace("file://", "");
120

121 122
        return FLoader::getTracks(newPaths);
    };
123 124 125 126 127 128 129 130

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


//// PUBLIC SLOTS

Camilo higuita's avatar
fixes  
Camilo higuita committed
131
QVariantList vvave::sourceFolders()
132 133 134 135 136 137 138 139
{
    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
140

141 142 143 144 145
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
146
}
147

Camilo higuita's avatar
fixes  
Camilo higuita committed
148
void vvave::scanDir(const QStringList &paths)
149
{
Camilo higuita's avatar
Camilo higuita committed
150
    this->checkCollection(paths, [=](uint size)
Camilo higuita's avatar
fixes  
Camilo higuita committed
151 152
    {
        emit this->refreshTables(size);
153
//        this->runBrain();
Camilo higuita's avatar
fixes  
Camilo higuita committed
154
    });
155 156 157 158
}

QStringList vvave::getSourceFolders()
{
Camilo higuita's avatar
Camilo higuita committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    return this->db->getSourcesFolders();
}

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

    QVariantList data;
    TagInfo info;

    for(QString url : urls)
        if(db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url))
        {
            const auto value = this->db->getDBData(QStringList() << url).first();
            data << FM::toMap(value);
        }
        else
        {
            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({
                                        {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)}
                                    });
            }
        }

    qDebug()<<"opening urls " << urls << data;

    emit this->openFiles(data);
206 207
}

208 209