summaryrefslogtreecommitdiffstats
path: root/noatun/library/noatun/playlist.h
blob: d1baa25d73767205cb860670c417bb20dadc7f71 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#ifndef NOATUNPLAYLIST_H
#define NOATUNPLAYLIST_H

#include <tqobject.h>
#include <kurl.h>
#include <tqdict.h>
#include <tqstringlist.h>
#include <cassert>
#include <kdemacros.h>

class PlaylistItem;

/**
 * If you're not coding a playlist, ignore this class.
 *
 * The backend.  Since PlaylistItemData is refcounted,
 * this contains the data, the PlaylistItem is the "reference"
 * <pre>
 * PlaylistItem m=new PlaylistItemData;
 * </pre>
 * Of course, you're supposed to inherit from PlaylistItemData
 * in your Playlist, since there are pure virtuals.
 *
 * You can create these objects on demand.
 *
 * @short Playlist item data
 * @author Charles Samuels
 * @version 2.3
 **/
class KDE_EXPORT PlaylistItemData
{
public:
	PlaylistItemData();
	virtual ~PlaylistItemData();

	/**
	 * Noatun asks your playlist for properties.  It is your
	 * responsiblity to store the information.  But usually a TQMap<TQString,TQString>
	 * is enough.
	 *
	 * If you return the default value, the default should not
	 * be written.
	 *
	 * This returns the property, or def if such a property doesn't exist
	 **/
	virtual TQString property(const TQString &key, const TQString &def=0) const=0;

	/**
	 * This sets the property with the given key and value.
	 *
	 * Important: If you use a TQMap, you'll have to remove the current
	 * item before adding a new one
	 **/
	virtual void setProperty(const TQString &key, const TQString &property)=0;

	/**
	 * remove the item with given key
	 **/
	virtual void clearProperty(const TQString &key)=0;

	/**
	 * return a list of property keys
	 **/
	virtual TQStringList properties() const=0;

	/**
	 * return whether if the given key exists
	 **/
	virtual bool isProperty(const TQString &key) const=0;

	/**
	 * return the title of the song. By default, this will
	 * use the following by default, in order of priority
	 *
	 * property("realtitle")
	 * property("title")
	 * url().filename()
	 *
	 * you shouldn't need to override this.
	 **/
	virtual TQString title() const;

	/**
	 * the true filename of the song, remote or local
	 **/
	virtual KURL url() const { return KURL(property("url")); }
	/**
	 * set the true filename of the song, remote or local
	 **/
	virtual void setUrl(const KURL &url) { setProperty("url", url.url()); }

	/**
	 * first, this checks for the property "mimetype", else
	 * it'l ask KMimeType based on file()
	 **/
	virtual TQCString mimetype() const;

	/**
	 * first, checks for the property "playObject", else,
	 * it'l ask aRts
	 **/
	virtual TQCString playObject() const;

	/**
	 * return the filename to send to the playobject
	 **/
	virtual TQString file() const { return url().path(); }

	/**
	 * what's the length of the song, in milliseconds?
	 **/
	virtual int length() const;

	/**
	 * sets the length of the song, in milliseconds
	 **/
	virtual void setLength(int ms);

	/**
	 * returns a friendly representation of the length
	 * of this file
	 **/
	TQString lengthString() const;

	/**
	 * compare yourself with the given PlaylistItemData
	 * This is implemented in the slow fashion of
	 * comparing all the properties.  You may
	 * have a much faster way of implementing this
	 * if this==&d, this will not be called, normally
	 **/
	virtual bool operator == (const PlaylistItemData &d) const;

	/**
	 * this is implemented as !(*this==d), you may have a
	 * faster way to implement this
	 **/
	virtual bool operator != (const PlaylistItemData &d) const;

	/**
	 * remove this item from the list
	 **/
	virtual void remove() = 0;


	/**
	 * Playlists should not download files if this is true
	 **/
	bool streamable() const  { return isProperty("stream_"); }

public:
	/**
	 * Call this when you want to signal
	 * the given item has been added to the list
	 **/
	void added();

	/**
	 * Your playlist must call this when the file
	 * is removed from the playlist
	 **/
	void removed();

	/**
	 * Your playlist must call this when the file
	 * is modified
	 **/
	void modified();


public: // reference counting
	/**
	 * Have the reference counter never delete this
	 *
	 * This is useful for when you want to keep all
	 * your items hanging around
	 **/
	void addRef() { mRefs++; }
	void removeRef()
	{
		mRefs--;
		if (!mRefs)
			delete this;
	}

private:
	mutable int mRefs;
};


/**
 * a reference to a PlaylistItem(Data)
 *
 * All methods here should have the same behavior
 * as they do for PlaylistItemData
 *
 * If you're a playlist, you should inherit
 * from PlaylistItemData
 *
 * It's client code's responsibility to ensure that
 * PlaylistItem is not null by using either the boolean
 * conversion or isNull()
 *
 * @short Playlist items
 * @author Charles Samuels
 * @version 2.3
 **/
class KDE_EXPORT PlaylistItem
{
public:
	PlaylistItem(const PlaylistItem &source);
	PlaylistItem(PlaylistItemData *source);
	PlaylistItem() : mData(0) {}
	~PlaylistItem();

	PlaylistItem &operator =(const PlaylistItem &source);
	PlaylistItem &operator =(PlaylistItemData *source);

	PlaylistItemData *data() { return mData; }
	const PlaylistItemData *data() const { return mData; }

	const PlaylistItem &operator =(const PlaylistItem &source) const;
	const PlaylistItem &operator =(const PlaylistItemData *source) const;

	operator bool() const { return (bool)mData; }
	bool isNull() const { return !(bool)mData; }

	bool operator ==(const PlaylistItem &i) const
	{
		if (data()==i.data()) return true;
		if (!data() || !i.data()) return false;
		return *i.data()==*data();
	}
	bool operator ==(const PlaylistItemData *i) const
	{
		if (data()==i) return true;
		if (!data() || !i) return false;
		return *i==*data();
	}

	bool operator !=(const PlaylistItem &i) const
		{ return ! (*this==i); }
	bool operator !=(const PlaylistItemData *i) const
		{ return ! (*this->data()==*i); }

	TQString property(const TQString &key, const TQString &def=0) const
	{
		assert(mData);
		return mData->property(key, def);
	}

	void setProperty(const TQString &key, const TQString &property)
	{
		assert(mData);
		const_cast<PlaylistItemData*>(mData)->setProperty(key, property);
	}

	void clearProperty(const TQString &key)
	{
		assert(mData);
		const_cast<PlaylistItemData*>(mData)->clearProperty(key);
	}

	TQStringList properties() const
	{
		assert(mData);
		return mData->properties();
	}

	bool isProperty(const TQString &key) const
	{
		assert(mData);
		return mData->isProperty(key);
	}

	KURL url() const { assert(mData); return mData->url(); }
	void setUrl(const KURL &url)
	{
		assert(mData);
		const_cast<PlaylistItemData*>(mData)->setUrl(url);
	}

	TQCString mimetype() const { assert(mData); return mData->mimetype(); }
	TQCString playObject() const { assert(mData); return mData->playObject(); }
	TQString file() const { assert(mData); return mData->file(); }

	TQString title() const
	{
		assert(mData);
		return mData->title();
	}

	int length() const
	{
		assert(mData);
		return mData->length();
	}

	void setLength(int ms) const
	{
		assert(mData);
		mData->setLength(ms);
	}

	TQString lengthString() const { assert(mData); return mData->lengthString(); }

	void remove() { assert(mData); mData->remove(); }

	bool streamable() const  { assert(mData); return mData->streamable(); }

private:
	// reference counting
	void removeRef() const;
	void addRef() const; // requires mData already has item

private:
	mutable PlaylistItemData *mData;
	void *_bc1, *_bc2;
};

/**
 * The playlist, which you derive from when creating
 * your own playlist.
 *
 * Do not, under any circumstances, call a Playlist method
 * when you can call a Player method, unless, of course, you
 * ARE the playlist.
 **/
class Playlist : public TQObject
{
Q_OBJECT
  TQ_OBJECT
	friend class PlaylistItemData;
public:
	Playlist(TQObject *tqparent, const char *name);
	/**
	 * on playlist unload, your playlist must
	 * have current()==0 and emit playCurrent
	 **/
	virtual ~Playlist();

	/**
	 * go to the front
	 **/
	virtual void reset()=0;

	/**
	 * empty the list
	 **/
	virtual void clear()=0;

	/**
	 * add a file
	 */
	virtual void addFile(const KURL&, bool play=false)=0;

	/**
	 * cycle forward, return that
	 **/
	virtual PlaylistItem next()=0;

	/**
	 * cycle to next section, return that
	 * defaults to return next()
	 */
	virtual PlaylistItem nextSection();

	/**
	 * cycle back, return that
	 **/
	virtual PlaylistItem previous()=0;

	/**
	 * cycle to previous section, return that
	 * defaults to return previous()
	 */
	virtual PlaylistItem previousSection();

	/**
	 * current item
	 **/
	virtual PlaylistItem current()=0;
	/**
	 * set the current item
	 **/
	virtual void setCurrent(const PlaylistItem &)=0;

	/**
	 * get the first item
	 **/
	virtual PlaylistItem getFirst() const =0;

	/**
	 * get the item after item, note that getFirst and getAfter do not
	 * have to follow play order since they are used solely to iterate
	 * over the entire collection in some order. Duplicating the play
	 * order (by looking into the future) is not necessary.
	 **/
	virtual PlaylistItem getAfter(const PlaylistItem &item) const =0;

	/**
	 * is the view visible?
	 **/

	virtual bool listVisible() const =0;

	/**
	 * do the KCmdLineArgs stuff
	 **/
	int handleArguments();

	/**
	 * return a list of songs in which at least one
	 * of the keys matches at least one of the values
	 *
	 * the default implementation will call getFirst()
	 * and getAfter() which could be potentially slow,
	 * depending how your playlist is designed.  So
	 * you're free to reimplement this if you could
	 * do better
	 *
	 * A value of "" is equal to an unset value
	 *
	 * limit is the maximum amount of items to return,
	 * or -1 if you want as many as possible
	 *
	 * if exact is true, a match is only made if
	 * the string is identical to a value.  if false
	 * a match is made if the string contains a value
	 *
	 * caseSensitive, if false, means that the given
	 * values are compared case insensitively to
	 * to the items in the playlist.  The keys
	 * are always compared with case sensitivity
	 **/
	virtual TQValueList<PlaylistItem> select(
			const TQStringList &keys, const TQStringList &values,
			int limit=-1, bool exact=false, bool caseSensitive=false
		);

	/**
	 * The default implementation will just call
	 * the above select.  Of course, you're free to implement
	 * both of these (with different mechanisms if need be)
	 * for speed
	 **/
	virtual TQValueList<PlaylistItem> select(
			const TQString &key, const TQString &value,
			int limit=-1, bool exact=false, bool caseSensitive=false
		);
	/**
	 * exactly the same as the above, except converts
	 * the const char* to TQString (utf8)
	 **/
	inline TQValueList<PlaylistItem> select(
			const char *key, const char *value,
			int limit=-1, bool exact=false, bool caseSensitive=false
		)
	{
		return select(
				TQString(key), TQString(value),
				limit, exact, caseSensitive
			);
	}

public slots:
	/**
	 * show the list!
	 **/
	virtual void showList()=0;
	/**
	 * hide it
	 **/
	virtual void hideList()=0;
	/**
	 * toggle visibility
	 **/
	virtual void toggleList();

signals:
	/**
	 * when you want the engine to reload current()
	 * This is how your playlist forces noatun to
	 * play a new song
	 **/
	void playCurrent();

	/**
	 * when the list is hidden
	 **/
	void listHidden();

	/**
	 * when the list is shown
	 **/
	void listShown();
};

/**
 * this class's methods will be called whenever
 * something happens to the playlist or its
 * items.
 *
 * If the playlist plugin changes, you don't have to do
 * anything.
 **/
class PlaylistNotifier
{
public:
	PlaylistNotifier();
	virtual ~PlaylistNotifier();

	/**
	 * a new item is added to the list
	 **/
	virtual void added(PlaylistItem &) {}

	/**
	 * an item is removed from the list
	 **/
	virtual void removed(PlaylistItem &) {}

	/**
	 * this item was modified (via a changed
	 * or added property
	 **/
	virtual void modified(PlaylistItem &) {}
};


#endif