2023-01-25 14:59:48 -05:00
# include "mainwindow.h"
# include <QApplication>
2023-03-22 12:36:24 -04:00
# include <QMessageBox>
# include <QProcess>
# include <QPushButton>
# include <QFile>
int main ( int argc , char * argv [ ] ) {
2023-01-25 14:59:48 -05:00
QApplication a ( argc , argv ) ;
2023-03-22 12:36:24 -04:00
bool isLock = false ;
int pid = 0 ;
QFile lockFile ( DAQLockFile ) ;
if ( lockFile . open ( QIODevice : : Text | QIODevice : : ReadOnly ) ) {
QTextStream in ( & lockFile ) ;
QString line = in . readLine ( ) ;
isLock = line . toInt ( ) ;
lockFile . close ( ) ;
}
QFile pidFile ( PIDFile ) ;
if ( pidFile . open ( QIODevice : : Text | QIODevice : : ReadOnly ) ) {
QTextStream in ( & pidFile ) ;
QString line = in . readLine ( ) ;
pid = line . toInt ( ) ;
pidFile . close ( ) ;
}
if ( isLock ) {
qDebug ( ) < < " The DAQ program is already opened. PID is " + QString : : number ( pid ) + " , and delete the " + DAQLockFile ;
QMessageBox msgBox ;
msgBox . setWindowTitle ( " Oopss.... " ) ;
2023-03-24 19:23:36 -04:00
msgBox . setText ( " The DAQ program is already opened, or crashed perviously. \n PID is " + QString : : number ( pid ) + " \n You can kill the procee by \" kill -9 <pid> \" and delete the " + DAQLockFile + " \n or click the \" Kill \" button " ) ;
2023-03-22 12:36:24 -04:00
msgBox . setIcon ( QMessageBox : : Information ) ;
2023-03-27 18:34:22 -04:00
QPushButton * kill = msgBox . addButton ( " Kill and Open New " , QMessageBox : : AcceptRole ) ;
2023-03-22 12:36:24 -04:00
msgBox . setStandardButtons ( QMessageBox : : Ok ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . exec ( ) ;
if ( msgBox . clickedButton ( ) = = kill ) {
remove ( DAQLockFile ) ;
QProcess : : execute ( " kill " , QStringList ( ) < < " -9 " < < QString : : number ( pid ) ) ;
} else {
return 0 ;
}
}
lockFile . open ( QIODevice : : Text | QIODevice : : WriteOnly ) ;
lockFile . write ( " 1 " ) ;
lockFile . close ( ) ;
pidFile . open ( QIODevice : : Text | QIODevice : : WriteOnly ) ;
pidFile . write ( QString : : number ( QCoreApplication : : applicationPid ( ) ) . toStdString ( ) . c_str ( ) ) ;
pidFile . close ( ) ;
2023-01-25 14:59:48 -05:00
MainWindow w ;
w . show ( ) ;
return a . exec ( ) ;
2023-03-22 12:36:24 -04:00
2023-01-25 14:59:48 -05:00
}