Skip to content

Commit

Permalink
Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rekols committed Sep 4, 2017
1 parent 630e6d9 commit cb7cd6c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 39 deletions.
4 changes: 2 additions & 2 deletions thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void Thread::run()
Utils::getCpuTime(currentWorkTime, currentTotalTime);
Utils::getNetworkBandWidth(recv, send);

emit updateNetworkTotal(Utils::networkConversion(send), Utils::networkConversion(recv));
emit updateNetworkSpeed("" + Utils::networkConversion(send - prevSend), "" + Utils::networkConversion(recv - prevRecv));
emit updateNetworkTotal(Utils::formatBytes(send), Utils::formatBytes(recv));
emit updateNetworkSpeed("" + Utils::formatBytes(send - prevSend), "" + Utils::formatBytes(recv - prevRecv));
emit updateCpuPercent((currentWorkTime - prevWorkTime) * 100.0 / (currentTotalTime - prevTotalTime));
}
}
27 changes: 1 addition & 26 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void Utils::getNetworkBandWidth(unsigned long long &receiveBytes, unsigned long
sendBytes = data.at(9).toLong();
}

QString Utils::networkConversion(long bytes)
QString Utils::formatBytes(long bytes)
{
if (bytes < 1024.0)
return QString::number(bytes, 'r', 1) + " B";
Expand Down Expand Up @@ -208,31 +208,6 @@ quint64 Utils::getFileSize(const QString &path)
return totalSize;
}

QString Utils::formatBytes(const quint64 &bytes)
{
#define formatUnit(v, u, t) QString().sprintf("%.1f %s", \
((double) v / (double) u), t)

if (bytes == 1L) // bytes
return QString("%1 byte").arg(bytes);
else if (bytes < KIBI) // bytes
return QString("%1 bytes").arg(bytes);
else if (bytes < MEBI) // KiB
return formatUnit(bytes, KIBI, "KB");
else if (bytes < GIBI) // MiB
return formatUnit(bytes, MEBI, "MB");
else if (bytes < TEBI) // GiB
return formatUnit(bytes, GIBI, "GB");
else if (bytes < PEBI) // TiB
return formatUnit(bytes, TEBI, "TB");
else if (bytes < EXBI) // PiB
return formatUnit(bytes, PEBI, "PB");
else // EiB
return formatUnit(bytes, EXBI, "EB");

#undef formatUnit
}

QFileInfoList Utils::getDpkgPackages()
{
QDir reports("/var/cache/apt/archives");
Expand Down
12 changes: 1 addition & 11 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,15 @@ class Utils
static void getMemoryInfo(QString &memory, float &percent);
static void getDiskInfo(QString &disk, float &percent);
static void getNetworkBandWidth(unsigned long long &receiveBytes, unsigned long long &sendBytes);
static QString networkConversion(long bytes);

static QString formatBytes(long bytes);
static quint64 getFileSize(const QString &path);
static QString formatBytes(const quint64 &bytes);

static QFileInfoList getDpkgPackages();
static QFileInfoList getCrashReports();
static QFileInfoList getAppLogs();
static QFileInfoList getAppCaches();
static QString getHomePath();
static QString sudoExec(const QString &cmd, QStringList args);

private:
static const quint64 KIBI = 1L << 10;
static const quint64 MEBI = 1L << 20;
static const quint64 GIBI = 1L << 30;
static const quint64 TEBI = 1L << 40;
static const quint64 PEBI = 1L << 50;
static const quint64 EXBI = 1L << 60;
};

#endif // UTILS_H

0 comments on commit cb7cd6c

Please sign in to comment.