-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainmenu.cpp
121 lines (112 loc) · 4.28 KB
/
mainmenu.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
116
117
118
119
120
121
/*
* Powersched
* Copyright (C) 2019 Jiri Babocky
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mainmenu.h"
#include "settings.h"
#include <QMenu>
#include <QApplication>
#include <QStandardPaths>
#include <QTimer>
MainMenu::MainMenu(QObject* parent):QSystemTrayIcon(parent){
disp = XOpenDisplay(nullptr);
profiles = new QActionGroup(this);
// build ui
QIcon icon = QIcon::fromTheme("computer");
setIcon(icon);
menu = new QMenu();
QAction* exit = new QAction(QIcon::fromTheme("application-exit"), "Exit", this);
QAction* settings = new QAction(QIcon::fromTheme("configure"), "Settings", this);
separator = menu->addSeparator();
menu->addAction(settings);
menu->addAction(exit);
loadModels();
setContextMenu(menu);
timer = new QTimer(this);
planAction();
// connect
connect(exit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(settings, SIGNAL(triggered()), this, SLOT(onSettings()));
connect(profiles, SIGNAL(triggered(QAction*)), this, SLOT(profileSelected(QAction*)));
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
}
MainMenu::~MainMenu(){
delete menu;
XCloseDisplay(disp);
}
void MainMenu::onSettings(){
Settings* settings = new Settings();
if (settings->exec()){
settings->saveSettings();
loadModels();
planAction();
}
delete settings;
}
void MainMenu::loadModels(){
if (profileModel)
delete profileModel;
if (triggerModel)
delete triggerModel;
profileModel = new ProfileModel(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)+"/profiles.json", this);
triggerModel = new TriggerModel(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)+"/triggers.json", profileModel, this);
// clear profiles
for (QAction* action:profiles->actions()){
menu->removeAction(action);
profiles->removeAction(action);
delete action;
}
for (int i = 0; i < profileModel->rowCount(); i++){
QAction* action = new QAction(profileModel->index(i).data().toString(), this);
action->setCheckable(true);
menu->insertAction(separator, action);
profiles->addAction(action);
}
}
void MainMenu::profileSelected(QAction* action){
QModelIndex profile = profileModel->find(action->text());
DPMSEnable(disp);
XSetScreenSaver(disp, 0, 0, DefaultBlanking, DefaultExposures);
DPMSSetTimeouts(disp, profile.data(ProfileModel::StandbyRole).toUInt()*60, profile.data(ProfileModel::SuspendRole).toUInt()*60, profile.data(ProfileModel::OffRole).toUInt()*60);
if (profile.data(ProfileModel::AwakeRole).toBool())
DPMSForceLevel(disp, DPMSModeOn);
XFlush(disp);
}
void MainMenu::planAction(){
timer->stop();
if (triggerModel->rowCount() == 0)
return;
int64_t timeout = triggerModel->index(0).data(TriggerModel::TimeoutRole).toLongLong();
QString profile = triggerModel->index(0).data(TriggerModel::ProfileRole).toPersistentModelIndex().data().toString();
for (int i = 0; i < triggerModel->rowCount(); i++){
if (triggerModel->index(i).data(TriggerModel::TimeoutRole).toLongLong() < timeout){
timeout = triggerModel->index(i).data(TriggerModel::TimeoutRole).toLongLong();
profile = triggerModel->index(i).data(TriggerModel::ProfileRole).toPersistentModelIndex().data().toString();
}
}
// schedule timer
profileToActivate = profile;
timer->start(timeout);
}
void MainMenu::onTimer(){
for (QAction* action: profiles->actions()){
if (action->text() == profileToActivate){
action->activate(QAction::Trigger);
planAction();
return;
}
}
planAction();
}