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