xref: /haiku/src/apps/mediaplayer/playlist/Playlist.cpp (revision 68d37cfb3a755a7270d772b505ee15c8b18aa5e0)
1 /*
2  * Playlist.cpp - Media Player for the Haiku Operating System
3  *
4  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5  * Copyright (C) 2007-2009 Stephan Aßmus <superstippi@gmx.de> (MIT ok)
6  * Copyright (C) 2008-2009 Fredrik Modéen <[FirstName]@[LastName].se> (MIT ok)
7  *
8  * Released under the terms of the MIT license.
9  */
10 
11 
12 #include "Playlist.h"
13 
14 #include <debugger.h>
15 #include <new>
16 #include <stdio.h>
17 #include <strings.h>
18 
19 #include <AppFileInfo.h>
20 #include <Application.h>
21 #include <Autolock.h>
22 #include <Directory.h>
23 #include <Entry.h>
24 #include <File.h>
25 #include <Message.h>
26 #include <Mime.h>
27 #include <NodeInfo.h>
28 #include <Path.h>
29 #include <Roster.h>
30 #include <String.h>
31 
32 #include <QueryFile.h>
33 
34 #include "FilePlaylistItem.h"
35 #include "FileReadWrite.h"
36 #include "MainApp.h"
37 
38 using std::nothrow;
39 
40 // TODO: using BList for objects is bad, replace it with a template
41 
42 Playlist::Listener::Listener() {}
43 Playlist::Listener::~Listener() {}
44 void Playlist::Listener::ItemAdded(PlaylistItem* item, int32 index) {}
45 void Playlist::Listener::ItemRemoved(int32 index) {}
46 void Playlist::Listener::ItemsSorted() {}
47 void Playlist::Listener::CurrentItemChanged(int32 newIndex, bool play) {}
48 void Playlist::Listener::ImportFailed() {}
49 
50 
51 // #pragma mark -
52 
53 
54 static void
55 make_item_compare_string(const PlaylistItem* item, char* buffer,
56 	size_t bufferSize)
57 {
58 	// TODO: Maybe "location" would be useful here as well.
59 //	snprintf(buffer, bufferSize, "%s - %s - %0*ld - %s",
60 //		item->Author().String(),
61 //		item->Album().String(),
62 //		3, item->TrackNumber(),
63 //		item->Title().String());
64 	snprintf(buffer, bufferSize, "%s", item->LocationURI().String());
65 }
66 
67 
68 static int
69 playlist_item_compare(const void* _item1, const void* _item2)
70 {
71 	// compare complete path
72 	const PlaylistItem* item1 = *(const PlaylistItem**)_item1;
73 	const PlaylistItem* item2 = *(const PlaylistItem**)_item2;
74 
75 	static const size_t bufferSize = 1024;
76 	char string1[bufferSize];
77 	make_item_compare_string(item1, string1, bufferSize);
78 	char string2[bufferSize];
79 	make_item_compare_string(item2, string2, bufferSize);
80 
81 	return strcmp(string1, string2);
82 }
83 
84 
85 // #pragma mark -
86 
87 
88 Playlist::Playlist()
89 	:
90 	BLocker("playlist lock"),
91 	fItems(),
92  	fCurrentIndex(-1)
93 {
94 }
95 
96 
97 Playlist::~Playlist()
98 {
99 	MakeEmpty();
100 
101 	if (fListeners.CountItems() > 0)
102 		debugger("Playlist::~Playlist() - there are still listeners attached!");
103 }
104 
105 
106 // #pragma mark - archiving
107 
108 
109 static const char* kItemArchiveKey = "item";
110 
111 
112 status_t
113 Playlist::Unarchive(const BMessage* archive)
114 {
115 	if (archive == NULL)
116 		return B_BAD_VALUE;
117 
118 	MakeEmpty();
119 
120 	BMessage itemArchive;
121 	for (int32 i = 0;
122 		archive->FindMessage(kItemArchiveKey, i, &itemArchive) == B_OK; i++) {
123 
124 		BArchivable* archivable = instantiate_object(&itemArchive);
125 		PlaylistItem* item = dynamic_cast<PlaylistItem*>(archivable);
126 		if (!item) {
127 			delete archivable;
128 			continue;
129 		}
130 
131 		if (!AddItem(item)) {
132 			delete item;
133 			return B_NO_MEMORY;
134 		}
135 	}
136 
137 	return B_OK;
138 }
139 
140 
141 status_t
142 Playlist::Archive(BMessage* into) const
143 {
144 	if (into == NULL)
145 		return B_BAD_VALUE;
146 
147 	int32 count = CountItems();
148 	for (int32 i = 0; i < count; i++) {
149 		const PlaylistItem* item = ItemAtFast(i);
150 		BMessage itemArchive;
151 		status_t ret = item->Archive(&itemArchive);
152 		if (ret != B_OK)
153 			return ret;
154 		ret = into->AddMessage(kItemArchiveKey, &itemArchive);
155 		if (ret != B_OK)
156 			return ret;
157 	}
158 
159 	return B_OK;
160 }
161 
162 
163 const uint32 kPlaylistMagicBytes = 'MPPL';
164 const char* kTextPlaylistMimeString = "text/x-playlist";
165 const char* kBinaryPlaylistMimeString = "application/x-vnd.haiku-playlist";
166 
167 status_t
168 Playlist::Unflatten(BDataIO* stream)
169 {
170 	if (stream == NULL)
171 		return B_BAD_VALUE;
172 
173 	uint32 magicBytes;
174 	ssize_t read = stream->Read(&magicBytes, 4);
175 	if (read != 4) {
176 		if (read < 0)
177 			return (status_t)read;
178 		return B_IO_ERROR;
179 	}
180 
181 	if (B_LENDIAN_TO_HOST_INT32(magicBytes) != kPlaylistMagicBytes)
182 		return B_BAD_VALUE;
183 
184 	BMessage archive;
185 	status_t ret = archive.Unflatten(stream);
186 	if (ret != B_OK)
187 		return ret;
188 
189 	return Unarchive(&archive);
190 }
191 
192 
193 status_t
194 Playlist::Flatten(BDataIO* stream) const
195 {
196 	if (stream == NULL)
197 		return B_BAD_VALUE;
198 
199 	BMessage archive;
200 	status_t ret = Archive(&archive);
201 	if (ret != B_OK)
202 		return ret;
203 
204 	uint32 magicBytes = B_HOST_TO_LENDIAN_INT32(kPlaylistMagicBytes);
205 	ssize_t written = stream->Write(&magicBytes, 4);
206 	if (written != 4) {
207 		if (written < 0)
208 			return (status_t)written;
209 		return B_IO_ERROR;
210 	}
211 
212 	return archive.Flatten(stream);
213 }
214 
215 
216 // #pragma mark - list access
217 
218 
219 void
220 Playlist::MakeEmpty(bool deleteItems)
221 {
222 	int32 count = CountItems();
223 	for (int32 i = count - 1; i >= 0; i--) {
224 		PlaylistItem* item = RemoveItem(i, false);
225 		_NotifyItemRemoved(i);
226 		if (deleteItems)
227 			item->ReleaseReference();
228 	}
229 	SetCurrentItemIndex(-1);
230 }
231 
232 
233 int32
234 Playlist::CountItems() const
235 {
236 	return fItems.CountItems();
237 }
238 
239 
240 bool
241 Playlist::IsEmpty() const
242 {
243 	return fItems.IsEmpty();
244 }
245 
246 
247 void
248 Playlist::Sort()
249 {
250 	fItems.SortItems(playlist_item_compare);
251 	_NotifyItemsSorted();
252 }
253 
254 
255 bool
256 Playlist::AddItem(PlaylistItem* item)
257 {
258 	return AddItem(item, CountItems());
259 }
260 
261 
262 bool
263 Playlist::AddItem(PlaylistItem* item, int32 index)
264 {
265 	if (!fItems.AddItem(item, index))
266 		return false;
267 
268 	if (index <= fCurrentIndex)
269 		SetCurrentItemIndex(fCurrentIndex + 1, false);
270 
271 	_NotifyItemAdded(item, index);
272 
273 	return true;
274 }
275 
276 
277 bool
278 Playlist::AdoptPlaylist(Playlist& other)
279 {
280 	return AdoptPlaylist(other, CountItems());
281 }
282 
283 
284 bool
285 Playlist::AdoptPlaylist(Playlist& other, int32 index)
286 {
287 	if (&other == this)
288 		return false;
289 	// NOTE: this is not intended to merge two "equal" playlists
290 	// the given playlist is assumed to be a temporary "dummy"
291 	if (fItems.AddList(&other.fItems, index)) {
292 		// take care of the notifications
293 		int32 count = other.CountItems();
294 		for (int32 i = index; i < index + count; i++) {
295 			PlaylistItem* item = ItemAtFast(i);
296 			_NotifyItemAdded(item, i);
297 		}
298 		if (index <= fCurrentIndex)
299 			SetCurrentItemIndex(fCurrentIndex + count);
300 		// empty the other list, so that the PlaylistItems are now ours
301 		other.fItems.MakeEmpty();
302 		return true;
303 	}
304 	return false;
305 }
306 
307 
308 PlaylistItem*
309 Playlist::RemoveItem(int32 index, bool careAboutCurrentIndex)
310 {
311 	PlaylistItem* item = (PlaylistItem*)fItems.RemoveItem(index);
312 	if (!item)
313 		return NULL;
314 	_NotifyItemRemoved(index);
315 
316 	if (careAboutCurrentIndex) {
317 		// fCurrentIndex isn't in sync yet, so might be one too large (if the
318 		// removed item was above the currently playing item).
319 		if (index < fCurrentIndex)
320 			SetCurrentItemIndex(fCurrentIndex - 1, false);
321 		else if (index == fCurrentIndex) {
322 			if (fCurrentIndex == CountItems())
323 				fCurrentIndex--;
324 			SetCurrentItemIndex(fCurrentIndex, true);
325 		}
326 	}
327 
328 	return item;
329 }
330 
331 
332 int32
333 Playlist::IndexOf(PlaylistItem* item) const
334 {
335 	return fItems.IndexOf(item);
336 }
337 
338 
339 PlaylistItem*
340 Playlist::ItemAt(int32 index) const
341 {
342 	return (PlaylistItem*)fItems.ItemAt(index);
343 }
344 
345 
346 PlaylistItem*
347 Playlist::ItemAtFast(int32 index) const
348 {
349 	return (PlaylistItem*)fItems.ItemAtFast(index);
350 }
351 
352 
353 // #pragma mark - navigation
354 
355 
356 bool
357 Playlist::SetCurrentItemIndex(int32 index, bool notify)
358 {
359 	bool result = true;
360 	if (index >= CountItems()) {
361 		index = CountItems() - 1;
362 		result = false;
363 		notify = false;
364 	}
365 	if (index < 0) {
366 		index = -1;
367 		result = false;
368 	}
369 	if (index == fCurrentIndex && !notify)
370 		return result;
371 
372 	fCurrentIndex = index;
373 	_NotifyCurrentItemChanged(fCurrentIndex, notify);
374 	return result;
375 }
376 
377 
378 int32
379 Playlist::CurrentItemIndex() const
380 {
381 	return fCurrentIndex;
382 }
383 
384 
385 void
386 Playlist::GetSkipInfo(bool* canSkipPrevious, bool* canSkipNext) const
387 {
388 	if (canSkipPrevious)
389 		*canSkipPrevious = fCurrentIndex > 0;
390 	if (canSkipNext)
391 		*canSkipNext = fCurrentIndex < CountItems() - 1;
392 }
393 
394 
395 // pragma mark -
396 
397 
398 bool
399 Playlist::AddListener(Listener* listener)
400 {
401 	BAutolock _(this);
402 	if (listener && !fListeners.HasItem(listener))
403 		return fListeners.AddItem(listener);
404 	return false;
405 }
406 
407 
408 void
409 Playlist::RemoveListener(Listener* listener)
410 {
411 	BAutolock _(this);
412 	fListeners.RemoveItem(listener);
413 }
414 
415 
416 // #pragma mark - support
417 
418 
419 void
420 Playlist::AppendItems(const BMessage* refsReceivedMessage, int32 appendIndex,
421 					  bool sortItems)
422 {
423 	// the playlist is replaced by the refs in the message
424 	// or the refs are appended at the appendIndex
425 	// in the existing playlist
426 	if (appendIndex == APPEND_INDEX_APPEND_LAST)
427 		appendIndex = CountItems();
428 
429 	bool add = appendIndex != APPEND_INDEX_REPLACE_PLAYLIST;
430 
431 	if (!add)
432 		MakeEmpty();
433 
434 	bool startPlaying = CountItems() == 0;
435 
436 	Playlist temporaryPlaylist;
437 	Playlist* playlist = add ? &temporaryPlaylist : this;
438 	bool hasSavedPlaylist = false;
439 
440 	// TODO: This is not very fair, we should abstract from
441 	// entry ref representation and support more URLs.
442 	BMessage archivedUrl;
443 	if (refsReceivedMessage->FindMessage("mediaplayer:url", &archivedUrl)
444 			== B_OK) {
445 		BUrl url(&archivedUrl);
446 		AddItem(new UrlPlaylistItem(url));
447 	}
448 
449 	entry_ref ref;
450 	int32 subAppendIndex = CountItems();
451 	for (int i = 0; refsReceivedMessage->FindRef("refs", i, &ref) == B_OK;
452 			i++) {
453 		Playlist subPlaylist;
454 		BString type = _MIMEString(&ref);
455 
456 		if (_IsPlaylist(type)) {
457 			AppendPlaylistToPlaylist(ref, &subPlaylist);
458 			// Do not sort the whole playlist anymore, as that
459 			// will screw up the ordering in the saved playlist.
460 			hasSavedPlaylist = true;
461 		} else {
462 			if (_IsQuery(type))
463 				AppendQueryToPlaylist(ref, &subPlaylist);
464 			else if (_IsM3u(ref))
465 				AppendM3uToPlaylist(ref, &subPlaylist);
466 			else {
467 				if (!_ExtraMediaExists(this, ref)) {
468 					AppendToPlaylistRecursive(ref, &subPlaylist);
469 				}
470 			}
471 
472 			// At least sort this subsection of the playlist
473 			// if the whole playlist is not sorted anymore.
474 			if (sortItems && hasSavedPlaylist)
475 				subPlaylist.Sort();
476 		}
477 
478 		if (!subPlaylist.IsEmpty()) {
479 			// Add to recent documents
480 			be_roster->AddToRecentDocuments(&ref, kAppSig);
481 		}
482 
483 		int32 subPlaylistCount = subPlaylist.CountItems();
484 		AdoptPlaylist(subPlaylist, subAppendIndex);
485 		subAppendIndex += subPlaylistCount;
486 	}
487 
488 	if (sortItems)
489 		playlist->Sort();
490 
491 	if (add)
492 		AdoptPlaylist(temporaryPlaylist, appendIndex);
493 
494 	if (startPlaying) {
495 		// open first file
496 		SetCurrentItemIndex(0);
497 	}
498 }
499 
500 
501 /*static*/ void
502 Playlist::AppendToPlaylistRecursive(const entry_ref& ref, Playlist* playlist)
503 {
504 	// recursively append the ref (dive into folders)
505 	BEntry entry(&ref, true);
506 	if (entry.InitCheck() != B_OK || !entry.Exists())
507 		return;
508 
509 	if (entry.IsDirectory()) {
510 		BDirectory dir(&entry);
511 		if (dir.InitCheck() != B_OK)
512 			return;
513 
514 		entry.Unset();
515 
516 		entry_ref subRef;
517 		while (dir.GetNextRef(&subRef) == B_OK) {
518 			AppendToPlaylistRecursive(subRef, playlist);
519 		}
520 	} else if (entry.IsFile()) {
521 		BString mimeString = _MIMEString(&ref);
522 		if (_IsMediaFile(mimeString)) {
523 			PlaylistItem* item = new (std::nothrow) FilePlaylistItem(ref);
524 			if (!_ExtraMediaExists(playlist, ref)) {
525 				_BindExtraMedia(item);
526 				if (item != NULL && !playlist->AddItem(item))
527 					delete item;
528 			} else
529 				delete item;
530 		} else
531 			printf("MIME Type = %s\n", mimeString.String());
532 	}
533 }
534 
535 
536 /*static*/ void
537 Playlist::AppendPlaylistToPlaylist(const entry_ref& ref, Playlist* playlist)
538 {
539 	BEntry entry(&ref, true);
540 	if (entry.InitCheck() != B_OK || !entry.Exists())
541 		return;
542 
543 	BString mimeString = _MIMEString(&ref);
544 	if (_IsTextPlaylist(mimeString)) {
545 		//printf("RunPlaylist thing\n");
546 		BFile file(&ref, B_READ_ONLY);
547 		FileReadWrite lineReader(&file);
548 
549 		BString str;
550 		entry_ref refPath;
551 		status_t err;
552 		BPath path;
553 		while (lineReader.Next(str)) {
554 			str = str.RemoveFirst("file://");
555 			str = str.RemoveLast("..");
556 			path = BPath(str.String());
557 			printf("Line %s\n", path.Path());
558 			if (path.Path() != NULL) {
559 				if ((err = get_ref_for_path(path.Path(), &refPath)) == B_OK) {
560 					PlaylistItem* item
561 						= new (std::nothrow) FilePlaylistItem(refPath);
562 					if (item == NULL || !playlist->AddItem(item))
563 						delete item;
564 				} else {
565 					printf("Error - %s: [%" B_PRIx32 "]\n", strerror(err),
566 						err);
567 				}
568 			} else
569 				printf("Error - No File Found in playlist\n");
570 		}
571 	} else if (_IsBinaryPlaylist(mimeString)) {
572 		BFile file(&ref, B_READ_ONLY);
573 		Playlist temp;
574 		if (temp.Unflatten(&file) == B_OK)
575 			playlist->AdoptPlaylist(temp, playlist->CountItems());
576 	}
577 }
578 
579 
580 /*static*/ void
581 Playlist::AppendM3uToPlaylist(const entry_ref& ref, Playlist* playlist)
582 {
583 	BFile file(&ref, B_READ_ONLY);
584 	FileReadWrite lineReader(&file);
585 
586 	BString line;
587 	while (lineReader.Next(line)) {
588 		if (line.FindFirst("#") != 0) {
589 			BPath path(line.String());
590 			entry_ref refPath;
591 			status_t err;
592 
593 			if ((err = get_ref_for_path(path.Path(), &refPath)) == B_OK) {
594 				PlaylistItem* item
595 					= new (std::nothrow) FilePlaylistItem(refPath);
596 				if (item == NULL || !playlist->AddItem(item))
597 					delete item;
598 			} else {
599 				printf("Error - %s: [%" B_PRIx32 "]\n", strerror(err), err);
600 			}
601 		}
602 
603 		line.Truncate(0);
604 	}
605 }
606 
607 
608 /*static*/ void
609 Playlist::AppendQueryToPlaylist(const entry_ref& ref, Playlist* playlist)
610 {
611 	BQueryFile query(&ref);
612 	if (query.InitCheck() != B_OK)
613 		return;
614 
615 	entry_ref foundRef;
616 	while (query.GetNextRef(&foundRef) == B_OK) {
617 		PlaylistItem* item = new (std::nothrow) FilePlaylistItem(foundRef);
618 		if (item == NULL || !playlist->AddItem(item))
619 			delete item;
620 	}
621 }
622 
623 
624 void
625 Playlist::NotifyImportFailed()
626 {
627 	BAutolock _(this);
628 	_NotifyImportFailed();
629 }
630 
631 
632 /*static*/ bool
633 Playlist::ExtraMediaExists(Playlist* playlist, PlaylistItem* item)
634 {
635 	FilePlaylistItem* fileItem = dynamic_cast<FilePlaylistItem*>(item);
636 	if (fileItem != NULL)
637 		return _ExtraMediaExists(playlist, fileItem->Ref());
638 
639 	// If we are here let's see if it is an url
640 	UrlPlaylistItem* urlItem = dynamic_cast<UrlPlaylistItem*>(item);
641 	if (urlItem == NULL)
642 		return true;
643 
644 	return _ExtraMediaExists(playlist, urlItem->Url());
645 }
646 
647 
648 // #pragma mark - private
649 
650 
651 /*static*/ bool
652 Playlist::_ExtraMediaExists(Playlist* playlist, const entry_ref& ref)
653 {
654 	BString exceptExtension = _GetExceptExtension(BPath(&ref).Path());
655 
656 	for (int32 i = 0; i < playlist->CountItems(); i++) {
657 		FilePlaylistItem* compare = dynamic_cast<FilePlaylistItem*>(playlist->ItemAt(i));
658 		if (compare == NULL)
659 			continue;
660 		if (compare->Ref() != ref
661 				&& _GetExceptExtension(BPath(&compare->Ref()).Path()) == exceptExtension )
662 			return true;
663 	}
664 	return false;
665 }
666 
667 
668 /*static*/ bool
669 Playlist::_ExtraMediaExists(Playlist* playlist, BUrl url)
670 {
671 	for (int32 i = 0; i < playlist->CountItems(); i++) {
672 		UrlPlaylistItem* compare
673 			= dynamic_cast<UrlPlaylistItem*>(playlist->ItemAt(i));
674 		if (compare == NULL)
675 			continue;
676 		if (compare->Url() == url)
677 			return true;
678 	}
679 	return false;
680 }
681 
682 
683 /*static*/ bool
684 Playlist::_IsImageFile(const BString& mimeString)
685 {
686 	BMimeType superType;
687 	BMimeType fileType(mimeString.String());
688 
689 	if (fileType.GetSupertype(&superType) != B_OK)
690 		return false;
691 
692 	if (superType == "image")
693 		return true;
694 
695 	return false;
696 }
697 
698 
699 /*static*/ bool
700 Playlist::_IsMediaFile(const BString& mimeString)
701 {
702 	BMimeType superType;
703 	BMimeType fileType(mimeString.String());
704 
705 	if (fileType.GetSupertype(&superType) != B_OK)
706 		return false;
707 
708 	// try a shortcut first
709 	if (superType == "audio" || superType == "video")
710 		return true;
711 
712 	// Look through our supported types
713 	app_info appInfo;
714 	if (be_app->GetAppInfo(&appInfo) != B_OK)
715 		return false;
716 	BFile appFile(&appInfo.ref, B_READ_ONLY);
717 	if (appFile.InitCheck() != B_OK)
718 		return false;
719 	BMessage types;
720 	BAppFileInfo appFileInfo(&appFile);
721 	if (appFileInfo.GetSupportedTypes(&types) != B_OK)
722 		return false;
723 
724 	const char* type;
725 	for (int32 i = 0; types.FindString("types", i, &type) == B_OK; i++) {
726 		if (strcasecmp(mimeString.String(), type) == 0)
727 			return true;
728 	}
729 
730 	return false;
731 }
732 
733 
734 /*static*/ bool
735 Playlist::_IsTextPlaylist(const BString& mimeString)
736 {
737 	return mimeString.Compare(kTextPlaylistMimeString) == 0;
738 }
739 
740 
741 /*static*/ bool
742 Playlist::_IsBinaryPlaylist(const BString& mimeString)
743 {
744 	return mimeString.Compare(kBinaryPlaylistMimeString) == 0;
745 }
746 
747 
748 /*static*/ bool
749 Playlist::_IsPlaylist(const BString& mimeString)
750 {
751 	return _IsTextPlaylist(mimeString) || _IsBinaryPlaylist(mimeString);
752 }
753 
754 
755 /*static*/ bool
756 Playlist::_IsM3u(const entry_ref& ref)
757 {
758 	BString path(BPath(&ref).Path());
759 	return path.FindLast(".m3u") == path.CountChars() - 4
760 		|| path.FindLast(".m3u8") == path.CountChars() - 5;
761 }
762 
763 
764 /*static*/ bool
765 Playlist::_IsQuery(const BString& mimeString)
766 {
767 	return mimeString.Compare(BQueryFile::MimeType()) == 0;
768 }
769 
770 
771 /*static*/ BString
772 Playlist::_MIMEString(const entry_ref* ref)
773 {
774 	BFile file(ref, B_READ_ONLY);
775 	BNodeInfo nodeInfo(&file);
776 	char mimeString[B_MIME_TYPE_LENGTH];
777 	if (nodeInfo.GetType(mimeString) != B_OK) {
778 		BMimeType type;
779 		if (BMimeType::GuessMimeType(ref, &type) != B_OK)
780 			return BString();
781 
782 		strlcpy(mimeString, type.Type(), B_MIME_TYPE_LENGTH);
783 		nodeInfo.SetType(type.Type());
784 	}
785 	return BString(mimeString);
786 }
787 
788 
789 // _BindExtraMedia() searches additional videos and audios
790 // and addes them as extra medias.
791 /*static*/ void
792 Playlist::_BindExtraMedia(PlaylistItem* item)
793 {
794 	FilePlaylistItem* fileItem = dynamic_cast<FilePlaylistItem*>(item);
795 	if (!fileItem)
796 		return;
797 
798 	// If the media file is foo.mp3, _BindExtraMedia() searches foo.avi.
799 	BPath mediaFilePath(&fileItem->Ref());
800 	BString mediaFilePathString = mediaFilePath.Path();
801 	BPath dirPath;
802 	mediaFilePath.GetParent(&dirPath);
803 	BDirectory dir(dirPath.Path());
804 	if (dir.InitCheck() != B_OK)
805 		return;
806 
807 	BEntry entry;
808 	BString entryPathString;
809 	while (dir.GetNextEntry(&entry, true) == B_OK) {
810 		if (!entry.IsFile())
811 			continue;
812 		entryPathString = BPath(&entry).Path();
813 		if (entryPathString != mediaFilePathString
814 				&& _GetExceptExtension(entryPathString) == _GetExceptExtension(mediaFilePathString)) {
815 			_BindExtraMedia(fileItem, entry);
816 		}
817 	}
818 }
819 
820 
821 /*static*/ void
822 Playlist::_BindExtraMedia(FilePlaylistItem* fileItem, const BEntry& entry)
823 {
824 	entry_ref ref;
825 	entry.GetRef(&ref);
826 	BString mimeString = _MIMEString(&ref);
827 	if (_IsMediaFile(mimeString)) {
828 		fileItem->AddRef(ref);
829 	} else if (_IsImageFile(mimeString)) {
830 		fileItem->AddImageRef(ref);
831 	}
832 }
833 
834 
835 /*static*/ BString
836 Playlist::_GetExceptExtension(const BString& path)
837 {
838 	int32 periodPos = path.FindLast('.');
839 	if (periodPos <= path.FindLast('/'))
840 		return path;
841 	return BString(path.String(), periodPos);
842 }
843 
844 
845 // #pragma mark - notifications
846 
847 
848 void
849 Playlist::_NotifyItemAdded(PlaylistItem* item, int32 index) const
850 {
851 	BList listeners(fListeners);
852 	int32 count = listeners.CountItems();
853 	for (int32 i = 0; i < count; i++) {
854 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
855 		listener->ItemAdded(item, index);
856 	}
857 }
858 
859 
860 void
861 Playlist::_NotifyItemRemoved(int32 index) const
862 {
863 	BList listeners(fListeners);
864 	int32 count = listeners.CountItems();
865 	for (int32 i = 0; i < count; i++) {
866 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
867 		listener->ItemRemoved(index);
868 	}
869 }
870 
871 
872 void
873 Playlist::_NotifyItemsSorted() const
874 {
875 	BList listeners(fListeners);
876 	int32 count = listeners.CountItems();
877 	for (int32 i = 0; i < count; i++) {
878 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
879 		listener->ItemsSorted();
880 	}
881 }
882 
883 
884 void
885 Playlist::_NotifyCurrentItemChanged(int32 newIndex, bool play) const
886 {
887 	BList listeners(fListeners);
888 	int32 count = listeners.CountItems();
889 	for (int32 i = 0; i < count; i++) {
890 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
891 		listener->CurrentItemChanged(newIndex, play);
892 	}
893 }
894 
895 
896 void
897 Playlist::_NotifyImportFailed() const
898 {
899 	BList listeners(fListeners);
900 	int32 count = listeners.CountItems();
901 	for (int32 i = 0; i < count; i++) {
902 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
903 		listener->ImportFailed();
904 	}
905 }
906