aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r--src/mainwindow.cpp187
1 files changed, 41 insertions, 146 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 3d01ba0..c8d688e 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -39,7 +39,8 @@ void MainWindow::restoreMainWindow() {
if (settings.value("geometry").isValid()) {
restoreGeometry(settings.value("geometry").toByteArray());
QPoint pos = QCursor::pos();
- for (QScreen *screen : QGuiApplication::screens()) {
+ auto localScreens = QGuiApplication::screens();
+ for (auto screen : qAsConst(localScreens)) {
QRect screenRect = screen->geometry();
if (screenRect.contains(pos)) {
this->move(screenRect.center() - this->rect().center());
@@ -205,11 +206,11 @@ void MainWindow::updateWindowTheme() {
settings.value("widgetStyle", "Fusion").toString()));
if (settings.value("windowTheme", "light").toString() == "dark") {
qApp->setPalette(darkPalette);
- this->webEngine->setStyleSheet(
+ webEngine->setStyleSheet(
"QWebEngineView{background:rgb(17, 27, 33);}"); // whatsapp dark color
} else {
qApp->setPalette(lightPalette);
- this->webEngine->setStyleSheet(
+ webEngine->setStyleSheet(
"QWebEngineView{background:#F0F0F0;}"); // whatsapp light color
}
@@ -316,7 +317,7 @@ void MainWindow::initSettingWidget() {
webSettings->setAttribute(
QWebEngineSettings::PlaybackRequiresUserGesture, checked);
- this->webEngine->page()->profile()->settings()->setAttribute(
+ webEngine->page()->profile()->settings()->setAttribute(
QWebEngineSettings::PlaybackRequiresUserGesture, checked);
});
@@ -356,9 +357,8 @@ void MainWindow::initSettingWidget() {
});
connect(settingsWidget, &SettingsWidget::notificationPopupTimeOutChanged,
- settingsWidget, [=]() {
- setNotificationPresenter(this->webEngine->page()->profile());
- });
+ settingsWidget,
+ [=]() { setNotificationPresenter(webEngine->page()->profile()); });
connect(settingsWidget, &SettingsWidget::notify, settingsWidget,
[=](QString message) { notify("", message); });
@@ -381,7 +381,7 @@ void MainWindow::initSettingWidget() {
settings.value("lockscreen", false).toBool());
// spell checker
- settingsWidget->loadDictionaries(m_dictionaries);
+ settingsWidget->loadDictionaries(dictionaries);
}
}
@@ -392,7 +392,8 @@ void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
}
-void MainWindow::handleZoomOnWindowStateChange(QWindowStateChangeEvent *ev) {
+void MainWindow::handleZoomOnWindowStateChange(
+ const QWindowStateChangeEvent *ev) {
if (settingsWidget != nullptr) {
if (ev->oldState().testFlag(Qt::WindowMaximized) &&
windowState().testFlag(Qt::WindowNoState)) {
@@ -479,7 +480,7 @@ void MainWindow::showSettings(bool isAskedByCLI) {
void MainWindow::updateSettingsUserAgentWidget() {
settingsWidget->updateDefaultUAButton(
- this->webEngine->page()->profile()->httpUserAgent());
+ webEngine->page()->profile()->httpUserAgent());
}
void MainWindow::askToReloadPage() {
@@ -743,17 +744,18 @@ void MainWindow::appAutoLockChanged() {
void MainWindow::checkWindowState() {
QObject *tray_icon_menu = this->findChild<QObject *>("trayIconMenu");
if (tray_icon_menu != nullptr) {
+ QMenu *menu = qobject_cast<QMenu *>(tray_icon_menu);
if (this->isVisible()) {
- ((QMenu *)(tray_icon_menu))->actions().at(0)->setDisabled(false);
- ((QMenu *)(tray_icon_menu))->actions().at(1)->setDisabled(true);
+ menu->actions().at(0)->setDisabled(false);
+ menu->actions().at(1)->setDisabled(true);
} else {
- ((QMenu *)(tray_icon_menu))->actions().at(0)->setDisabled(true);
- ((QMenu *)(tray_icon_menu))->actions().at(1)->setDisabled(false);
+ menu->actions().at(0)->setDisabled(true);
+ menu->actions().at(1)->setDisabled(false);
}
if (lockWidget && lockWidget->getIsLocked()) {
- ((QMenu *)(tray_icon_menu))->actions().at(4)->setDisabled(true);
+ menu->actions().at(4)->setDisabled(true);
} else {
- ((QMenu *)(tray_icon_menu))->actions().at(4)->setDisabled(false);
+ menu->actions().at(4)->setDisabled(false);
}
}
}
@@ -804,18 +806,18 @@ void MainWindow::createWebEngine() {
widgetSize.setHorizontalStretch(1);
widgetSize.setVerticalStretch(1);
- m_dictionaries = Dictionaries::GetDictionaries();
+ dictionaries = Dictionaries::GetDictionaries();
- WebView *webEngine = new WebView(this, m_dictionaries);
- setCentralWidget(webEngine);
- webEngine->setSizePolicy(widgetSize);
- webEngine->show();
+ WebView *webEngineView = new WebView(this, dictionaries);
+ setCentralWidget(webEngineView);
+ webEngineView->setSizePolicy(widgetSize);
+ webEngineView->show();
- this->webEngine = webEngine;
+ this->webEngine = webEngineView;
- webEngine->addAction(minimizeAction);
- webEngine->addAction(lockAction);
- webEngine->addAction(quitAction);
+ webEngineView->addAction(minimizeAction);
+ webEngineView->addAction(lockAction);
+ webEngineView->addAction(quitAction);
createWebPage(false);
}
@@ -832,11 +834,11 @@ const QIcon MainWindow::getTrayIcon(const int &notificationCount) const {
}
void MainWindow::createWebPage(bool offTheRecord) {
- if (offTheRecord && !m_otrProfile) {
- m_otrProfile.reset(new QWebEngineProfile);
+ if (offTheRecord && !otrProfile) {
+ otrProfile.reset(new QWebEngineProfile);
}
auto profile =
- offTheRecord ? m_otrProfile.get() : QWebEngineProfile::defaultProfile();
+ offTheRecord ? otrProfile.get() : QWebEngineProfile::defaultProfile();
QStringList dict_names;
dict_names.append(settings.value("sc_dict", "en-US").toString());
@@ -864,7 +866,7 @@ void MainWindow::createWebPage(bool offTheRecord) {
QUrl("https://web.whatsapp.com?v=" + QString::number(randomValue)));
connect(profile, &QWebEngineProfile::downloadRequested,
- &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
+ &downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
connect(page, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this,
SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
@@ -964,120 +966,13 @@ void MainWindow::handleLoadFinished(bool loaded) {
checkLoadedCorrectly();
updatePageTheme();
handleZoom();
- injectMutationObserver();
- injectPreventScrollWheelZoomHelper();
- injectFullWidthJavaScript();
- injectClassChangeObserver();
- injectNewChatJavaScript();
+
if (settingsWidget != nullptr) {
settingsWidget->refresh();
}
}
}
-void MainWindow::injectPreventScrollWheelZoomHelper() {
- QString js = R"(
- (function () {
- const SSWZ = function () {
- this.keyScrollHandler = function (e) {
- if (e.ctrlKey) {
- e.preventDefault();
- return false;
- }
- }
- };
- if (window === top) {
- const sswz = new SSWZ();
- window.addEventListener('wheel', sswz.keyScrollHandler, {
- passive: false
- });
- }
- })();
- )";
- webEngine->page()->runJavaScript(js);
-}
-
-void MainWindow::injectClassChangeObserver() {
- QString js = R"(
- const observer = new MutationObserver(() => {
- var haveFullView = document.body.classList.contains('whatsie-full-view');
- var container = document.querySelector('#app > .app-wrapper-web > div');
- if(container){
- if(haveFullView){
- container.style.width = '100%';
- container.style.height = '100%';
- container.style.top = '0';
- container.style.maxWidth = 'unset';
- }else{
- container.style.width = null;
- container.style.height = null;
- container.style.top = null;
- container.style.maxWidth = null;
- }
- }
- });
- observer.observe(document.body, {
- attributes: true,
- attributeFilter: ['class'],
- childList: false,
- characterData: false
- });)";
- webEngine->page()->runJavaScript(js);
-}
-
-void MainWindow::injectMutationObserver() {
- QString js =
- R"(function waitForElement(selector) {
- return new Promise(resolve => {
- if (document.querySelector(selector)) {
- return resolve(document.querySelector(selector));
- }
- const observer = new MutationObserver(mutations => {
- if (document.querySelector(selector)) {
- resolve(document.querySelector(selector));
- observer.disconnect();
- }
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- });
- };)";
- webEngine->page()->runJavaScript(js);
-}
-
-void MainWindow::injectFullWidthJavaScript() {
- if (!settings.value("fullWidthView", true).toBool())
- return;
- QString js =
- R"(waitForElement('#pane-side').then( () => {
- var container = document.querySelector('#app > .app-wrapper-web > div');
- container.style.width = '100%';
- container.style.height = '100%';
- container.style.top = '0';
- container.style.maxWidth = 'unset';
- });
- )";
- webEngine->page()->runJavaScript(js);
-}
-
-void MainWindow::injectNewChatJavaScript() {
- QString js = R"(const openNewChatWhatsie = (phone,text) => {
- const link = document.createElement('a');
- link.setAttribute('href',
- `whatsapp://send/?phone=${phone}&text=${text}`);
- document.body.append(link);
- link.click();
- document.body.removeChild(link);
- };
- function openNewChatWhatsieDefined()
- {
- return (openNewChatWhatsie != 'undefined');
- })";
- webEngine->page()->runJavaScript(js);
-}
-
void MainWindow::checkLoadedCorrectly() {
if (webEngine && webEngine->page()) {
// test 1 based on the class name of body tag of the page
@@ -1172,8 +1067,8 @@ void MainWindow::messageClicked() {
void MainWindow::doAppReload() {
- if (this->webEngine->page()) {
- this->webEngine->page()->disconnect();
+ if (webEngine->page()) {
+ webEngine->page()->disconnect();
}
createWebPage(false);
}
@@ -1195,7 +1090,7 @@ void MainWindow::triggerNewChat(QString phone, QString text) {
"openNewChatWhatsieDefined()",
[this, phone, text](const QVariant &result) {
if (result.toString().contains("true")) {
- this->webEngine->page()->runJavaScript(
+ webEngine->page()->runJavaScript(
QString("openNewChatWhatsie(\"%1\",\"%2\")").arg(phone, text));
} else {
// create send url equivalent
@@ -1203,7 +1098,7 @@ void MainWindow::triggerNewChat(QString phone, QString text) {
textStr = text.isEmpty() ? "" : "text=" + text;
QString urlStr =
"https://web.whatsapp.com/send?" + phoneStr + "&" + textStr;
- this->webEngine->page()->load(QUrl(urlStr));
+ webEngine->page()->load(QUrl(urlStr));
}
this->alreadyRunning();
});
@@ -1212,8 +1107,8 @@ void MainWindow::triggerNewChat(QString phone, QString text) {
void MainWindow::doReload(bool byPassCache, bool isAskedByCLI,
bool byLoadingQuirk) {
if (byLoadingQuirk) {
- this->webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
- byPassCache);
+ webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
+ byPassCache);
} else {
if (lockWidget && !lockWidget->getIsLocked()) {
this->notify(QApplication::applicationName(),
@@ -1229,13 +1124,13 @@ void MainWindow::doReload(bool byPassCache, bool isAskedByCLI,
this->show();
return;
}
- this->webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
- byPassCache);
+ webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
+ byPassCache);
}
}
void MainWindow::toggleMute(const bool &checked) {
- this->webEngine->page()->setAudioMuted(checked);
+ webEngine->page()->setAudioMuted(checked);
}
// get value of page theme when page is loaded