source: trunk/src/corelib/io/qfileinfo.cpp@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 34.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43#include "qfileinfo.h"
44#include "qglobal.h"
45#include "qdir.h"
46#include "qfileinfo_p.h"
47
48QT_BEGIN_NAMESPACE
49
50QString QFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const
51{
52 if (cache_enabled && !fileNames[(int)name].isNull())
53 return fileNames[(int)name];
54 QString ret = fileEngine->fileName(name);
55 if (ret.isNull())
56 ret = QLatin1String("");
57 if (cache_enabled)
58 fileNames[(int)name] = ret;
59 return ret;
60}
61
62QString QFileInfoPrivate::getFileOwner(QAbstractFileEngine::FileOwner own) const
63{
64 if (cache_enabled && !fileOwners[(int)own].isNull())
65 return fileOwners[(int)own];
66 QString ret = fileEngine->owner(own);
67 if (ret.isNull())
68 ret = QLatin1String("");
69 if (cache_enabled)
70 fileOwners[(int)own] = ret;
71 return ret;
72}
73
74uint QFileInfoPrivate::getFileFlags(QAbstractFileEngine::FileFlags request) const
75{
76 // We split the testing into tests for for LinkType, BundleType, PermsMask
77 // and the rest.
78 // Tests for file permissions on Windows can be slow, expecially on network
79 // paths and NTFS drives.
80 // In order to determine if a file is a symlink or not, we have to lstat().
81 // If we're not interested in that information, we might as well avoid one
82 // extra syscall. Bundle detecton on Mac can be slow, expecially on network
83 // paths, so we separate out that as well.
84
85 QAbstractFileEngine::FileFlags req = 0;
86 uint cachedFlags = 0;
87
88 if (request & (QAbstractFileEngine::FlagsMask | QAbstractFileEngine::TypesMask)) {
89 if (!getCachedFlag(CachedFileFlags)) {
90 req |= QAbstractFileEngine::FlagsMask;
91 req |= QAbstractFileEngine::TypesMask;
92 req &= (~QAbstractFileEngine::LinkType);
93 req &= (~QAbstractFileEngine::BundleType);
94
95 cachedFlags |= CachedFileFlags;
96 }
97
98 if (request & QAbstractFileEngine::LinkType) {
99 if (!getCachedFlag(CachedLinkTypeFlag)) {
100 req |= QAbstractFileEngine::LinkType;
101 cachedFlags |= CachedLinkTypeFlag;
102 }
103 }
104
105 if (request & QAbstractFileEngine::BundleType) {
106 if (!getCachedFlag(CachedBundleTypeFlag)) {
107 req |= QAbstractFileEngine::BundleType;
108 cachedFlags |= CachedBundleTypeFlag;
109 }
110 }
111 }
112
113 if (request & QAbstractFileEngine::PermsMask) {
114 if (!getCachedFlag(CachedPerms)) {
115 req |= QAbstractFileEngine::PermsMask;
116 cachedFlags |= CachedPerms;
117 }
118 }
119
120 if (req) {
121 if (cache_enabled)
122 req &= (~QAbstractFileEngine::Refresh);
123 else
124 req |= QAbstractFileEngine::Refresh;
125
126 QAbstractFileEngine::FileFlags flags = fileEngine->fileFlags(req);
127 fileFlags |= uint(flags);
128 setCachedFlag(cachedFlags);
129 }
130
131 return fileFlags & request;
132}
133
134QDateTime &QFileInfoPrivate::getFileTime(QAbstractFileEngine::FileTime request) const
135{
136 if (!cache_enabled)
137 clearFlags();
138 uint cf;
139 if (request == QAbstractFileEngine::CreationTime)
140 cf = CachedCTime;
141 else if (request == QAbstractFileEngine::ModificationTime)
142 cf = CachedMTime;
143 else
144 cf = CachedATime;
145 if (!getCachedFlag(cf)) {
146 fileTimes[request] = fileEngine->fileTime(request);
147 setCachedFlag(cf);
148 }
149 return fileTimes[request];
150}
151
152//************* QFileInfo
153
154/*!
155 \class QFileInfo
156 \reentrant
157 \brief The QFileInfo class provides system-independent file information.
158
159 \ingroup io
160 \ingroup shared
161
162 QFileInfo provides information about a file's name and position
163 (path) in the file system, its access rights and whether it is a
164 directory or symbolic link, etc. The file's size and last
165 modified/read times are also available. QFileInfo can also be
166 used to obtain information about a Qt \l{resource
167 system}{resource}.
168
169 A QFileInfo can point to a file with either a relative or an
170 absolute file path. Absolute file paths begin with the directory
171 separator "/" (or with a drive specification on Windows). Relative
172 file names begin with a directory name or a file name and specify
173 a path relative to the current working directory. An example of an
174 absolute path is the string "/tmp/quartz". A relative path might
175 look like "src/fatlib". You can use the function isRelative() to
176 check whether a QFileInfo is using a relative or an absolute file
177 path. You can call the function makeAbsolute() to convert a
178 relative QFileInfo's path to an absolute path.
179
180 The file that the QFileInfo works on is set in the constructor or
181 later with setFile(). Use exists() to see if the file exists and
182 size() to get its size.
183
184 The file's type is obtained with isFile(), isDir() and
185 isSymLink(). The symLinkTarget() function provides the name of the file
186 the symlink points to.
187
188 On Unix (including Mac OS X), the symlink has the same size() has
189 the file it points to, because Unix handles symlinks
190 transparently; similarly, opening a symlink using QFile
191 effectively opens the link's target. For example:
192
193 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 0
194
195 On Windows, symlinks (shortcuts) are \c .lnk files. The reported
196 size() is that of the symlink (not the link's target), and
197 opening a symlink using QFile opens the \c .lnk file. For
198 example:
199
200 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 1
201
202 Elements of the file's name can be extracted with path() and
203 fileName(). The fileName()'s parts can be extracted with
204 baseName(), suffix() or completeSuffix(). QFileInfo objects to
205 directories created by Qt classes will not have a trailing file
206 separator. If you wish to use trailing separators in your own file
207 info objects, just append one to the file name given to the constructors
208 or setFile().
209
210 The file's dates are returned by created(), lastModified() and
211 lastRead(). Information about the file's access permissions is
212 obtained with isReadable(), isWritable() and isExecutable(). The
213 file's ownership is available from owner(), ownerId(), group() and
214 groupId(). You can examine a file's permissions and ownership in a
215 single statement using the permission() function.
216
217 \section1 Performance Issues
218
219 Some of QFileInfo's functions query the file system, but for
220 performance reasons, some functions only operate on the
221 file name itself. For example: To return the absolute path of
222 a relative file name, absolutePath() has to query the file system.
223 The path() function, however, can work on the file name directly,
224 and so it is faster.
225
226 \note To speed up performance, QFileInfo caches information about
227 the file.
228
229 To speed up performance, QFileInfo caches information about the
230 file. Because files can be changed by other users or programs, or
231 even by other parts of the same program, there is a function that
232 refreshes the file information: refresh(). If you want to switch
233 off a QFileInfo's caching and force it to access the file system
234 every time you request information from it call setCaching(false).
235
236 \sa QDir, QFile
237*/
238
239/*!
240 Constructs an empty QFileInfo object.
241
242 Note that an empty QFileInfo object contain no file reference.
243
244 \sa setFile()
245*/
246QFileInfo::QFileInfo() : d_ptr(new QFileInfoPrivate())
247{
248}
249
250/*!
251 Constructs a new QFileInfo that gives information about the given
252 file. The \a file can also include an absolute or relative path.
253
254 \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()
255*/
256QFileInfo::QFileInfo(const QString &file) : d_ptr(new QFileInfoPrivate(file))
257{
258}
259
260/*!
261 Constructs a new QFileInfo that gives information about file \a
262 file.
263
264 If the \a file has a relative path, the QFileInfo will also have a
265 relative path.
266
267 \sa isRelative()
268*/
269QFileInfo::QFileInfo(const QFile &file) : d_ptr(new QFileInfoPrivate(file.fileName()))
270{
271}
272
273/*!
274 Constructs a new QFileInfo that gives information about the given
275 \a file in the directory \a dir.
276
277 If \a dir has a relative path, the QFileInfo will also have a
278 relative path.
279
280 If \a file is an absolute path, then the directory specified
281 by \a dir will be disregarded.
282
283 \sa isRelative()
284*/
285QFileInfo::QFileInfo(const QDir &dir, const QString &file)
286 : d_ptr(new QFileInfoPrivate(dir.filePath(file)))
287{
288}
289
290/*!
291 Constructs a new QFileInfo that is a copy of the given \a fileinfo.
292*/
293QFileInfo::QFileInfo(const QFileInfo &fileinfo)
294 : d_ptr(fileinfo.d_ptr)
295{
296
297}
298
299/*!
300 Destroys the QFileInfo and frees its resources.
301*/
302
303QFileInfo::~QFileInfo()
304{
305}
306
307/*!
308 \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo)
309
310 Returns true if this QFileInfo object refers to a different file
311 than the one specified by \a fileinfo; otherwise returns false.
312
313 \sa operator==()
314*/
315
316/*!
317 \overload
318 \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo) const
319*/
320
321/*!
322 \overload
323*/
324bool QFileInfo::operator==(const QFileInfo &fileinfo) const
325{
326 Q_D(const QFileInfo);
327 // ### Qt 5: understand long and short file names on Windows
328 // ### (GetFullPathName()).
329 if (fileinfo.d_ptr == d_ptr)
330 return true;
331 if (d->isDefaultConstructed || fileinfo.d_ptr->isDefaultConstructed)
332 return false;
333 if (d->fileEngine->caseSensitive() != fileinfo.d_ptr->fileEngine->caseSensitive())
334 return false;
335 if (fileinfo.size() == size()) { //if the size isn't the same...
336 QString file1 = canonicalFilePath(),
337 file2 = fileinfo.canonicalFilePath();
338 if (file1.length() == file2.length()) {
339 if (!fileinfo.d_ptr->fileEngine->caseSensitive()) {
340 for (int i = 0; i < file1.length(); i++) {
341 if (file1.at(i).toLower() != file2.at(i).toLower())
342 return false;
343 }
344 return true;
345 }
346 return (file1 == file2);
347 }
348 }
349 return false;
350}
351
352/*!
353 Returns true if this QFileInfo object refers to a file in the same
354 location as \a fileinfo; otherwise returns false.
355
356 Note that the result of comparing two empty QFileInfo objects,
357 containing no file references, is undefined.
358
359 \warning This will not compare two different symbolic links
360 pointing to the same file.
361
362 \warning Long and short file names that refer to the same file on Windows
363 are treated as if they referred to different files.
364
365 \sa operator!=()
366*/
367bool QFileInfo::operator==(const QFileInfo &fileinfo)
368{
369 return const_cast<const QFileInfo *>(this)->operator==(fileinfo);
370}
371
372/*!
373 Makes a copy of the given \a fileinfo and assigns it to this QFileInfo.
374*/
375QFileInfo &QFileInfo::operator=(const QFileInfo &fileinfo)
376{
377 d_ptr = fileinfo.d_ptr;
378 return *this;
379}
380
381/*!
382 Sets the file that the QFileInfo provides information about to \a
383 file.
384
385 The \a file can also include an absolute or relative file path.
386 Absolute paths begin with the directory separator (e.g. "/" under
387 Unix) or a drive specification (under Windows). Relative file
388 names begin with a directory name or a file name and specify a
389 path relative to the current directory.
390
391 Example:
392 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 2
393
394 \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
395*/
396void QFileInfo::setFile(const QString &file)
397{
398 bool caching = d_ptr.constData()->cache_enabled;
399 *this = QFileInfo(file);
400 d_ptr->cache_enabled = caching;
401}
402
403/*!
404 \overload
405
406 Sets the file that the QFileInfo provides information about to \a
407 file.
408
409 If \a file includes a relative path, the QFileInfo will also have
410 a relative path.
411
412 \sa isRelative()
413*/
414void QFileInfo::setFile(const QFile &file)
415{
416 setFile(file.fileName());
417}
418
419/*!
420 \overload
421
422 Sets the file that the QFileInfo provides information about to \a
423 file in directory \a dir.
424
425 If \a file includes a relative path, the QFileInfo will also
426 have a relative path.
427
428 \sa isRelative()
429*/
430void QFileInfo::setFile(const QDir &dir, const QString &file)
431{
432 setFile(dir.filePath(file));
433}
434
435/*!
436 Returns an absolute path including the file name.
437
438 The absolute path name consists of the full path and the file
439 name. On Unix this will always begin with the root, '/',
440 directory. On Windows this will always begin 'D:/' where D is a
441 drive letter, except for network shares that are not mapped to a
442 drive letter, in which case the path will begin '//sharename/'.
443 QFileInfo will uppercase drive letters. Note that QDir does not do
444 this. The code snippet below shows this.
445
446 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp newstuff
447
448 This function returns the same as filePath(), unless isRelative()
449 is true. In contrast to canonicalFilePath(), symbolic links or
450 redundant "." or ".." elements are not necessarily removed.
451
452 If the QFileInfo is empty it returns QDir::currentPath().
453
454 \sa filePath(), canonicalFilePath(), isRelative()
455*/
456QString QFileInfo::absoluteFilePath() const
457{
458 Q_D(const QFileInfo);
459 if (d->isDefaultConstructed)
460 return QLatin1String("");
461 return d->getFileName(QAbstractFileEngine::AbsoluteName);
462}
463
464/*!
465 Returns the canonical path including the file name, i.e. an absolute
466 path without symbolic links or redundant "." or ".." elements.
467
468 If the file does not exist, canonicalFilePath() returns an empty
469 string.
470
471 \sa filePath(), absoluteFilePath(), dir()
472*/
473QString QFileInfo::canonicalFilePath() const
474{
475 Q_D(const QFileInfo);
476 if (d->isDefaultConstructed)
477 return QLatin1String("");
478 return d->getFileName(QAbstractFileEngine::CanonicalName);
479}
480
481
482/*!
483 Returns a file's path absolute path. This doesn't include the
484 file name.
485
486 On Unix the absolute path will always begin with the root, '/',
487 directory. On Windows this will always begin 'D:/' where D is a
488 drive letter, except for network shares that are not mapped to a
489 drive letter, in which case the path will begin '//sharename/'.
490
491 In contrast to canonicalPath() symbolic links or redundant "." or
492 ".." elements are not necessarily removed.
493
494 \warning If the QFileInfo object was created with an empty QString,
495 the behavior of this function is undefined.
496
497 \sa absoluteFilePath(), path(), canonicalPath(), fileName(), isRelative()
498*/
499QString QFileInfo::absolutePath() const
500{
501 Q_D(const QFileInfo);
502
503 if (d->isDefaultConstructed) {
504 return QLatin1String("");
505 } else if (d->fileName.isEmpty()) {
506 qWarning("QFileInfo::absolutePath: Constructed with empty filename");
507 return QLatin1String("");
508 }
509 return d->getFileName(QAbstractFileEngine::AbsolutePathName);
510}
511
512/*!
513 Returns the file's path canonical path (excluding the file name),
514 i.e. an absolute path without symbolic links or redundant "." or ".." elements.
515
516 If the file does not exist, canonicalPath() returns an empty string.
517
518 \sa path(), absolutePath()
519*/
520QString QFileInfo::canonicalPath() const
521{
522 Q_D(const QFileInfo);
523 if (d->isDefaultConstructed)
524 return QLatin1String("");
525 return d->getFileName(QAbstractFileEngine::CanonicalPathName);
526}
527
528/*!
529 Returns the file's path. This doesn't include the file name.
530
531 Note that, if this QFileInfo object is given a path ending in a
532 slash, the name of the file is considered empty and this function
533 will return the entire path.
534
535 \sa filePath(), absolutePath(), canonicalPath(), dir(), fileName(), isRelative()
536*/
537QString QFileInfo::path() const
538{
539 Q_D(const QFileInfo);
540 if (d->isDefaultConstructed)
541 return QLatin1String("");
542 return d->getFileName(QAbstractFileEngine::PathName);
543}
544
545/*!
546 \fn bool QFileInfo::isAbsolute() const
547
548 Returns true if the file path name is absolute, otherwise returns
549 false if the path is relative.
550
551 \sa isRelative()
552*/
553
554/*!
555 Returns true if the file path name is relative, otherwise returns
556 false if the path is absolute (e.g. under Unix a path is absolute
557 if it begins with a "/").
558
559 \sa isAbsolute()
560*/
561bool QFileInfo::isRelative() const
562{
563 Q_D(const QFileInfo);
564 if (d->isDefaultConstructed)
565 return true;
566 return d->fileEngine->isRelativePath();
567}
568
569/*!
570 Converts the file's path to an absolute path if it is not already in that form.
571 Returns true to indicate that the path was converted; otherwise returns false
572 to indicate that the path was already absolute.
573
574 \sa filePath(), isRelative()
575*/
576bool QFileInfo::makeAbsolute()
577{
578 if (d_ptr.constData()->isDefaultConstructed
579 || !d_ptr.constData()->fileEngine->isRelativePath())
580 return false;
581 QString absFileName = d_ptr.constData()->getFileName(QAbstractFileEngine::AbsoluteName);
582 // QSharedDataPointer::operator->() will detach.
583
584 setFile(absFileName);
585 return true;
586}
587
588/*!
589 Returns true if the file exists; otherwise returns false.
590
591 \note If the file is a symlink that points to a non existing
592 file, false is returned.
593*/
594bool QFileInfo::exists() const
595{
596 Q_D(const QFileInfo);
597 if (d->isDefaultConstructed)
598 return false;
599 return d->getFileFlags(QAbstractFileEngine::ExistsFlag);
600}
601
602/*!
603 Refreshes the information about the file, i.e. reads in information
604 from the file system the next time a cached property is fetched.
605
606 \note On Windows CE, there might be a delay for the file system driver
607 to detect changes on the file.
608*/
609void QFileInfo::refresh()
610{
611 Q_D(QFileInfo);
612 d->clear();
613}
614
615/*!
616 Returns the file name, including the path (which may be absolute
617 or relative).
618
619 \sa absoluteFilePath(), canonicalFilePath(), isRelative()
620*/
621QString QFileInfo::filePath() const
622{
623 Q_D(const QFileInfo);
624 if (d->isDefaultConstructed)
625 return QLatin1String("");
626 return d->getFileName(QAbstractFileEngine::DefaultName);
627}
628
629/*!
630 Returns the name of the file, excluding the path.
631
632 Example:
633 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 3
634
635 Note that, if this QFileInfo object is given a path ending in a
636 slash, the name of the file is considered empty.
637
638 \sa isRelative(), filePath(), baseName(), extension()
639*/
640QString QFileInfo::fileName() const
641{
642 Q_D(const QFileInfo);
643 if (d->isDefaultConstructed)
644 return QLatin1String("");
645 return d->getFileName(QAbstractFileEngine::BaseName);
646}
647
648/*!
649 \since 4.3
650 Returns the name of the bundle.
651
652 On Mac OS X this returns the proper localized name for a bundle if the
653 path isBundle(). On all other platforms an empty QString is returned.
654
655 Example:
656 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 4
657
658 \sa isBundle(), filePath(), baseName(), extension()
659*/
660QString QFileInfo::bundleName() const
661{
662 Q_D(const QFileInfo);
663 if (d->isDefaultConstructed)
664 return QLatin1String("");
665 return d->getFileName(QAbstractFileEngine::BundleName);
666}
667
668/*!
669 Returns the base name of the file without the path.
670
671 The base name consists of all characters in the file up to (but
672 not including) the \e first '.' character.
673
674 Example:
675 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 5
676
677
678 The base name of a file is computed equally on all platforms, independent
679 of file naming conventions (e.g., ".bashrc" on Unix has an empty base
680 name, and the suffix is "bashrc").
681
682 \sa fileName(), suffix(), completeSuffix(), completeBaseName()
683*/
684QString QFileInfo::baseName() const
685{
686 Q_D(const QFileInfo);
687 if (d->isDefaultConstructed)
688 return QLatin1String("");
689 return d->getFileName(QAbstractFileEngine::BaseName).section(QLatin1Char('.'), 0, 0);
690}
691
692/*!
693 Returns the complete base name of the file without the path.
694
695 The complete base name consists of all characters in the file up
696 to (but not including) the \e last '.' character.
697
698 Example:
699 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 6
700
701 \sa fileName(), suffix(), completeSuffix(), baseName()
702*/
703QString QFileInfo::completeBaseName() const
704{
705 Q_D(const QFileInfo);
706 if (d->isDefaultConstructed)
707 return QLatin1String("");
708 QString name = d->getFileName(QAbstractFileEngine::BaseName);
709 int index = name.lastIndexOf(QLatin1Char('.'));
710 return (index == -1) ? name : name.left(index);
711}
712
713/*!
714 Returns the complete suffix of the file.
715
716 The complete suffix consists of all characters in the file after
717 (but not including) the first '.'.
718
719 Example:
720 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 7
721
722 \sa fileName(), suffix(), baseName(), completeBaseName()
723*/
724QString QFileInfo::completeSuffix() const
725{
726 Q_D(const QFileInfo);
727 if (d->isDefaultConstructed)
728 return QLatin1String("");
729 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
730 int firstDot = fileName.indexOf(QLatin1Char('.'));
731 if (firstDot == -1)
732 return QLatin1String("");
733 return fileName.mid(firstDot + 1);
734}
735
736/*!
737 Returns the suffix of the file.
738
739 The suffix consists of all characters in the file after (but not
740 including) the last '.'.
741
742 Example:
743 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 8
744
745 The suffix of a file is computed equally on all platforms, independent of
746 file naming conventions (e.g., ".bashrc" on Unix has an empty base name,
747 and the suffix is "bashrc").
748
749 \sa fileName(), completeSuffix(), baseName(), completeBaseName()
750*/
751QString QFileInfo::suffix() const
752{
753 Q_D(const QFileInfo);
754 if (d->isDefaultConstructed)
755 return QLatin1String("");
756 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
757 int lastDot = fileName.lastIndexOf(QLatin1Char('.'));
758 if (lastDot == -1)
759 return QLatin1String("");
760 return fileName.mid(lastDot + 1);
761}
762
763
764/*!
765 Returns the path of the object's parent directory as a QDir object.
766
767 \bold{Note:} The QDir returned always corresponds to the object's
768 parent directory, even if the QFileInfo represents a directory.
769
770 For each of the following, dir() returns a QDir for
771 \c{"~/examples/191697"}.
772
773 \snippet doc/src/snippets/fileinfo/main.cpp 0
774
775 For each of the following, dir() returns a QDir for
776 \c{"."}.
777
778 \snippet doc/src/snippets/fileinfo/main.cpp 1
779
780 \sa absolutePath(), filePath(), fileName(), isRelative(), absoluteDir()
781*/
782QDir QFileInfo::dir() const
783{
784 // ### Qt5: Maybe rename this to parentDirectory(), considering what it actually do?
785 return QDir(path());
786}
787
788/*!
789 Returns the file's absolute path as a QDir object.
790
791 \sa dir(), filePath(), fileName(), isRelative()
792*/
793QDir QFileInfo::absoluteDir() const
794{
795 return QDir(absolutePath());
796}
797
798#ifdef QT3_SUPPORT
799/*!
800 Use absoluteDir() or the dir() overload that takes no parameters
801 instead.
802*/
803QDir QFileInfo::dir(bool absPath) const
804{
805 if (absPath)
806 return absoluteDir();
807 return dir();
808}
809#endif //QT3_SUPPORT
810
811/*!
812 Returns true if the user can read the file; otherwise returns false.
813
814 \sa isWritable(), isExecutable(), permission()
815*/
816bool QFileInfo::isReadable() const
817{
818 Q_D(const QFileInfo);
819 if (d->isDefaultConstructed)
820 return false;
821 return d->getFileFlags(QAbstractFileEngine::ReadUserPerm);
822}
823
824/*!
825 Returns true if the user can write to the file; otherwise returns false.
826
827 \sa isReadable(), isExecutable(), permission()
828*/
829bool QFileInfo::isWritable() const
830{
831 Q_D(const QFileInfo);
832 if (d->isDefaultConstructed)
833 return false;
834 return d->getFileFlags(QAbstractFileEngine::WriteUserPerm);
835}
836
837/*!
838 Returns true if the file is executable; otherwise returns false.
839
840 \sa isReadable(), isWritable(), permission()
841*/
842bool QFileInfo::isExecutable() const
843{
844 Q_D(const QFileInfo);
845 if (d->isDefaultConstructed)
846 return false;
847 return d->getFileFlags(QAbstractFileEngine::ExeUserPerm);
848}
849
850/*!
851 Returns true if this is a `hidden' file; otherwise returns false.
852
853 \bold{Note:} This function returns true for the special entries
854 "." and ".." on Unix, even though QDir::entryList threats them as shown.
855*/
856bool QFileInfo::isHidden() const
857{
858 Q_D(const QFileInfo);
859 if (d->isDefaultConstructed)
860 return false;
861 return d->getFileFlags(QAbstractFileEngine::HiddenFlag);
862}
863
864/*!
865 Returns true if this object points to a file or to a symbolic
866 link to a file. Returns false if the
867 object points to something which isn't a file, such as a directory.
868
869 \sa isDir(), isSymLink(), isBundle()
870*/
871bool QFileInfo::isFile() const
872{
873 Q_D(const QFileInfo);
874 if (d->isDefaultConstructed)
875 return false;
876 return d->getFileFlags(QAbstractFileEngine::FileType);
877}
878
879/*!
880 Returns true if this object points to a directory or to a symbolic
881 link to a directory; otherwise returns false.
882
883 \sa isFile(), isSymLink(), isBundle()
884*/
885bool QFileInfo::isDir() const
886{
887 Q_D(const QFileInfo);
888 if (d->isDefaultConstructed)
889 return false;
890 return d->getFileFlags(QAbstractFileEngine::DirectoryType);
891}
892
893
894/*!
895 \since 4.3
896 Returns true if this object points to a bundle or to a symbolic
897 link to a bundle on Mac OS X; otherwise returns false.
898
899 \sa isDir(), isSymLink(), isFile()
900*/
901bool QFileInfo::isBundle() const
902{
903 Q_D(const QFileInfo);
904 if (d->isDefaultConstructed)
905 return false;
906 return d->getFileFlags(QAbstractFileEngine::BundleType);
907}
908
909/*!
910 Returns true if this object points to a symbolic link (or to a
911 shortcut on Windows); otherwise returns false.
912
913 On Unix (including Mac OS X), opening a symlink effectively opens
914 the \l{symLinkTarget()}{link's target}. On Windows, it opens the \c
915 .lnk file itself.
916
917 Example:
918
919 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 9
920
921 \note If the symlink points to a non existing file, exists() returns
922 false.
923
924 \sa isFile(), isDir(), symLinkTarget()
925*/
926bool QFileInfo::isSymLink() const
927{
928 Q_D(const QFileInfo);
929 if (d->isDefaultConstructed)
930 return false;
931 return d->getFileFlags(QAbstractFileEngine::LinkType);
932}
933
934/*!
935 Returns true if the object points to a directory or to a symbolic
936 link to a directory, and that directory is the root directory; otherwise
937 returns false.
938*/
939bool QFileInfo::isRoot() const
940{
941 Q_D(const QFileInfo);
942 if (d->isDefaultConstructed)
943 return true;
944 return d->getFileFlags(QAbstractFileEngine::RootFlag);
945}
946
947/*!
948 \fn QString QFileInfo::symLinkTarget() const
949 \since 4.2
950
951 Returns the absolute path to the file or directory a symlink (or shortcut
952 on Windows) points to, or a an empty string if the object isn't a symbolic
953 link.
954
955 This name may not represent an existing file; it is only a string.
956 QFileInfo::exists() returns true if the symlink points to an
957 existing file.
958
959 \sa exists(), isSymLink(), isDir(), isFile()
960*/
961
962/*!
963 \obsolete
964
965 Use symLinkTarget() instead.
966*/
967QString QFileInfo::readLink() const
968{
969 Q_D(const QFileInfo);
970 if (d->isDefaultConstructed)
971 return QLatin1String("");
972 return d->getFileName(QAbstractFileEngine::LinkName);
973}
974
975/*!
976 Returns the owner of the file. On systems where files
977 do not have owners, or if an error occurs, an empty string is
978 returned.
979
980 This function can be time consuming under Unix (in the order of
981 milliseconds).
982
983 \sa ownerId(), group(), groupId()
984*/
985QString QFileInfo::owner() const
986{
987 Q_D(const QFileInfo);
988 if (d->isDefaultConstructed)
989 return QLatin1String("");
990 return d->getFileOwner(QAbstractFileEngine::OwnerUser);
991}
992
993/*!
994 Returns the id of the owner of the file.
995
996 On Windows and on systems where files do not have owners this
997 function returns ((uint) -2).
998
999 \sa owner(), group(), groupId()
1000*/
1001uint QFileInfo::ownerId() const
1002{
1003 Q_D(const QFileInfo);
1004 if (d->isDefaultConstructed)
1005 return 0;
1006 return d->fileEngine->ownerId(QAbstractFileEngine::OwnerUser);
1007}
1008
1009/*!
1010 Returns the group of the file. On Windows, on systems where files
1011 do not have groups, or if an error occurs, an empty string is
1012 returned.
1013
1014 This function can be time consuming under Unix (in the order of
1015 milliseconds).
1016
1017 \sa groupId(), owner(), ownerId()
1018*/
1019QString QFileInfo::group() const
1020{
1021 Q_D(const QFileInfo);
1022 if (d->isDefaultConstructed)
1023 return QLatin1String("");
1024 return d->getFileOwner(QAbstractFileEngine::OwnerGroup);
1025}
1026
1027/*!
1028 Returns the id of the group the file belongs to.
1029
1030 On Windows and on systems where files do not have groups this
1031 function always returns (uint) -2.
1032
1033 \sa group(), owner(), ownerId()
1034*/
1035uint QFileInfo::groupId() const
1036{
1037 Q_D(const QFileInfo);
1038 if (d->isDefaultConstructed)
1039 return 0;
1040 return d->fileEngine->ownerId(QAbstractFileEngine::OwnerGroup);
1041}
1042
1043/*!
1044 Tests for file permissions. The \a permissions argument can be
1045 several flags of type QFile::Permissions OR-ed together to check
1046 for permission combinations.
1047
1048 On systems where files do not have permissions this function
1049 always returns true.
1050
1051 Example:
1052 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 10
1053
1054 \sa isReadable(), isWritable(), isExecutable()
1055*/
1056bool QFileInfo::permission(QFile::Permissions permissions) const
1057{
1058 Q_D(const QFileInfo);
1059 if (d->isDefaultConstructed)
1060 return false;
1061 return d->getFileFlags(QAbstractFileEngine::FileFlags((int)permissions)) == (uint)permissions;
1062}
1063
1064/*!
1065 Returns the complete OR-ed together combination of
1066 QFile::Permissions for the file.
1067*/
1068QFile::Permissions QFileInfo::permissions() const
1069{
1070 Q_D(const QFileInfo);
1071 if (d->isDefaultConstructed)
1072 return 0;
1073 return QFile::Permissions(d->getFileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask);
1074}
1075
1076
1077/*!
1078 Returns the file size in bytes. If the file does not exist or cannot be
1079 fetched, 0 is returned.
1080
1081 \sa exists()
1082*/
1083qint64 QFileInfo::size() const
1084{
1085 Q_D(const QFileInfo);
1086 if (d->isDefaultConstructed)
1087 return 0;
1088 if (!d->getCachedFlag(QFileInfoPrivate::CachedSize)) {
1089 d->setCachedFlag(QFileInfoPrivate::CachedSize);
1090 d->fileSize = d->fileEngine->size();
1091 }
1092 return d->fileSize;
1093}
1094
1095/*!
1096 Returns the date and time when the file was created.
1097
1098 On most Unix systems, this function returns the time of the last
1099 status change. A status change occurs when the file is created,
1100 but it also occurs whenever the user writes or sets inode
1101 information (for example, changing the file permissions).
1102
1103 If neither creation time nor "last status change" time are not
1104 available, returns the same as lastModified().
1105
1106 \sa lastModified() lastRead()
1107*/
1108QDateTime QFileInfo::created() const
1109{
1110 Q_D(const QFileInfo);
1111 if (d->isDefaultConstructed)
1112 return QDateTime();
1113 return d->getFileTime(QAbstractFileEngine::CreationTime);
1114}
1115
1116/*!
1117 Returns the date and time when the file was last modified.
1118
1119 \sa created() lastRead()
1120*/
1121QDateTime QFileInfo::lastModified() const
1122{
1123 Q_D(const QFileInfo);
1124 if (d->isDefaultConstructed)
1125 return QDateTime();
1126 return d->getFileTime(QAbstractFileEngine::ModificationTime);
1127}
1128
1129/*!
1130 Returns the date and time when the file was last read (accessed).
1131
1132 On platforms where this information is not available, returns the
1133 same as lastModified().
1134
1135 \sa created() lastModified()
1136*/
1137QDateTime QFileInfo::lastRead() const
1138{
1139 Q_D(const QFileInfo);
1140 if (d->isDefaultConstructed)
1141 return QDateTime();
1142 return d->getFileTime(QAbstractFileEngine::AccessTime);
1143}
1144
1145/*! \internal
1146 Detaches all internal data.
1147*/
1148void QFileInfo::detach()
1149{
1150 d_ptr.detach();
1151}
1152
1153/*!
1154 Returns true if caching is enabled; otherwise returns false.
1155
1156 \sa setCaching(), refresh()
1157*/
1158bool QFileInfo::caching() const
1159{
1160 Q_D(const QFileInfo);
1161 return d->cache_enabled;
1162}
1163
1164/*!
1165 If \a enable is true, enables caching of file information. If \a
1166 enable is false caching is disabled.
1167
1168 When caching is enabled, QFileInfo reads the file information from
1169 the file system the first time it's needed, but generally not
1170 later.
1171
1172 Caching is enabled by default.
1173
1174 \sa refresh(), caching()
1175*/
1176void QFileInfo::setCaching(bool enable)
1177{
1178 Q_D(QFileInfo);
1179 d->cache_enabled = enable;
1180}
1181
1182/*!
1183 \fn QString QFileInfo::baseName(bool complete)
1184
1185 Use completeBaseName() or the baseName() overload that takes no
1186 parameters instead.
1187*/
1188
1189/*!
1190 \fn QString QFileInfo::extension(bool complete = true) const
1191
1192 Use completeSuffix() or suffix() instead.
1193*/
1194
1195/*!
1196 \fn QString QFileInfo::absFilePath() const
1197
1198 Use absoluteFilePath() instead.
1199*/
1200
1201/*!
1202 \fn QString QFileInfo::dirPath(bool absPath) const
1203
1204 Use absolutePath() if the absolute path is wanted (\a absPath
1205 is true) or path() if it's not necessary (\a absPath is false).
1206*/
1207
1208/*!
1209 \fn bool QFileInfo::convertToAbs()
1210
1211 Use makeAbsolute() instead.
1212*/
1213
1214/*!
1215 \enum QFileInfo::Permission
1216
1217 \compat
1218
1219 \value ReadOwner
1220 \value WriteOwner
1221 \value ExeOwner
1222 \value ReadUser
1223 \value WriteUser
1224 \value ExeUser
1225 \value ReadGroup
1226 \value WriteGroup
1227 \value ExeGroup
1228 \value ReadOther
1229 \value WriteOther
1230 \value ExeOther
1231*/
1232
1233/*!
1234 \fn bool QFileInfo::permission(PermissionSpec permissions) const
1235 \compat
1236
1237 Use permission() instead.
1238*/
1239
1240/*!
1241 \typedef QFileInfoList
1242 \relates QFileInfo
1243
1244 Synonym for QList<QFileInfo>.
1245*/
1246
1247QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.