Merged helper scripts
[qcpufreq] / src / mainwindow.cpp
index f196a16..afffeef 100755 (executable)
@@ -30,7 +30,7 @@
 
 
 #define APPNAME "QCPUFreq"
-#define APPVERSION "0.1"
+#define APPVERSION "0.2"
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
@@ -80,24 +80,44 @@ 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()
@@ -317,22 +337,15 @@ void MainWindow::setAutoRotaion()
   */
 void MainWindow::setSmartReflex()
 {
-//SmartReflex is only supprted on Maemo5
+//SmartReflex is only supported on Maemo5
 #if defined(Q_WS_MAEMO_5)
-    QStringList arguments;
-    arguments.append( "/opt/usr/bin/set_sr" );
-
     if ( getSmartReflexState() == 1 )
-       arguments.append( "off" );
+       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);
-       arguments.append( "on" );
+       callHelper( "set_sr", "on");
     }
 
-    //execute the sr script script
-    QProcess script;
-    script.execute( "sudo", arguments );
-
 #endif
     //refresh the UI
     refresh();