New release: 0.4.4
[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     confirmApply = settings.value("main/confirm_apply", true).toBool();
42     overclocking = settings.value("main/overclocking", false).toBool();
43     advancedTemperature = settings.value("main/advanced_temperature", false).toBool();
44
45     /* The next few lines of code check if QCPUFreq is running on a power kernel and if
46      * the kernel-config script is installed.
47      *
48      * Basically we get the maximum available frequency - if it is greater than
49      * DEFAULT_FREQUENCY we can be sure that the current kernel is a power kernel.
50      */
51     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
52
53     powerKernel = false;
54     kernelConfigInstalled = false;
55
56     //open the file
57     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
58         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
59     } else {
60         //read the file
61         QTextStream in( &file );
62         QStringList freqList = in.readLine().split(" ");
63         int maxFreq = freqList.at(0).toInt();
64
65         //close the file
66         file.close();
67
68         if (maxFreq > DEFAULT_FREQUENCY) {
69             powerKernel = true;
70         }
71
72         //Check if kernel-config is installed
73         file.setFileName("/usr/sbin/kernel-config");
74         if (file.exists()) {
75             kernelConfigInstalled = true;
76         }
77     }
78
79     //on non-power-kernels we can disable the overclocking and temperature options
80     if (!powerKernel) {
81         ui->checkBox_oc->setDisabled(true);
82         ui->checkBox_temperature->setDisabled(true);
83     }
84
85     //reset GUI
86     reset();
87
88     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));
89     connect(ui->checkBox_oc, SIGNAL(clicked()), this, SLOT(showOverclockingWarning()));
90 }
91
92 Settings::~Settings()
93 {
94     delete ui;
95 }
96
97
98 /**
99   * Returns True if the kernel-config script is installed.
100   */
101 bool Settings::isKernelConfigInstalled()
102 {
103     return kernelConfigInstalled;
104 }
105
106
107 /**
108   * Returns true if we are on a Maemo 5 OS.
109   */
110 bool Settings::platformMaemo()
111 {
112     #if defined(Q_WS_MAEMO_5)
113         return true;
114     #else
115         return false;
116     #endif
117 }
118
119
120 /**
121   * Resets the UI to the values stored by QSettings.
122   */
123 void Settings::reset()
124 {
125     ui->checkBox_rotate->setChecked(autoRotate);
126     ui->checkBox_confirmapply->setChecked(confirmApply);
127     ui->checkBox_oc->setChecked(overclocking);
128     ui->checkBox_temperature->setChecked(advancedTemperature);
129 }
130
131
132 /**
133   * Saves the changes and hides the SettingsWidget.
134   *
135   * @emits: settingsChanged()
136   */
137 void Settings::save()
138 {
139     autoRotate = ui->checkBox_rotate->isChecked();
140     confirmApply = ui->checkBox_confirmapply->isChecked();
141     overclocking = ui->checkBox_oc->isChecked();
142     advancedTemperature = ui->checkBox_temperature->isChecked();
143
144     settings.setValue("main/autorotate", autoRotate);
145     settings.setValue("main/confirm_apply", confirmApply);
146     settings.setValue("main/overclocking", overclocking);
147     settings.setValue("main/advanced_temperature", advancedTemperature);
148
149     //save settings to file
150     settings.sync();
151
152     hide();
153
154     emit settingsChanged();
155 }
156
157
158 /**
159   * Displays a warning when overclocking is being enabled.
160   */
161 void Settings::showOverclockingWarning()
162 {
163     if (ui->checkBox_oc->isChecked()) {
164 #if defined(Q_WS_MAEMO_5)
165         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
166 #else
167         QMessageBox::warning(this, tr("Warning"), tr("Please note that overclocking voids your warranty and may break your device! Be careful!"));
168 #endif
169     }
170 }
171
172
173 /**
174   * Returns true if the user wants to display a more
175   * accurate temperature.
176   */
177 bool Settings::useAdvancedTemperature()
178 {
179     return advancedTemperature;
180 }
181
182
183 /**
184   * Do we need to display a confirmation dialog before applying new
185   * frequencies?
186   */
187 bool Settings::useConfirmation()
188 {
189     return confirmApply;
190 }
191
192
193 /**
194   * Returns true if auto-Rotate is enabled.
195   */
196 bool Settings::useAutoRotate()
197 {
198     return autoRotate;
199 }
200
201
202 /**
203   * Returns true if the user enabled overclocking.
204   */
205 bool Settings::useOverclocking()
206 {
207     return overclocking;
208 }
209
210
211 /**
212   * Returns true if QCPUFreq is running on a power kernel.
213   */
214 bool Settings::usePowerKernel()
215 {
216     return powerKernel;
217 }