Merged helper scripts
[qcpufreq] / src / mainwindow.cpp
index d36e648..afffeef 100755 (executable)
 #include <QTextStream>
 #include <QProcess>
 #include <QDesktopWidget>
+#if defined(Q_WS_MAEMO_5)
+    #include <QMaemo5InformationBox>
+#endif
 
 
 #define APPNAME "QCPUFreq"
-#define APPVERSION "0.1"
+#define APPVERSION "0.2"
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
     ui->setupUi(this);
+
     refresh();
 
     // enable auto rotation
@@ -47,6 +51,7 @@ MainWindow::MainWindow(QWidget *parent) :
     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
+    connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) );
 
 }
 
@@ -63,6 +68,7 @@ MainWindow::~MainWindow()
 void MainWindow::about()
 {
     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
+    refresh();
 }
 
 
@@ -74,24 +80,56 @@ void MainWindow::adjustFreq()
     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
     QString max;
     max.setNum( newmax );
-    QStringList arguments;
-#if defined(Q_WS_MAEMO_5)
-    //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
-    arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
-#else
-    arguments.append( "set_scalingmaxfreq" );
-#endif
-    arguments.append( max );
 
-    //execute the scaling script
-    QProcess script;
-    script.execute( "sudo", arguments );
+    callHelper( "set_maxfreq", max );
 
     refresh();
 }
 
 
 /**
+  * Calls the QCPUFreq helper script with "sudo action param"
+  *
+  * @param  action : the action of the helper script
+  * @param  param : the parameter for the action
+  * @return exit code
+  */
+int MainWindow::callHelper(QString action, QString param)
+{
+    QProcess helper;
+    QStringList arguments;
+    //run sudo in non-interactive mode
+    arguments.append( "-n" );
+
+    #if defined(Q_WS_MAEMO_5)
+       //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH
+       arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
+    #else
+       arguments.append( "QCPUFreq.helper" );
+    #endif
+
+    arguments.append( action );
+    arguments.append( param );
+
+    helper.execute( "sudo", arguments );
+
+    return helper.exitCode();
+}
+
+
+/**
+  * Returns the current CPU temperature
+  */
+QString MainWindow::getCPUTemp()
+{
+#if defined(Q_WS_MAEMO_5)
+    return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
+#endif
+    return tr( "Unknown" );
+}
+
+
+/**
   * Returns the maximum CPU frequency
   */
 int MainWindow::getMaxFreq()
@@ -231,6 +269,9 @@ void MainWindow::refresh()
     //display the current governor
     ui->freq_governor->setText( getScalingGovernor() );
 
+    //display current temperature
+    ui->cpu_temp->setText( getCPUTemp() );
+
     //smart reflex button
     if ( getSmartReflexState() == 1 ) {
        ui->sr_btn->setDown( true );
@@ -246,6 +287,8 @@ void MainWindow::refresh()
     ui->freq_adjust->setMaximum( getScalingSteps() );
     ui->freq_adjust->setInvertedAppearance( true );
     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
+
+    //ui->retranslateUi(this);
 }
 
 
@@ -290,6 +333,26 @@ void MainWindow::setAutoRotaion()
 
 
 /**
+  * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
+  */
+void MainWindow::setSmartReflex()
+{
+//SmartReflex is only supported on Maemo5
+#if defined(Q_WS_MAEMO_5)
+    if ( getSmartReflexState() == 1 )
+       callHelper( "set_sr", "off");
+    else {
+       QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
+       callHelper( "set_sr", "on");
+    }
+
+#endif
+    //refresh the UI
+    refresh();
+}
+
+
+/**
   * Returns true when the device is in portrait mode
   */
 bool MainWindow::usePortrait()