2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 18:55:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-08-16 18:55:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-08-16 18:55:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-08-16 18:55:23 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-08-16 18:55:23 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-08-16 18:55:23 +02:00
|
|
|
|
|
|
|
|
#include "fileutils.h"
|
|
|
|
|
|
2012-12-20 17:14:18 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2012-05-24 23:04:29 +04:00
|
|
|
#include <coreplugin/documentmanager.h>
|
2012-07-18 13:12:33 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2012-05-24 23:04:29 +04:00
|
|
|
#include <coreplugin/iversioncontrol.h>
|
2012-07-18 13:41:13 +04:00
|
|
|
#include <coreplugin/removefiledialog.h>
|
2012-05-24 23:04:29 +04:00
|
|
|
#include <coreplugin/vcsmanager.h>
|
2012-12-20 17:14:18 +01:00
|
|
|
#include <utils/consoleprocess.h>
|
2011-08-16 18:55:23 +02:00
|
|
|
#include <utils/environment.h>
|
2012-12-20 17:14:18 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
|
|
|
|
#include <utils/qtcprocess.h>
|
|
|
|
|
#include <utils/unixutils.h>
|
2011-08-16 18:55:23 +02:00
|
|
|
|
2012-12-20 17:14:18 +01:00
|
|
|
#include <QApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QMessageBox>
|
2012-12-20 17:14:18 +01:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QPushButton>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QWidget>
|
2011-08-16 18:55:23 +02:00
|
|
|
|
2012-05-24 23:04:29 +04:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
|
#include <QAbstractFileEngine>
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-20 17:14:18 +01:00
|
|
|
using namespace Utils;
|
2011-08-16 18:55:23 +02:00
|
|
|
|
2012-12-20 17:14:18 +01:00
|
|
|
namespace Core {
|
2011-08-16 18:55:23 +02:00
|
|
|
|
|
|
|
|
// Show error with option to open settings.
|
2012-12-20 17:14:18 +01:00
|
|
|
static void showGraphicalShellError(QWidget *parent, const QString &app, const QString &error)
|
2011-08-16 18:55:23 +02:00
|
|
|
{
|
|
|
|
|
const QString title = QApplication::translate("Core::Internal",
|
|
|
|
|
"Launching a file browser failed");
|
|
|
|
|
const QString msg = QApplication::translate("Core::Internal",
|
|
|
|
|
"Unable to start the file manager:\n\n%1\n\n").arg(app);
|
|
|
|
|
QMessageBox mbox(QMessageBox::Warning, title, msg, QMessageBox::Close, parent);
|
|
|
|
|
if (!error.isEmpty())
|
|
|
|
|
mbox.setDetailedText(QApplication::translate("Core::Internal",
|
|
|
|
|
"'%1' returned the following error:\n\n%2").arg(app, error));
|
|
|
|
|
QAbstractButton *settingsButton = mbox.addButton(QApplication::translate("Core::Internal", "Settings..."),
|
|
|
|
|
QMessageBox::ActionRole);
|
|
|
|
|
mbox.exec();
|
2012-12-29 01:31:08 +01:00
|
|
|
if (mbox.clickedButton() == settingsButton)
|
|
|
|
|
ICore::showOptionsDialog(Constants::SETTINGS_CATEGORY_CORE, Constants::SETTINGS_ID_ENVIRONMENT);
|
2011-08-16 18:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
|
|
|
|
|
{
|
|
|
|
|
// Mac, Windows support folder or file.
|
2012-12-20 17:14:18 +01:00
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
|
const QString explorer = Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
|
|
|
|
|
if (explorer.isEmpty()) {
|
|
|
|
|
QMessageBox::warning(parent,
|
|
|
|
|
QApplication::translate("Core::Internal",
|
|
|
|
|
"Launching Windows Explorer Failed"),
|
|
|
|
|
QApplication::translate("Core::Internal",
|
|
|
|
|
"Could not find explorer.exe in path to launch Windows Explorer."));
|
|
|
|
|
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);
|
2011-08-16 18:55:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileUtils::openTerminal(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
// Get terminal application
|
2012-12-20 17:14:18 +01:00
|
|
|
QString terminalEmulator;
|
2011-08-16 18:55:23 +02:00
|
|
|
QStringList args;
|
2012-12-20 17:14:18 +01:00
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
2012-12-29 20:41:11 +01:00
|
|
|
terminalEmulator = ConsoleProcess::defaultTerminalEmulator();
|
2012-12-20 17:14:18 +01:00
|
|
|
} else if (HostOsInfo::isMacHost()) {
|
|
|
|
|
terminalEmulator = ICore::resourcePath()
|
|
|
|
|
+ QLatin1String("/scripts/openTerminal.command");
|
|
|
|
|
} else {
|
|
|
|
|
args = QtcProcess::splitArgs(ConsoleProcess::terminalEmulator(ICore::settings()));
|
|
|
|
|
terminalEmulator = args.takeFirst();
|
|
|
|
|
args.append(QString::fromLocal8Bit(qgetenv("SHELL")));
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 18:55:23 +02:00
|
|
|
// Launch terminal with working directory set.
|
|
|
|
|
const QFileInfo fileInfo(path);
|
|
|
|
|
const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ?
|
|
|
|
|
fileInfo.absoluteFilePath() :
|
|
|
|
|
fileInfo.absolutePath());
|
|
|
|
|
QProcess::startDetached(terminalEmulator, args, pwd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileUtils::msgGraphicalShellAction()
|
|
|
|
|
{
|
2012-12-20 17:14:18 +01:00
|
|
|
if (HostOsInfo::isWindowsHost())
|
|
|
|
|
return QApplication::translate("Core::Internal", "Show in Explorer");
|
|
|
|
|
if (HostOsInfo::isMacHost())
|
|
|
|
|
return QApplication::translate("Core::Internal", "Show in Finder");
|
2011-11-15 10:19:20 +01:00
|
|
|
return QApplication::translate("Core::Internal", "Show Containing Folder");
|
2011-08-16 18:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileUtils::msgTerminalAction()
|
|
|
|
|
{
|
2012-12-20 17:14:18 +01:00
|
|
|
if (HostOsInfo::isWindowsHost())
|
|
|
|
|
return QApplication::translate("Core::Internal", "Open Command Prompt Here");
|
2011-11-15 10:19:20 +01:00
|
|
|
return QApplication::translate("Core::Internal", "Open Terminal Here");
|
2011-08-16 18:55:23 +02:00
|
|
|
}
|
2012-05-24 23:04:29 +04:00
|
|
|
|
2012-07-18 13:41:13 +04:00
|
|
|
void FileUtils::removeFile(const QString &filePath, bool deleteFromFS)
|
|
|
|
|
{
|
|
|
|
|
// remove from version control
|
|
|
|
|
ICore::vcsManager()->promptToDelete(filePath);
|
|
|
|
|
|
|
|
|
|
// remove from file system
|
|
|
|
|
if (deleteFromFS) {
|
|
|
|
|
QFile file(filePath);
|
|
|
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
// could have been deleted by vc
|
|
|
|
|
if (!file.remove())
|
|
|
|
|
QMessageBox::warning(ICore::mainWindow(),
|
|
|
|
|
QApplication::translate("Core::Internal", "Deleting File Failed"),
|
|
|
|
|
QApplication::translate("Core::Internal", "Could not delete file %1.").arg(filePath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-24 23:04:29 +04:00
|
|
|
static inline bool fileSystemRenameFile(const QString &orgFilePath,
|
|
|
|
|
const QString &newFilePath)
|
|
|
|
|
{
|
2012-11-29 12:20:21 +01:00
|
|
|
#if QT_VERSION < 0x050000
|
2012-11-28 17:32:47 +01:00
|
|
|
QAbstractFileEngine *fileEngine = QAbstractFileEngine::create(orgFilePath); // Due to QTBUG-3570
|
2012-05-24 23:04:29 +04:00
|
|
|
if (!fileEngine->caseSensitive() && orgFilePath.compare(newFilePath, Qt::CaseInsensitive) == 0)
|
|
|
|
|
return fileEngine->rename(newFilePath);
|
|
|
|
|
#endif
|
2012-11-29 12:20:21 +01:00
|
|
|
// QTBUG-3570 is also valid for Qt 5 but QAbstractFileEngine is now in a private header file and
|
|
|
|
|
// the symbol is not exported.
|
2012-05-24 23:04:29 +04:00
|
|
|
return QFile::rename(orgFilePath, newFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileUtils::renameFile(const QString &orgFilePath, const QString &newFilePath)
|
|
|
|
|
{
|
|
|
|
|
if (orgFilePath == newFilePath)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QString dir = QFileInfo(orgFilePath).absolutePath();
|
2012-12-20 17:14:18 +01:00
|
|
|
IVersionControl *vc = ICore::vcsManager()->findVersionControlForDirectory(dir);
|
2012-05-24 23:04:29 +04:00
|
|
|
|
|
|
|
|
bool result = false;
|
2012-12-20 17:14:18 +01:00
|
|
|
if (vc && vc->supportsOperation(IVersionControl::MoveOperation))
|
2012-05-24 23:04:29 +04:00
|
|
|
result = vc->vcsMove(orgFilePath, newFilePath);
|
|
|
|
|
if (!result) // The moving via vcs failed or the vcs does not support moving, fall back
|
|
|
|
|
result = fileSystemRenameFile(orgFilePath, newFilePath);
|
|
|
|
|
if (result) {
|
|
|
|
|
// yeah we moved, tell the filemanager about it
|
|
|
|
|
Core::DocumentManager::renamedFile(orgFilePath, newFilePath);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2012-12-20 17:14:18 +01:00
|
|
|
|
|
|
|
|
} // namespace Core
|