105 lines
2.9 KiB
C++
105 lines
2.9 KiB
C++
#include "NavigationController.h"
|
|
|
|
#include <QDir>
|
|
#include <QDebug>
|
|
|
|
#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);
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
auto subDirsNames = dir.entryList(QDir::AllDirs);
|
|
if(subDirsNames.length() > 0) {
|
|
valid = true;
|
|
} else {
|
|
auto fileNames = dir.entryList(QDir::Files);
|
|
int numAudio = 0;
|
|
for(auto file:fileNames){
|
|
if(file.endsWith(".flac") || file.endsWith(".mp3")){
|
|
numAudio++;
|
|
}
|
|
}
|
|
if(numAudio > 0) valid = true;
|
|
}
|
|
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();
|
|
}
|
|
}
|