208 Zeilen
7.7 KiB
Python
208 Zeilen
7.7 KiB
Python
"""
|
|
Controller für die Verwaltung von Accounts.
|
|
"""
|
|
|
|
import logging
|
|
import csv
|
|
from datetime import datetime
|
|
from PyQt5.QtWidgets import QFileDialog, QMessageBox
|
|
from PyQt5.QtCore import QObject
|
|
from application.use_cases.export_accounts_use_case import ExportAccountsUseCase
|
|
|
|
logger = logging.getLogger("account_controller")
|
|
|
|
class AccountController(QObject):
|
|
"""Controller für die Verwaltung von Accounts."""
|
|
|
|
def __init__(self, db_manager):
|
|
super().__init__()
|
|
self.db_manager = db_manager
|
|
self.parent_view = None
|
|
self.export_use_case = ExportAccountsUseCase(db_manager)
|
|
|
|
# Import Fingerprint Generator
|
|
from application.use_cases.generate_account_fingerprint_use_case import GenerateAccountFingerprintUseCase
|
|
self.fingerprint_generator = GenerateAccountFingerprintUseCase(db_manager)
|
|
|
|
def set_parent_view(self, view):
|
|
"""Setzt die übergeordnete View für Dialoge."""
|
|
self.parent_view = view
|
|
|
|
def on_account_created(self, platform: str, account_data: dict):
|
|
"""Wird aufgerufen, wenn ein Account erstellt wurde."""
|
|
account = {
|
|
"platform": platform.lower(),
|
|
"username": account_data.get("username", ""),
|
|
"password": account_data.get("password", ""),
|
|
"email": account_data.get("email", ""),
|
|
"phone": account_data.get("phone", ""),
|
|
"full_name": account_data.get("full_name", ""),
|
|
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
}
|
|
|
|
# Account zur Datenbank hinzufügen
|
|
account_id = self.db_manager.add_account(account)
|
|
logger.info(f"Account in Datenbank gespeichert: {account['username']} (ID: {account_id})")
|
|
|
|
# Fingerprint für neuen Account generieren
|
|
if account_id and account_id > 0:
|
|
logger.info(f"Generiere Fingerprint für neuen Account {account_id}")
|
|
fingerprint_id = self.fingerprint_generator.execute(account_id)
|
|
if fingerprint_id:
|
|
logger.info(f"Fingerprint {fingerprint_id} wurde Account {account_id} zugewiesen")
|
|
else:
|
|
logger.warning(f"Konnte keinen Fingerprint für Account {account_id} generieren")
|
|
|
|
# Erfolgsmeldung anzeigen
|
|
if self.parent_view:
|
|
QMessageBox.information(
|
|
self.parent_view,
|
|
"Erfolg",
|
|
f"Account erfolgreich erstellt!\n\nBenutzername: {account['username']}\nPasswort: {account['password']}\nE-Mail/Telefon: {account['email'] or account['phone']}"
|
|
)
|
|
|
|
def load_accounts(self, platform=None):
|
|
"""Lädt Accounts aus der Datenbank."""
|
|
try:
|
|
if platform and hasattr(self.db_manager, "get_accounts_by_platform"):
|
|
accounts = self.db_manager.get_accounts_by_platform(platform.lower())
|
|
else:
|
|
accounts = self.db_manager.get_all_accounts()
|
|
if platform:
|
|
accounts = [acc for acc in accounts if acc.get("platform", "").lower() == platform.lower()]
|
|
|
|
return accounts
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Laden der Accounts: {e}")
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Fehler beim Laden der Accounts:\n{str(e)}"
|
|
)
|
|
return []
|
|
|
|
def export_accounts(self, platform=None, accounts_to_export=None):
|
|
"""Exportiert Accounts in eine CSV-Datei."""
|
|
parent = self.parent_view or None
|
|
|
|
# Dialog für Format-Auswahl
|
|
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QRadioButton, QPushButton, QCheckBox, QDialogButtonBox
|
|
|
|
dialog = QDialog(parent)
|
|
dialog.setWindowTitle("Export-Optionen")
|
|
dialog.setMinimumWidth(300)
|
|
|
|
layout = QVBoxLayout(dialog)
|
|
|
|
# Format-Auswahl
|
|
csv_radio = QRadioButton("CSV Format (Excel-kompatibel)")
|
|
csv_radio.setChecked(True)
|
|
json_radio = QRadioButton("JSON Format")
|
|
|
|
layout.addWidget(csv_radio)
|
|
layout.addWidget(json_radio)
|
|
|
|
# Passwort-Option
|
|
include_passwords = QCheckBox("Passwörter einschließen")
|
|
include_passwords.setChecked(True)
|
|
layout.addWidget(include_passwords)
|
|
|
|
# Buttons
|
|
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
|
buttons.accepted.connect(dialog.accept)
|
|
buttons.rejected.connect(dialog.reject)
|
|
layout.addWidget(buttons)
|
|
|
|
if dialog.exec_() != QDialog.Accepted:
|
|
return
|
|
|
|
# Format bestimmen
|
|
format = 'csv' if csv_radio.isChecked() else 'json'
|
|
file_extension = '*.csv' if format == 'csv' else '*.json'
|
|
file_filter = f"{format.upper()}-Dateien ({file_extension});;Alle Dateien (*)"
|
|
|
|
# Dateiname vorschlagen
|
|
suggested_filename = self.export_use_case.get_export_filename(platform, format)
|
|
|
|
file_path, _ = QFileDialog.getSaveFileName(
|
|
parent,
|
|
"Konten exportieren",
|
|
suggested_filename,
|
|
file_filter
|
|
)
|
|
|
|
if not file_path:
|
|
return
|
|
|
|
try:
|
|
# Export durchführen mit Use Case
|
|
if accounts_to_export:
|
|
# Wenn spezifische Accounts übergeben wurden
|
|
export_data = self.export_use_case.execute_with_accounts(
|
|
accounts=accounts_to_export,
|
|
format=format,
|
|
include_passwords=include_passwords.isChecked()
|
|
)
|
|
else:
|
|
# Standard-Export basierend auf Platform
|
|
export_data = self.export_use_case.execute(
|
|
platform=platform,
|
|
format=format,
|
|
include_passwords=include_passwords.isChecked()
|
|
)
|
|
|
|
if not export_data:
|
|
QMessageBox.warning(
|
|
parent,
|
|
"Keine Daten",
|
|
"Es wurden keine Accounts zum Exportieren gefunden."
|
|
)
|
|
return
|
|
|
|
# Datei schreiben
|
|
with open(file_path, "wb") as f:
|
|
f.write(export_data)
|
|
|
|
logger.info(f"Accounts erfolgreich nach {file_path} exportiert")
|
|
|
|
if parent:
|
|
QMessageBox.information(
|
|
parent,
|
|
"Export erfolgreich",
|
|
f"Konten wurden erfolgreich nach {file_path} exportiert."
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Exportieren der Accounts: {e}")
|
|
if parent:
|
|
QMessageBox.critical(
|
|
parent,
|
|
"Export fehlgeschlagen",
|
|
f"Fehler beim Exportieren der Konten:\n{str(e)}"
|
|
)
|
|
|
|
def delete_account(self, account_id):
|
|
"""Löscht einen Account aus der Datenbank."""
|
|
try:
|
|
success = self.db_manager.delete_account(account_id)
|
|
|
|
if not success:
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Konto mit ID {account_id} konnte nicht gelöscht werden."
|
|
)
|
|
|
|
return success
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Löschen des Accounts: {e}")
|
|
if self.parent_view:
|
|
QMessageBox.critical(
|
|
self.parent_view,
|
|
"Fehler",
|
|
f"Fehler beim Löschen des Kontos:\n{str(e)}"
|
|
)
|
|
return False
|