BabeSettings.cpp 7.28 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

    if(BAE::isMobile())
69
        this->populateDB(QStringList()<<BAE::MusicPath<<BAE::DownloadsPath<<BAE::MusicPaths<<BAE::DownloadsPaths);
70
    else
71 72
        this->populateDB({BAE::MusicPath});
//        checkCollectionBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool());
73 74 75 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)
    {
        emit this->refreshTables({{BAE::TABLEMAP[type], true}});
    });

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
        {
93 94 95 96 97
            this->collectionWatcher();
            emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true},
                                {BAE::TABLEMAP[TABLE::ALBUMS], true},
                                {BAE::TABLEMAP[TABLE::ARTISTS], true},
                                {BAE::TABLEMAP[TABLE::PLAYLISTS], true}});
98

Camilo Higuita's avatar
Camilo Higuita committed
99

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

102
            qDebug()<<"Finished inserting into DB";
Camilo Higuita's avatar
Camilo Higuita committed
103 104
        }else
        {
Camilo Higuita's avatar
Camilo Higuita committed
105 106 107
            this->dirs.clear();
            this->collectionWatcher();
            this->watcher->removePaths(watcher->directories());
108
            this->startBrainz(BAE::loadSettings("BRAINZ", "BABE", false).toBool(), 3000);
Camilo Higuita's avatar
Camilo Higuita committed
109 110 111 112 113
        }


    });

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

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

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

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

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

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

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

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

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

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

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

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

    wait->start();

}

207
void BabeSettings::checkCollectionBrainz(const bool &state)
208
{
209
    qDebug()<<"BRAINZ STATE<<"<<state;
210
    this->startBrainz(state, 3000);
211 212
}

213
void BabeSettings::startBrainz(const bool &on, const uint &speed)
214
{
215
    this->brainDeamon->setInterval(speed);
216

217
    if(on)
Camilo Higuita's avatar
Camilo Higuita committed
218
        this->brainDeamon->start();
219 220 221
    else
        this->brainDeamon->pause();

Camilo Higuita's avatar
Camilo Higuita committed
222 223
}

224
void BabeSettings::populateDB(const QStringList &paths)
Camilo Higuita's avatar
Camilo Higuita committed
225 226
{
    qDebug() << "Function Name: " << Q_FUNC_INFO
Camilo Higuita's avatar
Camilo Higuita committed
227 228
             << "new path for database action: " << paths;
    auto newPaths = paths;
229

Camilo Higuita's avatar
Camilo Higuita committed
230 231 232 233
    for(auto path : newPaths)
        if(path.startsWith("file://"))
            path.replace("file://", "");
    fileLoader->requestPaths(newPaths);
Camilo Higuita's avatar
Camilo Higuita committed
234
}