BabeSettings.cpp 7.69 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
        this->populateDB({BAE::MusicPath, BAE::YoutubeCachePath});
    //        checkCollectionBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool());


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

    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, BAE::SEG::ONEHALF);
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 121
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), BAE::SEG::THREE);
        }
Camilo Higuita's avatar
Camilo Higuita committed
122 123
    });

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

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

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

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

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

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

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

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

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

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

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

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

    wait->start();

}

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

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

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

Camilo Higuita's avatar
Camilo Higuita committed
232 233
}

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

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