[Windows] Remove redundant DirectSound error codes
[xbmc.git] / xbmc / favourites / FavouritesUtils.cpp
blob5c1907c05853c6eba18d2bed1c46fe8621cf0ea5
1 /*
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.
7 */
9 #include "FavouritesUtils.h"
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"
26 #include <string>
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
36 item.SetLabel(label);
37 return true;
39 return false;
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);
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
64 item.SetArt("thumb", thumb);
65 return true;
67 return false;
70 bool MoveItem(CFileItemList& items, const std::shared_ptr<CFileItem>& item, int amount)
72 if (items.Size() < 2 || amount == 0)
73 return false;
75 int itemPos = -1;
76 for (const auto& i : items)
78 itemPos++;
80 if (i->GetPath() == item->GetPath())
81 break;
84 if (itemPos < 0 || itemPos >= items.Size())
85 return false;
87 int nextItem = (itemPos + amount) % items.Size();
88 if (nextItem < 0)
90 items.Add(item);
91 items.Remove(0);
93 else if (nextItem == 0)
95 items.AddFront(item, 0);
96 items.Remove(itemPos + 1);
98 else
100 items.Swap(itemPos, nextItem);
102 return true;
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)
115 return false;
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
125 return true;
128 namespace
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);
137 return true;
139 return false;
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());
153 return false;
156 } // namespace FAVOURITES_UTILS