a0b78f7aeba3539aed9a833792fca3f323d813ba
[qcpufreq] / src / settings.cpp
1 #include "settings.h"
2 #include "ui_settings.h"
3
4 #include <QFile>
5 #include <QMessageBox>
6 #include <QStringList>
7 #include <QTextStream>
8 #if defined(Q_WS_MAEMO_5)
9     #include <QMaemo5InformationBox>
10 #endif
11
12 #define DEFAULT_FREQUENCY 600000
13
14 Settings::Settings(QWidget *parent) :
15     QDialog(parent),
16     settings("qcpufreq"),
17     ui(new Ui::Settings)
18 {
19     ui->setupUi(this);
20
21     //read values from config file
22     autoRotate = settings.value("main/autorotate", true).toBool();
23     overclocking = settings.value("main/overclocking", false).toBool();
24     advancedTemperature = settings.value("main/advanced_temperature", false).toBool();
25
26     /* The next few lines of code check if QCPUFreq is running on a power kernel.
27      * Basically we get the maximum available frequency - if it is greater than
28      * DEFAULT_FREQUENCY we can be sure that the current kernel is a power kernel.
29      */
30     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
31
32     //open the file
33     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
34         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
35         powerKernel = false;
36     } else {
37         //read the file
38         QTextStream in( &file );
39         QStringList freqList = in.readLine().split(" ");
40         int maxFreq = freqList.at(0).toInt();
41
42         //close the file
43         file.close();
44
45         if (maxFreq > DEFAULT_FREQUENCY) {
46             powerKernel = true;
47         } else {
48             powerKernel = false;
49         }
50     }
51
52     //on non-power-kernels we can disable the overclocking and temperature options
53     if (!powerKernel) {
54         ui->checkBox_oc->setDisabled(true);
55         ui->checkBox_temperature->setDisabled(true);
56     }
57
58     //reset GUI
59     reset();
60
61     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));
62     connect(ui->checkBox_oc, SIGNAL(clicked()), this, SLOT(showOverclockingWarning()));
63 }
64
65 Settings::~Settings()
66 {
67     delete ui;
68 }
69
70
71 /**
72   * Returns true if we are on a Maemo 5 OS.
73   */
74 bool Settings::platformMaemo()
75 {
76     #if defined(Q_WS_MAEMO_5)
77         return true;
78     #else
79         return false;
80     #endif
81 }
82
83
84 /**
85   * Resets the UI to the values stored by QSettings.
86   */
87 void Settings::reset()
88 {
89     ui->checkBox_rotate->setChecked(autoRotate);
90     ui->checkBox_oc->setChecked(overclocking);
91     ui->checkBox_temperature->setChecked(advancedTemperature);
92 }
93
94
95 /**
96   * Saves the changes and hides the SettingsWidget.
97   */
98 void Settings::save()
99 {
100     autoRotate = ui->checkBox_rotate->isChecked();
101     overclocking = ui->checkBox_oc->isChecked();
102     advancedTemperature = ui->checkBox_temperature->isChecked();
103
104     settings.setValue("main/autorotate", autoRotate);
105     settings.setValue("main/overclocking", overclocking);
106     settings.setValue("main/advanced_temperature", advancedTemperature);
107
108     //save settings to file
109     settings.sync();
110
111     hide();
112 }
113
114
115 /**
116   * Displays a warning when overclocking is being enabled.
117   */
118 void Settings::showOverclockingWarning()
119 {
120     if (ui->checkBox_oc->isChecked()) {
121 #if defined(Q_WS_MAEMO_5)
122         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
123 #else
124         QMessageBox::warning(this, tr("Warning"), tr("Please note that overclocking voids your warranty and may break your device! Be careful!"));
125 #endif
126     }
127 }
128
129
130 /**
131   * Returns true if the user wants to display a more
132   * accurate temperature.
133   */
134 bool Settings::useAdvancedTemperature()
135 {
136     return advancedTemperature;
137 }
138
139
140 /**
141   * Returns true if auto-Rotate is enabled.
142   */
143 bool Settings::useAutoRotate()
144 {
145     return autoRotate;
146 }
147
148
149 /**
150   * Returns true if the user enabled overclocking.
151   */
152 bool Settings::useOverclocking()
153 {
154     return overclocking;
155 }
156
157
158 /**
159   * Returns true if QCPUFreq is running on a power kernel.
160   */
161 bool Settings::usePowerKernel()
162 {
163     return powerKernel;
164 }