KCoreAddons

kshell.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2003, 2007 Oswald Buddenhagen <[email protected]>
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 
15 QString 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 (QStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
27  if (!ret.isEmpty()) {
28  ret.append(QLatin1Char(' '));
29  }
30  ret.append(quoteArg(*it));
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 += fname.midRef(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 }
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString & append(QChar ch)
KCOREADDONS_EXPORT QString tildeCollapse(const QString &path)
Performs tilde collapse on path.
Definition: kshell.cpp:59
QString homePath()
Represents a user on your system.
Definition: kuser.h:192
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QList::iterator end()
KCOREADDONS_EXPORT QString quoteArg(const QString &arg)
Quotes arg according to system shell rules.
QStringRef midRef(int position, int n) const const
QString homeDir() const
The path to the user&#39;s home directory.
Definition: kuser_unix.cpp:167
QString & replace(int position, int n, QChar after)
QString mid(int position, int n) const const
typedef ConstIterator
KCOREADDONS_EXPORT QString tildeExpand(const QString &path)
Performs tilde expansion on path.
Definition: kshell.cpp:41
int length() const const
KCOREADDONS_EXPORT QString joinArgs(const QStringList &args)
Quotes and joins args together according to system shell rules.
Definition: kshell.cpp:23
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 23:06:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.