-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlaylistUtils.cpp
More file actions
209 lines (178 loc) · 5.69 KB
/
PlaylistUtils.cpp
File metadata and controls
209 lines (178 loc) · 5.69 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
#include "PlaylistUtils.h"
#include "Debug.h"
#include "MainWindow.h"
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <StringList.h>
#include <cstdio>
#define PLAYLIST_FOLDER "Playlists"
// Global pointer to MainWindow is needed to notify UI updates.
extern MainWindow *gMainWindow;
/**
* @brief Constructs the path to the playlist directory in user settings.
* @return BPath pointing to the 'BeTon/Playlists' folder.
*/
static BPath GetPlaylistDirectory() {
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append("BeTon");
path.Append(PLAYLIST_FOLDER);
}
return path;
}
/**
* @brief Adds a track to a specific playlist.
*
* If the track is already in the playlist, it is not added again.
* The playlist file (.m3u) is updated, and if the UI is available,
* the track is added to the UI display as well.
*
* @param path File path of the track.
* @param playlist Name of the playlist (without extension).
*/
void AddItemToPlaylist(const BString &path, const BString &playlist) {
DEBUG_PRINT("[PlaylistUtils] AddItemToPlaylist aufgerufen mit: %s -> %s\n",
path.String(), playlist.String());
std::vector<BString> items = LoadPlaylist(playlist);
for (const auto &item : items) {
if (item.Compare(path) == 0) {
DEBUG_PRINT("[PlaylistUtils] Pfad bereits enthalten, wird nicht erneut "
"hinzugefuegt\n");
return;
}
}
items.push_back(path);
SavePlaylist(playlist, items);
DEBUG_PRINT("[PlaylistUtils] Pfad hinzugefuegt und gespeichert\n");
if (gMainWindow) {
BString label;
BPath p(path.String());
label = p.Leaf();
gMainWindow->AddPlaylistEntry(playlist, label, path);
}
}
/**
* @brief Retrieves the file path for a content item by its index.
* @param index Index in the content list.
* @return File path string.
*/
BString GetPathForContentItem(int index) {
if (gMainWindow)
return gMainWindow->GetPathForContentItem(index);
return "";
}
/**
* @brief Creates a new, empty playlist file.
* @param name Name of the playlist.
*/
void CreatePlaylist(const BString &name) {
std::vector<BString> empty;
SavePlaylist(name, empty);
DEBUG_PRINT("[PlaylistUtils] Neue Playlist '%s' angelegt\n", name.String());
}
/**
* @brief Deletes a playlist file.
* @param name Name of the playlist to delete.
*/
void DeletePlaylist(const BString &name) {
BPath dirPath(GetPlaylistDirectory());
BString fileName = name;
fileName += ".m3u";
dirPath.Append(fileName.String());
BEntry entry(dirPath.Path());
if (entry.Exists()) {
if (entry.Remove() == B_OK) {
DEBUG_PRINT("[PlaylistUtils] Playlist '%s' geloescht (%s)\n",
name.String(), dirPath.Path());
} else {
DEBUG_PRINT(
"[PlaylistUtils] Playlist '%s' konnte nicht geloescht werden\n",
name.String());
}
}
}
/**
* @brief Loads all track paths from a playlist file.
*
* Each line in the .m3u file is treated as a file path.
*
* @param name Name of the playlist.
* @return Vector of file path strings.
*/
std::vector<BString> LoadPlaylist(const BString &name) {
std::vector<BString> items;
BPath dirPath = GetPlaylistDirectory();
BString fileName = name;
fileName += ".m3u";
dirPath.Append(fileName.String());
BFile file(dirPath.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK) {
DEBUG_PRINT("[PlaylistUtils] Datei konnte nicht geoeffnet werden: %s\n",
dirPath.Path());
return items;
}
off_t fileSize = 0;
if (file.GetSize(&fileSize) != B_OK || fileSize == 0) {
DEBUG_PRINT("[PlaylistUtils] Datei leer oder ungueltig: %s\n",
dirPath.Path());
return items;
}
char *buffer = new char[fileSize + 1];
if (file.Read(buffer, fileSize) != fileSize) {
DEBUG_PRINT("[PlaylistUtils] Fehler beim Lesen der Datei: %s\n",
dirPath.Path());
delete[] buffer;
return items;
}
buffer[fileSize] = '\0';
BString content(buffer);
delete[] buffer;
BStringList lines;
content.Split("\n", true, lines);
for (int32 i = 0; i < lines.CountStrings(); ++i) {
BString line = lines.StringAt(i);
line.RemoveAll("\r");
if (!line.IsEmpty()) {
items.push_back(line);
DEBUG_PRINT("[PlaylistUtils] Eingelesen: %s\n", line.String());
}
}
DEBUG_PRINT("[PlaylistUtils] %zu Eintraege geladen aus Playlist '%s'\n",
items.size(), name.String());
return items;
}
/**
* @brief Saves a list of paths to a playlist file.
*
* Overwrites the existing file. Creates the directory if it doesn't exist.
*
* @param name Name of the playlist.
* @param paths Vector of path strings to save.
*/
void SavePlaylist(const BString &name, const std::vector<BString> &paths) {
BPath dirPath = GetPlaylistDirectory();
BDirectory dir(dirPath.Path());
if (dir.InitCheck() != B_OK) {
create_directory(dirPath.Path(), 0777);
}
BString fileName = name;
fileName += ".m3u";
dirPath.Append(fileName.String());
BFile file(dirPath.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (file.InitCheck() != B_OK) {
DEBUG_PRINT(
"[PlaylistUtils-ERROR] Konnte Playlist-Datei nicht oeffnen: %s\n",
dirPath.Path());
return;
}
for (const auto &path : paths) {
file.Write(path.String(), path.Length());
file.Write("\n", 1);
DEBUG_PRINT("-> geschrieben: %s\n", path.String());
}
DEBUG_PRINT("Playlist '%s' gespeichert (%zu Eintraege)\n", name.String(),
paths.size());
}