@@ -24,121 +24,110 @@ RedShiftQt::RedShiftQt(QWidget *parent) :
2424
2525 log = new RedShiftQtLog (this );
2626
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);
2930
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 ());
3333
3434 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 ()));
4137 redshift->setProgram (conf->value (" redshift_path" ).toString ());
4238 redshift->setArguments (getArguments ());
43-
39+ onRedshiftFinished ();
4440 if (conf->value (" start_enabled" ).toBool ())
4541 redshift->start (QProcess::ReadOnly);
4642
47- helper = new QProcess (this );
48- helper->setProgram (conf->value (" redshift_path" ).toString ());
49-
5043 prefs = new RedShiftQtPrefs (this );
5144 connect (prefs, SIGNAL (confChanged ()), this , SLOT (onConfChanged ()));
5245
5346 if (conf->value (" show_prefs" ).toBool ())
54- prefs-> show ();
47+ showPrefs ();
5548}
5649
5750RedShiftQt::~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 ()
5871{
5972 redshift->kill ();
6073 redshift->waitForFinished ();
61- helper->kill ();
74+ helper->setArguments (QStringList (" -x" ));
75+ helper->start (QProcess::ReadOnly);
6276 helper->waitForFinished ();
6377}
6478
65- void RedShiftQt::onRedshiftStateChanged (QProcess::ProcessState state )
79+ void RedShiftQt::onRedshiftStarted ( )
6680{
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 ());
9088}
9189
92- void RedShiftQt::onReadyRead ()
90+ void RedShiftQt::onRedshiftFinished ()
9391{
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" );
9599}
96100
97101void RedShiftQt::onConfChanged ()
98102{
99103 int state = redshift->state ();
100- if (state) {
101- redshift->kill ();
102- redshift->waitForFinished ();
103- }
104+ if (state) stopRedshift ();
104105 redshift->setProgram (conf->value (" redshift_path" ).toString ());
105106 redshift->setArguments (getArguments ());
106107 helper->setProgram (conf->value (" redshift_path" ).toString ());
107- if (state) {
108- redshift->start (QProcess::ReadOnly);
109- }
108+ if (state) startRedshift ();
110109}
111110
112- void RedShiftQt::toggle ( void )
111+ void RedShiftQt::toggleRedshift ( )
113112{
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 ();
125115}
126116
127- void RedShiftQt::suspend (QAction* action )
117+ void RedShiftQt::toggleRedshift (QSystemTrayIcon::ActivationReason reason )
128118{
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 ();
136120}
137121
138- void RedShiftQt::activated (QSystemTrayIcon::ActivationReason reason )
122+ void RedShiftQt::suspendRedshift (QAction* action )
139123{
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 ));
142131}
143132
144133void RedShiftQt::showPrefs ()
@@ -149,6 +138,7 @@ void RedShiftQt::showPrefs()
149138
150139void RedShiftQt::showLog ()
151140{
141+ log->setInfo (getRedshiftInfo ());
152142 log->setVisible (true );
153143 log->raise ();
154144}
0 commit comments