QFileSystemEntry: Export static helper function checking for the root path

This provides a helper function which does the check on the string.
QFileInfo::isRoot() in addition checks for the existence of
the directory, which can cause hangs with network drives.
Use the new function in appropriate places in QtWidgets.

Task-number: QTBUG-6039
Change-Id: I54d0d860713e82b28fa4069a5345b042337f9c52
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
bb10
Friedemann Kleint 2017-06-12 15:31:53 +02:00
parent 00b8050d68
commit f2df5c64bd
4 changed files with 34 additions and 17 deletions

View File

@ -297,23 +297,27 @@ bool QFileSystemEntry::isAbsolute() const
bool QFileSystemEntry::isDriveRoot() const
{
resolveFilePath();
return QFileSystemEntry::isDriveRootPath(m_filePath);
}
bool QFileSystemEntry::isDriveRootPath(const QString &path)
{
#ifndef Q_OS_WINRT
return (m_filePath.length() == 3
&& m_filePath.at(0).isLetter() && m_filePath.at(1) == QLatin1Char(':')
&& m_filePath.at(2) == QLatin1Char('/'));
return (path.length() == 3
&& path.at(0).isLetter() && path.at(1) == QLatin1Char(':')
&& path.at(2) == QLatin1Char('/'));
#else // !Q_OS_WINRT
return m_filePath == QDir::rootPath();
return path == QDir::rootPath();
#endif // !Q_OS_WINRT
}
#endif
#endif // Q_OS_WIN
bool QFileSystemEntry::isRoot() const
bool QFileSystemEntry::isRootPath(const QString &path)
{
resolveFilePath();
if (m_filePath == QLatin1String("/")
if (path == QLatin1String("/")
#if defined(Q_OS_WIN)
|| isDriveRoot()
|| isUncRoot(m_filePath)
|| isDriveRootPath(path)
|| isUncRoot(path)
#endif
)
return true;
@ -321,6 +325,12 @@ bool QFileSystemEntry::isRoot() const
return false;
}
bool QFileSystemEntry::isRoot() const
{
resolveFilePath();
return isRootPath(m_filePath);
}
bool QFileSystemEntry::isEmpty() const
{
return m_filePath.isEmpty() && m_nativeFilePath.isEmpty();

View File

@ -94,6 +94,7 @@ public:
#if defined(Q_OS_WIN)
bool isDriveRoot() const;
static bool isDriveRootPath(const QString &path);
#endif
bool isRoot() const;
@ -103,6 +104,8 @@ public:
*this = QFileSystemEntry();
}
Q_CORE_EXPORT static bool isRootPath(const QString &path);
private:
// creates the QString version out of the bytearray version
void resolveFilePath() const;

View File

@ -52,6 +52,7 @@
#include <qstyle.h>
#include <qapplication.h>
#include <private/qabstractitemmodel_p.h>
#include <private/qfilesystementry_p.h>
#include <qdebug.h>
#include <stack>
@ -1104,8 +1105,9 @@ QString QDirModel::fileName(const QModelIndex &index) const
if (!d->indexValid(index))
return QString();
QFileInfo info = fileInfo(index);
if (info.isRoot())
return info.absoluteFilePath();
const QString &path = info.absoluteFilePath();
if (QFileSystemEntry::isRootPath(path))
return path;
if (d->resolveSymlinks && info.isSymLink())
info = d->resolvedInfo(info);
return info.fileName();
@ -1280,8 +1282,8 @@ QString QDirModelPrivate::name(const QModelIndex &index) const
{
const QDirNode *n = node(index);
const QFileInfo info = n->info;
if (info.isRoot()) {
QString name = info.absoluteFilePath();
QString name = info.absoluteFilePath();
if (QFileSystemEntry::isRootPath(name)) {
#if defined(Q_OS_WIN)
if (name.startsWith(QLatin1Char('/'))) // UNC host
return info.fileName();

View File

@ -46,6 +46,7 @@
#include <private/qfunctions_p.h>
#include <private/qguiapplication_p.h>
#include <private/qicon_p.h>
#include <private/qfilesystementry_p.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformservices.h>
#include <qpa/qplatformtheme.h>
@ -247,10 +248,11 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const
if (!retIcon.isNull())
return retIcon;
if (info.isRoot())
const QString &path = info.absoluteFilePath();
if (path.isEmpty() || QFileSystemEntry::isRootPath(path))
#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT)
{
UINT type = GetDriveType((wchar_t *)info.absoluteFilePath().utf16());
UINT type = GetDriveType(reinterpret_cast<const wchar_t *>(path.utf16()));
switch (type) {
case DRIVE_REMOVABLE:
@ -298,7 +300,7 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const
QString QFileIconProvider::type(const QFileInfo &info) const
{
if (info.isRoot())
if (QFileSystemEntry::isRootPath(info.absoluteFilePath()))
return QApplication::translate("QFileDialog", "Drive");
if (info.isFile()) {
if (!info.suffix().isEmpty()) {