Parent: [2e56d6] (diff)

Child: [dfef85] (diff)

Download this file

PlaylistAVT.cpp    355 lines (303 with data), 9.2 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* 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>
#include <QFile>
#include <QList>
#include <QObject>
#include <QDate>
#include <QTime>
#include <QDebug>
#include <QDir>
#include <QTimer>
#include <libupnpp/control/avtransport.hxx>
#include "PlaylistAVT.h"
#include "HelperStructs/Helper.h"
#include "upadapt/upputils.h"
using namespace std;
PlaylistAVT::PlaylistAVT(QObject *parent)
: Playlist(parent)
{
QTimer::singleShot(0, this, SLOT(playlist_updated()));
}
PlaylistAVT::PlaylistAVT(const string& _udn, QObject *parent)
: Playlist(parent)
{
string udn(_udn);
if (udn.find("uuid:") == 0) {
udn = udn.substr(5);
}
m_savefile = qs2utf8s(Helper::getHomeDataPath()) + string("sq-") +
(udn.empty() ? "savedQueue" : udn);
// We used to call these "savedQueue". Rename on first use. This
// can go away in a few releases.
string oldsaved = qs2utf8s(Helper::getHomeDataPath()) + "savedQueue";
if (!udn.empty()) {
if (access(oldsaved.c_str(), R_OK) == 0 &&
access(m_savefile.c_str(), 0) != 0) {
rename(oldsaved.c_str(), m_savefile.c_str());
}
}
m_meta.unSerialize(u8s2qs(m_savefile));
QTimer::singleShot(0, this, SLOT(playlist_updated()));
}
void PlaylistAVT::set_for_playing(int row)
{
qDebug() << "PlaylistAVT::set_for_playing " << row;
if (row < 0 || row >= int(m_meta.size())) {
m_play_idx = -1;
m_meta.setCurPlayTrack(-1);
return;
}
m_play_idx = row;
m_meta.setCurPlayTrack(row);
emit sig_playing_track_changed(row);
emit sig_play_now(m_meta[row]);
emit sig_track_metadata(m_meta[row]);
}
// Player switched tracks under us. Hopefully the uri matches another
// track. We first look ahead, because the normal situation is that
// the device switched to the nextURI track
void PlaylistAVT::psl_ext_track_change(const QString& uri)
{
qDebug() << "PlaylistAVT::psl_ext_track_change " << uri;
if (m_play_idx < -1) // ??
return;
for (unsigned int i = m_play_idx + 1; i < m_meta.size(); i++) {
if (!uri.compare(m_meta[i].filepath)) {
qDebug() << "PlaylistAVT::psl_ext_track_change: index now " << i;
m_play_idx = i;
m_meta.setCurPlayTrack(i);
emit sig_playing_track_changed(i);
emit sig_track_metadata(m_meta[i]);
return;
}
}
for (int i = 0; i <= m_play_idx && i < int(m_meta.size()); i++) {
if (!uri.compare(m_meta[i].filepath)) {
qDebug() << "PlaylistAVT::psl_ext_track_change: index now " << i;
m_play_idx = i;
m_meta.setCurPlayTrack(i);
emit sig_playing_track_changed(i);
emit sig_track_metadata(m_meta[i]);
return;
}
}
}
void PlaylistAVT::psl_onCurrentMetadata(const MetaData& md)
{
if (!m_meta.contains(md, true)) {
m_meta.push_back(md);
playlist_updated();
}
emit sig_track_metadata(md);
}
void PlaylistAVT::send_next_playing_signal()
{
// Do not do this in shuffle mode: makes no sense + as we never go
// through the stop state, shuffle does not work
if (CSettingsStorage::getInstance()->getPlaylistMode().shuffle)
return;
// Only if there is a track behind the current one
if (m_play_idx >= 0 && m_play_idx < int(m_meta.size()) - 1)
emit sig_next_track_to_play(m_meta[m_play_idx + 1]);
}
void PlaylistAVT::psl_prepare_for_end_of_track()
{
if (!valid_row(m_play_idx))
return;
send_next_playing_signal();
}
void PlaylistAVT::psl_next_track()
{
qDebug() << "PlaylistAVT::psl_next_track()";
int track_num = -1;
if(m_meta.empty()) {
qDebug() << "PlaylistAVT::psl_next_track(): empty playlist";
goto out;
}
if (CSettingsStorage::getInstance()->getPlaylistMode().shuffle) {
// shuffle mode
track_num = rand() % m_meta.size();
if (track_num == m_play_idx) {
track_num = (m_play_idx + 1) % m_meta.size();
}
} else {
if (m_play_idx >= int(m_meta.size()) -1) {
// last track
qDebug() << "PlaylistAVT::psl_next_track(): was last, stop or loop";
if (CSettingsStorage::getInstance()->getPlaylistMode().repAll) {
track_num = 0;
}
} else {
track_num = m_play_idx + 1;
qDebug() << "PlaylistAVT::psl_next_track(): new tnum " << track_num;
}
}
out:
if (track_num >= 0) {
// valid next track
if(checkTrack(m_meta[track_num])){
set_for_playing(track_num);
} else {
remove_row(track_num);
psl_next_track();
}
} else {
set_for_playing(-1);
emit sig_stop();
return;
}
}
void PlaylistAVT::psl_clear_playlist_impl()
{
emit sig_stop();
playlist_updated();
}
void PlaylistAVT::psl_play()
{
m_pause = false;
if (m_meta.empty()) {
return;
}
if (m_play_idx < 0) {
if (valid_row(m_selection_min_row)) {
set_for_playing(m_selection_min_row);
} else {
set_for_playing(0);
}
} else {
emit sig_resume_play();
}
}
void PlaylistAVT::psl_pause()
{
m_pause = true;
emit sig_pause();
}
void PlaylistAVT::psl_stop()
{
set_for_playing(-1);
emit sig_stop();
playlist_updated();
}
// fwd was pressed -> next track
void PlaylistAVT::psl_forward()
{
psl_next_track();
}
// GUI -->
void PlaylistAVT::psl_backward()
{
if (m_play_idx <= 0) {
return;
}
int track_num = m_play_idx - 1;
if (checkTrack(m_meta[track_num])) {
set_for_playing(track_num);
}
}
// GUI -->
void PlaylistAVT::psl_change_track_impl(int new_row)
{
if (!valid_row(new_row)) {
return;
}
if (checkTrack(m_meta[new_row])) {
set_for_playing(new_row);
} else {
set_for_playing(-1);
remove_row(new_row);
emit sig_stop();
playlist_updated();
}
}
// insert tracks after idx which may be -1
void PlaylistAVT::psl_insert_tracks(const MetaDataList& nmeta, int row)
{
// Change to more conventional "insert before"
++row;
qDebug() << "PlaylistAVT::psl_insert_tracks: cur size" <<
m_meta.size() << ". " << nmeta.size() << " rows before row " << row;
if (m_meta.empty()) {
if (row != 0) {
return;
}
} else if (row < 0 || row > int(m_meta.size())) {
qDebug() << " bad row";
return;
}
m_meta.insert(m_meta.begin() + row, nmeta.begin(), nmeta.end());
// Find the playing track (could be part of nmeta if this is a d&d
// cut/paste) and adjust m_play_idx
m_play_idx = -1;
for (auto it = m_meta.begin(); it != m_meta.end(); it++) {
if (it->pl_playing && m_play_idx == -1) {
m_play_idx = it - m_meta.begin();
} else {
it->pl_playing = false;
}
}
// Prepare following track
send_next_playing_signal();
playlist_updated();
}
void PlaylistAVT::playlist_updated()
{
emit sig_playlist_updated(m_meta, m_play_idx, 0);
string tmp(m_savefile + "-");
m_meta.serialize(u8s2qs(tmp));
rename(tmp.c_str(), m_savefile.c_str());
}
void PlaylistAVT::psl_remove_rows(const QList<int>& rows, bool select_next_row)
{
qDebug() << "PlaylistAVT::psl_remove_rows";
if (rows.empty()) {
return;
}
if (rows.contains(m_play_idx)) {
m_play_idx = -1;
emit sig_stop();
}
MetaDataList v_tmp_md;
int n_tracks_before_cur_idx = 0;
int first_row = rows[0];
for (unsigned int i = 0; i < m_meta.size(); i++) {
if (rows.contains(i)) {
if (int(i) < m_play_idx) {
n_tracks_before_cur_idx++;
}
} else {
MetaData md = m_meta[i];
md.pl_dragged = false;
md.pl_selected = false;
md.pl_playing = false;
v_tmp_md.push_back(md);
}
}
m_play_idx -= n_tracks_before_cur_idx;
m_meta = v_tmp_md;
if (m_play_idx < 0 || m_play_idx >= (int) m_meta.size()) {
m_play_idx = -1;
} else {
m_meta[m_play_idx].pl_playing = true;
}
if (select_next_row) {
m_meta[first_row].pl_selected = true;
}
playlist_updated();
emit sig_playlist_updated(m_meta, m_play_idx, 0);
}