aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-09-21 12:55:37 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-09-21 12:55:37 +0200
commit21afec7d459855808444ef8157a2249b2fb2782c (patch)
tree837a9914bc75314ab6946f8b1a2a9f87c19cb0dc
parent121297159b40b9cd8d5016893e83e9f01d0795ec (diff)
resolve quit segfault
-rw-r--r--flake.lock6
-rw-r--r--flake.nix16
-rw-r--r--src/main.cpp47
-rw-r--r--src/webwindow.cpp129
-rw-r--r--src/webwindow.hpp11
5 files changed, 109 insertions, 100 deletions
diff --git a/flake.lock b/flake.lock
index 34dde40..cd9a0f4 100644
--- a/flake.lock
+++ b/flake.lock
@@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
- "lastModified": 1726243404,
- "narHash": "sha256-sjiGsMh+1cWXb53Tecsm4skyFNag33GPbVgCdfj3n9I=",
+ "lastModified": 1726755586,
+ "narHash": "sha256-PmUr/2GQGvFTIJ6/Tvsins7Q43KTMvMFhvG6oaYK+Wk=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "345c263f2f53a3710abe117f28a5cb86d0ba4059",
+ "rev": "c04d5652cfa9742b1d519688f65d1bbccea9eb7e",
"type": "github"
},
"original": {
diff --git a/flake.nix b/flake.nix
index 21b5f58..a935eac 100644
--- a/flake.nix
+++ b/flake.nix
@@ -14,14 +14,18 @@
packages.x86_64-linux.default = pkgs.stdenv.mkDerivation {
name = "webtray";
src = self;
- buildPhase = "qmake6 . && make";
+ buildPhase = "qmake . -- -webengine-webrtc-pipewire && make";
installPhase = "mkdir -p $out/bin; install -t $out/bin webtray";
- buildInputs = [
- pkgs.kdePackages.wrapQtAppsHook
- pkgs.kdePackages.qmake
- pkgs.kdePackages.qtwebengine
- pkgs.kdePackages.qtwayland
+ nativeBuildInputs = with pkgs; [
+ qt6.wrapQtAppsHook
+ makeWrapper
+ ];
+
+ buildInputs = with pkgs; [
+ qt6.qmake
+ qt6.full
+ qt6.qtbase
];
};
diff --git a/src/main.cpp b/src/main.cpp
index cf14cf6..1ba4637 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,7 +1,6 @@
#include <QtWidgets/QApplication>
#include <iostream>
-#include <thread>
using namespace std::chrono_literals;
@@ -33,51 +32,47 @@ extract_url(const QStringList arguments)
int
main(int argc, char **argv)
{
- QApplication app(argc, argv);
- app.setQuitOnLastWindowClosed(false);
+ QApplication *app = new QApplication(argc, argv);
+ app->setQuitOnLastWindowClosed(false);
- QString url = extract_url(app.arguments());
+ QString url = extract_url(app->arguments());
if (url.isEmpty()) {
std::cerr << "webtray [--open-at-startup] <url>\n";
return -1;
}
- WebWindow webwindow(url);
- Tray tray;
+ WebWindow *webwindow = new WebWindow(url);
+ Tray *tray = new Tray();
- bool start_hidden = not app.arguments().contains("--open-at-startup");
+ bool start_hidden = not app->arguments().contains("--open-at-startup");
for (auto feature : features) {
- tray.set_permission(feature, webwindow.permissions().get(feature));
+ tray->set_permission(feature, webwindow->permissions().get(feature));
}
- webwindow.connect_icon_changed([&](auto icon) {
- tray.setIcon(icon);
- tray.show();
+ webwindow->connect_icon_changed([&](auto icon) {
+ tray->setIcon(icon);
+ tray->show();
});
- webwindow.connect_title_changed([&](auto title) { tray.set_title(title); });
+ webwindow->connect_title_changed([&](auto title) { tray->set_title(title); });
- webwindow.connect_notification([&](auto notification) {
- tray.send_notification(std::move(notification));
+ webwindow->connect_notification([&](auto notification) {
+ tray->send_notification(std::move(notification));
});
- tray.connect_toggle([&]() { webwindow.toggle_visibility(); });
- tray.connect_quit([&]() { webwindow.quit(); });
- tray.connect_permission_changed([&](auto feature, auto value) {
- webwindow.permissions().set(feature, value);
+ tray->connect_toggle([&]() { webwindow->toggle_visibility(); });
+ tray->connect_quit([&]() { webwindow->quit(); });
+ tray->connect_permission_changed([&](auto feature, auto value) {
+ webwindow->permissions().set(feature, value);
});
- tray.connect_reset_cookies([&]() { webwindow.reset_cookies(); });
+ tray->connect_reset_cookies([&]() { webwindow->reset_cookies(); });
- webwindow.show();
+ webwindow->show();
if (start_hidden) {
- webwindow.hide();
+ webwindow->hide();
}
- while (!tray.isSystemTrayAvailable()) {
- std::this_thread::sleep_for(50ms);
- };
-
- return app.exec();
+ return app->exec();
}
diff --git a/src/webwindow.cpp b/src/webwindow.cpp
index e8a83ad..823eec7 100644
--- a/src/webwindow.cpp
+++ b/src/webwindow.cpp
@@ -14,87 +14,90 @@
WebWindow::WebWindow(const QString &url)
: QMainWindow()
- , profile(QUrl(url).host())
- , page(&profile)
- , perm((profile.persistentStoragePath() + "/permissions.state").toStdString())
{
+ this->profile = new QWebEngineProfile(QUrl(url).host());
+
+ this->page = new QWebEnginePage(this->profile);
+ this->perm = new PermissionManager(
+ (profile->persistentStoragePath() + "/permissions.state").toStdString());
+ this->view = new QWebEngineView(this->page);
+
this->web_configure();
- this->profile.setPersistentCookiesPolicy(
+ this->profile->setPersistentCookiesPolicy(
QWebEngineProfile::AllowPersistentCookies);
- this->view.setPage(&this->page);
- this->view.setUrl(url);
+ this->view->setUrl(url);
- this->setCentralWidget(&this->view);
+ this->setCentralWidget(this->view);
}
void
WebWindow::web_configure()
{
- this->page.settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled,
- true);
+ this->page->settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled,
+ true);
- this->page.settings()->setAttribute(
+ this->page->settings()->setAttribute(
QWebEngineSettings::WebRTCPublicInterfacesOnly, false);
- this->page.settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
- false);
- this->page.settings()->setAttribute(
+ this->page->settings()->setAttribute(
+ QWebEngineSettings::ScrollAnimatorEnabled, false);
+ this->page->settings()->setAttribute(
QWebEngineSettings::FullScreenSupportEnabled, true);
- this->profile.setPushServiceEnabled(true);
-
- this->page.connect(&this->page,
- &QWebEnginePage::featurePermissionRequested,
- [&](const QUrl origin, QWebEnginePage::Feature feature) {
- this->permission_requested(origin, feature);
- });
-
- this->profile.connect(
- &this->profile,
- &QWebEngineProfile::downloadRequested,
- [&](QWebEngineDownloadRequest *request) {
- const QUrl url = QFileDialog::getSaveFileUrl(
- this,
- "",
- QUrl(this->profile.downloadPath() + "/" + request->downloadFileName()));
- if (!url.isEmpty()) {
- request->setDownloadFileName(url.path());
- request->accept();
- }
- });
-
- this->page.connect(&this->page,
- &QWebEnginePage::newWindowRequested,
- [=](QWebEngineNewWindowRequest &request) {
- QDesktopServices::openUrl(request.requestedUrl());
- });
-
- this->page.connect(&this->page,
- &QWebEnginePage::fullScreenRequested,
- [=](QWebEngineFullScreenRequest request) {
- request.accept();
- if (request.toggleOn()) {
- this->showFullScreen();
- } else {
- this->showNormal();
- }
- });
+ this->profile->setPushServiceEnabled(true);
+
+ this->page->connect(this->page,
+ &QWebEnginePage::featurePermissionRequested,
+ [&](const QUrl origin, QWebEnginePage::Feature feature) {
+ this->permission_requested(origin, feature);
+ });
+
+ this->profile->connect(this->profile,
+ &QWebEngineProfile::downloadRequested,
+ [&](QWebEngineDownloadRequest *request) {
+ const QUrl url = QFileDialog::getSaveFileUrl(
+ this,
+ "",
+ QUrl(this->profile->downloadPath() + "/" +
+ request->downloadFileName()));
+ if (!url.isEmpty()) {
+ request->setDownloadFileName(url.path());
+ request->accept();
+ }
+ });
+
+ this->page->connect(this->page,
+ &QWebEnginePage::newWindowRequested,
+ [=](QWebEngineNewWindowRequest &request) {
+ QDesktopServices::openUrl(request.requestedUrl());
+ });
+
+ this->page->connect(this->page,
+ &QWebEnginePage::fullScreenRequested,
+ [=](QWebEngineFullScreenRequest request) {
+ request.accept();
+ if (request.toggleOn()) {
+ this->showFullScreen();
+ } else {
+ this->showNormal();
+ }
+ });
}
void
WebWindow::permission_requested(const QUrl origin,
QWebEnginePage::Feature feature)
{
- if (this->perm.get(feature)) {
- this->page.setFeaturePermission(
+ if (this->perm->get(feature)) {
+ this->page->setFeaturePermission(
origin, feature, QWebEnginePage::PermissionGrantedByUser);
} else {
- this->page.setFeaturePermission(
+ this->page->setFeaturePermission(
origin, feature, QWebEnginePage::PermissionDeniedByUser);
}
- this->page.setFeaturePermission(
+ this->page->setFeaturePermission(
origin, feature, QWebEnginePage::PermissionUnknown);
}
@@ -108,33 +111,33 @@ WebWindow::closeEvent(QCloseEvent *event)
void
WebWindow::connect_icon_changed(std::function<void(const QIcon)> fn)
{
- this->view.connect(&this->view, &QWebEngineView::iconChanged, fn);
+ this->view->connect(this->view, &QWebEngineView::iconChanged, fn);
}
void
WebWindow::connect_notification(
std::function<void(std::unique_ptr<QWebEngineNotification>)> fn)
{
- this->profile.setNotificationPresenter(fn);
+ this->profile->setNotificationPresenter(fn);
}
void
WebWindow::connect_title_changed(std::function<void(const QString)> fn)
{
- this->view.connect(&this->view, &QWebEngineView::titleChanged, fn);
+ this->view->connect(this->view, &QWebEngineView::titleChanged, fn);
}
PermissionManager &
WebWindow::permissions()
{
- return this->perm;
+ return *this->perm;
}
void
WebWindow::reset_cookies()
{
std::filesystem::remove_all(
- this->profile.persistentStoragePath().toStdString());
+ this->profile->persistentStoragePath().toStdString());
QProcess process;
QStringList args = QApplication::instance()->arguments();
@@ -155,5 +158,11 @@ WebWindow::toggle_visibility()
void
WebWindow::quit()
{
+ this->page->disconnect();
+ this->view->disconnect();
+ this->profile->disconnect();
+ delete this->view;
+ delete this->page;
+ delete this->perm;
QApplication::instance()->quit();
}
diff --git a/src/webwindow.hpp b/src/webwindow.hpp
index 1d4a83e..c29a953 100644
--- a/src/webwindow.hpp
+++ b/src/webwindow.hpp
@@ -14,10 +14,10 @@
class WebWindow : public QMainWindow
{
private:
- QWebEngineProfile profile;
- QWebEnginePage page;
- QWebEngineView view;
- PermissionManager perm;
+ QWebEngineProfile *profile;
+ QWebEnginePage *page;
+ QWebEngineView *view;
+ PermissionManager *perm;
void web_configure();
void permission_requested(const QUrl origin, QWebEnginePage::Feature feature);
@@ -27,7 +27,8 @@ public:
WebWindow(const QString &url);
void connect_icon_changed(std::function<void(const QIcon)> fn);
void connect_title_changed(std::function<void(const QString)> fn);
- void connect_notification(std::function<void(std::unique_ptr<QWebEngineNotification>)> fn);
+ void connect_notification(
+ std::function<void(std::unique_ptr<QWebEngineNotification>)> fn);
PermissionManager &permissions();
void reset_cookies();
void toggle_visibility();