Apply auto-rotate setting without restarting QCPUFreq
[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   * @emits: settingsChanged()
117   */
118 void Settings::save()
119 {
120     autoRotate = ui->checkBox_rotate->isChecked();
121     overclocking = ui->checkBox_oc->isChecked();
122     advancedTemperature = ui->checkBox_temperature->isChecked();
123
124     settings.setValue("main/autorotate", autoRotate);
125     settings.setValue("main/overclocking", overclocking);
126     settings.setValue("main/advanced_temperature", advancedTemperature);
127
128     //save settings to file
129     settings.sync();
130
131     hide();
132
133     emit settingsChanged();
134 }
135
136
137 /**
138   * Displays a warning when overclocking is being enabled.
139   */
140 void Settings::showOverclockingWarning()
141 {
142     if (ui->checkBox_oc->isChecked()) {
143 #if defined(Q_WS_MAEMO_5)
144         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
145 #else
146         QMessageBox::warning(this, tr("Warning"), tr("Please note that overclocking voids your warranty and may break your device! Be careful!"));
147 #endif
148     }
149 }
150
151
152 /**
153   * Returns true if the user wants to display a more
154   * accurate temperature.
155   */
156 bool Settings::useAdvancedTemperature()
157 {
158     return advancedTemperature;
159 }
160
161
162 /**
163   * Returns true if auto-Rotate is enabled.
164   */
165 bool Settings::useAutoRotate()
166 {
167     return autoRotate;
168 }
169
170
171 /**
172   * Returns true if the user enabled overclocking.
173   */
174 bool Settings::useOverclocking()
175 {
176     return overclocking;
177 }
178
179
180 /**
181   * Returns true if QCPUFreq is running on a power kernel.
182   */
183 bool Settings::usePowerKernel()
184 {
185     return powerKernel;
186 }