KDECore
kshell.cpp
Go to the documentation of this file.00001 /* 00002 This file is part of the KDE libraries 00003 00004 Copyright (c) 2003,2007 Oswald Buddenhagen <ossi@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "kshell.h" 00023 #include "kuser.h" 00024 00025 #include <QtCore/QDir> 00026 00027 namespace KShell { 00028 00029 QString homeDir( const QString &user ) 00030 { 00031 if (user.isEmpty()) 00032 return QDir::homePath(); 00033 return KUser(user).homeDir(); 00034 } 00035 00036 } 00037 00038 QString KShell::joinArgs( const QStringList &args ) 00039 { 00040 QString ret; 00041 for (QStringList::ConstIterator it = args.begin(); it != args.end(); ++it) { 00042 if (!ret.isEmpty()) 00043 ret.append(QLatin1Char(' ')); 00044 ret.append(quoteArg(*it)); 00045 } 00046 return ret; 00047 } 00048 00049 #ifdef Q_OS_WIN 00050 # define ESCAPE '^' 00051 #else 00052 # define ESCAPE '\\' 00053 #endif 00054 00055 QString KShell::tildeExpand( const QString &fname ) 00056 { 00057 if (fname.length() && fname[0] == QLatin1Char('~')) { 00058 int pos = fname.indexOf( QLatin1Char('/') ); 00059 if (pos < 0) 00060 return homeDir( fname.mid(1) ); 00061 QString ret = homeDir( fname.mid(1, pos-1) ); 00062 if (!ret.isNull()) 00063 ret += fname.mid(pos); 00064 return ret; 00065 } else if (fname.length() > 1 && fname[0] == QLatin1Char(ESCAPE) && fname[1] == QLatin1Char('~')) { 00066 return fname.mid(1); 00067 } 00068 return fname; 00069 }