Parent: [ddc5e8] (diff)

Child: [22256a] (diff)

Download this file

Playlist.cpp    194 lines (166 with data), 5.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
/* 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 <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ctime>
#include <QList>
#include <QDebug>
#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_pause(false)
{
m_play_idx = -1;
m_selection_min_row = -1;
m_tpstate = AUDIO_UNKNOWN;
m_pause = false;
}
// 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::psl_new_transport_state(int tps, const char *)
{
// qDebug() << "Playlist::psl_new_transport_state " << 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:
emit sig_stopped();
break;
case AUDIO_PLAYING:
emit sig_playing();
break;
case AUDIO_PAUSED:
emit sig_paused();
break;
}
}
// 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;
}
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;
// Using selection_min_row appears to be source of confusion
// } 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 insertDone();
}
static string maybemakesavedir()
{
string sd = qs2utf8s(Helper::getHomeDataPath()) + "pl";
if (access(sd.c_str(), 0) != 0) {
if (mkdir(sd.c_str(), 0700) != 0) {
qDebug() << "Playlist::psl_load_playlist: can't create: " <<
sd.c_str();
return string();
}
}
return sd;
}
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;
}