From c0b3c06a7d4f6a2f90742e8dc81c898082a70416 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 5 Sep 2019 09:52:05 +0200 Subject: QFileDialog: Fix volume name display on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The volume names displayed did not match those of Windows Explorer; for example "New Volume (X:)" would be displayed for mapped network drives. Replace GetVolumeInformation() and manual formatting by the normal display name of IShellItem. Fixes: QTBUG-78043 Change-Id: Ia742b7733e8ddc31e9506f15d90d065b985a111d Reviewed-by: André de la Rocha --- src/widgets/dialogs/qfilesystemmodel.cpp | 39 +++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/widgets/dialogs/qfilesystemmodel.cpp') diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index e9e49e0c33..a04189513a 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -57,6 +57,9 @@ #ifdef Q_OS_WIN # include # include +# ifndef Q_OS_WINRT +# include +# endif #endif QT_BEGIN_NAMESPACE @@ -837,8 +840,8 @@ QString QFileSystemModelPrivate::displayName(const QModelIndex &index) const { #if defined(Q_OS_WIN) QFileSystemNode *dirNode = node(index); - if (!dirNode->volumeName.isNull()) - return dirNode->volumeName + QLatin1String(" (") + name(index) + QLatin1Char(')'); + if (!dirNode->volumeName.isEmpty()) + return dirNode->volumeName; #endif return name(index); } @@ -1773,6 +1776,27 @@ void QFileSystemModelPrivate::_q_directoryChanged(const QString &directory, cons removeNode(parentNode, toRemove[i]); } +#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +static QString volumeName(const QString &path) +{ + IShellItem *item = nullptr; + const QString native = QDir::toNativeSeparators(path); + HRESULT hr = SHCreateItemFromParsingName(reinterpret_cast(native.utf16()), + nullptr, IID_IShellItem, + reinterpret_cast(&item)); + if (FAILED(hr)) + return QString(); + LPWSTR name = nullptr; + hr = item->GetDisplayName(SIGDN_NORMALDISPLAY, &name); + if (FAILED(hr)) + return QString(); + QString result = QString::fromWCharArray(name); + CoTaskMemFree(name); + item->Release(); + return result; +} +#endif // Q_OS_WIN && !Q_OS_WINRT + /*! \internal @@ -1791,15 +1815,8 @@ QFileSystemModelPrivate::QFileSystemNode* QFileSystemModelPrivate::addNode(QFile #endif #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) //The parentNode is "" so we are listing the drives - if (parentNode->fileName.isEmpty()) { - wchar_t name[MAX_PATH + 1]; - //GetVolumeInformation requires to add trailing backslash - const QString nodeName = fileName + QLatin1String("\\"); - BOOL success = ::GetVolumeInformation((wchar_t *)(nodeName.utf16()), - name, MAX_PATH + 1, NULL, 0, NULL, NULL, 0); - if (success && name[0]) - node->volumeName = QString::fromWCharArray(name); - } + if (parentNode->fileName.isEmpty()) + node->volumeName = volumeName(fileName); #endif Q_ASSERT(!parentNode->children.contains(fileName)); parentNode->children.insert(fileName, node); -- cgit v1.2.3