refactor(script): use qfluentwidgets dialog for password

This commit is contained in:
RedDeadDepresso
2024-09-25 19:23:01 +01:00
parent ed822c577a
commit c3c571e731
2 changed files with 76 additions and 17 deletions

View File

@@ -0,0 +1,54 @@
import sys
from PySide6.QtCore import Signal, Qt
from PySide6.QtWidgets import QApplication, QHBoxLayout
from qfluentwidgets import Dialog, FluentTitleBar, LineEdit, setTheme, Theme
app = QApplication(sys.argv)
class PasswordDialog(Dialog):
yesSignal = Signal()
cancelSignal = Signal()
def __init__(self, title: str, content: str, parent=None):
super().__init__(title, content, parent)
self.passwordLineEdit = LineEdit(self)
self.password = ''
self.yesButtonPressed = False
self.__initLayout()
self.__connectSignalToSlot()
def __initLayout(self):
passwordLayout = QHBoxLayout()
passwordLayout.setContentsMargins(12, 12, 12, 12)
passwordLayout.addWidget(self.passwordLineEdit, 1, Qt.AlignTop)
self.vBoxLayout.insertLayout(2, passwordLayout, 1)
self.setTitleBar(FluentTitleBar(self))
def __connectSignalToSlot(self):
self.passwordLineEdit.textChanged.connect(self.onTextChanged)
self.yesSignal.connect(self.onYesSignal)
def onTextChanged(self, text: str):
self.password = text
def onYesSignal(self):
self.yesButtonPressed = True
def getPassword(self) -> str:
if self.yesButtonPressed:
print(self.password)
return self.password
return ''
def password_dialog(title: str, content: str) -> str:
setTheme(Theme.AUTO)
dialog = PasswordDialog(title, content)
dialog.exec()
return dialog.getPassword()
if __name__ == "__main__":
password = password_dialog('test', 'test')
print(password)

View File

@@ -1,7 +1,6 @@
import shutil
import datetime
import patoolib
import customtkinter
import subprocess
import time
from pathlib import Path
@@ -140,30 +139,36 @@ class FileManager:
raise Exception()
def extract_archive(self, archive_path: Union[Path, str]):
from app.components.password_dialog import password_dialog
"""Extract the archive."""
archive_path = Path(archive_path)
archive_name = archive_path.name
logger.info("ARCHIVE", f"Extracting {archive_name}")
extract_path = archive_path.with_stem(f"{archive_path.stem}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}")
extract_path = archive_path.with_name(f"{archive_path.stem}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}")
try:
patoolib.extract_archive(str(archive_path), outdir=str(extract_path))
return extract_path
except patoolib.util.PatoolError as e:
text = f"There is an error with the archive {archive_name} but it is impossible to detect the cause. Maybe it requires a password?"
while self.config.install_chara["Password"] == "Request Password":
except:
text = f"There is an error with the archive {archive_name}, but it is impossible to detect the cause. Maybe it requires a password?"
while True:
try:
dialog = customtkinter.CTkInputDialog(text=text, title="Enter Password")
password = dialog.get_input()
if password:
patoolib.extract_archive(str(archive_path), outdir=str(extract_path), password=password)
return extract_path
else:
password = password_dialog('Enter Password', text)
if not password:
break
except:
text = f"Wrong password or {archive_name} is corrupted. Please enter password again or click Cancel"
logger.skipped("ARCHIVE", archive_name)
patoolib.extract_archive(str(archive_path), outdir=str(extract_path), password=password)
return extract_path
except patoolib.util.PatoolError as e:
text = f"Wrong password or {archive_name} is corrupted. Please enter password again or click Cancel."
print(f"Error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
break
logger.skipped("ARCHIVE", archive_name)