Parent: [dd58ef] (diff)

Child: [1cc5a7] (diff)

Download this file

playlist.cpp    264 lines (227 with data), 7.1 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
/* 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 <sys/stat.h>
#include <sys/types.h>
#include <ctime>
#include <QList>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include "upadapt/upputils.h"
#include "playlist.h"
#include "HelperStructs/Helper.h"
using namespace std;
Playlist::Playlist(QObject* parent)
: QObject (parent), m_play_idx(-1), m_selection_min_row(-1),
m_insertion_point(-1), m_tpstate(AUDIO_UNKNOWN)
{
m_play_idx = -1;
m_selection_min_row = -1;
m_tpstate = AUDIO_UNKNOWN;
}
// GUI -->
void Playlist::psl_clear_playlist()
{
m_meta.clear();
m_play_idx = -1;
m_selection_min_row = -1;
m_insertion_point = -1;
psl_clear_playlist_impl();
}
// Remove one row
void Playlist::remove_row(int row)
{
//qDebug() << "Playlist::remove_row";
QList<int> remove_list;
remove_list << row;
psl_remove_rows(remove_list);
}
void Playlist::onRemoteTpState(int tps, const char *sst)
{
// qDebug() << "Playlist::onRemoteTpState " << s <<
// " play_idx " << m_play_idx;
// if (m_play_idx >= 0 && m_play_idx < int(m_meta.size()))
// qDebug() << " meta[idx].pl_playing " <<
// m_meta[m_play_idx].pl_playing;
m_tpstate = tps;
switch (tps) {
case AUDIO_UNKNOWN:
case AUDIO_STOPPED:
default:
//qDebug() << "Playlist::onRemoteTpState: STOPPED";
emit sig_stopped();
break;
case AUDIO_PLAYING:
//qDebug() << "Playlist::onRemoteTpState: PLAYING";
emit sig_playing();
break;
case AUDIO_PAUSED:
//qDebug() << "Playlist::onRemoteTpState: PAUSED";
emit sig_paused();
break;
}
onRemoteTpState_impl(tps, sst);
}
Playlist_Mode Playlist::mode()
{
return CSettingsStorage::getInstance()->getPlaylistMode();
}
// GUI -->
void Playlist::psl_change_mode(const Playlist_Mode& mode)
{
CSettingsStorage::getInstance()->setPlaylistMode(mode);
emit sig_mode_changed(mode);
}
void Playlist::get_metadata(MetaDataList& out)
{
out = m_meta;
}
class MetaDataCmp {
public:
enum SortCrit {SC_TIT, SC_ART, SC_ALB, SC_Y, SC_FN, SC_TNO};
MetaDataCmp(const vector<SortCrit>& crits)
: m_crits(crits) {}
bool operator()(const MetaData& o1, const MetaData& o2) {
int rel;
QString s1, s2;
for (unsigned int i = 0; i < m_crits.size(); i++) {
SortCrit crit = m_crits[i];
rel = obgetprop(o1, crit, s1).compare(obgetprop(o2, crit, s2));
if (rel < 0)
return true;
else if (rel > 0)
return false;
}
return false;
}
const QString& obgetprop(const MetaData& o, SortCrit crit, QString& stor) {
qint32 val;
switch(crit) {
case SC_TIT: return o.title;
case SC_ART: return o.artist;
case SC_ALB: return o.album;
case SC_FN: return o.filepath;
case SC_Y: val = o.year; goto valtostr;
case SC_TNO: val = o.track_num;
valtostr: {
char num[30];
sprintf(num, "%010d", int(val));
stor = num;
return stor;
}
default:
return nullstr;
}
}
vector<SortCrit> m_crits;
static QString nullstr;
};
QString MetaDataCmp::nullstr;
static void mdsort(const MetaDataList& inlist, MetaDataList& outlist,
const vector<MetaDataCmp::SortCrit> crits)
{
MetaDataCmp cmpo(crits);
outlist = inlist;
std::sort(outlist.begin(), outlist.end(), cmpo);
}
void Playlist::psl_sort_by_tno()
{
vector<MetaDataCmp::SortCrit> crits;
// Makes no sense to sort by tno independantly of album
crits.push_back(MetaDataCmp::SC_ALB);
crits.push_back(MetaDataCmp::SC_TNO);
MetaDataList md;
mdsort(m_meta, md, crits);
psl_clear_playlist();
psl_insert_tracks(md, -1);
}
void Playlist::psl_add_tracks(const MetaDataList& v_md)
{
qDebug() << "Playlist::psl_add_tracks: " <<
" ninserttracks " << v_md.size() <<
" m_meta.size() " << m_meta.size();
emit sig_sync();
Playlist_Mode playlist_mode = CSettingsStorage::getInstance()->getPlaylistMode();
if (playlist_mode.replace)
psl_clear_playlist();
int afteridx = -1;
if (playlist_mode.append) {
afteridx = m_meta.size() - 1;
} else {
if (m_insertion_point >= 0) {
afteridx = m_insertion_point;
} else if (m_play_idx >= 0) {
afteridx = m_play_idx;
// } else if (m_selection_min_row >= 0) {
// afteridx = m_selection_min_row;
} else {
// The more natural thing to do if neither playing nore
// selection is to append. Else clicking on several tracks
// inserts them in reverse order
afteridx = m_meta.size() - 1;
}
}
psl_insert_tracks(v_md, afteridx);
m_insertion_point = afteridx + 1;
if (playlist_mode.playAdded) {
psl_change_track(afteridx+1);
psl_play();
}
emit sig_insert_done();
}
static string maybemakesavedir()
{
QDir topdata(Helper::getHomeDataPath());
if (!topdata.exists("pl") && !topdata.mkdir("pl")) {
qDebug() << "Could not create: " << topdata.absoluteFilePath("pl");
}
return qs2utf8s(topdata.absoluteFilePath("pl"));
}
void Playlist::psl_load_playlist()
{
string savedir = maybemakesavedir();
if (savedir.empty())
return;
QString fn = QFileDialog::getOpenFileName(
0, tr("Open Playlist"), u8s2qs(savedir), tr("Saved playlists (*.pl)"));
if (!fn.isNull()) {
MetaDataList meta;
meta.unSerialize(fn);
psl_clear_playlist();
psl_add_tracks(meta);
}
}
void Playlist::psl_save_playlist()
{
string savedir = maybemakesavedir();
if (savedir.empty())
return;
QString fn = QFileDialog::getSaveFileName(
0, tr("Save Current Playlist"), u8s2qs(savedir),
tr("Saved playlists (*.pl)"));
if (!fn.isNull()) {
if (fn.indexOf(".pl") != fn.size() - 3)
fn += ".pl";
qDebug() << "Saving to " << fn;
m_meta.serialize(fn);
}
}
void Playlist::psl_selection_min_row(int row)
{
//qDebug() << "Playlist::psl_selection_min_row: now: " << row;
m_selection_min_row = row;
}