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