Mercurial > hg > easyhg
diff common.cpp @ 62:68aebc316898
* Some adjustments to process running (avoid timer): caller must now report errors
* Function to find user's real name
* Locate hg executable in path explicitly, use a setting to remember it
author | Chris Cannam |
---|---|
date | Wed, 17 Nov 2010 13:32:56 +0000 |
parents | f583e44d9d31 |
children | eaabc54de103 |
line wrap: on
line diff
--- a/common.cpp Wed Nov 17 11:48:58 2010 +0000 +++ b/common.cpp Wed Nov 17 13:32:56 2010 +0000 @@ -15,9 +15,44 @@ COPYING included with this distribution for more information. */ +#include "common.h" +#include "debug.h" -#include "common.h" +#include <QFileInfo> +#include <QProcessEnvironment> +#include <QStringList> +#include <sys/types.h> +#include <pwd.h> + +QString findExecutable(QString name) +{ + if (name != "") { + if (name[0] != '/') { + name = QFileInfo(name).fileName(); + QString path = + QProcessEnvironment::systemEnvironment().value("PATH"); + DEBUG << "findExecutable: seeking location for binary " << name + << ": system path is " << path; +#ifdef Q_OS_WIN32 + QChar pathSep = ';'; +#else + QChar pathSep = ':'; +#endif + QStringList elements = path.split(pathSep, QString::SkipEmptyParts); + foreach (QString element, elements) { + QString full = QString("%1/%2").arg(element).arg(name); + QFileInfo fi(full); + if (fi.exists() && fi.isFile() && fi.isExecutable()) { + name = full; + break; + } + } + } + } + return name; +} + QString getSystem() { #ifdef Q_WS_X11 @@ -35,15 +70,6 @@ return QString(""); } -QString getHgBinaryName() -{ - if (getSystem() == "Windows") - return QString("hg.exe"); - else - return QString("hg"); -} - - QString getHgDirName() { if (getSystem() == "Windows") @@ -56,8 +82,35 @@ } } +#ifdef Q_OS_WIN32 +QString getUserRealName() +{ + const int maxlen = 1023; + TCHAR buf[maxlen + 2]; + LPTSTR info = buf; + if (!GetUserNameEx(NameDisplay, info, maxlen)) { + return ""; + } + return QString::fromUcs2(info); +} +#elif Q_OS_MAC +// Nothing here: definition is in common_osx.mm +#else +QString getUserRealName() +{ + const int maxlen = 1023; + char buf[maxlen + 2]; + if (getlogin_r(buf, maxlen)) return ""; + struct passwd *p = getpwnam(buf); + if (!p) return ""; + + QString s(p->pw_gecos); + if (s != "") s = s.split(',')[0]; + return s; +} +#endif