Ticket #13845: 0005-Expose-the-current-track-as-a-scripting-object.2.patch
File 0005-Expose-the-current-track-as-a-scripting-object.2.patch, 8.5 KB (added by , 6 years ago) |
---|
-
src/apps/mediaplayer/MainWin.cpp
From 248101f17b3d48d7c20bb9332aa9549d42b5c506 Mon Sep 17 00:00:00 2001 From: Owen <owenca@users.noreply.github.com> Date: Fri, 15 Dec 2017 06:23:10 +0000 Subject: [PATCH 5/5] Expose the current track as a scripting object. * Add the CurrentTrack property to the MainWin class. * Make the PlaylistItem class scriptable. * Add the Name, Author, Album, Title, TrackNumber, Duration, and LocationURI properties to PlaylistItem. Example usage of the hey command: hey MediaPlayer GET Name OF CurrentTrack OF Window 0 --- src/apps/mediaplayer/MainWin.cpp | 43 ++++++-- src/apps/mediaplayer/MainWin.h | 2 + src/apps/mediaplayer/playlist/PlaylistItem.cpp | 138 +++++++++++++++++++++++++ src/apps/mediaplayer/playlist/PlaylistItem.h | 10 +- 4 files changed, 185 insertions(+), 8 deletions(-) diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp index b338404..0361bc7 100644
a b static property_info sPropertyInfo[] = { 190 190 "Gets the title of the nth track in Playlist.", 0, 191 191 { B_STRING_TYPE } 192 192 }, 193 { "CurrentTrack", { 0 }, 194 { B_DIRECT_SPECIFIER, 0 }, 195 NULL, 0, 196 { 0 } 197 }, 193 198 194 199 { 0 } 195 200 }; … … MainWin::MainWin(bool isFirstWindow, BMessage* message) 212 217 fHasFile(false), 213 218 fHasVideo(false), 214 219 fHasAudio(false), 220 fCurrentItem(NULL), 215 221 fPlaylist(new Playlist), 216 222 fPlaylistObserver(new PlaylistObserver(this)), 217 223 fController(new Controller), … … MainWin::MessageReceived(BMessage* msg) 622 628 623 629 case 14: 624 630 { 625 int32 i = specifier.GetInt32("index", 0);626 if (i >= fPlaylist->CountItems()) {631 int32 index = specifier.GetInt32("index", 0); 632 if (index >= fPlaylist->CountItems()) { 627 633 result = B_NO_INIT; 628 634 break; 629 635 } 630 636 631 637 BAutolock _(fPlaylist); 632 const PlaylistItem* item = fPlaylist->ItemAt(i );638 const PlaylistItem* item = fPlaylist->ItemAt(index); 633 639 result = item == NULL ? B_NO_INIT 634 640 : reply.AddString("result", item->Title()); 635 641 break; … … MainWin::MessageReceived(BMessage* msg) 774 780 itemRef.SetTo(fPlaylist->ItemAt( 775 781 fPlaylist->CurrentItemIndex())); 776 782 } 783 784 if (fCurrentItem != NULL) 785 RemoveHandler(fCurrentItem); 786 787 fCurrentItem = item; 777 788 _PlaylistItemOpened(itemRef, result); 778 789 break; 779 790 } … … MainWin::WindowActivated(bool active) 1120 1131 bool 1121 1132 MainWin::QuitRequested() 1122 1133 { 1134 if (fCurrentItem != NULL) 1135 RemoveHandler(fCurrentItem); 1136 1123 1137 BMessage message(M_PLAYER_QUIT); 1124 1138 GetQuitMessage(&message); 1125 1139 be_app->PostMessage(&message); … … MainWin::GetQuitMessage(BMessage* message) 1325 1339 1326 1340 1327 1341 BHandler* 1328 MainWin::ResolveSpecifier(BMessage* m essage, int32 index, BMessage* specifier,1342 MainWin::ResolveSpecifier(BMessage* msg, int32 index, BMessage* specifier, 1329 1343 int32 what, const char* property) 1330 1344 { 1331 1345 BPropertyInfo propertyInfo(sPropertyInfo); 1332 if (propertyInfo.FindMatch(message, index, specifier, what, property) 1333 < propertyInfo.CountProperties()) 1346 1347 if (propertyInfo.FindMatch(msg, index, specifier, what, property) >= 0) { 1348 if (strcmp(property, "CurrentTrack") == 0) { 1349 if (fCurrentItem != NULL) { 1350 AddHandler(fCurrentItem); 1351 msg->PopSpecifier(); 1352 return fCurrentItem; 1353 } 1354 1355 BMessage replyMsg(B_MESSAGE_NOT_UNDERSTOOD); 1356 replyMsg.AddInt32("error", B_NAME_NOT_FOUND); 1357 replyMsg.AddString("message", "There is no current track."); 1358 msg->SendReply(&replyMsg); 1359 return NULL; 1360 } 1361 1334 1362 return this; 1363 } 1335 1364 1336 return BWindow::ResolveSpecifier(m essage, index, specifier, what, property);1365 return BWindow::ResolveSpecifier(msg, index, specifier, what, property); 1337 1366 } 1338 1367 1339 1368 -
src/apps/mediaplayer/MainWin.h
diff --git a/src/apps/mediaplayer/MainWin.h b/src/apps/mediaplayer/MainWin.h index c2b2de8..aa2c172 100644
a b private: 164 164 bool fHasVideo; 165 165 bool fHasAudio; 166 166 167 PlaylistItem* fCurrentItem; 168 167 169 Playlist* fPlaylist; 168 170 PlaylistObserver* fPlaylistObserver; 169 171 Controller* fController; -
src/apps/mediaplayer/playlist/PlaylistItem.cpp
diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.cpp b/src/apps/mediaplayer/playlist/PlaylistItem.cpp index 1ed5fdc..d4c32b5 100644
a b 9 9 10 10 #include <Catalog.h> 11 11 #include <Locale.h> 12 #include <PropertyInfo.h> 12 13 13 14 #include "AudioTrackSupplier.h" 14 15 #include "TrackSupplier.h" … … 18 19 #define B_TRANSLATION_CONTEXT "MediaPlayer-PlaylistItem" 19 20 20 21 22 static property_info sProperties[] = { 23 { "Name", { B_GET_PROPERTY, 0 }, 24 { B_DIRECT_SPECIFIER, 0 }, 25 "Gets the name.", 0, 26 { B_STRING_TYPE } 27 }, 28 { "Author", { B_GET_PROPERTY, 0 }, 29 { B_DIRECT_SPECIFIER, 0 }, 30 "Gets the author.", 0, 31 { B_STRING_TYPE } 32 }, 33 { "Album", { B_GET_PROPERTY, 0 }, 34 { B_DIRECT_SPECIFIER, 0 }, 35 "Gets the album.", 0, 36 { B_STRING_TYPE } 37 }, 38 { "Title", { B_GET_PROPERTY, 0 }, 39 { B_DIRECT_SPECIFIER, 0 }, 40 "Gets the title.", 0, 41 { B_STRING_TYPE } 42 }, 43 { "TrackNumber", { B_GET_PROPERTY, 0 }, 44 { B_DIRECT_SPECIFIER, 0 }, 45 "Gets the track number.", 0, 46 { B_INT32_TYPE } 47 }, 48 { "Duration", { B_GET_PROPERTY, 0 }, 49 { B_DIRECT_SPECIFIER, 0 }, 50 "Gets the duration.", 0, 51 { B_INT64_TYPE } 52 }, 53 { "LocationURI", { B_GET_PROPERTY, 0 }, 54 { B_DIRECT_SPECIFIER, 0 }, 55 "Gets the location URI.", 0, 56 { B_STRING_TYPE } 57 }, 58 59 { 0 } 60 }; 61 62 63 BHandler* 64 PlaylistItem::ResolveSpecifier(BMessage* msg, int32 index, BMessage* specifier, 65 int32 what, const char* property) 66 { 67 BPropertyInfo propertyInfo(sProperties); 68 int32 i = propertyInfo.FindMatch(msg, index, specifier, what, property); 69 70 if (i >= 0 && i < propertyInfo.CountProperties()) 71 return this; 72 73 return BHandler::ResolveSpecifier(msg, index, specifier, what, property); 74 } 75 76 77 status_t 78 PlaylistItem::GetSupportedSuites(BMessage* msg) 79 { 80 msg->AddString("suites", "suite/vnd.Haiku-MediaPlayer"); 81 82 BPropertyInfo propertyInfo(sProperties); 83 msg->AddFlat("messages", &propertyInfo); 84 85 return BHandler::GetSupportedSuites(msg); 86 } 87 88 89 void 90 PlaylistItem::MessageReceived(BMessage* msg) 91 { 92 if (msg->what != B_GET_PROPERTY) { 93 BHandler::MessageReceived(msg); 94 return; 95 } 96 97 int32 index; 98 BMessage specifier; 99 int32 what; 100 const char* property; 101 102 if (msg->GetCurrentSpecifier(&index, &specifier, &what, &property) 103 != B_OK) { 104 BHandler::MessageReceived(msg); 105 return; 106 } 107 108 BPropertyInfo propertyInfo(sProperties); 109 int32 propertyIndex = propertyInfo.FindMatch(msg, index, &specifier, what, 110 property); 111 112 if (propertyIndex == B_ERROR) { 113 BHandler::MessageReceived(msg); 114 return; 115 } 116 117 status_t rc = B_BAD_SCRIPT_SYNTAX; 118 BMessage reply(B_REPLY); 119 120 switch (propertyIndex) { 121 case 0: 122 rc = reply.AddString("result", Name()); 123 break; 124 case 1: 125 rc = reply.AddString("result", Author()); 126 break; 127 case 2: 128 rc = reply.AddString("result", Album()); 129 break; 130 case 3: 131 rc = reply.AddString("result", Title()); 132 break; 133 case 4: 134 rc = reply.AddInt32("result", TrackNumber()); 135 break; 136 case 5: 137 rc = reply.AddInt64("result", Duration()); 138 break; 139 case 6: 140 rc = reply.AddString("result", LocationURI()); 141 break; 142 default: 143 BHandler::MessageReceived(msg); 144 return; 145 } 146 147 if (rc != B_OK) { 148 reply.what = B_MESSAGE_NOT_UNDERSTOOD; 149 reply.AddString("message", rc == B_BAD_SCRIPT_SYNTAX 150 ? "Didn't understand the specifier" 151 : strerror(rc)); 152 reply.AddInt32("error", rc); 153 } 154 155 msg->SendReply(&reply); 156 } 157 158 21 159 PlaylistItem::Listener::Listener() 22 160 { 23 161 } -
src/apps/mediaplayer/playlist/PlaylistItem.h
diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.h b/src/apps/mediaplayer/playlist/PlaylistItem.h index 7331c21..416afe0 100644
a b 7 7 8 8 9 9 #include <Archivable.h> 10 #include <Handler.h> 10 11 #include <List.h> 11 12 #include <NodeInfo.h> 12 13 #include <Referenceable.h> … … class BMessage; 18 19 class TrackSupplier; 19 20 20 21 21 class PlaylistItem : public B Archivable, public BReferenceable {22 class PlaylistItem : public BHandler, public BReferenceable { 22 23 public: 23 24 class Listener { 24 25 public: … … public: 38 39 virtual status_t Archive(BMessage* into, 39 40 bool deep = true) const = 0; 40 41 42 BHandler* ResolveSpecifier(BMessage* msg, int32 index, 43 BMessage* specifier, int32 what, 44 const char* property); 45 status_t GetSupportedSuites(BMessage* msg); 46 47 void MessageReceived(BMessage* msg); 48 41 49 // attributes 42 50 typedef enum { 43 51 ATTR_STRING_NAME = 'name',