-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlistmodel.cpp
305 lines (262 loc) · 8.4 KB
/
playlistmodel.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
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
#include "playlistmodel.h"
#include "playlistitem.h"
#include "arfariusapplication.h"
#include <QProgressDialog>
#include <QDirIterator>
#include <QtConcurrent>
#include <QFileInfo>
#include <QMimeData>
#include <QColor>
#include <QDebug>
PlayListModel::PlayListModel(QObject *parent) :
QAbstractTableModel(parent), items(), current(-1)
{
}
int PlayListModel::rowCount(const QModelIndex & /*parent*/) const
{
return items.count();
}
int PlayListModel::columnCount(const QModelIndex & /*parent*/) const
{
return PlayListItem::getColumnsCount();
}
Qt::DropActions PlayListModel::supportedDropActions() const
{
return QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
}
Qt::ItemFlags PlayListModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
QVariant PlayListModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
PlayListItem *i = items[index.row()];
return i->getColumn(index.column());
} else if (role == Qt::BackgroundRole && index.row() == current) {
return QVariant(QColor(152,209,117));
} else if (role == Qt::EditRole) {
PlayListItem *i = items[index.row()];
return i->getColumn(index.column());
}
return QVariant();
}
bool PlayListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
PlayListItem *i = items[index.row()];
i->setColumn(index.column(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant PlayListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal){
return PlayListItem::getColumnName(section);
} else if (orientation == Qt::Vertical) {
return QString("Row %1").arg(section);
}
return QVariant();
}
bool PlayListModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent)
beginRemoveRows( QModelIndex(), row, row + count - 1);
PlayListItem *current_item = nullptr;
if (current >= 0 && current < items.size()) {
current_item = items[current];
}
for (int i = row; i < row+count; i++) {
while(items[i]->isBusy()) arfariusApp->processEvents(QEventLoop::AllEvents, 10);
items[i]->deleteLater();
items.removeAt(i);
}
if (current_item) {
current = items.indexOf(current_item);
if (current == -1) {
if (items.count() > row) {
current = row;
emit itemUpdated(items[current]);
} else {
emit itemUpdated(nullptr);
}
}
}
endRemoveRows();
return true;
}
QStringList PlayListModel::mimeTypes() const
{
QStringList mime_types;
mime_types << "url";
return mime_types;
}
void PlayListModel::clickedItem(const QModelIndex &index)
{
int previous = current;
current = index.row();
emit itemUpdated(items[current]);
emit dataChanged(createIndex(previous, 0), createIndex(previous, PlayListItem::getColumnsCount()));
emit dataChanged(createIndex(current, 0), createIndex(current, PlayListItem::getColumnsCount()));
}
void PlayListModel::nextItem()
{
if (!items.count()) {
emit itemUpdated(nullptr);
} else {
int previous = current;
if (++current >= items.size()) {
current = -1;
emit itemUpdated(nullptr);
emit dataChanged(createIndex(previous, 0), createIndex(previous, PlayListItem::getColumnsCount()));
} else {
emit itemUpdated(items[current]);
emit dataChanged(createIndex(previous, 0), createIndex(previous, PlayListItem::getColumnsCount()));
emit dataChanged(createIndex(current, 0), createIndex(current, PlayListItem::getColumnsCount()));
}
}
}
void PlayListModel::prevItem()
{
if (!items.count()) {
emit itemUpdated(nullptr);
} else {
int previous = current;
if (--current < 0) {
current = -1;
emit itemUpdated(nullptr);
emit dataChanged(createIndex(previous, 0), createIndex(previous, PlayListItem::getColumnsCount()));
} else {
emit itemUpdated(items[current]);
emit dataChanged(createIndex(previous, 0), createIndex(previous, PlayListItem::getColumnsCount()));
emit dataChanged(createIndex(current, 0), createIndex(current, PlayListItem::getColumnsCount()));
}
}
}
void PlayListModel::save(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly |QIODevice::Truncate)) {
qWarning() << this << "save(): failed to open file";
return;
}
PlayListItem *item;
foreach (item, items) {
file.write(item->getUrlString().toUtf8() + "\n");
}
}
void PlayListModel::gather(QString path) {
QDir dir(path);
if (!dir.exists()) {
qWarning() << this << "gather(): directory doesn't exist";
return;
}
PlayListItem *item;
foreach (item, items) {
if (!item->isLocalFile()) {
continue;
}
auto filename = item->getUrlStringLocal();
QFileInfo fileinfo(filename);
dir.rename(filename, fileinfo.fileName());
item->setUrl(QUrl::fromLocalFile(dir.filePath(fileinfo.fileName())));
item->ensureHistogram();
}
}
void PlayListModel::removeCurrent() {
qDebug() << this << "removeCurrent()";
removeRows(current, 1, QModelIndex());
}
void PlayListModel::appendUrl(QUrl url)
{
appendUrls(QList<QUrl>{ url });
}
void PlayListModel::appendUrls(QList<QUrl> urls)
{
QProgressDialog progress("Adding files to playlist...", "Cancel", 0, urls.count());
progress.setWindowModality(Qt::ApplicationModal);
progress.show();
QList<PlayListItem *> new_items;
QListIterator<QUrl> urls_iterator(urls);
while (urls_iterator.hasNext()) {
QUrl url = urls_iterator.next();
if (url.isLocalFile()) {
QString extension = url.fileName().split(".").last().toLower();
if (extension == "m3u") {
new_items += urlsToItems(m3uToUrls(url));
} else {
new_items += urlToItems(url);
}
}
progress.setValue(progress.value() + 1);
if (progress.wasCanceled())
break;
}
progress.setValue(urls.count());
appendItems(new_items);
}
void PlayListModel::appendItems(QList<PlayListItem *> new_items)
{
if (new_items.length() > 0) {
beginInsertRows(QModelIndex(), new_items.count(), new_items.count() + new_items.size()-1);
items += new_items;
endInsertRows();
}
}
QList<PlayListItem *> PlayListModel::urlToItems(QUrl url)
{
QList<PlayListItem *> new_items;
if (url.isLocalFile()) {
QFileInfo f(url.path());
if (f.isDir()) {
QDirIterator iterator(url.path(), QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
if (!iterator.fileInfo().isDir()) {
new_items += urlToItems(QUrl::fromLocalFile(iterator.filePath()));
}
}
} else {
PlayListItem *p = new PlayListItem(url);
if (p->isValid()) {
p->ensureHistogram();
new_items += p;
}
}
} else {
new_items += new PlayListItem(url);
}
return new_items;
}
QList<PlayListItem *> PlayListModel::urlsToItems(QList<QUrl> urls)
{
QList<PlayListItem *> items;
QListIterator<QUrl> urls_iterator(urls);
while (urls_iterator.hasNext()) {
items += urlToItems(urls_iterator.next());
}
return items;
}
QList<QUrl> PlayListModel::m3uToUrls(QUrl url)
{
QList<QUrl> urls;
QFile m3u_file(url.path());
if (m3u_file.open(QFile::ReadOnly)) {
while (true) {
QByteArray line = m3u_file.readLine();
line.chop(1);
if (line.length() == 0) break;
if (line.at(0) == '#') continue;
urls.append(QUrl::fromUserInput(line));
}
}
return urls;
}