@@ -24,121 +24,110 @@ RedShiftQt::RedShiftQt(QWidget *parent) :
24
24
25
25
log = new RedShiftQtLog (this );
26
26
27
- info_timer = new QTimer (this );
28
- info_timer->setTimerType (Qt::VeryCoarseTimer);
27
+ timer = new QTimer (this );
28
+ connect (timer, SIGNAL (timeout ()), this , SLOT (startRedshift ()));
29
+ timer->setTimerType (Qt::VeryCoarseTimer);
29
30
30
- susp_timer = new QTimer (this );
31
- connect (susp_timer, SIGNAL (timeout ()), this , SLOT (toggle ()));
32
- susp_timer->setTimerType (Qt::VeryCoarseTimer);
31
+ helper = new QProcess (this );
32
+ helper->setProgram (conf->value (" redshift_path" ).toString ());
33
33
34
34
redshift = new QProcess (this );
35
- connect (redshift, SIGNAL (stateChanged (QProcess::ProcessState)),
36
- this , SLOT (onRedshiftStateChanged (QProcess::ProcessState)));
37
- // connect(redshift, SIGNAL(readyRead()), this, SLOT(onProcReadyRead()));
38
- // connect(redshift, SINGAL(readyReadStandardError(), this, SLOT(onProcReadyRead)));
39
- onRedshiftStateChanged (QProcess::NotRunning);
40
-
35
+ connect (redshift, SIGNAL (started ()), this , SLOT (onRedshiftStarted ()));
36
+ connect (redshift, SIGNAL (finished (int , QProcess::ExitStatus)), this , SLOT (onRedshiftFinished ()));
41
37
redshift->setProgram (conf->value (" redshift_path" ).toString ());
42
38
redshift->setArguments (getArguments ());
43
-
39
+ onRedshiftFinished ();
44
40
if (conf->value (" start_enabled" ).toBool ())
45
41
redshift->start (QProcess::ReadOnly);
46
42
47
- helper = new QProcess (this );
48
- helper->setProgram (conf->value (" redshift_path" ).toString ());
49
-
50
43
prefs = new RedShiftQtPrefs (this );
51
44
connect (prefs, SIGNAL (confChanged ()), this , SLOT (onConfChanged ()));
52
45
53
46
if (conf->value (" show_prefs" ).toBool ())
54
- prefs-> show ();
47
+ showPrefs ();
55
48
}
56
49
57
50
RedShiftQt::~RedShiftQt ()
51
+ {
52
+ stopRedshift ();
53
+ }
54
+
55
+ QString RedShiftQt::getRedshiftInfo ()
56
+ {
57
+ helper->setArguments (QStringList (" -pv" ));
58
+ helper->start (QProcess::ReadOnly);
59
+ helper->waitForFinished ();
60
+ return (helper->readAllStandardOutput ());
61
+ }
62
+
63
+ void RedShiftQt::startRedshift ()
64
+ {
65
+ timer->stop ();
66
+ redshift->start (QProcess::ReadOnly);
67
+ redshift->waitForStarted ();
68
+ }
69
+
70
+ void RedShiftQt::stopRedshift ()
58
71
{
59
72
redshift->kill ();
60
73
redshift->waitForFinished ();
61
- helper->kill ();
74
+ helper->setArguments (QStringList (" -x" ));
75
+ helper->start (QProcess::ReadOnly);
62
76
helper->waitForFinished ();
63
77
}
64
78
65
- void RedShiftQt::onRedshiftStateChanged (QProcess::ProcessState state )
79
+ void RedShiftQt::onRedshiftStarted ( )
66
80
{
67
- switch (state) {
68
- case QProcess::Running :
69
- tray->setIcon (QIcon (" :/tray/on" ));
70
- tray->setToolTip (windowTitle () +" : Running" );
71
- tray->status ->setChecked (true );
72
- tray->suspend ->setEnabled (true );
73
- log ->setStatus (QString (" Enabled" ));
74
- log ->appendToLog (redshift->program ().toUtf8 () + " " + redshift->arguments ().join (" " ).toUtf8 ());
75
- break ;
76
-
77
- case QProcess::NotRunning :
78
- tray->setIcon (QIcon (" :/tray/off" ));
79
- tray->setToolTip (windowTitle () +" : Suspended" );
80
- tray->status ->setChecked (false );
81
- tray->suspend ->setEnabled (false );
82
- log ->setInfo (QStringList (" Disabled" ));
83
- log ->appendToLog (" Redshift stopped" );
84
- break ;
85
-
86
- default :
87
- break ;
88
- }
89
- qDebug (" Process state: %d" , redshift->state ());
81
+ tray->setIcon (QIcon (" :/tray/on" ));
82
+ tray->setToolTip (windowTitle () + " : Running" );
83
+ tray->status ->setChecked (true );
84
+ tray->suspend ->setEnabled (true );
85
+ log ->setStatus (QString (" Enabled" ));
86
+ log ->setInfo (getRedshiftInfo ());
87
+ log ->appendToLog (" Redshift was started with: " + redshift->arguments ().join (" " ).toUtf8 ());
90
88
}
91
89
92
- void RedShiftQt::onReadyRead ()
90
+ void RedShiftQt::onRedshiftFinished ()
93
91
{
94
-
92
+ tray->setIcon (QIcon (" :/tray/off" ));
93
+ tray->setToolTip (windowTitle () + " : Suspended" );
94
+ tray->status ->setChecked (false );
95
+ tray->suspend ->setEnabled (false );
96
+ log ->setStatus (QString (" Disabled" ));
97
+ log ->setInfo (getRedshiftInfo ());
98
+ log ->appendToLog (" Redshift was suspended" );
95
99
}
96
100
97
101
void RedShiftQt::onConfChanged ()
98
102
{
99
103
int state = redshift->state ();
100
- if (state) {
101
- redshift->kill ();
102
- redshift->waitForFinished ();
103
- }
104
+ if (state) stopRedshift ();
104
105
redshift->setProgram (conf->value (" redshift_path" ).toString ());
105
106
redshift->setArguments (getArguments ());
106
107
helper->setProgram (conf->value (" redshift_path" ).toString ());
107
- if (state) {
108
- redshift->start (QProcess::ReadOnly);
109
- }
108
+ if (state) startRedshift ();
110
109
}
111
110
112
- void RedShiftQt::toggle ( void )
111
+ void RedShiftQt::toggleRedshift ( )
113
112
{
114
- if (redshift->state () == QProcess::Running) {
115
- redshift->kill ();
116
- redshift->waitForFinished ();
117
- helper->setArguments (QStringList (" -x" ));
118
- helper->start (QProcess::ReadOnly);
119
- helper->waitForFinished ();
120
- } else {
121
- susp_timer->stop ();
122
- redshift->start (QProcess::ReadOnly);
123
- redshift->waitForStarted ();
124
- }
113
+ if (redshift->state ()) stopRedshift ();
114
+ else startRedshift ();
125
115
}
126
116
127
- void RedShiftQt::suspend (QAction* action )
117
+ void RedShiftQt::toggleRedshift (QSystemTrayIcon::ActivationReason reason )
128
118
{
129
- toggle ();
130
-
131
- susp_timer->setInterval (action->data ().toInt () * 60 * 1000 );
132
- susp_timer->start ();
133
-
134
- tray->setToolTip (tray->toolTip () + QString (" for %1 minutes" ).arg (action->data ().toInt ()));
135
- log ->appendToLog (QString (" Suspending reshift for %1 minutes" ).arg (susp_timer->interval ()/60 /1000 ));
119
+ if (reason == QSystemTrayIcon::Trigger) toggleRedshift ();
136
120
}
137
121
138
- void RedShiftQt::activated (QSystemTrayIcon::ActivationReason reason )
122
+ void RedShiftQt::suspendRedshift (QAction* action )
139
123
{
140
- if (reason == QSystemTrayIcon::Trigger)
141
- toggle ();
124
+ stopRedshift ();
125
+
126
+ timer->setInterval (action->data ().toInt () * 60 * 1000 );
127
+ timer->start ();
128
+
129
+ tray->setToolTip (tray->toolTip () + QString (" for %1 minutes" ).arg (action->data ().toInt ()));
130
+ log ->appendToLog (QString (" Suspending reshift for %1 minutes" ).arg (timer->interval ()/60 /1000 ));
142
131
}
143
132
144
133
void RedShiftQt::showPrefs ()
@@ -149,6 +138,7 @@ void RedShiftQt::showPrefs()
149
138
150
139
void RedShiftQt::showLog ()
151
140
{
141
+ log ->setInfo (getRedshiftInfo ());
152
142
log ->setVisible (true );
153
143
log ->raise ();
154
144
}
0 commit comments