3762fd9b8a42995f55b0623e3a56d2352eab7feb
[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.
45      * Basically we get the maximum available frequency - if it is greater than
46      * DEFAULT_FREQUENCY we can be sure that the current kernel is a power kernel.
47      */
48     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
49
50     //open the file
51     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
52         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
53         powerKernel = false;
54     } else {
55         //read the file
56         QTextStream in( &file );
57         QStringList freqList = in.readLine().split(" ");
58         int maxFreq = freqList.at(0).toInt();
59
60         //close the file
61         file.close();
62
63         if (maxFreq > DEFAULT_FREQUENCY) {
64             powerKernel = true;
65         } else {
66             powerKernel = false;
67         }
68     }
69
70     //on non-power-kernels we can disable the overclocking and temperature options
71     if (!powerKernel) {
72         ui->checkBox_oc->setDisabled(true);
73         ui->checkBox_temperature->setDisabled(true);
74     }
75
76     //reset GUI
77     reset();
78
79     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));
80     connect(ui->checkBox_oc, SIGNAL(clicked()), this, SLOT(showOverclockingWarning()));
81 }
82
83 Settings::~Settings()
84 {
85     delete ui;
86 }
87
88
89 /**
90   * Returns true if we are on a Maemo 5 OS.
91   */
92 bool Settings::platformMaemo()
93 {
94     #if defined(Q_WS_MAEMO_5)
95         return true;
96     #else
97         return false;
98     #endif
99 }
100
101
102 /**
103   * Resets the UI to the values stored by QSettings.
104   */
105 void Settings::reset()
106 {
107     ui->checkBox_rotate->setChecked(autoRotate);
108     ui->checkBox_oc->setChecked(overclocking);
109     ui->checkBox_temperature->setChecked(advancedTemperature);
110 }
111
112
113 /**
114   * Saves the changes and hides the SettingsWidget.
115   */
116 void Settings::save()
117 {
118     autoRotate = ui->checkBox_rotate->isChecked();
119     overclocking = ui->checkBox_oc->isChecked();
120     advancedTemperature = ui->checkBox_temperature->isChecked();
121
122     settings.setValue("main/autorotate", autoRotate);
123     settings.setValue("main/overclocking", overclocking);
124     settings.setValue("main/advanced_temperature", advancedTemperature);
125
126     //save settings to file
127     settings.sync();
128
129     hide();
130 }
131
132
133 /**
134   * Displays a warning when overclocking is being enabled.
135   */
136 void Settings::showOverclockingWarning()
137 {
138     if (ui->checkBox_oc->isChecked()) {
139 #if defined(Q_WS_MAEMO_5)
140         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
141 #else
142         QMessageBox::warning(this, tr("Warning"), tr("Please note that overclocking voids your warranty and may break your device! Be careful!"));
143 #endif
144     }
145 }
146
147
148 /**
149   * Returns true if the user wants to display a more
150   * accurate temperature.
151   */
152 bool Settings::useAdvancedTemperature()
153 {
154     return advancedTemperature;
155 }
156
157
158 /**
159   * Returns true if auto-Rotate is enabled.
160   */
161 bool Settings::useAutoRotate()
162 {
163     return autoRotate;
164 }
165
166
167 /**
168   * Returns true if the user enabled overclocking.
169   */
170 bool Settings::useOverclocking()
171 {
172     return overclocking;
173 }
174
175
176 /**
177   * Returns true if QCPUFreq is running on a power kernel.
178   */
179 bool Settings::usePowerKernel()
180 {
181     return powerKernel;
182 }