BabeSettings.cpp 7.17 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 32 33 34 35 36
    this->brainDeamon = new Brain;
    
    //    connect(connection, &CollectionDB::initDB, [this] ()
    //    {
    //        this->populateDB(BAE::MusicPath);
    //    });
Camilo Higuita's avatar
Camilo Higuita committed
37 38 39 40 41

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

42 43 44
#if defined(Q_OS_LINUX)
    const auto notifyDir = BAE::NotifyDir;

Camilo Higuita's avatar
Camilo Higuita committed
45 46 47
    if(!BAE::fileExists(notifyDir+"/Babe.notifyrc"))
    {
        qDebug()<<"The Knotify file does not exists, going to create it";
48
        QFile knotify(":/assets/Babe.notifyrc");
Camilo Higuita's avatar
Camilo Higuita committed
49 50 51 52

        if(knotify.copy(notifyDir+"/Babe.notifyrc"))
            qDebug()<<"the knotify file got copied";
    }
53
#endif
Camilo Higuita's avatar
Camilo Higuita committed
54 55

    QDir collectionDBPath_dir(BAE::CollectionDBPath);
56
    QDir cachePath_dir(BAE::CachePath);
Camilo Higuita's avatar
Camilo Higuita committed
57 58 59 60 61 62 63 64 65
    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
66
    //    if(!connection->check_existance(TABLEMAP[TABLE::SOURCES], KEYMAP[KEY::URL], BAE::MusicPath))
Camilo Higuita's avatar
Camilo Higuita committed
67 68 69

    if(BAE::isMobile())
        this->populateDB({BAE::MusicPath, BAE::DownloadsPath});
70
    else
71
        checkCollectionBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool());
72 73 74 75 76 77 78 79 80 81 82

    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
83

Camilo Higuita's avatar
Camilo Higuita committed
84 85 86 87 88 89
    //    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
90
    {
91
        if(size > 0)
Camilo Higuita's avatar
Camilo Higuita committed
92
        {
Camilo Higuita's avatar
Camilo Higuita committed
93 94 95 96 97 98 99
            this->collectionWatcher();
            emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true},
                                {BAE::TABLEMAP[TABLE::ALBUMS], true},
                                {BAE::TABLEMAP[TABLE::ARTISTS], true},
                                {BAE::TABLEMAP[TABLE::PLAYLISTS], true}});


100
            this->startBrainz(true, 1500);
Camilo Higuita's avatar
Camilo Higuita committed
101 102
        }else
        {
Camilo Higuita's avatar
Camilo Higuita committed
103 104 105
            this->dirs.clear();
            this->collectionWatcher();
            this->watcher->removePaths(watcher->directories());
106
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), 3000);
Camilo Higuita's avatar
Camilo Higuita committed
107 108 109 110 111
        }


    });

112
    connect(this, &BabeSettings::collectionPathChanged, this, &BabeSettings::populateDB);
Camilo Higuita's avatar
Camilo Higuita committed
113 114

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

118
BabeSettings::~BabeSettings()
Camilo Higuita's avatar
Camilo Higuita committed
119 120 121
{
    qDebug()<<"DELETING SETTINGS";
    delete fileLoader;
122
    delete brainDeamon;
Camilo Higuita's avatar
Camilo Higuita committed
123 124
}

125
void BabeSettings::on_remove_clicked()
Camilo Higuita's avatar
Camilo Higuita committed
126 127 128 129 130 131 132 133 134 135
{
    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());
136
            emit refreshTables({{TABLEMAP[TABLE::TRACKS], true},
Camilo Higuita's avatar
Camilo Higuita committed
137
                                {TABLEMAP[TABLE::PLAYLISTS], true}});
Camilo Higuita's avatar
Camilo Higuita committed
138 139 140 141
        }
    }
}

142
void BabeSettings::refreshCollectionPaths()
Camilo Higuita's avatar
Camilo Higuita committed
143
{
144
    //    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
145

146 147 148
    //    for (auto track : this->connection->getDBData(queryTxt))
    //    {
    //    }
Camilo Higuita's avatar
Camilo Higuita committed
149 150
}

151
void BabeSettings::addToWatcher(QStringList paths)
Camilo Higuita's avatar
Camilo Higuita committed
152 153 154 155 156 157
{
    qDebug()<<"duplicated paths in watcher removd: "<<paths.removeDuplicates();

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

158
void BabeSettings::collectionWatcher()
Camilo Higuita's avatar
Camilo Higuita committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
    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);
}

187
void BabeSettings::handleDirectoryChanged(const QString &dir)
Camilo Higuita's avatar
Camilo Higuita committed
188 189 190 191 192
{
    qDebug()<<"directory changed:"<<dir;

    auto wait = new QTimer(this);
    wait->setSingleShot(true);
Camilo Higuita's avatar
fixes  
Camilo Higuita committed
193
    wait->setInterval(1500);
Camilo Higuita's avatar
Camilo Higuita committed
194 195 196

    connect(wait, &QTimer::timeout,[=]()
    {
Camilo Higuita's avatar
Camilo Higuita committed
197
        emit collectionPathChanged({dir});
Camilo Higuita's avatar
Camilo Higuita committed
198 199 200 201 202 203 204
        wait->deleteLater();
    });

    wait->start();

}

205
void BabeSettings::checkCollectionBrainz(const bool &state)
206
{
207 208
    //    this->refreshCollectionPaths();
    //    this->collectionWatcher();
209
    this->startBrainz(state, 3000);
210 211
}

212
void BabeSettings::startBrainz(const bool &on, const uint &speed)
213
{
214 215
    this->brainDeamon->setInterval(speed);
    if(on)
Camilo Higuita's avatar
Camilo Higuita committed
216
        this->brainDeamon->start();
217 218 219
    else
        this->brainDeamon->pause();

Camilo Higuita's avatar
Camilo Higuita committed
220 221
}

222
void BabeSettings::populateDB(const QStringList &paths)
Camilo Higuita's avatar
Camilo Higuita committed
223 224
{
    qDebug() << "Function Name: " << Q_FUNC_INFO
Camilo Higuita's avatar
Camilo Higuita committed
225 226 227 228 229 230
             << "new path for database action: " << paths;
    auto newPaths = paths;
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
    fileLoader->requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
231
}