Repository navigation
Expand file tree
/
Copy pathmenu.cpp
More file actions
876 lines (784 loc) · 22.3 KB
/
Copy pathmenu.cpp
File metadata and controls
876 lines (784 loc) · 22.3 KB
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file menu.cpp
* Plugin Main Menu
*
* @copyright 2020 zille. All Rights Reserved.
* @copyright 2025 - 2026 by Andreas Baierl. All Rights Reserved.
*
* @license{AGPL-3.0-or-later}
*/
#include <cstdlib>
#include <ftw.h>
#include <string>
#include <unistd.h>
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/osdbase.h>
#include <vdr/plugin.h>
#include <vdr/recording.h>
#include <vdr/remote.h>
#include <vdr/skins.h>
#include <vdr/videodir.h>
#include "device.h"
#include "logger.h"
#include "mediaplayer.h"
#include "menu.h"
bool cDeviceMenu::s_openMediaMenu = false; ///< open the file menu of the stopped mediaplayer
std::string cDeviceMenu::s_mediaSource; ///< file or playlist of the stopped mediaplayer
/**
* Build main or playlist menu
*/
cDeviceMenu::cDeviceMenu(const char *title, cOutputDevice *device,
int c0, int c1, int c2, int c3, int c4)
: cOsdMenu(title, c0, c1, c2, c3, c4),
m_pDevice(device)
{
pMenu = this;
m_playlist.clear();
if (cDrmDeviceControl::Control() && cDrmDeviceControl::Control()->Player()->GetFirstPlaylistEntry()) {
LOGDEBUG2(L_MEDIA, "mediaplayer: %s: pointer to cDrmDeviceControl exist.", __FUNCTION__);
PlayListMenu(); // Test if PL!!!
} else if (s_openMediaMenu) {
s_openMediaMenu = false;
MediaMenu(s_mediaSource);
} else {
MainMenu();
}
}
cDeviceMenu::~cDeviceMenu()
{
pMenu = NULL;
}
/**
* Create a seperator item
*
* @param label text inside separator
*
* @ingroup menu
*/
static inline cOsdItem *SeparatorName(const char *label)
{
return new cOsdItem(cString::sprintf("%s:", label), osUnknown, false);
}
/**
* Create an empty seperator item
*
* @ingroup menu
*/
static inline cOsdItem *SeparatorSpace(void)
{
return new cOsdItem(" ", osUnknown, false);
}
/**
* Open the plugin menu with the file of the stopped mediaplayer
*
* Used by the mediaplayer's Back key, which stops the replay and returns
* to the mediaplayer's file menu (like VDR's replay returns to the
* recordings menu).
*
* @param source played file or playlist
*/
void cDeviceMenu::OpenMediaMenu(const char *source)
{
s_mediaSource = source ? source : "";
s_openMediaMenu = true;
cRemote::CallPlugin(PLUGIN_NAME_I18N);
}
/**
* Create the file menu with the given file selected
*
* Falls back to the main menu, if the file isn't a local file (e.g. an url).
*
* @param source file or playlist
*/
void cDeviceMenu::MediaMenu(const std::string &source)
{
size_t slash = source.find_last_of('/');
if (source.empty() || source[0] != '/' || slash == std::string::npos) {
MainMenu();
return;
}
m_lastItem = source.substr(slash + 1);
m_path = source.substr(0, slash);
FindFileMenu(m_path, NULL);
}
/**
* Create main menu
*/
void cDeviceMenu::MainMenu(void)
{
int current;
m_shownMenu = ShownMenu::Main;
current = Current(); // get current menu item index
Clear(); // clear the menu
// pip
if (m_pDevice->UsePip()) {
Add(SeparatorName("PIP"));
Add(new cOsdItem(hk(tr(" Toggle")), osUser1));
Add(new cOsdItem(hk(tr(" Channel +")), osUser2));
Add(new cOsdItem(hk(tr(" Channel -")), osUser3));
Add(new cOsdItem(hk(tr(" Swap channels")), osUser4));
Add(new cOsdItem(hk(tr(" Swap position")), osUser5));
Add(new cOsdItem(hk(tr(" Switch to pip channel")), osUser6));
}
Add(SeparatorSpace());
// detach
Add(SeparatorName("Detach"));
Add(new cOsdItem(hk(tr(" Detach device")), osUser7));
Add(SeparatorSpace());
// mediaplayer
Add(SeparatorName("Mediaplayer"));
Add(new cOsdItem(hk(tr(" play file / make play list")), osUser8));
Add(new cOsdItem(hk(tr(" select play list")), osUser9));
Add(SeparatorSpace());
// zoom
Add(SeparatorName("Zoom"));
Add(m_pZoomItem = new cOsdItem(hk(tr(" Next zoom level"))));
Add(SeparatorSpace());
// audio
Add(SeparatorName("Audio"));
Add(m_pPassthroughItem = new cOsdItem(hk(m_pDevice->IsPassthroughOn() ? tr(" Switch passthrough off") : tr(" Switch passthrough on"))));
SetCurrent(Get(current)); // restore selected menu entry
Display();
}
/**
* Hotkeys
*
* @ingroup menu
*/
enum Hotkeys {
PIPKEYBASE = 100,
PIPTOGGLEONOFF,
PIPCHANNELUP,
PIPCHANNELDOWN,
PIPCHANNELSWAP,
PIPPOSITIONSWAP,
PIPCHANNELSWITCHBACK,
DETACHKEYBASE = 110,
DETACHDEVICE,
ATTACHDEVICE,
ZOOMKEYBASE = 120,
ZOOMNEXT,
ZOOMOFF,
AUDIOKEYBASE = 130,
PASSTHROUGHTOGGLE
};
/**
* Select the next zoom level and show it
*/
void cDeviceMenu::NextZoomLevel(void)
{
m_pDevice->NextZoomLevel();
Skins.QueueMessage(mtInfo, m_pDevice->ZoomLevelString());
}
/**
* Switch the passthrough on or off and show it
*/
void cDeviceMenu::TogglePassthrough(void)
{
m_pDevice->TogglePassthrough();
Skins.QueueMessage(mtInfo, m_pDevice->PassthroughString());
}
/**
* Handle a key code which was compose by hotkey handling in ProcessKey()
*
* @param code hotkey code to handle (see enum Hotkeys)
*/
void cDeviceMenu::HandleHotKey(int code) {
switch (code) {
// pip
case PIPTOGGLEONOFF:
m_pDevice->PipToggle();
break;
case PIPCHANNELUP:
m_pDevice->PipChannelChange(1);
break;
case PIPCHANNELDOWN:
m_pDevice->PipChannelChange(-1);
break;
case PIPCHANNELSWAP:
m_pDevice->PipChannelSwap(false);
break;
case PIPPOSITIONSWAP:
m_pDevice->PipSwapPosition();
break;
case PIPCHANNELSWITCHBACK:
m_pDevice->PipChannelSwap(true);
break;
// detach/ attach
case DETACHDEVICE:
m_pDevice->Detach();
break;
case ATTACHDEVICE:
m_pDevice->Attach();
break;
// zoom
case ZOOMNEXT:
NextZoomLevel();
break;
case ZOOMOFF:
m_pDevice->SetZoomLevel(0);
Skins.QueueMessage(mtInfo, m_pDevice->ZoomLevelString());
break;
// audio
case PASSTHROUGHTOGGLE:
TogglePassthrough();
break;
default:
break;
}
}
/**
* Handle key event
*
* @param key key event
*/
eOSState cDeviceMenu::ProcessKey(eKeys key)
{
eOSState state;
cOsdItem *item;
if (key == kNone)
UpdatePlayListMenu();
switch (m_hotkeyState) {
case HotkeyState::Initial:
// the color keys have other functions in the mediaplayer menus
if (m_shownMenu != ShownMenu::Main)
break;
if (key == kBlue) {
if (!m_pDevice->UsePip())
return osEnd;
m_hotkeyState = HotkeyState::Blue;
return osContinue;
}
if (key == kRed) {
m_hotkeyState = HotkeyState::Red;
return osContinue;
}
if (key == kGreen) {
m_hotkeyState = HotkeyState::Green;
return osContinue;
}
if (key == kYellow) {
m_hotkeyState = HotkeyState::Yellow;
return osContinue;
}
break;
case HotkeyState::Blue: // pip
if (k0 <= key && key <= k9) {
int hotkeyCode = static_cast<int>(PIPKEYBASE) + static_cast<int>(key) - static_cast<int>(k0);
m_hotkeyState = HotkeyState::Initial;
HandleHotKey(hotkeyCode);
return osEnd;
}
m_hotkeyState = HotkeyState::Initial;
break;
case HotkeyState::Red: // detach/ attach
if (k0 <= key && key <= k9) {
int hotkeyCode = static_cast<int>(DETACHKEYBASE) + static_cast<int>(key) - static_cast<int>(k0);
m_hotkeyState = HotkeyState::Initial;
HandleHotKey(hotkeyCode);
return osEnd;
}
m_hotkeyState = HotkeyState::Initial;
break;
case HotkeyState::Green: // zoom
if (k0 <= key && key <= k9) {
int hotkeyCode = static_cast<int>(ZOOMKEYBASE) + static_cast<int>(key) - static_cast<int>(k0);
m_hotkeyState = HotkeyState::Initial;
HandleHotKey(hotkeyCode);
return osEnd;
}
m_hotkeyState = HotkeyState::Initial;
break;
case HotkeyState::Yellow: // audio
if (k0 <= key && key <= k9) {
int hotkeyCode = static_cast<int>(AUDIOKEYBASE) + static_cast<int>(key) - static_cast<int>(k0);
m_hotkeyState = HotkeyState::Initial;
HandleHotKey(hotkeyCode);
return osEnd;
}
m_hotkeyState = HotkeyState::Initial;
break;
}
item = (cOsdItem *) Get(Current());
// zoom
if (key == kOk && m_shownMenu == ShownMenu::Main && item && item == m_pZoomItem) {
NextZoomLevel();
return osEnd;
}
// audio
if (key == kOk && m_shownMenu == ShownMenu::Main && item && item == m_pPassthroughItem) {
TogglePassthrough();
return osEnd;
}
state = cOsdMenu::ProcessKey(key);
switch (state) {
// pip
case osUser1: // toggle pip
m_pDevice->PipToggle();
return osEnd;
case osUser2: // pip channel +
m_pDevice->PipChannelChange(1);
return osEnd;
case osUser3: // pip channel -
m_pDevice->PipChannelChange(-1);
return osEnd;
case osUser4: // pip channel swap
m_pDevice->PipChannelSwap(false);
return osEnd;
case osUser5: // pip position swap
m_pDevice->PipSwapPosition();
return osEnd;
case osUser6: // pip switch main stream back to pip stream and close pip
m_pDevice->PipChannelSwap(true);
return osEnd;
// detach
case osUser7: // detach device
m_pDevice->Detach();
return osEnd;
// mediaplayer
case osUser8: // play file / make play list
m_path = cVideoDirectory::Name();
FindFileMenu(m_path, NULL);
return osContinue;
case osUser9: // select play list
m_path = cPlugin::ConfigDirectory("drmdevice");
SelectPlaylistMenu();
return osContinue;
default:
break;
}
switch (key) {
case kOk:
if (!item) // empty menu
break;
if (strcasestr(item->Text(), "[..]")) {
std::string newPath = m_path.substr(0 ,m_path.find_last_of("/"));
if (!m_lastItem.empty())
m_lastItem.clear();
m_lastItem = m_path.substr(m_path.find_last_of("/") + 1);
m_path = newPath;
FindFileMenu(m_path, NULL);
break;
}
if (cDrmDeviceControl::Control() && cDrmDeviceControl::Control()->Player()->GetCurrentPlaylistEntry()) {
cDrmDeviceControl::Control()->Player()->SetEntry(Current());
// PlayListMenu();
break;
}
if (IsValidMediaFile(item->Text())) {
PlayMedia(item->Text());
return osEnd;
} else {
std::string newPath = m_path + "/" + item->Text();
struct stat sb;
if (stat(newPath.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
m_path = newPath;
FindFileMenu(newPath, NULL);
}
}
break;
case kRed:
if (cDrmDeviceControl::Control() && cDrmDeviceControl::Control()->Player()->GetCurrentPlaylistEntry()) {
cDrmDeviceControl::Control()->Player()->ToggleRandomPlay();
PlayListMenu();
break;
}
if (m_playlist.empty()) {
if (item && IsValidMediaFile(item->Text())) {
PlayMedia(item->Text());
return osEnd;
}
} else {
m_path = cPlugin::ConfigDirectory("drmdevice");
PlayMedia(m_playlist.c_str());
return osEnd;
}
break;
case kGreen:
if (cDrmDeviceControl::Control()) {
cDrmDeviceControl::Control()->Player()->SkipSeconds(-60);
} else if (item) {
MakePlayList(item->Text(), "w");
Interface->Confirm(tr("New Playlist"), 1, true);
if (!m_lastItem.empty())
m_lastItem.clear();
m_lastItem = item->Text();
FindFileMenu(m_path, NULL);
}
break;
case kYellow:
if (cDrmDeviceControl::Control()) {
cDrmDeviceControl::Control()->Player()->SkipSeconds(60);
} else if (item) {
MakePlayList(item->Text(), "a");
Interface->Confirm(tr("Added to Playlist"), 1, true);
}
break;
case kBlue:
if (m_shownMenu == ShownMenu::Files) {
if (item)
DeleteItem(item->Text());
return osContinue;
}
state = osStopReplay;
break;
case kPlay:
if (item && IsValidMediaFile(item->Text())) {
PlayMedia(item->Text());
return osEnd;
}
break;
case kNext:
if (cDrmDeviceControl::Control())
cDrmDeviceControl::Control()->Player()->NextEntry();
break;
default:
break;
}
return state;
}
/*****************************************************************************
* Mediaplayer sub menus
****************************************************************************/
/**
* Update the playlist menu, if the mediaplayer plays another title
*
* The player thread must not access the menu, so the menu checks for a new title
* itself. If the player ended, the main menu is shown.
*/
void cDeviceMenu::UpdatePlayListMenu(void)
{
if (m_shownMenu != ShownMenu::Playlist)
return;
cDrmDeviceControl *control = cDrmDeviceControl::Control();
if (!control || !control->Player()->GetFirstPlaylistEntry()) {
MainMenu();
return;
}
if (control->Player()->GetCurrentPlaylistEntry() != m_pShownEntry)
PlayListMenu();
}
/**
* Create mediaplayer playlist menu
*/
void cDeviceMenu::PlayListMenu(void)
{
cPlaylistEntry *entry = cDrmDeviceControl::Control()->Player()->GetFirstPlaylistEntry();
m_pShownEntry = cDrmDeviceControl::Control()->Player()->GetCurrentPlaylistEntry();
m_shownMenu = ShownMenu::Playlist;
Clear();
while (entry) {
std::string p_string = entry->OsdItemString();
Add(new cOsdItem(p_string.c_str()), entry == m_pShownEntry);
if (!entry->GetNextEntry())
break;
entry = entry->GetNextEntry();
}
SetHelp(cDrmDeviceControl::Control()->Player()->IsRandomPlayActive() ? "Random Play" : " No Random Play",
"Jump -1 min", "Jump +1 min", "End player");
Display();
}
/**
* Create mediaplayer select playlist menu
*/
void cDeviceMenu::SelectPlaylistMenu(void)
{
struct dirent **dirList;
int n, i;
if ((n = scandir(cPlugin::ConfigDirectory("drmdevice"), &dirList, NULL, alphasort)) == -1) {
LOGERROR("mediaplayer: %s: searching PL in %s failed (%d): %m", __FUNCTION__,
cPlugin::ConfigDirectory("drmdevice"), errno);
return;
}
m_shownMenu = ShownMenu::Playlists;
Clear();
for (i = 0; i < n; i++) {
if (dirList[i]->d_name[0] != '.' && (strcasestr(dirList[i]->d_name, ".M3U"))) {
Add(new cOsdItem(dirList[i]->d_name));
}
free(dirList[i]);
}
free(dirList);
SetHelp("Play PL", NULL, NULL, NULL);
Display();
}
/**
* Create mediaplayer sub menu find file or make a play list
*
* @param searchPath path to start search mediafile
* @param playlist if there is a play list write to play list else make a new menu
*/
void cDeviceMenu::FindFileMenu(const std::string &searchPath, FILE *playlist)
{
struct dirent **dirList;
int n, i;
const char * sp;
if (!searchPath.size())
sp = "/";
else
sp = searchPath.c_str();
if (!playlist) {
m_shownMenu = ShownMenu::Files;
Clear();
if (searchPath.size())
Add(new cOsdItem("[..]"));
}
if ((n = scandir(sp, &dirList, NULL, alphasort)) == -1) {
LOGERROR("mediaplayer: %s: scanning directory %s failed (%d): %m", __FUNCTION__, sp, errno);
} else {
struct stat fileAttributs;
for (i = 0; i < n; i++) {
std::string str = searchPath + "/" + dirList[i]->d_name;
if (stat(str.c_str(), &fileAttributs) == -1) {
LOGERROR("mediaplayer: %s: stat on %s failed (%d): %m", __FUNCTION__, str.c_str(), errno);
} else {
if (S_ISDIR(fileAttributs.st_mode) && dirList[i]->d_name[0] != '.') {
if (playlist) {
FindFileMenu(str, playlist);
} else {
Add(new cOsdItem(dirList[i]->d_name),
!m_lastItem.compare(0, m_lastItem.length(), dirList[i]->d_name));
}
}
}
}
for (i = 0; i < n; i++) {
std::string str = searchPath + "/" + dirList[i]->d_name;
if (stat(str.c_str(), &fileAttributs) == -1) {
LOGERROR("mediaplayer: %s: stat on %s failed (%d): %m", __FUNCTION__, str.c_str(), errno);
} else {
if (S_ISREG(fileAttributs.st_mode) && dirList[i]->d_name[0] != '.') {
if (playlist) {
if (IsValidMediaFile(dirList[i]->d_name))
fprintf(playlist, "%s/%s\n", searchPath.c_str(),
dirList[i]->d_name);
} else {
Add(new cOsdItem(dirList[i]->d_name), m_lastItem == dirList[i]->d_name);
}
}
}
}
for (i = 0; i < n; i++)
free(dirList[i]);
free(dirList);
}
if (!playlist) {
SetHelp( m_playlist.empty() ? "Play File" : "Play PL", "New PL", "Add to PL", trVDR("Button$Delete"));
// SetHelp(Control->Player->Running ? NULL : "Set new PL",
// Control->Player->Running ? "Play Menu" : "Select PL");
Display();
}
}
/**
* Make a play list
*
* @param target path to start search mediafiles
* @param mode open file mode
*/
void cDeviceMenu::MakePlayList(const char * target, const char * mode)
{
if (m_playlist.empty())
m_playlist = "/default.m3u";
std::string plPath = cPlugin::ConfigDirectory("drmdevice");
plPath.append(m_playlist.c_str());
FILE *playlist = fopen(plPath.c_str(), mode);
if (!playlist)
return;
if (IsValidMediaFile(target)) {
fprintf(playlist, "%s/%s\n", m_path.c_str(), target);
} else {
std::string str = m_path + "/" + target;
FindFileMenu(str, playlist);
}
fclose (playlist);
}
/**
* Play media file
*
* @param name file name
*/
void cDeviceMenu::PlayMedia(const char *name)
{
std::string aim = m_path + "/" + name;
if (!cDrmDeviceControl::Control()) {
cControl::Launch(new cDrmDeviceControl(aim.c_str(), m_pDevice));
} else {
LOGERROR("mediaplayer: %s: can't start %s", __FUNCTION__, aim.c_str());
}
}
/**
* Number of entries, which couldn't be deleted (see RemoveEntry())
*
* @ingroup menu
*/
static int s_deleteErrors = 0;
/**
* Delete an entry of a folder, which is deleted (callback of nftw())
*
* @param path path of the entry
* @param sb unused
* @param type type of the entry (FTW_DP for a folder, after its entries)
* @param ftwbuf unused
* @return 0 to continue with the next entry
*
* @ingroup menu
*/
static int RemoveEntry(const char *path, [[maybe_unused]] const struct stat *sb, int type, [[maybe_unused]] struct FTW *ftwbuf)
{
if ((type == FTW_DP ? rmdir(path) : unlink(path)) < 0) {
LOGERROR("mediaplayer: %s: can't delete %s: %m", __FUNCTION__, path);
s_deleteErrors++;
}
return 0;
}
/**
* Check, if a recording is recorded or replayed by VDR at the moment
*
* @param path path of a recording folder (".rec")
*
* @ingroup menu
*/
static bool IsRecordingInUse(const char *path)
{
const char *replaying = cReplayControl::NowReplaying();
return cRecordControls::GetRecordControl(path) || (replaying && !strcmp(path, replaying));
}
/**
* Set by FindRecordingInUse(), if a recording in the checked folder is in use
*
* @ingroup menu
*/
static bool s_recordingInUse = false;
/**
* Check a folder entry for a recording in use (callback of nftw())
*
* @param path path of the entry
* @param sb unused
* @param type type of the entry
* @param ftwbuf unused
* @return 0 to continue, 1 to stop, if a recording in use was found
*
* @ingroup menu
*/
static int FindRecordingInUse(const char *path, [[maybe_unused]] const struct stat *sb, int type, [[maybe_unused]] struct FTW *ftwbuf)
{
if (type == FTW_D && IsRecordingInUse(path)) {
s_recordingInUse = true;
return 1;
}
return 0;
}
/**
* Check, if a file or folder belongs to a recording, which is recorded or replayed
*
* The recording may be the folder itself, a folder in it or the folder,
* which contains the file or folder.
*
* @param path path of the file or folder (as VDR knows the recordings,
* below the video directory)
* @param isFolder true, if the path is a folder
*
* @ingroup menu
*/
static bool ContainsRecordingInUse(const std::string &path, bool isFolder)
{
// a recording, which contains the path
for (size_t pos = path.find(".rec"); pos != std::string::npos; pos = path.find(".rec", pos + 1)) {
size_t end = pos + 4;
if ((end == path.size() || path[end] == '/') && IsRecordingInUse(path.substr(0, end).c_str()))
return true;
}
// a recording in the folder
s_recordingInUse = false;
if (isFolder)
nftw(path.c_str(), FindRecordingInUse, 16, FTW_PHYS | FTW_MOUNT);
return s_recordingInUse;
}
/**
* Get the real path of a folder
*
* @param path path of the folder
* @return the canonical path (empty, if it doesn't exist)
*
* @ingroup menu
*/
static std::string RealPath(const std::string &path)
{
char *real = realpath(path.c_str(), nullptr);
std::string result = real ? real : "";
free(real);
return result;
}
/**
* Delete a file or a folder with all its entries in the file menu
*
* To protect the system, only entries in the video directory are deleted. A
* symbolic link is deleted itself, not its target, and a folder is only
* deleted on the file system of the folder (mounted file systems are kept).
*
* @param name name of the file or folder in the shown folder
*/
void cDeviceMenu::DeleteItem(const char *name)
{
if (!strcmp(name, "[..]"))
return;
std::string videoDir = RealPath(cVideoDirectory::Name());
std::string folder = RealPath(m_path);
if (videoDir.empty() || folder.empty() ||
(folder != videoDir && folder.compare(0, videoDir.size() + 1, videoDir + "/"))) {
Skins.Message(mtError, tr("Only files in the video directory can be deleted"));
return;
}
std::string path = folder + "/" + name;
struct stat sb;
if (lstat(path.c_str(), &sb) < 0)
return;
bool isFolder = S_ISDIR(sb.st_mode);
// VDR records into it or plays it: VDR deletes such a recording itself (Recordings menu)
if (ContainsRecordingInUse(m_path + "/" + name, isFolder)) {
Skins.Message(mtError, tr("Recording in use (recorded or replayed)!"));
return;
}
if (!Interface->Confirm(isFolder ? tr("Delete folder with all files?") : tr("Delete file?")))
return;
s_deleteErrors = 0;
if (isFolder)
nftw(path.c_str(), RemoveEntry, 16, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
else
RemoveEntry(path.c_str(), &sb, FTW_F, nullptr);
LOGINFO("mediaplayer: deleted %s%s", path.c_str(), s_deleteErrors ? " (with errors)" : "");
if (s_deleteErrors)
Skins.Message(mtError, tr("Error while deleting!"));
// VDR rereads the recordings, if a recording was deleted
cRecordings::TouchUpdate();
// select the next entry
int current = Current();
cOsdItem *next = static_cast<cOsdItem *>(Get(current + 1));
if (!next)
next = static_cast<cOsdItem *>(Get(current - 1));
m_lastItem = next ? next->Text() : "";
FindFileMenu(m_path, NULL);
}
/**
* Test if it's a media file - at least if it has the right file extension...
*
* @param name file name
* @return true if it's a media file
*/
bool cDeviceMenu::IsValidMediaFile(const char *name)
{
if (strcasestr(name, ".MP3") ||
strcasestr(name, ".MP4") ||
strcasestr(name, ".MKV") ||
strcasestr(name, ".MPG") ||
strcasestr(name, ".AVI") ||
strcasestr(name, ".M2TS") ||
strcasestr(name, ".MPEG") ||
strcasestr(name, ".M3U") ||
strcasestr(name, ".TS")) {
return true;
}
return false;
}