Windows: Append file suffix in save-dialog.
authorFriedemann Kleint <Friedemann.Kleint@digia.com>
Thu, 4 Oct 2012 09:36:11 +0000 (11:36 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 4 Oct 2012 09:58:48 +0000 (11:58 +0200)
Task-number: QTBUG-27186
Change-Id: I04304fce1cbf6fb6794f352ff896eb463699d42b
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
src/plugins/platforms/windows/qwindowsdialoghelpers.cpp

index 8b053a5..34c8d69 100644 (file)
@@ -1025,6 +1025,26 @@ public:
     virtual QStringList selectedFiles() const;
 };
 
+// Append a suffix from the name filter "Foo files (*.foo;*.bar)"
+// unless the file name already has one.
+static inline QString appendSuffix(const QString &fileName, const QString &filter)
+{
+    const int lastDot = fileName.lastIndexOf(QLatin1Char('.'));
+    const int lastSlash = fileName.lastIndexOf(QLatin1Char('/'));
+    if (lastDot >= 0 && (lastSlash == -1 || lastDot > lastSlash))
+        return fileName;
+    int suffixPos = filter.indexOf(QLatin1String("(*."));
+    if (suffixPos < 0)
+        return fileName;
+    suffixPos += 3;
+    int endPos = filter.indexOf(QLatin1Char(';'), suffixPos + 1);
+    if (endPos < 0)
+        endPos = filter.indexOf(QLatin1Char(')'), suffixPos + 1);
+    if (endPos < 0)
+        return fileName;
+    return fileName + QLatin1Char('.') + filter.mid(suffixPos, endPos - suffixPos);
+}
+
 QPlatformDialogHelper::DialogCode QWindowsNativeSaveFileDialog::fileResult(QStringList *result /* = 0 */) const
 {
     if (result)
@@ -1034,7 +1054,7 @@ QPlatformDialogHelper::DialogCode QWindowsNativeSaveFileDialog::fileResult(QStri
     if (FAILED(hr) || !item)
         return QPlatformDialogHelper::Rejected;
     if (result)
-        result->push_back(QWindowsNativeFileDialogBase::itemPath(item));
+        result->push_back(appendSuffix(QWindowsNativeFileDialogBase::itemPath(item), selectedNameFilter()));
     return QPlatformDialogHelper::Accepted;
 }