- Add app icon - prevend android device from shutting down cpu while playing music - fix scaling bug - fix energy saver for RasPi by reconnecting to music player - minor refactorings and renaming
130 lines
3.7 KiB
C++
130 lines
3.7 KiB
C++
#include "NavigationController.h"
|
|
#include <controllers/SettingsHandler.h>
|
|
|
|
#include <QDir>
|
|
#include <QDebug>
|
|
#include <EnergySaver.h>
|
|
|
|
#include <models/NavigationItemModel.h>
|
|
#include <models/NavigationListModel.h>
|
|
#include <models/UiStateModel.h>
|
|
#include <controllers/MusicPlayer.h>
|
|
|
|
NavigationController::NavigationController(QObject *parent) : QObject(parent),
|
|
mRootItem(new NavigationItemModel(this)),
|
|
mNavList(new NavigationListModel(this)),
|
|
mUiState(new UiStateModel(this)),
|
|
mMediaPlayer(new MusicPlayer(this))
|
|
{
|
|
connect(mMediaPlayer, &MusicPlayer::navigateTo, [this](NavigationItemModel* item) {
|
|
mUiState->showNavigation();
|
|
mNavList->navigateTo(item);
|
|
});
|
|
/* Connect player state to energy saver to prevent device shutdown while playing music.
|
|
*/
|
|
connect(mMediaPlayer, &MusicPlayer::isPlayingChanged, this, &NavigationController::startOrStopEnergySaverDependingOnPlayerState);
|
|
}
|
|
|
|
void NavigationController::setDebugOutput(const QString& text)
|
|
{
|
|
mDebugOutput = text;
|
|
emit debugOutputChanged();
|
|
}
|
|
|
|
void NavigationController::init(const QString &rootPath)
|
|
{
|
|
auto rootDir = QDir(rootPath);
|
|
if(!rootDir.exists()) return;
|
|
mRootPath = rootPath;
|
|
|
|
add(mRootPath, mRootItem);
|
|
|
|
mNavList->setModelItems(mRootItem->getChildren());
|
|
}
|
|
|
|
void NavigationController::setUiProfile(const QString &profileString)
|
|
{
|
|
mUiState->setProfile(profileString);
|
|
}
|
|
|
|
void NavigationController::setContext(QQmlContext *context)
|
|
{
|
|
mContext = context;
|
|
setContextProperties();
|
|
}
|
|
|
|
void NavigationController::setContextProperties()
|
|
{
|
|
if(!mContext) return;
|
|
mContext->setContextProperty("navigationList", mNavList);
|
|
mContext->setContextProperty("uiStateModel", mUiState);
|
|
mContext->setContextProperty("musicModel", mMediaPlayer);
|
|
mContext->setContextProperty("debug", this);
|
|
}
|
|
|
|
void NavigationController::add(const QString &path, NavigationItemModel *parentItem)
|
|
{
|
|
auto dir = QDir(path);
|
|
if(!dir.exists()) return;
|
|
|
|
auto subDirsNames = dir.entryList(QDir::AllDirs);
|
|
|
|
for(const auto& name : subDirsNames){
|
|
if(name == "." || name == "..") continue;
|
|
if(!checkContent(path + "/" + name)) continue;
|
|
auto item = new NavigationItemModel(parentItem);
|
|
if(!item->setPath(path + "/" + name)) {
|
|
item->deleteLater();
|
|
continue;
|
|
}
|
|
connect(item, &NavigationItemModel::clicked, this, &NavigationController::onNavigationRequest);
|
|
parentItem->appendChild(item);
|
|
add(item->getPath(), item);
|
|
}
|
|
}
|
|
|
|
bool NavigationController::checkContent(const QString &path)
|
|
{
|
|
bool valid = false;
|
|
auto dir =QDir(path);
|
|
// directory must either contain subdirectories or media files
|
|
auto subDirsNames = dir.entryList(QDir::AllDirs);
|
|
if(subDirsNames.length() > 0) {
|
|
valid = true;
|
|
} else {
|
|
dir.setNameFilters(SettingsHandler::getAudioFileNameFilters());
|
|
auto fileNames = dir.entryList(QDir::Files);
|
|
int numAudio = 0;
|
|
for(auto file:fileNames){
|
|
numAudio++;
|
|
}
|
|
valid = numAudio > 0;
|
|
}
|
|
return valid;
|
|
}
|
|
|
|
void NavigationController::onNavigationRequest()
|
|
{
|
|
auto item = qobject_cast<NavigationItemModel*>(QObject::sender());
|
|
if(!item) return;
|
|
|
|
if(item->hasChildren())
|
|
mNavList->setModelItems(item->getChildren());
|
|
else {
|
|
mMediaPlayer->init(item);
|
|
mUiState->showMusicPlayer();
|
|
}
|
|
}
|
|
|
|
void NavigationController::startOrStopEnergySaverDependingOnPlayerState()
|
|
{
|
|
auto* energySaver = EnergySaver::instance();
|
|
assert(energySaver);
|
|
if(energySaver){
|
|
if(mMediaPlayer->isPlaying()){
|
|
energySaver->deactivate();
|
|
} else {
|
|
energySaver->activate();
|
|
}
|
|
}
|
|
}
|