Debian Codegraph @

components/extension/tagedit/chooser/chooser_files.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2019 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <boca.h>
14
15 #include "chooser_files.h"
16 #include "chooser_files_tree.h"
17 #include "chooser_files_utilities.h"
18
19 BoCA::ChooserFiles::ChooserFiles() : Chooser("Files")
20 {
21 list_directories = new ListBox(Point(7,7), Size(200, 150));
22
23 div_split = new Divider(210, OR_VERT | DIV_MOVABLE);
24 div_split->onDrag.Connect(&ChooserFiles::OnDragDivider, this);
25
26 edit_directory = new EditBox(Point(215, 7), Size(100, 0));
27 edit_directory->Deactivate();
28
29 list_files = new ListBox(Point(215, 34), Size(100, 150));
30 list_files->Deactivate();
31 list_files->onSelectEntry.Connect(&ChooserFiles::OnSelectFile, this);
32
33 text_nofiles = new Text(NIL, Point());
34 text_nofiles->SetFont(GUI::Font(GUI::Font::Default, 12, GUI::Font::Bold, 0, Setup::InactiveTextColor));
35
36 shortcut_previous = new Shortcut(0, Input::Keyboard::KeyUp, list_files);
37 shortcut_previous->onKeyDown.Connect(&ChooserFiles::OnShortcutPrevious, this);
38
39 shortcut_next = new Shortcut(0, Input::Keyboard::KeyDown, list_files);
40 shortcut_next->onKeyDown.Connect(&ChooserFiles::OnShortcutNext, this);
41
42 shortcut_first = new Shortcut(0, Input::Keyboard::KeyHome, list_files);
43 shortcut_first->onKeyDown.Connect(&ChooserFiles::OnShortcutFirst, this);
44
45 shortcut_last = new Shortcut(0, Input::Keyboard::KeyEnd, list_files);
46 shortcut_last->onKeyDown.Connect(&ChooserFiles::OnShortcutLast, this);
47
48 btn_save = new Button(String(), Point(176, 30), Size());
49 btn_save->SetOrientation(OR_LOWERRIGHT);
50 btn_save->Deactivate();
51 btn_save->onAction.Connect(&ChooserFiles::OnSave, this);
52
53 btn_saveall = new Button(String(), Point(88, 30), Size());
54 btn_saveall->SetOrientation(OR_LOWERRIGHT);
55 btn_saveall->Deactivate();
56 btn_saveall->onAction.Connect(&ChooserFiles::OnSaveAll, this);
57
58 ChooserFilesTree::onSelectDirectory.Connect(&ChooserFiles::OnSelectDirectory, this);
59
60 #ifdef __WIN32__
61 /* Add local drives.
62 */
63 for (Int i = 0; i < 26; i++)
64 {
65 char name[4] = { char('A' + i), ':', '\\', 0};
66 Directory drive(name);
67
68 if (drive.Exists())
69 {
70 Tree *tree = new ChooserFilesTree(drive);
71
72 trees.Add(tree);
73
74 list_directories->Add(tree);
75 }
76 }
77 #else
78 /* Add root directory.
79 */
80 Tree *root = new ChooserFilesTree(Directory("/"));
81
82 trees.Add(root);
83
84 list_directories->Add(root);
85 #endif
86
87 Add(list_directories);
88
89 Add(div_split);
90
91 Add(edit_directory);
92 Add(list_files);
93 Add(text_nofiles);
94
95 Add(shortcut_previous);
96 Add(shortcut_next);
97 Add(shortcut_first);
98 Add(shortcut_last);
99
100 Add(btn_save);
101 Add(btn_saveall);
102
103 onChangeSize.Connect(&ChooserFiles::OnChangeSize, this);
104
105 Settings::Get()->onChangeLanguageSettings.Connect(&ChooserFiles::OnChangeLanguageSettings, this);
106 }
107
108 BoCA::ChooserFiles::~ChooserFiles()
109 {
110 Settings::Get()->onChangeLanguageSettings.Disconnect(&ChooserFiles::OnChangeLanguageSettings, this);
111
112 ChooserFilesTree::onSelectDirectory.Disconnect(&ChooserFiles::OnSelectDirectory, this);
113
114 list_files->RemoveAllEntries();
115
116 foreach (Tree *tree, trees) DeleteObject(tree);
117
118 DeleteObject(list_directories);
119
120 DeleteObject(div_split);
121
122 DeleteObject(edit_directory);
123 DeleteObject(list_files);
124 DeleteObject(text_nofiles);
125
126 DeleteObject(shortcut_previous);
127 DeleteObject(shortcut_next);
128 DeleteObject(shortcut_first);
129 DeleteObject(shortcut_last);
130
131 DeleteObject(btn_save);
132 DeleteObject(btn_saveall);
133 }
134
135 /* Called when component canvas size changes.
136 * ----
137 */
138 Void BoCA::ChooserFiles::OnChangeSize(const Size &nSize)
139 {
140 Rect clientRect = Rect(GetPosition(), GetSize());
141 Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
142
143 list_directories->SetHeight(clientSize.cy - 15);
144
145 edit_directory->SetWidth(clientSize.cx - edit_directory->GetX() - 8);
146 list_files->SetSize(Size(clientSize.cx - list_files->GetX() - 8, clientSize.cy - 72));
147 text_nofiles->SetPosition(list_files->GetPosition() + Point((list_files->GetWidth() - text_nofiles->GetUnscaledTextWidth()) / 2, (list_files->GetHeight() - 13 - text_nofiles->GetUnscaledTextHeight()) / 2 + 13));
148 }
149
150 /* Called when application language is changed.
151 * ----
152 */
153 Void BoCA::ChooserFiles::OnChangeLanguageSettings()
154 {
155 I18n *i18n = I18n::Get();
156
157 i18n->SetContext("Extensions::Tag Editor");
158
159 SetText(i18n->TranslateString("Files"));
160
161 /* Hide all affected widgets prior to changing
162 * labels to avoid flickering.
163 */
164 Bool prevVisible = IsVisible();
165
166 if (prevVisible) Hide();
167
168 list_files->RemoveAllTabs();
169
170 list_files->AddTab(i18n->TranslateString("File"));
171
172 text_nofiles->SetText(i18n->TranslateString("no audio files found"));
173
174 btn_save->SetText(i18n->TranslateString("Save"));
175 btn_save->SetWidth(Math::Max(80, btn_save->GetUnscaledTextWidth() + 14));
176
177 btn_saveall->SetText(i18n->TranslateString("Save all"));
178 btn_saveall->SetWidth(Math::Max(80, btn_saveall->GetUnscaledTextWidth() + 14));
179
180 btn_save->SetX(btn_save->GetWidth() + btn_saveall->GetWidth() + 16);
181 btn_saveall->SetX(btn_saveall->GetWidth() + 8);
182
183 /* OnChangeSize will correct sizes of any other widgets.
184 */
185 OnChangeSize(GetSize());
186
187 /* Show all widgets again.
188 */
189 if (prevVisible) Show();
190 }
191
192 /* Called when the splitting divider is dragged with the mouse.
193 * ----
194 */
195 Void BoCA::ChooserFiles::OnDragDivider(Int pos)
196 {
197 Rect clientRect = Rect(GetRealPosition(), GetSize());
198 Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
199
200 if (pos > clientSize.cx - 300) pos = clientSize.cx - 300;
201 if (pos < 150) pos = 150;
202
203 if (div_split->GetPos() == pos) return;
204
205 Surface *surface = GetDrawSurface();
206
207 surface->StartPaint(GetVisibleArea());
208
209 div_split->Hide();
210 list_directories->Hide();
211 edit_directory->Hide();
212 list_files->Hide();
213
214 list_directories->SetWidth(pos - 10);
215
216 edit_directory->SetWidth(clientSize.cx - pos - 13);
217 edit_directory->SetX(pos + 5);
218
219 list_files->SetWidth(clientSize.cx - pos - 13);
220 list_files->SetX(pos + 5);
221
222 div_split->SetPos(pos);
223
224 text_nofiles->SetPosition(list_files->GetPosition() + Point((list_files->GetWidth() - text_nofiles->GetUnscaledTextWidth()) / 2, (list_files->GetHeight() - 13 - text_nofiles->GetUnscaledTextHeight()) / 2 + 13));
225
226 div_split->Show();
227 list_directories->Show();
228 edit_directory->Show();
229 list_files->Show();
230
231 if (text_nofiles->IsVisible()) text_nofiles->Paint(SP_PAINT);
232
233 surface->EndPaint();
234 }
235
236 /* Called when a directory entry is selected.
237 * ----
238 * Reads files in the selected directory and adds them to the files list.
239 */
240 Void BoCA::ChooserFiles::OnSelectDirectory(const Directory &directory)
241 {
242 if (String(directory) == edit_directory->GetText()) return;
243
244 GetSupportedFileExtensions();
245
246 if (modified.Length() > 0)
247 {
248 if (Dialogs::Message::Button::Yes == Dialogs::QuickMessage("There are unsaved files in this directory. Would you like to save them now?", "Save changes", Dialogs::Message::Buttons::YesNo, Dialogs::Message::Icon::Question)) OnSaveAll();
249 }
250
251 edit_directory->SetText(String(directory).Append(Directory::GetDirectoryDelimiter()));
252
253 if (IsRegistered()) container->GetDrawSurface()->StartPaint(Rect(list_files->GetRealPosition(), list_files->GetRealSize()));
254
255 list_files->RemoveAllEntries();
256
257 const Array<File> &files = directory.GetFiles();
258
259 foreach (const File &file, files)
260 {
261 String filename = file.GetFileName();
262 String extension = filename.Tail(filename.Length() - filename.FindLast(".") - 1).ToLower();
263
264 if (extensions.Get(extension.ComputeCRC32()) != NIL) list_files->AddEntry(filename);
265 }
266
267 if (list_files->Length() == 0)
268 {
269 list_files->Deactivate();
270
271 if (text_nofiles->IsVisible()) text_nofiles->Paint(SP_PAINT);
272 else text_nofiles->Show();
273 }
274 else
275 {
276 text_nofiles->Hide();
277 list_files->Activate();
278 }
279
280 if (IsRegistered()) container->GetDrawSurface()->EndPaint();
281
282 btn_save->Deactivate();
283 btn_saveall->Deactivate();
284
285 onSelectNone.Emit();
286
287 tracks.RemoveAll();
288 modified.RemoveAll();
289 }
290
291 /* Called when a file entry is selected.
292 * ----
293 * Tries to open the file and generate a track object.
294 */
295 Void BoCA::ChooserFiles::OnSelectFile(ListEntry *entry)
296 {
297 if (tracks.Get(list_files->GetSelectedEntryNumber()) != NIL)
298 {
299 if (modified.Get(list_files->GetSelectedEntryNumber()) != NIL) btn_save->Activate();
300 else btn_save->Deactivate();
301
302 onSelectTrack.Emit(tracks.Get(list_files->GetSelectedEntryNumber()));
303
304 return;
305 }
306
307 File file(entry->GetText(), edit_directory->GetText());
308 DecoderComponent *decoder = ChooserFilesUtilities::CreateDecoderComponent(file);
309
310 if (decoder == NIL)
311 {
312 I18n *i18n = I18n::Get();
313
314 i18n->SetContext("Extensions::Tag Editor::Errors");
315
316 Utilities::ErrorMessage(i18n->TranslateString("Unable to open file: %1\n\nError: %2").Replace("%1", file.GetFileName()).Replace("%2", i18n->TranslateString("Unknown file type", "Messages")));
317
318 return;
319 }
320
321 Track track;
322 Error error = decoder->GetStreamInfo(file, track);
323 String errorString = decoder->GetErrorString();
324
325 Registry::Get().DeleteComponent(decoder);
326
327 if (error == Error())
328 {
329 I18n *i18n = I18n::Get();
330
331 i18n->SetContext("Extensions::Tag Editor::Errors");
332
333 Utilities::ErrorMessage(i18n->TranslateString("Unable to open file: %1\n\nError: %2").Replace("%1", file.GetFileName()).Replace("%2", i18n->TranslateString(errorString, "Messages")));
334
335 return;
336 }
337
338 track.fileName = file;
339
340 btn_save->Deactivate();
341
342 onSelectTrack.Emit(track);
343
344 tracks.Add(track, list_files->GetSelectedEntryNumber());
345 }
346
347 /* Called when the up arrow key is pressed.
348 * ----
349 * Selects the previous file.
350 */
351 Void BoCA::ChooserFiles::OnShortcutPrevious()
352 {
353 if (!IsVisible() || !allowTrackChangeByArrowKey.Call()) return;
354
355 if (list_files->GetSelectedEntryNumber() == -1) list_files->SelectNthEntry(list_files->Length() - 1);
356 else list_files->SelectNthEntry(list_files->GetSelectedEntryNumber() - 1);
357 }
358
359 /* Called when the down arrow key is pressed.
360 * ----
361 * Selects the next file.
362 */
363 Void BoCA::ChooserFiles::OnShortcutNext()
364 {
365 if (!IsVisible() || !allowTrackChangeByArrowKey.Call()) return;
366
367 list_files->SelectNthEntry(list_files->GetSelectedEntryNumber() + 1);
368 }
369
370 /* Called when the home key is pressed.
371 * ----
372 * Selects the first file.
373 */
374 Void BoCA::ChooserFiles::OnShortcutFirst()
375 {
376 if (!IsVisible() || !allowTrackRemoveByDeleteKey.Call()) return;
377
378 list_files->SelectNthEntry(0);
379 }
380
381 /* Called when the end key is pressed.
382 * ----
383 * Selects the last file.
384 */
385 Void BoCA::ChooserFiles::OnShortcutLast()
386 {
387 if (!IsVisible() || !allowTrackRemoveByDeleteKey.Call()) return;
388
389 list_files->SelectNthEntry(list_files->Length() - 1);
390 }
391
392 /* Called when a track is modified.
393 * ----
394 * Finds the track and marks it as modified.
395 */
396 Void BoCA::ChooserFiles::OnModifyTrack(const Track &track)
397 {
398 for (Int i = 0; i < tracks.Length(); i++)
399 {
400 if (tracks.GetNth(i).GetTrackID() == track.GetTrackID())
401 {
402 GUI::Font font = list_files->GetNthEntry(tracks.GetNthIndex(i))->GetFont();
403
404 font.SetColor(Color(0, 0, 255));
405
406 list_files->GetNthEntry(tracks.GetNthIndex(i))->SetFont(font);
407
408 /* Update track.
409 */
410 tracks.GetNthReference(i) = track;
411
412 /* Add it to the list of modified tracks.
413 */
414 modified.Add(track.GetTrackID(), tracks.GetNthIndex(i));
415
416 if (tracks.GetNthIndex(i) == list_files->GetSelectedEntryNumber()) btn_save->Activate();
417
418 btn_saveall->Activate();
419
420 return;
421 }
422 }
423 }
424
425 /* Called when the "Save" button is clicked.
426 * ----
427 * Invokes SaveFileTag() for the selected file.
428 */
429 Void BoCA::ChooserFiles::OnSave()
430 {
431 Int i = list_files->GetSelectedEntryNumber();
432
433 if (SaveFileTag(tracks.Get(i)) != Success()) return;
434
435 GUI::Font font = list_files->GetNthEntry(i)->GetFont();
436
437 font.SetColor(Color(0, 0, 0));
438
439 list_files->GetNthEntry(i)->SetFont(font);
440
441 modified.Remove(i);
442
443 btn_save->Deactivate();
444
445 if (modified.Length() == 0) btn_saveall->Deactivate();
446 }
447
448 /* Called when the "Save all" button is clicked.
449 * ----
450 * Invokes SaveFileTag() for all modified files.
451 */
452 Void BoCA::ChooserFiles::OnSaveAll()
453 {
454 Int failed = 0;
455
456 while (modified.Length() - failed > 0)
457 {
458 Int i = modified.GetNthIndex(0 + failed);
459
460 if (SaveFileTag(tracks.Get(i)) != Success()) { failed++; continue; }
461
462 GUI::Font font = list_files->GetNthEntry(i)->GetFont();
463
464 font.SetColor(Color(0, 0, 0));
465
466 list_files->GetNthEntry(i)->SetFont(font);
467
468 modified.Remove(i);
469 }
470
471 if (modified.Get(list_files->GetSelectedEntryNumber()) == NIL) btn_save->Deactivate();
472
473 if (modified.Length() == 0) btn_saveall->Deactivate();
474 }
475
476 /* Called when the currently selected entry needs to be selected again.
477 * ----
478 */
479 Void BoCA::ChooserFiles::ReselectEntry()
480 {
481 if (list_files->GetSelectedEntry() == NIL) return;
482
483 OnSelectFile(list_files->GetSelectedEntry());
484 }
485
486 Int BoCA::ChooserFiles::SaveFileTag(const Track &track)
487 {
488 Config *config = Config::Get();
489
490 Int error = Error();
491 String errorString = "Not supported";
492
493 /* Create decoder component.
494 */
495 DecoderComponent *decoder = ChooserFilesUtilities::CreateDecoderComponent(track.fileName);
496
497 /* Loop over supported formats.
498 */
499 String lcURI = track.fileName.ToLower();
500
501 foreach (FileFormat *format, decoder->GetFormats())
502 {
503 foreach (const String &extension, format->GetExtensions())
504 {
505 if (!lcURI.EndsWith(String(".").Append(extension))) continue;
506
507 /* Update tags.
508 */
509 const Array<TagFormat> &tagFormats = format->GetTagFormats();
510
511 foreach (const TagFormat &tagFormat, tagFormats)
512 {
513 Registry &boca = AS::Registry::Get();
514 TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID(tagFormat.GetTagger());
515
516 if (tagger != NIL)
517 {
518 foreach (TagSpec *spec, tagger->GetTagSpecs())
519 {
520 if (spec->GetName() != tagFormat.GetName()) continue;
521
522 /* Set to Success() by default, so we won't report
523 * an error if the tag type is simply deactivated.
524 */
525 error = Success();
526
527 if (config->GetIntValue("Tags", String("Enable").Append(tagFormat.GetName().Replace(" ", NIL)), spec->IsDefault()))
528 {
529 error = tagger->UpdateStreamInfo(track.fileName, track);
530 errorString = tagger->GetErrorString();
531 }
532
533 break;
534 }
535
536 boca.DeleteComponent(tagger);
537 }
538 }
539
540 break;
541 }
542 }
543
544 Registry::Get().DeleteComponent(decoder);
545
546 /* Check for error.
547 */
548 if (error == Error())
549 {
550 I18n *i18n = I18n::Get();
551
552 i18n->SetContext("Extensions::Tag Editor::Errors");
553
554 Utilities::ErrorMessage(i18n->TranslateString("Unable to update tag: %1\n\nError: %2").Replace("%1", track.fileName).Replace("%2", i18n->TranslateString(errorString, "Messages")));
555
556 return Error();
557 }
558
559 return Success();
560 }
561
562 Void BoCA::ChooserFiles::GetSupportedFileExtensions()
563 {
564 if (extensions.Length() > 0) return;
565
566 Registry &boca = Registry::Get();
567
568 for (Int i = 0; i < boca.GetNumberOfComponents(); i++)
569 {
570 if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_DECODER) continue;
571
572 const Array<FileFormat *> &formats = boca.GetComponentFormats(i);
573
574 foreach (FileFormat *format, formats)
575 {
576 const Array<String> &extensions = format->GetExtensions();
577
578 foreach (const String &extension, extensions) this->extensions.Add(extension.ToLower(), extension.ToLower().ComputeCRC32());
579 }
580 }
581 }