BabeSettings.cpp 4.43 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
            bDebug::Instance()->msg("Finished inserting into DB "+QString::number(size)+" tracks");
87
            this->startBrainz(true, BAE::SEG::HALF);
Camilo Higuita's avatar
Camilo Higuita committed
88

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

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

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

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

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

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

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

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

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

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

Camilo Higuita's avatar
Camilo Higuita committed
134 135
}

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

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