mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-11 22:54:12 +02:00
Create the folder of the cable types file before writing into it
The catalogue is written with QSaveFile, which cannot write into a folder that is not there. A fresh profile has no documents folder, and a path typed into the settings may name a folder which was never created, so the first write failed with "No such file or directory" and no way for the user to see that the folder was the problem rather than the file (non-blocking review comment by ispyisail). writeFile() makes the folder first, which covers every writer: creating the template, appending a type and editing one all go through it. Covered by tst_cabletypelist::aMissingFolderIsCreated, writing into <tmp>/not/there/yet/qet_cable_types.csv. Verified in both directions: without the fix the test fails with 'createFile(path, &error) returned FALSE. (No such file or directory)', with it the file is created and reads back.
This commit is contained in:
@@ -635,6 +635,22 @@ bool CableTypeList::writeFile(const QString &path, const CableTypeListData &data
|
||||
header_lines.append(data.columns);
|
||||
const QByteArray content = MaterialList::serializeCsv(header_lines, rows, data.separator);
|
||||
|
||||
//The folder may not be there yet: a fresh profile has no
|
||||
//documents folder, and a path typed into the settings may name a
|
||||
//folder which was never created. QSaveFile cannot write into a
|
||||
//folder which does not exist, so without this the whole write
|
||||
//fails and the message says nothing about the real reason.
|
||||
const QString folder = QFileInfo(path).absolutePath();
|
||||
if (!folder.isEmpty() && !QDir(folder).exists() && !QDir().mkpath(folder))
|
||||
{
|
||||
if (error) {
|
||||
*error = QCoreApplication::translate(
|
||||
"CableTypeList",
|
||||
"Cannot create the folder %1 for the cable types file.").arg(folder);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QSaveFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly) || file.write(content) != content.size())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user