Merge pull request #149 from Vitalya-code/master

Move two functions into separate file
This commit is contained in:
Malte Jürgens 2023-04-27 18:04:34 +00:00 committed by GitHub
commit 8a6f49b949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 34 deletions

View File

@ -45,6 +45,7 @@ set(discord-screenaudio_SRC
src/log.cpp
src/userscript.cpp
src/centralwidget.cpp
src/localserver.cpp
resources.qrc
)

22
src/localserver.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "localserver.h"
bool isProgramRunning(const QString &program_name) {
QLocalSocket socket;
socket.connectToServer(program_name);
if (socket.waitForConnected()) {
return true; // program is already running
}
return false;
}
void showErrorMessage(const char *text) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setWindowIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png"));
msgBox.exec();
}

9
src/localserver.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "mainwindow.h"
#include <QLocalServer>
#include <QLocalSocket>
#include <QMessageBox>
bool isProgramRunning(const QString &program_name);
void showErrorMessage(const char *text);

View File

@ -1,3 +1,4 @@
#include "localserver.h"
#include "mainwindow.h"
#include "virtmic.h"
@ -12,43 +13,9 @@
#include <QLoggingCategory>
#include <QMessageBox>
void showErrorMessage(const char *text) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setWindowIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png"));
msgBox.exec();
}
bool isProgramRunning(const QString &program_name) {
QLocalSocket socket;
socket.connectToServer(program_name);
if (socket.waitForConnected()) {
return true; // program is already running
}
return false;
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Check if discord is already running
QString program_name = "discord-screenaudio";
if (isProgramRunning(program_name)) {
// if running show error message
showErrorMessage("discord-screenaudio is already running");
return 1;
}
// open server so we can check if discord is running
QLocalServer server;
server.listen(program_name);
QObject::connect(&server, &QLocalServer::newConnection, []() {});
QApplication::setApplicationName("discord-screenaudio");
QApplication::setWindowIcon(
QIcon(":assets/de.shorsh.discord-screenaudio.png"));
@ -88,6 +55,20 @@ int main(int argc, char *argv[]) {
qgetenv("QTWEBENGINE_CHROMIUM_FLAGS"));
MainWindow w(parser.isSet(notifySendOption));
// Check if discord is already running
QString program_name = "discord-screenaudio";
if (isProgramRunning(program_name)) {
// if running show error message
showErrorMessage("discord-screenaudio is already running");
return 1;
}
// open server so we can check if discord is running
QLocalServer server;
server.listen(program_name);
QObject::connect(&server, &QLocalServer::newConnection, []() {});
w.show();
return app.exec();