xref: /haiku/src/apps/mediaplayer/playlist/Playlist.cpp (revision 1f52c921e27aa442370e1bd4adc021acf2b78b64)
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 {
422 	// the playlist is replaced by the refs in the message
423 	// or the refs are appended at the appendIndex
424 	// in the existing playlist
425 	if (appendIndex == APPEND_INDEX_APPEND_LAST)
426 		appendIndex = CountItems();
427 
428 	bool add = appendIndex != APPEND_INDEX_REPLACE_PLAYLIST;
429 
430 	if (!add)
431 		MakeEmpty();
432 
433 	bool startPlaying = CountItems() == 0;
434 
435 	Playlist temporaryPlaylist;
436 	Playlist* playlist = add ? &temporaryPlaylist : this;
437 	bool sortPlaylist = true;
438 
439 	// TODO: This is not very fair, we should abstract from
440 	// entry ref representation and support more URLs.
441 	BMessage archivedUrl;
442 	if (refsReceivedMessage->FindMessage("mediaplayer:url", &archivedUrl)
443 			== B_OK) {
444 		BUrl url(&archivedUrl);
445 		AddItem(new UrlPlaylistItem(url));
446 	}
447 
448 	entry_ref ref;
449 	int32 subAppendIndex = CountItems();
450 	for (int i = 0; refsReceivedMessage->FindRef("refs", i, &ref) == B_OK;
451 			i++) {
452 		Playlist subPlaylist;
453 		BString type = _MIMEString(&ref);
454 
455 		if (_IsPlaylist(type)) {
456 			AppendPlaylistToPlaylist(ref, &subPlaylist);
457 			// Do not sort the whole playlist anymore, as that
458 			// will screw up the ordering in the saved playlist.
459 			sortPlaylist = false;
460 		} else {
461 			if (_IsQuery(type))
462 				AppendQueryToPlaylist(ref, &subPlaylist);
463 			else if (_IsM3u(ref))
464 				AppendM3uToPlaylist(ref, &subPlaylist);
465 			else {
466 				if (!_ExtraMediaExists(this, ref)) {
467 					AppendToPlaylistRecursive(ref, &subPlaylist);
468 				}
469 			}
470 
471 			// At least sort this subsection of the playlist
472 			// if the whole playlist is not sorted anymore.
473 			if (!sortPlaylist)
474 				subPlaylist.Sort();
475 		}
476 
477 		if (!subPlaylist.IsEmpty()) {
478 			// Add to recent documents
479 			be_roster->AddToRecentDocuments(&ref, kAppSig);
480 		}
481 
482 		int32 subPlaylistCount = subPlaylist.CountItems();
483 		AdoptPlaylist(subPlaylist, subAppendIndex);
484 		subAppendIndex += subPlaylistCount;
485 	}
486 	if (sortPlaylist)
487 		playlist->Sort();
488 
489 	if (add)
490 		AdoptPlaylist(temporaryPlaylist, appendIndex);
491 
492 	if (startPlaying) {
493 		// open first file
494 		SetCurrentItemIndex(0);
495 	}
496 }
497 
498 
499 /*static*/ void
500 Playlist::AppendToPlaylistRecursive(const entry_ref& ref, Playlist* playlist)
501 {
502 	// recursively append the ref (dive into folders)
503 	BEntry entry(&ref, true);
504 	if (entry.InitCheck() != B_OK || !entry.Exists())
505 		return;
506 
507 	if (entry.IsDirectory()) {
508 		BDirectory dir(&entry);
509 		if (dir.InitCheck() != B_OK)
510 			return;
511 
512 		entry.Unset();
513 
514 		entry_ref subRef;
515 		while (dir.GetNextRef(&subRef) == B_OK) {
516 			AppendToPlaylistRecursive(subRef, playlist);
517 		}
518 	} else if (entry.IsFile()) {
519 		BString mimeString = _MIMEString(&ref);
520 		if (_IsMediaFile(mimeString)) {
521 			PlaylistItem* item = new (std::nothrow) FilePlaylistItem(ref);
522 			if (!_ExtraMediaExists(playlist, ref)) {
523 				_BindExtraMedia(item);
524 				if (item != NULL && !playlist->AddItem(item))
525 					delete item;
526 			} else
527 				delete item;
528 		} else
529 			printf("MIME Type = %s\n", mimeString.String());
530 	}
531 }
532 
533 
534 /*static*/ void
535 Playlist::AppendPlaylistToPlaylist(const entry_ref& ref, Playlist* playlist)
536 {
537 	BEntry entry(&ref, true);
538 	if (entry.InitCheck() != B_OK || !entry.Exists())
539 		return;
540 
541 	BString mimeString = _MIMEString(&ref);
542 	if (_IsTextPlaylist(mimeString)) {
543 		//printf("RunPlaylist thing\n");
544 		BFile file(&ref, B_READ_ONLY);
545 		FileReadWrite lineReader(&file);
546 
547 		BString str;
548 		entry_ref refPath;
549 		status_t err;
550 		BPath path;
551 		while (lineReader.Next(str)) {
552 			str = str.RemoveFirst("file://");
553 			str = str.RemoveLast("..");
554 			path = BPath(str.String());
555 			printf("Line %s\n", path.Path());
556 			if (path.Path() != NULL) {
557 				if ((err = get_ref_for_path(path.Path(), &refPath)) == B_OK) {
558 					PlaylistItem* item
559 						= new (std::nothrow) FilePlaylistItem(refPath);
560 					if (item == NULL || !playlist->AddItem(item))
561 						delete item;
562 				} else {
563 					printf("Error - %s: [%" B_PRIx32 "]\n", strerror(err),
564 						err);
565 				}
566 			} else
567 				printf("Error - No File Found in playlist\n");
568 		}
569 	} else if (_IsBinaryPlaylist(mimeString)) {
570 		BFile file(&ref, B_READ_ONLY);
571 		Playlist temp;
572 		if (temp.Unflatten(&file) == B_OK)
573 			playlist->AdoptPlaylist(temp, playlist->CountItems());
574 	}
575 }
576 
577 
578 /*static*/ void
579 Playlist::AppendM3uToPlaylist(const entry_ref& ref, Playlist* playlist)
580 {
581 	BFile file(&ref, B_READ_ONLY);
582 	FileReadWrite lineReader(&file);
583 
584 	BString line;
585 	while (lineReader.Next(line)) {
586 		if (line.FindFirst("#") != 0) {
587 			BPath path(line.String());
588 			entry_ref refPath;
589 			status_t err;
590 
591 			if ((err = get_ref_for_path(path.Path(), &refPath)) == B_OK) {
592 				PlaylistItem* item
593 					= new (std::nothrow) FilePlaylistItem(refPath);
594 				if (item == NULL || !playlist->AddItem(item))
595 					delete item;
596 			} else {
597 				printf("Error - %s: [%" B_PRIx32 "]\n", strerror(err), err);
598 			}
599 		}
600 
601 		line.Truncate(0);
602 	}
603 }
604 
605 
606 /*static*/ void
607 Playlist::AppendQueryToPlaylist(const entry_ref& ref, Playlist* playlist)
608 {
609 	BQueryFile query(&ref);
610 	if (query.InitCheck() != B_OK)
611 		return;
612 
613 	entry_ref foundRef;
614 	while (query.GetNextRef(&foundRef) == B_OK) {
615 		PlaylistItem* item = new (std::nothrow) FilePlaylistItem(foundRef);
616 		if (item == NULL || !playlist->AddItem(item))
617 			delete item;
618 	}
619 }
620 
621 
622 void
623 Playlist::NotifyImportFailed()
624 {
625 	BAutolock _(this);
626 	_NotifyImportFailed();
627 }
628 
629 
630 /*static*/ bool
631 Playlist::ExtraMediaExists(Playlist* playlist, PlaylistItem* item)
632 {
633 	FilePlaylistItem* fileItem = dynamic_cast<FilePlaylistItem*>(item);
634 	if (fileItem != NULL)
635 		return _ExtraMediaExists(playlist, fileItem->Ref());
636 
637 	// If we are here let's see if it is an url
638 	UrlPlaylistItem* urlItem = dynamic_cast<UrlPlaylistItem*>(item);
639 	if (urlItem == NULL)
640 		return true;
641 
642 	return _ExtraMediaExists(playlist, urlItem->Url());
643 }
644 
645 
646 // #pragma mark - private
647 
648 
649 /*static*/ bool
650 Playlist::_ExtraMediaExists(Playlist* playlist, const entry_ref& ref)
651 {
652 	BString exceptExtension = _GetExceptExtension(BPath(&ref).Path());
653 
654 	for (int32 i = 0; i < playlist->CountItems(); i++) {
655 		FilePlaylistItem* compare = dynamic_cast<FilePlaylistItem*>(playlist->ItemAt(i));
656 		if (compare == NULL)
657 			continue;
658 		if (compare->Ref() != ref
659 				&& _GetExceptExtension(BPath(&compare->Ref()).Path()) == exceptExtension )
660 			return true;
661 	}
662 	return false;
663 }
664 
665 
666 /*static*/ bool
667 Playlist::_ExtraMediaExists(Playlist* playlist, BUrl url)
668 {
669 	for (int32 i = 0; i < playlist->CountItems(); i++) {
670 		UrlPlaylistItem* compare
671 			= dynamic_cast<UrlPlaylistItem*>(playlist->ItemAt(i));
672 		if (compare == NULL)
673 			continue;
674 		if (compare->Url() == url)
675 			return true;
676 	}
677 	return false;
678 }
679 
680 
681 /*static*/ bool
682 Playlist::_IsImageFile(const BString& mimeString)
683 {
684 	BMimeType superType;
685 	BMimeType fileType(mimeString.String());
686 
687 	if (fileType.GetSupertype(&superType) != B_OK)
688 		return false;
689 
690 	if (superType == "image")
691 		return true;
692 
693 	return false;
694 }
695 
696 
697 /*static*/ bool
698 Playlist::_IsMediaFile(const BString& mimeString)
699 {
700 	BMimeType superType;
701 	BMimeType fileType(mimeString.String());
702 
703 	if (fileType.GetSupertype(&superType) != B_OK)
704 		return false;
705 
706 	// try a shortcut first
707 	if (superType == "audio" || superType == "video")
708 		return true;
709 
710 	// Look through our supported types
711 	app_info appInfo;
712 	if (be_app->GetAppInfo(&appInfo) != B_OK)
713 		return false;
714 	BFile appFile(&appInfo.ref, B_READ_ONLY);
715 	if (appFile.InitCheck() != B_OK)
716 		return false;
717 	BMessage types;
718 	BAppFileInfo appFileInfo(&appFile);
719 	if (appFileInfo.GetSupportedTypes(&types) != B_OK)
720 		return false;
721 
722 	const char* type;
723 	for (int32 i = 0; types.FindString("types", i, &type) == B_OK; i++) {
724 		if (strcasecmp(mimeString.String(), type) == 0)
725 			return true;
726 	}
727 
728 	return false;
729 }
730 
731 
732 /*static*/ bool
733 Playlist::_IsTextPlaylist(const BString& mimeString)
734 {
735 	return mimeString.Compare(kTextPlaylistMimeString) == 0;
736 }
737 
738 
739 /*static*/ bool
740 Playlist::_IsBinaryPlaylist(const BString& mimeString)
741 {
742 	return mimeString.Compare(kBinaryPlaylistMimeString) == 0;
743 }
744 
745 
746 /*static*/ bool
747 Playlist::_IsPlaylist(const BString& mimeString)
748 {
749 	return _IsTextPlaylist(mimeString) || _IsBinaryPlaylist(mimeString);
750 }
751 
752 
753 /*static*/ bool
754 Playlist::_IsM3u(const entry_ref& ref)
755 {
756 	BString path(BPath(&ref).Path());
757 	return path.FindLast(".m3u") == path.CountChars() - 4
758 		|| path.FindLast(".m3u8") == path.CountChars() - 5;
759 }
760 
761 
762 /*static*/ bool
763 Playlist::_IsQuery(const BString& mimeString)
764 {
765 	return mimeString.Compare(BQueryFile::MimeType()) == 0;
766 }
767 
768 
769 /*static*/ BString
770 Playlist::_MIMEString(const entry_ref* ref)
771 {
772 	BFile file(ref, B_READ_ONLY);
773 	BNodeInfo nodeInfo(&file);
774 	char mimeString[B_MIME_TYPE_LENGTH];
775 	if (nodeInfo.GetType(mimeString) != B_OK) {
776 		BMimeType type;
777 		if (BMimeType::GuessMimeType(ref, &type) != B_OK)
778 			return BString();
779 
780 		strlcpy(mimeString, type.Type(), B_MIME_TYPE_LENGTH);
781 		nodeInfo.SetType(type.Type());
782 	}
783 	return BString(mimeString);
784 }
785 
786 
787 // _BindExtraMedia() searches additional videos and audios
788 // and addes them as extra medias.
789 /*static*/ void
790 Playlist::_BindExtraMedia(PlaylistItem* item)
791 {
792 	FilePlaylistItem* fileItem = dynamic_cast<FilePlaylistItem*>(item);
793 	if (!fileItem)
794 		return;
795 
796 	// If the media file is foo.mp3, _BindExtraMedia() searches foo.avi.
797 	BPath mediaFilePath(&fileItem->Ref());
798 	BString mediaFilePathString = mediaFilePath.Path();
799 	BPath dirPath;
800 	mediaFilePath.GetParent(&dirPath);
801 	BDirectory dir(dirPath.Path());
802 	if (dir.InitCheck() != B_OK)
803 		return;
804 
805 	BEntry entry;
806 	BString entryPathString;
807 	while (dir.GetNextEntry(&entry, true) == B_OK) {
808 		if (!entry.IsFile())
809 			continue;
810 		entryPathString = BPath(&entry).Path();
811 		if (entryPathString != mediaFilePathString
812 				&& _GetExceptExtension(entryPathString) == _GetExceptExtension(mediaFilePathString)) {
813 			_BindExtraMedia(fileItem, entry);
814 		}
815 	}
816 }
817 
818 
819 /*static*/ void
820 Playlist::_BindExtraMedia(FilePlaylistItem* fileItem, const BEntry& entry)
821 {
822 	entry_ref ref;
823 	entry.GetRef(&ref);
824 	BString mimeString = _MIMEString(&ref);
825 	if (_IsMediaFile(mimeString)) {
826 		fileItem->AddRef(ref);
827 	} else if (_IsImageFile(mimeString)) {
828 		fileItem->AddImageRef(ref);
829 	}
830 }
831 
832 
833 /*static*/ BString
834 Playlist::_GetExceptExtension(const BString& path)
835 {
836 	int32 periodPos = path.FindLast('.');
837 	if (periodPos <= path.FindLast('/'))
838 		return path;
839 	return BString(path.String(), periodPos);
840 }
841 
842 
843 // #pragma mark - notifications
844 
845 
846 void
847 Playlist::_NotifyItemAdded(PlaylistItem* item, int32 index) const
848 {
849 	BList listeners(fListeners);
850 	int32 count = listeners.CountItems();
851 	for (int32 i = 0; i < count; i++) {
852 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
853 		listener->ItemAdded(item, index);
854 	}
855 }
856 
857 
858 void
859 Playlist::_NotifyItemRemoved(int32 index) const
860 {
861 	BList listeners(fListeners);
862 	int32 count = listeners.CountItems();
863 	for (int32 i = 0; i < count; i++) {
864 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
865 		listener->ItemRemoved(index);
866 	}
867 }
868 
869 
870 void
871 Playlist::_NotifyItemsSorted() const
872 {
873 	BList listeners(fListeners);
874 	int32 count = listeners.CountItems();
875 	for (int32 i = 0; i < count; i++) {
876 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
877 		listener->ItemsSorted();
878 	}
879 }
880 
881 
882 void
883 Playlist::_NotifyCurrentItemChanged(int32 newIndex, bool play) const
884 {
885 	BList listeners(fListeners);
886 	int32 count = listeners.CountItems();
887 	for (int32 i = 0; i < count; i++) {
888 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
889 		listener->CurrentItemChanged(newIndex, play);
890 	}
891 }
892 
893 
894 void
895 Playlist::_NotifyImportFailed() const
896 {
897 	BList listeners(fListeners);
898 	int32 count = listeners.CountItems();
899 	for (int32 i = 0; i < count; i++) {
900 		Listener* listener = (Listener*)listeners.ItemAtFast(i);
901 		listener->ImportFailed();
902 	}
903 }
904