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
|
#include <vector>
#include <string>
#include <algorithm>
#include "filebrowser.h"
#include "mpv_service.h"
using std::string;
using std::vector;
using std::sort;
string cMpvFilebrowser::currentDir = "";
string cMpvFilebrowser::currentItem = "";
cMpvFilebrowser::cMpvFilebrowser(string RootDir)
:cOsdMenu("Filebrowser")
{
rootDir = RootDir;
if (currentDir == "")
currentDir = rootDir;
ShowDirectory(currentDir);
}
void cMpvFilebrowser::ShowDirectory(string Path)
{
Clear();
vector<string> Directories;
vector<string> Files;
DIR *hDir;
struct dirent *Entry;
hDir = opendir(Path.c_str());
while ((Entry = readdir(hDir)) != NULL)
{
if (!Entry || Entry->d_name[0] == '.')
continue;
struct stat Stat;
string Filename = Path + "/" + Entry->d_name;
stat(Filename.c_str(), &Stat);
if (S_ISDIR(Stat.st_mode))
Directories.push_back(Entry->d_name);
else if (S_ISREG(Stat.st_mode))
Files.push_back(Entry->d_name);
}
closedir(hDir);
sort(Directories.begin(), Directories.end());
sort(Files.begin(), Files.end());
for (unsigned int i=0; i<Directories.size(); i++)
AddItem(Path, Directories[i], true);
for (unsigned int i=0; i<Files.size(); i++)
AddItem(Path, Files[i], false);
string MenuTitle = "Filebrowser";
if (rootDir != Path)
MenuTitle += " (" + Path.substr(rootDir.size() + 1, string::npos) + ")";
SetTitle(MenuTitle.c_str());
SetHelp(NULL, NULL, "Shuffle", NULL);
Display();
}
void cMpvFilebrowser::AddItem(string Path, string Text, bool IsDir)
{
bool Current = false;
if (currentItem == Text)
Current = true;
Add(new cMpvFilebrowserMenuItem(Path, Text, IsDir), Current);
}
eOSState cMpvFilebrowser::ProcessKey(eKeys Key)
{
//TODO since we get more and more Keys we should use switch again
if (Key == kOk)
{
cMpvFilebrowserMenuItem *item = (cMpvFilebrowserMenuItem *) Get(Current());
string newPath = item->Path() + "/" + item->Text();
if (item->IsDirectory())
{
currentDir = newPath;
currentItem = "";
ShowDirectory(newPath);
return osContinue;
}
else
{
currentItem = item->Text();
PlayFile(newPath);
return osEnd;
}
}
else if (Key == kBack)
{
// we reached our root, so don't go back further and let VDR handle this (close submenu)
if (currentDir == rootDir)
return cOsdMenu::ProcessKey(Key);
currentItem = currentDir.substr(currentDir.find_last_of("/")+1, string::npos);
currentDir = currentDir.substr(0,currentDir.find_last_of("/"));
ShowDirectory(currentDir);
return osContinue;
}
else if (Key == kLeft || Key == kRight || Key == kUp || Key == kDown)
{
// first let VDR handle those keys or we get the previous item
eOSState State = cOsdMenu::ProcessKey(Key);
cMpvFilebrowserMenuItem *item = (cMpvFilebrowserMenuItem *) Get(Current());
currentItem = item->Text();
return State;
}
else if (Key == kYellow)
{
cMpvFilebrowserMenuItem *item = (cMpvFilebrowserMenuItem *) Get(Current());
string newPath = item->Path() + "/" + item->Text();
if (!item->IsDirectory())
{
currentItem = item->Text();
PlayFile(newPath, true);
return osEnd;
}
return osContinue;
}
return cOsdMenu::ProcessKey(Key);
}
bool cMpvFilebrowser::PlayFile(string Filename, bool Shuffle)
{
cPlugin *p;
p = cPluginManager::GetPlugin("mpv");
if (!p)
return false;
if (!Shuffle)
{
Mpv_PlayFile playFile;
playFile.Filename = (char *)Filename.c_str();
return p->Service("Mpv_PlayFile", &playFile);
}
else
{
Mpv_PlaylistShuffle shuffleFile;
shuffleFile.Filename = (char *)Filename.c_str();
return p->Service("Mpv_PlaylistShuffle", &shuffleFile);
}
// should never be reached, but silence a compiler warning
return false;
}
cMpvFilebrowserMenuItem::cMpvFilebrowserMenuItem(string Path, string Item, bool IsDir)
{
isDir = IsDir;
path = Path;
SetText(Item.c_str());
}
|