-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHaltDialog.cpp
115 lines (109 loc) · 2.86 KB
/
HaltDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "HaltDialog.h"
#include "ui_HaltDialog.h"
#include "EventServer.h"
#include <QDebug>
#include <QProcess>
#include <QTimer>
#include <QMutex>
#include <QMutexLocker>
#include <QtMath>
HaltDialog* HaltDialog::m_instance=NULL;
HaltDialog::HaltDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::HaltDialog)
{
ui->setupUi(this);
map.insert("halt","关机");
map.insert("reboot","重启");
timer=new QTimer(this);
timer->setInterval(1000);
beep=false;
beeper=new BeeperThread(this);
connect(timer,SIGNAL(timeout()),this,SLOT(flushTitle()));
connect(ui->cancel,SIGNAL(clicked()),this,SLOT(close()));
connect(ui->cancel,SIGNAL(clicked()),timer,SLOT(stop()));
EventServer::instance()->addMethod(this,"triggerHalt");
EventServer::instance()->addMethod(this,"triggerReboot");
}
HaltDialog::~HaltDialog(){
delete ui;
delete timer;
}
HaltDialog* HaltDialog::instance(){
static QMutex insMutex;
if(!HaltDialog::m_instance){
QMutexLocker locker(&insMutex);
if(!HaltDialog::m_instance)
HaltDialog::m_instance=new HaltDialog();
}
return HaltDialog::m_instance;
}
void HaltDialog::doAction(){
if(QProcess::startDetached("sudo "+action))
qApp->quit();
}
void HaltDialog::triggerHalt(QVariantHash params){
int delay=params.value("delay").toInt();
bool beep=params.value("beep").toBool();
if(delay<1) return;
this->halt(delay,beep);
}
void HaltDialog::triggerReboot(QVariantHash params){
int delay=params.value("delay").toInt();
bool beep=params.value("beep").toBool();
if(delay<1) return;
this->reboot(delay,beep);
}
void HaltDialog::flushTitle(){
this->delay=qMax<quint64>(QDateTime::currentDateTime().secsTo(this->triggerTime),0);
ui->title->setText(QString("系统将于%1秒后%2!").arg(delay).arg(map.value(action)));
if(delay<=0){
timer->stop();
if(beep){
ui->cancel->setEnabled(false);
ui->cancel->setText(QString("即将%1").arg(map.value(action)));
beeper->beep(3);
QTimer::singleShot(650,this,SLOT(doAction()));
}else{
doAction();
}
}
}
void HaltDialog::halt(int delay, bool beep){
this->action="halt";
this->triggerTime=QDateTime::currentDateTime().addSecs(delay);
this->beep=beep;
if(beep)
beeper->beep(2);
if(this->delay>1){
this->flushTitle();
timer->start();
this->show();
}else{
if(beep){
beeper->beep(3);
QTimer::singleShot(650,this,SLOT(doAction()));
}else{
doAction();
}
}
}
void HaltDialog::reboot(int delay, bool beep){
this->action="reboot";
this->triggerTime=QDateTime::currentDateTime().addSecs(delay);
this->beep=beep;
if(beep)
beeper->beep(2);
if(this->delay>1){
this->flushTitle();
timer->start();
this->show();
}else{
if(beep){
beeper->beep(3);
QTimer::singleShot(650,this,SLOT(doAction()));
}else{
doAction();
}
}
}