|
| 1 | +#include "redshiftqt.h" |
| 2 | + |
| 3 | +RedShiftQt::RedShiftQt(QWidget *parent) : |
| 4 | + QMainWindow(parent) |
| 5 | +{ |
| 6 | + |
| 7 | + setWindowIcon(QIcon(":/app/icon")); |
| 8 | + setWindowTitle(qApp->applicationName()); |
| 9 | + setGeometry(QStyle::alignedRect( |
| 10 | + Qt::LeftToRight, |
| 11 | + Qt::AlignCenter, |
| 12 | + QSize(0,0), |
| 13 | + qApp->desktop()->availableGeometry())); |
| 14 | + |
| 15 | + conf = new QSettings( |
| 16 | + QSettings::IniFormat, |
| 17 | + QSettings::UserScope, |
| 18 | + qApp->organizationName(), |
| 19 | + qApp->applicationName(), |
| 20 | + this); |
| 21 | + |
| 22 | + tray = new RedShiftQtTray(this); |
| 23 | + tray->show(); |
| 24 | + |
| 25 | + log = new RedShiftQtLog(this); |
| 26 | + |
| 27 | + info_timer = new QTimer(this); |
| 28 | + info_timer->setTimerType(Qt::VeryCoarseTimer); |
| 29 | + |
| 30 | + susp_timer = new QTimer(this); |
| 31 | + connect(susp_timer, SIGNAL(timeout()), this, SLOT(toggle())); |
| 32 | + susp_timer->setTimerType(Qt::VeryCoarseTimer); |
| 33 | + |
| 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 | + |
| 41 | + redshift->setProgram(conf->value("redshift_path").toString()); |
| 42 | + redshift->setArguments(getArguments()); |
| 43 | + |
| 44 | + if(conf->value("start_enabled").toBool()) |
| 45 | + redshift->start(QProcess::ReadOnly); |
| 46 | + |
| 47 | + helper = new QProcess(this); |
| 48 | + helper->setProgram(conf->value("redshift_path").toString()); |
| 49 | + |
| 50 | + prefs = new RedShiftQtPrefs(this); |
| 51 | + connect(prefs, SIGNAL(confChanged()), this, SLOT(onConfChanged())); |
| 52 | + |
| 53 | + if(conf->value("show_prefs").toBool()) |
| 54 | + prefs->show(); |
| 55 | +} |
| 56 | + |
| 57 | +RedShiftQt::~RedShiftQt() |
| 58 | +{ |
| 59 | + redshift->kill(); |
| 60 | + redshift->waitForFinished(); |
| 61 | + helper->kill(); |
| 62 | + helper->waitForFinished(); |
| 63 | +} |
| 64 | + |
| 65 | +void RedShiftQt::onRedshiftStateChanged(QProcess::ProcessState state) |
| 66 | +{ |
| 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()); |
| 90 | +} |
| 91 | + |
| 92 | +void RedShiftQt::onReadyRead() |
| 93 | +{ |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +void RedShiftQt::onConfChanged() |
| 98 | +{ |
| 99 | + int state = redshift->state(); |
| 100 | + if(state) { |
| 101 | + redshift->kill(); |
| 102 | + redshift->waitForFinished(); |
| 103 | + } |
| 104 | + redshift->setProgram(conf->value("redshift_path").toString()); |
| 105 | + redshift->setArguments(getArguments()); |
| 106 | + helper->setProgram(conf->value("redshift_path").toString()); |
| 107 | + if(state) { |
| 108 | + redshift->start(QProcess::ReadOnly); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void RedShiftQt::toggle(void) |
| 113 | +{ |
| 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 | + } |
| 125 | +} |
| 126 | + |
| 127 | +void RedShiftQt::suspend(QAction* action) |
| 128 | +{ |
| 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)); |
| 136 | +} |
| 137 | + |
| 138 | +void RedShiftQt::activated(QSystemTrayIcon::ActivationReason reason) |
| 139 | +{ |
| 140 | + if(reason == QSystemTrayIcon::Trigger) |
| 141 | + toggle(); |
| 142 | +} |
| 143 | + |
| 144 | +void RedShiftQt::showPrefs() |
| 145 | +{ |
| 146 | + prefs->setVisible(true); |
| 147 | + prefs->raise(); |
| 148 | +} |
| 149 | + |
| 150 | +void RedShiftQt::showLog() |
| 151 | +{ |
| 152 | + log->setVisible(true); |
| 153 | + log->raise(); |
| 154 | +} |
| 155 | + |
| 156 | +QStringList RedShiftQt::getArguments() |
| 157 | +{ |
| 158 | + QStringList args; |
| 159 | + if(conf->value("use_own_conf").toBool()) { |
| 160 | + |
| 161 | + conf->beginGroup("color-temp"); |
| 162 | + QString method(conf->value("method").toString()); |
| 163 | + if(method != "default") { |
| 164 | + args.append("-m"); |
| 165 | + args.append(method); |
| 166 | + } |
| 167 | + args.append("-t"); |
| 168 | + args.append(QString("%1:%2").arg( |
| 169 | + conf->value("day").toString(), |
| 170 | + conf->value("night").toString())); |
| 171 | + if(!conf->value("transition").toBool()) args.append("-r"); |
| 172 | + conf->endGroup(); |
| 173 | + |
| 174 | + conf->beginGroup("location"); |
| 175 | + QString provider(conf->value("provider").toString()); |
| 176 | + if(provider != "default") { |
| 177 | + args.append("-l"); |
| 178 | + if(provider == "manual") { |
| 179 | + args.append(QString("%1:%2").arg( |
| 180 | + conf->value("latitude").toString(), |
| 181 | + conf->value("longitude").toString())); |
| 182 | + } else { |
| 183 | + args.append(provider); |
| 184 | + } |
| 185 | + } |
| 186 | + conf->endGroup(); |
| 187 | + |
| 188 | + conf->beginGroup("brightness"); |
| 189 | + if(conf->value("adj-type").toInt() > 0) { |
| 190 | + args.append("-b"); |
| 191 | + if(conf->value("adj-type").toInt() > 1) { |
| 192 | + args.append(QString("%1:%2").arg( |
| 193 | + conf->value("day").toString(), |
| 194 | + conf->value("night").toString())); |
| 195 | + } else { |
| 196 | + args.append(conf->value("day").toString()); |
| 197 | + } |
| 198 | + } |
| 199 | + conf->endGroup(); |
| 200 | + |
| 201 | + conf->beginGroup("gamma"); |
| 202 | + if(conf->value("adj-type").toInt() > 0) { |
| 203 | + args.append("-g"); |
| 204 | + if(conf->value("adj-type").toInt() > 1) { |
| 205 | + args.append(conf->value("day").toString()); |
| 206 | + } else { |
| 207 | + args.append(conf->value("day").toString()); |
| 208 | + } |
| 209 | + } |
| 210 | + conf->endGroup(); |
| 211 | + |
| 212 | + } else { |
| 213 | + args.append("-c"); |
| 214 | + args.append(conf->value("config_path").toString()); |
| 215 | + } |
| 216 | + return args; |
| 217 | +} |
0 commit comments