Add some disk usage information to profile selector

This commit is contained in:
Mark Watkins 2018-05-07 07:45:56 +10:00
parent ca962a88bb
commit 7ced9d50ae
7 changed files with 139 additions and 9 deletions

View File

@ -560,6 +560,37 @@ const QString Machine::getBackupPath()
return p_profile->Get("{" + STR_GEN_DataFolder + "}/" + info.loadername + "_" + (info.serial.isEmpty() ? hexid() : info.serial) + "/Backup/"); return p_profile->Get("{" + STR_GEN_DataFolder + "}/" + info.loadername + "_" + (info.serial.isEmpty() ? hexid() : info.serial) + "/Backup/");
} }
// dirSize lazily pinched from https://stackoverflow.com/questions/47854288/can-not-get-directory-size-in-qt-c, thank's "Mike"
qint64 dirSize(QString dirPath) {
qint64 size = 0;
QDir dir(dirPath);
QDir::Filters fileFilters = QDir::Files | QDir::System | QDir::Hidden;
for(QString filePath : dir.entryList(fileFilters)) {
QFileInfo fi(dir, filePath);
size += fi.size();
}
QDir::Filters dirFilters = QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden;
for(QString childDirPath : dir.entryList(dirFilters))
size += dirSize(dirPath + QDir::separator() + childDirPath);
return size;
}
qint64 Machine::diskSpaceSummaries()
{
return dirSize(getSummariesPath());
}
qint64 Machine::diskSpaceEvents()
{
return dirSize(getEventsPath());
}
qint64 Machine::diskSpaceBackups()
{
return dirSize(getBackupPath());
}
bool Machine::Load() bool Machine::Load()
{ {
QString path = getDataPath(); QString path = getDataPath();

View File

@ -1,4 +1,4 @@
/* SleepLib Machine Class Header /* SleepLib Machine Class Header
* *
* Copyright (C) 2011-2018 Mark Watkins <mark@jedimark.net> * Copyright (C) 2011-2018 Mark Watkins <mark@jedimark.net>
* *
@ -137,6 +137,11 @@ class Machine
const QString getSummariesPath(); const QString getSummariesPath();
const QString getBackupPath(); const QString getBackupPath();
qint64 diskSpaceSummaries();
qint64 diskSpaceEvents();
qint64 diskSpaceBackups();
//! \brief Returns the machineID as a lower case hexadecimal string //! \brief Returns the machineID as a lower case hexadecimal string
QString hexid() { return QString().sprintf("%08lx", m_id); } QString hexid() { return QString().sprintf("%08lx", m_id); }

View File

@ -307,6 +307,36 @@ bool Profile::StoreMachines()
return true; return true;
} }
qint64 Profile::diskSpaceSummaries()
{
qint64 size = 0;
for (auto & mach : m_machlist) {
size += mach->diskSpaceSummaries();
}
return size;
}
qint64 Profile::diskSpaceEvents()
{
qint64 size = 0;
for (auto & mach : m_machlist) {
size += mach->diskSpaceEvents();
}
return size;
}
qint64 Profile::diskSpaceBackups()
{
qint64 size = 0;
for (auto & mach : m_machlist) {
size += mach->diskSpaceBackups();
}
return size;
}
qint64 Profile::diskSpace()
{
return (diskSpaceSummaries()+diskSpaceEvents()+diskSpaceBackups());
}
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
class Environment class Environment

View File

@ -51,6 +51,11 @@ class Profile : public Preferences
bool OpenMachines(); bool OpenMachines();
bool StoreMachines(); bool StoreMachines();
qint64 diskSpaceSummaries();
qint64 diskSpaceEvents();
qint64 diskSpaceBackups();
qint64 diskSpace();
//! \brief Returns hostname that locked profile, or empty string if unlocked //! \brief Returns hostname that locked profile, or empty string if unlocked
QString checkLock(); QString checkLock();

View File

@ -51,7 +51,11 @@ ProfileSelector::ProfileSelector(QWidget *parent) :
model = nullptr; model = nullptr;
proxy = nullptr; proxy = nullptr;
showDiskUsage = false;
on_diskSpaceInfo_linkActivated(showDiskUsage ? "show" : "hide");
ui->versionLabel->setText(VersionString); ui->versionLabel->setText(VersionString);
ui->diskSpaceInfo->setVisible(false);
} }
ProfileSelector::~ProfileSelector() ProfileSelector::~ProfileSelector()
@ -80,7 +84,7 @@ void ProfileSelector::updateProfileList()
ui->profileView->setStyleSheet("QHeaderView::section { background-color:lightgrey }"); ui->profileView->setStyleSheet("QHeaderView::section { background-color:lightgrey }");
int row = 0; int row = 0;
int sel = -1; // int sel = -1;
QFontMetrics fm(ui->profileView->font()); QFontMetrics fm(ui->profileView->font());
@ -89,9 +93,9 @@ void ProfileSelector::updateProfileList()
Profile *prof = pi.value(); Profile *prof = pi.value();
name = pi.key(); name = pi.key();
if (AppSetting->profileName() == name) { // if (AppSetting->profileName() == name) {
sel = row; // sel = row;
} // }
Machine * mach = prof->GetMachine(MT_CPAP); // only interested in last cpap machine... Machine * mach = prof->GetMachine(MT_CPAP); // only interested in last cpap machine...
if (!mach) { if (!mach) {
@ -140,7 +144,6 @@ void ProfileSelector::updateProfileList()
proxy->setSourceModel(model); proxy->setSourceModel(model);
proxy->setSortCaseSensitivity(Qt::CaseInsensitive); proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
ui->profileView->setModel(proxy); ui->profileView->setModel(proxy);
ui->profileView->setSelectionBehavior(QAbstractItemView::SelectRows); ui->profileView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->profileView->setSelectionMode(QAbstractItemView::SingleSelection); ui->profileView->setSelectionMode(QAbstractItemView::SingleSelection);
@ -195,13 +198,15 @@ void ProfileSelector::updateProfileHighlight(QString name)
} }
if (html.isEmpty()) { if (html.isEmpty()) {
html += tr("No profile information given"); html += tr("No profile information given")+"<br/>";
} }
ui->diskSpaceInfo->setVisible(true);
ui->profileInfoGroupBox->setTitle(tr("Current Profile: %1").arg(name)); ui->profileInfoGroupBox->setTitle(tr("Current Profile: %1").arg(name));
ui->profileInfoLabel->setText(html); ui->profileInfoLabel->setText(html);
} else {
on_diskSpaceInfo_linkActivated(showDiskUsage ? "show" : "hide"); // don't show disk info by default
} else {
ui->diskSpaceInfo->setVisible(false);
} }
} }
@ -218,6 +223,7 @@ void ProfileSelector::SelectProfile(QString profname)
mainwin->OpenProfile(profname); mainwin->OpenProfile(profname);
updateProfileHighlight(profname); updateProfileHighlight(profname);
} }
} }
@ -388,4 +394,38 @@ void ProfileSelector::on_buttonDestroyProfile_clicked()
} }
} }
QString formatSize(qint64 size) {
QStringList units = { "Bytes", "KB", "MB", "GB", "TB", "PB" };
int i;
double outputSize = size;
for (i=0; i<units.size()-1; i++) {
if (outputSize < 1024) break;
outputSize = outputSize/1024;
}
return QString("%0 %1").arg(outputSize, 0, 'f', 2).arg(units[i]);
}
void ProfileSelector::on_diskSpaceInfo_linkActivated(const QString &link)
{
QString html;
if (link == "show") {
html += "<a href='hide'>"+tr("Hide disk usage information")+"</a>";
if (p_profile) {
qint64 sizeSummaries = p_profile->diskSpaceSummaries();
qint64 sizeEvents = p_profile->diskSpaceEvents();
qint64 sizeBackups = p_profile->diskSpaceBackups();
html += "<table>"
"<tr><td align=right>"+tr("Summaries:")+"</td><td>"+formatSize(sizeSummaries)+"</td></tr>"
"<tr><td align=right>"+tr("Events:")+"</td><td>"+formatSize(sizeEvents)+"</td></tr>"
"<tr><td align=right>"+tr("Backups:")+"</td><td>"+formatSize(sizeBackups)+"</td></tr></table>";
}
showDiskUsage = true;
} else {
html += "<a href='show'>"+tr("Show disk usage information")+"</a>";
showDiskUsage = false;
}
ui->diskSpaceInfo->setText(html);
}

View File

@ -51,11 +51,14 @@ private slots:
void on_buttonDestroyProfile_clicked(); void on_buttonDestroyProfile_clicked();
void on_diskSpaceInfo_linkActivated(const QString &link);
private: private:
Ui::ProfileSelector *ui; Ui::ProfileSelector *ui;
QStandardItemModel *model; QStandardItemModel *model;
MySortFilterProxyModel2 *proxy; MySortFilterProxyModel2 *proxy;
bool showDiskUsage;
}; };
#endif // PROFILESELECTOR_H #endif // PROFILESELECTOR_H

View File

@ -276,6 +276,22 @@
<property name="wordWrap"> <property name="wordWrap">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="openExternalLinks">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="diskSpaceInfo">
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>