BabeSettings.cpp 3.87 KB
Newer Older
Camilo Higuita's avatar
Camilo Higuita committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
   Babe - tiny music player
   Copyright (C) 2017  Camilo Higuita
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

   */


19
#include "BabeSettings.h"
Camilo Higuita's avatar
Camilo Higuita committed
20
#include "../db/collectionDB.h"
21 22
#include "../utils/brain.h"
#include "../services/local/socket.h"
23
#include "../services/local/youtubedl.h"
24
#include "../utils/babeconsole.h"
Camilo Higuita's avatar
Camilo Higuita committed
25

26
BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
Camilo Higuita's avatar
Camilo Higuita committed
27
{
28
    this->connection = CollectionDB::getInstance();
29
    this->brainDeamon = new Brain;
30
    this->ytFetch = new youtubedl(this);
31 32
    this->babeSocket = new Socket(static_cast<quint16>(BAE::BabePort.toInt()), this);

33
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
Camilo higuita's avatar
Camilo higuita committed
34
    if(!BAE::fileExists(BAE::NotifyDir+"/vvave.notifyrc"))
Camilo Higuita's avatar
Camilo Higuita committed
35
    {
36
        QFile knotify(":/assets/vvave.notifyrc");
Camilo Higuita's avatar
Camilo Higuita committed
37

Camilo higuita's avatar
Camilo higuita committed
38
        if(knotify.copy(BAE::NotifyDir+"/vvave.notifyrc"))
39
            bDebug::Instance()->msg("the knotify file got copied");
Camilo Higuita's avatar
Camilo Higuita committed
40
    }
41
#endif    
Camilo Higuita's avatar
Camilo Higuita committed
42

43
    QDir cachePath_dir(BAE::CachePath);
Camilo Higuita's avatar
Camilo Higuita committed
44 45 46 47 48 49 50
    QDir youtubeCache_dir(BAE::YoutubeCachePath);

    if (!cachePath_dir.exists())
        cachePath_dir.mkpath(".");
    if (!youtubeCache_dir.exists())
        youtubeCache_dir.mkpath(".");

51
    connect(this->ytFetch, &youtubedl::done, [this]()
52
    {
53
        this->startBrainz(true, BAE::SEG::THREE);
54 55
    });

56
    connect(this->babeSocket, &Socket::message, this, &BabeSettings::fetchYoutubeTrack);
57 58
    connect(this->babeSocket, &Socket::connected, [this](const int &index)
    {
59
        auto playlists = this->connection->getPlaylistsList();
60
        bDebug::Instance()->msg("Sending playlists to socket: "+playlists.join(", "));
61 62
        this->babeSocket->sendMessageTo(index, playlists.join(","));
    });
63 64 65 66 67 68 69 70

    connect(this->brainDeamon, &Brain::finished, [this]()
    {
        emit this->brainFinished();
    });

    connect(this->brainDeamon, &Brain::done, [this](const TABLE type)
    {
71
        qDebug()<< "FINISHED WITH" << TABLEMAP[type];
Camilo Higuita's avatar
Camilo Higuita committed
72
        emit this->refreshATable(type);
73 74
    });

Camilo higuita's avatar
Camilo higuita committed
75
    connect(&this->fileLoader, &FileLoader::finished, [this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
76
    {
Camilo higuita's avatar
Camilo higuita committed
77
//        if(size > 0)
78
            this->startBrainz(true, BAE::SEG::HALF);
79 80
//        else
//            this->startBrainz(BAE::loadSettings("AUTO", "BRAINZ", true).toBool(), BAE::SEG::TWO);
81 82

        emit refreshTables(size);
Camilo Higuita's avatar
Camilo Higuita committed
83 84
    });

85
    connect(this, &BabeSettings::collectionPathChanged, this, &BabeSettings::populateDB);
Camilo Higuita's avatar
Camilo Higuita committed
86 87
}

88
BabeSettings::~BabeSettings()
Camilo Higuita's avatar
Camilo Higuita committed
89 90
{
    qDebug()<<"DELETING SETTINGS";
91
    delete brainDeamon;
Camilo Higuita's avatar
Camilo Higuita committed
92 93
}

94
void BabeSettings::refreshCollection()
95 96 97 98 99
{    
    if(this->connection->getSourcesFolders().isEmpty())
        this->populateDB(BAE::defaultSources);
    else
        this->populateDB(this->connection->getSourcesFolders());
Camilo Higuita's avatar
Camilo Higuita committed
100 101
}

102 103 104 105 106
void BabeSettings::fetchYoutubeTrack(const QString &message)
{
    this->ytFetch->fetch(message);
}

107
void BabeSettings::startBrainz(const bool &on, const uint &speed)
108
{
Camilo higuita's avatar
Camilo higuita committed
109
    qDebug()<< "Starting Brainz with interval: "<< speed << on;
110
    this->brainDeamon->setInterval(speed);
111

112
    if(on)
Camilo Higuita's avatar
Camilo Higuita committed
113
        this->brainDeamon->start();
114 115 116
    else
        this->brainDeamon->pause();

Camilo Higuita's avatar
Camilo Higuita committed
117 118
}

119
void BabeSettings::populateDB(const QStringList &paths)
120
{   
Camilo Higuita's avatar
Camilo Higuita committed
121
    auto newPaths = paths;
122

Camilo Higuita's avatar
Camilo Higuita committed
123 124 125
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
126

Camilo higuita's avatar
Camilo higuita committed
127
    this->fileLoader.requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
128
}