-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirectoryManagerWindow.cpp
More file actions
250 lines (209 loc) · 6.55 KB
/
DirectoryManagerWindow.cpp
File metadata and controls
250 lines (209 loc) · 6.55 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
#include "DirectoryManagerWindow.h"
#include "Messages.h"
#include "MusicSource.h"
#include "SyncSettingsDialog.h"
#include "TagSync.h"
#include <Alert.h>
#include <Catalog.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <LayoutBuilder.h>
#include <NodeInfo.h>
#include <Path.h>
#include <StorageDefs.h>
#include <cstring>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "DirectoryManagerWindow"
DirectoryManagerWindow::DirectoryManagerWindow(BMessenger cacheManager)
: BWindow(BRect(100, 100, 500, 400), B_TRANSLATE("Manage Music Folders"),
B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS),
fCacheManager(cacheManager) {
fDirectoryList = new BListView("directoryList");
fDirectoryList->SetInvocationMessage(new BMessage(MSG_DIR_EDIT));
BScrollView *scroll =
new BScrollView("scroll", fDirectoryList, 0, false, true);
fBtnAdd = new BButton("Add", B_TRANSLATE("Add"), new BMessage(MSG_DIR_ADD));
fBtnRemove = new BButton("Remove", B_TRANSLATE("Remove"),
new BMessage(MSG_DIR_REMOVE));
fBtnOK = new BButton("OK", B_TRANSLATE("OK"), new BMessage(MSG_DIR_OK));
fAddPanel =
new BFilePanel(B_OPEN_PANEL, new BMessenger(this), nullptr,
B_DIRECTORY_NODE, false, nullptr, nullptr, true, true);
BBox *buttonBox = new BBox(B_FANCY_BORDER);
BLayoutBuilder::Group<>(buttonBox, B_HORIZONTAL, 10)
.SetInsets(10, 10, 10, 10)
.Add(fBtnAdd)
.Add(fBtnRemove)
.AddGlue()
.Add(fBtnOK);
BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
.SetInsets(10, 10, 10, 10)
.Add(scroll)
.Add(buttonBox);
font_height fh;
be_plain_font->GetHeight(&fh);
float fontHeight = fh.ascent + fh.descent + fh.leading;
ResizeTo(fontHeight * 27, fontHeight * 20);
CenterOnScreen();
LoadSettings();
}
void DirectoryManagerWindow::MessageReceived(BMessage *msg) {
switch (msg->what) {
case MSG_DIR_ADD:
fAddPanel->Show();
break;
case B_REFS_RECEIVED: {
entry_ref ref;
if (msg->FindRef("refs", &ref) == B_OK) {
AddDirectory(ref);
}
break;
}
case MSG_DIR_REMOVE:
RemoveSelectedDirectory();
break;
case MSG_DIR_EDIT: {
int32 index = fDirectoryList->CurrentSelection();
if (index >= 0) {
EditDirectory(index);
}
break;
}
case MSG_DIR_OK:
SaveSettings();
if (fCacheManager.IsValid()) {
fCacheManager.SendMessage(MSG_RESCAN);
}
Quit();
break;
case MSG_SYNC_SETTINGS_RESULT: {
int32 index;
BMessage srcMsg;
if (msg->FindInt32("index", &index) == B_OK &&
msg->FindMessage("source", &srcMsg) == B_OK) {
MusicSource src;
src.LoadFrom(&srcMsg);
if (index < 0) {
fSources.push_back(src);
fDirectoryList->AddItem(new BStringItem(src.path.String()));
} else if (index >= 0 && index < (int32)fSources.size()) {
fSources[index] = src;
BStringItem *item =
static_cast<BStringItem *>(fDirectoryList->ItemAt(index));
if (item) {
item->SetText(src.path.String());
fDirectoryList->InvalidateItem(index);
}
}
SaveSettings();
}
break;
}
default:
BWindow::MessageReceived(msg);
break;
}
}
void DirectoryManagerWindow::AddDirectory(const entry_ref &ref) {
BPath path(&ref);
if (path.InitCheck() != B_OK)
return;
for (size_t i = 0; i < fSources.size(); ++i) {
if (fSources[i].path == path.Path())
return;
}
bool isBfs = TagSync::IsBeFsVolume(path);
SyncSettingsDialog *dialog =
new SyncSettingsDialog(BMessenger(this), -1, path.Path(), isBfs);
dialog->Show();
}
void DirectoryManagerWindow::RemoveSelectedDirectory() {
int32 index = fDirectoryList->CurrentSelection();
if (index < 0)
return;
delete fDirectoryList->RemoveItem(index);
fSources.erase(fSources.begin() + index);
SaveSettings();
}
void DirectoryManagerWindow::EditDirectory(int32 index) {
if (index < 0 || index >= static_cast<int32>(fSources.size()))
return;
MusicSource &src = fSources[index];
BPath path(src.path.String());
bool isBfs = TagSync::IsBeFsVolume(path);
SyncSettingsDialog *dialog = new SyncSettingsDialog(
BMessenger(this), index, src.path.String(), isBfs, &src);
dialog->Show();
}
void DirectoryManagerWindow::SaveSettings() {
BPath settingsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
return;
settingsPath.Append("BeTon");
create_directory(settingsPath.Path(), 0755);
settingsPath.Append("directories.settings");
BFile file(settingsPath.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (file.InitCheck() != B_OK)
return;
BMessage archive;
for (size_t i = 0; i < fSources.size(); ++i) {
BMessage srcMsg;
fSources[i].SaveTo(&srcMsg);
archive.AddMessage("source", &srcMsg);
}
archive.Flatten(&file);
}
void DirectoryManagerWindow::LoadSettings() {
BPath settingsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
return;
settingsPath.Append("BeTon/directories.settings");
BFile file(settingsPath.Path(), B_READ_ONLY);
if (file.InitCheck() == B_OK) {
BMessage archive;
if (archive.Unflatten(&file) == B_OK) {
for (int32 i = 0;; i++) {
BMessage srcMsg;
if (archive.FindMessage("source", i, &srcMsg) != B_OK)
break;
MusicSource src;
src.LoadFrom(&srcMsg);
fSources.push_back(src);
fDirectoryList->AddItem(new BStringItem(src.path.String()));
}
return;
}
}
MigrateFromOldFormat();
}
void DirectoryManagerWindow::MigrateFromOldFormat() {
BPath oldPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &oldPath) != B_OK)
return;
oldPath.Append("BeTon/directories.txt");
BFile oldFile(oldPath.Path(), B_READ_ONLY);
if (oldFile.InitCheck() != B_OK)
return;
BString line;
char ch;
while (oldFile.Read(&ch, 1) == 1) {
if (ch == '\n') {
if (!line.IsEmpty()) {
MusicSource src(line);
src.primary = SOURCE_TAGS;
src.secondary = SOURCE_NONE;
src.conflictMode = CONFLICT_OVERWRITE;
fSources.push_back(src);
fDirectoryList->AddItem(new BStringItem(line.String()));
line.Truncate(0);
}
} else {
line += ch;
}
}
if (!fSources.empty()) {
SaveSettings();
}
}