Chris@57: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@57: Chris@57: /* Chris@57: EasyMercurial Chris@57: Chris@57: Based on HgExplorer by Jari Korhonen Chris@57: Copyright (c) 2010 Jari Korhonen Chris@57: Copyright (c) 2010 Chris Cannam Chris@57: Copyright (c) 2010 Queen Mary, University of London Chris@57: Chris@57: This program is free software; you can redistribute it and/or Chris@57: modify it under the terms of the GNU General Public License as Chris@57: published by the Free Software Foundation; either version 2 of the Chris@57: License, or (at your option) any later version. See the file Chris@57: COPYING included with this distribution for more information. Chris@57: */ jtkorhonen@0: Chris@62: #include "common.h" Chris@62: #include "debug.h" jtkorhonen@0: Chris@62: #include Chris@62: #include Chris@62: #include Chris@77: #include jtkorhonen@0: Chris@62: #include Chris@76: Chris@76: #ifdef Q_OS_WIN32 Chris@76: #define _WIN32_WINNT 0x0500 Chris@166: #define SECURITY_WIN32 Chris@76: #include Chris@76: #include Chris@76: #else Chris@78: #include Chris@62: #include Chris@78: #include Chris@80: #include Chris@80: #include Chris@80: #include Chris@80: #include Chris@105: #include Chris@76: #endif Chris@62: Chris@62: QString findExecutable(QString name) Chris@62: { Chris@77: bool found = false; Chris@62: if (name != "") { Chris@62: if (name[0] != '/') { Chris@77: #ifdef Q_OS_WIN32 Chris@77: QChar pathSep = ';'; Chris@77: #else Chris@77: QChar pathSep = ':'; Chris@77: #endif Chris@62: name = QFileInfo(name).fileName(); Chris@62: QString path = Chris@62: QProcessEnvironment::systemEnvironment().value("PATH"); Chris@62: DEBUG << "findExecutable: seeking location for binary " << name Chris@74: << ": system path is " << path << endl; Chris@74: #ifndef Q_OS_WIN32 Chris@74: path = path + ":/usr/local/bin"; Chris@74: DEBUG << "... adding /usr/local/bin just in case (fix and add settings dlg please)" Chris@74: << endl; Chris@74: #endif Chris@62: QStringList elements = path.split(pathSep, QString::SkipEmptyParts); Chris@62: foreach (QString element, elements) { Chris@77: QString full = QDir(element).filePath(name); Chris@62: QFileInfo fi(full); Chris@62: if (fi.exists() && fi.isFile() && fi.isExecutable()) { Chris@62: name = full; Chris@77: found = true; Chris@62: break; Chris@62: } Chris@62: } Chris@62: } Chris@62: } Chris@77: #ifdef Q_OS_WIN32 Chris@77: if (!found) { Chris@77: if (!name.endsWith(".exe")) { Chris@77: return findExecutable(name + ".exe"); Chris@77: } Chris@77: } Chris@77: #endif Chris@62: return name; Chris@62: } jtkorhonen@0: Chris@62: #ifdef Q_OS_WIN32 Chris@62: QString getUserRealName() Chris@62: { Chris@76: TCHAR buf[1024]; Chris@76: long unsigned int maxlen = 1000; Chris@62: LPTSTR info = buf; jtkorhonen@0: Chris@76: if (!GetUserNameEx(NameDisplay, info, &maxlen)) { Chris@76: DEBUG << "GetUserNameEx failed: " << GetLastError() << endl; Chris@62: return ""; Chris@62: } jtkorhonen@0: Chris@76: #ifdef UNICODE Chris@76: return QString::fromUtf16((const unsigned short *)info); Chris@76: #else Chris@76: return QString::fromLocal8Bit(info); Chris@76: #endif Chris@62: } luisf@71: #else luisf@71: #ifdef Q_OS_MAC Chris@62: // Nothing here: definition is in common_osx.mm Chris@62: #else Chris@62: QString getUserRealName() Chris@62: { Chris@62: const int maxlen = 1023; Chris@62: char buf[maxlen + 2]; jtkorhonen@0: Chris@62: if (getlogin_r(buf, maxlen)) return ""; jtkorhonen@0: Chris@62: struct passwd *p = getpwnam(buf); Chris@62: if (!p) return ""; Chris@62: Chris@62: QString s(p->pw_gecos); Chris@62: if (s != "") s = s.split(',')[0]; Chris@62: return s; Chris@62: } Chris@62: #endif luisf@71: #endif jtkorhonen@0: Chris@78: void loseControllingTerminal() Chris@78: { Chris@78: #ifndef Q_OS_WIN32 Chris@80: Chris@80: if (!isatty(0)) { Chris@80: DEBUG << "stdin is not a terminal" << endl; Chris@80: } else { Chris@80: DEBUG << "stdin is a terminal, detaching from it" << endl; Chris@80: if (ioctl(0, TIOCNOTTY, NULL) < 0) { Chris@80: perror("ioctl failed"); Chris@83: DEBUG << "ioctl for TIOCNOTTY on stdin failed (errno = " << errno << ")" << endl; Chris@78: } else { Chris@83: DEBUG << "ioctl for TIOCNOTTY on stdin succeeded" << endl; Chris@83: return; Chris@78: } Chris@80: } Chris@80: Chris@80: int ttyfd = open("/dev/tty", O_RDWR); Chris@80: if (ttyfd < 0) { Chris@80: DEBUG << "failed to open controlling terminal" << endl; Chris@80: } else { Chris@80: if (ioctl(ttyfd, TIOCNOTTY, NULL) < 0) { Chris@80: perror("ioctl failed"); Chris@83: DEBUG << "ioctl for TIOCNOTTY on controlling terminal failed (errno = " << errno << ")" << endl; Chris@80: } else { Chris@83: DEBUG << "ioctl for TIOCNOTTY on controlling terminal succeeded" << endl; Chris@83: return; Chris@78: } Chris@78: } Chris@80: Chris@78: #endif Chris@78: } Chris@79: Chris@105: void installSignalHandlers() Chris@105: { Chris@105: #ifndef Q_OS_WIN32 Chris@105: sigset_t sgnals; Chris@105: sigemptyset (&sgnals); Chris@105: sigaddset(&sgnals, SIGHUP); Chris@105: sigaddset(&sgnals, SIGCONT); Chris@105: pthread_sigmask(SIG_BLOCK, &sgnals, 0); Chris@105: #endif Chris@105: } Chris@105: Chris@79: FolderStatus getFolderStatus(QString path) Chris@79: { Chris@85: if (path != "/" && path.endsWith("/")) { Chris@85: path = path.left(path.length()-1); Chris@85: } Chris@84: DEBUG << "getFolderStatus: " << path << endl; Chris@79: QFileInfo fi(path); Chris@79: if (fi.exists()) { Chris@84: DEBUG << "exists" << endl; Chris@79: QDir dir(path); Chris@79: if (!dir.exists()) { // returns false for files Chris@84: DEBUG << "not directory" << endl; Chris@79: return FolderIsFile; Chris@79: } Chris@79: if (QDir(dir.filePath(".hg")).exists()) { Chris@84: DEBUG << "has repo" << endl; Chris@79: return FolderHasRepo; Chris@79: } Chris@79: return FolderExists; Chris@79: } else { Chris@79: QDir parent = fi.dir(); Chris@79: if (parent.exists()) { Chris@84: DEBUG << "parent exists" << endl; Chris@79: return FolderParentExists; Chris@79: } Chris@79: return FolderUnknown; Chris@79: } Chris@79: } Chris@79: Chris@79: QString getContainingRepoFolder(QString path) Chris@79: { Chris@79: if (getFolderStatus(path) == FolderHasRepo) return ""; Chris@79: Chris@79: QFileInfo me(path); Chris@79: QFileInfo parent(me.dir().absolutePath()); Chris@79: Chris@79: while (me != parent) { Chris@79: QString parentPath = parent.filePath(); Chris@79: if (getFolderStatus(parentPath) == FolderHasRepo) { Chris@79: return parentPath; Chris@79: } Chris@79: me = parent; Chris@79: parent = me.dir().absolutePath(); Chris@79: } Chris@79: Chris@79: return ""; Chris@79: } Chris@79: Chris@79: QString xmlEncode(QString s) Chris@79: { Chris@79: s Chris@79: .replace("&", "&") Chris@79: .replace("<", "<") Chris@79: .replace(">", ">") Chris@79: .replace("\"", """) Chris@79: .replace("'", "'"); Chris@79: Chris@79: return s; Chris@79: }