~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/network/qurlinfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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