qurlinfo.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  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 The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #include "qurlinfo.h"
  40. #include "qurl.h"
  41. #include "qdir.h"
  42. #include <limits.h>
  43. QT_BEGIN_NAMESPACE
  44. class QUrlInfoPrivate
  45. {
  46. public:
  47. QUrlInfoPrivate() :
  48. permissions(0),
  49. size(0),
  50. isDir(false),
  51. isFile(true),
  52. isSymLink(false),
  53. isWritable(true),
  54. isReadable(true),
  55. isExecutable(false)
  56. {}
  57. QString name;
  58. int permissions;
  59. QString owner;
  60. QString group;
  61. qint64 size;
  62. QDateTime lastModified;
  63. QDateTime lastRead;
  64. bool isDir;
  65. bool isFile;
  66. bool isSymLink;
  67. bool isWritable;
  68. bool isReadable;
  69. bool isExecutable;
  70. };
  71. /*!
  72. \class QUrlInfo
  73. \brief The QUrlInfo class stores information about URLs.
  74. \internal
  75. \ingroup io
  76. \ingroup network
  77. \inmodule QtNetwork
  78. The information about a URL that can be retrieved includes name(),
  79. permissions(), owner(), group(), size(), lastModified(),
  80. lastRead(), isDir(), isFile(), isSymLink(), isWritable(),
  81. isReadable() and isExecutable().
  82. You can create your own QUrlInfo objects passing in all the
  83. relevant information in the constructor, and you can modify a
  84. QUrlInfo; for each getter mentioned above there is an equivalent
  85. setter. Note that setting values does not affect the underlying
  86. resource that the QUrlInfo provides information about; for example
  87. if you call setWritable(true) on a read-only resource the only
  88. thing changed is the QUrlInfo object, not the resource.
  89. \sa QUrl, {FTP Example}
  90. */
  91. /*!
  92. \enum QUrlInfo::PermissionSpec
  93. This enum is used by the permissions() function to report the
  94. permissions of a file.
  95. \value ReadOwner The file is readable by the owner of the file.
  96. \value WriteOwner The file is writable by the owner of the file.
  97. \value ExeOwner The file is executable by the owner of the file.
  98. \value ReadGroup The file is readable by the group.
  99. \value WriteGroup The file is writable by the group.
  100. \value ExeGroup The file is executable by the group.
  101. \value ReadOther The file is readable by anyone.
  102. \value WriteOther The file is writable by anyone.
  103. \value ExeOther The file is executable by anyone.
  104. */
  105. /*!
  106. Constructs an invalid QUrlInfo object with default values.
  107. \sa isValid()
  108. */
  109. QUrlInfo::QUrlInfo()
  110. {
  111. d = nullptr;
  112. }
  113. /*!
  114. Copy constructor, copies \a ui to this URL info object.
  115. */
  116. QUrlInfo::QUrlInfo(const QUrlInfo &ui)
  117. {
  118. if (ui.d) {
  119. d = new QUrlInfoPrivate;
  120. *d = *ui.d;
  121. } else {
  122. d = nullptr;
  123. }
  124. }
  125. /*!
  126. Constructs a QUrlInfo object by specifying all the URL's
  127. information.
  128. The information that is passed is the \a name, file \a
  129. permissions, \a owner and \a group and the file's \a size. Also
  130. passed is the \a lastModified date/time and the \a lastRead
  131. date/time. Flags are also passed, specifically, \a isDir, \a
  132. isFile, \a isSymLink, \a isWritable, \a isReadable and \a
  133. isExecutable.
  134. */
  135. QUrlInfo::QUrlInfo(const QString &name, int permissions, const QString &owner,
  136. const QString &group, qint64 size, const QDateTime &lastModified,
  137. const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
  138. bool isWritable, bool isReadable, bool isExecutable)
  139. {
  140. d = new QUrlInfoPrivate;
  141. d->name = name;
  142. d->permissions = permissions;
  143. d->owner = owner;
  144. d->group = group;
  145. d->size = size;
  146. d->lastModified = lastModified;
  147. d->lastRead = lastRead;
  148. d->isDir = isDir;
  149. d->isFile = isFile;
  150. d->isSymLink = isSymLink;
  151. d->isWritable = isWritable;
  152. d->isReadable = isReadable;
  153. d->isExecutable = isExecutable;
  154. }
  155. /*!
  156. Constructs a QUrlInfo object by specifying all the URL's
  157. information.
  158. The information that is passed is the \a url, file \a
  159. permissions, \a owner and \a group and the file's \a size. Also
  160. passed is the \a lastModified date/time and the \a lastRead
  161. date/time. Flags are also passed, specifically, \a isDir, \a
  162. isFile, \a isSymLink, \a isWritable, \a isReadable and \a
  163. isExecutable.
  164. */
  165. QUrlInfo::QUrlInfo(const QUrl &url, int permissions, const QString &owner,
  166. const QString &group, qint64 size, const QDateTime &lastModified,
  167. const QDateTime &lastRead, bool isDir, bool isFile, bool isSymLink,
  168. bool isWritable, bool isReadable, bool isExecutable)
  169. {
  170. d = new QUrlInfoPrivate;
  171. d->name = QFileInfo(url.path()).fileName();
  172. d->permissions = permissions;
  173. d->owner = owner;
  174. d->group = group;
  175. d->size = size;
  176. d->lastModified = lastModified;
  177. d->lastRead = lastRead;
  178. d->isDir = isDir;
  179. d->isFile = isFile;
  180. d->isSymLink = isSymLink;
  181. d->isWritable = isWritable;
  182. d->isReadable = isReadable;
  183. d->isExecutable = isExecutable;
  184. }
  185. /*!
  186. Sets the name of the URL to \a name. The name is the full text,
  187. for example, "http://qt-project.org/doc/qt-5.0/qtcore/qurl.html".
  188. If you call this function for an invalid URL info, this function
  189. turns it into a valid one.
  190. \sa isValid()
  191. */
  192. void QUrlInfo::setName(const QString &name)
  193. {
  194. if (!d)
  195. d = new QUrlInfoPrivate;
  196. d->name = name;
  197. }
  198. /*!
  199. If \a b is true then the URL is set to be a directory; if \a b is
  200. false then the URL is set not to be a directory (which normally
  201. means it is a file). (Note that a URL can refer to both a file and
  202. a directory even though most file systems do not support this.)
  203. If you call this function for an invalid URL info, this function
  204. turns it into a valid one.
  205. \sa isValid()
  206. */
  207. void QUrlInfo::setDir(bool b)
  208. {
  209. if (!d)
  210. d = new QUrlInfoPrivate;
  211. d->isDir = b;
  212. }
  213. /*!
  214. If \a b is true then the URL is set to be a file; if \b is false
  215. then the URL is set not to be a file (which normally means it is a
  216. directory). (Note that a URL can refer to both a file and a
  217. directory even though most file systems do not support this.)
  218. If you call this function for an invalid URL info, this function
  219. turns it into a valid one.
  220. \sa isValid()
  221. */
  222. void QUrlInfo::setFile(bool b)
  223. {
  224. if (!d)
  225. d = new QUrlInfoPrivate;
  226. d->isFile = b;
  227. }
  228. /*!
  229. Specifies that the URL refers to a symbolic link if \a b is true
  230. and that it does not if \a b is false.
  231. If you call this function for an invalid URL info, this function
  232. turns it into a valid one.
  233. \sa isValid()
  234. */
  235. void QUrlInfo::setSymLink(bool b)
  236. {
  237. if (!d)
  238. d = new QUrlInfoPrivate;
  239. d->isSymLink = b;
  240. }
  241. /*!
  242. Specifies that the URL is writable if \a b is true and not
  243. writable if \a b is false.
  244. If you call this function for an invalid URL info, this function
  245. turns it into a valid one.
  246. \sa isValid()
  247. */
  248. void QUrlInfo::setWritable(bool b)
  249. {
  250. if (!d)
  251. d = new QUrlInfoPrivate;
  252. d->isWritable = b;
  253. }
  254. /*!
  255. Specifies that the URL is readable if \a b is true and not
  256. readable if \a b is false.
  257. If you call this function for an invalid URL info, this function
  258. turns it into a valid one.
  259. \sa isValid()
  260. */
  261. void QUrlInfo::setReadable(bool b)
  262. {
  263. if (!d)
  264. d = new QUrlInfoPrivate;
  265. d->isReadable = b;
  266. }
  267. /*!
  268. Specifies that the owner of the URL is called \a s.
  269. If you call this function for an invalid URL info, this function
  270. turns it into a valid one.
  271. \sa isValid()
  272. */
  273. void QUrlInfo::setOwner(const QString &s)
  274. {
  275. if (!d)
  276. d = new QUrlInfoPrivate;
  277. d->owner = s;
  278. }
  279. /*!
  280. Specifies that the owning group of the URL is called \a s.
  281. If you call this function for an invalid URL info, this function
  282. turns it into a valid one.
  283. \sa isValid()
  284. */
  285. void QUrlInfo::setGroup(const QString &s)
  286. {
  287. if (!d)
  288. d = new QUrlInfoPrivate;
  289. d->group = s;
  290. }
  291. /*!
  292. Specifies the \a size of the URL.
  293. If you call this function for an invalid URL info, this function
  294. turns it into a valid one.
  295. \sa isValid()
  296. */
  297. void QUrlInfo::setSize(qint64 size)
  298. {
  299. if (!d)
  300. d = new QUrlInfoPrivate;
  301. d->size = size;
  302. }
  303. /*!
  304. Specifies that the URL has access permissions \a p.
  305. If you call this function for an invalid URL info, this function
  306. turns it into a valid one.
  307. \sa isValid()
  308. */
  309. void QUrlInfo::setPermissions(int p)
  310. {
  311. if (!d)
  312. d = new QUrlInfoPrivate;
  313. d->permissions = p;
  314. }
  315. /*!
  316. Specifies that the object the URL refers to was last modified at
  317. \a dt.
  318. If you call this function for an invalid URL info, this function
  319. turns it into a valid one.
  320. \sa isValid()
  321. */
  322. void QUrlInfo::setLastModified(const QDateTime &dt)
  323. {
  324. if (!d)
  325. d = new QUrlInfoPrivate;
  326. d->lastModified = dt;
  327. }
  328. /*!
  329. \since 4.4
  330. Specifies that the object the URL refers to was last read at
  331. \a dt.
  332. If you call this function for an invalid URL info, this function
  333. turns it into a valid one.
  334. \sa isValid()
  335. */
  336. void QUrlInfo::setLastRead(const QDateTime &dt)
  337. {
  338. if (!d)
  339. d = new QUrlInfoPrivate;
  340. d->lastRead = dt;
  341. }
  342. /*!
  343. Destroys the URL info object.
  344. */
  345. QUrlInfo::~QUrlInfo()
  346. {
  347. delete d;
  348. }
  349. /*!
  350. Assigns the values of \a ui to this QUrlInfo object.
  351. */
  352. QUrlInfo &QUrlInfo::operator=(const QUrlInfo &ui)
  353. {
  354. if (ui.d) {
  355. if (!d)
  356. d= new QUrlInfoPrivate;
  357. *d = *ui.d;
  358. } else {
  359. delete d;
  360. d = nullptr;
  361. }
  362. return *this;
  363. }
  364. /*!
  365. Returns the file name of the URL.
  366. \sa isValid()
  367. */
  368. QString QUrlInfo::name() const
  369. {
  370. if (!d)
  371. return QString();
  372. return d->name;
  373. }
  374. /*!
  375. Returns the permissions of the URL. You can use the \c PermissionSpec flags
  376. to test for certain permissions.
  377. \sa isValid()
  378. */
  379. int QUrlInfo::permissions() const
  380. {
  381. if (!d)
  382. return 0;
  383. return d->permissions;
  384. }
  385. /*!
  386. Returns the owner of the URL.
  387. \sa isValid()
  388. */
  389. QString QUrlInfo::owner() const
  390. {
  391. if (!d)
  392. return QString();
  393. return d->owner;
  394. }
  395. /*!
  396. Returns the group of the URL.
  397. \sa isValid()
  398. */
  399. QString QUrlInfo::group() const
  400. {
  401. if (!d)
  402. return QString();
  403. return d->group;
  404. }
  405. /*!
  406. Returns the size of the URL.
  407. \sa isValid()
  408. */
  409. qint64 QUrlInfo::size() const
  410. {
  411. if (!d)
  412. return 0;
  413. return d->size;
  414. }
  415. /*!
  416. Returns the last modification date of the URL.
  417. \sa isValid()
  418. */
  419. QDateTime QUrlInfo::lastModified() const
  420. {
  421. if (!d)
  422. return QDateTime();
  423. return d->lastModified;
  424. }
  425. /*!
  426. Returns the date when the URL was last read.
  427. \sa isValid()
  428. */
  429. QDateTime QUrlInfo::lastRead() const
  430. {
  431. if (!d)
  432. return QDateTime();
  433. return d->lastRead;
  434. }
  435. /*!
  436. Returns \c true if the URL is a directory; otherwise returns \c false.
  437. \sa isValid()
  438. */
  439. bool QUrlInfo::isDir() const
  440. {
  441. if (!d)
  442. return false;
  443. return d->isDir;
  444. }
  445. /*!
  446. Returns \c true if the URL is a file; otherwise returns \c false.
  447. \sa isValid()
  448. */
  449. bool QUrlInfo::isFile() const
  450. {
  451. if (!d)
  452. return false;
  453. return d->isFile;
  454. }
  455. /*!
  456. Returns \c true if the URL is a symbolic link; otherwise returns \c false.
  457. \sa isValid()
  458. */
  459. bool QUrlInfo::isSymLink() const
  460. {
  461. if (!d)
  462. return false;
  463. return d->isSymLink;
  464. }
  465. /*!
  466. Returns \c true if the URL is writable; otherwise returns \c false.
  467. \sa isValid()
  468. */
  469. bool QUrlInfo::isWritable() const
  470. {
  471. if (!d)
  472. return false;
  473. return d->isWritable;
  474. }
  475. /*!
  476. Returns \c true if the URL is readable; otherwise returns \c false.
  477. \sa isValid()
  478. */
  479. bool QUrlInfo::isReadable() const
  480. {
  481. if (!d)
  482. return false;
  483. return d->isReadable;
  484. }
  485. /*!
  486. Returns \c true if the URL is executable; otherwise returns \c false.
  487. \sa isValid()
  488. */
  489. bool QUrlInfo::isExecutable() const
  490. {
  491. if (!d)
  492. return false;
  493. return d->isExecutable;
  494. }
  495. /*!
  496. Returns \c true if \a i1 is greater than \a i2; otherwise returns
  497. false. The objects are compared by the value, which is specified
  498. by \a sortBy. This must be one of QDir::Name, QDir::Time or
  499. QDir::Size.
  500. */
  501. bool QUrlInfo::greaterThan(const QUrlInfo &i1, const QUrlInfo &i2,
  502. int sortBy)
  503. {
  504. switch (sortBy) {
  505. case QDir::Name:
  506. return i1.name() > i2.name();
  507. case QDir::Time:
  508. return i1.lastModified() > i2.lastModified();
  509. case QDir::Size:
  510. return i1.size() > i2.size();
  511. default:
  512. return false;
  513. }
  514. }
  515. /*!
  516. Returns \c true if \a i1 is less than \a i2; otherwise returns \c false.
  517. The objects are compared by the value, which is specified by \a
  518. sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
  519. */
  520. bool QUrlInfo::lessThan(const QUrlInfo &i1, const QUrlInfo &i2,
  521. int sortBy)
  522. {
  523. return !greaterThan(i1, i2, sortBy);
  524. }
  525. /*!
  526. Returns \c true if \a i1 equals to \a i2; otherwise returns \c false.
  527. The objects are compared by the value, which is specified by \a
  528. sortBy. This must be one of QDir::Name, QDir::Time or QDir::Size.
  529. */
  530. bool QUrlInfo::equal(const QUrlInfo &i1, const QUrlInfo &i2,
  531. int sortBy)
  532. {
  533. switch (sortBy) {
  534. case QDir::Name:
  535. return i1.name() == i2.name();
  536. case QDir::Time:
  537. return i1.lastModified() == i2.lastModified();
  538. case QDir::Size:
  539. return i1.size() == i2.size();
  540. default:
  541. return false;
  542. }
  543. }
  544. /*!
  545. Returns \c true if this QUrlInfo is equal to \a other; otherwise
  546. returns \c false.
  547. \sa lessThan(), equal()
  548. */
  549. bool QUrlInfo::operator==(const QUrlInfo &other) const
  550. {
  551. if (!d)
  552. return other.d == nullptr;
  553. if (!other.d)
  554. return false;
  555. return (d->name == other.d->name &&
  556. d->permissions == other.d->permissions &&
  557. d->owner == other.d->owner &&
  558. d->group == other.d->group &&
  559. d->size == other.d->size &&
  560. d->lastModified == other.d->lastModified &&
  561. d->lastRead == other.d->lastRead &&
  562. d->isDir == other.d->isDir &&
  563. d->isFile == other.d->isFile &&
  564. d->isSymLink == other.d->isSymLink &&
  565. d->isWritable == other.d->isWritable &&
  566. d->isReadable == other.d->isReadable &&
  567. d->isExecutable == other.d->isExecutable);
  568. }
  569. /*!
  570. \fn bool QUrlInfo::operator!=(const QUrlInfo &other) const
  571. \since 4.2
  572. Returns \c true if this QUrlInfo is not equal to \a other; otherwise
  573. returns \c false.
  574. \sa lessThan(), equal()
  575. */
  576. /*!
  577. Returns \c true if the URL info is valid; otherwise returns \c false.
  578. Valid means that the QUrlInfo contains real information.
  579. You should always check if the URL info is valid before relying on
  580. the values.
  581. */
  582. bool QUrlInfo::isValid() const
  583. {
  584. return d != nullptr;
  585. }
  586. QT_END_NAMESPACE