| 1 |
/* |
| 2 |
* Copyright (C) 2023 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 "FavouritesUtils.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "dialogs/GUIDialogFileBrowser.h" |
| 14 |
#include "favourites/FavouritesURL.h" |
| 15 |
#include "favourites/GUIWindowFavourites.h" |
| 16 |
#include "guilib/GUIComponent.h" |
| 17 |
#include "guilib/GUIKeyboardFactory.h" |
| 18 |
#include "guilib/GUIMessage.h" |
| 19 |
#include "guilib/GUIWindowManager.h" |
| 20 |
#include "guilib/LocalizeStrings.h" |
| 21 |
#include "storage/MediaManager.h" |
| 22 |
#include "utils/ExecString.h" |
| 23 |
#include "utils/Variant.h" |
| 24 |
#include "view/GUIViewState.h" |
| 25 |
|
| 26 |
#include <string> |
| 27 |
|
| 28 |
namespace FAVOURITES_UTILS |
| 29 |
{ |
| 30 |
bool ChooseAndSetNewName(CFileItem& item) |
| 31 |
{ |
| 32 |
std::string label = item.GetLabel(); |
| 33 |
if (CGUIKeyboardFactory::ShowAndGetInput(label, CVariant{g_localizeStrings.Get(16008)}, |
| 34 |
false)) // Enter new title |
| 35 |
{ |
| 36 |
item.SetLabel(label); |
| 37 |
return true; |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
bool ChooseAndSetNewThumbnail(CFileItem& item) |
| 43 |
{ |
| 44 |
CFileItemList prefilledItems; |
| 45 |
if (item.HasArt("thumb")) |
| 46 |
{ |
| 47 |
const auto current = std::make_shared<CFileItem>("thumb://Current", false); |
| 48 |
current->SetArt("thumb", item.GetArt("thumb")); |
| 49 |
current->SetLabel(g_localizeStrings.Get(20016)); // Current thumb |
| 50 |
prefilledItems.Add(current); |
| 51 |
} |
| 52 |
|
| 53 |
const auto none = std::make_shared<CFileItem>("thumb://None", false); |
| 54 |
none->SetArt("icon", item.GetArt("icon")); |
| 55 |
none->SetLabel(g_localizeStrings.Get(20018)); // No thumb |
| 56 |
prefilledItems.Add(none); |
| 57 |
|
| 58 |
std::string thumb; |
| 59 |
VECSOURCES sources; |
| 60 |
CServiceBroker::GetMediaManager().GetLocalDrives(sources); |
| 61 |
if (CGUIDialogFileBrowser::ShowAndGetImage(prefilledItems, sources, g_localizeStrings.Get(1030), |
| 62 |
thumb)) // Browse for image |
| 63 |
{ |
| 64 |
item.SetArt("thumb", thumb); |
| 65 |
return true; |
| 66 |
} |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
bool MoveItem(CFileItemList& items, const std::shared_ptr<CFileItem>& item, int amount) |
| 71 |
{ |
| 72 |
if (items.Size() < 2 || amount == 0) |
| 73 |
return false; |
| 74 |
|
| 75 |
int itemPos = -1; |
| 76 |
for (const auto& i : items) |
| 77 |
{ |
| 78 |
itemPos++; |
| 79 |
|
| 80 |
if (i->GetPath() == item->GetPath()) |
| 81 |
break; |
| 82 |
} |
| 83 |
|
| 84 |
if (itemPos < 0 || itemPos >= items.Size()) |
| 85 |
return false; |
| 86 |
|
| 87 |
int nextItem = (itemPos + amount) % items.Size(); |
| 88 |
if (nextItem < 0) |
| 89 |
{ |
| 90 |
items.Add(item); |
| 91 |
items.Remove(0); |
| 92 |
} |
| 93 |
else if (nextItem == 0) |
| 94 |
{ |
| 95 |
items.AddFront(item, 0); |
| 96 |
items.Remove(itemPos + 1); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
items.Swap(itemPos, nextItem); |
| 101 |
} |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool RemoveItem(CFileItemList& items, const std::shared_ptr<CFileItem>& item) |
| 106 |
{ |
| 107 |
int iBefore = items.Size(); |
| 108 |
items.Remove(item.get()); |
| 109 |
return items.Size() == iBefore - 1; |
| 110 |
} |
| 111 |
|
| 112 |
bool ShouldEnableMoveItems() |
| 113 |
{ |
| 114 |
if (CServiceBroker::GetFavouritesService().Size() <= 1) |
| 115 |
return false; |
| 116 |
|
| 117 |
auto& mgr = CServiceBroker::GetGUI()->GetWindowManager(); |
| 118 |
CGUIWindowFavourites* window = mgr.GetWindow<CGUIWindowFavourites>(WINDOW_FAVOURITES); |
| 119 |
if (window && window->IsActive()) |
| 120 |
{ |
| 121 |
const CGUIViewState* state = window->GetViewState(); |
| 122 |
if (state && state->GetSortMethod().sortBy != SortByUserPreference) |
| 123 |
return false; // in favs window, allow move only if current sort method is by user preference |
| 124 |
} |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
namespace |
| 129 |
{ |
| 130 |
bool ExecuteAction(const std::string& execString) |
| 131 |
{ |
| 132 |
if (!execString.empty()) |
| 133 |
{ |
| 134 |
CGUIMessage message(GUI_MSG_EXECUTE, 0, 0); |
| 135 |
message.SetStringParam(execString); |
| 136 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(message); |
| 137 |
return true; |
| 138 |
} |
| 139 |
return false; |
| 140 |
} |
| 141 |
} // unnamed namespace |
| 142 |
|
| 143 |
bool ExecuteAction(const CExecString& execString) |
| 144 |
{ |
| 145 |
return ExecuteAction(execString.GetExecString()); |
| 146 |
} |
| 147 |
|
| 148 |
bool ExecuteAction(const CFavouritesURL& favURL) |
| 149 |
{ |
| 150 |
if (favURL.IsValid()) |
| 151 |
return ExecuteAction(favURL.GetExecString()); |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace FAVOURITES_UTILS |