-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlaylistModel.cpp
153 lines (135 loc) · 3.73 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
#include "PlaylistModel.h"
#include "MainWindow.h"
#include "DeadbeefTranslator.h"
#include "QtGui.h"
#undef DBAPI
#define DBAPI api->deadbeef
PlaylistModel::PlaylistModel(QObject *parent, DBApi *Api) : PlayItemTableModel(parent, Api) {
connect(api, SIGNAL(playlistContentChanged(ddb_playlist_t*)),
this, SLOT(onPlaylistContentChanged(ddb_playlist_t*)));
}
PlaylistModel::PlaylistModel(ddb_playlist_t *plt, QObject *parent, DBApi *Api) : PlayItemTableModel(parent, Api) {
setPlaylist(plt);
}
PlaylistModel::~PlaylistModel() {
if (plt) {
DBAPI->plt_unref(plt);
}
}
void PlaylistModel::setPlaylist(ddb_playlist_t *plt_new) {
beginResetModel();
if (plt) {
DBAPI->plt_unref(plt);
}
this->plt = plt_new;
if (plt) {
DBAPI->plt_ref(plt);
}
endResetModel();
}
void PlaylistModel::onPlaylistContentChanged(ddb_playlist_t *plt_changed) {
if (plt == plt_changed) {
// refresh
setPlaylist(plt);
}
}
int PlaylistModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid()) {
qDebug() << "PlaylistModel: rowCount (err)";
return 0;
}
if (plt) {
DBAPI->pl_lock ();
int rowCount = DBAPI->plt_get_item_count(plt, m_iter);
DBAPI->pl_unlock ();
return rowCount;
}
qDebug() << (void *)this << "PlaylistModel: rowCount (err2)";
return 0;
}
playItemList PlaylistModel::tracks(const QList<int> &tracks) const {
if (tracks.length() == 0 || m_playlistLock)
return playItemList();
playItemList list;
foreach(int t, tracks) {
list.append(DBAPI->plt_get_item_for_idx(plt, t, m_iter));
}
return list;
}
void PlaylistModel::sort(int n, Qt::SortOrder order) {
if (m_playlistLock) {
return;
}
if (plt) {
beginResetModel();
DBAPI->plt_sort_v2(plt, m_iter, -1, getHeaderFormat(n).toUtf8(), order);
endResetModel();
}
}
void PlaylistModel::insertTracks(playItemList *l, int after) {
DB_playItem_t *it;
if (after == -1) {
it = nullptr;
}
else if (after == -2) {
it = DBAPI->plt_get_last(plt,m_iter);
}
else {
if (after > DBAPI->plt_get_item_count(plt, m_iter)) {
return;
}
it = DBAPI->plt_get_item_for_idx(plt,after,m_iter);
}
DB_playItem_t *iter = it;
beginInsertRows(QModelIndex(),after, l->length());
foreach(DB_playItem_t *i, *l) {
DB_playItem_t *inserted = DBAPI->plt_insert_item(plt,iter,i);
iter = inserted;
}
if (it) {
DBAPI->pl_item_unref(it);
}
endInsertRows();
}
void PlaylistModel::moveIndexes(QList<int> ind, int after) {
// qDebug() << "moveItems:" << ind.length() << "items to be put after" << after;
// DeaDBeeF requires uint32_t array
uint32_t inds[ind.length()];
int i = 0;
foreach(int d, ind) {
inds[i] = d;
i++;
}
int lastItem = DBAPI->plt_get_item_count(plt, m_iter) - 1;
if (after == -2) {
after = lastItem;
}
DB_playItem_t *bef;
if (after == -1) {
bef = DBAPI->plt_get_first(plt,m_iter);
}
else {
if (after > lastItem) {
return;
}
bef = DBAPI->plt_get_item_for_idx(plt, after+1, m_iter);
}
// TODO: use beginMoveRows?
beginResetModel();
DBAPI->pl_lock();
DBAPI->plt_move_items(plt, m_iter, plt, bef, inds, ind.length());
if (bef) {
DBAPI->pl_item_unref(bef);
}
DBAPI->pl_unlock();
endResetModel();
}
void PlaylistModel::removeIndexes(QList<int> ind) {
playItemList l = tracks(ind);
// TODO: use beginRemoveRows?
beginResetModel();
foreach(DB_playItem_t *it, l) {
DBAPI->plt_remove_item(plt,it);
}
endResetModel();
}