-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMetadataHandler.cpp
More file actions
287 lines (252 loc) · 9.54 KB
/
MetadataHandler.cpp
File metadata and controls
287 lines (252 loc) · 9.54 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
#include "MetadataHandler.h"
#include "CacheManager.h"
#include "Debug.h"
#include "Messages.h"
#include "MusicSource.h"
#include "TagSync.h"
#include <Alert.h>
#include <Directory.h>
#include <Entry.h>
#include <Path.h>
MetadataHandler::MetadataHandler(BMessenger target) : fTarget(target) {}
MetadataHandler::~MetadataHandler() {}
/**
* @brief Applies the provided cover art data to all audio files in the same
* directory as the given file.
*
* @param filePath Path to a file in the target directory (usually the one being
* edited).
* @param data Pointer to the raw image data.
* @param size Size of the image data in bytes.
*/
void MetadataHandler::ApplyAlbumCover(const BString &filePath, const void *data,
ssize_t size) {
_ProcessDirectoryForCover(filePath, false, data, size);
}
/**
* @brief Removes embedded cover art from all audio files in the same directory
* as the given file.
*
* @param filePath Path to a file in the target directory.
*/
void MetadataHandler::ClearAlbumCover(const BString &filePath) {
_ProcessDirectoryForCover(filePath, true);
}
/**
* @brief Applies cover art to all files specified in the BMessage.
*
* Takes a BMessage containing a "bytes" buffer and "mime" string, and a list of
* "file" strings. Applies the cover to each file individually.
*
* @param msg The message containing cover data and file list.
*/
void MetadataHandler::ApplyCoverToAll(const BMessage *msg) {
const void *data = nullptr;
ssize_t size = 0;
if (msg->FindData("bytes", B_RAW_TYPE, &data, &size) == B_OK && data &&
size > 0) {
const char *mime = nullptr;
msg->FindString("mime", &mime);
BString path;
int32 i = 0;
while (msg->FindString("file", i++, &path) == B_OK) {
TagSync::WriteEmbeddedCover(BPath(path.String()), (const uint8 *)data,
(size_t)size, mime);
}
}
}
/**
* @brief Saves metadata tags to one or more files based on the BMessage.
*
* Iterates through "file" entries in the message and updates tags based on
* available fields (title, artist, album, etc.).
* Also updates BFS attributes if available and notifies the UI/CacheManager.
*
* @param msg The message containing tag data and file paths.
*/
void MetadataHandler::SaveTags(const BMessage *msg) {
BString file;
int32 i = 0;
while (msg->FindString("file", i++, &file) == B_OK) {
if (file.IsEmpty())
continue;
BPath path(file.String());
TagData td;
TagSync::ReadTags(path, td);
BString s;
if (msg->FindString("title", &s) == B_OK)
td.title = s;
if (msg->FindString("artist", &s) == B_OK)
td.artist = s;
if (msg->FindString("album", &s) == B_OK)
td.album = s;
if (msg->FindString("albumArtist", &s) == B_OK)
td.albumArtist = s;
if (msg->FindString("composer", &s) == B_OK)
td.composer = s;
if (msg->FindString("genre", &s) == B_OK)
td.genre = s;
if (msg->FindString("comment", &s) == B_OK)
td.comment = s;
auto _toUInt = [](const char *str) -> unsigned int {
return (unsigned int)atoi(str);
};
if (msg->FindString("year", &s) == B_OK)
td.year = _toUInt(s.String());
if (msg->FindString("track", &s) == B_OK)
td.track = _toUInt(s.String());
if (msg->FindString("trackTotal", &s) == B_OK)
td.trackTotal = _toUInt(s.String());
if (msg->FindString("disc", &s) == B_OK)
td.disc = _toUInt(s.String());
if (msg->FindString("discTotal", &s) == B_OK)
td.discTotal = _toUInt(s.String());
if (msg->FindString("mbAlbumID", &s) == B_OK)
td.mbAlbumID = s;
if (msg->FindString("mbArtistID", &s) == B_OK)
td.mbArtistID = s;
if (msg->FindString("mbTrackID", &s) == B_OK)
td.mbTrackID = s;
DEBUG_PRINT("[MetadataHandler] SaveTags: Calling WriteTagsToFile. "
"mbAlbumID='%s', mbTrackID='%s'\\n",
td.mbAlbumID.String(), td.mbTrackID.String());
bool ok = TagSync::WriteTagsToFile(path, td, nullptr);
if (ok) {
TagData tdSaved;
TagSync::ReadTags(path, tdSaved);
if (TagSync::IsBeFsVolume(path)) {
TagSync::WriteBfsAttributes(path, tdSaved, nullptr, 512 * 1024);
}
BMessage update(MSG_MEDIA_ITEM_FOUND);
update.AddString("path", path.Path());
update.AddString("title", td.title);
update.AddString("artist", td.artist);
update.AddString("album", td.album);
update.AddString("genre", td.genre);
update.AddString("comment", td.comment);
update.AddInt32("year", td.year);
update.AddInt32("track", td.track);
update.AddInt32("trackTotal", td.trackTotal);
update.AddInt32("disc", td.disc);
update.AddInt32("discTotal", td.discTotal);
update.AddInt32("duration", td.lengthSec);
update.AddInt32("bitrate", td.bitrate);
update.AddString("mbAlbumID", td.mbAlbumID);
update.AddString("mbArtistID", td.mbArtistID);
update.AddString("mbTrackID", td.mbTrackID);
fTarget.SendMessage(&update);
} else {
(new BAlert("savefail", "Konnte Tags nicht speichern.", "OK"))->Go();
}
}
}
/**
* @brief Helper to iterate over the text file's directory and apply/clear cover
* art for all supported audio files.
*
* @param filePath Path to a file in the target directory (acts as anchor).
* @param clear If true, removes cover art. If false, applies `data`.
* @param data Raw image data (ignored if clear is true).
* @param size Size of image data (ignored if clear is true).
*/
void MetadataHandler::_ProcessDirectoryForCover(const BString &filePath,
bool clear, const void *data,
size_t size) {
BPath p(filePath.String());
BPath parent;
if (p.GetParent(&parent) == B_OK) {
BDirectory dir(parent.Path());
BEntry entry;
while (dir.GetNextEntry(&entry) == B_OK) {
BPath ep;
if (entry.GetPath(&ep) == B_OK && !entry.IsDirectory()) {
BString pathStr = ep.Path();
if (pathStr.EndsWith(".mp3") || pathStr.EndsWith(".flac") ||
pathStr.EndsWith(".m4a") || pathStr.EndsWith(".ogg") ||
pathStr.EndsWith(".wav")) {
bool res;
if (clear) {
res = TagSync::WriteEmbeddedCover(ep, nullptr, 0, nullptr);
} else {
res = TagSync::WriteEmbeddedCover(ep, (const uint8 *)data, size,
nullptr);
}
DEBUG_PRINT(" -> %s cover for '%s': %s\\n",
clear ? "clearing" : "applying", pathStr.String(),
res ? "OK" : "FAIL");
}
}
}
}
}
/**
* @brief Synchronizes metadata between Tags and BFS attributes.
*
* Reads the MusicSource settings for each file's directory and uses
* the configured ConflictMode for merging.
*
* @param files List of file paths to sync.
* @param towardsBfs If true, reads from Tags and writes to BFS.
* If false, reads from BFS and writes to Tags.
*/
void MetadataHandler::SyncMetadata(const std::vector<BString> &files) {
for (size_t i = 0; i < files.size(); ++i) {
BPath path(files[i].String());
MusicSource src = MusicSource::GetSourceForPath(files[i]);
TagData tags, bfs;
TagSync::ReadTags(path, tags);
TagSync::ReadBfsAttributes(path, bfs);
bool primIsBfs = (src.primary == SOURCE_BFS);
const TagData &primaryData = primIsBfs ? bfs : tags;
const TagData &secondaryData = primIsBfs ? tags : bfs;
TagData merged;
bool conflict = false;
bool changed =
TagSync::SmartMerge(primaryData, secondaryData, merged, conflict);
if (conflict && src.conflictMode == CONFLICT_ASK) {
DEBUG_PRINT("[MetadataHandler] CONFLICT for: %s\n", path.Path());
primaryData.LogDifferences(secondaryData);
fflush(stdout);
BMessage ask(MSG_SYNC_CONFLICT);
ask.AddString("path", path.Path());
ask.AddInt32("index", i);
ask.AddInt32("total", files.size());
ask.AddBool("towardsBfs", true);
fTarget.SendMessage(&ask);
continue;
}
bool canWriteTags =
(src.primary == SOURCE_TAGS || src.secondary == SOURCE_TAGS);
bool canWriteBfs =
(src.primary == SOURCE_BFS || src.secondary == SOURCE_BFS);
if (canWriteTags) {
if (merged.HasDifferences(tags)) {
TagSync::WriteTags(path, merged);
DEBUG_PRINT("[MetadataHandler] Updated Tags for %s\n", path.Path());
}
}
if (canWriteBfs) {
if (merged.HasDifferences(bfs)) {
TagSync::WriteBfsAttributes(path, merged, nullptr);
DEBUG_PRINT("[MetadataHandler] Updated BFS for %s\n", path.Path());
}
}
if (changed || conflict) {
BMessage update(MSG_MEDIA_ITEM_FOUND);
update.AddString("path", path.Path());
update.AddString("title", merged.title);
update.AddString("artist", merged.artist);
update.AddString("album", merged.album);
update.AddString("genre", merged.genre);
update.AddInt32("year", merged.year);
update.AddInt32("track", merged.track);
fTarget.SendMessage(&update);
}
BMessage progress(MSG_SYNC_PROGRESS);
progress.AddInt32("current", i + 1);
progress.AddInt32("total", files.size());
fTarget.SendMessage(&progress);
}
BMessage done(MSG_SYNC_DONE);
fTarget.SendMessage(&done);
}