Rework fsutils class to only use Qt-style separators

fsutils methods are guaranteed to accept strings with both native and qt-style separators and return strings with qt-style separators where appropriate
This commit is contained in:
Nick Tiskov 2013-11-05 22:33:04 +04:00
commit 1334386a1b
5 changed files with 87 additions and 84 deletions

View file

@ -71,11 +71,14 @@ using namespace libtorrent;
* This function makes sure the directory separator used is consistent
* with the OS being run.
*/
QString fsutils::toDisplayPath(const QString& path)
{
QString fsutils::toNativePath(const QString& path) {
return QDir::toNativeSeparators(path);
}
QString fsutils::fromNativePath(const QString &path) {
return QDir::fromNativeSeparators(path);
}
/**
* Returns the file extension part of a file name.
*/
@ -87,15 +90,16 @@ QString fsutils::fileExtension(const QString &filename)
QString fsutils::fileName(const QString& file_path)
{
const int slash_index = file_path.lastIndexOf(QRegExp("[/\\\\]"));
QString path = fsutils::fromNativePath(file_path);
const int slash_index = path.lastIndexOf("/");
if (slash_index == -1)
return file_path;
return file_path.mid(slash_index + 1);
return path;
return path.mid(slash_index + 1);
}
bool fsutils::isValidTorrentFile(const QString& torrent_path) {
try {
boost::intrusive_ptr<libtorrent::torrent_info> t = new torrent_info(torrent_path.toUtf8().constData());
boost::intrusive_ptr<libtorrent::torrent_info> t = new torrent_info(fsutils::toNativePath(torrent_path).toUtf8().constData());
if (!t->is_valid() || t->num_files() == 0)
return false;
} catch(std::exception&) {
@ -220,14 +224,14 @@ bool fsutils::sameFiles(const QString& path1, const QString& path2)
return same;
}
QString fsutils::updateLabelInSavePath(QString defaultSavePath,QString save_path, const QString& old_label, const QString& new_label) {
if (old_label == new_label) return save_path;
defaultSavePath.replace("\\", "/");
save_path.replace("\\", "/");
qDebug("UpdateLabelInSavePath(%s, %s, %s)", qPrintable(save_path), qPrintable(old_label), qPrintable(new_label));
if (!save_path.startsWith(defaultSavePath)) return save_path;
QString new_save_path = save_path;
new_save_path.replace(defaultSavePath, "");
QString fsutils::updateLabelInSavePath(const QString& defaultSavePath, const QString& save_path, const QString& old_label, const QString& new_label) {
if (old_label == new_label) return fsutils::fromNativePath(save_path);
QString defaultPath = fsutils::fromNativePath(defaultSavePath);
QString path = fsutils::fromNativePath(save_path);
qDebug("UpdateLabelInSavePath(%s, %s, %s)", qPrintable(path), qPrintable(old_label), qPrintable(new_label));
if (!path.startsWith(defaultPath)) return path;
QString new_save_path = path;
new_save_path.remove(defaultPath);
QStringList path_parts = new_save_path.split("/", QString::SkipEmptyParts);
if (path_parts.empty()) {
if (!new_label.isEmpty())
@ -245,9 +249,9 @@ QString fsutils::updateLabelInSavePath(QString defaultSavePath,QString save_path
}
}
}
new_save_path = defaultSavePath;
if (!new_save_path.endsWith(QDir::separator())) new_save_path += QDir::separator();
new_save_path += path_parts.join(QDir::separator());
new_save_path = defaultPath;
if (!new_save_path.endsWith("/")) new_save_path += "/";
new_save_path += path_parts.join("/");
qDebug("New save path is %s", qPrintable(new_save_path));
return new_save_path;
}
@ -268,7 +272,6 @@ bool fsutils::isValidFileSystemName(const QString& filename) {
long long fsutils::freeDiskSpaceOnPath(QString path) {
if (path.isEmpty()) return -1;
path.replace("\\", "/");
QDir dir_path(path);
if (!dir_path.exists()) {
QStringList parts = path.split("/");
@ -307,7 +310,7 @@ long long fsutils::freeDiskSpaceOnPath(QString path) {
{
ULARGE_INTEGER bytesFree, bytesTotal;
unsigned long long *ret;
if (pGetDiskFreeSpaceEx((LPCTSTR)(QDir::toNativeSeparators(dir_path.path())).utf16(), &bytesFree, &bytesTotal, NULL)) {
if (pGetDiskFreeSpaceEx((LPCTSTR)(fsutils::toNativePath(dir_path.path())).utf16(), &bytesFree, &bytesTotal, NULL)) {
ret = (unsigned long long*)&bytesFree;
return *ret;
} else {
@ -321,10 +324,10 @@ long long fsutils::freeDiskSpaceOnPath(QString path) {
QString fsutils::branchPath(const QString& file_path, QString* removed)
{
QString ret = file_path;
if (ret.endsWith("/") || ret.endsWith("\\"))
QString ret = fsutils::fromNativePath(file_path);
if (ret.endsWith("/"))
ret.chop(1);
const int slashIndex = ret.lastIndexOf(QRegExp("[/\\\\]"));
const int slashIndex = ret.lastIndexOf("/");
if (slashIndex >= 0) {
if (removed)
*removed = ret.mid(slashIndex + 1);
@ -342,35 +345,33 @@ bool fsutils::sameFileNames(const QString &first, const QString &second)
#endif
}
// Replace ~ in path
QString fsutils::expandPath(const QString& path) {
QString ret = path.trimmed();
if (ret.isEmpty()) return ret;
if (ret == "~")
return QDir::homePath();
if (ret[0] == '~' && (ret[1] == '/' || ret[1] == '\\')) {
ret.replace(0, 1, QDir::homePath());
} else {
if (!QDir::isAbsolutePath(ret))
ret = QDir(ret).absolutePath();
}
return QDir::cleanPath(path);
QString fsutils::expandPath(const QString &path) {
QString ret = fsutils::fromNativePath(path.trimmed());
if (ret.isEmpty())
return ret;
return QDir::cleanPath(ret);
}
QString fsutils::expandPathAbs(const QString& path) {
QString ret = fsutils::expandPath(path);
if (!QDir::isAbsolutePath(ret))
ret = QDir(ret).absolutePath();
return ret;
}
QString fsutils::QDesktopServicesDataLocation() {
#ifdef Q_WS_WIN
LPWSTR path=new WCHAR[256];
QString result;
#if defined Q_WS_WINCE
if (SHGetSpecialFolderPath(0, path, CSIDL_APPDATA, FALSE))
#else
if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE))
#endif
result = QString::fromWCharArray(path);
result = fsutils::fromNativePath(QString::fromWCharArray(path));
if (!QCoreApplication::applicationName().isEmpty())
result = result + QLatin1String("\\") + qApp->applicationName();
if (!result.endsWith("\\"))
result += "\\";
result += QLatin1String("/") + qApp->applicationName();
if (!result.endsWith("/"))
result += "/";
return result;
#else
#ifdef Q_WS_MAC
@ -397,7 +398,7 @@ QString fsutils::QDesktopServicesDataLocation() {
QString fsutils::QDesktopServicesCacheLocation() {
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
return QDesktopServicesDataLocation() + QLatin1String("\\cache");
return QDesktopServicesDataLocation() + QLatin1String("cache");
#else
#ifdef Q_WS_MAC
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
@ -477,8 +478,8 @@ QString fsutils::searchEngineLocation() {
QString folder = "nova";
if (misc::pythonVersion() >= 3)
folder = "nova3";
const QString location = QDir::cleanPath(QDesktopServicesDataLocation()
+ QDir::separator() + folder);
const QString location = fsutils::expandPathAbs(QDesktopServicesDataLocation()
+ folder);
QDir locationDir(location);
if (!locationDir.exists())
locationDir.mkpath(locationDir.absolutePath());
@ -486,8 +487,8 @@ QString fsutils::searchEngineLocation() {
}
QString fsutils::BTBackupLocation() {
const QString location = QDir::cleanPath(QDesktopServicesDataLocation()
+ QDir::separator() + "BT_backup");
const QString location = fsutils::expandPathAbs(QDesktopServicesDataLocation()
+ "BT_backup");
QDir locationDir(location);
if (!locationDir.exists())
locationDir.mkpath(locationDir.absolutePath());
@ -495,7 +496,7 @@ QString fsutils::BTBackupLocation() {
}
QString fsutils::cacheLocation() {
QString location = QDir::cleanPath(QDesktopServicesCacheLocation());
QString location = fsutils::expandPathAbs(QDesktopServicesCacheLocation());
QDir locationDir(location);
if (!locationDir.exists())
locationDir.mkpath(locationDir.absolutePath());