source: vendor/trolltech/current/src/kernel/qurlinfo.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 17.5 KB
Line 
1/****************************************************************************
2** $Id: qurlinfo.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QUrlInfo class
5**
6** Created : 950429
7**
8** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
9**
10** This file is part of the kernel module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qurlinfo.h"
39
40#ifndef QT_NO_NETWORKPROTOCOL
41
42#include "qurloperator.h"
43#include "qdir.h"
44#include <limits.h>
45
46class QUrlInfoPrivate
47{
48public:
49 QUrlInfoPrivate() :
50 permissions(0),
51 size(0),
52 isDir(FALSE),
53 isFile(TRUE),
54 isSymLink(FALSE),
55 isWritable(TRUE),
56 isReadable(TRUE),
57 isExecutable(FALSE)
58 {}
59
60 QString name;
61 int permissions;
62 QString owner;
63 QString group;
64#if defined(QT_LARGEFILE_SUPPORT)
65 QIODevice::Offset size;
66#else
67 uint size;
68#endif
69 QDateTime lastModified;
70 QDateTime lastRead;
71 bool isDir;
72 bool isFile;
73 bool isSymLink;
74 bool isWritable;
75 bool isReadable;
76 bool isExecutable;
77};
78
79
80/*!
81 \class QUrlInfo qurlinfo.h
82 \brief The QUrlInfo class stores information about URLs.
83
84 \ingroup io
85 \ingroup misc
86
87 This class is just a container for storing information about URLs,
88 which is why all information must be passed in the constructor.
89
90 Unless you're reimplementing a network protocol you're unlikely to
91 create QUrlInfo objects yourself, but you may receive QUrlInfo
92 objects from functions, e.g. QUrlOperator::info().
93
94 The information that can be retrieved includes name(),
95 permissions(), owner(), group(), size(), lastModified(),
96 lastRead(), isDir(), isFile(), isSymLink(), isWritable(),
97 isReadable() and isExecutable().
98*/
99
100/*!
101 \enum QUrlInfo::PermissionSpec
102
103 This enum is used by the permissions() function to report the
104 permissions of a file.
105
106 \value ReadOwner The file is readable by the owner of the file.
107 \value WriteOwner The file is writable by the owner of the file.
108 \value ExeOwner The file is executable by the owner of the file.
109 \value ReadGroup The file is readable by the group.
110 \value WriteGroup The file is writable by the group.
111 \value ExeGroup The file is executable by the group.
112 \value ReadOther The file is readable by anyone.
113 \value WriteOther The file is writable by anyone.
114 \value ExeOther The file is executable by anyone.
115*/
116
117/*!
118 Constructs an invalid QUrlInfo object with default values.
119
120 \sa isValid()
121*/
122
123QUrlInfo::QUrlInfo()
124{
125 d = 0;
126}
127
128/*!
129 Constructs a QUrlInfo object with information about the file \a
130 file in the \a path. It tries to find the information about the \a
131 file in the QUrlOperator \a path.
132
133 If the information is not found, this constructor creates an
134 invalid QUrlInfo, i.e. isValid() returns FALSE. You should always
135 check if the URL info is valid before relying on the return values
136 of any getter functions.
137
138 If \a file is empty, it defaults to the QUrlOperator \a path, i.e.
139 to the directory.
140
141 \sa isValid() QUrlOperator::info()
142*/
143
144QUrlInfo::QUrlInfo( const QUrlOperator &path, const QString &file )
145{
146 QString file_ = file;
147 if ( file_.isEmpty() )
148 file_ = ".";
149
150 QUrlInfo inf = path.info( file_ );
151 if ( inf.d ) {
152 d = new QUrlInfoPrivate;
153 *d = *inf.d;
154 } else {
155 d = 0;
156 }
157}
158
159/*!
160 Copy constructor, copies \a ui to this URL info object.
161*/
162
163QUrlInfo::QUrlInfo( const QUrlInfo &ui )
164{
165 if ( ui.d ) {
166 d = new QUrlInfoPrivate;
167 *d = *ui.d;
168 } else {
169 d = 0;
170 }
171}
172
173/*!
174 Constructs a QUrlInfo object by specifying all the URL's
175 information.
176
177 The information that is passed is the \a name, file \a
178 permissions, \a owner and \a group and the file's \a size. Also
179 passed is the \a lastModified date/time and the \a lastRead
180 date/time. Flags are also passed, specifically, \a isDir, \a
181 isFile, \a isSymLink, \a isWritable, \a isReadable and \a
182 isExecutable.
183*/
184
185#if defined(QT_ABI_QT4)
186QUrlInfo::QUrlInfo( const QString &name, int permissions, const QString &owner,
187 const QString &group, QIODevice::Offset size, const QDateTime &lastModified,
188 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
189 bool isWritable, bool isReadable, bool isExecutable )
190#else
191QUrlInfo::QUrlInfo( const QString &name, int permissions, const QString &owner,
192 const QString &group, uint size, const QDateTime &lastModified,
193 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
194 bool isWritable, bool isReadable, bool isExecutable )
195#endif
196{
197 d = new QUrlInfoPrivate;
198 d->name = name;
199 d->permissions = permissions;
200 d->owner = owner;
201 d->group = group;
202 d->size = size;
203 d->lastModified = lastModified;
204 d->lastRead = lastRead;
205 d->isDir = isDir;
206 d->isFile = isFile;
207 d->isSymLink = isSymLink;
208 d->isWritable = isWritable;
209 d->isReadable = isReadable;
210 d->isExecutable = isExecutable;
211}
212
213
214/*!
215 Constructs a QUrlInfo object by specifying all the URL's
216 information.
217
218 The information that is passed is the \a url, file \a
219 permissions, \a owner and \a group and the file's \a size. Also
220 passed is the \a lastModified date/time and the \a lastRead
221 date/time. Flags are also passed, specifically, \a isDir, \a
222 isFile, \a isSymLink, \a isWritable, \a isReadable and \a
223 isExecutable.
224*/
225
226#if defined(QT_ABI_QT4)
227QUrlInfo::QUrlInfo( const QUrl &url, int permissions, const QString &owner,
228 const QString &group, QIODevice::Offset size, const QDateTime &lastModified,
229 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
230 bool isWritable, bool isReadable, bool isExecutable )
231#else
232QUrlInfo::QUrlInfo( const QUrl &url, int permissions, const QString &owner,
233 const QString &group, uint size, const QDateTime &lastModified,
234 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
235 bool isWritable, bool isReadable, bool isExecutable )
236#endif
237{
238 d = new QUrlInfoPrivate;
239 d->name = QFileInfo( url.path() ).fileName();
240 d->permissions = permissions;
241 d->owner = owner;
242 d->group = group;
243 d->size = size;
244 d->lastModified = lastModified;
245 d->lastRead = lastRead;
246 d->isDir = isDir;
247 d->isFile = isFile;
248 d->isSymLink = isSymLink;
249 d->isWritable = isWritable;
250 d->isReadable = isReadable;
251 d->isExecutable = isExecutable;
252}
253
254
255/*!
256 Sets the name of the URL to \a name. The name is the full text,
257 for example, "http://doc.trolltech.com/qurlinfo.html".
258
259 If you call this function for an invalid URL info, this function
260 turns it into a valid one.
261
262 \sa isValid()
263*/
264
265void QUrlInfo::setName( const QString &name )
266{
267 if ( !d )
268 d = new QUrlInfoPrivate;
269 d->name = name;
270}
271
272
273/*!
274 If \a b is TRUE then the URL is set to be a directory; if \b is
275 FALSE then the URL is set not to be a directory (which normally
276 means it is a file). (Note that a URL can refer to both a file and
277 a directory even though most file systems do not support this.)
278
279 If you call this function for an invalid URL info, this function
280 turns it into a valid one.
281
282 \sa isValid()
283*/
284
285void QUrlInfo::setDir( bool b )
286{
287 if ( !d )
288 d = new QUrlInfoPrivate;
289 d->isDir = b;
290}
291
292
293/*!
294 If \a b is TRUE then the URL is set to be a file; if \b is FALSE
295 then the URL is set not to be a file (which normally means it is a
296 directory). (Note that a URL can refer to both a file and a
297 directory even though most file systems do not support this.)
298
299 If you call this function for an invalid URL info, this function
300 turns it into a valid one.
301
302 \sa isValid()
303*/
304
305void QUrlInfo::setFile( bool b )
306{
307 if ( !d )
308 d = new QUrlInfoPrivate;
309 d->isFile = b;
310}
311
312
313/*!
314 Specifies that the URL refers to a symbolic link if \a b is TRUE
315 and that it does not if \a b is FALSE.
316
317 If you call this function for an invalid URL info, this function
318 turns it into a valid one.
319
320 \sa isValid()
321*/
322
323void QUrlInfo::setSymLink( bool b )
324{
325 if ( !d )
326 d = new QUrlInfoPrivate;
327 d->isSymLink = b;
328}
329
330
331/*!
332 Specifies that the URL is writable if \a b is TRUE and not
333 writable if \a b is FALSE.
334
335 If you call this function for an invalid URL info, this function
336 turns it into a valid one.
337
338 \sa isValid()
339*/
340
341void QUrlInfo::setWritable( bool b )
342{
343 if ( !d )
344 d = new QUrlInfoPrivate;
345 d->isWritable = b;
346}
347
348
349/*!
350 Specifies that the URL is readable if \a b is TRUE and not
351 readable if \a b is FALSE.
352
353 If you call this function for an invalid URL info, this function
354 turns it into a valid one.
355
356 \sa isValid()
357*/
358
359void QUrlInfo::setReadable( bool b )
360{
361 if ( !d )
362 d = new QUrlInfoPrivate;
363 d->isReadable = b;
364}
365
366/*!
367 Specifies that the owner of the URL is called \a s.
368
369 If you call this function for an invalid URL info, this function
370 turns it into a valid one.
371
372 \sa isValid()
373*/
374
375void QUrlInfo::setOwner( const QString &s )
376{
377 if ( !d )
378 d = new QUrlInfoPrivate;
379 d->owner = s;
380}
381
382/*!
383 Specifies that the owning group of the URL is called \a s.
384
385 If you call this function for an invalid URL info, this function
386 turns it into a valid one.
387
388 \sa isValid()
389*/
390
391void QUrlInfo::setGroup( const QString &s )
392{
393 if ( !d )
394 d = new QUrlInfoPrivate;
395 d->group = s;
396}
397
398/*!
399 Specifies the \a size of the URL.
400
401 If you call this function for an invalid URL info, this function
402 turns it into a valid one.
403
404 \sa isValid()
405*/
406
407#if defined(QT_ABI_QT4)
408void QUrlInfo::setSize( QIODevice::Offset size )
409#else
410void QUrlInfo::setSize( uint size )
411#endif
412{
413 if ( !d )
414 d = new QUrlInfoPrivate;
415 d->size = size;
416}
417
418
419// ### reggie - what's the permission type? As in Unix?
420
421/*!
422 Specifies that the URL has access permisions, \a p.
423
424 If you call this function for an invalid URL info, this function
425 turns it into a valid one.
426
427 \sa isValid()
428*/
429
430void QUrlInfo::setPermissions( int p )
431{
432 if ( !d )
433 d = new QUrlInfoPrivate;
434 d->permissions = p;
435}
436
437/*!
438 Specifies that the object the URL refers to was last modified at
439 \a dt.
440
441 If you call this function for an invalid URL info, this function
442 turns it into a valid one.
443
444 \sa isValid()
445*/
446
447void QUrlInfo::setLastModified( const QDateTime &dt )
448{
449 if ( !d )
450 d = new QUrlInfoPrivate;
451 d->lastModified = dt;
452}
453
454/*!
455 Destroys the URL info object.
456
457 The QUrlOperator object to which this URL referred (if any) is not
458 affected.
459*/
460
461QUrlInfo::~QUrlInfo()
462{
463 delete d;
464}
465
466/*!
467 Assigns the values of \a ui to this QUrlInfo object.
468*/
469
470QUrlInfo &QUrlInfo::operator=( const QUrlInfo &ui )
471{
472 if ( ui.d ) {
473 if ( !d )
474 d= new QUrlInfoPrivate;
475 *d = *ui.d;
476 } else {
477 delete d;
478 d = 0;
479 }
480 return *this;
481}
482
483/*!
484 Returns the file name of the URL.
485
486 \sa isValid()
487*/
488
489QString QUrlInfo::name() const
490{
491 if ( !d )
492 return QString::null;
493 return d->name;
494}
495
496/*!
497 Returns the permissions of the URL. You can use the \c PermissionSpec flags
498 to test for certain permissions.
499
500 \sa isValid()
501*/
502
503int QUrlInfo::permissions() const
504{
505 if ( !d )
506 return 0;
507 return d->permissions;
508}
509
510/*!
511 Returns the owner of the URL.
512
513 \sa isValid()
514*/
515
516QString QUrlInfo::owner() const
517{
518 if ( !d )
519 return QString::null;
520 return d->owner;
521}
522
523/*!
524 Returns the group of the URL.
525
526 \sa isValid()
527*/
528
529QString QUrlInfo::group() const
530{
531 if ( !d )
532 return QString::null;
533 return d->group;
534}
535
536/*!
537 Returns the size of the URL.
538
539 \sa isValid()
540*/
541
542#if defined(QT_ABI_QT4)
543QIODevice::Offset QUrlInfo::size() const
544#else
545uint QUrlInfo::size() const
546#endif
547{
548 if ( !d )
549 return 0;
550#if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4)
551 return d->size > UINT_MAX ? UINT_MAX : (uint)d->size;
552#else
553 return d->size;
554#endif
555}
556
557/*!
558 Returns the last modification date of the URL.
559
560 \sa isValid()
561*/
562
563QDateTime QUrlInfo::lastModified() const
564{
565 if ( !d )
566 return QDateTime();
567 return d->lastModified;
568}
569
570/*!
571 Returns the date when the URL was last read.
572
573 \sa isValid()
574*/
575
576QDateTime QUrlInfo::lastRead() const
577{
578 if ( !d )
579 return QDateTime();
580 return d->lastRead;
581}
582
583/*!
584 Returns TRUE if the URL is a directory; otherwise returns FALSE.
585
586 \sa isValid()
587*/
588
589bool QUrlInfo::isDir() const
590{
591 if ( !d )
592 return FALSE;
593 return d->isDir;
594}
595
596/*!
597 Returns TRUE if the URL is a file; otherwise returns FALSE.
598
599 \sa isValid()
600*/
601
602bool QUrlInfo::isFile() const
603{
604 if ( !d )
605 return FALSE;
606 return d->isFile;
607}
608
609/*!
610 Returns TRUE if the URL is a symbolic link; otherwise returns FALSE.
611
612 \sa isValid()
613*/
614
615bool QUrlInfo::isSymLink() const
616{
617 if ( !d )
618 return FALSE;
619 return d->isSymLink;
620}
621
622/*!
623 Returns TRUE if the URL is writable; otherwise returns FALSE.
624
625 \sa isValid()
626*/
627
628bool QUrlInfo::isWritable() const
629{
630 if ( !d )
631 return FALSE;
632 return d->isWritable;
633}
634
635/*!
636 Returns TRUE if the URL is readable; otherwise returns FALSE.
637
638 \sa isValid()
639*/
640
641bool QUrlInfo::isReadable() const
642{
643 if ( !d )
644 return FALSE;
645 return d->isReadable;
646}
647
648/*!
649 Returns TRUE if the URL is executable; otherwise returns FALSE.
650
651 \sa isValid()
652*/
653
654bool QUrlInfo::isExecutable() const
655{
656 if ( !d )
657 return FALSE;
658 return d->isExecutable;
659}
660
661/*!
662 Returns TRUE if \a i1 is greater than \a i2; otherwise returns
663 FALSE. The objects are compared by the value, which is specified
664 by \a sortBy. This must be one of QDir::Name, QDir::Time or
665 QDir::Size.
666*/
667
668bool QUrlInfo::greaterThan( const QUrlInfo &i1, const QUrlInfo &i2,
669 int sortBy )
670{
671 switch ( sortBy ) {
672 case QDir::Name:
673 return i1.name() > i2.name();
674 case QDir::Time:
675 return i1.lastModified() > i2.lastModified();
676 case QDir::Size:
677 return i1.size() > i2.size();
678 default:
679 return FALSE;
680 }
681}
682
683/*!
684 Returns TRUE if \a i1 is less than \a i2; otherwise returns FALSE.
685 The objects are compared by the value, which is specified by \a
686 sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
687*/
688
689bool QUrlInfo::lessThan( const QUrlInfo &i1, const QUrlInfo &i2,
690 int sortBy )
691{
692 return !greaterThan( i1, i2, sortBy );
693}
694
695/*!
696 Returns TRUE if \a i1 equals to \a i2; otherwise returns FALSE.
697 The objects are compared by the value, which is specified by \a
698 sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
699*/
700
701bool QUrlInfo::equal( const QUrlInfo &i1, const QUrlInfo &i2,
702 int sortBy )
703{
704 switch ( sortBy ) {
705 case QDir::Name:
706 return i1.name() == i2.name();
707 case QDir::Time:
708 return i1.lastModified() == i2.lastModified();
709 case QDir::Size:
710 return i1.size() == i2.size();
711 default:
712 return FALSE;
713 }
714}
715
716/*!
717 Compares this QUrlInfo with \a i and returns TRUE if they are
718 equal; otherwise returns FALSE.
719*/
720
721bool QUrlInfo::operator==( const QUrlInfo &i ) const
722{
723 if ( !d )
724 return i.d == 0;
725 if ( !i.d )
726 return FALSE;
727
728 return ( d->name == i.d->name &&
729 d->permissions == i.d->permissions &&
730 d->owner == i.d->owner &&
731 d->group == i.d->group &&
732 d->size == i.d->size &&
733 d->lastModified == i.d->lastModified &&
734 d->lastRead == i.d->lastRead &&
735 d->isDir == i.d->isDir &&
736 d->isFile == i.d->isFile &&
737 d->isSymLink == i.d->isSymLink &&
738 d->isWritable == i.d->isWritable &&
739 d->isReadable == i.d->isReadable &&
740 d->isExecutable == i.d->isExecutable );
741}
742
743/*!
744 Returns TRUE if the URL info is valid; otherwise returns FALSE.
745 Valid means that the QUrlInfo contains real information. For
746 example, a call to QUrlOperator::info() might return a an invalid
747 QUrlInfo, if no information about the requested entry is
748 available.
749
750 You should always check if the URL info is valid before relying on
751 the values.
752*/
753bool QUrlInfo::isValid() const
754{
755 return d != 0;
756}
757
758#endif // QT_NO_NETWORKPROTOCOL
Note: See TracBrowser for help on using the repository browser.