VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/globals/UIPathOperations.cpp

Last change on this file was 102378, checked in by vboxsync, 6 months ago

FE/Qt: bugref:10561. Several fixes in file system model related code to make them work correctly with Windows file system.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: UIPathOperations.cpp 102378 2023-11-29 12:06:14Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIPathOperations class implementation.
4 */
5
6/*
7 * Copyright (C) 2016-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes: */
29#include <QList>
30
31/* GUI includes: */
32#include "UIPathOperations.h"
33
34const QChar UIPathOperations::delimiter = QChar('/');
35const QChar UIPathOperations::dosDelimiter = QChar('\\');
36
37/* static */ QString UIPathOperations::removeMultipleDelimiters(const QString &path)
38{
39 QString newPath(path);
40 QString doubleDelimiter(2, delimiter);
41
42 while (newPath.contains(doubleDelimiter) && !newPath.isEmpty())
43 newPath = newPath.replace(doubleDelimiter, delimiter);
44 return newPath;
45}
46
47/* static */ QString UIPathOperations::removeTrailingDelimiters(const QString &path)
48{
49 if (path.isNull() || path.isEmpty())
50 return QString();
51 QString newPath(path);
52 /* Make sure for we dont have any trailing delimiters: */
53 while (newPath.length() > 1 && newPath.at(newPath.length() - 1) == UIPathOperations::delimiter)
54 newPath.chop(1);
55 return newPath;
56}
57
58/* static */ QString UIPathOperations::addTrailingDelimiters(const QString &path)
59{
60 if (path.isNull() || path.isEmpty())
61 return QString();
62 QString newPath(path);
63 while (newPath.length() > 1 && newPath.at(newPath.length() - 1) != UIPathOperations::delimiter)
64 newPath += UIPathOperations::delimiter;
65 return newPath;
66}
67
68/* static */ QString UIPathOperations::addStartDelimiter(const QString &path)
69{
70 if (path.isEmpty())
71 return QString(path);
72 QString newPath(path);
73
74 if (doesPathStartWithDriveLetter(newPath))
75 {
76 if (newPath.length() == 2)
77 {
78 newPath += delimiter;
79 return newPath;
80 }
81 if (newPath.at(2) != delimiter)
82 newPath.insert(2, delimiter);
83 return newPath;
84 }
85 if (newPath.at(0) != delimiter)
86 newPath.insert(0, delimiter);
87 return newPath;
88}
89
90/* static */ QString UIPathOperations::sanitize(const QString &path)
91{
92 QString newPath = addStartDelimiter(removeTrailingDelimiters(removeMultipleDelimiters(path))).replace(dosDelimiter, delimiter);
93 return newPath;
94}
95
96/* static */ QString UIPathOperations::mergePaths(const QString &path, const QString &baseName)
97{
98 QString newBase(baseName);
99 newBase = newBase.remove(delimiter);
100
101 /* make sure we have one and only one trailing '/': */
102 QString newPath(sanitize(path));
103 if(newPath.isEmpty())
104 newPath = delimiter;
105 if(newPath.at(newPath.length() - 1) != delimiter)
106 newPath += UIPathOperations::delimiter;
107 newPath += newBase;
108 return sanitize(newPath);
109}
110
111/* static */ QString UIPathOperations::getObjectName(const QString &path)
112{
113 if (path.length() <= 1)
114 return QString(path);
115
116 QString strTemp(sanitize(path));
117 if (strTemp.length() < 2)
118 return strTemp;
119 int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
120 if (lastSlashPosition == -1)
121 return QString();
122 return strTemp.right(strTemp.length() - lastSlashPosition - 1);
123}
124
125/* static */ QString UIPathOperations::getPathExceptObjectName(const QString &path)
126{
127 if (path.length() <= 1)
128 return QString(path);
129
130 QString strTemp(sanitize(path));
131 int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
132 if (lastSlashPosition == -1)
133 return QString();
134 return strTemp.left(lastSlashPosition + 1);
135}
136
137/* static */ QString UIPathOperations::constructNewItemPath(const QString &previousPath, const QString &newBaseName)
138{
139 if (previousPath.length() <= 1)
140 return QString(previousPath);
141 return sanitize(mergePaths(getPathExceptObjectName(previousPath), newBaseName));
142}
143
144/* static */ QStringList UIPathOperations::pathTrail(const QString &path)
145{
146#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
147 return path.split(UIPathOperations::delimiter, Qt::SkipEmptyParts);
148#else
149 return path.split(UIPathOperations::delimiter, QString::SkipEmptyParts);
150#endif
151}
152
153/* static */ bool UIPathOperations::doesPathStartWithDriveLetter(const QString &path)
154{
155 if (path.length() < 2)
156 return false;
157 /* search for ':' with the path: */
158 if (!path[0].isLetter())
159 return false;
160 if (path[1] != ':')
161 return false;
162 return true;
163}
164
165/* static */ const QString UIPathOperations::replaceDosDelimeter(const QString &path)
166{
167 QString newPath(path);
168 return newPath.replace(dosDelimiter, delimiter);
169}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use