annotate common.cpp @ 105:1928f9b408e6

* Better-behaved comment dialog; avoid empty comments. Also ignore signals from closing ptys
author Chris Cannam
date Thu, 25 Nov 2010 17:21:32 +0000
parents 7ed57064f293
children 4bd17f36d059
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 }
Chris@62 84
jtkorhonen@0 85 QString getSystem()
jtkorhonen@0 86 {
jtkorhonen@0 87 #ifdef Q_WS_X11
jtkorhonen@0 88 return QString("Linux");
jtkorhonen@0 89 #endif
jtkorhonen@0 90
jtkorhonen@0 91 #ifdef Q_WS_MAC
jtkorhonen@0 92 return QString("Mac");
jtkorhonen@0 93 #endif
jtkorhonen@0 94
jtkorhonen@0 95 #ifdef Q_WS_WIN
jtkorhonen@0 96 return QString("Windows");
jtkorhonen@0 97 #endif
jtkorhonen@0 98
jtkorhonen@0 99 return QString("");
jtkorhonen@0 100 }
jtkorhonen@0 101
jtkorhonen@0 102 QString getHgDirName()
jtkorhonen@0 103 {
jtkorhonen@0 104 if (getSystem() == "Windows")
jtkorhonen@0 105 {
jtkorhonen@0 106 return QString(".hg\\");
jtkorhonen@0 107 }
jtkorhonen@0 108 else
jtkorhonen@0 109 {
jtkorhonen@0 110 return QString(".hg/");
jtkorhonen@0 111 }
jtkorhonen@0 112 }
jtkorhonen@0 113
Chris@62 114 #ifdef Q_OS_WIN32
Chris@62 115 QString getUserRealName()
Chris@62 116 {
Chris@76 117 TCHAR buf[1024];
Chris@76 118 long unsigned int maxlen = 1000;
Chris@62 119 LPTSTR info = buf;
jtkorhonen@0 120
Chris@76 121 if (!GetUserNameEx(NameDisplay, info, &maxlen)) {
Chris@76 122 DEBUG << "GetUserNameEx failed: " << GetLastError() << endl;
Chris@62 123 return "";
Chris@62 124 }
jtkorhonen@0 125
Chris@76 126 #ifdef UNICODE
Chris@76 127 return QString::fromUtf16((const unsigned short *)info);
Chris@76 128 #else
Chris@76 129 return QString::fromLocal8Bit(info);
Chris@76 130 #endif
Chris@62 131 }
luisf@71 132 #else
luisf@71 133 #ifdef Q_OS_MAC
Chris@62 134 // Nothing here: definition is in common_osx.mm
Chris@62 135 #else
Chris@62 136 QString getUserRealName()
Chris@62 137 {
Chris@62 138 const int maxlen = 1023;
Chris@62 139 char buf[maxlen + 2];
jtkorhonen@0 140
Chris@62 141 if (getlogin_r(buf, maxlen)) return "";
jtkorhonen@0 142
Chris@62 143 struct passwd *p = getpwnam(buf);
Chris@62 144 if (!p) return "";
Chris@62 145
Chris@62 146 QString s(p->pw_gecos);
Chris@62 147 if (s != "") s = s.split(',')[0];
Chris@62 148 return s;
Chris@62 149 }
Chris@62 150 #endif
luisf@71 151 #endif
jtkorhonen@0 152
Chris@78 153 void loseControllingTerminal()
Chris@78 154 {
Chris@78 155 #ifndef Q_OS_WIN32
Chris@80 156
Chris@80 157 if (!isatty(0)) {
Chris@80 158 DEBUG << "stdin is not a terminal" << endl;
Chris@80 159 } else {
Chris@80 160 DEBUG << "stdin is a terminal, detaching from it" << endl;
Chris@80 161 if (ioctl(0, TIOCNOTTY, NULL) < 0) {
Chris@80 162 perror("ioctl failed");
Chris@83 163 DEBUG << "ioctl for TIOCNOTTY on stdin failed (errno = " << errno << ")" << endl;
Chris@78 164 } else {
Chris@83 165 DEBUG << "ioctl for TIOCNOTTY on stdin succeeded" << endl;
Chris@83 166 return;
Chris@78 167 }
Chris@80 168 }
Chris@80 169
Chris@80 170 int ttyfd = open("/dev/tty", O_RDWR);
Chris@80 171 if (ttyfd < 0) {
Chris@80 172 DEBUG << "failed to open controlling terminal" << endl;
Chris@80 173 } else {
Chris@80 174 if (ioctl(ttyfd, TIOCNOTTY, NULL) < 0) {
Chris@80 175 perror("ioctl failed");
Chris@83 176 DEBUG << "ioctl for TIOCNOTTY on controlling terminal failed (errno = " << errno << ")" << endl;
Chris@80 177 } else {
Chris@83 178 DEBUG << "ioctl for TIOCNOTTY on controlling terminal succeeded" << endl;
Chris@83 179 return;
Chris@78 180 }
Chris@78 181 }
Chris@80 182
Chris@78 183 #endif
Chris@78 184 }
Chris@79 185
Chris@105 186 void installSignalHandlers()
Chris@105 187 {
Chris@105 188 #ifndef Q_OS_WIN32
Chris@105 189 sigset_t sgnals;
Chris@105 190 sigemptyset (&sgnals);
Chris@105 191 sigaddset(&sgnals, SIGHUP);
Chris@105 192 sigaddset(&sgnals, SIGCONT);
Chris@105 193 pthread_sigmask(SIG_BLOCK, &sgnals, 0);
Chris@105 194 #endif
Chris@105 195 }
Chris@105 196
Chris@79 197 FolderStatus getFolderStatus(QString path)
Chris@79 198 {
Chris@85 199 if (path != "/" && path.endsWith("/")) {
Chris@85 200 path = path.left(path.length()-1);
Chris@85 201 }
Chris@84 202 DEBUG << "getFolderStatus: " << path << endl;
Chris@79 203 QFileInfo fi(path);
Chris@79 204 if (fi.exists()) {
Chris@84 205 DEBUG << "exists" << endl;
Chris@79 206 QDir dir(path);
Chris@79 207 if (!dir.exists()) { // returns false for files
Chris@84 208 DEBUG << "not directory" << endl;
Chris@79 209 return FolderIsFile;
Chris@79 210 }
Chris@79 211 if (QDir(dir.filePath(".hg")).exists()) {
Chris@84 212 DEBUG << "has repo" << endl;
Chris@79 213 return FolderHasRepo;
Chris@79 214 }
Chris@79 215 return FolderExists;
Chris@79 216 } else {
Chris@79 217 QDir parent = fi.dir();
Chris@79 218 if (parent.exists()) {
Chris@84 219 DEBUG << "parent exists" << endl;
Chris@79 220 return FolderParentExists;
Chris@79 221 }
Chris@79 222 return FolderUnknown;
Chris@79 223 }
Chris@79 224 }
Chris@79 225
Chris@79 226 QString getContainingRepoFolder(QString path)
Chris@79 227 {
Chris@79 228 if (getFolderStatus(path) == FolderHasRepo) return "";
Chris@79 229
Chris@79 230 QFileInfo me(path);
Chris@79 231 QFileInfo parent(me.dir().absolutePath());
Chris@79 232
Chris@79 233 while (me != parent) {
Chris@79 234 QString parentPath = parent.filePath();
Chris@79 235 if (getFolderStatus(parentPath) == FolderHasRepo) {
Chris@79 236 return parentPath;
Chris@79 237 }
Chris@79 238 me = parent;
Chris@79 239 parent = me.dir().absolutePath();
Chris@79 240 }
Chris@79 241
Chris@79 242 return "";
Chris@79 243 }
Chris@79 244
Chris@79 245 QString xmlEncode(QString s)
Chris@79 246 {
Chris@79 247 s
Chris@79 248 .replace("&", "&amp;")
Chris@79 249 .replace("<", "&lt;")
Chris@79 250 .replace(">", "&gt;")
Chris@79 251 .replace("\"", "&quot;")
Chris@79 252 .replace("'", "&apos;");
Chris@79 253
Chris@79 254 return s;
Chris@79 255 }