BabeSettings.cpp 3.89 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 = new CollectionDB(this);
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 59
    connect(this->babeSocket, &Socket::connected, [this](const int &index)
    {
        auto playlists = this->connection->getPlaylists();
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)
    {
Camilo Higuita's avatar
Camilo Higuita committed
71
        emit this->refreshATable(type);
72 73
    });

Camilo higuita's avatar
Camilo higuita committed
74
    connect(&this->fileLoader, &FileLoader::finished, [this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
75
    {
76 77
        bDebug::Instance()->msg("Finished inserting into DB "+QString::number(size)+" tracks");

78
        if(size > 0)
79
            this->startBrainz(true, BAE::SEG::HALF);
80
        else
Camilo higuita's avatar
Camilo higuita committed
81
            this->startBrainz(BAE::loadSettings("AUTO", "BRAINZ", true).toBool(), BAE::SEG::TWO);
82 83

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

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

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

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

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

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

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

Camilo Higuita's avatar
Camilo Higuita committed
118 119
}

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

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

Camilo Higuita's avatar
Camilo Higuita committed
128
    fileLoader.requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
129
}