KCoreAddons

kshell.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2003, 2007 Oswald Buddenhagen <ossi@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kshell.h"
10#include "kshell_p.h"
11#include "kuser.h"
12
13#include <QDir>
14
15QString KShell::homeDir(const QString &user)
16{
17 if (user.isEmpty()) {
18 return QDir::homePath();
19 }
20 return KUser(user).homeDir();
21}
22
24{
25 QString ret;
26 for (const auto &arg : args) {
27 if (!ret.isEmpty()) {
28 ret.append(QLatin1Char(' '));
29 }
30 ret += quoteArg(arg);
31 }
32 return ret;
33}
34
35#ifdef Q_OS_WIN
36#define ESCAPE '^'
37#else
38#define ESCAPE '\\'
39#endif
40
42{
43 if (!fname.isEmpty() && fname[0] == QLatin1Char('~')) {
44 int pos = fname.indexOf(QLatin1Char('/'));
45 if (pos < 0) {
46 return homeDir(fname.mid(1));
47 }
48 QString ret = homeDir(fname.mid(1, pos - 1));
49 if (!ret.isNull()) {
50 ret += QStringView(fname).mid(pos);
51 }
52 return ret;
53 } else if (fname.length() > 1 && fname[0] == QLatin1Char(ESCAPE) && fname[1] == QLatin1Char('~')) {
54 return fname.mid(1);
55 }
56 return fname;
57}
58
60{
61 if (!path.isEmpty()) {
62 const auto homePath = QDir::homePath();
63 if (path.startsWith(homePath)) {
64 auto newPath = path;
65 newPath.replace(0, homePath.length(), QLatin1Char('~'));
66 return newPath;
67 }
68 }
69 return path;
70}
Represents a user on your system.
Definition kuser.h:226
QString homeDir() const
The path to the user's home directory.
KCOREADDONS_EXPORT QString quoteArg(const QString &arg)
Quotes arg according to system shell rules.
KCOREADDONS_EXPORT QString joinArgs(const QStringList &args)
Quotes and joins args together according to system shell rules.
Definition kshell.cpp:23
KCOREADDONS_EXPORT QString tildeCollapse(const QString &path)
Performs tilde collapse on path.
Definition kshell.cpp:59
KCOREADDONS_EXPORT QString tildeExpand(const QString &path)
Performs tilde expansion on path.
Definition kshell.cpp:41
QString homePath()
QString & append(QChar ch)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool isNull() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QStringView mid(qsizetype start, qsizetype length) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.