From a8d7965c43d0862b730fe2d44dfab8b711ef5a61 Mon Sep 17 00:00:00 2001 From: Wizzard <25581244+Wizzard@users.noreply.toomuchslop.com> Date: Wed, 12 Jul 2023 18:28:56 -0400 Subject: [PATCH] Fixes --- assets/userscript.js | 12 ++- src/mainwindow.cpp | 189 ++++++++++++++++++++++++------------------- src/mainwindow.h | 38 ++++----- 3 files changed, 134 insertions(+), 105 deletions(-) diff --git a/assets/userscript.js b/assets/userscript.js index 7137917..3dbc8cb 100644 --- a/assets/userscript.js +++ b/assets/userscript.js @@ -340,7 +340,17 @@ function main() { // }) // ); - section.appendChild( + section.appendChild( + createSwitch( + "Allow audio from your mic (and audio you pass through) to become stereo", + await userscript.getBoolPref("stereoMic", false), + (enabled) => { + userscript.setStereoMic(enabled); + } + ) + ); + + section.appendChild( createSwitch( "Move discord-awesomeaudio to the system tray instead of closing", await userscript.getBoolPref("trayIcon", false), diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6b135ca..21130fe 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -18,129 +18,148 @@ #include #include #include +#include +#include #include +// Custom network interceptor class +class NetworkInterceptor : public QWebEngineUrlRequestInterceptor { +public: + NetworkInterceptor(QObject* parent = nullptr) : QWebEngineUrlRequestInterceptor(parent) {} + + void interceptRequest(QWebEngineUrlRequestInfo& info) override { + // Modify the request headers here as needed + info.setHttpHeader("Access-Control-Allow-Origin", "*"); + info.setHttpHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + } +}; + MainWindow *MainWindow::m_instance = nullptr; MainWindow::MainWindow(bool useNotifySend, QWidget *parent) : QMainWindow(parent) { - assert(MainWindow::m_instance == nullptr); - MainWindow::m_instance = this; - setupSettings(); - m_settings->setValue("useNotifySend", useNotifySend); - m_centralWidget = new CentralWidget(this); - setCentralWidget(m_centralWidget); - setupTrayIcon(); - setMinimumSize(800, 300); - connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this), - &QShortcut::activated, this, &MainWindow::toggleOrCloseWindow); - if (m_settings->contains("geometry")) { - restoreGeometry(m_settings->value("geometry").toByteArray()); - } else { - resize(1000, 700); - showMaximized(); - } - if (m_settings->value("trayIcon", false).toBool() && - m_settings->value("startHidden", false).toBool()) { - hide(); - QTimer::singleShot(0, [=]() { hide(); }); - } + assert(MainWindow::m_instance == nullptr); + MainWindow::m_instance = this; + setupSettings(); + m_settings->setValue("useNotifySend", useNotifySend); + m_centralWidget = new CentralWidget(this); + setCentralWidget(m_centralWidget); + + // Create and install the network interceptor + NetworkInterceptor* networkInterceptor = new NetworkInterceptor(this); + QWebEngineProfile::defaultProfile()->setRequestInterceptor(networkInterceptor); + + setupTrayIcon(); + setMinimumSize(800, 300); + connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this), + &QShortcut::activated, this, &MainWindow::toggleOrCloseWindow); + if (m_settings->contains("geometry")) { + restoreGeometry(m_settings->value("geometry").toByteArray()); + } else { + resize(1000, 700); + showMaximized(); + } + if (m_settings->value("trayIcon", false).toBool() && + m_settings->value("startHidden", false).toBool()) { + hide(); + QTimer::singleShot(0, [=]() { hide(); }); + } } void MainWindow::fullScreenRequested( - QWebEngineFullScreenRequest fullScreenRequest) { - fullScreenRequest.accept(); - if (fullScreenRequest.toggleOn()) { - m_wasMaximized = isMaximized(); - if (!m_wasMaximized) { - showNormal(); + QWebEngineFullScreenRequest fullScreenRequest) { + fullScreenRequest.accept(); + if (fullScreenRequest.toggleOn()) { + m_wasMaximized = isMaximized(); + if (!m_wasMaximized) { + showNormal(); + } + showFullScreen(); + } else { + m_wasMaximized ? showMaximized() : showNormal(); } - showFullScreen(); - } else { - m_wasMaximized ? showMaximized() : showNormal(); - } } void MainWindow::setupTrayIcon() { - if (m_settings->value("trayIcon", false).toBool() == false || - m_trayIcon != nullptr) - return; + if (m_settings->value("trayIcon", false).toBool() == false || + m_trayIcon != nullptr) + return; - auto aboutAction = new QAction( - "discord-awesomeaudio v" + QString(DISCORD_SCEENAUDIO_VERSION_FULL), this); - aboutAction->setIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png")); - aboutAction->setEnabled(false); + auto aboutAction = new QAction( + "discord-awesomeaudio v" + QString(DISCORD_SCEENAUDIO_VERSION_FULL), this); + aboutAction->setIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png")); + aboutAction->setEnabled(false); - auto exitAction = new QAction("Exit", this); - connect(exitAction, &QAction::triggered, []() { QApplication::quit(); }); + auto exitAction = new QAction("Exit", this); + connect(exitAction, &QAction::triggered, []() { QApplication::quit(); }); - m_trayIconMenu = new QMenu(this); - m_trayIconMenu->addAction(aboutAction); - m_trayIconMenu->addAction(exitAction); + m_trayIconMenu = new QMenu(this); + m_trayIconMenu->addAction(aboutAction); + m_trayIconMenu->addAction(exitAction); - m_trayIcon = new QSystemTrayIcon(this); - m_trayIcon->setContextMenu(m_trayIconMenu); - m_trayIcon->setIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png")); - m_trayIcon->show(); + m_trayIcon = new QSystemTrayIcon(this); + m_trayIcon->setContextMenu(m_trayIconMenu); + m_trayIcon->setIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png")); + m_trayIcon->show(); - connect(m_trayIcon, &QSystemTrayIcon::activated, [this](auto reason) { - if (reason == QSystemTrayIcon::Trigger) { - toggleOrCloseWindow(); - } - }); + connect(m_trayIcon, &QSystemTrayIcon::activated, [this](auto reason) { + if (reason == QSystemTrayIcon::Trigger) { + toggleOrCloseWindow(); + } + }); } void MainWindow::cleanTrayIcon() { - if (m_trayIcon == nullptr) - return; - m_trayIcon->hide(); - m_trayIconMenu->deleteLater(); - m_trayIcon->deleteLater(); - m_trayIconMenu = nullptr; - m_trayIcon = nullptr; + if (m_trayIcon == nullptr) + return; + m_trayIcon->hide(); + m_trayIconMenu->deleteLater(); + m_trayIcon->deleteLater(); + m_trayIconMenu = nullptr; + m_trayIcon = nullptr; } void MainWindow::setupSettings() { - m_settings = - new QSettings("discord-awesomeaudio", "discord-awesomeaudio", this); - m_settings->beginGroup("settings"); - m_settings->endGroup(); + m_settings = + new QSettings("discord-awesomeaudio", "discord-awesomeaudio", this); + m_settings->beginGroup("settings"); + m_settings->endGroup(); } QSettings *MainWindow::settings() const { return m_settings; } void MainWindow::setTrayIcon(bool enabled) { - m_settings->setValue("trayIcon", enabled); - if (enabled) { - setupTrayIcon(); - } else { - cleanTrayIcon(); - } + m_settings->setValue("trayIcon", enabled); + if (enabled) { + setupTrayIcon(); + } else { + cleanTrayIcon(); + } } void MainWindow::closeEvent(QCloseEvent *event) { - if (m_settings->value("trayIcon", false).toBool()) { - hide(); - } else { - m_settings->setValue("geometry", saveGeometry()); - QApplication::quit(); - } + if (m_settings->value("trayIcon", false).toBool()) { + hide(); + } else { + m_settings->setValue("geometry", saveGeometry()); + QApplication::quit(); + } } MainWindow *MainWindow::instance() { return m_instance; } CentralWidget *MainWindow::centralWidget() { - return instance()->m_centralWidget; + return instance()->m_centralWidget; }; void MainWindow::toggleOrCloseWindow() { - if (isVisible()) { - if (m_trayIcon == nullptr) - QApplication::quit(); - else - hide(); - } else { - show(); - activateWindow(); - } + if (isVisible()) { + if (m_trayIcon == nullptr) + QApplication::quit(); + else + hide(); + } else { + show(); + activateWindow(); + } } diff --git a/src/mainwindow.h b/src/mainwindow.h index b8822a8..9a4200d 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -10,31 +10,31 @@ #include #include #include +#include class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(bool useNotifySend = false, QWidget *parent = nullptr); - static MainWindow *instance(); - QSettings *settings() const; - static CentralWidget *centralWidget(); + explicit MainWindow(bool useNotifySend = false, QWidget *parent = nullptr); + static MainWindow *instance(); + QSettings *settings() const; + static CentralWidget *centralWidget(); private: - void setupTrayIcon(); - void cleanTrayIcon(); - void setupSettings(); - QWebEngineProfile *prepareProfile(); - void closeEvent(QCloseEvent *event) override; - QSystemTrayIcon *m_trayIcon = nullptr; - QMenu *m_trayIconMenu; - QSettings *m_settings; - bool m_wasMaximized; - static MainWindow *m_instance; - CentralWidget *m_centralWidget; + void setupTrayIcon(); + void cleanTrayIcon(); + void setupSettings(); + void closeEvent(QCloseEvent *event) override; + QSystemTrayIcon *m_trayIcon = nullptr; + QMenu *m_trayIconMenu; + QSettings *m_settings; + bool m_wasMaximized; + static MainWindow *m_instance; + CentralWidget *m_centralWidget; public Q_SLOTS: - void setTrayIcon(bool enabled); - void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest); - void toggleOrCloseWindow(); + void setTrayIcon(bool enabled); + void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest); + void toggleOrCloseWindow(); };