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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
/*
* Copyright (C) 2012-2019 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 "PVRGUIDirectory.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "guilib/LocalizeStrings.h"
#include "guilib/WindowIDs.h"
#include "input/WindowTranslator.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/channels/PVRChannel.h"
#include "pvr/channels/PVRChannelGroupMember.h"
#include "pvr/channels/PVRChannelGroups.h"
#include "pvr/channels/PVRChannelGroupsContainer.h"
#include "pvr/channels/PVRChannelsPath.h"
#include "pvr/epg/EpgContainer.h"
#include "pvr/epg/EpgSearchFilter.h"
#include "pvr/epg/EpgSearchPath.h"
#include "pvr/recordings/PVRRecording.h"
#include "pvr/recordings/PVRRecordings.h"
#include "pvr/recordings/PVRRecordingsPath.h"
#include "pvr/timers/PVRTimerInfoTag.h"
#include "pvr/timers/PVRTimers.h"
#include "pvr/timers/PVRTimersPath.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
#include <memory>
#include <set>
#include <string>
#include <vector>
using namespace PVR;
bool CPVRGUIDirectory::Exists() const
{
if (!CServiceBroker::GetPVRManager().IsStarted())
return false;
return m_url.IsProtocol("pvr") && StringUtils::StartsWith(m_url.GetFileName(), "recordings");
}
bool CPVRGUIDirectory::SupportsWriteFileOperations() const
{
if (!CServiceBroker::GetPVRManager().IsStarted())
return false;
const std::string filename = m_url.GetFileName();
return URIUtils::IsPVRRecording(filename);
}
namespace
{
bool GetRootDirectory(bool bRadio, CFileItemList& results)
{
std::shared_ptr<CFileItem> item;
const std::shared_ptr<CPVRClients> clients = CServiceBroker::GetPVRManager().Clients();
// EPG
const bool bAnyClientSupportingEPG = clients->AnyClientSupportingEPG();
if (bAnyClientSupportingEPG)
{
item.reset(
new CFileItem(StringUtils::Format("pvr://guide/{}/", bRadio ? "radio" : "tv"), true));
item->SetLabel(g_localizeStrings.Get(19069)); // Guide
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(bRadio ? WINDOW_RADIO_GUIDE
: WINDOW_TV_GUIDE));
item->SetArt("icon", "DefaultPVRGuide.png");
results.Add(item);
}
// Channels
item.reset(new CFileItem(
bRadio ? CPVRChannelsPath::PATH_RADIO_CHANNELS : CPVRChannelsPath::PATH_TV_CHANNELS, true));
item->SetLabel(g_localizeStrings.Get(19019)); // Channels
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(bRadio ? WINDOW_RADIO_CHANNELS
: WINDOW_TV_CHANNELS));
item->SetArt("icon", "DefaultPVRChannels.png");
results.Add(item);
// Recordings
if (clients->AnyClientSupportingRecordings())
{
item.reset(new CFileItem(bRadio ? CPVRRecordingsPath::PATH_ACTIVE_RADIO_RECORDINGS
: CPVRRecordingsPath::PATH_ACTIVE_TV_RECORDINGS,
true));
item->SetLabel(g_localizeStrings.Get(19017)); // Recordings
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(
bRadio ? WINDOW_RADIO_RECORDINGS : WINDOW_TV_RECORDINGS));
item->SetArt("icon", "DefaultPVRRecordings.png");
results.Add(item);
}
// Timers/Timer rules
// - always present, because Reminders are always available, no client support needed for this
item.reset(new CFileItem(
bRadio ? CPVRTimersPath::PATH_RADIO_TIMERS : CPVRTimersPath::PATH_TV_TIMERS, true));
item->SetLabel(g_localizeStrings.Get(19040)); // Timers
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(bRadio ? WINDOW_RADIO_TIMERS
: WINDOW_TV_TIMERS));
item->SetArt("icon", "DefaultPVRTimers.png");
results.Add(item);
item.reset(new CFileItem(
bRadio ? CPVRTimersPath::PATH_RADIO_TIMER_RULES : CPVRTimersPath::PATH_TV_TIMER_RULES, true));
item->SetLabel(g_localizeStrings.Get(19138)); // Timer rules
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(
bRadio ? WINDOW_RADIO_TIMER_RULES : WINDOW_TV_TIMER_RULES));
item->SetArt("icon", "DefaultPVRTimerRules.png");
results.Add(item);
// Search
if (bAnyClientSupportingEPG)
{
item.reset(new CFileItem(
bRadio ? CPVREpgSearchPath::PATH_RADIO_SEARCH : CPVREpgSearchPath::PATH_TV_SEARCH, true));
item->SetLabel(g_localizeStrings.Get(137)); // Search
item->SetProperty("node.target", CWindowTranslator::TranslateWindow(bRadio ? WINDOW_RADIO_SEARCH
: WINDOW_TV_SEARCH));
item->SetArt("icon", "DefaultPVRSearch.png");
results.Add(item);
}
return true;
}
} // unnamed namespace
bool CPVRGUIDirectory::GetDirectory(CFileItemList& results) const
{
std::string base = m_url.Get();
URIUtils::RemoveSlashAtEnd(base);
std::string fileName = m_url.GetFileName();
URIUtils::RemoveSlashAtEnd(fileName);
results.SetCacheToDisc(CFileItemList::CACHE_NEVER);
if (fileName.empty())
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
std::shared_ptr<CFileItem> item;
item.reset(new CFileItem(base + "channels/", true));
item->SetLabel(g_localizeStrings.Get(19019)); // Channels
item->SetLabelPreformatted(true);
results.Add(item);
item.reset(new CFileItem(base + "recordings/active/", true));
item->SetLabel(g_localizeStrings.Get(19017)); // Recordings
item->SetLabelPreformatted(true);
results.Add(item);
item.reset(new CFileItem(base + "recordings/deleted/", true));
item->SetLabel(g_localizeStrings.Get(19184)); // Deleted recordings
item->SetLabelPreformatted(true);
results.Add(item);
// Sort by name only. Labels are preformatted.
results.AddSortMethod(SortByLabel, 551 /* Name */, LABEL_MASKS("%L", "", "%L", ""));
}
return true;
}
else if (StringUtils::StartsWith(fileName, "tv"))
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
return GetRootDirectory(false, results);
}
return true;
}
else if (StringUtils::StartsWith(fileName, "radio"))
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
return GetRootDirectory(true, results);
}
return true;
}
else if (StringUtils::StartsWith(fileName, "recordings"))
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
return GetRecordingsDirectory(results);
}
return true;
}
else if (StringUtils::StartsWith(fileName, "channels"))
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
return GetChannelsDirectory(results);
}
return true;
}
else if (StringUtils::StartsWith(fileName, "timers"))
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
return GetTimersDirectory(results);
}
return true;
}
const CPVREpgSearchPath path(m_url.Get());
if (path.IsValid())
{
if (CServiceBroker::GetPVRManager().IsStarted())
{
if (path.IsSavedSearchesRoot())
return GetSavedSearchesDirectory(path.IsRadio(), results);
}
return true;
}
return false;
}
bool CPVRGUIDirectory::HasTVRecordings()
{
return CServiceBroker::GetPVRManager().IsStarted() &&
CServiceBroker::GetPVRManager().Recordings()->GetNumTVRecordings() > 0;
}
bool CPVRGUIDirectory::HasDeletedTVRecordings()
{
return CServiceBroker::GetPVRManager().IsStarted() &&
CServiceBroker::GetPVRManager().Recordings()->HasDeletedTVRecordings();
}
bool CPVRGUIDirectory::HasRadioRecordings()
{
return CServiceBroker::GetPVRManager().IsStarted() &&
CServiceBroker::GetPVRManager().Recordings()->GetNumRadioRecordings() > 0;
}
bool CPVRGUIDirectory::HasDeletedRadioRecordings()
{
return CServiceBroker::GetPVRManager().IsStarted() &&
CServiceBroker::GetPVRManager().Recordings()->HasDeletedRadioRecordings();
}
namespace
{
std::string TrimSlashes(const std::string& strOrig)
{
std::string strReturn = strOrig;
while (strReturn[0] == '/')
strReturn.erase(0, 1);
URIUtils::RemoveSlashAtEnd(strReturn);
return strReturn;
}
bool IsDirectoryMember(const std::string& strDirectory,
const std::string& strEntryDirectory,
bool bGrouped)
{
const std::string strUseDirectory = TrimSlashes(strDirectory);
const std::string strUseEntryDirectory = TrimSlashes(strEntryDirectory);
// Case-insensitive comparison since sub folders are created with case-insensitive matching (GetSubDirectories)
if (bGrouped)
return StringUtils::EqualsNoCase(strUseDirectory, strUseEntryDirectory);
else
return StringUtils::StartsWithNoCase(strUseEntryDirectory, strUseDirectory);
}
void GetSubDirectories(const CPVRRecordingsPath& recParentPath,
const std::vector<std::shared_ptr<CPVRRecording>>& recordings,
CFileItemList& results)
{
// Only active recordings are fetched to provide sub directories.
// Not applicable for deleted view which is supposed to be flattened.
std::set<std::shared_ptr<CFileItem>> unwatchedFolders;
bool bRadio = recParentPath.IsRadio();
for (const auto& recording : recordings)
{
if (recording->IsDeleted())
continue;
if (recording->IsRadio() != bRadio)
continue;
const std::string strCurrent =
recParentPath.GetUnescapedSubDirectoryPath(recording->Directory());
if (strCurrent.empty())
continue;
CPVRRecordingsPath recChildPath(recParentPath);
recChildPath.AppendSegment(strCurrent);
const std::string strFilePath = recChildPath;
std::shared_ptr<CFileItem> item;
if (!results.Contains(strFilePath))
{
item.reset(new CFileItem(strCurrent, true));
item->SetPath(strFilePath);
item->SetLabel(strCurrent);
item->SetLabelPreformatted(true);
item->m_dateTime = recording->RecordingTimeAsLocalTime();
item->SetProperty("totalepisodes", 0);
item->SetProperty("watchedepisodes", 0);
item->SetProperty("unwatchedepisodes", 0);
item->SetProperty("sizeinbytes", UINT64_C(0));
// Assume all folders are watched, we'll change the overlay later
item->SetOverlayImage(CGUIListItem::ICON_OVERLAY_WATCHED, false);
results.Add(item);
}
else
{
item = results.Get(strFilePath);
if (item->m_dateTime < recording->RecordingTimeAsLocalTime())
item->m_dateTime = recording->RecordingTimeAsLocalTime();
}
item->IncrementProperty("totalepisodes", 1);
if (recording->GetPlayCount() == 0)
{
unwatchedFolders.insert(item);
item->IncrementProperty("unwatchedepisodes", 1);
}
else
{
item->IncrementProperty("watchedepisodes", 1);
}
item->SetLabel2(StringUtils::Format("{} / {}", item->GetProperty("watchedepisodes").asString(),
item->GetProperty("totalepisodes").asString()));
item->IncrementProperty("sizeinbytes", recording->GetSizeInBytes());
}
// Replace the incremental size of the recordings with a string equivalent
for (auto& item : results.GetList())
{
int64_t size = item->GetProperty("sizeinbytes").asInteger();
item->ClearProperty("sizeinbytes");
item->m_dwSize = size; // We'll also sort recording folders by size
if (size > 0)
item->SetProperty("recordingsize", StringUtils::SizeToString(size));
}
// Change the watched overlay to unwatched for folders containing unwatched entries
for (auto& item : unwatchedFolders)
item->SetOverlayImage(CGUIListItem::ICON_OVERLAY_UNWATCHED, false);
}
} // unnamed namespace
bool CPVRGUIDirectory::GetRecordingsDirectory(CFileItemList& results) const
{
results.SetContent("recordings");
bool bGrouped = false;
const std::vector<std::shared_ptr<CPVRRecording>> recordings =
CServiceBroker::GetPVRManager().Recordings()->GetAll();
if (m_url.HasOption("view"))
{
const std::string view = m_url.GetOption("view");
if (view == "grouped")
bGrouped = true;
else if (view == "flat")
bGrouped = false;
else
{
CLog::LogF(LOGERROR, "Unsupported value '{}' for url parameter 'view'", view);
return false;
}
}
else
{
bGrouped = CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
CSettings::SETTING_PVRRECORD_GROUPRECORDINGS);
}
const CPVRRecordingsPath recPath(m_url.GetWithoutOptions());
if (recPath.IsValid())
{
// Get the directory structure if in non-flatten mode
// Deleted view is always flatten. So only for an active view
const std::string strDirectory = recPath.GetUnescapedDirectoryPath();
if (!recPath.IsDeleted() && bGrouped)
GetSubDirectories(recPath, recordings, results);
// get all files of the current directory or recursively all files starting at the current directory if in flatten mode
std::shared_ptr<CFileItem> item;
for (const auto& recording : recordings)
{
// Omit recordings not matching criteria
if (recording->IsDeleted() != recPath.IsDeleted() ||
recording->IsRadio() != recPath.IsRadio() ||
!IsDirectoryMember(strDirectory, recording->Directory(), bGrouped))
continue;
item = std::make_shared<CFileItem>(recording);
item->SetOverlayImage(CGUIListItem::ICON_OVERLAY_UNWATCHED, recording->GetPlayCount() > 0);
results.Add(item);
}
}
return recPath.IsValid();
}
bool CPVRGUIDirectory::GetSavedSearchesDirectory(bool bRadio, CFileItemList& results) const
{
const std::vector<std::shared_ptr<CPVREpgSearchFilter>> searches =
CServiceBroker::GetPVRManager().EpgContainer().GetSavedSearches(bRadio);
for (const auto& search : searches)
{
results.Add(std::make_shared<CFileItem>(search));
}
return true;
}
bool CPVRGUIDirectory::GetChannelGroupsDirectory(bool bRadio,
bool bExcludeHidden,
CFileItemList& results)
{
const CPVRChannelGroups* channelGroups =
CServiceBroker::GetPVRManager().ChannelGroups()->Get(bRadio);
if (channelGroups)
{
std::shared_ptr<CFileItem> item;
const std::vector<std::shared_ptr<CPVRChannelGroup>> groups =
channelGroups->GetMembers(bExcludeHidden);
for (const auto& group : groups)
{
item = std::make_shared<CFileItem>(group->GetPath(), true);
item->m_strTitle = group->GroupName();
item->SetLabel(group->GroupName());
results.Add(item);
}
return true;
}
return false;
}
bool CPVRGUIDirectory::GetChannelsDirectory(CFileItemList& results) const
{
const CPVRChannelsPath path(m_url.GetWithoutOptions());
if (path.IsValid())
{
if (path.IsEmpty())
{
std::shared_ptr<CFileItem> item;
// all tv channels
item.reset(new CFileItem(CPVRChannelsPath::PATH_TV_CHANNELS, true));
item->SetLabel(g_localizeStrings.Get(19020)); // TV
item->SetLabelPreformatted(true);
results.Add(item);
// all radio channels
item.reset(new CFileItem(CPVRChannelsPath::PATH_RADIO_CHANNELS, true));
item->SetLabel(g_localizeStrings.Get(19021)); // Radio
item->SetLabelPreformatted(true);
results.Add(item);
return true;
}
else if (path.IsChannelsRoot())
{
return GetChannelGroupsDirectory(path.IsRadio(), true, results);
}
else if (path.IsChannelGroup())
{
const std::string& strGroupName = path.GetGroupName();
bool bShowHiddenChannels = path.IsHiddenChannelGroup();
std::shared_ptr<CPVRChannelGroup> group;
if (bShowHiddenChannels || strGroupName == "*") // all channels
{
group = CServiceBroker::GetPVRManager().ChannelGroups()->GetGroupAll(path.IsRadio());
}
else
{
group = CServiceBroker::GetPVRManager()
.ChannelGroups()
->Get(path.IsRadio())
->GetByName(strGroupName);
}
if (group)
{
const bool playedOnly =
(m_url.HasOption("view") && (m_url.GetOption("view") == "lastplayed"));
const std::vector<std::shared_ptr<CPVRChannelGroupMember>> groupMembers =
group->GetMembers();
for (const auto& groupMember : groupMembers)
{
if (bShowHiddenChannels != groupMember->Channel()->IsHidden())
continue;
if (playedOnly && !groupMember->Channel()->LastWatched())
continue;
results.Add(std::make_shared<CFileItem>(groupMember));
}
}
else
{
CLog::LogF(LOGERROR, "Unable to obtain members of channel group '{}'", strGroupName);
return false;
}
return true;
}
}
return false;
}
namespace
{
bool GetTimersRootDirectory(const CPVRTimersPath& path,
bool bHideDisabled,
const std::vector<std::shared_ptr<CPVRTimerInfoTag>>& timers,
CFileItemList& results)
{
bool bRadio = path.IsRadio();
bool bRules = path.IsRules();
for (const auto& timer : timers)
{
if ((bRadio == timer->IsRadio() ||
(bRules && timer->ClientChannelUID() == PVR_TIMER_ANY_CHANNEL)) &&
(bRules == timer->IsTimerRule()) && (!bHideDisabled || !timer->IsDisabled()))
{
const auto item = std::make_shared<CFileItem>(timer);
const CPVRTimersPath timersPath(path.GetPath(), timer->ClientID(), timer->ClientIndex());
item->SetPath(timersPath.GetPath());
results.Add(item);
}
}
return true;
}
bool GetTimersSubDirectory(const CPVRTimersPath& path,
bool bHideDisabled,
const std::vector<std::shared_ptr<CPVRTimerInfoTag>>& timers,
CFileItemList& results)
{
bool bRadio = path.IsRadio();
int iParentId = path.GetParentId();
int iClientId = path.GetClientId();
std::shared_ptr<CFileItem> item;
for (const auto& timer : timers)
{
if ((timer->IsRadio() == bRadio) && timer->HasParent() && (timer->ClientID() == iClientId) &&
(timer->ParentClientIndex() == iParentId) && (!bHideDisabled || !timer->IsDisabled()))
{
item.reset(new CFileItem(timer));
const CPVRTimersPath timersPath(path.GetPath(), timer->ClientID(), timer->ClientIndex());
item->SetPath(timersPath.GetPath());
results.Add(item);
}
}
return true;
}
} // unnamed namespace
bool CPVRGUIDirectory::GetTimersDirectory(CFileItemList& results) const
{
const CPVRTimersPath path(m_url.GetWithoutOptions());
if (path.IsValid() && (path.IsTimersRoot() || path.IsTimerRule()))
{
bool bHideDisabled = false;
if (m_url.HasOption("view"))
{
const std::string view = m_url.GetOption("view");
if (view == "hidedisabled")
{
bHideDisabled = true;
}
else
{
CLog::LogF(LOGERROR, "Unsupported value '{}' for url parameter 'view'", view);
return false;
}
}
else
{
bHideDisabled = CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
CSettings::SETTING_PVRTIMERS_HIDEDISABLEDTIMERS);
}
const std::vector<std::shared_ptr<CPVRTimerInfoTag>> timers =
CServiceBroker::GetPVRManager().Timers()->GetAll();
if (path.IsTimersRoot())
{
/* Root folder containing either timer rules or timers. */
return GetTimersRootDirectory(path, bHideDisabled, timers, results);
}
else if (path.IsTimerRule())
{
/* Sub folder containing the timers scheduled by the given timer rule. */
return GetTimersSubDirectory(path, bHideDisabled, timers, results);
}
}
return false;
}
|