comparison 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
comparison
equal deleted inserted replaced
61:bf57a16315bd 62:68aebc316898
13 published by the Free Software Foundation; either version 2 of the 13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file 14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information. 15 COPYING included with this distribution for more information.
16 */ 16 */
17 17
18 #include "common.h"
19 #include "debug.h"
18 20
19 #include "common.h" 21 #include <QFileInfo>
22 #include <QProcessEnvironment>
23 #include <QStringList>
20 24
25 #include <sys/types.h>
26 #include <pwd.h>
27
28 QString findExecutable(QString name)
29 {
30 if (name != "") {
31 if (name[0] != '/') {
32 name = QFileInfo(name).fileName();
33 QString path =
34 QProcessEnvironment::systemEnvironment().value("PATH");
35 DEBUG << "findExecutable: seeking location for binary " << name
36 << ": system path is " << path;
37 #ifdef Q_OS_WIN32
38 QChar pathSep = ';';
39 #else
40 QChar pathSep = ':';
41 #endif
42 QStringList elements = path.split(pathSep, QString::SkipEmptyParts);
43 foreach (QString element, elements) {
44 QString full = QString("%1/%2").arg(element).arg(name);
45 QFileInfo fi(full);
46 if (fi.exists() && fi.isFile() && fi.isExecutable()) {
47 name = full;
48 break;
49 }
50 }
51 }
52 }
53 return name;
54 }
55
21 QString getSystem() 56 QString getSystem()
22 { 57 {
23 #ifdef Q_WS_X11 58 #ifdef Q_WS_X11
24 return QString("Linux"); 59 return QString("Linux");
25 #endif 60 #endif
33 #endif 68 #endif
34 69
35 return QString(""); 70 return QString("");
36 } 71 }
37 72
38 QString getHgBinaryName()
39 {
40 if (getSystem() == "Windows")
41 return QString("hg.exe");
42 else
43 return QString("hg");
44 }
45
46
47 QString getHgDirName() 73 QString getHgDirName()
48 { 74 {
49 if (getSystem() == "Windows") 75 if (getSystem() == "Windows")
50 { 76 {
51 return QString(".hg\\"); 77 return QString(".hg\\");
54 { 80 {
55 return QString(".hg/"); 81 return QString(".hg/");
56 } 82 }
57 } 83 }
58 84
85 #ifdef Q_OS_WIN32
86 QString getUserRealName()
87 {
88 const int maxlen = 1023;
89 TCHAR buf[maxlen + 2];
90 LPTSTR info = buf;
59 91
92 if (!GetUserNameEx(NameDisplay, info, maxlen)) {
93 return "";
94 }
60 95
96 return QString::fromUcs2(info);
97 }
98 #elif Q_OS_MAC
99 // Nothing here: definition is in common_osx.mm
100 #else
101 QString getUserRealName()
102 {
103 const int maxlen = 1023;
104 char buf[maxlen + 2];
61 105
106 if (getlogin_r(buf, maxlen)) return "";
62 107
108 struct passwd *p = getpwnam(buf);
109 if (!p) return "";
110
111 QString s(p->pw_gecos);
112 if (s != "") s = s.split(',')[0];
113 return s;
114 }
115 #endif
63 116