-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlaylistManager.cpp
More file actions
185 lines (153 loc) · 5.13 KB
/
PlaylistManager.cpp
File metadata and controls
185 lines (153 loc) · 5.13 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
#include "PlaylistManager.h"
#include "PlaylistListView.h"
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <stdio.h>
PlaylistManager::PlaylistManager(BMessenger target) : fTarget(target) {
fPlaylistView = new PlaylistListView("playlist", fTarget);
}
PlaylistManager::~PlaylistManager() {}
PlaylistListView *PlaylistManager::View() const { return fPlaylistView; }
void PlaylistManager::LoadAvailablePlaylists() {
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
if (fPlaylistBasePath.IsEmpty())
return;
path.SetTo(fPlaylistBasePath);
BDirectory dir(path.Path());
if (dir.InitCheck() != B_OK)
return;
BEntry entry;
while (dir.GetNextEntry(&entry) == B_OK) {
BPath filePath(&entry);
if (!filePath.Path() || !filePath.Leaf())
continue;
BString name = filePath.Leaf();
if (name.EndsWith(".m3u"))
name.Truncate(name.Length() - 4);
fPlaylistView->AddItem(name, filePath.Path());
}
}
/**
* @brief Loads a playlist from disk.
* @param name The name of the playlist (without extension).
* @return std::vector<BString> List of file paths in the playlist.
*/
std::vector<BString> PlaylistManager::LoadPlaylist(const BString &name) {
std::vector<BString> paths;
BPath dirPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &dirPath) != B_OK)
return paths;
if (fPlaylistBasePath.IsEmpty())
return paths;
dirPath.SetTo(fPlaylistBasePath);
BString playlistFile = name;
playlistFile += ".m3u";
dirPath.Append(playlistFile.String());
BFile file(dirPath.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK)
return paths;
BString line;
char ch;
while (file.Read(&ch, 1) == 1) {
if (ch == '\n') {
line.Trim();
if (!line.IsEmpty() && !line.StartsWith("#")) {
paths.push_back(line);
}
line.Truncate(0);
} else {
line += ch;
}
}
return paths;
}
/**
* @brief Saves a playlist to disk.
* @param name The name of the playlist (without extension).
* @param paths List of file paths to save.
*/
void PlaylistManager::SavePlaylist(const BString &name,
const std::vector<BString> &paths) {
BPath dirPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &dirPath) != B_OK)
return;
if (fPlaylistBasePath.IsEmpty())
return;
dirPath.SetTo(fPlaylistBasePath);
create_directory(dirPath.Path(), 0777);
BString fileName = name;
fileName.Append(".m3u");
BPath playlistPath(dirPath.Path(), fileName.String());
BFile file(playlistPath.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (file.InitCheck() != B_OK)
return;
for (const auto &path : paths) {
file.Write(path.String(), path.Length());
file.Write("\n", 1);
}
printf("Playlist '%s' gespeichert (%zu Einträge)\n", name.String(),
paths.size());
if (fPlaylistView->FindIndexByName(name) < 0) {
fPlaylistView->AddItem(name, playlistPath.Path());
}
}
void PlaylistManager::AddPlaylistEntry(const BString &name,
const BString &fullPath) {
fPlaylistView->AddItem(name, fullPath);
}
void PlaylistManager::GetPlaylistNames(BMessage &out, bool onlyWritable) const {
const int32 count = fPlaylistView->CountItems();
for (int32 i = 0; i < count; ++i) {
if (onlyWritable && !fPlaylistView->IsWritableAt(i))
continue;
BString name = fPlaylistView->ItemAt(i);
name.Trim();
if (!name.IsEmpty())
out.AddString("name", name);
}
}
bool PlaylistManager::IsPlaylistWritable(const BString &name) const {
const int32 idx = fPlaylistView->FindIndexByName(name);
return (idx >= 0) ? fPlaylistView->IsWritableAt(idx) : false;
}
void PlaylistManager::Select(int32 index) { fPlaylistView->Select(index); }
int32 PlaylistManager::CountItems() const {
return fPlaylistView->CountItems();
}
void PlaylistManager::CreateNewPlaylist(const BString &name) {
fPlaylistView->CreateNewPlaylist(name);
}
void PlaylistManager::RenamePlaylist(const BString &oldName,
const BString &newName) {
fPlaylistView->RenameItem(oldName, newName);
}
void PlaylistManager::SetPlaylistFolderPath(const BString &path) {
fPlaylistBasePath = path;
}
/**
* @brief Reorders an item within a playlist and saves the change.
* @param name Playlist name.
* @param fromIndex Original index.
* @param toIndex New index.
*/
void PlaylistManager::ReorderPlaylistItem(const BString &name, int32 fromIndex,
int32 toIndex) {
if (fromIndex == toIndex)
return;
std::vector<BString> paths = LoadPlaylist(name);
if (fromIndex < 0 || fromIndex >= (int32)paths.size())
return;
if (toIndex < 0 || toIndex >= (int32)paths.size())
return;
// Move the item
BString item = paths[fromIndex];
paths.erase(paths.begin() + fromIndex);
paths.insert(paths.begin() + toIndex, item);
// Save the reordered playlist
SavePlaylist(name, paths);
}