mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-14 02:27:09 -07:00
Minor code refactoring
This commit is contained in:
parent
3bb474e12f
commit
a85ddada25
3 changed files with 54 additions and 53 deletions
|
@ -112,6 +112,58 @@ bool fsutils::isValidTorrentFile(const QString& torrent_path) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an empty folder tree.
|
||||||
|
*
|
||||||
|
* This function will also remove .DS_Store files on Mac OS and
|
||||||
|
* Thumbs.db on Windows.
|
||||||
|
*/
|
||||||
|
bool fsutils::smartRemoveEmptyFolderTree(const QString& dir_path)
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO << dir_path;
|
||||||
|
if (dir_path.isEmpty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QDir dir(dir_path);
|
||||||
|
if (!dir.exists())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Remove Files created by the OS
|
||||||
|
#if defined Q_WS_MAC
|
||||||
|
QFile::remove(dir_path + QLatin1String("/.DS_Store"));
|
||||||
|
#elif defined Q_WS_WIN
|
||||||
|
QFile::remove(dir_path + QLatin1String("/Thumbs.db"));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
QFileInfoList sub_files = dir.entryInfoList();
|
||||||
|
foreach (const QFileInfo& info, sub_files) {
|
||||||
|
QString sub_name = info.fileName();
|
||||||
|
if (sub_name == "." || sub_name == "..")
|
||||||
|
continue;
|
||||||
|
|
||||||
|
QString sub_path = info.absoluteFilePath();
|
||||||
|
qDebug() << Q_FUNC_INFO << "sub file: " << sub_path;
|
||||||
|
if (info.isDir()) {
|
||||||
|
if (!smartRemoveEmptyFolderTree(sub_path)) {
|
||||||
|
qWarning() << Q_FUNC_INFO << "Failed to remove folder: " << sub_path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (info.isHidden()) {
|
||||||
|
qDebug() << Q_FUNC_INFO << "Removing hidden file: " << sub_path;
|
||||||
|
if (!QFile::remove(sub_path)) {
|
||||||
|
qWarning() << Q_FUNC_INFO << "Failed to remove " << sub_path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << Q_FUNC_INFO << "Folder is not empty, aborting. Found: " << sub_path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qDebug() << Q_FUNC_INFO << "Calling rmdir on " << dir_path;
|
||||||
|
return QDir().rmdir(dir_path);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size of a file.
|
* Returns the size of a file.
|
||||||
* If the file is a folder, it will compute its size based on its content.
|
* If the file is a folder, it will compute its size based on its content.
|
||||||
|
|
|
@ -56,6 +56,7 @@ static QString branchPath(const QString& file_path, QString* removed = 0);
|
||||||
static bool sameFileNames(const QString& first, const QString& second);
|
static bool sameFileNames(const QString& first, const QString& second);
|
||||||
static QString expandPath(const QString& path);
|
static QString expandPath(const QString& path);
|
||||||
static bool isValidTorrentFile(const QString& path);
|
static bool isValidTorrentFile(const QString& path);
|
||||||
|
static bool smartRemoveEmptyFolderTree(const QString& dir_path);
|
||||||
|
|
||||||
/* Ported from Qt4 to drop dependency on QtGui */
|
/* Ported from Qt4 to drop dependency on QtGui */
|
||||||
static QString QDesktopServicesDataLocation();
|
static QString QDesktopServicesDataLocation();
|
||||||
|
|
|
@ -98,58 +98,6 @@ static libtorrent::sha1_hash QStringToSha1(const QString& s) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an empty folder tree.
|
|
||||||
*
|
|
||||||
* This function will also remove .DS_Store files on Mac OS and
|
|
||||||
* Thumbs.db on Windows.
|
|
||||||
*/
|
|
||||||
static bool smartRemoveEmptyFolderTree(const QString& dir_path)
|
|
||||||
{
|
|
||||||
qDebug() << Q_FUNC_INFO << dir_path;
|
|
||||||
if (dir_path.isEmpty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
QDir dir(dir_path);
|
|
||||||
if (!dir.exists())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// Remove Files created by the OS
|
|
||||||
#if defined Q_WS_MAC
|
|
||||||
QFile::remove(dir_path + QLatin1String("/.DS_Store"));
|
|
||||||
#elif defined Q_WS_WIN
|
|
||||||
QFile::remove(dir_path + QLatin1String("/Thumbs.db"));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QFileInfoList sub_files = dir.entryInfoList();
|
|
||||||
foreach (const QFileInfo& info, sub_files) {
|
|
||||||
QString sub_name = info.fileName();
|
|
||||||
if (sub_name == "." || sub_name == "..")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
QString sub_path = info.absoluteFilePath();
|
|
||||||
qDebug() << Q_FUNC_INFO << "sub file: " << sub_path;
|
|
||||||
if (info.isDir()) {
|
|
||||||
if (!smartRemoveEmptyFolderTree(sub_path)) {
|
|
||||||
qWarning() << Q_FUNC_INFO << "Failed to remove folder: " << sub_path;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (info.isHidden()) {
|
|
||||||
qDebug() << Q_FUNC_INFO << "Removing hidden file: " << sub_path;
|
|
||||||
if (!QFile::remove(sub_path)) {
|
|
||||||
qWarning() << Q_FUNC_INFO << "Failed to remove " << sub_path;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
qWarning() << Q_FUNC_INFO << "Folder is not empty, aborting. Found: " << sub_path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
qDebug() << Q_FUNC_INFO << "Calling rmdir on " << dir_path;
|
|
||||||
return QDir().rmdir(dir_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Main constructor
|
// Main constructor
|
||||||
QBtSession::QBtSession()
|
QBtSession::QBtSession()
|
||||||
: m_scanFolders(ScanFoldersModel::instance(this)),
|
: m_scanFolders(ScanFoldersModel::instance(this)),
|
||||||
|
@ -2336,7 +2284,7 @@ void QBtSession::readAlerts() {
|
||||||
if (savePathsToRemove.contains(hash)) {
|
if (savePathsToRemove.contains(hash)) {
|
||||||
const QString dirpath = savePathsToRemove.take(hash);
|
const QString dirpath = savePathsToRemove.take(hash);
|
||||||
qDebug() << "Removing save path: " << dirpath << "...";
|
qDebug() << "Removing save path: " << dirpath << "...";
|
||||||
bool ok = smartRemoveEmptyFolderTree(dirpath);
|
bool ok = fsutils::smartRemoveEmptyFolderTree(dirpath);
|
||||||
Q_UNUSED(ok);
|
Q_UNUSED(ok);
|
||||||
qDebug() << "Folder was removed: " << ok;
|
qDebug() << "Folder was removed: " << ok;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue