Parent: [2305a0] (diff)

Child: [85910e] (diff)

Download this file

Playlist.cpp    205 lines (171 with data), 5.7 kB

  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
/* Copyright (C) 2011 Lucio Carreras
*
* This file is part of sayonara player
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <ctime>
using namespace std;
#include <QFile>
#include <QList>
#include <QObject>
#include <QDate>
#include <QTime>
#include <QDebug>
#include <QDir>
#include <libupnpp/control/avtransport.hxx>
#include "Playlist.h"
#include "HelperStructs/MetaData.h"
#include "HelperStructs/Helper.h"
#include "HelperStructs/CSettingsStorage.h"
#include "HelperStructs/PlaylistMode.h"
Playlist::Playlist(QObject * parent)
: QObject (parent)
{
_settings = CSettingsStorage::getInstance();
_playlist_mode = _settings->getPlaylistMode();
_v_meta_data.clear();
_cur_play_idx = -1;
_is_playing = false;
_pause = false;
}
Playlist::~Playlist()
{
}
// create a playlist, where metadata is already available
void Playlist::psl_create_playlist(MetaDataList& v_meta_data){
bool instant_play = ((_v_meta_data.size() == 0) && (!_is_playing));
if(!_playlist_mode.append){
_v_meta_data.clear();
_cur_play_idx = -1;
}
// no tracks in new playlist
if(v_meta_data.size() == 0)
{
emit sig_playlist_updated(_v_meta_data, _cur_play_idx, 0);
return;
}
foreach(MetaData md, v_meta_data){
_v_meta_data.push_back(md);
}
emit sig_playlist_updated(_v_meta_data, _cur_play_idx, 0);
if(instant_play)
send_cur_playing_signal(0);
}
void Playlist::send_cur_playing_signal(int i)
{
qDebug() << "Playlist::send_cur_playing_signal: " << i;
if (i < 0 && i >= int(_v_meta_data.size()))
return;
_is_playing = true;
if(!checkTrack(_v_meta_data[i]))
return;
emit sig_playing_track_changed(i);
emit sig_play_now(_v_meta_data[i]);
emit sig_track_metadata(_v_meta_data[i]);
_cur_play_idx = i;
}
// Player switched tracks under us. We get the URI
void Playlist::psl_ext_track_change(const QString& uri)
{
if (_cur_play_idx < 0 || _cur_play_idx >= int(_v_meta_data.size()) - 1)
return;
for (unsigned int i = _cur_play_idx + 1; i < _v_meta_data.size(); i++) {
if (!uri.compare(_v_meta_data[i].filepath)) {
qDebug() << "Playlist::psl_ext_track_change: index now " << i;
_cur_play_idx = i;
emit sig_playing_track_changed(i);
emit sig_track_metadata(_v_meta_data[i]);
break;
}
}
}
void Playlist::send_next_playing_signal(int i)
{
if (i >= 0 && i < int(_v_meta_data.size()))
emit sig_next_track_to_play(_v_meta_data[i]);
}
void Playlist::psl_prepare_for_end_of_track()
{
if (_cur_play_idx < 0 || _cur_play_idx >= int(_v_meta_data.size()) - 1)
return;
send_next_playing_signal(_cur_play_idx+1);
}
void Playlist::psl_new_transport_state(int tps)
{
string s;
switch (tps) {
case UPnPClient::AVTransport::Stopped: s = "Stopped"; break;
case UPnPClient::AVTransport::Playing: s = "Playing"; break;
case UPnPClient::AVTransport::Transitioning: s = "Transitioning"; break;
case UPnPClient::AVTransport::PausedPlayback: s = "PausedPlayback"; break;
case UPnPClient::AVTransport::PausedRecording: s = "PausedRecording"; break;
case UPnPClient::AVTransport::Recording: s = "Recording"; break;
case UPnPClient::AVTransport::NoMediaPresent: s = "NoMediaPresent"; break;
case UPnPClient::AVTransport::Unknown:
default:
s = "Unknown"; break;
}
qDebug() << "psl_new_transport_state: " << s.c_str();
}
void Playlist::psl_next_track()
{
qDebug() << "Playlist::psl_next_track()";
int track_num = -1;
if(_v_meta_data.size() == 0){
qDebug() << "Playlist::psl_next_track(): empty playlist";
_cur_play_idx = -1;
_is_playing = false;
emit sig_stopped();
return;
}
// shuffle mode
if(_playlist_mode.shuffle) {
track_num = rand() % _v_meta_data.size();
if (track_num == _cur_play_idx) {
track_num = (_cur_play_idx + 1) % _v_meta_data.size();
}
} else {
// last track?
if(_cur_play_idx >= (int) _v_meta_data.size() -1) {
qDebug() << "Playlist::psl_next_track(): was last, stop";
track_num = -1;
if(_playlist_mode.repAll) {
track_num = 0;
} else {
_is_playing = false;
_cur_play_idx = -1;
emit sig_stopped();
return;
}
} else {
track_num = _cur_play_idx + 1;
qDebug() << "Playlist::psl_next_track(): new tnum " << track_num;
}
}
// valid next track
if(track_num >= 0) {
_v_meta_data.setCurPlayTrack(track_num);
MetaData md = _v_meta_data[track_num];
if(checkTrack(md)){
send_cur_playing_signal(track_num);
} else {
remove_row(track_num);
psl_next_track();
}
}
}
uint Playlist::get_num_tracks()
{
return _v_meta_data.size();
}