| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 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 "PlayList.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "PlayListFactory.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "filesystem/File.h" |
| 15 |
#include "interfaces/AnnouncementManager.h" |
| 16 |
#include "music/tags/MusicInfoTag.h" |
| 17 |
#include "utils/Random.h" |
| 18 |
#include "utils/StringUtils.h" |
| 19 |
#include "utils/URIUtils.h" |
| 20 |
#include "utils/Variant.h" |
| 21 |
#include "utils/log.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <cassert> |
| 25 |
#include <iostream> |
| 26 |
#include <sstream> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
|
| 32 |
using namespace MUSIC_INFO; |
| 33 |
using namespace XFILE; |
| 34 |
using namespace PLAYLIST; |
| 35 |
|
| 36 |
CPlayList::CPlayList(Id id /* = PLAYLIST::TYPE_NONE */) : m_id(id) |
| 37 |
{ |
| 38 |
m_iPlayableItems = -1; |
| 39 |
m_bShuffled = false; |
| 40 |
m_bWasPlayed = false; |
| 41 |
} |
| 42 |
|
| 43 |
void CPlayList::AnnounceRemove(int pos) |
| 44 |
{ |
| 45 |
if (m_id == TYPE_NONE) |
| 46 |
return; |
| 47 |
|
| 48 |
CVariant data; |
| 49 |
data["playlistid"] = m_id; |
| 50 |
data["position"] = pos; |
| 51 |
CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::Playlist, "OnRemove", data); |
| 52 |
} |
| 53 |
|
| 54 |
void CPlayList::AnnounceClear() |
| 55 |
{ |
| 56 |
if (m_id == TYPE_NONE) |
| 57 |
return; |
| 58 |
|
| 59 |
CVariant data; |
| 60 |
data["playlistid"] = m_id; |
| 61 |
CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::Playlist, "OnClear", data); |
| 62 |
} |
| 63 |
|
| 64 |
void CPlayList::AnnounceAdd(const std::shared_ptr<CFileItem>& item, int pos) |
| 65 |
{ |
| 66 |
if (m_id == TYPE_NONE) |
| 67 |
return; |
| 68 |
|
| 69 |
CVariant data; |
| 70 |
data["playlistid"] = m_id; |
| 71 |
data["position"] = pos; |
| 72 |
CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::Playlist, "OnAdd", item, data); |
| 73 |
} |
| 74 |
|
| 75 |
void CPlayList::Add(const std::shared_ptr<CFileItem>& item, int iPosition, int iOrder) |
| 76 |
{ |
| 77 |
int iOldSize = size(); |
| 78 |
if (iPosition < 0 || iPosition >= iOldSize) |
| 79 |
iPosition = iOldSize; |
| 80 |
if (iOrder < 0 || iOrder >= iOldSize) |
| 81 |
item->m_iprogramCount = iOldSize; |
| 82 |
else |
| 83 |
item->m_iprogramCount = iOrder; |
| 84 |
|
| 85 |
// increment the playable counter |
| 86 |
item->ClearProperty("unplayable"); |
| 87 |
if (m_iPlayableItems < 0) |
| 88 |
m_iPlayableItems = 1; |
| 89 |
else |
| 90 |
m_iPlayableItems++; |
| 91 |
|
| 92 |
// set 'IsPlayable' property - needed for properly handling plugin:// URLs |
| 93 |
item->SetProperty("IsPlayable", true); |
| 94 |
|
| 95 |
//CLog::Log(LOGDEBUG,"{} item:({:02}/{:02})[{}]", __FUNCTION__, iPosition, item->m_iprogramCount, item->GetPath()); |
| 96 |
if (iPosition == iOldSize) |
| 97 |
m_vecItems.push_back(item); |
| 98 |
else |
| 99 |
{ |
| 100 |
ivecItems it = m_vecItems.begin() + iPosition; |
| 101 |
m_vecItems.insert(it, 1, item); |
| 102 |
// correct any duplicate order values |
| 103 |
if (iOrder < iOldSize) |
| 104 |
IncrementOrder(iPosition + 1, iOrder); |
| 105 |
} |
| 106 |
AnnounceAdd(item, iPosition); |
| 107 |
} |
| 108 |
|
| 109 |
void CPlayList::Add(const std::shared_ptr<CFileItem>& item) |
| 110 |
{ |
| 111 |
Add(item, -1, -1); |
| 112 |
} |
| 113 |
|
| 114 |
void CPlayList::Add(const CPlayList& playlist) |
| 115 |
{ |
| 116 |
for (int i = 0; i < playlist.size(); i++) |
| 117 |
Add(playlist[i], -1, -1); |
| 118 |
} |
| 119 |
|
| 120 |
void CPlayList::Add(const CFileItemList& items) |
| 121 |
{ |
| 122 |
for (int i = 0; i < items.Size(); i++) |
| 123 |
Add(items[i]); |
| 124 |
} |
| 125 |
|
| 126 |
void CPlayList::Insert(const CPlayList& playlist, int iPosition /* = -1 */) |
| 127 |
{ |
| 128 |
// out of bounds so just add to the end |
| 129 |
int iSize = size(); |
| 130 |
if (iPosition < 0 || iPosition >= iSize) |
| 131 |
{ |
| 132 |
Add(playlist); |
| 133 |
return; |
| 134 |
} |
| 135 |
for (int i = 0; i < playlist.size(); i++) |
| 136 |
{ |
| 137 |
int iPos = iPosition + i; |
| 138 |
Add(playlist[i], iPos, iPos); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void CPlayList::Insert(const CFileItemList& items, int iPosition /* = -1 */) |
| 143 |
{ |
| 144 |
// out of bounds so just add to the end |
| 145 |
int iSize = size(); |
| 146 |
if (iPosition < 0 || iPosition >= iSize) |
| 147 |
{ |
| 148 |
Add(items); |
| 149 |
return; |
| 150 |
} |
| 151 |
for (int i = 0; i < items.Size(); i++) |
| 152 |
{ |
| 153 |
Add(items[i], iPosition + i, iPosition + i); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void CPlayList::Insert(const std::shared_ptr<CFileItem>& item, int iPosition /* = -1 */) |
| 158 |
{ |
| 159 |
// out of bounds so just add to the end |
| 160 |
int iSize = size(); |
| 161 |
if (iPosition < 0 || iPosition >= iSize) |
| 162 |
{ |
| 163 |
Add(item); |
| 164 |
return; |
| 165 |
} |
| 166 |
Add(item, iPosition, iPosition); |
| 167 |
} |
| 168 |
|
| 169 |
void CPlayList::DecrementOrder(int iOrder) |
| 170 |
{ |
| 171 |
if (iOrder < 0) return; |
| 172 |
|
| 173 |
// it was the last item so do nothing |
| 174 |
if (iOrder == size()) return; |
| 175 |
|
| 176 |
// fix all items with an order greater than the removed iOrder |
| 177 |
ivecItems it; |
| 178 |
it = m_vecItems.begin(); |
| 179 |
while (it != m_vecItems.end()) |
| 180 |
{ |
| 181 |
CFileItemPtr item = *it; |
| 182 |
if (item->m_iprogramCount > iOrder) |
| 183 |
{ |
| 184 |
//CLog::Log(LOGDEBUG,"{} fixing item at order {}", __FUNCTION__, item->m_iprogramCount); |
| 185 |
item->m_iprogramCount--; |
| 186 |
} |
| 187 |
++it; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void CPlayList::IncrementOrder(int iPosition, int iOrder) |
| 192 |
{ |
| 193 |
if (iOrder < 0) return; |
| 194 |
|
| 195 |
// fix all items with an order equal or greater to the added iOrder at iPos |
| 196 |
ivecItems it; |
| 197 |
it = m_vecItems.begin() + iPosition; |
| 198 |
while (it != m_vecItems.end()) |
| 199 |
{ |
| 200 |
CFileItemPtr item = *it; |
| 201 |
if (item->m_iprogramCount >= iOrder) |
| 202 |
{ |
| 203 |
//CLog::Log(LOGDEBUG,"{} fixing item at order {}", __FUNCTION__, item->m_iprogramCount); |
| 204 |
item->m_iprogramCount++; |
| 205 |
} |
| 206 |
++it; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void CPlayList::Clear() |
| 211 |
{ |
| 212 |
bool announce = false; |
| 213 |
if (!m_vecItems.empty()) |
| 214 |
{ |
| 215 |
m_vecItems.erase(m_vecItems.begin(), m_vecItems.end()); |
| 216 |
announce = true; |
| 217 |
} |
| 218 |
m_strPlayListName = ""; |
| 219 |
m_iPlayableItems = -1; |
| 220 |
m_bWasPlayed = false; |
| 221 |
|
| 222 |
if (announce) |
| 223 |
AnnounceClear(); |
| 224 |
} |
| 225 |
|
| 226 |
int CPlayList::size() const |
| 227 |
{ |
| 228 |
return (int)m_vecItems.size(); |
| 229 |
} |
| 230 |
|
| 231 |
const std::shared_ptr<CFileItem> CPlayList::operator[](int iItem) const |
| 232 |
{ |
| 233 |
if (iItem < 0 || iItem >= size()) |
| 234 |
{ |
| 235 |
assert(false); |
| 236 |
CLog::Log(LOGERROR, "Error trying to retrieve an item that's out of range"); |
| 237 |
return CFileItemPtr(); |
| 238 |
} |
| 239 |
return m_vecItems[iItem]; |
| 240 |
} |
| 241 |
|
| 242 |
std::shared_ptr<CFileItem> CPlayList::operator[](int iItem) |
| 243 |
{ |
| 244 |
if (iItem < 0 || iItem >= size()) |
| 245 |
{ |
| 246 |
assert(false); |
| 247 |
CLog::Log(LOGERROR, "Error trying to retrieve an item that's out of range"); |
| 248 |
return CFileItemPtr(); |
| 249 |
} |
| 250 |
return m_vecItems[iItem]; |
| 251 |
} |
| 252 |
|
| 253 |
void CPlayList::Shuffle(int iPosition) |
| 254 |
{ |
| 255 |
if (size() == 0) |
| 256 |
// nothing to shuffle, just set the flag for later |
| 257 |
m_bShuffled = true; |
| 258 |
else |
| 259 |
{ |
| 260 |
if (iPosition >= size()) |
| 261 |
return; |
| 262 |
if (iPosition < 0) |
| 263 |
iPosition = 0; |
| 264 |
CLog::Log(LOGDEBUG, "{} shuffling at pos:{}", __FUNCTION__, iPosition); |
| 265 |
|
| 266 |
ivecItems it = m_vecItems.begin() + iPosition; |
| 267 |
KODI::UTILS::RandomShuffle(it, m_vecItems.end()); |
| 268 |
|
| 269 |
// the list is now shuffled! |
| 270 |
m_bShuffled = true; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
struct SSortPlayListItem |
| 275 |
{ |
| 276 |
static bool PlaylistSort(const CFileItemPtr &left, const CFileItemPtr &right) |
| 277 |
{ |
| 278 |
return (left->m_iprogramCount < right->m_iprogramCount); |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
void CPlayList::UnShuffle() |
| 283 |
{ |
| 284 |
std::sort(m_vecItems.begin(), m_vecItems.end(), SSortPlayListItem::PlaylistSort); |
| 285 |
// the list is now unshuffled! |
| 286 |
m_bShuffled = false; |
| 287 |
} |
| 288 |
|
| 289 |
const std::string& CPlayList::GetName() const |
| 290 |
{ |
| 291 |
return m_strPlayListName; |
| 292 |
} |
| 293 |
|
| 294 |
void CPlayList::Remove(const std::string& strFileName) |
| 295 |
{ |
| 296 |
int iOrder = -1; |
| 297 |
int position = 0; |
| 298 |
ivecItems it; |
| 299 |
it = m_vecItems.begin(); |
| 300 |
while (it != m_vecItems.end() ) |
| 301 |
{ |
| 302 |
CFileItemPtr item = *it; |
| 303 |
if (item->GetPath() == strFileName) |
| 304 |
{ |
| 305 |
iOrder = item->m_iprogramCount; |
| 306 |
it = m_vecItems.erase(it); |
| 307 |
AnnounceRemove(position); |
| 308 |
//CLog::Log(LOGDEBUG,"PLAYLIST, removing item at order {}", iPos); |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
++position; |
| 313 |
++it; |
| 314 |
} |
| 315 |
} |
| 316 |
DecrementOrder(iOrder); |
| 317 |
} |
| 318 |
|
| 319 |
int CPlayList::FindOrder(int iOrder) const |
| 320 |
{ |
| 321 |
for (int i = 0; i < size(); i++) |
| 322 |
{ |
| 323 |
if (m_vecItems[i]->m_iprogramCount == iOrder) |
| 324 |
return i; |
| 325 |
} |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
// remove item from playlist by position |
| 330 |
void CPlayList::Remove(int position) |
| 331 |
{ |
| 332 |
int iOrder = -1; |
| 333 |
if (position >= 0 && position < (int)m_vecItems.size()) |
| 334 |
{ |
| 335 |
iOrder = m_vecItems[position]->m_iprogramCount; |
| 336 |
m_vecItems.erase(m_vecItems.begin() + position); |
| 337 |
} |
| 338 |
DecrementOrder(iOrder); |
| 339 |
|
| 340 |
AnnounceRemove(position); |
| 341 |
} |
| 342 |
|
| 343 |
int CPlayList::RemoveDVDItems() |
| 344 |
{ |
| 345 |
std::vector <std::string> vecFilenames; |
| 346 |
|
| 347 |
// Collect playlist items from DVD share |
| 348 |
ivecItems it; |
| 349 |
it = m_vecItems.begin(); |
| 350 |
while (it != m_vecItems.end() ) |
| 351 |
{ |
| 352 |
CFileItemPtr item = *it; |
| 353 |
if ( item->IsCDDA() || item->IsOnDVD() ) |
| 354 |
{ |
| 355 |
vecFilenames.push_back( item->GetPath() ); |
| 356 |
} |
| 357 |
++it; |
| 358 |
} |
| 359 |
|
| 360 |
// Delete them from playlist |
| 361 |
int nFileCount = vecFilenames.size(); |
| 362 |
if ( nFileCount ) |
| 363 |
{ |
| 364 |
std::vector <std::string>::iterator it; |
| 365 |
it = vecFilenames.begin(); |
| 366 |
while (it != vecFilenames.end() ) |
| 367 |
{ |
| 368 |
std::string& strFilename = *it; |
| 369 |
Remove( strFilename ); |
| 370 |
++it; |
| 371 |
} |
| 372 |
vecFilenames.erase( vecFilenames.begin(), vecFilenames.end() ); |
| 373 |
} |
| 374 |
return nFileCount; |
| 375 |
} |
| 376 |
|
| 377 |
bool CPlayList::Swap(int position1, int position2) |
| 378 |
{ |
| 379 |
if ( |
| 380 |
(position1 < 0) || |
| 381 |
(position2 < 0) || |
| 382 |
(position1 >= size()) || |
| 383 |
(position2 >= size()) |
| 384 |
) |
| 385 |
{ |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
if (!IsShuffled()) |
| 390 |
{ |
| 391 |
// swap the ordinals before swapping the items! |
| 392 |
//CLog::Log(LOGDEBUG,"PLAYLIST swapping items at orders ({}, {})",m_vecItems[position1]->m_iprogramCount,m_vecItems[position2]->m_iprogramCount); |
| 393 |
std::swap(m_vecItems[position1]->m_iprogramCount, m_vecItems[position2]->m_iprogramCount); |
| 394 |
} |
| 395 |
|
| 396 |
// swap the items |
| 397 |
std::swap(m_vecItems[position1], m_vecItems[position2]); |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
void CPlayList::SetUnPlayable(int iItem) |
| 402 |
{ |
| 403 |
if (iItem < 0 || iItem >= size()) |
| 404 |
{ |
| 405 |
CLog::Log(LOGWARNING, "Attempt to set unplayable index {}", iItem); |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
CFileItemPtr item = m_vecItems[iItem]; |
| 410 |
if (!item->GetProperty("unplayable").asBoolean()) |
| 411 |
{ |
| 412 |
item->SetProperty("unplayable", true); |
| 413 |
m_iPlayableItems--; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
bool CPlayList::Load(const std::string& strFileName) |
| 419 |
{ |
| 420 |
Clear(); |
| 421 |
m_strBasePath = URIUtils::GetDirectory(strFileName); |
| 422 |
|
| 423 |
CFileStream file; |
| 424 |
if (!file.Open(strFileName)) |
| 425 |
return false; |
| 426 |
|
| 427 |
if (file.GetLength() > 1024*1024) |
| 428 |
{ |
| 429 |
CLog::Log(LOGWARNING, "{} - File is larger than 1 MB, most likely not a playlist", |
| 430 |
__FUNCTION__); |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
return LoadData(file); |
| 435 |
} |
| 436 |
|
| 437 |
bool CPlayList::LoadData(std::istream &stream) |
| 438 |
{ |
| 439 |
// try to read as a string |
| 440 |
std::ostringstream ostr; |
| 441 |
ostr << stream.rdbuf(); |
| 442 |
return LoadData(ostr.str()); |
| 443 |
} |
| 444 |
|
| 445 |
bool CPlayList::LoadData(const std::string& strData) |
| 446 |
{ |
| 447 |
return false; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
bool CPlayList::Expand(int position) |
| 452 |
{ |
| 453 |
CFileItemPtr item = m_vecItems[position]; |
| 454 |
std::unique_ptr<CPlayList> playlist (CPlayListFactory::Create(*item.get())); |
| 455 |
if (playlist == nullptr) |
| 456 |
return false; |
| 457 |
|
| 458 |
std::string path = item->GetDynPath(); |
| 459 |
|
| 460 |
if (!playlist->Load(path)) |
| 461 |
return false; |
| 462 |
|
| 463 |
// remove any item that points back to itself |
| 464 |
for (int i = 0;i<playlist->size();i++) |
| 465 |
{ |
| 466 |
if (StringUtils::EqualsNoCase((*playlist)[i]->GetPath(), path)) |
| 467 |
{ |
| 468 |
playlist->Remove(i); |
| 469 |
i--; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
// @todo |
| 474 |
// never change original path (id) of a file item |
| 475 |
for (int i = 0;i<playlist->size();i++) |
| 476 |
{ |
| 477 |
(*playlist)[i]->SetDynPath((*playlist)[i]->GetPath()); |
| 478 |
(*playlist)[i]->SetPath(item->GetDynPath()); |
| 479 |
(*playlist)[i]->SetStartOffset(item->GetStartOffset()); |
| 480 |
} |
| 481 |
|
| 482 |
if (playlist->size() <= 0) |
| 483 |
return false; |
| 484 |
|
| 485 |
Remove(position); |
| 486 |
Insert(*playlist, position); |
| 487 |
return true; |
| 488 |
} |
| 489 |
|
| 490 |
void CPlayList::UpdateItem(const CFileItem *item) |
| 491 |
{ |
| 492 |
if (!item) return; |
| 493 |
|
| 494 |
for (ivecItems it = m_vecItems.begin(); it != m_vecItems.end(); ++it) |
| 495 |
{ |
| 496 |
CFileItemPtr playlistItem = *it; |
| 497 |
if (playlistItem->IsSamePath(item)) |
| 498 |
{ |
| 499 |
std::string temp = playlistItem->GetPath(); // save path, it may have been altered |
| 500 |
*playlistItem = *item; |
| 501 |
playlistItem->SetPath(temp); |
| 502 |
break; |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
const std::string& CPlayList::ResolveURL(const std::shared_ptr<CFileItem>& item) const |
| 508 |
{ |
| 509 |
if (item->IsMusicDb() && item->HasMusicInfoTag()) |
| 510 |
return item->GetMusicInfoTag()->GetURL(); |
| 511 |
else |
| 512 |
return item->GetDynPath(); |
| 513 |
} |