2 * Copyright (C) 2023 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "FavouritesUtils.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"
28 namespace FAVOURITES_UTILS
30 bool ChooseAndSetNewName(CFileItem
& item
)
32 std::string label
= item
.GetLabel();
33 if (CGUIKeyboardFactory::ShowAndGetInput(label
, CVariant
{g_localizeStrings
.Get(16008)},
34 false)) // Enter new title
42 bool ChooseAndSetNewThumbnail(CFileItem
& item
)
44 CFileItemList prefilledItems
;
45 if (item
.HasArt("thumb"))
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
);
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
);
60 CServiceBroker::GetMediaManager().GetLocalDrives(sources
);
61 if (CGUIDialogFileBrowser::ShowAndGetImage(prefilledItems
, sources
, g_localizeStrings
.Get(1030),
62 thumb
)) // Browse for image
64 item
.SetArt("thumb", thumb
);
70 bool MoveItem(CFileItemList
& items
, const std::shared_ptr
<CFileItem
>& item
, int amount
)
72 if (items
.Size() < 2 || amount
== 0)
76 for (const auto& i
: items
)
80 if (i
->GetPath() == item
->GetPath())
84 if (itemPos
< 0 || itemPos
>= items
.Size())
87 int nextItem
= (itemPos
+ amount
) % items
.Size();
93 else if (nextItem
== 0)
95 items
.AddFront(item
, 0);
96 items
.Remove(itemPos
+ 1);
100 items
.Swap(itemPos
, nextItem
);
105 bool RemoveItem(CFileItemList
& items
, const std::shared_ptr
<CFileItem
>& item
)
107 int iBefore
= items
.Size();
108 items
.Remove(item
.get());
109 return items
.Size() == iBefore
- 1;
112 bool ShouldEnableMoveItems()
114 if (CServiceBroker::GetFavouritesService().Size() <= 1)
117 auto& mgr
= CServiceBroker::GetGUI()->GetWindowManager();
118 CGUIWindowFavourites
* window
= mgr
.GetWindow
<CGUIWindowFavourites
>(WINDOW_FAVOURITES
);
119 if (window
&& window
->IsActive())
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
130 bool ExecuteAction(const std::string
& execString
)
132 if (!execString
.empty())
134 CGUIMessage
message(GUI_MSG_EXECUTE
, 0, 0);
135 message
.SetStringParam(execString
);
136 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(message
);
141 } // unnamed namespace
143 bool ExecuteAction(const CExecString
& execString
)
145 return ExecuteAction(execString
.GetExecString());
148 bool ExecuteAction(const CFavouritesURL
& favURL
)
150 if (favURL
.IsValid())
151 return ExecuteAction(favURL
.GetExecString());
156 } // namespace FAVOURITES_UTILS