BabeSettings.cpp 7.67 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"
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 30
{

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

Camilo Higuita's avatar
Camilo Higuita committed
35 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 41 42
#if defined(Q_OS_LINUX)
    const auto notifyDir = BAE::NotifyDir;

Camilo Higuita's avatar
Camilo Higuita committed
43 44 45
    if(!BAE::fileExists(notifyDir+"/Babe.notifyrc"))
    {
        qDebug()<<"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 49 50

        if(knotify.copy(notifyDir+"/Babe.notifyrc"))
            qDebug()<<"the knotify file got copied";
    }
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(".");

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

    if(BAE::isMobile())
67
        this->populateDB(QStringList()<<BAE::MusicPath<<BAE::DownloadsPath<<BAE::MusicPaths<<BAE::DownloadsPaths);
68
    else
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        this->populateDB({BAE::MusicPath, BAE::YoutubeCachePath});
    //        checkCollectionBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool());


    connect(this->ytFetch, &YouTube::done, [this]()
    {
        this->populateDB({BAE::YoutubeCachePath});
    });

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

    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
96 97 98 99 100 101
    //    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
102
    {
103
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
104
        {
105 106 107 108 109
            this->collectionWatcher();
            emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true},
                                {BAE::TABLEMAP[TABLE::ALBUMS], true},
                                {BAE::TABLEMAP[TABLE::ARTISTS], true},
                                {BAE::TABLEMAP[TABLE::PLAYLISTS], true}});
110

Camilo Higuita's avatar
Camilo Higuita committed
111

112
            //            this->startBrainz(true, 1500);
Camilo Higuita's avatar
Camilo Higuita committed
113

114
            qDebug()<<"Finished inserting into DB";
115
        }/*else
Camilo Higuita's avatar
Camilo Higuita committed
116
        {
Camilo Higuita's avatar
Camilo Higuita committed
117 118 119
            this->dirs.clear();
            this->collectionWatcher();
            this->watcher->removePaths(watcher->directories());
120
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), 3000);
121
        }*/
Camilo Higuita's avatar
Camilo Higuita committed
122 123 124 125


    });

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

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

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

139
void BabeSettings::on_remove_clicked()
Camilo Higuita's avatar
Camilo Higuita committed
140 141 142 143 144 145 146 147 148 149
{
    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());
150
            emit refreshTables({{TABLEMAP[TABLE::TRACKS], true},
Camilo Higuita's avatar
Camilo Higuita committed
151
                                {TABLEMAP[TABLE::PLAYLISTS], true}});
Camilo Higuita's avatar
Camilo Higuita committed
152 153 154 155
        }
    }
}

156
void BabeSettings::refreshCollectionPaths()
Camilo Higuita's avatar
Camilo Higuita committed
157
{
158
    //    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
159

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

165
void BabeSettings::addToWatcher(QStringList paths)
Camilo Higuita's avatar
Camilo Higuita committed
166 167 168 169 170 171
{
    qDebug()<<"duplicated paths in watcher removd: "<<paths.removeDuplicates();

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

172
void BabeSettings::collectionWatcher()
Camilo Higuita's avatar
Camilo Higuita committed
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 200
{
    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);
}

201
void BabeSettings::handleDirectoryChanged(const QString &dir)
Camilo Higuita's avatar
Camilo Higuita committed
202 203 204 205 206
{
    qDebug()<<"directory changed:"<<dir;

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

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

    wait->start();

}

219
void BabeSettings::checkCollectionBrainz(const bool &state)
220
{
221
    qDebug()<<"BRAINZ STATE<<"<<state;
222
    this->startBrainz(state, 3000);
223 224
}

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

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

Camilo Higuita's avatar
Camilo Higuita committed
234 235
}

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

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