Improve free disk space reporting

For non-existent directories (which will be created on demand) `Utils::Fs::freeDiskSpaceOnPath`
will return invalid value so instead query its parent path.
This commit is contained in:
Chocobo1 2022-07-23 12:27:00 +08:00
parent 50b01ed45d
commit 88d9e82fc9
No known key found for this signature in database
GPG key ID: 210D9C873253A68C

View file

@ -135,6 +135,22 @@ namespace
return -1; return -1;
} }
qint64 queryFreeDiskSpace(const Path &path)
{
const Path root = path.rootItem();
Path current = path;
qint64 freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
// for non-existent directories (which will be created on demand) `Utils::Fs::freeDiskSpaceOnPath`
// will return invalid value so instead query its parent/ancestor paths
while ((freeSpace < 0) && (current != root))
{
current = current.parentPath();
freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
}
return freeSpace;
}
void setPath(FileSystemPathComboEdit *fsPathEdit, const Path &newPath) void setPath(FileSystemPathComboEdit *fsPathEdit, const Path &newPath)
{ {
int existingIndex = indexOfPath(fsPathEdit, newPath); int existingIndex = indexOfPath(fsPathEdit, newPath);
@ -530,9 +546,10 @@ void AddNewTorrentDialog::updateDiskSpaceLabel()
} }
} }
const QString freeSpace = Utils::Misc::friendlyUnit(queryFreeDiskSpace(m_ui->savePath->selectedPath()));
const QString sizeString = tr("%1 (Free space on disk: %2)").arg( const QString sizeString = tr("%1 (Free space on disk: %2)").arg(
((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable.")) ((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable."))
, Utils::Misc::friendlyUnit(Utils::Fs::freeDiskSpaceOnPath(m_ui->savePath->selectedPath()))); , freeSpace);
m_ui->labelSizeData->setText(sizeString); m_ui->labelSizeData->setText(sizeString);
} }