annotate common.cpp @ 146:5e16b83374fd

* Merge
author Chris Cannam
date Wed, 01 Dec 2010 17:43:43 +0000
parents 4bd17f36d059
children 0dfd6567ec0c
rev   line source
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@78 33 #include <errno.h>
Chris@62 34 #include <pwd.h>
Chris@78 35 #include <unistd.h>
Chris@80 36 #include <sys/ioctl.h>
Chris@80 37 #include <sys/types.h>
Chris@80 38 #include <sys/stat.h>
Chris@80 39 #include <fcntl.h>
Chris@105 40 #include <signal.h>
Chris@76 41 #endif
Chris@62 42
Chris@62 43 QString findExecutable(QString name)
Chris@62 44 {
Chris@77 45 bool found = false;
Chris@62 46 if (name != "") {
Chris@62 47 if (name[0] != '/') {
Chris@77 48 #ifdef Q_OS_WIN32
Chris@77 49 QChar pathSep = ';';
Chris@77 50 #else
Chris@77 51 QChar pathSep = ':';
Chris@77 52 #endif
Chris@62 53 name = QFileInfo(name).fileName();
Chris@62 54 QString path =
Chris@62 55 QProcessEnvironment::systemEnvironment().value("PATH");
Chris@62 56 DEBUG << "findExecutable: seeking location for binary " << name
Chris@74 57 << ": system path is " << path << endl;
Chris@74 58 #ifndef Q_OS_WIN32
Chris@74 59 path = path + ":/usr/local/bin";
Chris@74 60 DEBUG << "... adding /usr/local/bin just in case (fix and add settings dlg please)"
Chris@74 61 << endl;
Chris@74 62 #endif
Chris@62 63 QStringList elements = path.split(pathSep, QString::SkipEmptyParts);
Chris@62 64 foreach (QString element, elements) {
Chris@77 65 QString full = QDir(element).filePath(name);
Chris@62 66 QFileInfo fi(full);
Chris@62 67 if (fi.exists() && fi.isFile() && fi.isExecutable()) {
Chris@62 68 name = full;
Chris@77 69 found = true;
Chris@62 70 break;
Chris@62 71 }
Chris@62 72 }
Chris@62 73 }
Chris@62 74 }
Chris@77 75 #ifdef Q_OS_WIN32
Chris@77 76 if (!found) {
Chris@77 77 if (!name.endsWith(".exe")) {
Chris@77 78 return findExecutable(name + ".exe");
Chris@77 79 }
Chris@77 80 }
Chris@77 81 #endif
Chris@62 82 return name;
Chris@62 83 }
jtkorhonen@0 84
Chris@62 85 #ifdef Q_OS_WIN32
Chris@62 86 QString getUserRealName()
Chris@62 87 {
Chris@76 88 TCHAR buf[1024];
Chris@76 89 long unsigned int maxlen = 1000;
Chris@62 90 LPTSTR info = buf;
jtkorhonen@0 91
Chris@76 92 if (!GetUserNameEx(NameDisplay, info, &maxlen)) {
Chris@76 93 DEBUG << "GetUserNameEx failed: " << GetLastError() << endl;
Chris@62 94 return "";
Chris@62 95 }
jtkorhonen@0 96
Chris@76 97 #ifdef UNICODE
Chris@76 98 return QString::fromUtf16((const unsigned short *)info);
Chris@76 99 #else
Chris@76 100 return QString::fromLocal8Bit(info);
Chris@76 101 #endif
Chris@62 102 }
luisf@71 103 #else
luisf@71 104 #ifdef Q_OS_MAC
Chris@62 105 // Nothing here: definition is in common_osx.mm
Chris@62 106 #else
Chris@62 107 QString getUserRealName()
Chris@62 108 {
Chris@62 109 const int maxlen = 1023;
Chris@62 110 char buf[maxlen + 2];
jtkorhonen@0 111
Chris@62 112 if (getlogin_r(buf, maxlen)) return "";
jtkorhonen@0 113
Chris@62 114 struct passwd *p = getpwnam(buf);
Chris@62 115 if (!p) return "";
Chris@62 116
Chris@62 117 QString s(p->pw_gecos);
Chris@62 118 if (s != "") s = s.split(',')[0];
Chris@62 119 return s;
Chris@62 120 }
Chris@62 121 #endif
luisf@71 122 #endif
jtkorhonen@0 123
Chris@78 124 void loseControllingTerminal()
Chris@78 125 {
Chris@78 126 #ifndef Q_OS_WIN32
Chris@80 127
Chris@80 128 if (!isatty(0)) {
Chris@80 129 DEBUG << "stdin is not a terminal" << endl;
Chris@80 130 } else {
Chris@80 131 DEBUG << "stdin is a terminal, detaching from it" << endl;
Chris@80 132 if (ioctl(0, TIOCNOTTY, NULL) < 0) {
Chris@80 133 perror("ioctl failed");
Chris@83 134 DEBUG << "ioctl for TIOCNOTTY on stdin failed (errno = " << errno << ")" << endl;
Chris@78 135 } else {
Chris@83 136 DEBUG << "ioctl for TIOCNOTTY on stdin succeeded" << endl;
Chris@83 137 return;
Chris@78 138 }
Chris@80 139 }
Chris@80 140
Chris@80 141 int ttyfd = open("/dev/tty", O_RDWR);
Chris@80 142 if (ttyfd < 0) {
Chris@80 143 DEBUG << "failed to open controlling terminal" << endl;
Chris@80 144 } else {
Chris@80 145 if (ioctl(ttyfd, TIOCNOTTY, NULL) < 0) {
Chris@80 146 perror("ioctl failed");
Chris@83 147 DEBUG << "ioctl for TIOCNOTTY on controlling terminal failed (errno = " << errno << ")" << endl;
Chris@80 148 } else {
Chris@83 149 DEBUG << "ioctl for TIOCNOTTY on controlling terminal succeeded" << endl;
Chris@83 150 return;
Chris@78 151 }
Chris@78 152 }
Chris@80 153
Chris@78 154 #endif
Chris@78 155 }
Chris@79 156
Chris@105 157 void installSignalHandlers()
Chris@105 158 {
Chris@105 159 #ifndef Q_OS_WIN32
Chris@105 160 sigset_t sgnals;
Chris@105 161 sigemptyset (&sgnals);
Chris@105 162 sigaddset(&sgnals, SIGHUP);
Chris@105 163 sigaddset(&sgnals, SIGCONT);
Chris@105 164 pthread_sigmask(SIG_BLOCK, &sgnals, 0);
Chris@105 165 #endif
Chris@105 166 }
Chris@105 167
Chris@79 168 FolderStatus getFolderStatus(QString path)
Chris@79 169 {
Chris@85 170 if (path != "/" && path.endsWith("/")) {
Chris@85 171 path = path.left(path.length()-1);
Chris@85 172 }
Chris@84 173 DEBUG << "getFolderStatus: " << path << endl;
Chris@79 174 QFileInfo fi(path);
Chris@79 175 if (fi.exists()) {
Chris@84 176 DEBUG << "exists" << endl;
Chris@79 177 QDir dir(path);
Chris@79 178 if (!dir.exists()) { // returns false for files
Chris@84 179 DEBUG << "not directory" << endl;
Chris@79 180 return FolderIsFile;
Chris@79 181 }
Chris@79 182 if (QDir(dir.filePath(".hg")).exists()) {
Chris@84 183 DEBUG << "has repo" << endl;
Chris@79 184 return FolderHasRepo;
Chris@79 185 }
Chris@79 186 return FolderExists;
Chris@79 187 } else {
Chris@79 188 QDir parent = fi.dir();
Chris@79 189 if (parent.exists()) {
Chris@84 190 DEBUG << "parent exists" << endl;
Chris@79 191 return FolderParentExists;
Chris@79 192 }
Chris@79 193 return FolderUnknown;
Chris@79 194 }
Chris@79 195 }
Chris@79 196
Chris@79 197 QString getContainingRepoFolder(QString path)
Chris@79 198 {
Chris@79 199 if (getFolderStatus(path) == FolderHasRepo) return "";
Chris@79 200
Chris@79 201 QFileInfo me(path);
Chris@79 202 QFileInfo parent(me.dir().absolutePath());
Chris@79 203
Chris@79 204 while (me != parent) {
Chris@79 205 QString parentPath = parent.filePath();
Chris@79 206 if (getFolderStatus(parentPath) == FolderHasRepo) {
Chris@79 207 return parentPath;
Chris@79 208 }
Chris@79 209 me = parent;
Chris@79 210 parent = me.dir().absolutePath();
Chris@79 211 }
Chris@79 212
Chris@79 213 return "";
Chris@79 214 }
Chris@79 215
Chris@79 216 QString xmlEncode(QString s)
Chris@79 217 {
Chris@79 218 s
Chris@79 219 .replace("&", "&amp;")
Chris@79 220 .replace("<", "&lt;")
Chris@79 221 .replace(">", "&gt;")
Chris@79 222 .replace("\"", "&quot;")
Chris@79 223 .replace("'", "&apos;");
Chris@79 224
Chris@79 225 return s;
Chris@79 226 }