| 1 |
/* |
| 2 |
* Copyright (C) 2022 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 "GUIWindowFavourites.h" |
| 10 |
|
| 11 |
#include "ContextMenuManager.h" |
| 12 |
#include "FileItem.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "favourites/FavouritesURL.h" |
| 15 |
#include "favourites/FavouritesUtils.h" |
| 16 |
#include "guilib/GUIComponent.h" |
| 17 |
#include "guilib/GUIMessage.h" |
| 18 |
#include "guilib/GUIWindowManager.h" |
| 19 |
#include "input/actions/Action.h" |
| 20 |
#include "input/actions/ActionIDs.h" |
| 21 |
#include "messaging/ApplicationMessenger.h" |
| 22 |
#include "utils/PlayerUtils.h" |
| 23 |
#include "utils/StringUtils.h" |
| 24 |
#include "utils/guilib/GUIContentUtils.h" |
| 25 |
#include "video/VideoUtils.h" |
| 26 |
#include "video/guilib/VideoPlayActionProcessor.h" |
| 27 |
#include "video/guilib/VideoSelectActionProcessor.h" |
| 28 |
|
| 29 |
CGUIWindowFavourites::CGUIWindowFavourites() |
| 30 |
: CGUIMediaWindow(WINDOW_FAVOURITES, "MyFavourites.xml") |
| 31 |
{ |
| 32 |
m_loadType = KEEP_IN_MEMORY; |
| 33 |
CServiceBroker::GetFavouritesService().Events().Subscribe( |
| 34 |
this, &CGUIWindowFavourites::OnFavouritesEvent); |
| 35 |
} |
| 36 |
|
| 37 |
CGUIWindowFavourites::~CGUIWindowFavourites() |
| 38 |
{ |
| 39 |
CServiceBroker::GetFavouritesService().Events().Unsubscribe(this); |
| 40 |
} |
| 41 |
|
| 42 |
void CGUIWindowFavourites::OnFavouritesEvent(const CFavouritesService::FavouritesUpdated& event) |
| 43 |
{ |
| 44 |
CGUIMessage m(GUI_MSG_REFRESH_LIST, GetID(), 0, 0); |
| 45 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(m); |
| 46 |
} |
| 47 |
|
| 48 |
namespace |
| 49 |
{ |
| 50 |
class CVideoSelectActionProcessor : public VIDEO::GUILIB::CVideoSelectActionProcessorBase |
| 51 |
{ |
| 52 |
public: |
| 53 |
explicit CVideoSelectActionProcessor(const std::shared_ptr<CFileItem>& item) |
| 54 |
: CVideoSelectActionProcessorBase(item) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
protected: |
| 59 |
bool OnPlayPartSelected(unsigned int part) override |
| 60 |
{ |
| 61 |
// part numbers are 1-based |
| 62 |
FAVOURITES_UTILS::ExecuteAction( |
| 63 |
{"PlayMedia", *m_item, StringUtils::Format("playoffset={}", part - 1)}); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
bool OnResumeSelected() override |
| 68 |
{ |
| 69 |
FAVOURITES_UTILS::ExecuteAction({"PlayMedia", *m_item, "resume"}); |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
bool OnPlaySelected() override |
| 74 |
{ |
| 75 |
FAVOURITES_UTILS::ExecuteAction({"PlayMedia", *m_item, "noresume"}); |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
bool OnQueueSelected() override |
| 80 |
{ |
| 81 |
FAVOURITES_UTILS::ExecuteAction({"QueueMedia", *m_item, ""}); |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool OnInfoSelected() override |
| 86 |
{ |
| 87 |
return UTILS::GUILIB::CGUIContentUtils::ShowInfoForItem(*m_item); |
| 88 |
} |
| 89 |
|
| 90 |
bool OnChooseSelected() override |
| 91 |
{ |
| 92 |
CONTEXTMENU::ShowFor(m_item, CContextMenuManager::MAIN); |
| 93 |
return true; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
class CVideoPlayActionProcessor : public VIDEO::GUILIB::CVideoPlayActionProcessorBase |
| 98 |
{ |
| 99 |
public: |
| 100 |
explicit CVideoPlayActionProcessor(const ::std::shared_ptr<CFileItem>& item) |
| 101 |
: CVideoPlayActionProcessorBase(item) |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
protected: |
| 106 |
bool OnResumeSelected() override |
| 107 |
{ |
| 108 |
FAVOURITES_UTILS::ExecuteAction({"PlayMedia", *m_item, "resume"}); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
bool OnPlaySelected() override |
| 113 |
{ |
| 114 |
FAVOURITES_UTILS::ExecuteAction({"PlayMedia", *m_item, "noresume"}); |
| 115 |
return true; |
| 116 |
} |
| 117 |
}; |
| 118 |
} // namespace |
| 119 |
|
| 120 |
bool CGUIWindowFavourites::OnSelect(int itemIdx) |
| 121 |
{ |
| 122 |
if (itemIdx < 0 || itemIdx >= m_vecItems->Size()) |
| 123 |
return false; |
| 124 |
|
| 125 |
const auto item{(*m_vecItems)[itemIdx]}; |
| 126 |
const CFavouritesURL favURL{*item, GetID()}; |
| 127 |
if (!favURL.IsValid()) |
| 128 |
return false; |
| 129 |
|
| 130 |
const bool isPlayMedia{favURL.GetAction() == CFavouritesURL::Action::PLAY_MEDIA}; |
| 131 |
|
| 132 |
const auto target{CServiceBroker::GetFavouritesService().ResolveFavourite(*item)}; |
| 133 |
if (!target) |
| 134 |
return false; |
| 135 |
|
| 136 |
CFileItem targetItem{*target}; |
| 137 |
|
| 138 |
// video select action setting is for files only, except exec func is playmedia... |
| 139 |
if (targetItem.HasVideoInfoTag() && (!targetItem.m_bIsFolder || isPlayMedia)) |
| 140 |
{ |
| 141 |
// play the given/default video version, even if multiple versions are available |
| 142 |
targetItem.SetProperty("has_resolved_video_asset", true); |
| 143 |
|
| 144 |
CVideoSelectActionProcessor proc{std::make_shared<CFileItem>(targetItem)}; |
| 145 |
if (proc.ProcessDefaultAction()) |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// exec the execute string for the original (!) item |
| 150 |
return FAVOURITES_UTILS::ExecuteAction(favURL); |
| 151 |
} |
| 152 |
|
| 153 |
bool CGUIWindowFavourites::OnAction(const CAction& action) |
| 154 |
{ |
| 155 |
const int selectedItem = m_viewControl.GetSelectedItem(); |
| 156 |
if (selectedItem < 0 || selectedItem >= m_vecItems->Size()) |
| 157 |
return false; |
| 158 |
|
| 159 |
if (action.GetID() == ACTION_PLAYER_PLAY) |
| 160 |
{ |
| 161 |
const auto target{ |
| 162 |
CServiceBroker::GetFavouritesService().ResolveFavourite(*(*m_vecItems)[selectedItem])}; |
| 163 |
if (!target) |
| 164 |
return false; |
| 165 |
|
| 166 |
const auto item{std::make_shared<CFileItem>(*target)}; |
| 167 |
|
| 168 |
// video play action setting is for files and folders... |
| 169 |
if (item->HasVideoInfoTag() || (item->m_bIsFolder && VIDEO_UTILS::IsItemPlayable(*item))) |
| 170 |
{ |
| 171 |
CVideoPlayActionProcessor proc{item}; |
| 172 |
if (proc.ProcessDefaultAction()) |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
if (CPlayerUtils::IsItemPlayable(*item)) |
| 177 |
{ |
| 178 |
CFavouritesURL target{*item, {}}; |
| 179 |
if (target.GetAction() != CFavouritesURL::Action::PLAY_MEDIA) |
| 180 |
{ |
| 181 |
// build a playmedia execute string for given target |
| 182 |
target = CFavouritesURL{CFavouritesURL::Action::PLAY_MEDIA, |
| 183 |
{StringUtils::Paramify(item->GetPath())}}; |
| 184 |
} |
| 185 |
return FAVOURITES_UTILS::ExecuteAction(target); |
| 186 |
} |
| 187 |
return false; |
| 188 |
} |
| 189 |
else if (action.GetID() == ACTION_SHOW_INFO) |
| 190 |
{ |
| 191 |
const auto targetItem{ |
| 192 |
CServiceBroker::GetFavouritesService().ResolveFavourite(*(*m_vecItems)[selectedItem])}; |
| 193 |
|
| 194 |
return UTILS::GUILIB::CGUIContentUtils::ShowInfoForItem(*targetItem); |
| 195 |
} |
| 196 |
else if (action.GetID() == ACTION_MOVE_ITEM_UP) |
| 197 |
{ |
| 198 |
if (FAVOURITES_UTILS::ShouldEnableMoveItems()) |
| 199 |
return MoveItem(selectedItem, -1); |
| 200 |
} |
| 201 |
else if (action.GetID() == ACTION_MOVE_ITEM_DOWN) |
| 202 |
{ |
| 203 |
if (FAVOURITES_UTILS::ShouldEnableMoveItems()) |
| 204 |
return MoveItem(selectedItem, +1); |
| 205 |
} |
| 206 |
else if (action.GetID() == ACTION_DELETE_ITEM) |
| 207 |
{ |
| 208 |
return RemoveItem(selectedItem); |
| 209 |
} |
| 210 |
|
| 211 |
return CGUIMediaWindow::OnAction(action); |
| 212 |
} |
| 213 |
|
| 214 |
bool CGUIWindowFavourites::OnMessage(CGUIMessage& message) |
| 215 |
{ |
| 216 |
bool ret = false; |
| 217 |
|
| 218 |
switch (message.GetMessage()) |
| 219 |
{ |
| 220 |
case GUI_MSG_REFRESH_LIST: |
| 221 |
{ |
| 222 |
const int size = m_vecItems->Size(); |
| 223 |
int selected = m_viewControl.GetSelectedItem(); |
| 224 |
if (m_vecItems->Size() > 0 && selected == size - 1) |
| 225 |
--selected; // remove of last item, select the new last item after refresh |
| 226 |
|
| 227 |
Refresh(true); |
| 228 |
|
| 229 |
if (m_vecItems->Size() < size) |
| 230 |
{ |
| 231 |
// item removed. select item after the removed item |
| 232 |
m_viewControl.SetSelectedItem(selected); |
| 233 |
} |
| 234 |
|
| 235 |
ret = true; |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return ret || CGUIMediaWindow::OnMessage(message); |
| 241 |
} |
| 242 |
|
| 243 |
bool CGUIWindowFavourites::Update(const std::string& strDirectory, |
| 244 |
bool updateFilterPath /* = true */) |
| 245 |
{ |
| 246 |
std::string directory = strDirectory; |
| 247 |
if (directory.empty()) |
| 248 |
directory = "favourites://"; |
| 249 |
|
| 250 |
return CGUIMediaWindow::Update(directory, updateFilterPath); |
| 251 |
} |
| 252 |
|
| 253 |
bool CGUIWindowFavourites::MoveItem(int item, int amount) |
| 254 |
{ |
| 255 |
if (item < 0 || item >= m_vecItems->Size() || m_vecItems->Size() < 2 || amount == 0) |
| 256 |
return false; |
| 257 |
|
| 258 |
if (FAVOURITES_UTILS::MoveItem(*m_vecItems, (*m_vecItems)[item], amount) && |
| 259 |
CServiceBroker::GetFavouritesService().Save(*m_vecItems)) |
| 260 |
{ |
| 261 |
int selected = item + amount; |
| 262 |
if (selected >= m_vecItems->Size()) |
| 263 |
selected = 0; |
| 264 |
else if (selected < 0) |
| 265 |
selected = m_vecItems->Size() - 1; |
| 266 |
|
| 267 |
m_viewControl.SetSelectedItem(selected); |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
bool CGUIWindowFavourites::RemoveItem(int item) |
| 275 |
{ |
| 276 |
if (item < 0 || item >= m_vecItems->Size()) |
| 277 |
return false; |
| 278 |
|
| 279 |
if (FAVOURITES_UTILS::RemoveItem(*m_vecItems, (*m_vecItems)[item]) && |
| 280 |
CServiceBroker::GetFavouritesService().Save(*m_vecItems)) |
| 281 |
return true; |
| 282 |
|
| 283 |
return false; |
| 284 |
} |