[Qt-interest] How to tell a file is on the local network.
Konrad Rosenbaum
konrad at silmor.de
Sat Jan 1 14:00:18 CET 2011
On Saturday 01 January 2011, Mihail Naydenov wrote:
> A quick question: Is there a way to tell if a QUrl and/or QString and/or
> QFileInfo points to a file located on the local network?
> AFAIK both cases have schema "file:"., so it seam to me the only way is
> to test the path itself (for begins with // on windows), but this is not
> cross platform if I recall.
First, you have to distinguish between QFile(Info) and QUrl - they use
different schemes.
QFile and QFileInfo use absolute and relative path names only (not URLs) -
the syntax of those names depends on the platform you are working on with
two exceptions:
* on all platforms "/" is allowed as directory separator (i.e. on Windows it
is translated to "\" automatically
* paths starting with ":/" are redirected to internal resources
So the QFile path name "C:/helloworld.txt" is not a URL of scheme "C", but
an actual path name. The corresponding URL would be
"file:///C:/helloworld.txt" - but you can't feed this URL to QFile.
Whether a path is machine local, LAN local, or external network depends
entirely on the syntactic and semantic rules of the environment you are
working in. You can test whether a file is on the machine or on the network,
but that is platform specific (Windows: if it starts with "\\" it is
probably network, if it has a drive letter other than "C:" your chances are
fifty fifty; on Linux/Unix you have to check the output of the mount
command).
QUrl maps generic networked or local URL style path names. It is used (among
other things) in conjunction with QNetworkAccessManager to access network
resources (like HTTP downloads). It is not directly usable with QFile - you
would have to check it and then get the "local part" of the URL before you
can use it with QFile.
It is rather simple to check a URL: get the host name from it - if it is
empty or "localhost" then the resource is local(*), otherwise it is on the
network.
(*)in the case of "file://" it may also be mapped locally (a mounted network
drive); sorry, there is always a hitch... :-(
Konrad
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110101/6a6eb3ad/attachment.bin
More information about the Qt-interest-old
mailing list