IAIMPPlaylist *Plugin::GetPlaylist(const std::wstring &playlistName, bool activate, bool create) { IAIMPPlaylist *playlistPointer = nullptr; if (SUCCEEDED(m_playlistManager->GetLoadedPlaylistByName(AIMPString(playlistName), &playlistPointer)) && playlistPointer) { if (activate) m_playlistManager->SetActivePlaylist(playlistPointer); return UpdatePlaylistGrouping(playlistPointer); } else if (create) { if (SUCCEEDED(m_playlistManager->CreatePlaylist(AIMPString(playlistName), activate, &playlistPointer))) return UpdatePlaylistGrouping(playlistPointer); } return nullptr; }
AimpMenu *AimpMenu::Get(const std::wstring &id) { IAIMPMenuItem *menuItem = nullptr; if (SUCCEEDED(m_menuManager->GetByID(AIMPString(L"AIMPYouTube" + id), &menuItem))) { return new AimpMenu(menuItem); } return nullptr; }
std::wstring Plugin::Lang(const std::wstring &key, int part) { std::wstring ret; if (!m_muiService) return ret; IAIMPString *value = nullptr; if (part > -1) { if (SUCCEEDED(m_muiService->GetValuePart(AIMPString(key), part, &value))) { ret = value->GetData(); value->Release(); } } else { if (SUCCEEDED(m_muiService->GetValue(AIMPString(key), &value))) { ret = value->GetData(); value->Release(); } } return ret; }
IAIMPPlaylist *Plugin::GetPlaylistById(const std::wstring &playlistId, bool activate) { IAIMPPlaylist *playlistPointer = nullptr; if (SUCCEEDED(m_playlistManager->GetLoadedPlaylistByID(AIMPString(playlistId), &playlistPointer)) && playlistPointer) { if (activate) m_playlistManager->SetActivePlaylist(playlistPointer); return UpdatePlaylistGrouping(playlistPointer); } return nullptr; }
IAIMPMenuItem *AimpMenu::Add(const std::wstring &name, CallbackFunc action, UINT icon, CallbackFunc showAction) { IAIMPMenuItem *newItem = nullptr; if (SUCCEEDED(m_core->CreateObject(IID_IAIMPMenuItem, reinterpret_cast<void **>(&newItem)))) { newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_PARENT, m_menuItem); newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_ID, AIMPString(L"AIMPSoundcloud" + name)); if (action) { IAIMPAction *newAction = nullptr; if (SUCCEEDED(m_core->CreateObject(IID_IAIMPAction, reinterpret_cast<void **>(&newAction)))) { newAction->SetValueAsObject(AIMP_ACTION_PROPID_ID, AIMPString(L"AIMPSoundcloudAction" + name)); newAction->SetValueAsObject(AIMP_ACTION_PROPID_GROUPNAME, AIMPString(L"SoundCloud")); AIMPString actionName; if (m_menuItem) { IAIMPString *parentName; if (SUCCEEDED(m_menuItem->GetValueAsObject(AIMP_MENUITEM_PROPID_NAME, IID_IAIMPString, reinterpret_cast<void **>(&parentName)))) { actionName->Add(parentName); parentName->Release(); actionName->Add2(L" -> ", 4); } } actionName->Add(AIMPString(name)); newAction->SetValueAsObject(AIMP_ACTION_PROPID_NAME, actionName); newAction->SetValueAsObject(AIMP_ACTION_PROPID_EVENT, new ClickHandler(action, newItem)); newAction->SetValueAsInt32(AIMP_ACTION_PROPID_ENABLED, true); m_core->RegisterExtension(IID_IAIMPServiceActionManager, newAction); newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_ACTION, newAction); newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_NAME, AIMPString(name)); newAction->Release(); } } else { newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_ID, AIMPString(L"AIMPSoundcloud" + name)); newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_NAME, AIMPString(name)); newItem->SetValueAsInt32(AIMP_MENUITEM_PROPID_ENABLED, true); } if (showAction) { newItem->SetValueAsObject(AIMP_MENUITEM_PROPID_EVENT_ONSHOW, new ClickHandler(showAction, newItem)); } if (icon > 0) { SetIcon(newItem, icon); } newItem->SetValueAsInt32(AIMP_MENUITEM_PROPID_STYLE, AIMP_MENUITEM_STYLE_NORMAL); newItem->SetValueAsInt32(AIMP_MENUITEM_PROPID_VISIBLE, true); m_core->RegisterExtension(IID_IAIMPServiceMenuManager, newItem); return newItem; } return nullptr; }
IAIMPPlaylist *Plugin::UpdatePlaylistGrouping(IAIMPPlaylist *pl) { if (!pl) return nullptr; // Change playlist grouping template to %A (album) IAIMPPropertyList *plProp = nullptr; if (SUCCEEDED(pl->QueryInterface(IID_IAIMPPropertyList, reinterpret_cast<void **>(&plProp)))) { plProp->SetValueAsInt32(AIMP_PLAYLIST_PROPID_GROUPPING_OVERRIDEN, 1); plProp->SetValueAsObject(AIMP_PLAYLIST_PROPID_GROUPPING_TEMPLATE, AIMPString(L"%A")); plProp->Release(); } return pl; }
HRESULT WINAPI FileSystem::CreateStream(IAIMPString *FileName, IAIMPStream **Stream) { HRESULT ret = E_FAIL; if (Config::TrackInfo *ti = Tools::TrackInfo(FileName)) { EventListener *listener = new EventListener(); *Stream = listener->m_stream; std::wstring url = YouTubeAPI::GetStreamUrl(ti->Id); uintptr_t *taskId = nullptr; ret = m_httpClient->Get(AIMPString(url), 0, *Stream, listener, nullptr, reinterpret_cast<void **>(&taskId)); if (ret == S_OK) { listener->m_stream->Wait(); } } return ret; }
HRESULT WINAPI Plugin::Initialize(IAIMPCore *Core) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; if (Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Status::Ok) return E_FAIL; m_core = Core; AIMPString::Init(Core); if (FAILED(m_core->QueryInterface(IID_IAIMPServiceMUI, reinterpret_cast<void **>(&m_muiService)))) { Finalize(); return E_FAIL; } if (!Config::Init(Core)) { Finalize(); return E_FAIL; } if (!AimpHTTP::Init(Core)) { Finalize(); return E_FAIL; } if (!AimpMenu::Init(Core)) { Finalize(); return E_FAIL; } Config::LoadExtendedConfig(); m_accessToken = Config::GetString(L"AccessToken"); if (AimpMenu *addMenu = AimpMenu::Get(AIMP_MENUID_PLAYER_PLAYLIST_ADDING)) { addMenu->Add(Lang(L"SoundCloud.Menu\\AddURL"), [this](IAIMPMenuItem *) { AddURLDialog::Show(); }, IDB_ICON)->Release(); delete addMenu; } if (AimpMenu *playlistMenu = AimpMenu::Get(AIMP_MENUID_PLAYER_PLAYLIST_MANAGE)) { playlistMenu->Add(Lang(L"SoundCloud.Menu\\MyTracksAndPlaylists"), [this](IAIMPMenuItem *) { SoundCloudAPI::LoadMyTracksAndPlaylists(); }, IDB_ICON, [this](IAIMPMenuItem *item) { item->SetValueAsInt32(AIMP_MENUITEM_PROPID_VISIBLE, isConnected()); })->Release(); playlistMenu->Add(Lang(L"SoundCloud.Menu\\MyStream"), [this](IAIMPMenuItem *) { if (!isConnected()) { OptionsDialog::Connect(SoundCloudAPI::LoadStream); return; } SoundCloudAPI::LoadStream(); }, IDB_ICON)->Release(); playlistMenu->Add(Lang(L"SoundCloud.Menu\\MyLikes"), [this](IAIMPMenuItem *) { if (!isConnected()) { OptionsDialog::Connect(SoundCloudAPI::LoadLikes); return; } SoundCloudAPI::LoadLikes(); }, IDB_ICON)->Release(); delete playlistMenu; } auto enableIfValid = [this](IAIMPMenuItem *item) { int valid = 0; ForSelectedTracks([&valid](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) valid++; return 0; }); item->SetValueAsInt32(AIMP_MENUITEM_PROPID_VISIBLE, valid > 0); }; if (AimpMenu *contextMenu = AimpMenu::Get(AIMP_MENUID_PLAYER_PLAYLIST_CONTEXT_FUNCTIONS)) { AimpMenu *recommendations = new AimpMenu(contextMenu->Add(Lang(L"SoundCloud.Menu\\LoadRecommendations", 0), nullptr, IDB_ICON, enableIfValid)); recommendations->Add(Lang(L"SoundCloud.Menu\\LoadRecommendations", 1), [this](IAIMPMenuItem *) { ForSelectedTracks([](IAIMPPlaylist *pl, IAIMPPlaylistItem *item, int64_t id) -> int { if (id > 0) { SoundCloudAPI::LoadRecommendations(id, false, item); } return 0; }); })->Release(); recommendations->Add(Lang(L"SoundCloud.Menu\\LoadRecommendations", 2), [this](IAIMPMenuItem *) { ForSelectedTracks([](IAIMPPlaylist *pl, IAIMPPlaylistItem *item, int64_t id) -> int { if (id > 0) { SoundCloudAPI::LoadRecommendations(id, true, item); } return 0; }); })->Release(); delete recommendations; contextMenu->Add(Lang(L"SoundCloud.Menu\\OpenInBrowser"), [this](IAIMPMenuItem *) { ForSelectedTracks([this](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) { wchar_t url[256]; wsprintf(url, L"https://api.soundcloud.com/tracks/%ld?client_id=" TEXT(CLIENT_ID) L"&oauth_token=", id); AimpHTTP::Get(url + m_accessToken, [this](unsigned char *data, int size) { rapidjson::Document d; d.Parse(reinterpret_cast<const char *>(data)); if (d.IsObject() && d.HasMember("permalink_url")) { ShellExecuteA(GetMainWindowHandle(), "open", d["permalink_url"].GetString(), NULL, NULL, SW_SHOWNORMAL); } }); } return 0; }); }, IDB_ICON, enableIfValid)->Release(); contextMenu->Add(Lang(L"SoundCloud.Menu\\AddToExclusions"), [this](IAIMPMenuItem *) { ForSelectedTracks([](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) { Config::TrackExclusions.insert(id); return FLAG_DELETE_ITEM; } return 0; }); Config::SaveExtendedConfig(); }, IDB_ICON, enableIfValid)->Release(); contextMenu->Add(Lang(L"SoundCloud.Menu\\Repost"), [this](IAIMPMenuItem *) { ForSelectedTracks([](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) { SoundCloudAPI::RepostSong(id); } return 0; }); }, IDB_ICON, enableIfValid)->Release(); contextMenu->Add(Lang(L"SoundCloud.Menu\\LikeUnlike", 0), [this](IAIMPMenuItem *) { ForSelectedTracks([](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) { if (Config::Likes.find(id) != Config::Likes.end()) { SoundCloudAPI::UnlikeSong(id); Config::Likes.erase(id); } else { SoundCloudAPI::LikeSong(id); Config::Likes.insert(id); } } return 0; }); Timer::SingleShot(1000, SoundCloudAPI::LoadLikes); Config::SaveExtendedConfig(); }, IDB_ICON, [this](IAIMPMenuItem *item) { int likes = 0; int unlikes = 0; int valid = 0; ForSelectedTracks([&](IAIMPPlaylist *, IAIMPPlaylistItem *, int64_t id) -> int { if (id > 0) { valid++; if (Config::Likes.find(id) != Config::Likes.end()) { unlikes++; } else { likes++; } } return 0; }); if (valid == 0) { item->SetValueAsInt32(AIMP_MENUITEM_PROPID_VISIBLE, false); return; } item->SetValueAsInt32(AIMP_MENUITEM_PROPID_VISIBLE, true); if (unlikes == 0) { item->SetValueAsObject(AIMP_MENUITEM_PROPID_NAME, AIMPString(Lang(L"SoundCloud.Menu\\LikeUnlike", 1))); // Like } else if (likes == 0) { item->SetValueAsObject(AIMP_MENUITEM_PROPID_NAME, AIMPString(Lang(L"SoundCloud.Menu\\LikeUnlike", 2))); // Unlike } else { item->SetValueAsObject(AIMP_MENUITEM_PROPID_NAME, AIMPString(Lang(L"SoundCloud.Menu\\LikeUnlike", 0))); // Like / Unlike } })->Release(); delete contextMenu; } if (FAILED(m_core->QueryInterface(IID_IAIMPServicePlaylistManager, reinterpret_cast<void **>(&m_playlistManager)))) { Finalize(); return E_FAIL; } if (FAILED(m_core->QueryInterface(IID_IAIMPServiceMessageDispatcher, reinterpret_cast<void **>(&m_messageDispatcher)))) { Finalize(); return E_FAIL; } m_messageHook = new MessageHook(this); if (FAILED(m_messageDispatcher->Hook(m_messageHook))) { delete m_messageHook; Finalize(); return E_FAIL; } if (FAILED(m_core->RegisterExtension(IID_IAIMPServiceOptionsDialog, static_cast<OptionsDialog::Base *>(new OptionsDialog(this))))) { Finalize(); return E_FAIL; } if (FAILED(m_core->RegisterExtension(IID_IAIMPServicePlaylistManager, new PlaylistListener()))) { Finalize(); return E_FAIL; } if (Config::GetInt32(L"CheckOnStartup", 1)) { Timer::SingleShot(2000, MonitorCallback); } StartMonitorTimer(); return S_OK; }