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>
|
Chris@172
|
25 #include <QRegExp>
|
jtkorhonen@0
|
26
|
Chris@62
|
27 #include <sys/types.h>
|
Chris@76
|
28
|
Chris@76
|
29 #ifdef Q_OS_WIN32
|
Chris@76
|
30 #define _WIN32_WINNT 0x0500
|
Chris@166
|
31 #define SECURITY_WIN32
|
Chris@76
|
32 #include <windows.h>
|
Chris@76
|
33 #include <security.h>
|
Chris@76
|
34 #else
|
Chris@78
|
35 #include <errno.h>
|
Chris@62
|
36 #include <pwd.h>
|
Chris@78
|
37 #include <unistd.h>
|
Chris@80
|
38 #include <sys/ioctl.h>
|
Chris@80
|
39 #include <sys/types.h>
|
Chris@80
|
40 #include <sys/stat.h>
|
Chris@80
|
41 #include <fcntl.h>
|
Chris@105
|
42 #include <signal.h>
|
Chris@76
|
43 #endif
|
Chris@62
|
44
|
Chris@172
|
45 QString findInPath(QString name, QString installPath, bool executableRequired)
|
Chris@62
|
46 {
|
Chris@77
|
47 bool found = false;
|
Chris@62
|
48 if (name != "") {
|
Chris@172
|
49 if (name[0] != '/'
|
Chris@172
|
50 #ifdef Q_OS_WIN32
|
Chris@178
|
51 && (QRegExp("^\\w:").indexIn(name) != 0)
|
Chris@172
|
52 #endif
|
Chris@178
|
53 ) {
|
Chris@77
|
54 #ifdef Q_OS_WIN32
|
Chris@77
|
55 QChar pathSep = ';';
|
Chris@77
|
56 #else
|
Chris@77
|
57 QChar pathSep = ':';
|
Chris@77
|
58 #endif
|
Chris@62
|
59 name = QFileInfo(name).fileName();
|
Chris@62
|
60 QString path =
|
Chris@62
|
61 QProcessEnvironment::systemEnvironment().value("PATH");
|
Chris@62
|
62 DEBUG << "findExecutable: seeking location for binary " << name
|
Chris@74
|
63 << ": system path is " << path << endl;
|
Chris@172
|
64 if (installPath != "") {
|
Chris@172
|
65 DEBUG << "findExecutable: install path is " << installPath
|
Chris@172
|
66 << ", adding to system path" << endl;
|
Chris@172
|
67 //!!! path = path + pathSep + installPath;
|
Chris@172
|
68 path = installPath + pathSep + path;
|
Chris@172
|
69 }
|
Chris@74
|
70 #ifndef Q_OS_WIN32
|
Chris@178
|
71 //!!!
|
Chris@74
|
72 path = path + ":/usr/local/bin";
|
Chris@74
|
73 DEBUG << "... adding /usr/local/bin just in case (fix and add settings dlg please)"
|
Chris@74
|
74 << endl;
|
Chris@74
|
75 #endif
|
Chris@62
|
76 QStringList elements = path.split(pathSep, QString::SkipEmptyParts);
|
Chris@62
|
77 foreach (QString element, elements) {
|
Chris@77
|
78 QString full = QDir(element).filePath(name);
|
Chris@62
|
79 QFileInfo fi(full);
|
Chris@172
|
80 DEBUG << "findExecutable: looking at " << full << endl;
|
Chris@172
|
81 if (fi.exists() && fi.isFile()) {
|
Chris@172
|
82 DEBUG << "findExecutable: it's a file" << endl;
|
Chris@172
|
83 if (!executableRequired || fi.isExecutable()) {
|
Chris@172
|
84 name = full;
|
Chris@172
|
85 DEBUG << "findExecutable: found at " << name << endl;
|
Chris@172
|
86 found = true;
|
Chris@172
|
87 break;
|
Chris@172
|
88 }
|
Chris@62
|
89 }
|
Chris@62
|
90 }
|
Chris@62
|
91 }
|
Chris@62
|
92 }
|
Chris@77
|
93 #ifdef Q_OS_WIN32
|
Chris@77
|
94 if (!found) {
|
Chris@77
|
95 if (!name.endsWith(".exe")) {
|
Chris@172
|
96 return findInPath(name + ".exe", installPath, executableRequired);
|
Chris@77
|
97 }
|
Chris@77
|
98 }
|
Chris@77
|
99 #endif
|
Chris@213
|
100 if (found) {
|
Chris@213
|
101 return name;
|
Chris@213
|
102 } else {
|
Chris@213
|
103 return "";
|
Chris@213
|
104 }
|
Chris@62
|
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
|
Chris@78
|
146 void loseControllingTerminal()
|
Chris@78
|
147 {
|
Chris@78
|
148 #ifndef Q_OS_WIN32
|
Chris@80
|
149
|
Chris@80
|
150 if (!isatty(0)) {
|
Chris@80
|
151 DEBUG << "stdin is not a terminal" << endl;
|
Chris@80
|
152 } else {
|
Chris@80
|
153 DEBUG << "stdin is a terminal, detaching from it" << endl;
|
Chris@80
|
154 if (ioctl(0, TIOCNOTTY, NULL) < 0) {
|
Chris@80
|
155 perror("ioctl failed");
|
Chris@83
|
156 DEBUG << "ioctl for TIOCNOTTY on stdin failed (errno = " << errno << ")" << endl;
|
Chris@78
|
157 } else {
|
Chris@83
|
158 DEBUG << "ioctl for TIOCNOTTY on stdin succeeded" << endl;
|
Chris@83
|
159 return;
|
Chris@78
|
160 }
|
Chris@80
|
161 }
|
Chris@80
|
162
|
Chris@80
|
163 int ttyfd = open("/dev/tty", O_RDWR);
|
Chris@80
|
164 if (ttyfd < 0) {
|
Chris@80
|
165 DEBUG << "failed to open controlling terminal" << endl;
|
Chris@80
|
166 } else {
|
Chris@80
|
167 if (ioctl(ttyfd, TIOCNOTTY, NULL) < 0) {
|
Chris@80
|
168 perror("ioctl failed");
|
Chris@83
|
169 DEBUG << "ioctl for TIOCNOTTY on controlling terminal failed (errno = " << errno << ")" << endl;
|
Chris@80
|
170 } else {
|
Chris@83
|
171 DEBUG << "ioctl for TIOCNOTTY on controlling terminal succeeded" << endl;
|
Chris@83
|
172 return;
|
Chris@78
|
173 }
|
Chris@78
|
174 }
|
Chris@80
|
175
|
Chris@78
|
176 #endif
|
Chris@78
|
177 }
|
Chris@79
|
178
|
Chris@105
|
179 void installSignalHandlers()
|
Chris@105
|
180 {
|
Chris@105
|
181 #ifndef Q_OS_WIN32
|
Chris@105
|
182 sigset_t sgnals;
|
Chris@105
|
183 sigemptyset (&sgnals);
|
Chris@105
|
184 sigaddset(&sgnals, SIGHUP);
|
Chris@105
|
185 sigaddset(&sgnals, SIGCONT);
|
Chris@105
|
186 pthread_sigmask(SIG_BLOCK, &sgnals, 0);
|
Chris@105
|
187 #endif
|
Chris@105
|
188 }
|
Chris@105
|
189
|
Chris@79
|
190 FolderStatus getFolderStatus(QString path)
|
Chris@79
|
191 {
|
Chris@85
|
192 if (path != "/" && path.endsWith("/")) {
|
Chris@85
|
193 path = path.left(path.length()-1);
|
Chris@85
|
194 }
|
Chris@84
|
195 DEBUG << "getFolderStatus: " << path << endl;
|
Chris@79
|
196 QFileInfo fi(path);
|
Chris@79
|
197 if (fi.exists()) {
|
Chris@84
|
198 DEBUG << "exists" << endl;
|
Chris@79
|
199 QDir dir(path);
|
Chris@79
|
200 if (!dir.exists()) { // returns false for files
|
Chris@84
|
201 DEBUG << "not directory" << endl;
|
Chris@79
|
202 return FolderIsFile;
|
Chris@79
|
203 }
|
Chris@79
|
204 if (QDir(dir.filePath(".hg")).exists()) {
|
Chris@84
|
205 DEBUG << "has repo" << endl;
|
Chris@79
|
206 return FolderHasRepo;
|
Chris@79
|
207 }
|
Chris@79
|
208 return FolderExists;
|
Chris@79
|
209 } else {
|
Chris@79
|
210 QDir parent = fi.dir();
|
Chris@79
|
211 if (parent.exists()) {
|
Chris@84
|
212 DEBUG << "parent exists" << endl;
|
Chris@79
|
213 return FolderParentExists;
|
Chris@79
|
214 }
|
Chris@79
|
215 return FolderUnknown;
|
Chris@79
|
216 }
|
Chris@79
|
217 }
|
Chris@79
|
218
|
Chris@79
|
219 QString getContainingRepoFolder(QString path)
|
Chris@79
|
220 {
|
Chris@79
|
221 if (getFolderStatus(path) == FolderHasRepo) return "";
|
Chris@79
|
222
|
Chris@79
|
223 QFileInfo me(path);
|
Chris@79
|
224 QFileInfo parent(me.dir().absolutePath());
|
Chris@79
|
225
|
Chris@79
|
226 while (me != parent) {
|
Chris@79
|
227 QString parentPath = parent.filePath();
|
Chris@79
|
228 if (getFolderStatus(parentPath) == FolderHasRepo) {
|
Chris@79
|
229 return parentPath;
|
Chris@79
|
230 }
|
Chris@79
|
231 me = parent;
|
Chris@79
|
232 parent = me.dir().absolutePath();
|
Chris@79
|
233 }
|
Chris@79
|
234
|
Chris@79
|
235 return "";
|
Chris@79
|
236 }
|
Chris@79
|
237
|
Chris@79
|
238 QString xmlEncode(QString s)
|
Chris@79
|
239 {
|
Chris@79
|
240 s
|
Chris@79
|
241 .replace("&", "&")
|
Chris@79
|
242 .replace("<", "<")
|
Chris@79
|
243 .replace(">", ">")
|
Chris@79
|
244 .replace("\"", """)
|
Chris@79
|
245 .replace("'", "'");
|
Chris@79
|
246
|
Chris@79
|
247 return s;
|
Chris@79
|
248 }
|