Files
discord-awesomeaudio/src/mainwindow.cpp

91 lines
2.8 KiB
C++
Raw Normal View History

2022-07-07 13:11:51 +02:00
#include "mainwindow.h"
#include "virtmic.h"
#ifdef KNOTIFICATIONS
2022-07-29 16:08:19 +02:00
#include <KNotification>
2022-07-29 16:31:04 +02:00
#endif
2022-07-09 22:07:14 +02:00
#include <QApplication>
2022-07-07 21:48:23 +02:00
#include <QColor>
2022-07-07 13:11:51 +02:00
#include <QComboBox>
#include <QFile>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QThread>
#include <QUrl>
2022-07-29 16:08:19 +02:00
#include <QWebEngineNotification>
#include <QWebEngineProfile>
2022-07-07 13:11:51 +02:00
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
#include <QWidget>
2022-10-10 19:50:26 +00:00
MainWindow *MainWindow::m_instance = nullptr;
2022-11-04 23:16:49 +01:00
MainWindow::MainWindow(bool useNotifySend, QWidget *parent)
: QMainWindow(parent) {
2022-10-10 19:50:26 +00:00
assert(MainWindow::m_instance == nullptr);
MainWindow::m_instance = this;
2022-11-04 23:16:49 +01:00
m_useNotifySend = useNotifySend;
2022-07-09 22:07:14 +02:00
setupWebView();
2022-07-07 13:11:51 +02:00
resize(1000, 700);
showMaximized();
}
void MainWindow::setupWebView() {
auto page = new DiscordPage(this);
2022-07-27 14:13:59 +02:00
connect(page, &QWebEnginePage::fullScreenRequested, this,
&MainWindow::fullScreenRequested);
m_webView = new QWebEngineView(this);
2022-07-09 22:07:14 +02:00
m_webView->setPage(page);
2022-07-27 14:13:59 +02:00
2022-11-04 23:16:49 +01:00
if (m_useKF5Notifications || m_useNotifySend)
QWebEngineProfile::defaultProfile()->setNotificationPresenter(
[&](std::unique_ptr<QWebEngineNotification> notificationInfo) {
if (m_useNotifySend) {
auto title = notificationInfo->title();
auto message = notificationInfo->message();
auto image_path =
QString("/tmp/discord-screenaudio-%1.png").arg(title);
notificationInfo->icon().save(image_path);
QProcess::execute("notify-send",
{"--icon", image_path, "--app-name",
"discord-screenaudio", title, message});
} else if (m_useKF5Notifications) {
KNotification *notification =
new KNotification("discordNotification");
notification->setTitle(notificationInfo->title());
notification->setText(notificationInfo->message());
notification->setPixmap(
QPixmap::fromImage(notificationInfo->icon()));
notification->setDefaultAction("View");
connect(notification, &KNotification::defaultActivated,
[&, notificationInfo = std::move(notificationInfo)]() {
notificationInfo->click();
activateWindow();
});
notification->sendEvent();
}
});
2022-07-29 16:08:19 +02:00
2022-07-07 13:11:51 +02:00
setCentralWidget(m_webView);
}
2022-07-27 14:13:59 +02:00
void MainWindow::fullScreenRequested(
QWebEngineFullScreenRequest fullScreenRequest) {
fullScreenRequest.accept();
if (fullScreenRequest.toggleOn()) {
m_wasMaximized = isMaximized();
showFullScreen();
} else {
m_wasMaximized ? showMaximized() : showNormal();
}
2022-07-27 14:13:59 +02:00
}
void MainWindow::closeEvent(QCloseEvent *event) { QApplication::quit(); }
2022-10-10 19:50:26 +00:00
MainWindow *MainWindow::instance() { return m_instance; }