mirror of
https://github.com/KDE/konsole.git
synced 2026-05-19 03:57:43 -04:00
Allow %u (user) in tab/window title
BUG: 154295 svn path=/trunk/KDE/kdebase/apps/konsole/; revision=952487
This commit is contained in:
@@ -24,6 +24,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
|
||||
// Qt
|
||||
#include <KDebug>
|
||||
@@ -52,7 +54,9 @@ ProcessInfo::ProcessInfo(int pid , bool enableEnvironmentRead)
|
||||
, _pid(pid)
|
||||
, _parentPid(0)
|
||||
, _foregroundPid(0)
|
||||
, _userId(0)
|
||||
, _lastError(NoError)
|
||||
, _userName(QString())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -91,7 +95,7 @@ QString ProcessInfo::format(const QString& input) const
|
||||
QString output(input);
|
||||
|
||||
// search for and replace known marker
|
||||
output.replace("%u","NOT IMPLEMENTED YET");
|
||||
output.replace("%u",userName());
|
||||
output.replace("%n",name(&ok));
|
||||
output.replace("%c",formatCommand(name(&ok),arguments(&ok),ShortCommandFormat));
|
||||
output.replace("%C",formatCommand(name(&ok),arguments(&ok),LongCommandFormat));
|
||||
@@ -219,12 +223,36 @@ QString ProcessInfo::name(bool* ok) const
|
||||
return _name;
|
||||
}
|
||||
|
||||
int ProcessInfo::userId(bool* ok) const
|
||||
{
|
||||
*ok = _fields & UID;
|
||||
|
||||
return _userId;
|
||||
}
|
||||
|
||||
QString ProcessInfo::userName() const
|
||||
{
|
||||
return _userName;
|
||||
}
|
||||
|
||||
void ProcessInfo::setPid(int pid)
|
||||
{
|
||||
_pid = pid;
|
||||
_fields |= PROCESS_ID;
|
||||
}
|
||||
|
||||
void ProcessInfo::setUserId(int uid)
|
||||
{
|
||||
_userId = uid;
|
||||
_fields |= UID;
|
||||
}
|
||||
|
||||
void ProcessInfo::setUserName(const QString& name)
|
||||
{
|
||||
_userName = name;
|
||||
}
|
||||
|
||||
|
||||
void ProcessInfo::setParentPid(int pid)
|
||||
{
|
||||
_parentPid = pid;
|
||||
@@ -314,6 +342,19 @@ bool UnixProcessInfo::readProcessInfo(int pid , bool enableEnvironmentRead)
|
||||
return ok;
|
||||
}
|
||||
|
||||
void UnixProcessInfo::readUserName()
|
||||
{
|
||||
bool ok = false;
|
||||
int uid = userId(&ok);
|
||||
if (!ok) return;
|
||||
|
||||
struct passwd *pwuser = getpwuid(uid);
|
||||
if (pwuser)
|
||||
setUserName(QString(pwuser->pw_name));
|
||||
else
|
||||
setUserName(QString());
|
||||
}
|
||||
|
||||
|
||||
class LinuxProcessInfo : public UnixProcessInfo
|
||||
{
|
||||
@@ -336,6 +377,29 @@ private:
|
||||
QString processNameString;
|
||||
QString foregroundPidString;
|
||||
|
||||
// For user id read process status file ( /proc/<pid>/status )
|
||||
// Can not use getuid() due to it does not work for 'su'
|
||||
QFile statusInfo( QString("/proc/%1/status").arg(pid) );
|
||||
if ( statusInfo.open(QIODevice::ReadOnly) )
|
||||
{
|
||||
QTextStream stream(&statusInfo);
|
||||
QString data = stream.readAll();
|
||||
int uidIndex = data.indexOf("Uid:");
|
||||
QString uidLine = data.mid(uidIndex + 4, 16); // grab some data
|
||||
QString uidString = uidLine.split('\t', QString::SkipEmptyParts)[0];
|
||||
bool ok = false;
|
||||
int uid = uidString.toInt(&ok);
|
||||
if (ok)
|
||||
setUserId(uid);
|
||||
readUserName();
|
||||
}
|
||||
else
|
||||
{
|
||||
setFileError( statusInfo.error() );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// read process status file ( /proc/<pid/stat )
|
||||
//
|
||||
// the expected file format is a list of fields separated by spaces, using
|
||||
|
||||
@@ -122,6 +122,12 @@ public:
|
||||
*/
|
||||
int foregroundPid(bool* ok) const;
|
||||
|
||||
/* Returns the user id of the process */
|
||||
int userId(bool* ok) const;
|
||||
|
||||
/** Returns the user's name of the process */
|
||||
QString userName() const;
|
||||
|
||||
/** Returns the name of the current process */
|
||||
QString name(bool* ok) const;
|
||||
|
||||
@@ -224,12 +230,19 @@ protected:
|
||||
*/
|
||||
virtual bool readProcessInfo(int pid , bool readEnvironment) = 0;
|
||||
|
||||
/* Read the user name */
|
||||
virtual void readUserName(void) = 0;
|
||||
|
||||
/** Sets the process id associated with this ProcessInfo instance */
|
||||
void setPid(int pid);
|
||||
/** Sets the parent process id as returned by parentPid() */
|
||||
void setParentPid(int pid);
|
||||
/** Sets the foreground process id as returend by foregroundPid() */
|
||||
void setForegroundPid(int pid);
|
||||
/** Sets the user id associated with this ProcessInfo instance */
|
||||
void setUserId(int uid);
|
||||
/** Sets the user name of the process as set by readUserName() */
|
||||
void setUserName(const QString& name);
|
||||
/** Sets the name of the process as returned by name() */
|
||||
void setName(const QString& name);
|
||||
/** Sets the current working directory for the process */
|
||||
@@ -280,7 +293,8 @@ private:
|
||||
ARGUMENTS = 8,
|
||||
ENVIRONMENT = 16,
|
||||
NAME = 32,
|
||||
CURRENT_DIR = 64
|
||||
CURRENT_DIR = 64,
|
||||
UID =128
|
||||
};
|
||||
|
||||
char _fields; // a bitmap indicating which fields are valid
|
||||
@@ -292,10 +306,12 @@ private:
|
||||
int _pid;
|
||||
int _parentPid;
|
||||
int _foregroundPid;
|
||||
int _userId;
|
||||
|
||||
Error _lastError;
|
||||
|
||||
QString _name;
|
||||
QString _userName;
|
||||
QString _currentDir;
|
||||
|
||||
QVector<QString> _arguments;
|
||||
@@ -344,6 +360,8 @@ protected:
|
||||
*/
|
||||
virtual bool readProcessInfo(int pid , bool readEnvironment);
|
||||
|
||||
virtual void readUserName(void);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Read the standard process information -- PID, parent PID, foreground PID.
|
||||
|
||||
Reference in New Issue
Block a user