Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
jtkorhonen@0
|
17
|
Chris@62
|
18 #include "common.h"
|
Chris@62
|
19 #include "debug.h"
|
jtkorhonen@0
|
20
|
Chris@62
|
21 #include <QFileInfo>
|
Chris@62
|
22 #include <QProcessEnvironment>
|
Chris@62
|
23 #include <QStringList>
|
Chris@77
|
24 #include <QDir>
|
jtkorhonen@0
|
25
|
Chris@62
|
26 #include <sys/types.h>
|
Chris@76
|
27
|
Chris@76
|
28 #ifdef Q_OS_WIN32
|
Chris@76
|
29 #define _WIN32_WINNT 0x0500
|
Chris@76
|
30 #include <windows.h>
|
Chris@76
|
31 #include <security.h>
|
Chris@76
|
32 #else
|
Chris@62
|
33 #include <pwd.h>
|
Chris@76
|
34 #endif
|
Chris@62
|
35
|
Chris@62
|
36 QString findExecutable(QString name)
|
Chris@62
|
37 {
|
Chris@77
|
38 bool found = false;
|
Chris@62
|
39 if (name != "") {
|
Chris@62
|
40 if (name[0] != '/') {
|
Chris@77
|
41 #ifdef Q_OS_WIN32
|
Chris@77
|
42 QChar pathSep = ';';
|
Chris@77
|
43 #else
|
Chris@77
|
44 QChar pathSep = ':';
|
Chris@77
|
45 #endif
|
Chris@62
|
46 name = QFileInfo(name).fileName();
|
Chris@62
|
47 QString path =
|
Chris@62
|
48 QProcessEnvironment::systemEnvironment().value("PATH");
|
Chris@62
|
49 DEBUG << "findExecutable: seeking location for binary " << name
|
Chris@74
|
50 << ": system path is " << path << endl;
|
Chris@74
|
51 #ifndef Q_OS_WIN32
|
Chris@74
|
52 path = path + ":/usr/local/bin";
|
Chris@74
|
53 DEBUG << "... adding /usr/local/bin just in case (fix and add settings dlg please)"
|
Chris@74
|
54 << endl;
|
Chris@74
|
55 #endif
|
Chris@62
|
56 QStringList elements = path.split(pathSep, QString::SkipEmptyParts);
|
Chris@62
|
57 foreach (QString element, elements) {
|
Chris@77
|
58 QString full = QDir(element).filePath(name);
|
Chris@62
|
59 QFileInfo fi(full);
|
Chris@62
|
60 if (fi.exists() && fi.isFile() && fi.isExecutable()) {
|
Chris@62
|
61 name = full;
|
Chris@77
|
62 found = true;
|
Chris@62
|
63 break;
|
Chris@62
|
64 }
|
Chris@62
|
65 }
|
Chris@62
|
66 }
|
Chris@62
|
67 }
|
Chris@77
|
68 #ifdef Q_OS_WIN32
|
Chris@77
|
69 if (!found) {
|
Chris@77
|
70 if (!name.endsWith(".exe")) {
|
Chris@77
|
71 return findExecutable(name + ".exe");
|
Chris@77
|
72 }
|
Chris@77
|
73 }
|
Chris@77
|
74 #endif
|
Chris@62
|
75 return name;
|
Chris@62
|
76 }
|
Chris@62
|
77
|
jtkorhonen@0
|
78 QString getSystem()
|
jtkorhonen@0
|
79 {
|
jtkorhonen@0
|
80 #ifdef Q_WS_X11
|
jtkorhonen@0
|
81 return QString("Linux");
|
jtkorhonen@0
|
82 #endif
|
jtkorhonen@0
|
83
|
jtkorhonen@0
|
84 #ifdef Q_WS_MAC
|
jtkorhonen@0
|
85 return QString("Mac");
|
jtkorhonen@0
|
86 #endif
|
jtkorhonen@0
|
87
|
jtkorhonen@0
|
88 #ifdef Q_WS_WIN
|
jtkorhonen@0
|
89 return QString("Windows");
|
jtkorhonen@0
|
90 #endif
|
jtkorhonen@0
|
91
|
jtkorhonen@0
|
92 return QString("");
|
jtkorhonen@0
|
93 }
|
jtkorhonen@0
|
94
|
jtkorhonen@0
|
95 QString getHgDirName()
|
jtkorhonen@0
|
96 {
|
jtkorhonen@0
|
97 if (getSystem() == "Windows")
|
jtkorhonen@0
|
98 {
|
jtkorhonen@0
|
99 return QString(".hg\\");
|
jtkorhonen@0
|
100 }
|
jtkorhonen@0
|
101 else
|
jtkorhonen@0
|
102 {
|
jtkorhonen@0
|
103 return QString(".hg/");
|
jtkorhonen@0
|
104 }
|
jtkorhonen@0
|
105 }
|
jtkorhonen@0
|
106
|
Chris@62
|
107 #ifdef Q_OS_WIN32
|
Chris@62
|
108 QString getUserRealName()
|
Chris@62
|
109 {
|
Chris@76
|
110 TCHAR buf[1024];
|
Chris@76
|
111 long unsigned int maxlen = 1000;
|
Chris@62
|
112 LPTSTR info = buf;
|
jtkorhonen@0
|
113
|
Chris@76
|
114 if (!GetUserNameEx(NameDisplay, info, &maxlen)) {
|
Chris@76
|
115 DEBUG << "GetUserNameEx failed: " << GetLastError() << endl;
|
Chris@62
|
116 return "";
|
Chris@62
|
117 }
|
jtkorhonen@0
|
118
|
Chris@76
|
119 #ifdef UNICODE
|
Chris@76
|
120 return QString::fromUtf16((const unsigned short *)info);
|
Chris@76
|
121 #else
|
Chris@76
|
122 return QString::fromLocal8Bit(info);
|
Chris@76
|
123 #endif
|
Chris@62
|
124 }
|
luisf@71
|
125 #else
|
luisf@71
|
126 #ifdef Q_OS_MAC
|
Chris@62
|
127 // Nothing here: definition is in common_osx.mm
|
Chris@62
|
128 #else
|
Chris@62
|
129 QString getUserRealName()
|
Chris@62
|
130 {
|
Chris@62
|
131 const int maxlen = 1023;
|
Chris@62
|
132 char buf[maxlen + 2];
|
jtkorhonen@0
|
133
|
Chris@62
|
134 if (getlogin_r(buf, maxlen)) return "";
|
jtkorhonen@0
|
135
|
Chris@62
|
136 struct passwd *p = getpwnam(buf);
|
Chris@62
|
137 if (!p) return "";
|
Chris@62
|
138
|
Chris@62
|
139 QString s(p->pw_gecos);
|
Chris@62
|
140 if (s != "") s = s.split(',')[0];
|
Chris@62
|
141 return s;
|
Chris@62
|
142 }
|
Chris@62
|
143 #endif
|
luisf@71
|
144 #endif
|
jtkorhonen@0
|
145
|