diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 47 | ||||
-rw-r--r-- | src/webwindow.cpp | 129 | ||||
-rw-r--r-- | src/webwindow.hpp | 11 |
3 files changed, 96 insertions, 91 deletions
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(); |