Gracefully handling signals
Signed-off-by: Dušan Poizl <nou.spiro@gmail.com>
This commit is contained in:
@@ -11,6 +11,11 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <linux/btrfs.h>
|
#include <linux/btrfs.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
int MainWindow::socketPair[2] = {0, 0};
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||||
loading(false),
|
loading(false),
|
||||||
@@ -50,6 +55,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
|||||||
m_database = new Database(this);
|
m_database = new Database(this);
|
||||||
if(!m_database->init())
|
if(!m_database->init())
|
||||||
QMessageBox::critical(this, tr("Can't open DB"), tr("Can't open SQLITE database"));
|
QMessageBox::critical(this, tr("Can't open DB"), tr("Can't open SQLITE database"));
|
||||||
|
|
||||||
|
setupSigterm();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
@@ -87,6 +94,39 @@ void MainWindow::keyReleaseEvent(QKeyEvent *event)
|
|||||||
event->ignore();
|
event->ignore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::setupSigterm()
|
||||||
|
{
|
||||||
|
struct sigaction signal;
|
||||||
|
|
||||||
|
signal.sa_handler = MainWindow::signalHandler;
|
||||||
|
sigemptyset(&signal.sa_mask);
|
||||||
|
signal.sa_flags = 0;
|
||||||
|
signal.sa_flags |= SA_RESTART;
|
||||||
|
|
||||||
|
sigaction(SIGHUP, &signal, 0);
|
||||||
|
sigaction(SIGTERM, &signal, 0);
|
||||||
|
sigaction(SIGINT, &signal, 0);
|
||||||
|
|
||||||
|
::socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair);
|
||||||
|
socketNotifier = new QSocketNotifier(socketPair[1], QSocketNotifier::Read, this);
|
||||||
|
connect(socketNotifier, SIGNAL(activated(int)), this, SLOT(socketNotify()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::signalHandler(int)
|
||||||
|
{
|
||||||
|
char a = 1;
|
||||||
|
::write(socketPair[0], &a, sizeof(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::socketNotify()
|
||||||
|
{
|
||||||
|
socketNotifier->setEnabled(false);
|
||||||
|
char tmp;
|
||||||
|
read(socketPair[1], &tmp, sizeof(tmp));
|
||||||
|
close();
|
||||||
|
socketNotifier->setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::pixmapLoaded(QPixmap pix)
|
void MainWindow::pixmapLoaded(QPixmap pix)
|
||||||
{
|
{
|
||||||
m_image->setImage(pix);
|
m_image->setImage(pix);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QSocketNotifier>
|
||||||
#include "imageringlist.h"
|
#include "imageringlist.h"
|
||||||
#include "imagescrollarea.h"
|
#include "imagescrollarea.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
@@ -14,13 +15,18 @@ class MainWindow : public QMainWindow
|
|||||||
Database *m_database;
|
Database *m_database;
|
||||||
bool loading;
|
bool loading;
|
||||||
int queued;
|
int queued;
|
||||||
|
static int socketPair[2];
|
||||||
|
QSocketNotifier *socketNotifier;
|
||||||
public:
|
public:
|
||||||
MainWindow(QWidget *parent = 0);
|
MainWindow(QWidget *parent = 0);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
protected:
|
protected:
|
||||||
void keyPressEvent(QKeyEvent *event);
|
void keyPressEvent(QKeyEvent *event);
|
||||||
void keyReleaseEvent(QKeyEvent *event);
|
void keyReleaseEvent(QKeyEvent *event);
|
||||||
|
void setupSigterm();
|
||||||
|
static void signalHandler(int);
|
||||||
protected slots:
|
protected slots:
|
||||||
|
void socketNotify();
|
||||||
void updateWindowTitle();
|
void updateWindowTitle();
|
||||||
void pixmapLoaded(QPixmap pix);
|
void pixmapLoaded(QPixmap pix);
|
||||||
void openFile();
|
void openFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user