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
|
/***************************************************************************
begin : Thu Aug 19 2004
copyright : (C) 2002 - 2004 by Michael Pyne
email : michael.pyne@kde.org
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <kaction.h>
#include <kapplication.h>
#include <kdebug.h>
#include "tracksequenceiterator.h"
#include "playlist.h"
#include "actioncollection.h"
#include "tag.h"
#include "filehandle.h"
using namespace ActionCollection;
TrackSequenceIterator::TrackSequenceIterator() :
m_current(0)
{
}
TrackSequenceIterator::TrackSequenceIterator(const TrackSequenceIterator &other) :
m_current(other.m_current)
{
}
TrackSequenceIterator::~TrackSequenceIterator()
{
}
void TrackSequenceIterator::setCurrent(PlaylistItem *current)
{
m_current = current;
}
void TrackSequenceIterator::playlistChanged()
{
}
void TrackSequenceIterator::itemAboutToDie(const PlaylistItem *)
{
}
DefaultSequenceIterator::DefaultSequenceIterator() :
TrackSequenceIterator()
{
}
DefaultSequenceIterator::DefaultSequenceIterator(const DefaultSequenceIterator &other)
: TrackSequenceIterator(other)
{
}
DefaultSequenceIterator::~DefaultSequenceIterator()
{
}
void DefaultSequenceIterator::advance()
{
if(!current())
return;
bool isRandom = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
bool loop = action("loopPlaylist") && action<KToggleAction>("loopPlaylist")->isChecked();
bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();
if(isRandom || albumRandom) {
if(m_randomItems.isEmpty() && loop) {
// Since refillRandomList will remove the currently playing item,
// we should clear it out first since that's not good for e.g.
// lists with 1-2 items. We need to remember the Playlist though.
Playlist *playlist = current()->playlist();
setCurrent(0);
refillRandomList(playlist);
}
if(m_randomItems.isEmpty()) {
setCurrent(0);
return;
}
PlaylistItem *item;
if(albumRandom) {
if(m_albumSearch.isNull() || m_albumSearch.matchedItems().isEmpty()) {
item = m_randomItems[TDEApplication::random() % m_randomItems.count()];
initAlbumSearch(item);
}
// This can be null if initAlbumSearch() left the m_albumSearch
// empty because the album text was empty. Since we initAlbumSearch()
// with an item, the matchedItems() should never be empty.
if(!m_albumSearch.isNull()) {
PlaylistItemList albumMatches = m_albumSearch.matchedItems();
if(albumMatches.isEmpty()) {
kdError(65432) << "Unable to initialize album random play.\n";
kdError(65432) << "List of potential results is empty.\n";
return; // item is still set to random song from a few lines earlier.
}
item = albumMatches[0];
// Pick first song remaining in list.
for(unsigned i = 0; i < albumMatches.count(); ++i)
if(albumMatches[i]->file().tag()->track() < item->file().tag()->track())
item = albumMatches[i];
m_albumSearch.clearItem(item);
if(m_albumSearch.matchedItems().isEmpty()) {
m_albumSearch.clearComponents();
m_albumSearch.search();
}
}
else
kdError(65432) << "Unable to perform album random play on " << *item << endl;
}
else
item = m_randomItems[TDEApplication::random() % m_randomItems.count()];
setCurrent(item);
m_randomItems.remove(item);
}
else {
PlaylistItem *next = current()->itemBelow();
if(!next && loop) {
Playlist *p = current()->playlist();
next = p->firstChild();
while(next && !next->isVisible())
next = static_cast<PlaylistItem *>(next->nextSibling());
}
setCurrent(next);
}
}
void DefaultSequenceIterator::backup()
{
if(!current())
return;
PlaylistItem *item = current()->itemAbove();
if(item)
setCurrent(item);
}
void DefaultSequenceIterator::prepareToPlay(Playlist *playlist)
{
bool random = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();
if(random || albumRandom) {
PlaylistItemList items = playlist->selectedItems();
if(items.isEmpty())
items = playlist->visibleItems();
PlaylistItem *newItem = 0;
if(!items.isEmpty())
newItem = items[TDEApplication::random() % items.count()];
setCurrent(newItem);
refillRandomList();
}
else {
TQListViewItemIterator it(playlist, TQListViewItemIterator::Visible | TQListViewItemIterator::Selected);
if(!it.current())
it = TQListViewItemIterator(playlist, TQListViewItemIterator::Visible);
setCurrent(static_cast<PlaylistItem *>(it.current()));
}
}
void DefaultSequenceIterator::reset()
{
m_randomItems.clear();
m_albumSearch.clearComponents();
m_albumSearch.search();
setCurrent(0);
}
void DefaultSequenceIterator::playlistChanged()
{
refillRandomList();
}
void DefaultSequenceIterator::itemAboutToDie(const PlaylistItem *item)
{
PlaylistItem *stfu_gcc = const_cast<PlaylistItem *>(item);
m_randomItems.remove(stfu_gcc);
}
void DefaultSequenceIterator::setCurrent(PlaylistItem *current)
{
PlaylistItem *oldCurrent = DefaultSequenceIterator::current();
TrackSequenceIterator::setCurrent(current);
bool random = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();
if((albumRandom || random) && current && m_randomItems.isEmpty()) {
// We're setting a current item, refill the random list now, and remove
// the current item.
refillRandomList();
}
m_randomItems.remove(current);
if(albumRandom && current && !oldCurrent) {
// Same idea as above
initAlbumSearch(current);
m_albumSearch.clearItem(current);
}
}
DefaultSequenceIterator *DefaultSequenceIterator::clone() const
{
return new DefaultSequenceIterator(*this);
}
void DefaultSequenceIterator::refillRandomList(Playlist *p)
{
if(!p) {
if (!current())
return;
p = current()->playlist();
if(!p) {
kdError(65432) << k_funcinfo << "Item has no playlist!\n";
return;
}
}
m_randomItems = p->visibleItems();
m_randomItems.remove(current());
m_albumSearch.clearComponents();
m_albumSearch.search();
}
void DefaultSequenceIterator::initAlbumSearch(PlaylistItem *searchItem)
{
if(!searchItem)
return;
m_albumSearch.clearPlaylists();
m_albumSearch.addPlaylist(searchItem->playlist());
ColumnList columns;
m_albumSearch.setSearchMode(PlaylistSearch::MatchAll);
m_albumSearch.clearComponents();
// If the album name is empty, it will mess up the search,
// so ignore empty album names.
if(searchItem->file().tag()->album().isEmpty())
return;
columns.append(PlaylistItem::AlbumColumn);
m_albumSearch.addComponent(PlaylistSearch::Component(
searchItem->file().tag()->album(),
true,
columns,
PlaylistSearch::Component::Exact)
);
// If there is an Artist tag with the track, match against it as well
// to avoid things like multiple "Greatest Hits" albums matching the
// search.
if(!searchItem->file().tag()->artist().isEmpty()) {
kdDebug(65432) << "Searching both artist and album.\n";
columns[0] = PlaylistItem::ArtistColumn;
m_albumSearch.addComponent(PlaylistSearch::Component(
searchItem->file().tag()->artist(),
true,
columns,
PlaylistSearch::Component::Exact)
);
}
m_albumSearch.search();
}
// vim: set et sw=4 tw=0:
|