1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright (C) 2022 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "GUIWindowFavourites.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "favourites/FavouritesURL.h"
#include "favourites/FavouritesUtils.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIMessage.h"
#include "guilib/GUIWindowManager.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
#include "messaging/ApplicationMessenger.h"
#include "utils/PlayerUtils.h"
#include "utils/StringUtils.h"
CGUIWindowFavourites::CGUIWindowFavourites()
: CGUIMediaWindow(WINDOW_FAVOURITES, "MyFavourites.xml")
{
m_loadType = KEEP_IN_MEMORY;
CServiceBroker::GetFavouritesService().Events().Subscribe(
this, &CGUIWindowFavourites::OnFavouritesEvent);
}
CGUIWindowFavourites::~CGUIWindowFavourites()
{
CServiceBroker::GetFavouritesService().Events().Unsubscribe(this);
}
void CGUIWindowFavourites::OnFavouritesEvent(const CFavouritesService::FavouritesUpdated& event)
{
CGUIMessage m(GUI_MSG_REFRESH_LIST, GetID(), 0, 0);
CServiceBroker::GetAppMessenger()->SendGUIMessage(m);
}
namespace
{
bool ExecuteAction(const std::string& execute)
{
if (!execute.empty())
{
CGUIMessage message(GUI_MSG_EXECUTE, 0, 0);
message.SetStringParam(execute);
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(message);
return true;
}
return false;
}
} // namespace
bool CGUIWindowFavourites::OnSelect(int item)
{
if (item < 0 || item >= m_vecItems->Size())
return false;
return ExecuteAction(CFavouritesURL(*(*m_vecItems)[item], GetID()).GetExecString());
}
bool CGUIWindowFavourites::OnAction(const CAction& action)
{
const int selectedItem = m_viewControl.GetSelectedItem();
if (selectedItem < 0 || selectedItem >= m_vecItems->Size())
return false;
if (action.GetID() == ACTION_PLAYER_PLAY)
{
const CFavouritesURL favURL((*m_vecItems)[selectedItem]->GetPath());
if (!favURL.IsValid())
return false;
// If action is playmedia, just play it
if (favURL.GetAction() == CFavouritesURL::Action::PLAY_MEDIA)
return ExecuteAction(favURL.GetExecString());
// Resolve and check the target
const auto item = std::make_shared<CFileItem>(favURL.GetTarget(), favURL.IsDir());
if (CPlayerUtils::IsItemPlayable(*item))
{
CFavouritesURL target(*item, {});
if (target.GetAction() == CFavouritesURL::Action::PLAY_MEDIA)
{
return ExecuteAction(target.GetExecString());
}
else
{
// build and execute a playmedia execute string
target = CFavouritesURL(CFavouritesURL::Action::PLAY_MEDIA,
{StringUtils::Paramify(item->GetPath())});
return ExecuteAction(target.GetExecString());
}
}
return false;
}
else if (action.GetID() == ACTION_MOVE_ITEM_UP)
{
if (FAVOURITES_UTILS::ShouldEnableMoveItems())
return MoveItem(selectedItem, -1);
}
else if (action.GetID() == ACTION_MOVE_ITEM_DOWN)
{
if (FAVOURITES_UTILS::ShouldEnableMoveItems())
return MoveItem(selectedItem, +1);
}
else if (action.GetID() == ACTION_DELETE_ITEM)
{
return RemoveItem(selectedItem);
}
return CGUIMediaWindow::OnAction(action);
}
bool CGUIWindowFavourites::OnMessage(CGUIMessage& message)
{
bool ret = false;
switch (message.GetMessage())
{
case GUI_MSG_REFRESH_LIST:
{
const int size = m_vecItems->Size();
int selected = m_viewControl.GetSelectedItem();
if (m_vecItems->Size() > 0 && selected == size - 1)
--selected; // remove of last item, select the new last item after refresh
Refresh(true);
if (m_vecItems->Size() < size)
{
// item removed. select item after the removed item
m_viewControl.SetSelectedItem(selected);
}
ret = true;
break;
}
}
return ret || CGUIMediaWindow::OnMessage(message);
}
bool CGUIWindowFavourites::Update(const std::string& strDirectory,
bool updateFilterPath /* = true */)
{
std::string directory = strDirectory;
if (directory.empty())
directory = "favourites://";
return CGUIMediaWindow::Update(directory, updateFilterPath);
}
bool CGUIWindowFavourites::MoveItem(int item, int amount)
{
if (item < 0 || item >= m_vecItems->Size() || m_vecItems->Size() < 2 || amount == 0)
return false;
if (FAVOURITES_UTILS::MoveItem(*m_vecItems, (*m_vecItems)[item], amount) &&
CServiceBroker::GetFavouritesService().Save(*m_vecItems))
{
int selected = item + amount;
if (selected >= m_vecItems->Size())
selected = 0;
else if (selected < 0)
selected = m_vecItems->Size() - 1;
m_viewControl.SetSelectedItem(selected);
return true;
}
return false;
}
bool CGUIWindowFavourites::RemoveItem(int item)
{
if (item < 0 || item >= m_vecItems->Size())
return false;
if (FAVOURITES_UTILS::RemoveItem(*m_vecItems, (*m_vecItems)[item]) &&
CServiceBroker::GetFavouritesService().Save(*m_vecItems))
return true;
return false;
}
|