Browse Source

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
adaptive-webui-19844
Nick Tiskov 11 years ago
parent
commit
1334386a1b
  1. 14
      src/addnewtorrentdialog.cpp
  2. 99
      src/fs_utils.cpp
  3. 48
      src/fs_utils.h
  4. 4
      src/preferences/options_imp.cpp
  5. 6
      src/qtlibtorrent/qbtsession.cpp

14
src/addnewtorrentdialog.cpp

@ -68,7 +68,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent) :
QIniSettings settings; QIniSettings settings;
Preferences pref; Preferences pref;
ui->start_torrent_cb->setChecked(!pref.addTorrentsInPause()); ui->start_torrent_cb->setChecked(!pref.addTorrentsInPause());
ui->save_path_combo->addItem(fsutils::toDisplayPath(pref.getSavePath()), pref.getSavePath()); ui->save_path_combo->addItem(fsutils::toNativePath(pref.getSavePath()), pref.getSavePath());
loadSavePathHistory(); loadSavePathHistory();
ui->save_path_combo->insertSeparator(ui->save_path_combo->count()); ui->save_path_combo->insertSeparator(ui->save_path_combo->count());
ui->save_path_combo->addItem(tr("Other...", "Other save path...")); ui->save_path_combo->addItem(tr("Other...", "Other save path..."));
@ -242,7 +242,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString& torrent_path, const QString
// Update save paths (append file name to them) // Update save paths (append file name to them)
QString single_file_relpath = misc::toQStringU(fs.file_path(0)); QString single_file_relpath = misc::toQStringU(fs.file_path(0));
for (int i=0; i<ui->save_path_combo->count()-1; ++i) { for (int i=0; i<ui->save_path_combo->count()-1; ++i) {
ui->save_path_combo->setItemText(i, fsutils::toDisplayPath(QDir(ui->save_path_combo->itemText(i)).absoluteFilePath(single_file_relpath))); ui->save_path_combo->setItemText(i, fsutils::toNativePath(QDir(ui->save_path_combo->itemText(i)).absoluteFilePath(single_file_relpath)));
} }
} }
@ -326,7 +326,7 @@ void AddNewTorrentDialog::updateFileNameInSavePaths(const QString &new_filename)
{ {
for(int i=0; i<ui->save_path_combo->count()-1; ++i) { for(int i=0; i<ui->save_path_combo->count()-1; ++i) {
const QDir folder(ui->save_path_combo->itemData(i).toString()); const QDir folder(ui->save_path_combo->itemData(i).toString());
ui->save_path_combo->setItemText(i, fsutils::toDisplayPath(folder.absoluteFilePath(new_filename))); ui->save_path_combo->setItemText(i, fsutils::toNativePath(folder.absoluteFilePath(new_filename)));
} }
} }
@ -383,9 +383,9 @@ void AddNewTorrentDialog::onSavePathChanged(int index)
else { else {
// New path, prepend to combo box // New path, prepend to combo box
if (!new_filename.isEmpty()) if (!new_filename.isEmpty())
ui->save_path_combo->insertItem(0, fsutils::toDisplayPath(QDir(new_path).absoluteFilePath(new_filename)), new_path); ui->save_path_combo->insertItem(0, fsutils::toNativePath(QDir(new_path).absoluteFilePath(new_filename)), new_path);
else else
ui->save_path_combo->insertItem(0, fsutils::toDisplayPath(new_path), new_path); ui->save_path_combo->insertItem(0, fsutils::toNativePath(new_path), new_path);
ui->save_path_combo->setCurrentIndex(0); ui->save_path_combo->setCurrentIndex(0);
} }
// Update file name in all save_paths // Update file name in all save_paths
@ -543,7 +543,7 @@ void AddNewTorrentDialog::loadSavePathHistory()
QStringList raw_path_history = settings.value("TorrentAdditionDlg/save_path_history").toStringList(); QStringList raw_path_history = settings.value("TorrentAdditionDlg/save_path_history").toStringList();
foreach (const QString &sp, raw_path_history) { foreach (const QString &sp, raw_path_history) {
if (QDir(sp) != default_save_path) if (QDir(sp) != default_save_path)
ui->save_path_combo->addItem(fsutils::toDisplayPath(sp), sp); ui->save_path_combo->addItem(fsutils::toNativePath(sp), sp);
} }
} }
@ -696,7 +696,7 @@ void AddNewTorrentDialog::updateMetadata(const QTorrentHandle &h) {
// Update save paths (append file name to them) // Update save paths (append file name to them)
QString single_file_relpath = misc::toQStringU(fs.file_path(0)); QString single_file_relpath = misc::toQStringU(fs.file_path(0));
for (int i=0; i<ui->save_path_combo->count()-1; ++i) { for (int i=0; i<ui->save_path_combo->count()-1; ++i) {
ui->save_path_combo->setItemText(i, fsutils::toDisplayPath(QDir(ui->save_path_combo->itemText(i)).absoluteFilePath(single_file_relpath))); ui->save_path_combo->setItemText(i, fsutils::toNativePath(QDir(ui->save_path_combo->itemText(i)).absoluteFilePath(single_file_relpath)));
} }
} }

99
src/fs_utils.cpp

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

48
src/fs_utils.h

@ -42,30 +42,32 @@ class fsutils
Q_DECLARE_TR_FUNCTIONS(fsutils) Q_DECLARE_TR_FUNCTIONS(fsutils)
public: public:
static QString toDisplayPath(const QString& path); static QString toNativePath(const QString& path);
static QString fileExtension(const QString& filename); static QString fromNativePath(const QString& path);
static QString fileName(const QString& file_path); static QString fileExtension(const QString& filename);
static qint64 computePathSize(const QString& path); static QString fileName(const QString& file_path);
static bool sameFiles(const QString& path1, const QString& path2); static qint64 computePathSize(const QString& path);
static QString updateLabelInSavePath(QString defaultSavePath, QString save_path, const QString& old_label, const QString& new_label); static bool sameFiles(const QString& path1, const QString& path2);
static QString toValidFileSystemName(QString filename); static QString updateLabelInSavePath(const QString &defaultSavePath, const QString &save_path, const QString& old_label, const QString& new_label);
static bool isValidFileSystemName(const QString& filename); static QString toValidFileSystemName(QString filename);
static long long freeDiskSpaceOnPath(QString path); static bool isValidFileSystemName(const QString& filename);
static QString branchPath(const QString& file_path, QString* removed = 0); static long long freeDiskSpaceOnPath(QString path);
static bool sameFileNames(const QString& first, const QString& second); static QString branchPath(const QString& file_path, QString* removed = 0);
static QString expandPath(const QString& path); static bool sameFileNames(const QString& first, const QString& second);
static bool isValidTorrentFile(const QString& path); static QString expandPath(const QString& path);
static bool smartRemoveEmptyFolderTree(const QString& dir_path); static QString expandPathAbs(const QString& path);
static bool forceRemove(const QString& file_path); static bool isValidTorrentFile(const QString& path);
static bool smartRemoveEmptyFolderTree(const QString& dir_path);
static bool forceRemove(const QString& file_path);
/* Ported from Qt4 to drop dependency on QtGui */ /* Ported from Qt4 to drop dependency on QtGui */
static QString QDesktopServicesDataLocation(); static QString QDesktopServicesDataLocation();
static QString QDesktopServicesCacheLocation(); static QString QDesktopServicesCacheLocation();
static QString QDesktopServicesDownloadLocation(); static QString QDesktopServicesDownloadLocation();
/* End of Qt4 code */ /* End of Qt4 code */
static QString searchEngineLocation(); static QString searchEngineLocation();
static QString BTBackupLocation(); static QString BTBackupLocation();
static QString cacheLocation(); static QString cacheLocation();
}; };

4
src/preferences/options_imp.cpp

@ -1146,13 +1146,13 @@ QString options_imp::askForExportDir(const QString& currentExportPath)
void options_imp::on_browseExportDirButton_clicked() { void options_imp::on_browseExportDirButton_clicked() {
const QString newExportDir = askForExportDir(textExportDir->text()); const QString newExportDir = askForExportDir(textExportDir->text());
if (!newExportDir.isNull()) if (!newExportDir.isNull())
textExportDir->setText(fsutils::toDisplayPath(newExportDir)); textExportDir->setText(fsutils::toNativePath(newExportDir));
} }
void options_imp::on_browseExportDirFinButton_clicked() { void options_imp::on_browseExportDirFinButton_clicked() {
const QString newExportDir = askForExportDir(textExportDirFin->text()); const QString newExportDir = askForExportDir(textExportDirFin->text());
if (!newExportDir.isNull()) if (!newExportDir.isNull())
textExportDirFin->setText(fsutils::toDisplayPath(newExportDir)); textExportDirFin->setText(fsutils::toNativePath(newExportDir));
} }
void options_imp::on_browseFilterButton_clicked() { void options_imp::on_browseFilterButton_clicked() {

6
src/qtlibtorrent/qbtsession.cpp

@ -1201,9 +1201,9 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(from_url)); addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(from_url));
}else{ }else{
if (fastResume) if (fastResume)
addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(fsutils::toDisplayPath(path))); addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(fsutils::toNativePath(path)));
else else
addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(fsutils::toDisplayPath(path))); addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(fsutils::toNativePath(path)));
} }
// Send torrent addition signal // Send torrent addition signal
@ -1783,7 +1783,7 @@ void QBtSession::setDefaultTempPath(const QString &temppath) {
if (!h.is_valid()) continue; if (!h.is_valid()) continue;
if (!h.is_seed()) { if (!h.is_seed()) {
QString torrent_tmp_path = QDir::fromNativeSeparators(temppath); QString torrent_tmp_path = QDir::fromNativeSeparators(temppath);
qDebug("Moving torrent to its temp save path: %s", qPrintable(fsutils::toDisplayPath(torrent_tmp_path))); qDebug("Moving torrent to its temp save path: %s", qPrintable(fsutils::toNativePath(torrent_tmp_path)));
h.move_storage(torrent_tmp_path); h.move_storage(torrent_tmp_path);
} }
} }

Loading…
Cancel
Save