burn_cd_dialog.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. """
  3. Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. """
  15. import sys
  16. from subprocess import Popen
  17. from shlex import split
  18. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  19. QApplication,
  20. QMessageBox,
  21. )
  22. from PyQt5.QtCore import QTimer # pylint: disable=no-name-in-module
  23. from pycdio import DRIVER_DEVICE
  24. from cdio import get_devices, Device, DriverUnsupportedError, DeviceException
  25. from utils import (
  26. make_sure_file_exists,
  27. log,
  28. )
  29. from input import validate_cd_record_config
  30. import config as const
  31. class InfoMsgBox:
  32. def __init__(self, icon: QMessageBox.Icon, title: str, text: str) -> None:
  33. self.app = QApplication([])
  34. self.title = title
  35. self.text = text
  36. self.icon = icon
  37. self.show_msg_box()
  38. self.app.exec_()
  39. def show_msg_box(self):
  40. self.message_box = QMessageBox()
  41. self.message_box.setIcon(self.icon)
  42. self.message_box.setWindowTitle(self.title)
  43. self.message_box.setText(self.text)
  44. self.message_box.show()
  45. class CDBurnerGUI:
  46. def __init__(self, drive: Device):
  47. self.app = QApplication([])
  48. self.drive = drive
  49. self.exit_code = 1
  50. self.show_burning_msg_box()
  51. self.start_burn_subprocess()
  52. self.app.exec_()
  53. def burning_successful(self) -> bool:
  54. if self.exit_code == 0:
  55. return True
  56. return False
  57. def show_burning_msg_box(self):
  58. self.message_box = QMessageBox()
  59. self.message_box.setWindowTitle("Info")
  60. self.message_box.setText("Burning CD...")
  61. self.message_box.setInformativeText(
  62. "Please wait for a few minutes. You can close this Window, as "
  63. + "there will spawn another window after the operation is "
  64. + "finished."
  65. )
  66. self.message_box.show()
  67. def start_burn_subprocess(self):
  68. process = Popen(["grep", "-a"])
  69. while process.poll() is None:
  70. QApplication.processEvents()
  71. self.message_box.accept()
  72. # Yeah this is hacky but it doesn't work when calling quit directly
  73. QTimer.singleShot(0, self.app.quit)
  74. self.exit_code = process.returncode
  75. def get_cd_drives() -> list:
  76. cd_drives = get_devices(DRIVER_DEVICE)
  77. return cd_drives
  78. def eject_drive(drive: Device) -> None:
  79. try:
  80. drive.eject_media()
  81. except (DriverUnsupportedError, DeviceException):
  82. log(f"Eject of CD-ROM drive {drive} failed")
  83. if __name__ == "__main__":
  84. validate_cd_record_config()
  85. make_sure_file_exists(const.CD_RECORD_CACHEFILE)
  86. drives = get_cd_drives()
  87. if not drives:
  88. InfoMsgBox(
  89. QMessageBox.Critical,
  90. "Error",
  91. "Error: Could not find a CD-ROM. Please try again",
  92. )
  93. sys.exit(1)
  94. if len(drives) != 1:
  95. # TODO: let user choose between drive slots / letters / devices
  96. log("Warning: More than one cd drive found", color="yellow")
  97. drives = drives[0]
  98. BURN_SUCESS = CDBurnerGUI(drives[0]).burning_successful()
  99. if BURN_SUCESS:
  100. InfoMsgBox(QMessageBox.Info, "Info", "Successfully burned CD.")
  101. else:
  102. InfoMsgBox(QMessageBox.Critical, "Error", "Error: Failed to burn CD.")
  103. eject_drive(drives[0])