1 /* 2 * Copyright 2009-2010 Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 6 #include "PlaylistItem.h" 7 8 #include <stdio.h> 9 10 #include <Catalog.h> 11 #include <Locale.h> 12 13 14 #undef B_TRANSLATION_CONTEXT 15 #define B_TRANSLATION_CONTEXT "MediaPlayer-PlaylistItem" 16 17 18 PlaylistItem::Listener::Listener() 19 { 20 } 21 22 PlaylistItem::Listener::~Listener() 23 { 24 } 25 26 void PlaylistItem::Listener::ItemChanged(const PlaylistItem* item) 27 { 28 } 29 30 31 // #pragma mark - 32 33 34 //#define DEBUG_INSTANCE_COUNT 35 #ifdef DEBUG_INSTANCE_COUNT 36 static vint32 sInstanceCount = 0; 37 #endif 38 39 40 PlaylistItem::PlaylistItem() 41 : 42 fPlaybackFailed(false) 43 { 44 #ifdef DEBUG_INSTANCE_COUNT 45 atomic_add(&sInstanceCount, 1); 46 printf("%p->PlaylistItem::PlaylistItem() (%ld)\n", this, sInstanceCount); 47 #endif 48 } 49 50 51 PlaylistItem::~PlaylistItem() 52 { 53 #ifdef DEBUG_INSTANCE_COUNT 54 atomic_add(&sInstanceCount, -1); 55 printf("%p->PlaylistItem::~PlaylistItem() (%ld)\n", this, sInstanceCount); 56 #endif 57 } 58 59 60 BString 61 PlaylistItem::Name() const 62 { 63 BString name; 64 if (GetAttribute(ATTR_STRING_NAME, name) != B_OK) 65 name = B_TRANSLATE_CONTEXT("<unnamed>", "PlaylistItem-name"); 66 return name; 67 } 68 69 70 BString 71 PlaylistItem::Author() const 72 { 73 BString author; 74 if (GetAttribute(ATTR_STRING_AUTHOR, author) != B_OK) 75 author = B_TRANSLATE_CONTEXT("<unknown>", "PlaylistItem-author"); 76 return author; 77 } 78 79 80 BString 81 PlaylistItem::Album() const 82 { 83 BString album; 84 if (GetAttribute(ATTR_STRING_ALBUM, album) != B_OK) 85 album = B_TRANSLATE_CONTEXT("<unknown>", "PlaylistItem-album"); 86 return album; 87 } 88 89 90 BString 91 PlaylistItem::Title() const 92 { 93 BString title; 94 if (GetAttribute(ATTR_STRING_TITLE, title) != B_OK) 95 title = B_TRANSLATE_CONTEXT("<untitled>", "PlaylistItem-title"); 96 return title; 97 } 98 99 100 int32 101 PlaylistItem::TrackNumber() const 102 { 103 int32 trackNumber; 104 if (GetAttribute(ATTR_INT32_TRACK, trackNumber) != B_OK) 105 trackNumber = 0; 106 return trackNumber; 107 } 108 109 110 void 111 PlaylistItem::SetPlaybackFailed() 112 { 113 fPlaybackFailed = true; 114 } 115 116 117 //! You must hold the Playlist lock. 118 bool 119 PlaylistItem::AddListener(Listener* listener) 120 { 121 if (listener && !fListeners.HasItem(listener)) 122 return fListeners.AddItem(listener); 123 return false; 124 } 125 126 127 //! You must hold the Playlist lock. 128 void 129 PlaylistItem::RemoveListener(Listener* listener) 130 { 131 fListeners.RemoveItem(listener); 132 } 133 134 135 void 136 PlaylistItem::_NotifyListeners() const 137 { 138 BList listeners(fListeners); 139 int32 count = listeners.CountItems(); 140 for (int32 i = 0; i < count; i++) { 141 Listener* listener = (Listener*)listeners.ItemAtFast(i); 142 listener->ItemChanged(this); 143 } 144 } 145 146