Improve confirmation dialog
[qcpufreq] / src / mainwindow.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 0a9af8b..8cfd5ee
@@ -1,6 +1,6 @@
 /*
  * QCPUFreq - a simple cpufreq GUI
- * Copyright (C) 2010 Daniel Klaffenbach <daniel.klaffenbach@cs.tu-chemnitz.de>
+ * Copyright (C) 2010 Daniel Klaffenbach <danielklaffenbach@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <QFile>
 #include <QMessageBox>
 #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.4.2"
+
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
-    ui(new Ui::MainWindow)
+    ui(new Ui::MainWindow),
+    //create helper process
+    helperProcess( this ),
+    //create a new, stackable help window
+    helpWindow( this ),
+    //create UI refresh timer
+    refreshTimer( this ),
+    //create a QGraphicsScene for the little chip icon
+    scene( this )
 {
+    //this is a stacked window on Maemo 5
+    #if defined(Q_WS_MAEMO_5)
+        setAttribute(Qt::WA_Maemo5StackedWindow);
+    #endif
+
     ui->setupUi(this);
-    refresh();
 
-    // enable auto rotation
-    setAutoRotaion();
+    //Settings widget
+    settings = new Settings;
+    settings->hide();
 
-    //create a QGraphicsScene for the little chip icon
-    scene = new QGraphicsScene();
+    //load preset dialog
+    loadPresetDialog = new LoadPreset;
+    loadPresetDialog->hide();
+
+    init();
+
+    //applies the settings from the settings dialog
+    applySettings();
+
+    //initialize orientation
     orientationChanged();
 
+    //refresh UI every 10 seconds
+    refreshTimer.start( 10000 );
+
+    // initialize stackable help window
+    #if defined(Q_WS_MAEMO_5)
+        helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
+    #endif
+    helpWindow.setWindowFlags( windowFlags() | Qt::Window );
+
+    //show errors about the sudo setup only once
+    showSudoError = true;
+
     //connect signals and slots
+    connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
     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()) );
+    connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
+    connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
+    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
+    connect(ui->actionLoad, SIGNAL(triggered()), loadPresetDialog, SLOT(show()));
+    connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
+    connect(settings, SIGNAL(settingsChanged()), this, SLOT(applySettings()));
+    connect(loadPresetDialog, SIGNAL(load(QString)), this, SLOT(loadPreset(QString)));
 
 }
 
 MainWindow::~MainWindow()
 {
+    delete loadPresetDialog;
+    delete settings;
     delete ui;
-    delete scene;
 }
 
 
@@ -64,6 +107,7 @@ MainWindow::~MainWindow()
 void MainWindow::about()
 {
     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
+    refresh();
 }
 
 
@@ -73,34 +117,142 @@ void MainWindow::about()
 void MainWindow::adjustFreq()
 {
     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
+
+    if (newmax == getMaxFreq() ) {
+        //we do not need to change anything in this case
+        return;
+    }
+
     QString max;
+
+    //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
+    if (newmax < getMinFreq())
+        newmax = getMinFreq();
+
     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 );
+    //check for overclocking
+    #if defined(Q_WS_MAEMO_5)
+    if (!settings->useOverclocking() && newmax > 600000) {
+        QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
+        refresh();
+        return;
+    }
+    #endif
+
+    //check for 599MHz <-> 600MHz problem on power kernels
+    if (max == "600000" && settings->usePowerKernel()) {
+        //we really need to set the maximum to 599MHz
+        max = "599000";
+    }
+
+    if (settings->useConfirmation()) {
+        QMessageBox box;
+        box.setStandardButtons(QMessageBox::Apply | QMessageBox::Cancel);
+        box.setDefaultButton(QMessageBox::Apply);
+        box.setIcon(QMessageBox::Question);
+        box.setText(tr("You have requested to change the maximum frequency."));
+        QString verboseMax;
+        verboseMax.setNum( newmax/1000 );
+        box.setInformativeText( tr("Do you really want to use %1 MHz as the new maximum frequency?").arg(verboseMax) );
+        int ret = box.exec();
+
+        if (ret != QMessageBox::Apply) {
+            refresh();
+            return;
+        }
+    }
+
+    callHelper( "set_maxfreq", max );
+    refresh();
+
+}
+
+
+/**
+  * SLOT: applies the settings from the Settings dialog.
+  */
+void MainWindow::applySettings()
+{
+    setAutoRotation();
+    setAdvancedTemperature();
 
+    //if overclocking is/was enabled we can also enable the "Load preset" option
+    if (settings->useOverclocking() && settings->usePowerKernel() && settings->isKernelConfigInstalled()) {
+        ui->actionLoad->setEnabled(true);
+    } else {
+        ui->actionLoad->setEnabled(false);
+    }
+
+    //refresh the GUI after applying the settings
     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)
+{
+    QStringList arguments;
+
+    #if defined(Q_WS_MAEMO_5)
+    //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
+    arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
+    #else
+    arguments.append( "QCPUFreq.helper" );
+    #endif
+
+    arguments.append( action );
+    arguments.append( param );
+
+    helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
+
+    if ( showSudoError && !helperProcess.waitForFinished( 2000 )) {
+        //do not show this error again
+        showSudoError = false;
+        QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
+    }
+
+    return helperProcess.exitCode();
+}
+
+
+/**
   * Returns the current CPU temperature
   */
 QString MainWindow::getCPUTemp()
 {
 #if defined(Q_WS_MAEMO_5)
-    return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
+    QFile file( "/sys/class/power_supply/bq27200-0/temp" );
+
+    //check if we can read a more accurate temperature (only for power kernel)
+    if (file.exists())
+        return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
+    else {
+        /*
+          We actually only need to read the raw temperature, but it appears that by also reading temp1_input
+          the raw temperature (temp1_input_raw) is being updated more frequently.
+        */
+        readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
+
+        //read the current system temperature
+        QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
+        if (tstring == "0")
+            return tr( "Unknown" );
+
+        //convert it to an integer and calculate the approx. temperature from the raw value
+        int tint = tstring.toInt();
+        tint = ( 0.65 * tint );
+        tstring.setNum(tint);
+        return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
+    }
 #endif
-    return "Unknown";
+    return tr( "Unknown" );
 }
 
 
@@ -119,8 +271,7 @@ int MainWindow::getMaxFreq()
   */
 int MainWindow::getMinFreq()
 {
-    QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
-    return tmp.toInt();
+    return this->minFreq;
 }
 
 
@@ -129,23 +280,20 @@ int MainWindow::getMinFreq()
   */
 int MainWindow::getScalingFreq(int step)
 {
-    QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
-    QStringList freqs = tmp.split( " " );
     step = step - 1;
     if ( step < 0 )
          step = 0;
-    if ( step > getScalingSteps() )
-        step = getScalingSteps();
+    if ( step > getScalingSteps() - 1 )
+        step = getScalingSteps() - 1;
 
-    tmp = freqs[ step ];
-    return tmp.toInt();
+    return this->scalingFrequencies[ step ].toInt();
 }
 
 
 /**
   * Returns the name of the current CPU frequency scaling governor
   *
-  * \return    name of governor
+  * @return    QString - name of governor
   */
 QString MainWindow::getScalingGovernor()
 {
@@ -155,26 +303,25 @@ QString MainWindow::getScalingGovernor()
 
 /**
   * Returns the amount of available scaling steps.
+  *
+  * @return int
   */
 int MainWindow::getScalingSteps()
 {
-    QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
-    QStringList freqs = tmp.split( " " );
-    return (freqs.size() - 1);
+    return this->scalingSteps;
 }
 
 
 /**
   * Returns the scaling step for the specified frequency.
+  *
+  * @return int
   */
 int MainWindow::getScalingStep( int freq )
 {
-    for( int i = 1; i <= getScalingSteps(); ++i ) {
-           if ( getScalingFreq(i) == freq )
-                return i;
-    }
-
-    return 1;
+    QString tmp;
+    tmp.setNum(freq);
+    return this->scalingFrequencies.indexOf(tmp) + 1;
 }
 
 
@@ -189,17 +336,91 @@ int MainWindow::getSmartReflexState()
 #if defined(Q_WS_MAEMO_5)
     QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
 
-    if ( tmp == "1" )
-       return 1;
-    else
-       return 0;
+    if ( tmp == "1" ) {
+        return 1;
+    } else {
+        return 0;
+    }
 #else
+    //disable UI checkbox
+    ui->sr_box->setDisabled( true );
+
     return 0;
 #endif
 }
 
 
 /**
+  * Initializes internal variables, such as:
+  *  - scalingSteps
+  *  - scalingFrequencies
+  *  - minFreq
+  */
+void MainWindow::init()
+{
+    this->minFreq = 0;
+    QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
+    QStringList freqList = freqs.split( " " );
+    //change the order of the QStringList - last element becomes first
+    for (int i=freqList.size() - 1; i>=0; --i) {
+        if (freqList.at(i) != "")
+            this->scalingFrequencies << freqList.at(i);
+    }
+    this->scalingSteps = (this->scalingFrequencies.size());
+
+    //set minFreq and check avoid_frequencies
+    QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
+    //check if avoid file exists (only on power kernel)
+    QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
+    if (file.exists()) {
+        QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
+        QStringList avoidList = avoid.split( " " );
+
+        //check if min is in avoid_frequencies
+        for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
+            min.setNum( getScalingFreq(i) );
+            if (!avoidList.contains(min)) {
+                this->minFreq = min.toInt();
+                break;
+            }
+        }
+    } else {
+        this->minFreq = min.toInt();
+    }
+    file.close();
+
+    //enable save and load option on power kernels
+    if (settings->usePowerKernel() && settings->isKernelConfigInstalled()) {
+        ui->actionSave->setEnabled(true);
+        //loading presets may cause overclocking - only enable it if overclokcing is enabled
+        if (settings->useOverclocking()) {
+            ui->actionLoad->setEnabled(true);
+        }
+    }
+}
+
+
+/**
+  * Loads a voltage preset by calling kernel-config.
+  *
+  * Available presets are:
+  *  - default
+  *  - ideal
+  *  - lv
+  *  - ulv
+  *  - xlv
+  *  - custom -> any preset named "custom"
+  */
+void MainWindow::loadPreset(QString presetName)
+{
+    #if defined(Q_WS_MAEMO_5)
+        callHelper("loadpreset", presetName);
+        QMaemo5InformationBox::information(this, tr( "The preset was loaded." ), QMaemo5InformationBox::DefaultTimeout);
+    #endif
+}
+
+
+/**
   * Reads any file in /sys/
   *
   * \param     sys_file : full path to sys file - omit "/sys/"
@@ -219,6 +440,9 @@ QString MainWindow::readSysFile(QString sys_file)
     QTextStream in( &file );
     QString txt = in.readLine();
 
+    //close the file
+    file.close();
+
     return txt;
 }
 
@@ -248,19 +472,15 @@ void MainWindow::refresh()
     ui->cpu_temp->setText( getCPUTemp() );
 
     //smart reflex button
-    if ( getSmartReflexState() == 1 ) {
-       ui->sr_btn->setDown( true );
-       ui->sr_btn->setText( tr( "Enabled" ) );
-    } else {
-       ui->sr_btn->setDown( false );
-       ui->sr_btn->setText( tr( "Disabled" ) );
-    }
+    if ( getSmartReflexState() == 1 )
+        ui->sr_box->setCheckState( Qt::Checked );
+    else
+        ui->sr_box->setCheckState( Qt::Unchecked );
 
 
     //display frequency slider
     ui->freq_adjust->setMinimum( 1 );
     ui->freq_adjust->setMaximum( getScalingSteps() );
-    ui->freq_adjust->setInvertedAppearance( true );
     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
 }
 
@@ -274,33 +494,60 @@ void MainWindow::orientationChanged()
 
     //check whether we are using portrait or landscape mode
     if ( usePortrait() ) {
-       //in portrait mode we want to display the large image
-       image.load( ":/img/chip256" );
-       this->scene->clear();
-       this->scene->addPixmap(  image  );
-
-       ui->graphicsPortrait->setScene( this->scene );
-       ui->graphicsPortrait->setMaximumSize( 256, 256 );
-       ui->graphicsLandscape->setMaximumSize( 0, 0 );
+        //in portrait mode we want to display the large image
+        image.load( ":/img/chip256" );
+        scene.clear();
+        scene.addPixmap(  image  );
+
+        ui->graphicsPortrait->setScene( &scene );
+        ui->graphicsPortrait->setMaximumSize( 256, 256 );
+        ui->graphicsLandscape->setMaximumSize( 0, 0 );
     } else {
-       image.load( ":/img/chip128" );
-       this->scene->clear();
-       this->scene->addPixmap(  image  );
+        image.load( ":/img/chip128" );
+        scene.clear();
+        scene.addPixmap(  image  );
+
+        ui->graphicsLandscape->setScene( &scene );
+        ui->graphicsLandscape->setMaximumSize( 128, 128 );
+        ui->graphicsPortrait->setMaximumSize( 0, 0 );
+    }
+}
+
 
-       ui->graphicsLandscape->setScene( this->scene );
-       ui->graphicsLandscape->setMaximumSize( 128, 128 );
-       ui->graphicsPortrait->setMaximumSize( 0, 0 );
+/**
+  * Saves the current maximim frequency as default (only on power kernel).
+  */
+void MainWindow::save()
+{
+    if (settings->usePowerKernel()) {
+        callHelper( "save", "null" );
+        #if defined(Q_WS_MAEMO_5)
+            QMaemo5InformationBox::information(this, tr( "The current frequency settings have been saved as default." ), QMaemo5InformationBox::DefaultTimeout);
+        #endif
     }
 }
 
 
 /**
-  * Enables the auto-rotation feature of Maemo5 devices
+  * Checks the settings if the "bq27x00_battery" needs to be loaded.
   */
-void MainWindow::setAutoRotaion()
+void MainWindow::setAdvancedTemperature()
+{
+    if (settings->usePowerKernel() && settings->useAdvancedTemperature()) {
+       callHelper( "load_bq27", "null" );
+    }
+}
+
+
+/**
+  * Enables or disables the auto-rotation feature of Maemo5 devices.
+  */
+void MainWindow::setAutoRotation()
 {
 #if defined(Q_WS_MAEMO_5)
-    setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+    setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
+    loadPresetDialog->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
+    settings->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
 #endif
 }
 
@@ -310,19 +557,14 @@ 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" );
-    else
-       arguments.append( "on" );
-
-    //execute the sr script script
-    QProcess script;
-    script.execute( "sudo", arguments );
+        callHelper( "set_sr", "off");
+    else {
+        QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
+        callHelper( "set_sr", "on");
+    }
 
 #endif
     //refresh the UI
@@ -331,13 +573,32 @@ void MainWindow::setSmartReflex()
 
 
 /**
+  * SLOT: display the help window
+  */
+void MainWindow::showHelp()
+{
+    helpWindow.show();
+}
+
+
+/**
+  * SLOT: displays the settings widget
+  */
+void MainWindow::showSettings()
+{
+    settings->reset();
+    settings->show();
+}
+
+
+/**
   * Returns true when the device is in portrait mode
   */
 bool MainWindow::usePortrait()
 {
     QRect screenGeometry = QApplication::desktop()->screenGeometry();
     if (screenGeometry.width() > screenGeometry.height())
-       return false;
+        return false;
     else
-       return true;
+        return true;
 }