@ -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 ( defaultSave Path ) ) return save_ path;
if ( ! path . startsWith ( defaultPath ) ) return path ;
QString new_save_path = save_ path;
QString new_save_path = path ;
new_save_path . replac e ( defaultSave Path , " " ) ;
new_save_path . remov e ( 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 = defaultSave Path ;
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 ( f ile_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 : : cl eanPath( QDesktopServicesDataLocation ( )
const QString location = fsutils : : exp and PathAbs ( 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 : : cl eanPath( QDesktopServicesDataLocation ( )
const QString location = fsutils : : exp and PathAbs ( 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 : : cl eanPath( QDesktopServicesCacheLocation ( ) ) ;
QString location = fsutils : : exp and PathAbs ( QDesktopServicesCacheLocation ( ) ) ;
QDir locationDir ( location ) ;
QDir locationDir ( location ) ;
if ( ! locationDir . exists ( ) )
if ( ! locationDir . exists ( ) )
locationDir . mkpath ( locationDir . absolutePath ( ) ) ;
locationDir . mkpath ( locationDir . absolutePath ( ) ) ;