forked from qt-creator/qt-creator
Cleanup in PathChooser and FileUtils
Mostly use HostOsInfo instead of direct macros. Change-Id: Ic6da7c987268fc8a0d06057e17c5bc0b131cbcb2 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
@@ -34,19 +34,19 @@
|
|||||||
#include "qtcassert.h"
|
#include "qtcassert.h"
|
||||||
|
|
||||||
#include "synchronousprocess.h"
|
#include "synchronousprocess.h"
|
||||||
|
#include "hostosinfo.h"
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QSettings>
|
|
||||||
#include <QProcess>
|
|
||||||
|
|
||||||
#include <qevent.h>
|
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QEvent>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
|
#include <QProcess>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class Utils::PathChooser
|
\class Utils::PathChooser
|
||||||
@@ -57,7 +57,7 @@
|
|||||||
Has some validation logic for embedding into QWizardPage.
|
Has some validation logic for embedding into QWizardPage.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*static*/ const char * const Utils::PathChooser::browseButtonLabel =
|
const char * const Utils::PathChooser::browseButtonLabel =
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
QT_TRANSLATE_NOOP("Utils::PathChooser", "Choose...");
|
QT_TRANSLATE_NOOP("Utils::PathChooser", "Choose...");
|
||||||
#else
|
#else
|
||||||
@@ -541,14 +541,12 @@ QString PathChooser::label()
|
|||||||
|
|
||||||
QString PathChooser::homePath()
|
QString PathChooser::homePath()
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
// Return 'users/<name>/Documents' on Windows, since Windows explorer
|
// Return 'users/<name>/Documents' on Windows, since Windows explorer
|
||||||
// does not let people actually display the contents of their home
|
// does not let people actually display the contents of their home
|
||||||
// directory. Alternatively, create a QtCreator-specific directory?
|
// directory. Alternatively, create a QtCreator-specific directory?
|
||||||
return QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
|
if (HostOsInfo::isWindowsHost())
|
||||||
#else
|
return QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
|
||||||
return QDir::homePath();
|
return QDir::homePath();
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathChooser::setExpectedKind(Kind expected)
|
void PathChooser::setExpectedKind(Kind expected)
|
||||||
|
|||||||
@@ -29,41 +29,36 @@
|
|||||||
|
|
||||||
#include "fileutils.h"
|
#include "fileutils.h"
|
||||||
|
|
||||||
|
#include <coreplugin/coreconstants.h>
|
||||||
#include <coreplugin/documentmanager.h>
|
#include <coreplugin/documentmanager.h>
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <coreplugin/iversioncontrol.h>
|
#include <coreplugin/iversioncontrol.h>
|
||||||
#include <coreplugin/removefiledialog.h>
|
#include <coreplugin/removefiledialog.h>
|
||||||
#include <coreplugin/vcsmanager.h>
|
#include <coreplugin/vcsmanager.h>
|
||||||
|
#include <utils/consoleprocess.h>
|
||||||
#include <utils/environment.h>
|
#include <utils/environment.h>
|
||||||
|
#include <utils/hostosinfo.h>
|
||||||
|
#include <utils/qtcprocess.h>
|
||||||
|
#include <utils/unixutils.h>
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QProcess>
|
|
||||||
#include <QApplication>
|
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QProcess>
|
||||||
|
#include <QPushButton>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#if QT_VERSION < 0x050000
|
#if QT_VERSION < 0x050000
|
||||||
#include <QAbstractFileEngine>
|
#include <QAbstractFileEngine>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef Q_OS_WIN
|
using namespace Utils;
|
||||||
#include <utils/consoleprocess.h>
|
|
||||||
#include <utils/qtcprocess.h>
|
|
||||||
#ifndef Q_OS_MAC
|
|
||||||
#include <coreplugin/coreconstants.h>
|
|
||||||
#include <utils/unixutils.h>
|
|
||||||
#include <QPushButton>
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using namespace Core;
|
namespace Core {
|
||||||
|
|
||||||
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
|
|
||||||
// Show error with option to open settings.
|
// Show error with option to open settings.
|
||||||
static inline void showGraphicalShellError(QWidget *parent,
|
static void showGraphicalShellError(QWidget *parent, const QString &app, const QString &error)
|
||||||
const QString &app,
|
|
||||||
const QString &error)
|
|
||||||
{
|
{
|
||||||
const QString title = QApplication::translate("Core::Internal",
|
const QString title = QApplication::translate("Core::Internal",
|
||||||
"Launching a file browser failed");
|
"Launching a file browser failed");
|
||||||
@@ -76,73 +71,71 @@ static inline void showGraphicalShellError(QWidget *parent,
|
|||||||
QAbstractButton *settingsButton = mbox.addButton(QApplication::translate("Core::Internal", "Settings..."),
|
QAbstractButton *settingsButton = mbox.addButton(QApplication::translate("Core::Internal", "Settings..."),
|
||||||
QMessageBox::ActionRole);
|
QMessageBox::ActionRole);
|
||||||
mbox.exec();
|
mbox.exec();
|
||||||
if (mbox.clickedButton() == settingsButton)
|
if (mbox.clickedButton() == settingsButton) {
|
||||||
Core::ICore::showOptionsDialog(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE),
|
ICore::showOptionsDialog(QLatin1String(Constants::SETTINGS_CATEGORY_CORE),
|
||||||
QLatin1String(Core::Constants::SETTINGS_ID_ENVIRONMENT));
|
QLatin1String(Constants::SETTINGS_ID_ENVIRONMENT));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
|
void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
|
||||||
{
|
{
|
||||||
// Mac, Windows support folder or file.
|
// Mac, Windows support folder or file.
|
||||||
#if defined(Q_OS_WIN)
|
if (HostOsInfo::isWindowsHost()) {
|
||||||
const QString explorer = Utils::Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
|
const QString explorer = Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
|
||||||
if (explorer.isEmpty()) {
|
if (explorer.isEmpty()) {
|
||||||
QMessageBox::warning(parent,
|
QMessageBox::warning(parent,
|
||||||
QApplication::translate("Core::Internal",
|
QApplication::translate("Core::Internal",
|
||||||
"Launching Windows Explorer Failed"),
|
"Launching Windows Explorer Failed"),
|
||||||
QApplication::translate("Core::Internal",
|
QApplication::translate("Core::Internal",
|
||||||
"Could not find explorer.exe in path to launch Windows Explorer."));
|
"Could not find explorer.exe in path to launch Windows Explorer."));
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
QStringList param;
|
||||||
|
if (!QFileInfo(pathIn).isDir())
|
||||||
|
param += QLatin1String("/select,");
|
||||||
|
param += QDir::toNativeSeparators(pathIn);
|
||||||
|
QProcess::startDetached(explorer, param);
|
||||||
|
} else if (HostOsInfo::isMacHost()) {
|
||||||
|
QStringList scriptArgs;
|
||||||
|
scriptArgs << QLatin1String("-e")
|
||||||
|
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
|
||||||
|
.arg(pathIn);
|
||||||
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
||||||
|
scriptArgs.clear();
|
||||||
|
scriptArgs << QLatin1String("-e")
|
||||||
|
<< QLatin1String("tell application \"Finder\" to activate");
|
||||||
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
||||||
|
} else {
|
||||||
|
// we cannot select a file here, because no file browser really supports it...
|
||||||
|
const QFileInfo fileInfo(pathIn);
|
||||||
|
const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
|
||||||
|
const QString app = UnixUtils::fileBrowser(ICore::settings());
|
||||||
|
QProcess browserProc;
|
||||||
|
const QString browserArgs = UnixUtils::substituteFileBrowserParameters(app, folder);
|
||||||
|
bool success = browserProc.startDetached(browserArgs);
|
||||||
|
const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
|
||||||
|
success = success && error.isEmpty();
|
||||||
|
if (!success)
|
||||||
|
showGraphicalShellError(parent, app, error);
|
||||||
}
|
}
|
||||||
QStringList param;
|
|
||||||
if (!QFileInfo(pathIn).isDir())
|
|
||||||
param += QLatin1String("/select,");
|
|
||||||
param += QDir::toNativeSeparators(pathIn);
|
|
||||||
QProcess::startDetached(explorer, param);
|
|
||||||
#elif defined(Q_OS_MAC)
|
|
||||||
Q_UNUSED(parent)
|
|
||||||
QStringList scriptArgs;
|
|
||||||
scriptArgs << QLatin1String("-e")
|
|
||||||
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
|
|
||||||
.arg(pathIn);
|
|
||||||
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
|
||||||
scriptArgs.clear();
|
|
||||||
scriptArgs << QLatin1String("-e")
|
|
||||||
<< QLatin1String("tell application \"Finder\" to activate");
|
|
||||||
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
|
||||||
#else
|
|
||||||
// we cannot select a file here, because no file browser really supports it...
|
|
||||||
const QFileInfo fileInfo(pathIn);
|
|
||||||
const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
|
|
||||||
const QString app = Utils::UnixUtils::fileBrowser(Core::ICore::settings());
|
|
||||||
QProcess browserProc;
|
|
||||||
const QString browserArgs = Utils::UnixUtils::substituteFileBrowserParameters(app, folder);
|
|
||||||
bool success = browserProc.startDetached(browserArgs);
|
|
||||||
const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
|
|
||||||
success = success && error.isEmpty();
|
|
||||||
if (!success)
|
|
||||||
showGraphicalShellError(parent, app, error);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileUtils::openTerminal(const QString &path)
|
void FileUtils::openTerminal(const QString &path)
|
||||||
{
|
{
|
||||||
// Get terminal application
|
// Get terminal application
|
||||||
#ifdef Q_OS_WIN
|
QString terminalEmulator;
|
||||||
const QString terminalEmulator = QString::fromLocal8Bit(qgetenv("COMSPEC"));
|
|
||||||
const QStringList args; // none
|
|
||||||
#elif defined(Q_OS_MAC)
|
|
||||||
const QString terminalEmulator = Core::ICore::resourcePath()
|
|
||||||
+ QLatin1String("/scripts/openTerminal.command");
|
|
||||||
QStringList args;
|
QStringList args;
|
||||||
#else
|
if (HostOsInfo::isWindowsHost()) {
|
||||||
QStringList args = Utils::QtcProcess::splitArgs(
|
terminalEmulator = QString::fromLocal8Bit(qgetenv("COMSPEC"));
|
||||||
Utils::ConsoleProcess::terminalEmulator(Core::ICore::settings()));
|
} else if (HostOsInfo::isMacHost()) {
|
||||||
const QString terminalEmulator = args.takeFirst();
|
terminalEmulator = ICore::resourcePath()
|
||||||
const QString shell = QString::fromLocal8Bit(qgetenv("SHELL"));
|
+ QLatin1String("/scripts/openTerminal.command");
|
||||||
args.append(shell);
|
} else {
|
||||||
#endif
|
args = QtcProcess::splitArgs(ConsoleProcess::terminalEmulator(ICore::settings()));
|
||||||
|
terminalEmulator = args.takeFirst();
|
||||||
|
args.append(QString::fromLocal8Bit(qgetenv("SHELL")));
|
||||||
|
}
|
||||||
|
|
||||||
// Launch terminal with working directory set.
|
// Launch terminal with working directory set.
|
||||||
const QFileInfo fileInfo(path);
|
const QFileInfo fileInfo(path);
|
||||||
const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ?
|
const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ?
|
||||||
@@ -153,22 +146,18 @@ void FileUtils::openTerminal(const QString &path)
|
|||||||
|
|
||||||
QString FileUtils::msgGraphicalShellAction()
|
QString FileUtils::msgGraphicalShellAction()
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_WIN)
|
if (HostOsInfo::isWindowsHost())
|
||||||
return QApplication::translate("Core::Internal", "Show in Explorer");
|
return QApplication::translate("Core::Internal", "Show in Explorer");
|
||||||
#elif defined(Q_OS_MAC)
|
if (HostOsInfo::isMacHost())
|
||||||
return QApplication::translate("Core::Internal", "Show in Finder");
|
return QApplication::translate("Core::Internal", "Show in Finder");
|
||||||
#else
|
|
||||||
return QApplication::translate("Core::Internal", "Show Containing Folder");
|
return QApplication::translate("Core::Internal", "Show Containing Folder");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString FileUtils::msgTerminalAction()
|
QString FileUtils::msgTerminalAction()
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
if (HostOsInfo::isWindowsHost())
|
||||||
return QApplication::translate("Core::Internal", "Open Command Prompt Here");
|
return QApplication::translate("Core::Internal", "Open Command Prompt Here");
|
||||||
#else
|
|
||||||
return QApplication::translate("Core::Internal", "Open Terminal Here");
|
return QApplication::translate("Core::Internal", "Open Terminal Here");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileUtils::removeFile(const QString &filePath, bool deleteFromFS)
|
void FileUtils::removeFile(const QString &filePath, bool deleteFromFS)
|
||||||
@@ -209,10 +198,10 @@ bool FileUtils::renameFile(const QString &orgFilePath, const QString &newFilePat
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
QString dir = QFileInfo(orgFilePath).absolutePath();
|
QString dir = QFileInfo(orgFilePath).absolutePath();
|
||||||
Core::IVersionControl *vc = Core::ICore::vcsManager()->findVersionControlForDirectory(dir);
|
IVersionControl *vc = ICore::vcsManager()->findVersionControlForDirectory(dir);
|
||||||
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
if (vc && vc->supportsOperation(Core::IVersionControl::MoveOperation))
|
if (vc && vc->supportsOperation(IVersionControl::MoveOperation))
|
||||||
result = vc->vcsMove(orgFilePath, newFilePath);
|
result = vc->vcsMove(orgFilePath, newFilePath);
|
||||||
if (!result) // The moving via vcs failed or the vcs does not support moving, fall back
|
if (!result) // The moving via vcs failed or the vcs does not support moving, fall back
|
||||||
result = fileSystemRenameFile(orgFilePath, newFilePath);
|
result = fileSystemRenameFile(orgFilePath, newFilePath);
|
||||||
@@ -222,3 +211,5 @@ bool FileUtils::renameFile(const QString &orgFilePath, const QString &newFilePat
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Core
|
||||||
|
|||||||
Reference in New Issue
Block a user