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) : @@ -68,7 +68,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent) :
QIniSettings settings;
Preferences pref;
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();
ui->save_path_combo->insertSeparator(ui->save_path_combo->count());
ui->save_path_combo->addItem(tr("Other...", "Other save path..."));
@ -242,7 +242,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString& torrent_path, const QString @@ -242,7 +242,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString& torrent_path, const QString
// Update save paths (append file name to them)
QString single_file_relpath = misc::toQStringU(fs.file_path(0));
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) @@ -326,7 +326,7 @@ void AddNewTorrentDialog::updateFileNameInSavePaths(const QString &new_filename)
{
for(int i=0; i<ui->save_path_combo->count()-1; ++i) {
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) @@ -383,9 +383,9 @@ void AddNewTorrentDialog::onSavePathChanged(int index)
else {
// New path, prepend to combo box
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
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);
}
// Update file name in all save_paths
@ -543,7 +543,7 @@ void AddNewTorrentDialog::loadSavePathHistory() @@ -543,7 +543,7 @@ void AddNewTorrentDialog::loadSavePathHistory()
QStringList raw_path_history = settings.value("TorrentAdditionDlg/save_path_history").toStringList();
foreach (const QString &sp, raw_path_history) {
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) { @@ -696,7 +696,7 @@ void AddNewTorrentDialog::updateMetadata(const QTorrentHandle &h) {
// Update save paths (append file name to them)
QString single_file_relpath = misc::toQStringU(fs.file_path(0));
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; @@ -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) @@ -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) @@ -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 @@ -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) { @@ -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) { @@ -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) { @@ -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) @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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());

48
src/fs_utils.h

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

4
src/preferences/options_imp.cpp

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

6
src/qtlibtorrent/qbtsession.cpp

@ -1201,9 +1201,9 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr @@ -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));
}else{
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
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
@ -1783,7 +1783,7 @@ void QBtSession::setDefaultTempPath(const QString &temppath) { @@ -1783,7 +1783,7 @@ void QBtSession::setDefaultTempPath(const QString &temppath) {
if (!h.is_valid()) continue;
if (!h.is_seed()) {
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);
}
}

Loading…
Cancel
Save