Changeset View
Changeset View
Standalone View
Standalone View
languages/qmljs/libs/utils/fileutils_mac.mm
- This file was added.
1 | /**************************************************************************** | ||||
---|---|---|---|---|---|
2 | ** | ||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | ||||
4 | ** Contact: http://www.qt.io/licensing | ||||
5 | ** | ||||
6 | ** This file is part of Qt Creator. | ||||
7 | ** | ||||
8 | ** Commercial License Usage | ||||
9 | ** Licensees holding valid commercial Qt licenses may use this file in | ||||
10 | ** accordance with the commercial license agreement provided with the | ||||
11 | ** Software or, alternatively, in accordance with the terms contained in | ||||
12 | ** a written agreement between you and The Qt Company. For licensing terms and | ||||
13 | ** conditions see http://www.qt.io/terms-conditions. For further information | ||||
14 | ** use the contact form at http://www.qt.io/contact-us. | ||||
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 or version 3 as published by the Free | ||||
19 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | ||||
20 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | ||||
21 | ** following information to ensure the GNU Lesser General Public License | ||||
22 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | ||||
23 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||||
24 | ** | ||||
25 | ** In addition, as a special exception, The Qt Company gives you certain additional | ||||
26 | ** rights. These rights are described in The Qt Company LGPL Exception | ||||
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | ||||
28 | ** | ||||
29 | ****************************************************************************/ | ||||
30 | | ||||
31 | #include "fileutils_mac.h" | ||||
32 | | ||||
33 | #include "autoreleasepool.h" | ||||
34 | #include "qtcassert.h" | ||||
35 | | ||||
36 | #include <QDir> | ||||
37 | #include <QFileInfo> | ||||
38 | #include <QUrl> | ||||
39 | | ||||
40 | #include <Foundation/NSURL.h> | ||||
41 | | ||||
42 | namespace Utils { | ||||
43 | namespace Internal { | ||||
44 | | ||||
45 | QUrl filePathUrl(const QUrl &url) | ||||
46 | { | ||||
47 | Utils::AutoreleasePool pool; Q_UNUSED(pool) | ||||
48 | QUrl ret = url; | ||||
49 | NSURL *nsurl = url.toNSURL(); | ||||
50 | if ([nsurl isFileReferenceURL]) | ||||
51 | ret = QUrl::fromNSURL([nsurl filePathURL]); | ||||
52 | return ret; | ||||
53 | } | ||||
54 | | ||||
55 | QString normalizePathName(const QString &filePath) | ||||
56 | { | ||||
57 | AutoreleasePool pool; Q_UNUSED(pool) | ||||
58 | | ||||
59 | // NSURL getResourceValue returns values based on the cleaned path so we need to work on that. | ||||
60 | // It also returns the disk name for "/" and "/.." and errors on "" and relative paths, | ||||
61 | // so avoid that | ||||
62 | | ||||
63 | // we cannot know the normalized name for relative paths | ||||
64 | if (QFileInfo(filePath).isRelative()) | ||||
65 | return filePath; | ||||
66 | | ||||
67 | QString result; | ||||
68 | QString path = QDir::cleanPath(filePath); | ||||
69 | // avoid empty paths and paths like "/../foo" or "/.." | ||||
70 | if (path.isEmpty() || path.contains(QLatin1String("/../")) || path.endsWith(QLatin1String("/.."))) | ||||
71 | return filePath; | ||||
72 | | ||||
73 | while (path != QLatin1String("/") /*be defensive->*/&& path != QLatin1String(".") && !path.isEmpty()) { | ||||
74 | QFileInfo info(path); | ||||
75 | NSURL *nsurl = [NSURL fileURLWithPath:path.toNSString()]; | ||||
76 | NSString *out; | ||||
77 | QString component; | ||||
78 | if ([nsurl getResourceValue:(NSString **)&out forKey:NSURLNameKey error:nil]) | ||||
79 | component = QString::fromNSString(out); | ||||
80 | else // e.g. if the full path does not exist | ||||
81 | component = info.fileName(); | ||||
82 | result.prepend(QLatin1Char('/') + component); | ||||
83 | path = info.path(); | ||||
84 | } | ||||
85 | QTC_ASSERT(path == QLatin1String("/"), return filePath); | ||||
86 | return result; | ||||
87 | } | ||||
88 | | ||||
89 | } // Internal | ||||
90 | } // Utils |