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 documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 
52 //![newstuff]
53     QFileInfo fi("c:/temp/foo"); => fi.absoluteFilePath() => "C:/temp/foo"
54 //![newstuff]
55 
56 //! [0]
57 #ifdef Q_OS_UNIX
58 
59 QFileInfo info1("/home/bob/bin/untabify");
60 info1.isSymLink();          // returns true
61 info1.absoluteFilePath();   // returns "/home/bob/bin/untabify"
62 info1.size();               // returns 56201
63 info1.symLinkTarget();      // returns "/opt/pretty++/bin/untabify"
64 
65 QFileInfo info2(info1.symLinkTarget());
66 info2.isSymLink();          // returns false
67 info2.absoluteFilePath();   // returns "/opt/pretty++/bin/untabify"
68 info2.size();               // returns 56201
69 
70 #endif
71 //! [0]
72 
73 
74 //! [1]
75 #ifdef Q_OS_WIN
76 
77 QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk");
78 info1.isSymLink();          // returns true
79 info1.absoluteFilePath();   // returns "C:/Documents and Settings/Bob/untabify.lnk"
80 info1.size();               // returns 743
81 info1.symLinkTarget();      // returns "C:/Pretty++/untabify"
82 
83 QFileInfo info2(info1.symLinkTarget());
84 info2.isSymLink();          // returns false
85 info2.absoluteFilePath();   // returns "C:/Pretty++/untabify"
86 info2.size();               // returns 63942
87 
88 #endif
89 //! [1]
90 
91 
92 //! [2]
93 QString absolute = "/local/bin";
94 QString relative = "local/bin";
95 QFileInfo absFile(absolute);
96 QFileInfo relFile(relative);
97 
98 QDir::setCurrent(QDir::rootPath());
99 // absFile and relFile now point to the same file
100 
101 QDir::setCurrent("/tmp");
102 // absFile now points to "/local/bin",
103 // while relFile points to "/tmp/local/bin"
104 //! [2]
105 
106 
107 //! [3]
108 QFileInfo fi("/tmp/archive.tar.gz");
109 QString name = fi.fileName();                // name = "archive.tar.gz"
110 //! [3]
111 
112 
113 //! [4]
114 QFileInfo fi("/Applications/Safari.app");
115 QString bundle = fi.bundleName();                // name = "Safari"
116 //! [4]
117 
118 
119 //! [5]
120 QFileInfo fi("/tmp/archive.tar.gz");
121 QString base = fi.baseName();  // base = "archive"
122 //! [5]
123 
124 
125 //! [6]
126 QFileInfo fi("/tmp/archive.tar.gz");
127 QString base = fi.completeBaseName();  // base = "archive.tar"
128 //! [6]
129 
130 
131 //! [7]
132 QFileInfo fi("/tmp/archive.tar.gz");
133 QString ext = fi.completeSuffix();  // ext = "tar.gz"
134 //! [7]
135 
136 
137 //! [8]
138 QFileInfo fi("/tmp/archive.tar.gz");
139 QString ext = fi.suffix();  // ext = "gz"
140 //! [8]
141 
142 
143 //! [9]
144 QFileInfo info(fileName);
145 if (info.isSymLink())
146     fileName = info.symLinkTarget();
147 //! [9]
148 
149 
150 //! [10]
151 QFileInfo fi("/tmp/archive.tar.gz");
152 if (fi.permission(QFile::WriteUser | QFile::ReadGroup))
153     qWarning("I can change the file; my group can read the file");
154 if (fi.permission(QFile::WriteGroup | QFile::WriteOther))
155     qWarning("The group or others can change the file");
156 //! [10]
157