BabeSettings.cpp 7.87 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 40

    qDebug() << "Getting collectionDB info from: " << BAE::CollectionDBPath;
    qDebug() << "Getting settings info from: " << BAE::SettingPath;
    qDebug() << "Getting artwork files from: " << BAE::CachePath;

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

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

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

    QDir collectionDBPath_dir(BAE::CollectionDBPath);
55
    QDir cachePath_dir(BAE::CachePath);
Camilo Higuita's avatar
Camilo Higuita committed
56 57 58 59 60 61 62 63 64
    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(".");

Camilo Higuita's avatar
Camilo Higuita committed
65
    //    if(!connection->check_existance(TABLEMAP[TABLE::SOURCES], KEYMAP[KEY::URL], BAE::MusicPath))
Camilo Higuita's avatar
Camilo Higuita committed
66 67

    if(BAE::isMobile())
68
        this->populateDB(QStringList()<<BAE::MusicPath<<BAE::DownloadsPath<<BAE::MusicPaths<<BAE::DownloadsPaths);
69
    else
70 71 72 73 74 75
        this->populateDB({BAE::MusicPath, BAE::YoutubeCachePath});
    //        checkCollectionBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool());


    connect(this->ytFetch, &YouTube::done, [this]()
    {
76
        this->startBrainz(true, BAE::SEG::THREE);
77 78 79 80 81 82
    });

    connect(this->babeSocket, &Socket::message, this->ytFetch, &YouTube::fetch);
    connect(this->babeSocket, &Socket::connected, [this](const int &index)
    {
        auto playlists = this->connection->getPlaylists();
83
        bDebug::Instance()->msg("Sending playlists to socket: "+playlists.join(", "));
84 85
        this->babeSocket->sendMessageTo(index, playlists.join(","));
    });
86 87 88 89 90 91 92 93 94 95 96

    connect(this->brainDeamon, &Brain::finished, [this]()
    {
        emit this->brainFinished();
    });

    connect(this->brainDeamon, &Brain::done, [this](const TABLE type)
    {
        emit this->refreshTables({{BAE::TABLEMAP[type], true}});
    });

Camilo Higuita's avatar
Camilo Higuita committed
97 98 99 100 101 102
    //    connect(this->fileLoader, &FileLoader::trackReady, [this]()
    //    {
    //        this->ui->progressBar->setValue(this->ui->progressBar->value()+1);
    //    });

    connect(this->fileLoader, &FileLoader::finished,[this](int size)
Camilo Higuita's avatar
Camilo Higuita committed
103
    {
104
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
105
        {
106 107 108 109 110
            this->collectionWatcher();
            emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true},
                                {BAE::TABLEMAP[TABLE::ALBUMS], true},
                                {BAE::TABLEMAP[TABLE::ARTISTS], true},
                                {BAE::TABLEMAP[TABLE::PLAYLISTS], true}});
111

Camilo Higuita's avatar
Camilo Higuita committed
112

113
            //            this->startBrainz(true, BAE::SEG::ONEHALF);
Camilo Higuita's avatar
Camilo Higuita committed
114

115
            bDebug::Instance()->msg("Finished inserting into DB");
116
        }else
Camilo Higuita's avatar
Camilo Higuita committed
117
        {
Camilo Higuita's avatar
Camilo Higuita committed
118 119 120
            this->dirs.clear();
            this->collectionWatcher();
            this->watcher->removePaths(watcher->directories());
121 122
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), BAE::SEG::THREE);
        }
Camilo Higuita's avatar
Camilo Higuita committed
123 124
    });

125
    connect(this, &BabeSettings::collectionPathChanged, this, &BabeSettings::populateDB);
Camilo Higuita's avatar
Camilo Higuita committed
126 127

    this->watcher = new QFileSystemWatcher(this);
128
    connect(this->watcher, &QFileSystemWatcher::directoryChanged, this, &BabeSettings::handleDirectoryChanged);
Camilo Higuita's avatar
Camilo Higuita committed
129 130
}

131
BabeSettings::~BabeSettings()
Camilo Higuita's avatar
Camilo Higuita committed
132 133 134
{
    qDebug()<<"DELETING SETTINGS";
    delete fileLoader;
135
    delete brainDeamon;
Camilo Higuita's avatar
Camilo Higuita committed
136 137
}

138
void BabeSettings::on_remove_clicked()
Camilo Higuita's avatar
Camilo Higuita committed
139 140 141 142 143 144 145 146 147 148
{
    qDebug() << this->pathToRemove;
    if (!this->pathToRemove.isEmpty())
    {
        if(this->connection->removeSource(this->pathToRemove))
        {
            this->refreshCollectionPaths();
            this->dirs.clear();
            this->collectionWatcher();
            this->watcher->removePaths(watcher->directories());
149
            emit refreshTables({{TABLEMAP[TABLE::TRACKS], true},
Camilo Higuita's avatar
Camilo Higuita committed
150
                                {TABLEMAP[TABLE::PLAYLISTS], true}});
Camilo Higuita's avatar
Camilo Higuita committed
151 152 153 154
        }
    }
}

155
void BabeSettings::refreshCollectionPaths()
Camilo Higuita's avatar
Camilo Higuita committed
156
{
157
    //    auto queryTxt = QString("SELECT %1 FROM %2").arg(BAE::KEYMAP[BAE::KEY::URL], BAE::TABLEMAP[BAE::TABLE::SOURCES]);
Camilo Higuita's avatar
Camilo Higuita committed
158

159 160 161
    //    for (auto track : this->connection->getDBData(queryTxt))
    //    {
    //    }
Camilo Higuita's avatar
Camilo Higuita committed
162 163
}

164
void BabeSettings::addToWatcher(QStringList paths)
Camilo Higuita's avatar
Camilo Higuita committed
165
{
166
    bDebug::Instance()->msg("Removed duplicated paths in watcher: "+paths.removeDuplicates());
Camilo Higuita's avatar
Camilo Higuita committed
167 168 169 170

    if(!paths.isEmpty()) watcher->addPaths(paths);
}

171
void BabeSettings::collectionWatcher()
Camilo Higuita's avatar
Camilo Higuita committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
    auto queryTxt = QString("SELECT %1 FROM %2").arg(BAE::KEYMAP[BAE::KEY::URL], BAE::TABLEMAP[BAE::TABLE::TRACKS]);

    for (auto track : this->connection->getDBData(queryTxt))
    {
        auto location = track[BAE::KEY::URL];
        if(!location.startsWith(BAE::YoutubeCachePath,Qt::CaseInsensitive)) //exclude the youtube cache folder
        {
            if (!this->dirs.contains(QFileInfo(location).dir().path()) && BAE::fileExists(location)) //check if parent dir isn't already in list and it exists
            {
                QString dir = QFileInfo(location).dir().path();
                this->dirs << dir;

                QDirIterator it(dir, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); // get all the subdirectories to watch
                while (it.hasNext())
                {
                    QString subDir = QFileInfo(it.next()).path();

                    if(QFileInfo(subDir).isDir() && !this->dirs.contains(subDir) && BAE::fileExists(subDir))
                        this->dirs <<subDir;
                }

            }
        }
    }
    this->addToWatcher(this->dirs);
}

200
void BabeSettings::handleDirectoryChanged(const QString &dir)
Camilo Higuita's avatar
Camilo Higuita committed
201
{
202
    bDebug::Instance()->msg("directory changed:"+dir);
Camilo Higuita's avatar
Camilo Higuita committed
203 204 205

    auto wait = new QTimer(this);
    wait->setSingleShot(true);
Camilo Higuita's avatar
fixes  
Camilo Higuita committed
206
    wait->setInterval(1500);
Camilo Higuita's avatar
Camilo Higuita committed
207 208 209

    connect(wait, &QTimer::timeout,[=]()
    {
Camilo Higuita's avatar
Camilo Higuita committed
210
        emit collectionPathChanged({dir});
Camilo Higuita's avatar
Camilo Higuita committed
211 212 213 214 215 216 217
        wait->deleteLater();
    });

    wait->start();

}

218
void BabeSettings::checkCollectionBrainz(const bool &state)
219
{
220
    bDebug::Instance()->msg("BRAINZ STATE<<"+state);
221
    this->startBrainz(state, BAE::SEG::THREE);
222 223
}

224
void BabeSettings::startBrainz(const bool &on, const uint &speed)
225
{
226
    this->brainDeamon->setInterval(speed);
227

228
    if(on)
Camilo Higuita's avatar
Camilo Higuita committed
229
        this->brainDeamon->start();
230 231 232
    else
        this->brainDeamon->pause();

Camilo Higuita's avatar
Camilo Higuita committed
233 234
}

235
void BabeSettings::populateDB(const QStringList &paths)
236
{   
Camilo Higuita's avatar
Camilo Higuita committed
237
    auto newPaths = paths;
238

Camilo Higuita's avatar
Camilo Higuita committed
239 240 241 242
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
    fileLoader->requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
243
}