| 1 | /* |
| 2 | * Copyright (C) 2005-2018 Team Kodi |
| 3 | * This file is part of Kodi - https://kodi.tv |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | * See LICENSES/README.md for more information. |
| 7 | */ |
| 8 | |
| 9 | #include "GUIInfoHelper.h" |
| 10 | |
| 11 | #include "FileItem.h" |
| 12 | #include "PlayListPlayer.h" |
| 13 | #include "ServiceBroker.h" |
| 14 | #include "guilib/GUIComponent.h" |
| 15 | #include "guilib/GUIWindow.h" |
| 16 | #include "guilib/GUIWindowManager.h" |
| 17 | #include "guilib/IGUIContainer.h" |
| 18 | #include "guilib/LocalizeStrings.h" |
| 19 | #include "guilib/guiinfo/GUIInfoLabels.h" |
| 20 | #include "playlists/PlayList.h" |
| 21 | #include "utils/StringUtils.h" |
| 22 | #include "utils/URIUtils.h" |
| 23 | #include "windows/GUIMediaWindow.h" |
| 24 | |
| 25 | namespace KODI |
| 26 | { |
| 27 | namespace GUILIB |
| 28 | { |
| 29 | namespace GUIINFO |
| 30 | { |
| 31 | |
| 32 | // conditions for window retrieval |
| 33 | static const int WINDOW_CONDITION_HAS_LIST_ITEMS = 1; |
| 34 | static const int WINDOW_CONDITION_IS_MEDIA_WINDOW = 2; |
| 35 | |
| 36 | std::string GetPlaylistLabel(int item, PLAYLIST::Id playlistId /* = TYPE_NONE */) |
| 37 | { |
| 38 | PLAYLIST::CPlayListPlayer& player = CServiceBroker::GetPlaylistPlayer(); |
| 39 | |
| 40 | if (playlistId == PLAYLIST::TYPE_NONE) |
| 41 | playlistId = player.GetCurrentPlaylist(); |
| 42 | |
| 43 | switch (item) |
| 44 | { |
| 45 | case PLAYLIST_LENGTH: |
| 46 | { |
| 47 | return std::to_string(player.GetPlaylist(playlistId).size()); |
| 48 | } |
| 49 | case PLAYLIST_POSITION: |
| 50 | { |
| 51 | int currentSong = player.GetCurrentItemIdx(); |
| 52 | if (currentSong > -1) |
| 53 | return std::to_string(currentSong + 1); |
| 54 | break; |
| 55 | } |
| 56 | case PLAYLIST_RANDOM: |
| 57 | { |
| 58 | if (player.IsShuffled(playlistId)) |
| 59 | return g_localizeStrings.Get(16041); // 16041: On |
| 60 | else |
| 61 | return g_localizeStrings.Get(591); // 591: Off |
| 62 | } |
| 63 | case PLAYLIST_REPEAT: |
| 64 | { |
| 65 | PLAYLIST::RepeatState state = player.GetRepeat(playlistId); |
| 66 | if (state == PLAYLIST::RepeatState::ONE) |
| 67 | return g_localizeStrings.Get(592); // 592: One |
| 68 | else if (state == PLAYLIST::RepeatState::ALL) |
| 69 | return g_localizeStrings.Get(593); // 593: All |
| 70 | else |
| 71 | return g_localizeStrings.Get(594); // 594: Off |
| 72 | } |
| 73 | } |
| 74 | return std::string(); |
| 75 | } |
| 76 | |
| 77 | namespace |
| 78 | { |
| 79 | |
| 80 | bool CheckWindowCondition(CGUIWindow *window, int condition) |
| 81 | { |
| 82 | // check if it satisfies our condition |
| 83 | if (!window) |
| 84 | return false; |
| 85 | if ((condition & WINDOW_CONDITION_HAS_LIST_ITEMS) && !window->HasListItems()) |
| 86 | return false; |
| 87 | if ((condition & WINDOW_CONDITION_IS_MEDIA_WINDOW) && !window->IsMediaWindow()) |
| 88 | return false; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | CGUIWindow* GetWindowWithCondition(int contextWindow, int condition) |
| 93 | { |
| 94 | const CGUIWindowManager& windowMgr = CServiceBroker::GetGUI()->GetWindowManager(); |
| 95 | |
| 96 | CGUIWindow *window = windowMgr.GetWindow(contextWindow); |
| 97 | if (CheckWindowCondition(window, condition)) |
| 98 | return window; |
| 99 | |
| 100 | // try topmost dialog |
| 101 | window = windowMgr.GetWindow(windowMgr.GetTopmostModalDialog()); |
| 102 | if (CheckWindowCondition(window, condition)) |
| 103 | return window; |
| 104 | |
| 105 | // try active window |
| 106 | window = windowMgr.GetWindow(windowMgr.GetActiveWindow()); |
| 107 | if (CheckWindowCondition(window, condition)) |
| 108 | return window; |
| 109 | |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | } // unnamed namespace |
| 114 | |
| 115 | CGUIWindow* GetWindow(int contextWindow) |
| 116 | { |
| 117 | return GetWindowWithCondition(contextWindow, 0); |
| 118 | } |
| 119 | |
| 120 | CFileItemPtr GetCurrentListItemFromWindow(int contextWindow) |
| 121 | { |
| 122 | CGUIWindow* window = GetWindowWithCondition(contextWindow, WINDOW_CONDITION_HAS_LIST_ITEMS); |
| 123 | if (window) |
| 124 | return window->GetCurrentListItem(); |
| 125 | |
| 126 | return CFileItemPtr(); |
| 127 | } |
| 128 | |
| 129 | CGUIMediaWindow* GetMediaWindow(int contextWindow) |
| 130 | { |
| 131 | CGUIWindow* window = GetWindowWithCondition(contextWindow, WINDOW_CONDITION_IS_MEDIA_WINDOW); |
| 132 | if (window) |
| 133 | return static_cast<CGUIMediaWindow*>(window); |
| 134 | |
| 135 | return nullptr; |
| 136 | } |
| 137 | |
| 138 | CGUIControl* GetActiveContainer(int containerId, int contextWindow) |
| 139 | { |
| 140 | CGUIWindow *window = GetWindow(contextWindow); |
| 141 | if (!window) |
| 142 | return nullptr; |
| 143 | |
| 144 | CGUIControl *control = nullptr; |
| 145 | if (!containerId) // No container specified, so we lookup the current view container |
| 146 | { |
| 147 | if (window->IsMediaWindow()) |
| 148 | containerId = static_cast<CGUIMediaWindow*>(window)->GetViewContainerID(); |
| 149 | else |
| 150 | control = window->GetFocusedControl(); |
| 151 | } |
| 152 | |
| 153 | if (!control) |
| 154 | control = window->GetControl(containerId); |
| 155 | |
| 156 | if (control && control->IsContainer()) |
| 157 | return control; |
| 158 | |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | std::shared_ptr<CGUIListItem> GetCurrentListItem(int contextWindow, |
| 163 | int containerId /* = 0 */, |
| 164 | int itemOffset /* = 0 */, |
| 165 | unsigned int itemFlags /* = 0 */) |
| 166 | { |
| 167 | std::shared_ptr<CGUIListItem> item; |
| 168 | |
| 169 | if (containerId == 0 && |
| 170 | itemOffset == 0 && |
| 171 | !(itemFlags & INFOFLAG_LISTITEM_CONTAINER) && |
| 172 | !(itemFlags & INFOFLAG_LISTITEM_ABSOLUTE) && |
| 173 | !(itemFlags & INFOFLAG_LISTITEM_POSITION)) |
| 174 | item = GetCurrentListItemFromWindow(contextWindow); |
| 175 | |
| 176 | if (!item) |
| 177 | { |
| 178 | CGUIControl* activeContainer = GetActiveContainer(containerId, contextWindow); |
| 179 | if (activeContainer) |
| 180 | item = static_cast<IGUIContainer *>(activeContainer)->GetListItem(itemOffset, itemFlags); |
| 181 | } |
| 182 | |
| 183 | return item; |
| 184 | } |
| 185 | |
| 186 | std::string GetFileInfoLabelValueFromPath(int info, const std::string& filenameAndPath) |
| 187 | { |
| 188 | std::string value = filenameAndPath; |
| 189 | |
| 190 | if (info == PLAYER_PATH) |
| 191 | { |
| 192 | // do this twice since we want the path outside the archive if this is to be of use. |
| 193 | if (URIUtils::IsInArchive(value)) |
| 194 | value = URIUtils::GetParentPath(value); |
| 195 | |
| 196 | value = URIUtils::GetParentPath(value); |
| 197 | } |
| 198 | else if (info == PLAYER_FILENAME) |
| 199 | { |
| 200 | value = URIUtils::GetFileName(value); |
| 201 | } |
| 202 | |
| 203 | return value; |
| 204 | } |
| 205 | |
| 206 | } // namespace GUIINFO |
| 207 | } // namespace GUILIB |
| 208 | } // namespace KODI |