game-importer.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. GameImporter::GameImporter(View* parent) : Panel(parent, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. importList.onChange([&] { eventChange(); });
  4. abortButton.setCollapsible().setText("Abort").onActivate([&] { processing = false; });
  5. closeButton.setCollapsible().setText("Close").onActivate([&] { eventClose(); });
  6. }
  7. auto GameImporter::import(string system, const vector<string>& files) -> void {
  8. importList.reset();
  9. abortButton.setVisible(true);
  10. closeButton.setVisible(false);
  11. resize();
  12. systemSelection.setEnabled(false);
  13. programWindow.show(*this);
  14. processing = true;
  15. uint index = 1;
  16. for(auto& file : files) {
  17. if(!processing) break;
  18. messageLabel.setText({"[", index++, "/", files.size(), "] Importing ", Location::file(file), " ..."});
  19. Application::processEvents();
  20. for(auto& medium : media) {
  21. if(medium->name() != system) continue;
  22. auto error = medium->import(file);
  23. ListViewItem item{&importList};
  24. if(!error) {
  25. item.setIcon(Icon::Action::Add);
  26. } else {
  27. item.setIcon(Icon::Action::Close);
  28. item.setForegroundColor({192, 0, 0});
  29. item.setAttribute("error", error);
  30. }
  31. item.setText(Location::file(file));
  32. importList.resizeColumn();
  33. }
  34. }
  35. processing = false;
  36. messageLabel.setText("Completed.");
  37. abortButton.setVisible(false);
  38. closeButton.setVisible(true);
  39. resize();
  40. systemSelection.setEnabled(true);
  41. }
  42. auto GameImporter::eventChange() -> void {
  43. if(processing) return;
  44. if(auto item = importList.selected()) {
  45. if(auto error = item.attribute("error")) {
  46. messageLabel.setText({"Error: ", error, "."});
  47. } else {
  48. messageLabel.setText("OK.");
  49. }
  50. } else {
  51. messageLabel.setText("Completed.");
  52. }
  53. }
  54. auto GameImporter::eventClose() -> void {
  55. if(processing) return;
  56. gameManager.refresh();
  57. }