BabeSettings.cpp 4.44 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
{

29
    this->connection = new CollectionDB(this);
30
    this->brainDeamon = new Brain;
31
    this->ytFetch = new youtubedl(this);
32 33
    this->babeSocket = new Socket(static_cast<quint16>(BAE::BabePort.toInt()), this);

34

Camilo Higuita's avatar
Camilo Higuita committed
35 36 37 38
    qDebug() << "Getting collectionDB info from: " << BAE::CollectionDBPath;
    qDebug() << "Getting settings info from: " << BAE::SettingPath;
    qDebug() << "Getting artwork files from: " << BAE::CachePath;

39
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
40 41
    const auto notifyDir = BAE::NotifyDir;

42
    if(!BAE::fileExists(notifyDir+"/vvave.notifyrc"))
Camilo Higuita's avatar
Camilo Higuita committed
43
    {
44
        bDebug::Instance()->msg("The Knotify file does not exists, going to create it");
45
        QFile knotify(":/assets/vvave.notifyrc");
Camilo Higuita's avatar
Camilo Higuita committed
46

47
        if(knotify.copy(notifyDir+"/vvave.notifyrc"))
48
            bDebug::Instance()->msg("the knotify file got copied");
Camilo Higuita's avatar
Camilo Higuita committed
49
    }
50
#endif    
Camilo Higuita's avatar
Camilo Higuita committed
51

52
    QDir cachePath_dir(BAE::CachePath);
Camilo Higuita's avatar
Camilo Higuita committed
53 54 55 56 57 58 59
    QDir youtubeCache_dir(BAE::YoutubeCachePath);

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

60
    connect(this->ytFetch, &youtubedl::done, [this]()
61
    {
62
        this->startBrainz(true, BAE::SEG::THREE);
63 64
    });

65
    connect(this->babeSocket, &Socket::message, this, &BabeSettings::fetchYoutubeTrack);
66 67 68
    connect(this->babeSocket, &Socket::connected, [this](const int &index)
    {
        auto playlists = this->connection->getPlaylists();
69
        bDebug::Instance()->msg("Sending playlists to socket: "+playlists.join(", "));
70 71
        this->babeSocket->sendMessageTo(index, playlists.join(","));
    });
72 73 74 75 76 77 78 79

    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
80
        emit this->refreshATable(type);
81 82
    });

Camilo Higuita's avatar
Camilo Higuita committed
83
    connect(&this->fileLoader, &FileLoader::finished,[this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
84
    {
85
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
86
        {
87
            bDebug::Instance()->msg("Finished inserting into DB "+QString::number(size)+" tracks");
88
            this->startBrainz(true, BAE::SEG::HALF);
Camilo Higuita's avatar
Camilo Higuita committed
89

90
        }else
Camilo Higuita's avatar
Camilo Higuita committed
91
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), BAE::SEG::TWO);
92 93

        emit refreshTables(size);
Camilo Higuita's avatar
Camilo Higuita committed
94 95
    });

96
    connect(this, &BabeSettings::collectionPathChanged, this, &BabeSettings::populateDB);
Camilo Higuita's avatar
Camilo Higuita committed
97 98
}

99
BabeSettings::~BabeSettings()
Camilo Higuita's avatar
Camilo Higuita committed
100 101
{
    qDebug()<<"DELETING SETTINGS";
102
    delete brainDeamon;
Camilo Higuita's avatar
Camilo Higuita committed
103 104
}

105
void BabeSettings::refreshCollection()
106 107 108 109 110
{    
    if(this->connection->getSourcesFolders().isEmpty())
        this->populateDB(BAE::defaultSources);
    else
        this->populateDB(this->connection->getSourcesFolders());
Camilo Higuita's avatar
Camilo Higuita committed
111 112
}

113 114 115 116 117
void BabeSettings::fetchYoutubeTrack(const QString &message)
{
    this->ytFetch->fetch(message);
}

118
void BabeSettings::checkCollectionBrainz(const bool &state)
119
{
120
    bDebug::Instance()->msg("BRAINZ STATE<<"+state);
121
    this->startBrainz(state, BAE::SEG::THREE);
122 123
}

124
void BabeSettings::startBrainz(const bool &on, const uint &speed)
125
{
126 127
    bDebug::Instance()->msg("Starting Brainz with interval: " + QString::number(BAE::SEG::ONEHALF));

128
    this->brainDeamon->setInterval(speed);
129

130
    if(on)
Camilo Higuita's avatar
Camilo Higuita committed
131
        this->brainDeamon->start();
132 133 134
    else
        this->brainDeamon->pause();

Camilo Higuita's avatar
Camilo Higuita committed
135 136
}

137
void BabeSettings::populateDB(const QStringList &paths)
138
{   
Camilo Higuita's avatar
Camilo Higuita committed
139
    auto newPaths = paths;
140

Camilo Higuita's avatar
Camilo Higuita committed
141 142 143
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
Camilo Higuita's avatar
Camilo Higuita committed
144
    fileLoader.requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
145
}