Skip to content

Commit 377333c

Browse files
committed
Prevent session managers from forcefully restoring the app
1 parent 6a0c02f commit 377333c

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/MainWindow.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <QInputDialog>
5252
#include <QMenu>
5353
#include <QMessageBox>
54+
#include <QSessionManager>
5455
#include <QWhatsThis>
5556

5657
using namespace std;
@@ -434,11 +435,16 @@ void MainWindow::closeEvent(QCloseEvent *event)
434435

435436
#endif
436437

437-
if (_trayIcon->isVisible())
438+
if (_trayIcon->isVisible() && !_allowCloseEvent)
438439
{
439440
hide();
440441
event->ignore();
441442
}
443+
else
444+
{
445+
event->accept();
446+
QMainWindow::closeEvent(event);
447+
}
442448
}
443449

444450
void MainWindow::resizeEvent(QResizeEvent *event)
@@ -447,6 +453,11 @@ void MainWindow::resizeEvent(QResizeEvent *event)
447453
AppConfig::instance().setBytes(AppConfig::LastWindowGeometry, saveGeometry());
448454
}
449455

456+
void MainWindow::shutdown()
457+
{
458+
_allowCloseEvent = true;
459+
this->close();
460+
}
450461

451462
// Systray
452463
void MainWindow::raiseWindow()
@@ -1280,3 +1291,4 @@ void MainWindow::launchFirstRunSetup()
12801291
});
12811292
});
12821293
}
1294+

src/MainWindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public slots:
7171
void onRelinkRequested();
7272
void raiseWindow();
7373
void launchFirstRunSetup();
74+
void shutdown();
7475

7576
private slots:
7677
void applyConfig();
@@ -125,6 +126,7 @@ private slots:
125126
IAudioService* _audioService = nullptr;
126127

127128
bool _blockApply = false;
129+
bool _allowCloseEvent = false;
128130

129131
QString _currentImpulseResponse = "";
130132
QString _currentVdc = "";

src/main.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QStyle>
1111
#include <QScopeGuard>
1212
#include <QTextStream>
13+
#include <QSessionManager>
1314

1415
#define FORCE_CRASH_HANDLER
1516

@@ -112,8 +113,13 @@ int main(int argc,
112113
Log::information("Application version: " + QString(APP_VERSION_FULL));
113114
Log::information("Qt library version: " + QString(qVersion()));
114115

115-
116-
Log::debug("Launched by system session manager: " + QString(qApp->isSessionRestored() ? "yes" : "no"));
116+
Log::debug("Launched by system session manager: " + QString(qApp->isSessionRestored() ? "yes" : "no")); /* unreliable */
117+
QGuiApplication::setFallbackSessionManagementEnabled(false);
118+
QObject::connect(qApp, &QGuiApplication::saveStateRequest, qApp, [](QSessionManager &manager){
119+
// Block session restore requests
120+
manager.setRestartHint(QSessionManager::RestartNever);
121+
manager.release();
122+
}, Qt::DirectConnection);
117123

118124
QFile id("/var/lib/dbus/machine-id");
119125
if(id.open(QFile::ReadOnly | QFile::Text))
@@ -146,7 +152,6 @@ int main(int argc,
146152
DspConfig::instance(parser.isSet(watch));
147153
AppConfig::instance().set(AppConfig::ExecutablePath, QString::fromLocal8Bit(exepath));
148154

149-
QApplication::setFallbackSessionManagementEnabled(false);
150155
MainWindow w(parser.isSet(tray));
151156

152157
QObject::connect(instanceMonitor, &SingleInstanceMonitor::raiseWindow, &w, &MainWindow::raiseWindow);
@@ -160,7 +165,7 @@ int main(int argc,
160165
)
161166
);
162167
w.setWindowFlags(Qt::WindowContextHelpButtonHint | Qt::WindowCloseButtonHint);
163-
w.hide();
168+
w.hide();
164169

165170
if (!parser.isSet(tray))
166171
{
@@ -170,7 +175,7 @@ int main(int argc,
170175
{
171176
w.show();
172177
}
173-
}
178+
}
174179

175180
return QApplication::exec();
176181
}

0 commit comments

Comments
 (0)