source: trunk/src/corelib/io/qabstractfileengine.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 36.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qabstractfileengine.h"
43#include "private/qabstractfileengine_p.h"
44#include "qdatetime.h"
45#include "qmutex.h"
46#include "qvariant.h"
47// built-in handlers
48#include "qfsfileengine.h"
49#include "qdiriterator.h"
50
51QT_BEGIN_NAMESPACE
52
53/*!
54 \class QAbstractFileEngineHandler
55 \reentrant
56
57 \brief The QAbstractFileEngineHandler class provides a way to register
58 custom file engines with your application.
59
60 \ingroup io
61 \since 4.1
62
63 QAbstractFileEngineHandler is a factory for creating QAbstractFileEngine
64 objects (file engines), which are used internally by QFile, QFileInfo, and
65 QDir when working with files and directories.
66
67 When you open a file, Qt chooses a suitable file engine by passing the
68 file name from QFile or QDir through an internal list of registered file
69 engine handlers. The first handler to recognize the file name is used to
70 create the engine. Qt provides internal file engines for working with
71 regular files and resources, but you can also register your own
72 QAbstractFileEngine subclasses.
73
74 To install an application-specific file engine, you subclass
75 QAbstractFileEngineHandler and reimplement create(). When you instantiate
76 the handler (e.g. by creating an instance on the stack or on the heap), it
77 will automatically register with Qt. (The latest registered handler takes
78 precedence over existing handlers.)
79
80 For example:
81
82 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 0
83
84 When the handler is destroyed, it is automatically removed from Qt.
85
86 The most common approach to registering a handler is to create an instance
87 as part of the start-up phase of your application. It is also possible to
88 limit the scope of the file engine handler to a particular area of
89 interest (e.g. a special file dialog that needs a custom file engine). By
90 creating the handler inside a local scope, you can precisely control the
91 area in which your engine will be applied without disturbing file
92 operations in other parts of your application.
93
94 \sa QAbstractFileEngine, QAbstractFileEngine::create()
95*/
96
97/*
98 All application-wide handlers are stored in this list. The mutex must be
99 acquired to ensure thread safety.
100 */
101Q_GLOBAL_STATIC_WITH_ARGS(QMutex, fileEngineHandlerMutex, (QMutex::Recursive))
102static bool qt_abstractfileenginehandlerlist_shutDown = false;
103class QAbstractFileEngineHandlerList : public QList<QAbstractFileEngineHandler *>
104{
105public:
106 ~QAbstractFileEngineHandlerList()
107 {
108 QMutexLocker locker(fileEngineHandlerMutex());
109 qt_abstractfileenginehandlerlist_shutDown = true;
110 }
111};
112Q_GLOBAL_STATIC(QAbstractFileEngineHandlerList, fileEngineHandlers)
113
114/*!
115 Constructs a file handler and registers it with Qt. Once created this
116 handler's create() function will be called (along with all the other
117 handlers) for any paths used. The most recently created handler that
118 recognizes the given path (i.e. that returns a QAbstractFileEngine) is
119 used for the new path.
120
121 \sa create()
122 */
123QAbstractFileEngineHandler::QAbstractFileEngineHandler()
124{
125 QMutexLocker locker(fileEngineHandlerMutex());
126 fileEngineHandlers()->prepend(this);
127}
128
129/*!
130 Destroys the file handler. This will automatically unregister the handler
131 from Qt.
132 */
133QAbstractFileEngineHandler::~QAbstractFileEngineHandler()
134{
135 QMutexLocker locker(fileEngineHandlerMutex());
136 // Remove this handler from the handler list only if the list is valid.
137 if (!qt_abstractfileenginehandlerlist_shutDown)
138 fileEngineHandlers()->removeAll(this);
139}
140
141/*!
142 \fn QAbstractFileEngine *QAbstractFileEngineHandler::create(const QString &fileName) const
143
144 Creates a file engine for file \a fileName. Returns 0 if this
145 file handler cannot handle \a fileName.
146
147 Example:
148
149 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 1
150
151 \sa QAbstractFileEngine::create()
152*/
153
154/*!
155 Creates and returns a QAbstractFileEngine suitable for processing \a
156 fileName.
157
158 You should not need to call this function; use QFile, QFileInfo or
159 QDir directly instead.
160
161 If you reimplemnt this function, it should only return file
162 engines that knows how to handle \a fileName; otherwise, it should
163 return 0.
164
165 \sa QAbstractFileEngineHandler
166*/
167QAbstractFileEngine *QAbstractFileEngine::create(const QString &fileName)
168{
169 QMutexLocker locker(fileEngineHandlerMutex());
170
171 // check for registered handlers that can load the file
172 for (int i = 0; i < fileEngineHandlers()->size(); i++) {
173 if (QAbstractFileEngine *ret = fileEngineHandlers()->at(i)->create(fileName))
174 return ret;
175 }
176
177#ifdef QT_BUILD_CORE_LIB
178 if (!fileName.startsWith(QLatin1Char('/'))) {
179 int prefixSeparator = fileName.indexOf(QLatin1Char(':'));
180 if (prefixSeparator > 1) {
181 QString prefix = fileName.left(prefixSeparator);
182 QString fileNameWithoutPrefix = fileName.mid(prefixSeparator + 1).prepend(QLatin1Char('/'));
183 const QStringList &paths = QDir::searchPaths(prefix);
184 for (int i = 0; i < paths.count(); i++) {
185 QString path = paths.at(i);
186 path.append(fileNameWithoutPrefix);
187 QAbstractFileEngine *engine = create(path);
188 if (engine && (engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::ExistsFlag)) {
189 return engine;
190 }
191 delete engine;
192 }
193 }
194 }
195#endif
196
197#ifdef QT_NO_FSFILEENGINE
198 return 0;
199#else
200 // fall back to regular file engine
201 return new QFSFileEngine(fileName);
202#endif
203}
204
205/*!
206 \class QAbstractFileEngine
207 \reentrant
208
209 \brief The QAbstractFileEngine class provides an abstraction for accessing
210 the filesystem.
211
212 \ingroup io
213 \since 4.1
214
215 The QDir, QFile, and QFileInfo classes all make use of a
216 QAbstractFileEngine internally. If you create your own QAbstractFileEngine
217 subclass (and register it with Qt by creating a QAbstractFileEngineHandler
218 subclass), your file engine will be used when the path is one that your
219 file engine handles.
220
221 A QAbstractFileEngine refers to one file or one directory. If the referent
222 is a file, the setFileName(), rename(), and remove() functions are
223 applicable. If the referent is a directory the mkdir(), rmdir(), and
224 entryList() functions are applicable. In all cases the caseSensitive(),
225 isRelativePath(), fileFlags(), ownerId(), owner(), and fileTime()
226 functions are applicable.
227
228 A QAbstractFileEngine subclass can be created to do synchronous network I/O
229 based file system operations, local file system operations, or to operate
230 as a resource system to access file based resources.
231
232 \sa QAbstractFileEngineHandler
233*/
234
235/*!
236 \enum QAbstractFileEngine::FileName
237
238 These values are used to request a file name in a particular
239 format.
240
241 \value DefaultName The same filename that was passed to the
242 QAbstractFileEngine.
243 \value BaseName The name of the file excluding the path.
244 \value PathName The path to the file excluding the base name.
245 \value AbsoluteName The absolute path to the file (including
246 the base name).
247 \value AbsolutePathName The absolute path to the file (excluding
248 the base name).
249 \value LinkName The full file name of the file that this file is a
250 link to. (This will be empty if this file is not a link.)
251 \value CanonicalName Often very similar to LinkName. Will return the true path to the file.
252 \value CanonicalPathName Same as CanonicalName, excluding the base name.
253 \value BundleName Returns the name of the bundle implies BundleType is set.
254
255 \sa fileName(), setFileName()
256*/
257
258/*!
259 \enum QAbstractFileEngine::FileFlag
260
261 The permissions and types of a file, suitable for OR'ing together.
262
263 \value ReadOwnerPerm The owner of the file has permission to read
264 it.
265 \value WriteOwnerPerm The owner of the file has permission to
266 write to it.
267 \value ExeOwnerPerm The owner of the file has permission to
268 execute it.
269 \value ReadUserPerm The current user has permission to read the
270 file.
271 \value WriteUserPerm The current user has permission to write to
272 the file.
273 \value ExeUserPerm The current user has permission to execute the
274 file.
275 \value ReadGroupPerm Members of the current user's group have
276 permission to read the file.
277 \value WriteGroupPerm Members of the current user's group have
278 permission to write to the file.
279 \value ExeGroupPerm Members of the current user's group have
280 permission to execute the file.
281 \value ReadOtherPerm All users have permission to read the file.
282 \value WriteOtherPerm All users have permission to write to the
283 file.
284 \value ExeOtherPerm All users have permission to execute the file.
285
286 \value LinkType The file is a link to another file (or link) in
287 the file system (i.e. not a file or directory).
288 \value FileType The file is a regular file to the file system
289 (i.e. not a link or directory)
290 \value BundleType The file is a Mac OS X bundle implies DirectoryType
291 \value DirectoryType The file is a directory in the file system
292 (i.e. not a link or file).
293
294 \value HiddenFlag The file is hidden.
295 \value ExistsFlag The file actually exists in the file system.
296 \value RootFlag The file or the file pointed to is the root of the filesystem.
297 \value LocalDiskFlag The file resides on the local disk and can be passed to standard file functions.
298 \value Refresh Passing this flag will force the file engine to refresh all flags.
299
300 \omitvalue PermsMask
301 \omitvalue TypesMask
302 \omitvalue FlagsMask
303 \omitvalue FileInfoAll
304
305 \sa fileFlags(), setFileName()
306*/
307
308/*!
309 \enum QAbstractFileEngine::FileTime
310
311 These are used by the fileTime() function.
312
313 \value CreationTime When the file was created.
314 \value ModificationTime When the file was most recently modified.
315 \value AccessTime When the file was most recently accessed (e.g.
316 read or written to).
317
318 \sa setFileName()
319*/
320
321/*!
322 \enum QAbstractFileEngine::FileOwner
323
324 \value OwnerUser The user who owns the file.
325 \value OwnerGroup The group who owns the file.
326
327 \sa owner(), ownerId(), setFileName()
328*/
329
330/*!
331 Constructs a new QAbstractFileEngine that does not refer to any file or directory.
332
333 \sa setFileName()
334 */
335QAbstractFileEngine::QAbstractFileEngine() : d_ptr(new QAbstractFileEnginePrivate)
336{
337 d_ptr->q_ptr = this;
338}
339
340/*!
341 \internal
342
343 Constructs a QAbstractFileEngine.
344 */
345QAbstractFileEngine::QAbstractFileEngine(QAbstractFileEnginePrivate &dd) : d_ptr(&dd)
346{
347 d_ptr->q_ptr = this;
348}
349
350/*!
351 Destroys the QAbstractFileEngine.
352 */
353QAbstractFileEngine::~QAbstractFileEngine()
354{
355 delete d_ptr;
356 d_ptr = 0;
357}
358
359/*!
360 \fn bool QAbstractFileEngine::open(QIODevice::OpenMode mode)
361
362 Opens the file in the specified \a mode. Returns true if the file
363 was successfully opened; otherwise returns false.
364
365 The \a mode is an OR combination of QIODevice::OpenMode and
366 QIODevice::HandlingMode values.
367*/
368bool QAbstractFileEngine::open(QIODevice::OpenMode openMode)
369{
370 Q_UNUSED(openMode);
371 return false;
372}
373
374/*!
375 Closes the file, returning true if successful; otherwise returns false.
376
377 The default implementation always returns false.
378*/
379bool QAbstractFileEngine::close()
380{
381 return false;
382}
383
384/*!
385 Flushes the open file, returning true if successful; otherwise returns
386 false.
387
388 The default implementation always returns false.
389*/
390bool QAbstractFileEngine::flush()
391{
392 return false;
393}
394
395/*!
396 Returns the size of the file.
397*/
398qint64 QAbstractFileEngine::size() const
399{
400 return 0;
401}
402
403/*!
404 Returns the current file position.
405
406 This is the position of the data read/write head of the file.
407*/
408qint64 QAbstractFileEngine::pos() const
409{
410 return 0;
411}
412
413/*!
414 \fn bool QAbstractFileEngine::seek(qint64 offset)
415
416 Sets the file position to the given \a offset. Returns true if
417 the position was successfully set; otherwise returns false.
418
419 The offset is from the beginning of the file, unless the
420 file is sequential.
421
422 \sa isSequential()
423*/
424bool QAbstractFileEngine::seek(qint64 pos)
425{
426 Q_UNUSED(pos);
427 return false;
428}
429
430/*!
431 Returns true if the file is a sequential access device; returns
432 false if the file is a direct access device.
433
434 Operations involving size() and seek(int) are not valid on
435 sequential devices.
436*/
437bool QAbstractFileEngine::isSequential() const
438{
439 return false;
440}
441
442/*!
443 Requests that the file is deleted from the file system. If the
444 operation succeeds return true; otherwise return false.
445
446 This virtual function must be reimplemented by all subclasses.
447
448 \sa setFileName() rmdir()
449 */
450bool QAbstractFileEngine::remove()
451{
452 return false;
453}
454
455/*!
456 Copies the contents of this file to a file with the name \a newName.
457 Returns true on success; otherwise, false is returned.
458*/
459bool QAbstractFileEngine::copy(const QString &newName)
460{
461 Q_UNUSED(newName);
462 return false;
463}
464
465/*!
466 Requests that the file be renamed to \a newName in the file
467 system. If the operation succeeds return true; otherwise return
468 false.
469
470 This virtual function must be reimplemented by all subclasses.
471
472 \sa setFileName()
473 */
474bool QAbstractFileEngine::rename(const QString &newName)
475{
476 Q_UNUSED(newName);
477 return false;
478}
479
480/*!
481 Creates a link from the file currently specified by fileName() to
482 \a newName. What a link is depends on the underlying filesystem
483 (be it a shortcut on Windows or a symbolic link on Unix). Returns
484 true if successful; otherwise returns false.
485*/
486bool QAbstractFileEngine::link(const QString &newName)
487{
488 Q_UNUSED(newName);
489 return false;
490}
491
492/*!
493 Requests that the directory \a dirName be created. If
494 \a createParentDirectories is true, then any sub-directories in \a dirName
495 that don't exist must be created. If \a createParentDirectories is false then
496 any sub-directories in \a dirName must already exist for the function to
497 succeed. If the operation succeeds return true; otherwise return
498 false.
499
500 This virtual function must be reimplemented by all subclasses.
501
502 \sa setFileName() rmdir() isRelativePath()
503 */
504bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
505{
506 Q_UNUSED(dirName);
507 Q_UNUSED(createParentDirectories);
508 return false;
509}
510
511/*!
512 Requests that the directory \a dirName is deleted from the file
513 system. When \a recurseParentDirectories is true, then any empty
514 parent-directories in \a dirName must also be deleted. If
515 \a recurseParentDirectories is false, only the \a dirName leaf-node
516 should be deleted. In most file systems a directory cannot be deleted
517 using this function if it is non-empty. If the operation succeeds
518 return true; otherwise return false.
519
520 This virtual function must be reimplemented by all subclasses.
521
522 \sa setFileName() remove() mkdir() isRelativePath()
523 */
524bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
525{
526 Q_UNUSED(dirName);
527 Q_UNUSED(recurseParentDirectories);
528 return false;
529}
530
531/*!
532 Requests that the file be set to size \a size. If \a size is larger
533 than the current file then it is filled with 0's, if smaller it is
534 simply truncated. If the operations succceeds return true; otherwise
535 return false;
536
537 This virtual function must be reimplemented by all subclasses.
538
539 \sa size()
540*/
541bool QAbstractFileEngine::setSize(qint64 size)
542{
543 Q_UNUSED(size);
544 return false;
545}
546
547/*!
548 Should return true if the underlying file system is case-sensitive;
549 otherwise return false.
550
551 This virtual function must be reimplemented by all subclasses.
552 */
553bool QAbstractFileEngine::caseSensitive() const
554{
555 return false;
556}
557
558/*!
559 Return true if the file referred to by this file engine has a
560 relative path; otherwise return false.
561
562 This virtual function must be reimplemented by all subclasses.
563
564 \sa setFileName()
565 */
566bool QAbstractFileEngine::isRelativePath() const
567{
568 return false;
569}
570
571/*!
572 Requests that a list of all the files matching the \a filters
573 list based on the \a filterNames in the file engine's directory
574 are returned.
575
576 Should return an empty list if the file engine refers to a file
577 rather than a directory, or if the directory is unreadable or does
578 not exist or if nothing matches the specifications.
579
580 This virtual function must be reimplemented by all subclasses.
581
582 \sa setFileName()
583 */
584QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
585{
586 QStringList ret;
587 QDirIterator it(fileName(), filterNames, filters);
588 while (it.hasNext()) {
589 it.next();
590 ret << it.fileName();
591 }
592 return ret;
593}
594
595/*!
596 This function should return the set of OR'd flags that are true
597 for the file engine's file, and that are in the \a type's OR'd
598 members.
599
600 In your reimplementation you can use the \a type argument as an
601 optimization hint and only return the OR'd set of members that are
602 true and that match those in \a type; in other words you can
603 ignore any members not mentioned in \a type, thus avoiding some
604 potentially expensive lookups or system calls.
605
606 This virtual function must be reimplemented by all subclasses.
607
608 \sa setFileName()
609*/
610QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
611{
612 Q_UNUSED(type);
613 return 0;
614}
615
616/*!
617 Requests that the file's permissions be set to \a perms. The argument
618 perms will be set to the OR-ed together combination of
619 QAbstractFileEngine::FileInfo, with only the QAbstractFileEngine::PermsMask being
620 honored. If the operations succceeds return true; otherwise return
621 false;
622
623 This virtual function must be reimplemented by all subclasses.
624
625 \sa size()
626*/
627bool QAbstractFileEngine::setPermissions(uint perms)
628{
629 Q_UNUSED(perms);
630 return false;
631}
632
633/*!
634 Return the file engine's current file name in the format
635 specified by \a file.
636
637 If you don't handle some \c FileName possibilities, return the
638 file name set in setFileName() when an unhandled format is
639 requested.
640
641 This virtual function must be reimplemented by all subclasses.
642
643 \sa setFileName(), FileName
644 */
645QString QAbstractFileEngine::fileName(FileName file) const
646{
647 Q_UNUSED(file);
648 return QString();
649}
650
651/*!
652 If \a owner is \c OwnerUser return the ID of the user who owns
653 the file. If \a owner is \c OwnerGroup return the ID of the group
654 that own the file. If you can't determine the owner return -2.
655
656 This virtual function must be reimplemented by all subclasses.
657
658 \sa owner() setFileName(), FileOwner
659 */
660uint QAbstractFileEngine::ownerId(FileOwner owner) const
661{
662 Q_UNUSED(owner);
663 return 0;
664}
665
666/*!
667 If \a owner is \c OwnerUser return the name of the user who owns
668 the file. If \a owner is \c OwnerGroup return the name of the group
669 that own the file. If you can't determine the owner return
670 QString().
671
672 This virtual function must be reimplemented by all subclasses.
673
674 \sa ownerId() setFileName(), FileOwner
675 */
676QString QAbstractFileEngine::owner(FileOwner owner) const
677{
678 Q_UNUSED(owner);
679 return QString();
680}
681
682/*!
683 If \a time is \c CreationTime, return when the file was created.
684 If \a time is \c ModificationTime, return when the file was most
685 recently modified. If \a time is \c AccessTime, return when the
686 file was most recently accessed (e.g. read or written).
687 If the time cannot be determined return QDateTime() (an invalid
688 date time).
689
690 This virtual function must be reimplemented by all subclasses.
691
692 \sa setFileName(), QDateTime, QDateTime::isValid(), FileTime
693 */
694QDateTime QAbstractFileEngine::fileTime(FileTime time) const
695{
696 Q_UNUSED(time);
697 return QDateTime();
698}
699
700/*!
701 Sets the file engine's file name to \a file. This file name is the
702 file that the rest of the virtual functions will operate on.
703
704 This virtual function must be reimplemented by all subclasses.
705
706 \sa rename()
707 */
708void QAbstractFileEngine::setFileName(const QString &file)
709{
710 Q_UNUSED(file);
711}
712
713/*!
714 Returns the native file handle for this file engine. This handle must be
715 used with care; its value and type are platform specific, and using it
716 will most likely lead to non-portable code.
717*/
718int QAbstractFileEngine::handle() const
719{
720 return -1;
721}
722
723/*!
724 \since 4.3
725
726 Returns true if the current position is at the end of the file; otherwise,
727 returns false.
728
729 This function bases its behavior on calling extension() with
730 AtEndExtension. If the engine does not support this extension, false is
731 returned.
732
733 \sa extension(), supportsExtension(), QFile::atEnd()
734*/
735bool QAbstractFileEngine::atEnd() const
736{
737 return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
738}
739
740/*!
741 \since 4.4
742
743 Maps \a size bytes of the file into memory starting at \a offset.
744 Returns a pointer to the memory if successful; otherwise returns false
745 if, for example, an error occurs.
746
747 This function bases its behavior on calling extension() with
748 MapExtensionOption. If the engine does not support this extension, 0 is
749 returned.
750
751 \a flags is currently not used, but could be used in the future.
752
753 \sa unmap(), supportsExtension()
754 */
755
756uchar *QAbstractFileEngine::map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
757{
758 MapExtensionOption option;
759 option.offset = offset;
760 option.size = size;
761 option.flags = flags;
762 MapExtensionReturn r;
763 if (!extension(MapExtension, &option, &r))
764 return 0;
765 return r.address;
766}
767
768/*!
769 \since 4.4
770
771 Unmaps the memory \a address. Returns true if the unmap succeeds; otherwise
772 returns false.
773
774 This function bases its behavior on calling extension() with
775 UnMapExtensionOption. If the engine does not support this extension, false is
776 returned.
777
778 \sa map(), supportsExtension()
779 */
780bool QAbstractFileEngine::unmap(uchar *address)
781{
782 UnMapExtensionOption options;
783 options.address = address;
784 return extension(UnMapExtension, &options);
785}
786
787/*!
788 \since 4.3
789 \class QAbstractFileEngineIterator
790 \brief The QAbstractFileEngineIterator class provides an iterator
791 interface for custom file engines.
792
793 If all you want is to iterate over entries in a directory, see
794 QDirIterator instead. This class is only for custom file engine authors.
795
796 QAbstractFileEngineIterator is a unidirectional single-use virtual
797 iterator that plugs into QDirIterator, providing transparent proxy
798 iteration for custom file engines.
799
800 You can subclass QAbstractFileEngineIterator to provide an iterator when
801 writing your own file engine. To plug the iterator into your file system,
802 you simply return an instance of this subclass from a reimplementation of
803 QAbstractFileEngine::beginEntryList().
804
805 Example:
806
807 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 2
808
809 QAbstractFileEngineIterator is associated with a path, name filters, and
810 entry filters. The path is the directory that the iterator lists entries
811 in. The name filters and entry filters are provided for file engines that
812 can optimize directory listing at the iterator level (e.g., network file
813 systems that need to minimize network traffic), but they can also be
814 ignored by the iterator subclass; QAbstractFileEngineIterator already
815 provides the required filtering logics in the matchesFilters() function.
816 You can call dirName() to get the directory name, nameFilters() to get a
817 stringlist of name filters, and filters() to get the entry filters.
818
819 The pure virual function hasNext() returns true if the current directory
820 has at least one more entry (i.e., the directory name is valid and
821 accessible, and we have not reached the end of the entry list), and false
822 otherwise. Reimplement next() to seek to the next entry.
823
824 The pure virtual function currentFileName() returns the name of the
825 current entry without advancing the iterator. The currentFilePath()
826 function is provided for convenience; it returns the full path of the
827 current entry.
828
829 Here is an example of how to implement an interator that returns each of
830 three fixed entries in sequence.
831
832 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 3
833
834 Note: QAbstractFileEngineIterator does not deal with QDir::IteratorFlags;
835 it simply returns entries for a single directory.
836
837 \sa QDirIterator
838*/
839
840/*!
841 \enum QAbstractFileEngineIterator::EntryInfoType
842 \internal
843
844 This enum describes the different types of information that can be
845 requested through the QAbstractFileEngineIterator::entryInfo() function.
846*/
847
848/*!
849 \typedef QAbstractFileEngine::Iterator
850 \since 4.3
851 \relates QAbstractFileEngine
852
853 Synonym for QAbstractFileEngineIterator.
854*/
855
856class QAbstractFileEngineIteratorPrivate
857{
858public:
859 QString path;
860 QDir::Filters filters;
861 QStringList nameFilters;
862 QFileInfo fileInfo;
863};
864
865/*!
866 Constructs a QAbstractFileEngineIterator, using the entry filters \a
867 filters, and wildcard name filters \a nameFilters.
868*/
869QAbstractFileEngineIterator::QAbstractFileEngineIterator(QDir::Filters filters,
870 const QStringList &nameFilters)
871 : d(new QAbstractFileEngineIteratorPrivate)
872{
873 d->nameFilters = nameFilters;
874 d->filters = filters;
875}
876
877/*!
878 Destroys the QAbstractFileEngineIterator.
879
880 \sa QDirIterator
881*/
882QAbstractFileEngineIterator::~QAbstractFileEngineIterator()
883{
884 delete d;
885}
886
887/*!
888 Returns the path for this iterator. QDirIterator is responsible for
889 assigning this path; it cannot change during the iterator's lifetime.
890
891 \sa nameFilters(), filters()
892*/
893QString QAbstractFileEngineIterator::path() const
894{
895 return d->path;
896}
897
898/*!
899 \internal
900
901 Sets the iterator path to \a path. This function is called from within
902 QDirIterator.
903*/
904void QAbstractFileEngineIterator::setPath(const QString &path)
905{
906 d->path = path;
907}
908
909/*!
910 Returns the name filters for this iterator.
911
912 \sa QDir::nameFilters(), filters(), path()
913*/
914QStringList QAbstractFileEngineIterator::nameFilters() const
915{
916 return d->nameFilters;
917}
918
919/*!
920 Returns the entry filters for this iterator.
921
922 \sa QDir::filter(), nameFilters(), path()
923*/
924QDir::Filters QAbstractFileEngineIterator::filters() const
925{
926 return d->filters;
927}
928
929/*!
930 \fn QString QAbstractFileEngineIterator::currentFileName() const = 0
931
932 This pure virtual function returns the name of the current directory
933 entry, excluding the path.
934
935 \sa currentFilePath()
936*/
937
938/*!
939 Returns the path to the current directory entry. It's the same as
940 prepending path() to the return value of currentFileName().
941
942 \sa currentFileName()
943*/
944QString QAbstractFileEngineIterator::currentFilePath() const
945{
946 QString name = currentFileName();
947 if (!name.isNull()) {
948 QString tmp = path();
949 if (!tmp.isEmpty()) {
950 if (!tmp.endsWith(QLatin1Char('/')))
951 tmp.append(QLatin1Char('/'));
952 name.prepend(tmp);
953 }
954 }
955 return name;
956}
957
958/*!
959 The virtual function returns a QFileInfo for the current directory
960 entry. This function is provided for convenience. It can also be slightly
961 faster that creating a QFileInfo object yourself, as the object returned
962 by this function might contain cached information that QFileInfo otherwise
963 would have to access through the file engine.
964
965 \sa currentFileName()
966*/
967QFileInfo QAbstractFileEngineIterator::currentFileInfo() const
968{
969 QString path = currentFilePath();
970 if (d->fileInfo.filePath() != path)
971 d->fileInfo.setFile(path);
972
973 // return a shallow copy
974 return d->fileInfo;
975}
976
977/*!
978 \internal
979
980 Returns the entry info \a type for this iterator's current directory entry
981 as a QVariant. If \a type is undefined for this entry, a null QVariant is
982 returned.
983
984 \sa QAbstractFileEngine::beginEntryList(), QDir::beginEntryList()
985*/
986QVariant QAbstractFileEngineIterator::entryInfo(EntryInfoType type) const
987{
988 Q_UNUSED(type)
989 return QVariant();
990}
991
992/*!
993 \fn virtual QString QAbstractFileEngineIterator::next() = 0
994
995 This pure virtual function advances the iterator to the next directory
996 entry, and returns the file path to the current entry.
997
998 This function can optionally make use of nameFilters() and filters() to
999 optimize its performance.
1000
1001 Reimplement this function in a subclass to advance the iterator.
1002
1003 \sa QDirIterator::next()
1004*/
1005
1006/*!
1007 \fn virtual bool QAbstractFileEngineIterator::hasNext() const = 0
1008
1009 This pure virtual function returns true if there is at least one more
1010 entry in the current directory (i.e., the iterator path is valid and
1011 accessible, and the iterator has not reached the end of the entry list).
1012
1013 \sa QDirIterator::hasNext()
1014*/
1015
1016/*!
1017 Returns an instance of a QAbstractFileEngineIterator using \a filters for
1018 entry filtering and \a filterNames for name filtering. This function is
1019 called by QDirIterator to initiate directory iteration.
1020
1021 QDirIterator takes ownership of the returned instance, and deletes it when
1022 it's done.
1023
1024 \sa QDirIterator
1025*/
1026QAbstractFileEngine::Iterator *QAbstractFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
1027{
1028 Q_UNUSED(filters);
1029 Q_UNUSED(filterNames);
1030 return 0;
1031}
1032
1033/*!
1034 \internal
1035*/
1036QAbstractFileEngine::Iterator *QAbstractFileEngine::endEntryList()
1037{
1038 return 0;
1039}
1040
1041/*!
1042 Reads a number of characters from the file into \a data. At most
1043 \a maxlen characters will be read.
1044
1045 Returns -1 if a fatal error occurs, or 0 if there are no bytes to
1046 read.
1047*/
1048qint64 QAbstractFileEngine::read(char *data, qint64 maxlen)
1049{
1050 Q_UNUSED(data);
1051 Q_UNUSED(maxlen);
1052 return -1;
1053}
1054
1055/*!
1056 Writes \a len bytes from \a data to the file. Returns the number
1057 of characters written on success; otherwise returns -1.
1058*/
1059qint64 QAbstractFileEngine::write(const char *data, qint64 len)
1060{
1061 Q_UNUSED(data);
1062 Q_UNUSED(len);
1063 return -1;
1064}
1065
1066/*!
1067 This function reads one line, terminated by a '\n' character, from the
1068 file info \a data. At most \a maxlen characters will be read. The
1069 end-of-line character is included.
1070*/
1071qint64 QAbstractFileEngine::readLine(char *data, qint64 maxlen)
1072{
1073 qint64 readSoFar = 0;
1074 while (readSoFar < maxlen) {
1075 char c;
1076 qint64 readResult = read(&c, 1);
1077 if (readResult <= 0)
1078 return (readSoFar > 0) ? readSoFar : -1;
1079 ++readSoFar;
1080 *data++ = c;
1081 if (c == '\n')
1082 return readSoFar;
1083 }
1084 return readSoFar;
1085}
1086
1087/*!
1088 \enum QAbstractFileEngine::Extension
1089 \since 4.3
1090
1091 This enum describes the types of extensions that the file engine can
1092 support. Before using these extensions, you must verify that the extension
1093 is supported (i.e., call supportsExtension()).
1094
1095 \value AtEndExtension Whether the current file position is at the end of
1096 the file or not. This extension allows file engines that implement local
1097 buffering to report end-of-file status without having to check the size of
1098 the file. It is also useful for sequential files, where the size of the
1099 file cannot be used to determine whether or not you have reached the end.
1100 This extension returns true if the file is at the end; otherwise it returns
1101 false. The input and output arguments to extension() are ignored.
1102
1103 \value FastReadLineExtension Whether the file engine provides a
1104 fast implementation for readLine() or not. If readLine() remains
1105 unimplemented in the file engine, QAbstractFileEngine will provide
1106 an implementation based on calling read() repeatedly. If
1107 supportsExtension() returns false for this extension, however,
1108 QIODevice can provide a faster implementation by making use of its
1109 internal buffer. For engines that already provide a fast readLine()
1110 implementation, returning false for this extension can avoid
1111 unnnecessary double-buffering in QIODevice.
1112
1113 \value MapExtension Whether the file engine provides the ability to map
1114 a file to memory.
1115
1116 \value UnMapExtension Whether the file engine provides the ability to
1117 unmap memory that was previously mapped.
1118*/
1119
1120/*!
1121 \class QAbstractFileEngine::ExtensionOption
1122 \since 4.3
1123 \brief provides an extended input argument to QAbstractFileEngine's
1124 extension support.
1125
1126 \sa QAbstractFileEngine::extension()
1127*/
1128
1129/*!
1130 \class QAbstractFileEngine::ExtensionReturn
1131 \since 4.3
1132 \brief provides an extended output argument to QAbstractFileEngine's
1133 extension support.
1134
1135 \sa QAbstractFileEngine::extension()
1136*/
1137
1138/*!
1139 \since 4.3
1140
1141 This virtual function can be reimplemented in a QAbstractFileEngine
1142 subclass to provide support for extensions. The \a option argument is
1143 provided as input to the extension, and this function can store output
1144 results in \a output.
1145
1146 The behavior of this function is determined by \a extension; see the
1147 Extension documentation for details.
1148
1149 You can call supportsExtension() to check if an extension is supported by
1150 the file engine.
1151
1152 By default, no extensions are supported, and this function returns false.
1153
1154 \sa supportsExtension(), Extension
1155*/
1156bool QAbstractFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
1157{
1158 Q_UNUSED(extension);
1159 Q_UNUSED(option);
1160 Q_UNUSED(output);
1161 return false;
1162}
1163
1164/*!
1165 \since 4.3
1166
1167 This virtual function returns true if the file engine supports \a
1168 extension; otherwise, false is returned. By default, no extensions are
1169 supported.
1170
1171 \sa extension()
1172*/
1173bool QAbstractFileEngine::supportsExtension(Extension extension) const
1174{
1175 Q_UNUSED(extension);
1176 return false;
1177}
1178
1179/*!
1180 Returns the QFile::FileError that resulted from the last failed
1181 operation. If QFile::UnspecifiedError is returned, QFile will
1182 use its own idea of the error status.
1183
1184 \sa QFile::FileError, errorString()
1185 */
1186QFile::FileError QAbstractFileEngine::error() const
1187{
1188 Q_D(const QAbstractFileEngine);
1189 return d->fileError;
1190}
1191
1192/*!
1193 Returns the human-readable message appropriate to the current error
1194 reported by error(). If no suitable string is available, an
1195 empty string is returned.
1196
1197 \sa error()
1198 */
1199QString QAbstractFileEngine::errorString() const
1200{
1201 Q_D(const QAbstractFileEngine);
1202 return d->errorString;
1203}
1204
1205/*!
1206 Sets the error type to \a error, and the error string to \a errorString.
1207 Call this function to set the error values returned by the higher-level
1208 classes.
1209
1210 \sa QFile::error(), QIODevice::errorString(), QIODevice::setErrorString()
1211*/
1212void QAbstractFileEngine::setError(QFile::FileError error, const QString &errorString)
1213{
1214 Q_D(QAbstractFileEngine);
1215 d->fileError = error;
1216 d->errorString = errorString;
1217}
1218
1219QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.