Untitled

 avatar
unknown
c_cpp
20 days ago
3.2 kB
1
Indexable
WebLoginView::WebLoginView()
    : QWidget(nullptr)
    , webView(nullptr)
    , loadingLabel(nullptr)
    , mainWindow(nullptr)
    , positionUpdateTimer(nullptr)
    , isCleaningUp(false)
{
    QWebEngineProfile *profile = QWebEngineProfile::defaultProfile();
    profile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
    profile->setPersistentStoragePath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));

    for (QWidget* widget : QApplication::topLevelWidgets()) {
        if (widget->inherits("MainWindow")) {
            mainWindow = widget;
            break;
        }
    }

    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::Tool);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_ShowWithoutActivating);
    setAttribute(Qt::WA_NoMousePropagation);
    setAttribute(Qt::WA_AlwaysShowToolTips);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);

    loadingLabel = new QLabel("Loading...", this);
    loadingLabel->setStyleSheet("QLabel { color: white; font-size: 16px; background-color: #171717; }");
    loadingLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(loadingLabel);

    webView = new QWebEngineView(this);
    webView->setPage(new QWebEnginePage(profile, webView));
    webView->hide();
    layout->addWidget(webView);

    connect(webView, &QWebEngineView::urlChanged, this, &WebLoginView::handleUrlChanged);
    connect(webView, &QWebEngineView::loadFinished, this, [this](bool) {
        if (loadingLabel) {
            loadingLabel->hide();
        }
        if (webView) {
            webView->show();
        }
        emit loadFinished();
    });

    connect(QWebEngineProfile::defaultProfile()->cookieStore(),
        &QWebEngineCookieStore::cookieAdded,
        [this](const QNetworkCookie &cookie) {
            if (cookie.domain().contains("auth.riotgames.com")) {
            }
        }
    );

    resize(450, 687);

    if (!mainWindow.isNull()) {
        QRect mainGeometry = mainWindow->geometry();
        int x = mainGeometry.x() + (mainGeometry.width() - width()) / 2;
        int y = mainGeometry.y() + (mainGeometry.height() - height()) / 2;

        if (QScreen *screen = QApplication::screenAt(mainWindow->pos())) {
            QRect screenGeometry = screen->geometry();
            x = qMax(screenGeometry.left(), qMin(x, screenGeometry.right() - width()));
            y = qMax(screenGeometry.top(), qMin(y, screenGeometry.bottom() - height()));
        }

        move(x, y);
    }

    setupMainWindowTracking();
    checkLoginStatus();
}

void WebLoginView::checkLoginStatus()
{
    QUrl authUrl(LOGIN_URL);

    webView->setUrl(authUrl);
}

QString WebLoginView::extractAccessToken(const QString &url)
{
    QUrl urlObj(url);
    QString fragment = urlObj.fragment();
    QUrlQuery query(fragment);

    return query.queryItemValue("access_token");
}

void WebLoginView::handleUrlChanged(const QUrl &url)
{
    QString urlString = url.toString();

    if (urlString.contains("access_token")) {
        QString accessToken = extractAccessToken(urlString);
        emit loginSuccessful(accessToken);
        close();
    }
}
Leave a Comment