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
{

Camilo Higuita's avatar
Camilo Higuita committed
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);

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

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

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

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

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

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

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

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

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

Camilo Higuita's avatar
Camilo Higuita committed
82
    connect(&this->fileLoader, &FileLoader::finished,[this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
83
    {
84
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
85
        {
86 87
            bDebug::Instance()->msg("Finished inserting into DB "+QString::number(size)+" tracks");
            bDebug::Instance()->msg("Starting Brainz with interval: " + QString::number(BAE::SEG::ONEHALF));
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
    this->brainDeamon->setInterval(speed);
127

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

Camilo Higuita's avatar
Camilo Higuita committed
133 134
}

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

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