-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMusicSource.cpp
More file actions
126 lines (104 loc) · 3.2 KB
/
MusicSource.cpp
File metadata and controls
126 lines (104 loc) · 3.2 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
/**
* @file MusicSource.cpp
* @brief Implementation of MusicSource synchronization settings.
*/
#include "MusicSource.h"
#include "Debug.h"
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
MusicSource::MusicSource()
: primary(SOURCE_TAGS), secondary(SOURCE_BFS), conflictMode(CONFLICT_ASK) {}
MusicSource::MusicSource(const BString &p)
: path(p), primary(SOURCE_TAGS), secondary(SOURCE_BFS),
conflictMode(CONFLICT_ASK) {}
void MusicSource::LoadFrom(const BMessage *msg) {
if (!msg)
return;
const char *pathStr;
if (msg->FindString("path", &pathStr) == B_OK)
path = pathStr;
int32 val;
if (msg->FindInt32("primary", &val) == B_OK)
primary = static_cast<SourceType>(val);
if (msg->FindInt32("secondary", &val) == B_OK)
secondary = static_cast<SourceType>(val);
if (msg->FindInt32("conflictMode", &val) == B_OK)
conflictMode = static_cast<ConflictMode>(val);
}
void MusicSource::SaveTo(BMessage *msg) const {
if (!msg)
return;
msg->AddString("path", path);
msg->AddInt32("primary", static_cast<int32>(primary));
msg->AddInt32("secondary", static_cast<int32>(secondary));
msg->AddInt32("conflictMode", static_cast<int32>(conflictMode));
}
const char *MusicSource::SourceTypeName(SourceType t) {
switch (t) {
case SOURCE_TAGS:
return "Tags";
case SOURCE_BFS:
return "BFS Attributes";
case SOURCE_NONE:
return "None";
default:
return "Unknown";
}
}
const char *MusicSource::ConflictModeName(ConflictMode m) {
switch (m) {
case CONFLICT_OVERWRITE:
return "Overwrite";
case CONFLICT_FILL_EMPTY:
return "Fill Empty";
case CONFLICT_ASK:
return "Ask";
default:
return "Unknown";
}
}
/**
* @brief Find MusicSource settings for a given file path.
*/
MusicSource MusicSource::GetSourceForPath(const BString &filePath) {
BPath settingsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK) {
DEBUG_PRINT("[MusicSource] find_directory failed\n");
return MusicSource();
}
settingsPath.Append("BeTon/directories.settings");
DEBUG_PRINT("[MusicSource] Looking for settings at: %s\n",
settingsPath.Path());
BFile file(settingsPath.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK) {
DEBUG_PRINT("[MusicSource] Settings file not found\n");
return MusicSource();
}
BMessage archive;
if (archive.Unflatten(&file) != B_OK) {
DEBUG_PRINT("[MusicSource] Failed to unflatten settings\n");
return MusicSource();
}
MusicSource bestMatch;
int32 bestLen = 0;
BMessage srcMsg;
int32 idx = 0;
while (archive.FindMessage("source", idx++, &srcMsg) == B_OK) {
MusicSource src;
src.LoadFrom(&srcMsg);
DEBUG_PRINT("[MusicSource] Source %ld: path='%s', conflictMode=%d\n",
(long)idx, src.path.String(), (int)src.conflictMode);
if (filePath.StartsWith(src.path.String())) {
int32 len = src.path.Length();
if (len > bestLen) {
bestLen = len;
bestMatch = src;
DEBUG_PRINT("[MusicSource] Best match updated: len=%ld\n", (long)len);
}
}
}
DEBUG_PRINT("[MusicSource] Result for '%s': conflictMode=%d\n",
filePath.String(), (int)bestMatch.conflictMode);
return bestMatch;
}