-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_state.cpp
208 lines (177 loc) · 4.87 KB
/
app_state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "app_state.h"
#include <QDebug>
const QStringList AppState::audioExtensions = QStringList() << "mp3" << "m4a" << "ogg" << "wav" << "flac" << "aac"
<< "wma" << "alac" << "opus" << "mpc" << "ape" << "tta"
<< "ac3" << "dts";
AppState::AppState(PlaylistModel *playlistModel, QObject *parent)
: QObject(parent),
m_audioPlayer(new AudioPlayer(this)),
m_metadataLoader(new MetadataLoader(this)),
m_playlistModel(playlistModel),
m_currentSongIndex(-1),
m_sidebarVisible(true)
{
connect(m_audioPlayer, &AudioPlayer::stateChanged, this, &AppState::playbackStateChanged);
connect(m_audioPlayer, &AudioPlayer::durationChanged, this, &AppState::durationChanged);
connect(m_audioPlayer, &AudioPlayer::positionChanged, this, &AppState::positionChanged);
connect(m_audioPlayer, &AudioPlayer::trackFinished, this, &AppState::nextSong);
}
int AppState::currentSongIndex() const
{
return m_currentSongIndex;
}
void AppState::setCurrentSongIndex(int index)
{
if (index < 0 || index >= m_playlistModel->length())
return;
if (index == m_currentSongIndex)
{
togglePlayback();
return;
}
m_currentSongIndex = index;
emit currentSongIndexChanged(m_currentSongIndex);
emit currentSongChanged(currentSong());
m_audioPlayer->play(m_playlistModel->getSong(m_currentSongIndex).path);
}
void AppState::togglePlayback()
{
m_audioPlayer->togglePlayback();
}
void AppState::nextSong()
{
if (m_currentSongIndex + 1 < m_playlistModel->length())
{
setCurrentSongIndex(m_currentSongIndex + 1);
}
else
{
setCurrentSongIndex(0);
}
}
void AppState::previousSong()
{
if (m_currentSongIndex - 1 >= 0)
{
setCurrentSongIndex(m_currentSongIndex - 1);
}
else
{
setCurrentSongIndex(m_playlistModel->length() - 1);
}
}
void AppState::clearPlaylist()
{
m_audioPlayer->stop();
m_playlistModel->clear();
m_currentSongIndex = -1;
emit currentSongIndexChanged(m_currentSongIndex);
emit currentSongChanged(currentSong());
emit emptyChanged(empty());
emit sidebarVisibleChanged(sidebarVisible());
}
int AppState::volume() const
{
return m_audioPlayer->volume();
}
void AppState::setVolume(int volume)
{
if (m_audioPlayer->volume() != volume)
{
m_audioPlayer->setVolume(volume);
emit volumeChanged(volume);
}
}
bool AppState::sidebarVisible() const
{
return !empty() && m_sidebarVisible;
}
void AppState::setSidebarVisible(bool visible)
{
m_sidebarVisible = visible;
emit sidebarVisibleChanged(m_sidebarVisible);
}
void AppState::addUrl(const QUrl &url)
{
auto localFile = url.toLocalFile();
QFileInfo fileInfo(localFile);
if (fileInfo.isDir())
{
addDirectory(QDir(localFile));
}
else if (isAudioFile(localFile))
{
addFile(localFile);
}
}
void AppState::addDirectory(const QDir &dir)
{
auto files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden | QDir::AllDirs | QDir::Readable, QDir::Name);
for (const QString &file : files)
{
QFileInfo fileInfo(dir, file);
if (fileInfo.isDir())
{
addDirectory(QDir(fileInfo.absoluteFilePath()));
}
else if (isAudioFile(fileInfo.absoluteFilePath()))
{
addFile(fileInfo.absoluteFilePath());
}
}
}
void AppState::addFile(const QString &filePath)
{
auto song = m_metadataLoader->load(filePath);
m_playlistModel->addSong(song);
if (m_currentSongIndex == -1)
{
setCurrentSongIndex(0);
emit emptyChanged(empty());
emit sidebarVisibleChanged(sidebarVisible());
}
}
Song AppState::currentSong() const
{
if (m_currentSongIndex == -1)
return {};
auto song = m_playlistModel->getSong(m_currentSongIndex);
return song;
}
bool AppState::empty() const
{
return m_currentSongIndex == -1;
}
qint64 AppState::duration() const
{
return m_audioPlayer->duration();
}
qint64 AppState::position() const
{
return m_audioPlayer->position();
}
void AppState::setPosition(qint64 position)
{
if (m_audioPlayer->position() != position)
{
m_audioPlayer->setPosition(position);
emit positionChanged(position);
}
}
QString AppState::playbackState() const
{
switch(m_audioPlayer->state()) {
case QMediaPlayer::StoppedState:
return "StoppedState";
case QMediaPlayer::PlayingState:
return "PlayingState";
case QMediaPlayer::PausedState:
return "PausedState";
default:
return "UnknownState";
}
}
bool AppState::isAudioFile(const QString &filePath)
{
return audioExtensions.contains(QFileInfo(filePath).suffix().toLower());
}