Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Commit

Permalink
Fix the mount tray disk space monitor: it should now work up to yotta…
Browse files Browse the repository at this point in the history
…byes of space, instead of just gigabytes.
  • Loading branch information
Ken Moore committed Aug 6, 2013
1 parent e5460e1 commit 6edfd8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src-qt4/pc-mounttray/fsDialog.cpp
Expand Up @@ -29,12 +29,12 @@ void FSDialog::generateUI(){
//Get the data for this item
QString name = fsList[i].section("::",0,0);
QString filesystem = fsList[i].section("::",1,1);
int totalK = fsList[i].section("::",2,2).toInt();
int usedK = fsList[i].section("::",3,3).toInt();
double totalK = fsList[i].section("::",2,2).toDouble();
double usedK = fsList[i].section("::",3,3).toDouble();
int percent = fsList[i].section("::",4,4).toInt();
//Create item for the data
QStringList text; //column text
text << name << filesystem.toUpper() << FSWatcher::intToDisplay(usedK)+"/"+FSWatcher::intToDisplay(totalK) << QString::number(percent)+"%";
text << name << filesystem.toUpper() << FSWatcher::doubleToDisplay(usedK)+"/"+FSWatcher::doubleToDisplay(totalK) << QString::number(percent)+"%";
QTreeWidgetItem *tmp = new QTreeWidgetItem(text);

//set the item background based upon urgency
Expand Down
38 changes: 24 additions & 14 deletions src-qt4/pc-mounttray/fsWatcher.cpp
Expand Up @@ -34,8 +34,8 @@ QStringList FSWatcher::getFSmountpoints(){
//second line contains the data
QString avail = tmp[1].section(" ",0,0,QString::SectionSkipEmpty);
QString used = tmp[1].section(" ",1,1,QString::SectionSkipEmpty);
int iUsed = displayToInt(used);
int iTotal = displayToInt(avail) + iUsed;
double iUsed = floor(displayToDouble(used));
double iTotal = floor(displayToDouble(avail)) + iUsed;
int percent = calculatePercentage(iUsed, iTotal);
//qDebug() << "Percent calc: tot:"<<iTotal<<"used"<<iUsed<<"percent"<<percent;
//format the output string and add it in
Expand All @@ -59,8 +59,8 @@ QStringList FSWatcher::getFSmountpoints(){
QString total = dfout[i].section(" ",2,2,QString::SectionSkipEmpty).simplified();
QString used = dfout[i].section(" ",3,3,QString::SectionSkipEmpty).simplified();
//Calculate the percent
int iUsed = displayToInt(used);
int iTotal = displayToInt(total);
double iUsed = displayToDouble(used);
double iTotal = displayToDouble(total);
int percent = calculatePercentage(iUsed, iTotal);
//qDebug() << "df Item:" << dfout[i];
//qDebug() << " - Detected:" << name << fs << iTotal << iUsed << percent;
Expand All @@ -75,28 +75,38 @@ QStringList FSWatcher::getFSmountpoints(){

}

int FSWatcher::displayToInt(QString entry){
int FSWatcher::displayToDouble(QString entry){
//split the number from the size label
//qDebug() << "Display to Int conversion:" << entry;
QString units = entry.right(1); //last character
entry.chop(1); //remove the unit
double num = entry.toDouble();
//qDebug() << "initial number:" << num << "units:" << units;
if(units=="K"){} //Kilobytes (no change)
else if(units=="M"){ num=num*1024; } //Megabytes to K
else if(units=="G"){ num=num*1048576; } //Gigabytes to K
else{ num=0; } //smaller than a KB
QStringList unitL; unitL << "K" << "M" << "G" << "T" << "P" << "E" << "Z" << "Y";
bool ok = false;
for(int i=0; i< unitL.length(); i++){
if(units == unitL[i]){ num = num*pow(1024.0,i); ok = true; break;}
}
if(!ok){num=0; }
//qDebug() << "number:" << num;
return num;
}

QString FSWatcher::intToDisplay(int K){
QString FSWatcher::doubleToDisplay(double K){
QString num;
//qDebug() << "Int to Display:" << K;
double kdb = K; //using pure integers causes errors with large numbers
if( K > 1048576 ){ num = QString::number( int((kdb*100)/1048576)/100.0 ) +"G"; }
else if(K > 1024){ num = QString::number( int((kdb*100)/1024)/100.0 ) +"M"; }
else{ num = QString::number(K) +"K"; }
QStringList units; units << "K" << "M" << "G" << "T" << "P" << "E" << "Z" << "Y";
int i=0;
while( (kdb > 1000) && (i < 8) ){
kdb = kdb/1024;
i++;
}
if(i<8){
num = QString::number( int((kdb*100))/100.0) + units[i];
}else{
num = "??";
}
//qDebug() << "Display:" << num;
return num;

Expand Down Expand Up @@ -133,7 +143,7 @@ void FSWatcher::checkFS(){
}

//===== Calculate Percentages =====
int FSWatcher::calculatePercentage(int used, int total){
int FSWatcher::calculatePercentage(double used, double total){
double U = used;
double T = total;
double result = (U/T)*100.0;
Expand Down
7 changes: 4 additions & 3 deletions src-qt4/pc-mounttray/fsWatcher.h
Expand Up @@ -11,6 +11,7 @@
#include <QDebug>
#include <QTime>

#include <math.h>

class FSWatcher : public QObject
{
Expand All @@ -24,15 +25,15 @@ class FSWatcher : public QObject
void stop();

static QStringList getFSmountpoints();
static QString intToDisplay(int);
static int displayToInt(QString);
static QString doubleToDisplay(double);
static int displayToDouble(QString);

private:
QTimer *timer;
QStringList oldBadDevs;

static QStringList runCMD(QString);
static int calculatePercentage(int,int);
static int calculatePercentage(double,double);

public slots:
void checkFS(); //function in a timer loop
Expand Down

0 comments on commit 6edfd8a

Please sign in to comment.