TrackManager.cpp Example File
dictaphone/src/TrackManager.cpp
#include "TrackManager.hpp"
#include <QtCore/QDir>
#include <QtCore/QDirIterator>
using namespace bb::cascades;
static QString trackStorageLocation()
{
return QString("data/tracks/");
}
static void initializeTrackStorage()
{
QDir trackStorage(trackStorageLocation());
if (!trackStorage.exists())
QDir::root().mkpath(trackStorage.absolutePath());
}
TrackManager::TrackManager(QObject *parent)
: QObject(parent)
, m_model(new GroupDataModel(this))
, m_trackCounter(0)
{
m_model->setGrouping(ItemGrouping::None);
initializeTrackStorage();
updateTrackInformation();
}
QUrl TrackManager::nextTrackUrl() const
{
const int nextTrack = m_trackCounter + 1;
return QUrl(QString::fromLatin1("%1/track%2.m4a").arg(QDir(trackStorageLocation()).absolutePath())
.arg(nextTrack, 3, 10, QLatin1Char('0')));
}
void TrackManager::clearAllTracks()
{
QDirIterator it(trackStorageLocation(), QDir::Files | QDir::NoDotAndDotDot);
while (it.hasNext()) {
it.next();
QFile::remove(it.fileInfo().absoluteFilePath());
}
updateTrackInformation();
}
void TrackManager::update()
{
updateTrackInformation();
}
GroupDataModel* TrackManager::model() const
{
return m_model;
}
bool TrackManager::hasRecordedTracks() const
{
return (m_model->childCount(QVariantList()) != 0);
}
void TrackManager::updateTrackInformation()
{
const bool oldHasRecordedTracks = hasRecordedTracks();
m_model->clear();
m_trackCounter = 0;
QDirIterator it(trackStorageLocation(), QDir::Files | QDir::NoDotAndDotDot);
while (it.hasNext()) {
it.next();
QVariantMap entry;
entry["name"] = it.fileName();
entry["url"] = it.fileInfo().absoluteFilePath();
m_model->insert(entry);
m_trackCounter++;
}
if (oldHasRecordedTracks != hasRecordedTracks())
emit hasRecordedTracksChanged();
}