BabeSettings.cpp 4.54 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 21
#include "../db/collectionDB.h"
#include "fileloader.h"
22 23 24
#include "../utils/brain.h"
#include "../services/local/socket.h"
#include "../services/web/youtube.h"
25
#include "../utils/babeconsole.h"
Camilo Higuita's avatar
Camilo Higuita committed
26

27
BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
Camilo Higuita's avatar
Camilo Higuita committed
28 29 30 31
{

    this->connection = new CollectionDB(this);
    this->fileLoader = new FileLoader;
32
    this->brainDeamon = new Brain;
33 34 35
    this->ytFetch = new YouTube(this);
    this->babeSocket = new Socket(static_cast<quint16>(BAE::BabePort.toInt()),this);

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

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

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

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

    QDir collectionDBPath_dir(BAE::CollectionDBPath);
54
    QDir cachePath_dir(BAE::CachePath);
Camilo Higuita's avatar
Camilo Higuita committed
55 56 57 58 59 60 61 62 63
    QDir youtubeCache_dir(BAE::YoutubeCachePath);

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

64 65
    connect(this->ytFetch, &YouTube::done, [this]()
    {
66
        this->startBrainz(true, BAE::SEG::THREE);
67 68 69 70 71 72
    });

    connect(this->babeSocket, &Socket::message, this->ytFetch, &YouTube::fetch);
    connect(this->babeSocket, &Socket::connected, [this](const int &index)
    {
        auto playlists = this->connection->getPlaylists();
73
        bDebug::Instance()->msg("Sending playlists to socket: "+playlists.join(", "));
74 75
        this->babeSocket->sendMessageTo(index, playlists.join(","));
    });
76 77 78 79 80 81 82 83

    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
84
        emit this->refreshATable(type);
85 86
    });

Camilo Higuita's avatar
Camilo Higuita committed
87
    connect(this->fileLoader, &FileLoader::finished,[this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
88
    {
89
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
90
        {
91 92
            bDebug::Instance()->msg("Finished inserting into DB "+QString::number(size)+" tracks");
            bDebug::Instance()->msg("Starting Brainz with interval: " + QString::number(BAE::SEG::ONEHALF));
Camilo Higuita's avatar
Camilo Higuita committed
93 94
            this->startBrainz(true, BAE::SEG::ONEHALF);

95 96
        }else
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), BAE::SEG::THREE);
97 98

        emit refreshTables(size);
Camilo Higuita's avatar
Camilo Higuita committed
99 100
    });

101
    connect(this, &BabeSettings::collectionPathChanged, this, &BabeSettings::populateDB);
Camilo Higuita's avatar
Camilo Higuita committed
102 103
}

104
BabeSettings::~BabeSettings()
Camilo Higuita's avatar
Camilo Higuita committed
105 106 107
{
    qDebug()<<"DELETING SETTINGS";
    delete fileLoader;
108
    delete brainDeamon;
Camilo Higuita's avatar
Camilo Higuita committed
109 110
}

111
void BabeSettings::refreshCollection()
112 113 114 115 116
{    
    if(this->connection->getSourcesFolders().isEmpty())
        this->populateDB(BAE::defaultSources);
    else
        this->populateDB(this->connection->getSourcesFolders());
Camilo Higuita's avatar
Camilo Higuita committed
117 118
}

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

125
void BabeSettings::startBrainz(const bool &on, const uint &speed)
126
{
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 143
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
    fileLoader->requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
144
}