Utils: Make FileName hold an QUrl optionally

This is meant to be helpful to piggy-back device information
in situations where the file resides on a remote file system.

The approach taken effectively duplicates the path data of the
url if the url constructor is actually used, but does not rely
on lossless path/fromPath conversions as it leaves the non-url
code paths unaffected in all situations.

The total overhead is deemed acceptable as the common use case
only default-constructs the m_url member which only sets a
pimpl nullptr.

Change-Id: Ie441cb1355ce086507333c31c7356ae5e6d10b96
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2019-05-09 16:35:25 +02:00
parent 1e9636ab8a
commit c238fc88b1
2 changed files with 46 additions and 1 deletions

View File

@@ -648,17 +648,32 @@ QFileInfo FileName::toFileInfo() const
return QFileInfo(m_data);
}
FileName FileName::fromUrl(const QUrl &url)
{
FileName fn;
fn.m_url = url;
fn.m_data = url.path();
return fn;
}
/// \returns a QString for passing on to QString based APIs
const QString &FileName::toString() const
{
return m_data;
}
QUrl FileName::toUrl() const
{
return m_url;
}
/// \returns a QString to display to the user
/// Converts the separators to the native format
QString FileName::toUserOutput() const
{
return QDir::toNativeSeparators(toString());
if (m_url.isEmpty())
return QDir::toNativeSeparators(toString());
return m_url.toString();
}
QString FileName::fileName(int pathComponents) const
@@ -769,6 +784,20 @@ FileName FileName::fromUtf8(const char *filename, int filenameSize)
return FileName::fromString(QString::fromUtf8(filename, filenameSize));
}
FileName FileName::fromVariant(const QVariant &variant)
{
if (variant.type() == QVariant::Url)
return FileName::fromUrl(variant.toUrl());
return FileName::fromString(variant.toString());
}
QVariant FileName::toVariant() const
{
if (!m_url.isEmpty())
return m_url;
return m_data;
}
bool FileName::operator==(const FileName &other) const
{
return QString::compare(m_data, other.m_data, HostOsInfo::fileNameCaseSensitivity()) == 0;
@@ -832,6 +861,11 @@ bool FileName::endsWith(const QString &s) const
return m_data.endsWith(s, HostOsInfo::fileNameCaseSensitivity());
}
bool FileName::isLocal() const
{
return m_url.isEmpty() || m_url.isLocalFile();
}
/// \returns the relativeChildPath of FileName to parent if FileName is a child of parent
/// \note returns a empty FileName if FileName is not a child of parent
/// That is, this never returns a path starting with "../"