annotate common.cpp @ 170:86d04c76e42e

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