Commit 6e09fa8c authored by Christian Kandeler's avatar Christian Kandeler

Utils::FileUtils: Use Utils::FileName for file paths instead of QString.

Change-Id: I9ee4c0760820e9299e238c116936ce8ef140c727
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
Reviewed-by: default avatarDaniel Teske <daniel.teske@nokia.com>
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@nokia.com>
parent b21f7ca7
......@@ -138,7 +138,7 @@ bool BuildableHelperLibrary::copyFiles(const QString &sourcePath,
QString *errorMessage)
{
// try remove the directory
if (!FileUtils::removeRecursively(targetDirectory, errorMessage))
if (!FileUtils::removeRecursively(FileName::fromString(targetDirectory), errorMessage))
return false;
if (!QDir().mkpath(targetDirectory)) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory);
......
......@@ -59,14 +59,14 @@ namespace Utils {
\return Whether the operation succeeded.
*/
bool FileUtils::removeRecursively(const QString &filePath, QString *error)
bool FileUtils::removeRecursively(const FileName &filePath, QString *error)
{
QFileInfo fileInfo(filePath);
QFileInfo fileInfo = filePath.toFileInfo();
if (!fileInfo.exists() && !fileInfo.isSymLink())
return true;
QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
QFile::setPermissions(filePath.toString(), fileInfo.permissions() | QFile::WriteUser);
if (fileInfo.isDir()) {
QDir dir(filePath);
QDir dir(filePath.toString());
dir = dir.canonicalPath();
if (dir.isRoot()) {
if (error) {
......@@ -86,21 +86,21 @@ bool FileUtils::removeRecursively(const QString &filePath, QString *error)
QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
| QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QString &fileName, fileNames) {
if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error))
if (!removeRecursively(FileName(filePath).appendPath(fileName), error))
return false;
}
if (!QDir::root().rmdir(dir.path())) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory '%1'.")
.arg(QDir::toNativeSeparators(filePath));
.arg(filePath.toUserOutput());
}
return false;
}
} else {
if (!QFile::remove(filePath)) {
if (!QFile::remove(filePath.toString())) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file '%1'.")
.arg(QDir::toNativeSeparators(filePath));
.arg(filePath.toUserOutput());
}
return false;
}
......@@ -125,36 +125,36 @@ bool FileUtils::removeRecursively(const QString &filePath, QString *error)
\return Whether the operation succeeded.
*/
bool FileUtils::copyRecursively(const QString &srcFilePath,
const QString &tgtFilePath, QString *error)
bool FileUtils::copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath,
QString *error)
{
QFileInfo srcFileInfo(srcFilePath);
QFileInfo srcFileInfo = srcFilePath.toFileInfo();
if (srcFileInfo.isDir()) {
QDir targetDir(tgtFilePath);
QDir targetDir(tgtFilePath.toString());
targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) {
if (!targetDir.mkdir(tgtFilePath.toFileInfo().fileName())) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory '%1'.")
.arg(QDir::toNativeSeparators(tgtFilePath));
.arg(tgtFilePath.toUserOutput());
return false;
}
}
QDir sourceDir(srcFilePath);
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
QDir sourceDir(srcFilePath.toString());
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot
| QDir::Hidden | QDir::System);
foreach (const QString &fileName, fileNames) {
const QString newSrcFilePath
= srcFilePath + QLatin1Char('/') + fileName;
const QString newTgtFilePath
= tgtFilePath + QLatin1Char('/') + fileName;
FileName newSrcFilePath = srcFilePath;
newSrcFilePath.appendPath(fileName);
FileName newTgtFilePath = tgtFilePath;
newTgtFilePath.appendPath(fileName);
if (!copyRecursively(newSrcFilePath, newTgtFilePath, error))
return false;
}
} else {
if (!QFile::copy(srcFilePath, tgtFilePath)) {
if (!QFile::copy(srcFilePath.toString(), tgtFilePath.toString())) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file '%1' to '%2'.")
.arg(QDir::toNativeSeparators(srcFilePath),
QDir::toNativeSeparators(tgtFilePath));
.arg(srcFilePath.toUserOutput(), tgtFilePath.toUserOutput());
}
return false;
}
......@@ -169,19 +169,16 @@ bool FileUtils::copyRecursively(const QString &srcFilePath,
\return Whether at least one file in \a filePath has a newer date than \a timeStamp.
*/
bool FileUtils::isFileNewerThan(const QString &filePath,
const QDateTime &timeStamp)
bool FileUtils::isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp)
{
QFileInfo fileInfo(filePath);
QFileInfo fileInfo = filePath.toFileInfo();
if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp)
return true;
if (fileInfo.isDir()) {
const QStringList dirContents = QDir(filePath)
const QStringList dirContents = QDir(filePath.toString())
.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QString &curFileName, dirContents) {
const QString curFilePath
= filePath + QLatin1Char('/') + curFileName;
if (isFileNewerThan(curFilePath, timeStamp))
if (isFileNewerThan(FileName(filePath).appendPath(curFileName), timeStamp))
return true;
}
}
......@@ -197,15 +194,15 @@ bool FileUtils::isFileNewerThan(const QString &filePath,
return Symlink target file path.
*/
QString FileUtils::resolveSymlinks(const QString &path)
FileName FileUtils::resolveSymlinks(const FileName &path)
{
QFileInfo f(path);
QFileInfo f = path.toFileInfo();
int links = 16;
while (links-- && f.isSymLink())
f.setFile(f.symLinkTarget());
if (links <= 0)
return QString();
return f.filePath();
return FileName();
return FileName::fromString(f.filePath());
}
QByteArray FileReader::fetchQrc(const QString &fileName)
......
......@@ -50,14 +50,52 @@ QT_END_NAMESPACE
namespace Utils {
class QTCREATOR_UTILS_EXPORT FileName : private QString
{
public:
FileName();
explicit FileName(const QFileInfo &info);
QFileInfo toFileInfo() const;
static FileName fromString(const QString &filename);
static FileName fromUserInput(const QString &filename);
QString toString() const;
QString toUserOutput() const;
FileName parentDir() const;
bool operator==(const FileName &other) const;
bool operator!=(const FileName &other) const;
bool operator<(const FileName &other) const;
bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const;
bool isChildOf(const FileName &s) const;
bool isChildOf(const QDir &dir) const;
bool endsWith(const QString &s) const;
Utils::FileName relativeChildPath(const FileName &parent) const;
Utils::FileName &appendPath(const QString &s);
Utils::FileName &append(const QString &str);
Utils::FileName &append(QChar str);
using QString::size;
using QString::count;
using QString::length;
using QString::isEmpty;
using QString::isNull;
using QString::clear;
private:
FileName(const QString &string);
};
class QTCREATOR_UTILS_EXPORT FileUtils {
public:
static bool removeRecursively(const QString &filePath, QString *error = 0);
static bool copyRecursively(const QString &srcFilePath,
const QString &tgtFilePath, QString *error = 0);
static bool isFileNewerThan(const QString &filePath,
const QDateTime &timeStamp);
static QString resolveSymlinks(const QString &path);
static bool removeRecursively(const FileName &filePath, QString *error = 0);
static bool copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath,
QString *error = 0);
static bool isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp);
static FileName resolveSymlinks(const FileName &path);
};
class QTCREATOR_UTILS_EXPORT FileReader
......@@ -139,45 +177,6 @@ private:
bool m_autoRemove;
};
class QTCREATOR_UTILS_EXPORT FileName : private QString
{
public:
FileName();
explicit FileName(const QFileInfo &info);
QFileInfo toFileInfo() const;
static FileName fromString(const QString &filename);
static FileName fromUserInput(const QString &filename);
QString toString() const;
QString toUserOutput() const;
FileName parentDir() const;
bool operator==(const FileName &other) const;
bool operator!=(const FileName &other) const;
bool operator<(const FileName &other) const;
bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const;
bool isChildOf(const FileName &s) const;
bool isChildOf(const QDir &dir) const;
bool endsWith(const QString &s) const;
Utils::FileName relativeChildPath(const FileName &parent) const;
Utils::FileName &appendPath(const QString &s);
Utils::FileName &append(const QString &str);
Utils::FileName &append(QChar str);
using QString::size;
using QString::count;
using QString::length;
using QString::isEmpty;
using QString::isNull;
using QString::clear;
private:
FileName(const QString &string);
};
} // namespace Utils
QT_BEGIN_NAMESPACE
......
......@@ -100,7 +100,8 @@ bool SaveFile::commit()
return false;
}
QString finalFileName = Utils::FileUtils::resolveSymlinks(m_finalFileName);
QString finalFileName
= FileUtils::resolveSymlinks(FileName::fromString(m_finalFileName)).toString();
QString bakname = finalFileName + QLatin1Char('~');
QFile::remove(bakname); // Kill old backup
QFile::rename(finalFileName, bakname); // Backup current file
......
......@@ -636,7 +636,7 @@ DebianManager::ActionStatus DebianManager::createTemplate(Qt4ProjectManager::Qt4
if (!QFile::rename(location.appendPath(QLatin1String("debian")).toString(), debianDir.toString())) {
raiseError(tr("Unable to move new debian directory to '%1'.").arg(debianDir.toUserOutput()));
Utils::FileUtils::removeRecursively(location.toString(), &error);
Utils::FileUtils::removeRecursively(location, &error);
return ActionFailed;
}
......
......@@ -299,9 +299,9 @@ void MaemoCopyToSysrootStep::run(QFutureInterface<bool> &fi)
+ deployable.remoteDirectory() + sep + localFileInfo.fileName();
sysrootDir.mkpath(deployable.remoteDirectory().mid(1));
QString errorMsg;
Utils::FileUtils::removeRecursively(targetFilePath, &errorMsg);
if (!Utils::FileUtils::copyRecursively(deployable.localFilePath().toString(),
targetFilePath, &errorMsg)) {
Utils::FileUtils::removeRecursively(Utils::FileName::fromString(targetFilePath), &errorMsg);
if (!Utils::FileUtils::copyRecursively(deployable.localFilePath(),
Utils::FileName::fromString(targetFilePath), &errorMsg)) {
emit addOutput(tr("Sysroot installation failed: %1\n"
" Continuing anyway.").arg(errorMsg), ErrorMessageOutput);
}
......
......@@ -372,7 +372,7 @@ bool MaemoDebianPackageCreationStep::copyDebianFiles(bool inSourceBuild)
return false;
}
QString error;
if (!FileUtils::removeRecursively(debianDirPath, &error)) {
if (!FileUtils::removeRecursively(FileName::fromString(debianDirPath), &error)) {
raiseError(tr("Packaging failed: Could not remove directory '%1': %2")
.arg(debianDirPath, error));
return false;
......
......@@ -58,6 +58,7 @@ using namespace Core;
using namespace Qt4ProjectManager;
using namespace RemoteLinux;
using namespace QSsh;
using namespace Utils;
namespace Madde {
namespace Internal {
......@@ -127,7 +128,7 @@ void MaemoPublisherFremantleFree::createPackage()
if (QFileInfo(tmpDirContainer()).exists()) {
emit progressReport(tr("Removing left-over temporary directory..."));
QString error;
if (!Utils::FileUtils::removeRecursively(tmpDirContainer(), &error)) {
if (!FileUtils::removeRecursively(FileName::fromString(tmpDirContainer()), &error)) {
finishWithFailure(tr("Error removing temporary directory: %1").arg(error),
tr("Publishing failed: Could not create source package."));
return;
......@@ -201,7 +202,7 @@ bool MaemoPublisherFremantleFree::copyRecursively(const QString &srcFilePath,
}
} else {
if (tgtFilePath == m_tmpProjectDir + QLatin1String("/debian/rules")) {
Utils::FileReader reader;
FileReader reader;
if (!reader.fetch(srcFilePath)) {
emit progressReport(reader.errorString(), ErrorOutput);
return false;
......@@ -211,7 +212,7 @@ bool MaemoPublisherFremantleFree::copyRecursively(const QString &srcFilePath,
rulesContents.replace("# Add here commands to configure the package.",
"qmake " + QFileInfo(m_project->document()->fileName()).fileName().toLocal8Bit());
MaemoDebianPackageCreationStep::ensureShlibdeps(rulesContents);
Utils::FileSaver saver(tgtFilePath);
FileSaver saver(tgtFilePath);
saver.write(rulesContents);
if (!saver.finalize()) {
emit progressReport(saver.errorString(), ErrorOutput);
......@@ -243,7 +244,7 @@ bool MaemoPublisherFremantleFree::fixNewlines()
const QStringList &fileNames = debianDir.entryList(QDir::Files);
foreach (const QString &fileName, fileNames) {
QString filePath = debianDir.filePath(fileName);
Utils::FileReader reader;
FileReader reader;
if (!reader.fetch(filePath))
return false;
QByteArray contents = reader.data();
......@@ -251,7 +252,7 @@ bool MaemoPublisherFremantleFree::fixNewlines()
if (!contents.contains(crlf))
continue;
contents.replace(crlf, "\n");
Utils::FileSaver saver(filePath);
FileSaver saver(filePath);
saver.write(contents);
if (!saver.finalize())
return false;
......@@ -365,7 +366,7 @@ void MaemoPublisherFremantleFree::runDpkgBuildPackage()
}
foreach (const QString &filePath, d.filesToExclude()) {
QString error;
if (!Utils::FileUtils::removeRecursively(filePath, &error)) {
if (!FileUtils::removeRecursively(FileName::fromString(filePath), &error)) {
finishWithFailure(error,
tr("Publishing failed: Could not create package."));
}
......
......@@ -72,6 +72,8 @@
#include <QDeclarativeContext>
#include <QDesktopServices>
using namespace Utils;
namespace QtSupport {
namespace Internal {
......@@ -337,9 +339,9 @@ QString ExamplesWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileI
.arg(nativeProjectDir));
lay->addWidget(descrLbl, 0, 0, 1, 2);
QLabel *txt = new QLabel(tr("&Location:"));
Utils::PathChooser *chooser = new Utils::PathChooser;
PathChooser *chooser = new PathChooser;
txt->setBuddy(chooser);
chooser->setExpectedKind(Utils::PathChooser::ExistingDirectory);
chooser->setExpectedKind(PathChooser::ExistingDirectory);
QSettings *settings = Core::ICore::settings();
chooser->setPath(settings->value(QString::fromLatin1(C_FALLBACK_ROOT),
Core::DocumentManager::projectsDirectory()).toString());
......@@ -368,15 +370,18 @@ QString ExamplesWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileI
} else {
QString error;
QString targetDir = destBaseDir + QLatin1Char('/') + exampleDirName;
if (Utils::FileUtils::copyRecursively(projectDir, targetDir, &error)) {
if (FileUtils::copyRecursively(FileName::fromString(projectDir),
FileName::fromString(targetDir), &error)) {
// set vars to new location
const QStringList::Iterator end = filesToOpen.end();
for (QStringList::Iterator it = filesToOpen.begin(); it != end; ++it)
it->replace(projectDir, targetDir);
foreach (const QString &dependency, dependencies) {
QString dirName = QDir(dependency).dirName();
if (!Utils::FileUtils::copyRecursively(dependency, targetDir + QDir::separator()+ dirName, &error)) {
FileName targetFile = FileName::fromString(targetDir);
targetFile.appendPath(QDir(dependency).dirName());
if (!FileUtils::copyRecursively(FileName::fromString(dependency), targetFile,
&error)) {
QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Copy Project"), error);
// do not fail, just warn;
}
......
......@@ -137,7 +137,7 @@ bool AbstractPackagingStep::isPackagingNeeded() const
const DeploymentData &dd = target()->deploymentData();
for (int i = 0; i < dd.fileCount(); ++i) {
if (Utils::FileUtils::isFileNewerThan(dd.fileAt(i).localFilePath().toString(),
if (Utils::FileUtils::isFileNewerThan(dd.fileAt(i).localFilePath(),
packageInfo.lastModified())) {
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment