#include "screenlockdetector.h" #ifdef Q_OS_LINUX #include "platform/screenlockdetector_linux.h" #endif #ifdef Q_OS_MAC #include "platform/screenlockdetector_macos.h" #endif #include ScreenLockDetector::ScreenLockDetector(QObject *parent) : QObject(parent) , m_detector(nullptr) { } ScreenLockDetector::~ScreenLockDetector() { if (m_detector) { delete m_detector; m_detector = nullptr; } } bool ScreenLockDetector::isScreenLocked() const { if (m_detector) { return m_detector->isScreenLocked(); } return false; } bool ScreenLockDetector::initialize() { qDebug() << "================================================="; qDebug() << "ScreenLockDetector: Creating platform-specific detector..."; qDebug() << "================================================="; // 创建平台特定的检测器 m_detector = createPlatformDetector(); if (!m_detector) { qWarning() << "Failed to create platform-specific detector"; qWarning() << "Current platform is not supported"; return false; } // 连接信号 connect(m_detector, &ScreenLockDetectorBase::screenLocked, this, &ScreenLockDetector::screenLocked); connect(m_detector, &ScreenLockDetectorBase::screenUnlocked, this, &ScreenLockDetector::screenUnlocked); connect(m_detector, &ScreenLockDetectorBase::lockStateChanged, this, &ScreenLockDetector::lockStateChanged); // 初始化检测器 bool success = m_detector->initialize(); if (success) { qDebug() << "================================================="; qDebug() << "ScreenLockDetector initialized successfully"; qDebug() << "================================================="; } else { qWarning() << "Failed to initialize platform-specific detector"; delete m_detector; m_detector = nullptr; } return success; } ScreenLockDetectorBase* ScreenLockDetector::createPlatformDetector() { #ifdef Q_OS_MAC qDebug() << "Platform detected: macOS"; return new ScreenLockDetectorMacOS(this); #elif defined(Q_OS_LINUX) qDebug() << "Platform detected: Linux"; return new ScreenLockDetectorLinux(this); #else qWarning() << "Platform not supported"; return nullptr; #endif }