Make the settings dialog aware of the presence of kernel-config
[qcpufreq] / src / settings.cpp
1 /*
2  * QCPUFreq - a simple cpufreq GUI
3  * Copyright (C) 2010 Daniel Klaffenbach <danielklaffenbach@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "settings.h"
20 #include "ui_settings.h"
21
22 #include <QFile>
23 #include <QMessageBox>
24 #include <QStringList>
25 #include <QTextStream>
26 #if defined(Q_WS_MAEMO_5)
27     #include <QMaemo5InformationBox>
28 #endif
29
30 #define DEFAULT_FREQUENCY 600000
31
32 Settings::Settings(QWidget *parent) :
33     QDialog(parent),
34     settings("qcpufreq"),
35     ui(new Ui::Settings)
36 {
37     ui->setupUi(this);
38
39     //read values from config file
40     autoRotate = settings.value("main/autorotate", true).toBool();
41     overclocking = settings.value("main/overclocking", false).toBool();
42     advancedTemperature = settings.value("main/advanced_temperature", false).toBool();
43
44     /* The next few lines of code check if QCPUFreq is running on a power kernel and if
45      * the kernel-config script is installed.
46      *
47      * Basically we get the maximum available frequency - if it is greater than
48      * DEFAULT_FREQUENCY we can be sure that the current kernel is a power kernel.
49      */
50     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
51
52     powerKernel = false;
53     kernelConfigInstalled = false;
54
55     //open the file
56     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
57         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
58     } else {
59         //read the file
60         QTextStream in( &file );
61         QStringList freqList = in.readLine().split(" ");
62         int maxFreq = freqList.at(0).toInt();
63
64         //close the file
65         file.close();
66
67         if (maxFreq > DEFAULT_FREQUENCY) {
68             powerKernel = true;
69         }
70
71         //Check if kernel-config is installed
72         file.setFileName("/usr/sbin/kernel-config");
73         if (file.exists()) {
74             kernelConfigInstalled = true;
75         }
76     }
77
78     //on non-power-kernels we can disable the overclocking and temperature options
79     if (!powerKernel) {
80         ui->checkBox_oc->setDisabled(true);
81         ui->checkBox_temperature->setDisabled(true);
82     }
83
84     //reset GUI
85     reset();
86
87     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));
88     connect(ui->checkBox_oc, SIGNAL(clicked()), this, SLOT(showOverclockingWarning()));
89 }
90
91 Settings::~Settings()
92 {
93     delete ui;
94 }
95
96
97 /**
98   * Returns True if the kernel-config script is installed.
99   */
100 bool Settings::isKernelConfigInstalled()
101 {
102     return kernelConfigInstalled;
103 }
104
105
106 /**
107   * Returns true if we are on a Maemo 5 OS.
108   */
109 bool Settings::platformMaemo()
110 {
111     #if defined(Q_WS_MAEMO_5)
112         return true;
113     #else
114         return false;
115     #endif
116 }
117
118
119 /**
120   * Resets the UI to the values stored by QSettings.
121   */
122 void Settings::reset()
123 {
124     ui->checkBox_rotate->setChecked(autoRotate);
125     ui->checkBox_oc->setChecked(overclocking);
126     ui->checkBox_temperature->setChecked(advancedTemperature);
127 }
128
129
130 /**
131   * Saves the changes and hides the SettingsWidget.
132   *
133   * @emits: settingsChanged()
134   */
135 void Settings::save()
136 {
137     autoRotate = ui->checkBox_rotate->isChecked();
138     overclocking = ui->checkBox_oc->isChecked();
139     advancedTemperature = ui->checkBox_temperature->isChecked();
140
141     settings.setValue("main/autorotate", autoRotate);
142     settings.setValue("main/overclocking", overclocking);
143     settings.setValue("main/advanced_temperature", advancedTemperature);
144
145     //save settings to file
146     settings.sync();
147
148     hide();
149
150     emit settingsChanged();
151 }
152
153
154 /**
155   * Displays a warning when overclocking is being enabled.
156   */
157 void Settings::showOverclockingWarning()
158 {
159     if (ui->checkBox_oc->isChecked()) {
160 #if defined(Q_WS_MAEMO_5)
161         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
162 #else
163         QMessageBox::warning(this, tr("Warning"), tr("Please note that overclocking voids your warranty and may break your device! Be careful!"));
164 #endif
165     }
166 }
167
168
169 /**
170   * Returns true if the user wants to display a more
171   * accurate temperature.
172   */
173 bool Settings::useAdvancedTemperature()
174 {
175     return advancedTemperature;
176 }
177
178
179 /**
180   * Returns true if auto-Rotate is enabled.
181   */
182 bool Settings::useAutoRotate()
183 {
184     return autoRotate;
185 }
186
187
188 /**
189   * Returns true if the user enabled overclocking.
190   */
191 bool Settings::useOverclocking()
192 {
193     return overclocking;
194 }
195
196
197 /**
198   * Returns true if QCPUFreq is running on a power kernel.
199   */
200 bool Settings::usePowerKernel()
201 {
202     return powerKernel;
203 }