Fix bugtracker #251: title block template with slash in name fails silently

Saving a new user title block template (right-click "Cartouches
utilisateur" > "Nouveau modèle" > "Enregistrer sous") with a name
containing a slash (or other filesystem-reserved character) silently
did nothing, with no error shown. The entered name is turned directly
into a filename (TitleBlockTemplatesFilesCollection::toFileName()), so
e.g. "foo/bar" becomes a path "foo/bar.titleblock" -- since "foo/"
essentially never exists as a directory, the underlying file write
fails, but that failure was never surfaced:

- TitleBlockTemplateLocation::isValid() only checked for an empty
  name, so an invalid name still counted as "valid" and got passed
  through to save.
- QETTitleBlockTemplateEditor::saveAs(const TitleBlockTemplateLocation&)
  discarded the bool result of setTemplateXmlDescription() and
  unconditionally returned true, marking the undo stack clean as if
  the save had actually succeeded.

Fix:
- isValid() now also rejects names containing \ / : * ? " < > |,
  matching the character set that's actually unsafe once the name
  becomes a filename.
- saveAs() (the no-arg entry point that asks the user for a location)
  now shows a clear error dialog when the entered name is rejected,
  distinguishing "user cancelled" (location.name() empty) from
  "name was invalid" (non-empty but rejected by isValid()).
- saveAs(location) now checks setTemplateXmlDescription()'s return
  value and shows an error dialog instead of reporting false success
  on any future/other write failure, not just this one.

Verified: clean rebuild, only the intended files recompiled and
linked successfully. Live-tested under Xvfb: creating a new template
and using "Enregistrer sous" with the name "foo/bar" now shows
"Le nom « foo/bar » n'est pas valide : il ne doit pas contenir les
caractères suivants : \ / : * ? " < > |" instead of silently doing
nothing; reopening the save-as dialog afterward showed the name field
correctly empty (nothing was partially written). Saving again with a
valid name ("mytemplate_valid") completed with no error dialog, and
the resulting mytemplate_valid.titleblock file was confirmed present
on disk in the user's title-block collection directory.
This commit is contained in:
ispyisail
2026-08-11 12:36:31 +12:00
parent 13e245b104
commit 2d889568a5
2 changed files with 36 additions and 2 deletions
+7 -1
View File
@@ -92,7 +92,13 @@ void TitleBlockTemplateLocation::setName(const QString &name) {
*/
bool TitleBlockTemplateLocation::isValid() const
{
return(!name_.isEmpty());
// The name becomes (part of) a filename on disk (see
// TitleBlockTemplatesFilesCollection::toFileName()). A name containing
// a path separator or another filesystem-reserved character silently
// fails to save instead of erroring, because it turns into an
// unintended subpath rather than a plain filename (bugtracker #251).
static const QRegularExpression invalid_chars_re(QStringLiteral("[\\\\/:*?\"<>|]"));
return(!name_.isEmpty() && !name_.contains(invalid_chars_re));
}
/**