Debian Codegraph @

src/qurlinfo.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qurlinfo.h"
43
44 #include "qurl.h"
45 #include "qdir.h"
46 #include "limits.h"
47
48 QT_BEGIN_NAMESPACE
49
50 class QUrlInfoPrivate
51 {
52 public:
53 QUrlInfoPrivate() :
54 permissions(0),
55 size(0),
56 isDir(false),
57 isFile(true),
58 isSymLink(false),
59 isWritable(true),
60 isReadable(true),
61 isExecutable(false)
62 {}
63
64 QString name;
65 int permissions;
66 QString owner;
67 QString group;
68 qint64 size;
69
70 QDateTime lastModified;
71 QDateTime lastRead;
72 bool isDir;
73 bool isFile;
74 bool isSymLink;
75 bool isWritable;
76 bool isReadable;
77 bool isExecutable;
78 };
79
80
81 /*!
82 \class QUrlInfo
83 \brief The QUrlInfo class stores information about URLs.
84
85 \ingroup io
86 \ingroup network
87 \inmodule QtNetwork
88
89 The information about a URL that can be retrieved includes name(),
90 permissions(), owner(), group(), size(), lastModified(),
91 lastRead(), isDir(), isFile(), isSymLink(), isWritable(),
92 isReadable() and isExecutable().
93
94 You can create your own QUrlInfo objects passing in all the
95 relevant information in the constructor, and you can modify a
96 QUrlInfo; for each getter mentioned above there is an equivalent
97 setter. Note that setting values does not affect the underlying
98 resource that the QUrlInfo provides information about; for example
99 if you call setWritable(true) on a read-only resource the only
100 thing changed is the QUrlInfo object, not the resource.
101
102 \sa QUrl, {FTP Example}
103 */
104
105 /*!
106 \enum QUrlInfo::PermissionSpec
107
108 This enum is used by the permissions() function to report the
109 permissions of a file.
110
111 \value ReadOwner The file is readable by the owner of the file.
112 \value WriteOwner The file is writable by the owner of the file.
113 \value ExeOwner The file is executable by the owner of the file.
114 \value ReadGroup The file is readable by the group.
115 \value WriteGroup The file is writable by the group.
116 \value ExeGroup The file is executable by the group.
117 \value ReadOther The file is readable by anyone.
118 \value WriteOther The file is writable by anyone.
119 \value ExeOther The file is executable by anyone.
120 */
121
122 /*!
123 Constructs an invalid QUrlInfo object with default values.
124
125 \sa isValid()
126 */
127
128 QUrlInfo::QUrlInfo()
129 {
130 d = 0;
131 }
132
133 /*!
134 Copy constructor, copies \a ui to this URL info object.
135 */
136
137 QUrlInfo::QUrlInfo(const QUrlInfo &ui)
138 {
139 if (ui.d) {
140 d = new QUrlInfoPrivate;
141 *d = *ui.d;
142 } else {
143 d = 0;
144 }
145 }
146
147 /*!
148 Constructs a QUrlInfo object by specifying all the URL's
149 information.
150
151 The information that is passed is the \a name, file \a
152 permissions, \a owner and \a group and the file's \a size. Also
153 passed is the \a lastModified date/time and the \a lastRead
154 date/time. Flags are also passed, specifically, \a isDir, \a
155 isFile, \a isSymLink, \a isWritable, \a isReadable and \a
156 isExecutable.
157 */
158
159 QUrlInfo::QUrlInfo(const QString &name, int permissions, const QString &owner,
160 const QString &group, qint64 size, const QDateTime &lastModified,
161 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
162 bool isWritable, bool isReadable, bool isExecutable)
163 {
164 d = new QUrlInfoPrivate;
165 d->name = name;
166 d->permissions = permissions;
167 d->owner = owner;
168 d->group = group;
169 d->size = size;
170 d->lastModified = lastModified;
171 d->lastRead = lastRead;
172 d->isDir = isDir;
173 d->isFile = isFile;
174 d->isSymLink = isSymLink;
175 d->isWritable = isWritable;
176 d->isReadable = isReadable;
177 d->isExecutable = isExecutable;
178 }
179
180
181 /*!
182 Constructs a QUrlInfo object by specifying all the URL's
183 information.
184
185 The information that is passed is the \a url, file \a
186 permissions, \a owner and \a group and the file's \a size. Also
187 passed is the \a lastModified date/time and the \a lastRead
188 date/time. Flags are also passed, specifically, \a isDir, \a
189 isFile, \a isSymLink, \a isWritable, \a isReadable and \a
190 isExecutable.
191 */
192
193 QUrlInfo::QUrlInfo(const QUrl &url, int permissions, const QString &owner,
194 const QString &group, qint64 size, const QDateTime &lastModified,
195 const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
196 bool isWritable, bool isReadable, bool isExecutable)
197 {
198 d = new QUrlInfoPrivate;
199 d->name = QFileInfo(url.path()).fileName();
200 d->permissions = permissions;
201 d->owner = owner;
202 d->group = group;
203 d->size = size;
204 d->lastModified = lastModified;
205 d->lastRead = lastRead;
206 d->isDir = isDir;
207 d->isFile = isFile;
208 d->isSymLink = isSymLink;
209 d->isWritable = isWritable;
210 d->isReadable = isReadable;
211 d->isExecutable = isExecutable;
212 }
213
214
215 /*!
216 Sets the name of the URL to \a name. The name is the full text,
217 for example, "http://qt.nokia.com/doc/qurlinfo.html".
218
219 If you call this function for an invalid URL info, this function
220 turns it into a valid one.
221
222 \sa isValid()
223 */
224
225 void QUrlInfo::setName(const QString &name)
226 {
227 if (!d)
228 d = new QUrlInfoPrivate;
229 d->name = name;
230 }
231
232
233 /*!
234 If \a b is true then the URL is set to be a directory; if \a b is
235 false then the URL is set not to be a directory (which normally
236 means it is a file). (Note that a URL can refer to both a file and
237 a directory even though most file systems do not support this.)
238
239 If you call this function for an invalid URL info, this function
240 turns it into a valid one.
241
242 \sa isValid()
243 */
244
245 void QUrlInfo::setDir(bool b)
246 {
247 if (!d)
248 d = new QUrlInfoPrivate;
249 d->isDir = b;
250 }
251
252
253 /*!
254 If \a b is true then the URL is set to be a file; if \b is false
255 then the URL is set not to be a file (which normally means it is a
256 directory). (Note that a URL can refer to both a file and a
257 directory even though most file systems do not support this.)
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
265 void QUrlInfo::setFile(bool b)
266 {
267 if (!d)
268 d = new QUrlInfoPrivate;
269 d->isFile = b;
270 }
271
272
273 /*!
274 Specifies that the URL refers to a symbolic link if \a b is true
275 and that it does not if \a b is false.
276
277 If you call this function for an invalid URL info, this function
278 turns it into a valid one.
279
280 \sa isValid()
281 */
282
283 void QUrlInfo::setSymLink(bool b)
284 {
285 if (!d)
286 d = new QUrlInfoPrivate;
287 d->isSymLink = b;
288 }
289
290
291 /*!
292 Specifies that the URL is writable if \a b is true and not
293 writable if \a b is false.
294
295 If you call this function for an invalid URL info, this function
296 turns it into a valid one.
297
298 \sa isValid()
299 */
300
301 void QUrlInfo::setWritable(bool b)
302 {
303 if (!d)
304 d = new QUrlInfoPrivate;
305 d->isWritable = b;
306 }
307
308
309 /*!
310 Specifies that the URL is readable if \a b is true and not
311 readable if \a b is false.
312
313 If you call this function for an invalid URL info, this function
314 turns it into a valid one.
315
316 \sa isValid()
317 */
318
319 void QUrlInfo::setReadable(bool b)
320 {
321 if (!d)
322 d = new QUrlInfoPrivate;
323 d->isReadable = b;
324 }
325
326 /*!
327 Specifies that the owner of the URL is called \a s.
328
329 If you call this function for an invalid URL info, this function
330 turns it into a valid one.
331
332 \sa isValid()
333 */
334
335 void QUrlInfo::setOwner(const QString &s)
336 {
337 if (!d)
338 d = new QUrlInfoPrivate;
339 d->owner = s;
340 }
341
342 /*!
343 Specifies that the owning group of the URL is called \a s.
344
345 If you call this function for an invalid URL info, this function
346 turns it into a valid one.
347
348 \sa isValid()
349 */
350
351 void QUrlInfo::setGroup(const QString &s)
352 {
353 if (!d)
354 d = new QUrlInfoPrivate;
355 d->group = s;
356 }
357
358 /*!
359 Specifies the \a size of the URL.
360
361 If you call this function for an invalid URL info, this function
362 turns it into a valid one.
363
364 \sa isValid()
365 */
366
367 void QUrlInfo::setSize(qint64 size)
368 {
369 if (!d)
370 d = new QUrlInfoPrivate;
371 d->size = size;
372 }
373
374 /*!
375 Specifies that the URL has access permissions \a p.
376
377 If you call this function for an invalid URL info, this function
378 turns it into a valid one.
379
380 \sa isValid()
381 */
382
383 void QUrlInfo::setPermissions(int p)
384 {
385 if (!d)
386 d = new QUrlInfoPrivate;
387 d->permissions = p;
388 }
389
390 /*!
391 Specifies that the object the URL refers to was last modified at
392 \a dt.
393
394 If you call this function for an invalid URL info, this function
395 turns it into a valid one.
396
397 \sa isValid()
398 */
399
400 void QUrlInfo::setLastModified(const QDateTime &dt)
401 {
402 if (!d)
403 d = new QUrlInfoPrivate;
404 d->lastModified = dt;
405 }
406
407 /*!
408 \since 4.4
409
410 Specifies that the object the URL refers to was last read at
411 \a dt.
412
413 If you call this function for an invalid URL info, this function
414 turns it into a valid one.
415
416 \sa isValid()
417 */
418
419 void QUrlInfo::setLastRead(const QDateTime &dt)
420 {
421 if (!d)
422 d = new QUrlInfoPrivate;
423 d->lastRead = dt;
424 }
425
426 /*!
427 Destroys the URL info object.
428 */
429
430 QUrlInfo::~QUrlInfo()
431 {
432 delete d;
433 }
434
435 /*!
436 Assigns the values of \a ui to this QUrlInfo object.
437 */
438
439 QUrlInfo &QUrlInfo::operator=(const QUrlInfo &ui)
440 {
441 if (ui.d) {
442 if (!d)
443 d= new QUrlInfoPrivate;
444 *d = *ui.d;
445 } else {
446 delete d;
447 d = 0;
448 }
449 return *this;
450 }
451
452 /*!
453 Returns the file name of the URL.
454
455 \sa isValid()
456 */
457
458 QString QUrlInfo::name() const
459 {
460 if (!d)
461 return QString();
462 return d->name;
463 }
464
465 /*!
466 Returns the permissions of the URL. You can use the \c PermissionSpec flags
467 to test for certain permissions.
468
469 \sa isValid()
470 */
471
472 int QUrlInfo::permissions() const
473 {
474 if (!d)
475 return 0;
476 return d->permissions;
477 }
478
479 /*!
480 Returns the owner of the URL.
481
482 \sa isValid()
483 */
484
485 QString QUrlInfo::owner() const
486 {
487 if (!d)
488 return QString();
489 return d->owner;
490 }
491
492 /*!
493 Returns the group of the URL.
494
495 \sa isValid()
496 */
497
498 QString QUrlInfo::group() const
499 {
500 if (!d)
501 return QString();
502 return d->group;
503 }
504
505 /*!
506 Returns the size of the URL.
507
508 \sa isValid()
509 */
510
511 qint64 QUrlInfo::size() const
512 {
513 if (!d)
514 return 0;
515 return d->size;
516 }
517
518 /*!
519 Returns the last modification date of the URL.
520
521 \sa isValid()
522 */
523
524 QDateTime QUrlInfo::lastModified() const
525 {
526 if (!d)
527 return QDateTime();
528 return d->lastModified;
529 }
530
531 /*!
532 Returns the date when the URL was last read.
533
534 \sa isValid()
535 */
536
537 QDateTime QUrlInfo::lastRead() const
538 {
539 if (!d)
540 return QDateTime();
541 return d->lastRead;
542 }
543
544 /*!
545 Returns true if the URL is a directory; otherwise returns false.
546
547 \sa isValid()
548 */
549
550 bool QUrlInfo::isDir() const
551 {
552 if (!d)
553 return false;
554 return d->isDir;
555 }
556
557 /*!
558 Returns true if the URL is a file; otherwise returns false.
559
560 \sa isValid()
561 */
562
563 bool QUrlInfo::isFile() const
564 {
565 if (!d)
566 return false;
567 return d->isFile;
568 }
569
570 /*!
571 Returns true if the URL is a symbolic link; otherwise returns false.
572
573 \sa isValid()
574 */
575
576 bool QUrlInfo::isSymLink() const
577 {
578 if (!d)
579 return false;
580 return d->isSymLink;
581 }
582
583 /*!
584 Returns true if the URL is writable; otherwise returns false.
585
586 \sa isValid()
587 */
588
589 bool QUrlInfo::isWritable() const
590 {
591 if (!d)
592 return false;
593 return d->isWritable;
594 }
595
596 /*!
597 Returns true if the URL is readable; otherwise returns false.
598
599 \sa isValid()
600 */
601
602 bool QUrlInfo::isReadable() const
603 {
604 if (!d)
605 return false;
606 return d->isReadable;
607 }
608
609 /*!
610 Returns true if the URL is executable; otherwise returns false.
611
612 \sa isValid()
613 */
614
615 bool QUrlInfo::isExecutable() const
616 {
617 if (!d)
618 return false;
619 return d->isExecutable;
620 }
621
622 /*!
623 Returns true if \a i1 is greater than \a i2; otherwise returns
624 false. The objects are compared by the value, which is specified
625 by \a sortBy. This must be one of QDir::Name, QDir::Time or
626 QDir::Size.
627 */
628
629 bool QUrlInfo::greaterThan(const QUrlInfo &i1, const QUrlInfo &i2,
630 int sortBy)
631 {
632 switch (sortBy) {
633 case QDir::Name:
634 return i1.name() > i2.name();
635 case QDir::Time:
636 return i1.lastModified() > i2.lastModified();
637 case QDir::Size:
638 return i1.size() > i2.size();
639 default:
640 return false;
641 }
642 }
643
644 /*!
645 Returns true if \a i1 is less than \a i2; otherwise returns false.
646 The objects are compared by the value, which is specified by \a
647 sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
648 */
649
650 bool QUrlInfo::lessThan(const QUrlInfo &i1, const QUrlInfo &i2,
651 int sortBy)
652 {
653 return !greaterThan(i1, i2, sortBy);
654 }
655
656 /*!
657 Returns true if \a i1 equals to \a i2; otherwise returns false.
658 The objects are compared by the value, which is specified by \a
659 sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
660 */
661
662 bool QUrlInfo::equal(const QUrlInfo &i1, const QUrlInfo &i2,
663 int sortBy)
664 {
665 switch (sortBy) {
666 case QDir::Name:
667 return i1.name() == i2.name();
668 case QDir::Time:
669 return i1.lastModified() == i2.lastModified();
670 case QDir::Size:
671 return i1.size() == i2.size();
672 default:
673 return false;
674 }
675 }
676
677 /*!
678 Returns true if this QUrlInfo is equal to \a other; otherwise
679 returns false.
680
681 \sa lessThan(), equal()
682 */
683
684 bool QUrlInfo::operator==(const QUrlInfo &other) const
685 {
686 if (!d)
687 return other.d == 0;
688 if (!other.d)
689 return false;
690
691 return (d->name == other.d->name &&
692 d->permissions == other.d->permissions &&
693 d->owner == other.d->owner &&
694 d->group == other.d->group &&
695 d->size == other.d->size &&
696 d->lastModified == other.d->lastModified &&
697 d->lastRead == other.d->lastRead &&
698 d->isDir == other.d->isDir &&
699 d->isFile == other.d->isFile &&
700 d->isSymLink == other.d->isSymLink &&
701 d->isWritable == other.d->isWritable &&
702 d->isReadable == other.d->isReadable &&
703 d->isExecutable == other.d->isExecutable);
704 }
705
706 /*!
707 \fn bool QUrlInfo::operator!=(const QUrlInfo &other) const
708 \since 4.2
709
710 Returns true if this QUrlInfo is not equal to \a other; otherwise
711 returns false.
712
713 \sa lessThan(), equal()
714 */
715
716 /*!
717 Returns true if the URL info is valid; otherwise returns false.
718 Valid means that the QUrlInfo contains real information.
719
720 You should always check if the URL info is valid before relying on
721 the values.
722 */
723 bool QUrlInfo::isValid() const
724 {
725 return d != 0;
726 }
727
728 QT_END_NAMESPACE