2010-03-20 22:09:27 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
* Copyright ( C ) 2006 Christophe Dumez
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
* In addition , as a special exception , the copyright holders give permission to
* link this program with the OpenSSL project ' s " OpenSSL " library ( or with
* modified versions of it that use the same license as the " OpenSSL " library ) ,
* and distribute the linked executables . You must obey the GNU General Public
* License in all respects for all of the code used other than " OpenSSL " . If you
* modify file ( s ) , you may extend this exception to your version of the file ( s ) ,
* but you are not obligated to do so . If you do not wish to do so , delete this
* exception statement from your version .
*
* Contact : chris @ qbittorrent . org
*/
# include "misc.h"
# include <QUrl>
# include <QDir>
# include <QFileInfo>
# include <QDateTime>
# include <QByteArray>
2011-01-06 15:12:07 +03:00
# include <QDebug>
2011-09-11 21:22:54 +04:00
# include <QProcess>
2011-10-02 18:56:00 +04:00
# include <QSettings>
2010-03-20 22:09:27 +03:00
# ifdef DISABLE_GUI
# include <QCoreApplication>
# else
# include <QApplication>
# include <QDesktopWidget>
# endif
# ifdef Q_WS_WIN
# include <shlobj.h>
2010-06-09 13:48:14 +04:00
# include <windows.h>
2010-12-31 16:36:32 +03:00
# include <PowrProf.h>
2010-06-09 13:48:14 +04:00
const int UNLEN = 256 ;
# else
# include <unistd.h>
# include <sys/types.h>
2010-03-20 22:09:27 +03:00
# endif
# ifdef Q_WS_MAC
# include <CoreServices/CoreServices.h>
2010-08-17 14:50:14 +04:00
# include <Carbon/Carbon.h>
2010-03-20 22:09:27 +03:00
# endif
2011-10-25 21:56:54 +04:00
# ifndef Q_WS_WIN
# if defined(Q_WS_MAC) || defined(Q_OS_FREEBSD)
# include <sys/param.h>
# include <sys/mount.h>
# else
# include <sys/vfs.h>
# endif
# else
2010-03-20 22:09:27 +03:00
# include <winbase.h>
# endif
2011-01-15 11:42:32 +03:00
# ifndef DISABLE_GUI
2011-03-07 23:22:35 +03:00
# if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
2010-08-16 21:35:32 +04:00
# include <QDBusInterface>
# include <QDBusMessage>
# endif
2011-01-15 11:42:32 +03:00
# endif // DISABLE_GUI
2010-08-16 21:35:32 +04:00
2011-10-05 00:17:49 +04:00
# ifdef Q_WS_WIN
# include <QDesktopServices>
# endif
2010-11-23 00:55:32 +03:00
using namespace libtorrent ;
2011-03-21 22:01:36 +03:00
const int MAX_FILENAME_LENGTH = 255 ;
2011-01-20 19:45:59 +03:00
static struct { const char * source ; const char * comment ; } units [ ] = {
2011-03-21 22:01:36 +03:00
QT_TRANSLATE_NOOP3 ( " misc " , " B " , " bytes " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " KiB " , " kibibytes (1024 bytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " MiB " , " mebibytes (1024 kibibytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " GiB " , " gibibytes (1024 mibibytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " TiB " , " tebibytes (1024 gibibytes) " )
} ;
2010-12-02 21:21:45 +03:00
2010-03-20 22:09:27 +03:00
QString misc : : QDesktopServicesDataLocation ( ) {
# ifdef Q_WS_WIN
2010-04-28 15:45:13 +04:00
LPWSTR path = new WCHAR [ 256 ] ;
QString result ;
2010-03-20 22:09:27 +03:00
# if defined Q_WS_WINCE
if ( SHGetSpecialFolderPath ( 0 , path , CSIDL_APPDATA , FALSE ) )
# else
2010-10-25 23:34:42 +04:00
if ( SHGetSpecialFolderPath ( 0 , path , CSIDL_LOCAL_APPDATA , FALSE ) )
2010-03-20 22:09:27 +03:00
# endif
2010-10-25 23:34:42 +04:00
result = QString : : fromWCharArray ( path ) ;
2010-03-20 22:09:27 +03:00
if ( ! QCoreApplication : : applicationName ( ) . isEmpty ( ) )
result = result + QLatin1String ( " \\ " ) + qApp - > applicationName ( ) ;
2012-02-20 21:30:53 +04:00
if ( ! result . endsWith ( " \\ " ) )
2010-06-05 22:59:05 +04:00
result + = " \\ " ;
2010-05-30 21:51:40 +04:00
return result ;
2010-03-20 22:09:27 +03:00
# else
# ifdef Q_WS_MAC
FSRef ref ;
OSErr err = FSFindFolder ( kUserDomain , kApplicationSupportFolderType , false , & ref ) ;
if ( err )
return QString ( ) ;
QString path ;
QByteArray ba ( 2048 , 0 ) ;
if ( FSRefMakePath ( & ref , reinterpret_cast < UInt8 * > ( ba . data ( ) ) , ba . size ( ) ) = = noErr )
path = QString : : fromUtf8 ( ba ) . normalized ( QString : : NormalizationForm_C ) ;
path + = QLatin1Char ( ' / ' ) + qApp - > applicationName ( ) ;
return path ;
# else
QString xdgDataHome = QLatin1String ( qgetenv ( " XDG_DATA_HOME " ) ) ;
if ( xdgDataHome . isEmpty ( ) )
xdgDataHome = QDir : : homePath ( ) + QLatin1String ( " /.local/share " ) ;
xdgDataHome + = QLatin1String ( " /data/ " )
2010-10-25 23:34:42 +04:00
+ qApp - > applicationName ( ) ;
2010-03-20 22:09:27 +03:00
return xdgDataHome ;
# endif
# endif
}
QString misc : : QDesktopServicesCacheLocation ( ) {
# ifdef Q_WS_WIN
return QDesktopServicesDataLocation ( ) + QLatin1String ( " \\ cache " ) ;
# else
# ifdef Q_WS_MAC
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
FSRef ref ;
OSErr err = FSFindFolder ( kUserDomain , kCachedDataFolderType , false , & ref ) ;
if ( err )
return QString ( ) ;
2010-06-09 19:13:52 +04:00
QString path ;
QByteArray ba ( 2048 , 0 ) ;
if ( FSRefMakePath ( & ref , reinterpret_cast < UInt8 * > ( ba . data ( ) ) , ba . size ( ) ) = = noErr )
2010-08-16 21:42:21 +04:00
path = QString : : fromUtf8 ( ba ) . normalized ( QString : : NormalizationForm_C ) ;
2010-03-20 22:09:27 +03:00
path + = QLatin1Char ( ' / ' ) + qApp - > applicationName ( ) ;
return path ;
# else
QString xdgCacheHome = QLatin1String ( qgetenv ( " XDG_CACHE_HOME " ) ) ;
if ( xdgCacheHome . isEmpty ( ) )
xdgCacheHome = QDir : : homePath ( ) + QLatin1String ( " /.cache " ) ;
xdgCacheHome + = QLatin1Char ( ' / ' ) + QCoreApplication : : applicationName ( ) ;
return xdgCacheHome ;
# endif
# endif
}
2011-10-02 18:56:00 +04:00
QString misc : : QDesktopServicesDownloadLocation ( ) {
# ifdef Q_WS_WIN
// TODO: Use IKnownFolderManager to get path of FOLDERID_Downloads
// instead of hardcoding "Downloads"
2011-10-02 19:43:55 +04:00
// Unfortunately, this would break compatibility with WinXP
2011-10-08 14:34:57 +04:00
return QDir ( QDesktopServices : : storageLocation ( QDesktopServices : : DocumentsLocation ) ) . absoluteFilePath ( tr ( " Downloads " ) ) ;
2011-10-02 18:56:00 +04:00
# endif
# ifdef Q_WS_X11
QString save_path ;
// Default save path on Linux
QString config_path = QString : : fromLocal8Bit ( qgetenv ( " XDG_CONFIG_HOME " ) . constData ( ) ) ;
if ( config_path . isEmpty ( ) )
config_path = QDir : : home ( ) . absoluteFilePath ( " .config " ) ;
QString user_dirs_file = config_path + " /user-dirs.dirs " ;
if ( QFile : : exists ( user_dirs_file ) ) {
QSettings settings ( user_dirs_file , QSettings : : IniFormat ) ;
QString xdg_download_dir = settings . value ( " XDG_DOWNLOAD_DIR " ) . toString ( ) ;
if ( ! xdg_download_dir . isEmpty ( ) ) {
// Resolve $HOME environment variables
xdg_download_dir . replace ( " $HOME " , QDir : : homePath ( ) ) ;
save_path = xdg_download_dir ;
qDebug ( ) < < Q_FUNC_INFO < < " SUCCESS: Using XDG path for downloads: " < < save_path ;
}
}
// Fallback
if ( ! save_path . isEmpty ( ) & & ! QFile : : exists ( save_path ) ) {
QDir ( ) . mkpath ( save_path ) ;
}
if ( save_path . isEmpty ( ) | | ! QFile : : exists ( save_path ) ) {
save_path = QDir : : home ( ) . absoluteFilePath ( tr ( " Downloads " ) ) ;
qDebug ( ) < < Q_FUNC_INFO < < " using " < < save_path < < " as fallback since the XDG detection did not work " ;
}
return save_path ;
# endif
# ifdef Q_WS_MAC
2011-11-16 23:47:00 +04:00
// TODO: How to support this on Mac OS X?
2011-10-02 18:56:00 +04:00
# endif
// Fallback
return QDir : : home ( ) . absoluteFilePath ( tr ( " Downloads " ) ) ;
}
2010-03-20 22:09:27 +03:00
long long misc : : freeDiskSpaceOnPath ( QString path ) {
2012-02-20 21:30:53 +04:00
if ( path . isEmpty ( ) ) return - 1 ;
2011-09-25 16:04:51 +04:00
path . replace ( " \\ " , " / " ) ;
2010-03-20 22:09:27 +03:00
QDir dir_path ( path ) ;
2012-02-20 21:30:53 +04:00
if ( ! dir_path . exists ( ) ) {
2010-06-22 15:53:49 +04:00
QStringList parts = path . split ( " / " ) ;
while ( parts . size ( ) > 1 & & ! QDir ( parts . join ( " / " ) ) . exists ( ) ) {
parts . removeLast ( ) ;
}
dir_path = QDir ( parts . join ( " / " ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! dir_path . exists ( ) ) return - 1 ;
2010-03-20 22:09:27 +03:00
}
Q_ASSERT ( dir_path . exists ( ) ) ;
2011-10-25 21:56:54 +04:00
# ifndef Q_WS_WIN
unsigned long long available ;
struct statfs stats ;
const QString statfs_path = dir_path . path ( ) + " /. " ;
const int ret = statfs ( qPrintable ( statfs_path ) , & stats ) ;
2012-02-20 21:30:53 +04:00
if ( ret = = 0 ) {
2011-10-25 21:56:54 +04:00
available = ( ( unsigned long long ) stats . f_bavail ) *
( ( unsigned long long ) stats . f_bsize ) ;
return available ;
} else {
return - 1 ;
}
# else
typedef BOOL ( WINAPI * GetDiskFreeSpaceEx_t ) ( LPCTSTR ,
PULARGE_INTEGER ,
PULARGE_INTEGER ,
PULARGE_INTEGER ) ;
GetDiskFreeSpaceEx_t
pGetDiskFreeSpaceEx = ( GetDiskFreeSpaceEx_t ) : : GetProcAddress
(
: : GetModuleHandle ( TEXT ( " kernel32.dll " ) ) ,
" GetDiskFreeSpaceExW "
) ;
if ( pGetDiskFreeSpaceEx )
{
ULARGE_INTEGER bytesFree , bytesTotal ;
unsigned long long * ret ;
if ( pGetDiskFreeSpaceEx ( ( LPCTSTR ) ( QDir : : toNativeSeparators ( dir_path . path ( ) ) ) . utf16 ( ) , & bytesFree , & bytesTotal , NULL ) ) {
ret = ( unsigned long long * ) & bytesFree ;
return * ret ;
} else {
return - 1 ;
}
} else {
return - 1 ;
}
# endif
2010-03-20 22:09:27 +03:00
}
2011-01-15 11:42:32 +03:00
# ifndef DISABLE_GUI
2010-12-31 16:36:32 +03:00
void misc : : shutdownComputer ( bool sleep ) {
2011-03-07 23:22:35 +03:00
# if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
2011-04-05 20:22:16 +04:00
// Use dbus to power off / suspend the system
2012-02-20 21:30:53 +04:00
if ( sleep ) {
2011-04-05 20:41:54 +04:00
// Recent systems use UPower
2011-04-05 20:22:16 +04:00
QDBusInterface upowerIface ( " org.freedesktop.UPower " , " /org/freedesktop/UPower " ,
2011-04-05 20:41:54 +04:00
" org.freedesktop.UPower " , QDBusConnection : : systemBus ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( upowerIface . isValid ( ) ) {
2011-04-05 20:41:54 +04:00
upowerIface . call ( " Suspend " ) ;
return ;
}
// HAL (older systems)
QDBusInterface halIface ( " org.freedesktop.Hal " , " /org/freedesktop/Hal/devices/computer " ,
" org.freedesktop.Hal.Device.SystemPowerManagement " ,
QDBusConnection : : systemBus ( ) ) ;
halIface . call ( " Suspend " , 5 ) ;
2011-04-05 20:22:16 +04:00
} else {
2011-04-05 20:41:54 +04:00
// Recent systems use ConsoleKit
2011-04-05 20:22:16 +04:00
QDBusInterface consolekitIface ( " org.freedesktop.ConsoleKit " , " /org/freedesktop/ConsoleKit/Manager " ,
" org.freedesktop.ConsoleKit.Manager " , QDBusConnection : : systemBus ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( consolekitIface . isValid ( ) ) {
2011-04-05 20:41:54 +04:00
consolekitIface . call ( " Stop " ) ;
return ;
}
// HAL (older systems)
QDBusInterface halIface ( " org.freedesktop.Hal " , " /org/freedesktop/Hal/devices/computer " ,
" org.freedesktop.Hal.Device.SystemPowerManagement " ,
QDBusConnection : : systemBus ( ) ) ;
halIface . call ( " Shutdown " ) ;
2011-04-05 20:22:16 +04:00
}
2010-12-31 16:36:32 +03:00
# endif
# ifdef Q_WS_MAC
AEEventID EventToSend ;
2012-02-20 21:30:53 +04:00
if ( sleep )
2010-12-31 16:36:32 +03:00
EventToSend = kAESleep ;
else
EventToSend = kAEShutDown ;
2010-08-16 21:42:21 +04:00
AEAddressDesc targetDesc ;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0 , kSystemProcess } ;
AppleEvent eventReply = { typeNull , NULL } ;
AppleEvent appleEventToSend = { typeNull , NULL } ;
OSStatus error = noErr ;
error = AECreateDesc ( typeProcessSerialNumber , & kPSNOfSystemProcess ,
sizeof ( kPSNOfSystemProcess ) , & targetDesc ) ;
if ( error ! = noErr )
{
2010-08-17 14:50:14 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
error = AECreateAppleEvent ( kCoreEventClass , EventToSend , & targetDesc ,
kAutoGenerateReturnID , kAnyTransactionID , & appleEventToSend ) ;
AEDisposeDesc ( & targetDesc ) ;
if ( error ! = noErr )
{
2010-10-25 23:34:42 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
error = AESend ( & appleEventToSend , & eventReply , kAENoReply ,
kAENormalPriority , kAEDefaultTimeout , NULL , NULL ) ;
AEDisposeDesc ( & appleEventToSend ) ;
if ( error ! = noErr )
{
2010-08-17 14:50:14 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
AEDisposeDesc ( & eventReply ) ;
# endif
2010-08-16 21:57:15 +04:00
# ifdef Q_WS_WIN
2010-08-16 22:34:30 +04:00
HANDLE hToken ; // handle to process token
TOKEN_PRIVILEGES tkp ; // pointer to token structure
2012-02-20 21:30:53 +04:00
if ( ! OpenProcessToken ( GetCurrentProcess ( ) , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY , & hToken ) )
2010-08-16 22:34:30 +04:00
return ;
// Get the LUID for shutdown privilege.
LookupPrivilegeValue ( NULL , SE_SHUTDOWN_NAME ,
& tkp . Privileges [ 0 ] . Luid ) ;
tkp . PrivilegeCount = 1 ; // one privilege to set
tkp . Privileges [ 0 ] . Attributes = SE_PRIVILEGE_ENABLED ;
// Get shutdown privilege for this process.
AdjustTokenPrivileges ( hToken , FALSE , & tkp , 0 ,
( PTOKEN_PRIVILEGES ) NULL , 0 ) ;
// Cannot test the return value of AdjustTokenPrivileges.
if ( GetLastError ( ) ! = ERROR_SUCCESS )
return ;
2011-09-22 21:44:01 +04:00
2012-02-20 21:30:53 +04:00
if ( sleep )
2010-12-31 16:36:32 +03:00
SetSuspendState ( false , false , false ) ;
else
InitiateSystemShutdownA ( 0 , tr ( " qBittorrent will shutdown the computer now because all downloads are complete. " ) . toLocal8Bit ( ) . data ( ) , 10 , true , false ) ;
2010-08-16 22:34:30 +04:00
// Disable shutdown privilege.
tkp . Privileges [ 0 ] . Attributes = 0 ;
AdjustTokenPrivileges ( hToken , FALSE , & tkp , 0 ,
( PTOKEN_PRIVILEGES ) NULL , 0 ) ;
2010-08-16 21:57:15 +04:00
# endif
2010-08-16 21:35:32 +04:00
}
2011-01-15 11:48:20 +03:00
# endif // DISABLE_GUI
2010-08-16 21:35:32 +04:00
2011-03-21 22:01:36 +03:00
QString misc : : fixFileNames ( QString path ) {
//qDebug() << Q_FUNC_INFO << path;
path . replace ( " \\ " , " / " ) ;
QStringList parts = path . split ( " / " , QString : : SkipEmptyParts ) ;
2012-02-20 21:30:53 +04:00
if ( parts . isEmpty ( ) ) return path ;
2011-03-21 22:01:36 +03:00
QString last_part = parts . takeLast ( ) ;
QList < QString > : : iterator it ;
2012-02-20 21:30:53 +04:00
for ( it = parts . begin ( ) ; it ! = parts . end ( ) ; it + + ) {
2011-03-21 22:01:36 +03:00
QByteArray raw_filename = it - > toLocal8Bit ( ) ;
// Make sure the filename is not too long
2012-02-20 21:30:53 +04:00
if ( raw_filename . size ( ) > MAX_FILENAME_LENGTH ) {
2011-03-21 22:01:36 +03:00
qDebug ( ) < < " Folder " < < * it < < " was cut because it was too long " ;
raw_filename . resize ( MAX_FILENAME_LENGTH ) ;
* it = QString : : fromLocal8Bit ( raw_filename . constData ( ) ) ;
qDebug ( ) < < " New folder name is " < < * it ;
Q_ASSERT ( it - > length ( ) = = MAX_FILENAME_LENGTH ) ;
}
}
// Fix the last part (file name)
QByteArray raw_lastPart = last_part . toLocal8Bit ( ) ;
qDebug ( ) < < " Last part length: " < < raw_lastPart . length ( ) ;
2012-02-20 21:30:53 +04:00
if ( raw_lastPart . length ( ) > MAX_FILENAME_LENGTH ) {
2011-03-21 22:01:36 +03:00
qDebug ( ) < < " Filename " < < last_part < < " was cut because it was too long " ;
// Shorten the name, keep the file extension
int point_index = raw_lastPart . lastIndexOf ( " . " ) ;
QByteArray extension = " " ;
2012-02-20 21:30:53 +04:00
if ( point_index > = 0 ) {
2011-03-21 22:01:36 +03:00
extension = raw_lastPart . mid ( point_index ) ;
raw_lastPart = raw_lastPart . left ( point_index ) ;
}
raw_lastPart = raw_lastPart . left ( MAX_FILENAME_LENGTH - extension . length ( ) ) + extension ;
Q_ASSERT ( raw_lastPart . length ( ) = = MAX_FILENAME_LENGTH ) ;
last_part = QString : : fromLocal8Bit ( raw_lastPart . constData ( ) ) ;
qDebug ( ) < < " New file name is " < < last_part ;
}
parts < < last_part ;
return parts . join ( " / " ) ;
}
2010-04-06 13:17:33 +04:00
QString misc : : truncateRootFolder ( boost : : intrusive_ptr < torrent_info > t ) {
2012-02-20 21:30:53 +04:00
if ( t - > num_files ( ) = = 1 ) {
2010-06-19 18:39:23 +04:00
// Single file torrent
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
QString path = QString : : fromUtf8 ( t - > file_at ( 0 ) . path . c_str ( ) ) ;
# else
2010-06-19 18:39:23 +04:00
QString path = QString : : fromUtf8 ( t - > file_at ( 0 ) . path . string ( ) . c_str ( ) ) ;
2011-04-17 14:29:44 +04:00
# endif
2011-03-21 22:01:36 +03:00
// Remove possible subfolders
path = fixFileNames ( fileName ( path ) ) ;
t - > rename_file ( 0 , path . toUtf8 ( ) . data ( ) ) ;
return QString ( ) ;
2010-06-19 18:39:23 +04:00
}
2010-04-06 13:17:33 +04:00
QString root_folder ;
2012-02-20 21:30:53 +04:00
for ( int i = 0 ; i < t - > num_files ( ) ; + + i ) {
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
QString path = QString : : fromUtf8 ( t - > file_at ( i ) . path . c_str ( ) ) ;
# else
QString path = QString : : fromUtf8 ( t - > file_at ( i ) . path . string ( ) . c_str ( ) ) ;
# endif
2010-04-06 13:17:33 +04:00
QStringList path_parts = path . split ( " / " , QString : : SkipEmptyParts ) ;
2012-02-20 21:30:53 +04:00
if ( path_parts . size ( ) > 1 ) {
2010-04-06 13:17:33 +04:00
root_folder = path_parts . takeFirst ( ) ;
}
2011-03-21 22:01:36 +03:00
path = fixFileNames ( path_parts . join ( " / " ) ) ;
t - > rename_file ( i , path . toUtf8 ( ) . data ( ) ) ;
2010-04-06 13:17:33 +04:00
}
return root_folder ;
}
QString misc : : truncateRootFolder ( libtorrent : : torrent_handle h ) {
2010-06-19 18:39:23 +04:00
torrent_info t = h . get_torrent_info ( ) ;
2012-02-20 21:30:53 +04:00
if ( t . num_files ( ) = = 1 ) {
2010-06-19 18:39:23 +04:00
// Single file torrent
// Remove possible subfolders
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
QString path = QString : : fromUtf8 ( t . file_at ( 0 ) . path . c_str ( ) ) ;
# else
2010-06-19 18:39:23 +04:00
QString path = QString : : fromUtf8 ( t . file_at ( 0 ) . path . string ( ) . c_str ( ) ) ;
2011-04-17 14:29:44 +04:00
# endif
2011-03-21 22:01:36 +03:00
path = fixFileNames ( fileName ( path ) ) ;
t . rename_file ( 0 , path . toUtf8 ( ) . data ( ) ) ;
return QString ( ) ;
2010-06-19 18:39:23 +04:00
}
2010-04-06 13:17:33 +04:00
QString root_folder ;
2012-02-20 21:30:53 +04:00
for ( int i = 0 ; i < t . num_files ( ) ; + + i ) {
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
QString path = QString : : fromUtf8 ( t . file_at ( i ) . path . c_str ( ) ) ;
# else
QString path = QString : : fromUtf8 ( t . file_at ( i ) . path . string ( ) . c_str ( ) ) ;
# endif
2010-04-06 13:17:33 +04:00
QStringList path_parts = path . split ( " / " , QString : : SkipEmptyParts ) ;
2012-02-20 21:30:53 +04:00
if ( path_parts . size ( ) > 1 ) {
2010-04-06 13:17:33 +04:00
root_folder = path_parts . takeFirst ( ) ;
}
2011-03-21 22:01:36 +03:00
path = fixFileNames ( path_parts . join ( " / " ) ) ;
h . rename_file ( i , path . toUtf8 ( ) . data ( ) ) ;
2010-04-06 13:17:33 +04:00
}
return root_folder ;
}
2010-12-02 21:21:45 +03:00
bool misc : : sameFiles ( const QString & path1 , const QString & path2 ) {
QFile f1 ( path1 ) , f2 ( path2 ) ;
2012-02-20 21:30:53 +04:00
if ( ! f1 . exists ( ) | | ! f2 . exists ( ) ) return false ;
if ( f1 . size ( ) ! = f2 . size ( ) ) return false ;
if ( ! f1 . open ( QIODevice : : ReadOnly ) ) return false ;
if ( ! f2 . open ( QIODevice : : ReadOnly ) ) {
2010-03-20 22:09:27 +03:00
f1 . close ( ) ;
return false ;
}
2010-12-02 21:21:45 +03:00
bool same = true ;
while ( ! f1 . atEnd ( ) & & ! f2 . atEnd ( ) ) {
2012-02-20 21:30:53 +04:00
if ( f1 . read ( 5 ) ! = f2 . read ( 5 ) ) {
2010-12-02 21:21:45 +03:00
same = false ;
break ;
}
2010-03-20 22:09:27 +03:00
}
2010-12-02 21:21:45 +03:00
f1 . close ( ) ; f2 . close ( ) ;
return same ;
2010-03-20 22:09:27 +03:00
}
2011-02-12 12:50:23 +03:00
QString misc : : updateLabelInSavePath ( QString defaultSavePath , QString save_path , const QString & old_label , const QString & new_label ) {
2012-02-20 21:30:53 +04:00
if ( old_label = = new_label ) return save_path ;
2011-02-12 12:50:23 +03:00
# if defined(Q_WS_WIN) || defined(Q_OS_OS2)
defaultSavePath . replace ( " \\ " , " / " ) ;
save_path . replace ( " \\ " , " / " ) ;
# endif
2010-04-22 20:19:49 +04:00
qDebug ( " UpdateLabelInSavePath(%s, %s, %s) " , qPrintable ( save_path ) , qPrintable ( old_label ) , qPrintable ( new_label ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! save_path . startsWith ( defaultSavePath ) ) return save_path ;
2010-12-02 21:21:45 +03:00
QString new_save_path = save_path ;
new_save_path . replace ( defaultSavePath , " " ) ;
2011-02-12 12:50:23 +03:00
QStringList path_parts = new_save_path . split ( " / " , QString : : SkipEmptyParts ) ;
2012-02-20 21:30:53 +04:00
if ( path_parts . empty ( ) ) {
if ( ! new_label . isEmpty ( ) )
2010-04-10 18:17:26 +04:00
path_parts < < new_label ;
} else {
2012-02-20 21:30:53 +04:00
if ( old_label . isEmpty ( ) | | path_parts . first ( ) ! = old_label ) {
if ( path_parts . first ( ) ! = new_label )
2010-04-10 18:50:11 +04:00
path_parts . prepend ( new_label ) ;
2010-04-10 18:17:26 +04:00
} else {
2012-02-20 21:30:53 +04:00
if ( new_label . isEmpty ( ) ) {
2010-04-10 18:17:26 +04:00
path_parts . removeAt ( 0 ) ;
2010-04-10 18:50:11 +04:00
} else {
2012-02-20 21:30:53 +04:00
if ( path_parts . first ( ) ! = new_label )
2010-04-10 18:50:11 +04:00
path_parts . replace ( 0 , new_label ) ;
}
2010-04-10 18:17:26 +04:00
}
}
new_save_path = defaultSavePath ;
2012-02-20 21:30:53 +04:00
if ( ! new_save_path . endsWith ( QDir : : separator ( ) ) ) new_save_path + = QDir : : separator ( ) ;
2010-04-10 18:17:26 +04:00
new_save_path + = path_parts . join ( QDir : : separator ( ) ) ;
2010-04-22 20:19:49 +04:00
qDebug ( " New save path is %s " , qPrintable ( new_save_path ) ) ;
2010-04-10 18:17:26 +04:00
return new_save_path ;
}
2010-03-20 22:09:27 +03:00
QString misc : : toValidFileSystemName ( QString filename ) {
qDebug ( " toValidFSName: %s " , qPrintable ( filename ) ) ;
2012-02-16 22:32:13 +04:00
const QRegExp regex ( " [ \\ \\ /:? \" *<>|] " ) ;
filename . replace ( regex , " " ) ;
2010-03-20 22:09:27 +03:00
qDebug ( " toValidFSName, result: %s " , qPrintable ( filename ) ) ;
2012-02-16 22:32:13 +04:00
return filename . trimmed ( ) ;
2010-03-20 22:09:27 +03:00
}
2012-02-15 23:57:40 +04:00
bool misc : : isValidFileSystemName ( const QString & filename ) {
2012-02-20 21:30:53 +04:00
if ( filename . isEmpty ( ) ) return false ;
2012-02-15 23:57:40 +04:00
const QRegExp regex ( " [ \\ \\ /:? \" *<>|] " ) ;
return ! filename . contains ( regex ) ;
2010-03-20 22:09:27 +03:00
}
# ifndef DISABLE_GUI
// Get screen center
QPoint misc : : screenCenter ( QWidget * win ) {
int scrn = 0 ;
const QWidget * w = win - > window ( ) ;
2012-02-20 21:30:53 +04:00
if ( w )
2010-03-20 22:09:27 +03:00
scrn = QApplication : : desktop ( ) - > screenNumber ( w ) ;
2012-02-20 21:30:53 +04:00
else if ( QApplication : : desktop ( ) - > isVirtualDesktop ( ) )
2010-03-20 22:09:27 +03:00
scrn = QApplication : : desktop ( ) - > screenNumber ( QCursor : : pos ( ) ) ;
else
scrn = QApplication : : desktop ( ) - > screenNumber ( win ) ;
QRect desk ( QApplication : : desktop ( ) - > availableGeometry ( scrn ) ) ;
return QPoint ( ( desk . width ( ) - win - > frameGeometry ( ) . width ( ) ) / 2 , ( desk . height ( ) - win - > frameGeometry ( ) . height ( ) ) / 2 ) ;
}
# endif
2011-09-11 21:22:54 +04:00
/**
* Detects the version of python by calling
* " python --version " and parsing the output .
*/
int misc : : pythonVersion ( ) {
static int version = - 1 ;
if ( version < 0 ) {
QProcess python_proc ;
python_proc . start ( " python " , QStringList ( ) < < " --version " , QIODevice : : ReadOnly ) ;
if ( ! python_proc . waitForFinished ( ) ) return - 1 ;
if ( python_proc . exitCode ( ) < 0 ) return - 1 ;
QByteArray output = python_proc . readAllStandardOutput ( ) ;
if ( output . isEmpty ( ) )
output = python_proc . readAllStandardError ( ) ;
const QByteArray version_str = output . split ( ' ' ) . last ( ) ;
qDebug ( ) < < " Python version is: " < < version_str . trimmed ( ) ;
if ( version_str . startsWith ( " 3. " ) )
version = 3 ;
else
version = 2 ;
}
return version ;
}
2010-03-20 22:09:27 +03:00
QString misc : : searchEngineLocation ( ) {
2011-09-11 21:22:54 +04:00
QString folder = " nova " ;
if ( pythonVersion ( ) > = 3 )
folder = " nova3 " ;
2010-07-23 18:05:53 +04:00
const QString location = QDir : : cleanPath ( QDesktopServicesDataLocation ( )
2011-09-11 21:22:54 +04:00
+ QDir : : separator ( ) + folder ) ;
2010-03-20 22:09:27 +03:00
QDir locationDir ( location ) ;
2012-02-20 21:30:53 +04:00
if ( ! locationDir . exists ( ) )
2010-03-20 22:09:27 +03:00
locationDir . mkpath ( locationDir . absolutePath ( ) ) ;
return location ;
}
QString misc : : BTBackupLocation ( ) {
2010-07-23 18:05:53 +04:00
const QString location = QDir : : cleanPath ( QDesktopServicesDataLocation ( )
2010-08-16 21:42:21 +04:00
+ QDir : : separator ( ) + " BT_backup " ) ;
2010-03-20 22:09:27 +03:00
QDir locationDir ( location ) ;
2012-02-20 21:30:53 +04:00
if ( ! locationDir . exists ( ) )
2010-03-20 22:09:27 +03:00
locationDir . mkpath ( locationDir . absolutePath ( ) ) ;
return location ;
}
QString misc : : cacheLocation ( ) {
2010-05-30 21:51:40 +04:00
QString location = QDir : : cleanPath ( QDesktopServicesCacheLocation ( ) ) ;
2010-03-20 22:09:27 +03:00
QDir locationDir ( location ) ;
2012-02-20 21:30:53 +04:00
if ( ! locationDir . exists ( ) )
2010-03-20 22:09:27 +03:00
locationDir . mkpath ( locationDir . absolutePath ( ) ) ;
return location ;
}
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
// use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes
2011-01-25 20:04:55 +03:00
QString misc : : friendlyUnit ( qreal val ) {
2012-02-20 21:30:53 +04:00
if ( val < 0 )
2010-03-20 22:09:27 +03:00
return tr ( " Unknown " , " Unknown (size) " ) ;
2010-12-02 21:21:45 +03:00
int i = 0 ;
2010-03-20 22:09:27 +03:00
while ( val > = 1024. & & i + + < 6 )
val / = 1024. ;
2012-02-20 21:30:53 +04:00
if ( i = = 0 )
2011-01-20 19:45:59 +03:00
return QString : : number ( ( long ) val ) + " " + tr ( units [ 0 ] . source , units [ 0 ] . comment ) ;
return QString : : number ( val , ' f ' , 1 ) + " " + tr ( units [ i ] . source , units [ i ] . comment ) ;
2010-03-20 22:09:27 +03:00
}
bool misc : : isPreviewable ( QString extension ) {
2012-02-20 21:30:53 +04:00
if ( extension . isEmpty ( ) ) return false ;
2010-03-20 22:09:27 +03:00
extension = extension . toUpper ( ) ;
2012-02-20 21:30:53 +04:00
if ( extension = = " AVI " ) return true ;
if ( extension = = " MP3 " ) return true ;
if ( extension = = " OGG " ) return true ;
if ( extension = = " OGV " ) return true ;
if ( extension = = " OGM " ) return true ;
if ( extension = = " WMV " ) return true ;
if ( extension = = " WMA " ) return true ;
if ( extension = = " MPEG " ) return true ;
if ( extension = = " MPG " ) return true ;
if ( extension = = " ASF " ) return true ;
if ( extension = = " QT " ) return true ;
if ( extension = = " RM " ) return true ;
if ( extension = = " RMVB " ) return true ;
if ( extension = = " RMV " ) return true ;
if ( extension = = " SWF " ) return true ;
if ( extension = = " FLV " ) return true ;
if ( extension = = " WAV " ) return true ;
if ( extension = = " MOV " ) return true ;
if ( extension = = " VOB " ) return true ;
if ( extension = = " MID " ) return true ;
if ( extension = = " AC3 " ) return true ;
if ( extension = = " MP4 " ) return true ;
if ( extension = = " MP2 " ) return true ;
if ( extension = = " AVI " ) return true ;
if ( extension = = " FLAC " ) return true ;
if ( extension = = " AU " ) return true ;
if ( extension = = " MPE " ) return true ;
if ( extension = = " MOV " ) return true ;
if ( extension = = " MKV " ) return true ;
if ( extension = = " AIF " ) return true ;
if ( extension = = " AIFF " ) return true ;
if ( extension = = " AIFC " ) return true ;
if ( extension = = " RA " ) return true ;
if ( extension = = " RAM " ) return true ;
if ( extension = = " M4P " ) return true ;
if ( extension = = " M4A " ) return true ;
if ( extension = = " 3GP " ) return true ;
if ( extension = = " AAC " ) return true ;
if ( extension = = " SWA " ) return true ;
if ( extension = = " MPC " ) return true ;
if ( extension = = " MPP " ) return true ;
if ( extension = = " M3U " ) return true ;
2010-03-20 22:09:27 +03:00
return false ;
}
2010-07-23 02:19:42 +04:00
QString misc : : bcLinkToMagnet ( QString bc_link ) {
QByteArray raw_bc = bc_link . toUtf8 ( ) ;
raw_bc = raw_bc . mid ( 8 ) ; // skip bc://bt/
raw_bc = QByteArray : : fromBase64 ( raw_bc ) ; // Decode base64
// Format is now AA/url_encoded_filename/size_bytes/info_hash/ZZ
QStringList parts = QString ( raw_bc ) . split ( " / " ) ;
2012-02-20 21:30:53 +04:00
if ( parts . size ( ) ! = 5 ) return QString : : null ;
2010-07-23 02:19:42 +04:00
QString filename = parts . at ( 1 ) ;
QString hash = parts . at ( 3 ) ;
QString magnet = " magnet:?xt=urn:btih: " + hash ;
magnet + = " &dn= " + filename ;
return magnet ;
}
2010-03-20 22:09:27 +03:00
QString misc : : magnetUriToName ( QString magnet_uri ) {
QString name = " " ;
QRegExp regHex ( " dn=([^&]+) " ) ;
const int pos = regHex . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regHex . cap ( 1 ) ;
2010-03-20 22:09:27 +03:00
// URL decode
name = QUrl : : fromPercentEncoding ( found . toLocal8Bit ( ) ) . replace ( " + " , " " ) ;
}
return name ;
}
QString misc : : magnetUriToHash ( QString magnet_uri ) {
QString hash = " " ;
QRegExp regHex ( " urn:btih:([0-9A-Za-z]+) " ) ;
// Hex
int pos = regHex . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regHex . cap ( 1 ) ;
2011-06-05 20:08:30 +04:00
qDebug ( ) < < Q_FUNC_INFO < < " regex found: " < < found ;
2012-02-20 21:30:53 +04:00
if ( found . length ( ) = = 40 ) {
2011-06-05 20:08:30 +04:00
const sha1_hash sha1 ( QByteArray : : fromHex ( found . toAscii ( ) ) . constData ( ) ) ;
2010-03-20 22:09:27 +03:00
qDebug ( " magnetUriToHash (Hex): hash: %s " , qPrintable ( misc : : toQString ( sha1 ) ) ) ;
return misc : : toQString ( sha1 ) ;
}
}
// Base 32
QRegExp regBase32 ( " urn:btih:([A-Za-z2-7=]+) " ) ;
pos = regBase32 . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regBase32 . cap ( 1 ) ;
2012-02-20 21:30:53 +04:00
if ( found . length ( ) > 20 & & ( found . length ( ) * 5 ) % 40 = = 0 ) {
2010-03-20 22:09:27 +03:00
const sha1_hash sha1 ( base32decode ( regBase32 . cap ( 1 ) . toStdString ( ) ) ) ;
hash = misc : : toQString ( sha1 ) ;
}
}
qDebug ( " magnetUriToHash (base32): hash: %s " , qPrintable ( hash ) ) ;
return hash ;
}
// Replace ~ in path
QString misc : : expandPath ( QString path ) {
path = path . trimmed ( ) ;
2012-02-20 21:30:53 +04:00
if ( path . isEmpty ( ) ) return path ;
if ( path . length ( ) = = 1 ) {
if ( path [ 0 ] = = ' ~ ' ) return QDir : : homePath ( ) ;
2010-03-20 22:09:27 +03:00
}
2012-02-20 21:30:53 +04:00
if ( path [ 0 ] = = ' ~ ' & & path [ 1 ] = = QDir : : separator ( ) ) {
2011-09-25 16:04:51 +04:00
path . replace ( 0 , 1 , QDir : : homePath ( ) ) ;
2010-03-20 22:09:27 +03:00
} else {
2012-02-20 21:30:53 +04:00
if ( QDir : : isAbsolutePath ( path ) ) {
2010-03-20 22:09:27 +03:00
path = QDir ( path ) . absolutePath ( ) ;
}
}
return QDir : : cleanPath ( path ) ;
}
// Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m".
QString misc : : userFriendlyDuration ( qlonglong seconds ) {
2012-02-20 21:30:53 +04:00
if ( seconds < 0 | | seconds > = MAX_ETA ) {
2010-03-20 22:09:27 +03:00
return QString : : fromUtf8 ( " ∞ " ) ;
}
2012-02-20 21:30:53 +04:00
if ( seconds = = 0 ) {
2010-05-21 15:25:30 +04:00
return " 0 " ;
}
2012-02-20 21:30:53 +04:00
if ( seconds < 60 ) {
2010-03-20 22:09:27 +03:00
return tr ( " < 1m " , " < 1 minute " ) ;
}
int minutes = seconds / 60 ;
2012-02-20 21:30:53 +04:00
if ( minutes < 60 ) {
2010-03-20 22:09:27 +03:00
return tr ( " %1m " , " e.g: 10minutes " ) . arg ( QString : : number ( minutes ) ) ;
}
int hours = minutes / 60 ;
minutes = minutes - hours * 60 ;
2012-02-20 21:30:53 +04:00
if ( hours < 24 ) {
2010-06-21 18:10:20 +04:00
return tr ( " %1h %2m " , " e.g: 3hours 5minutes " ) . arg ( QString : : number ( hours ) ) . arg ( QString : : number ( minutes ) ) ;
2010-03-20 22:09:27 +03:00
}
int days = hours / 24 ;
hours = hours - days * 24 ;
2012-02-20 21:30:53 +04:00
if ( days < 100 ) {
2010-06-21 18:10:20 +04:00
return tr ( " %1d %2h " , " e.g: 2days 10hours " ) . arg ( QString : : number ( days ) ) . arg ( QString : : number ( hours ) ) ;
2010-03-20 22:09:27 +03:00
}
return QString : : fromUtf8 ( " ∞ " ) ;
}
2010-06-05 22:59:05 +04:00
2010-06-09 13:48:14 +04:00
QString misc : : getUserIDString ( ) {
QString uid = " 0 " ;
# ifdef Q_WS_WIN
char buffer [ UNLEN + 1 ] = { 0 } ;
DWORD buffer_len = UNLEN + 1 ;
if ( ! GetUserNameA ( buffer , & buffer_len ) )
uid = QString ( buffer ) ;
# else
uid = QString : : number ( getuid ( ) ) ;
# endif
return uid ;
}
2010-06-05 22:59:05 +04:00
QStringList misc : : toStringList ( const QList < bool > & l ) {
QStringList ret ;
2012-02-20 21:30:53 +04:00
foreach ( const bool & b , l ) {
2010-12-02 21:21:45 +03:00
ret < < ( b ? " 1 " : " 0 " ) ;
2010-06-05 22:59:05 +04:00
}
return ret ;
}
QList < int > misc : : intListfromStringList ( const QStringList & l ) {
QList < int > ret ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & s , l ) {
2010-06-05 22:59:05 +04:00
ret < < s . toInt ( ) ;
}
return ret ;
}
QList < bool > misc : : boolListfromStringList ( const QStringList & l ) {
QList < bool > ret ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & s , l ) {
2010-12-02 21:21:45 +03:00
ret < < ( s = = " 1 " ) ;
2010-08-16 21:42:21 +04:00
}
return ret ;
2010-06-05 22:59:05 +04:00
}
2010-10-25 23:34:42 +04:00
quint64 misc : : computePathSize ( QString path )
{
// Check if it is a file
QFileInfo fi ( path ) ;
2012-02-20 21:30:53 +04:00
if ( ! fi . exists ( ) ) return 0 ;
if ( fi . isFile ( ) ) return fi . size ( ) ;
2010-10-25 23:34:42 +04:00
// Compute folder size
quint64 size = 0 ;
2012-02-20 21:30:53 +04:00
foreach ( const QFileInfo & subfi , QDir ( path ) . entryInfoList ( QDir : : Dirs | QDir : : Files ) ) {
if ( subfi . fileName ( ) . startsWith ( " . " ) ) continue ;
if ( subfi . isDir ( ) )
2010-10-25 23:34:42 +04:00
size + = misc : : computePathSize ( subfi . absoluteFilePath ( ) ) ;
else
size + = subfi . size ( ) ;
}
return size ;
}
2010-11-09 22:10:42 +03:00
bool misc : : isValidTorrentFile ( const QString & torrent_path ) {
try {
boost : : intrusive_ptr < libtorrent : : torrent_info > t = new torrent_info ( torrent_path . toUtf8 ( ) . constData ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! t - > is_valid ( ) | | t - > num_files ( ) = = 0 )
2010-11-09 22:10:42 +03:00
throw std : : exception ( ) ;
} catch ( std : : exception & ) {
return false ;
}
return true ;
}
2010-12-29 23:29:55 +03:00
2012-02-16 23:32:18 +04:00
/**
* Returns a path constructed from all the elements of file_path except the last .
* A typical use is to obtain the parent path for a path supplied by the user .
*/
2010-12-30 22:38:53 +03:00
QString misc : : branchPath ( QString file_path , bool uses_slashes )
2010-12-29 23:29:55 +03:00
{
2012-02-20 21:30:53 +04:00
if ( ! uses_slashes )
2010-12-30 22:38:53 +03:00
file_path . replace ( " \\ " , " / " ) ;
2011-01-06 15:12:07 +03:00
Q_ASSERT ( ! file_path . contains ( " \\ " ) ) ;
2012-02-20 21:30:53 +04:00
if ( file_path . endsWith ( " / " ) )
2011-01-06 15:12:07 +03:00
file_path . chop ( 1 ) ; // Remove trailing slash
qDebug ( ) < < Q_FUNC_INFO < < " before: " < < file_path ;
2012-02-20 21:30:53 +04:00
if ( file_path . contains ( " / " ) )
2011-01-06 15:12:07 +03:00
return file_path . left ( file_path . lastIndexOf ( ' / ' ) ) ;
return " " ;
2010-12-29 23:29:55 +03:00
}
2010-12-30 22:12:03 +03:00
bool misc : : isUrl ( const QString & s )
{
const QString scheme = QUrl ( s ) . scheme ( ) ;
QRegExp is_url ( " http[s]?|ftp " , Qt : : CaseInsensitive ) ;
return is_url . exactMatch ( scheme ) ;
}
QString misc : : fileName ( QString file_path )
{
file_path . replace ( " \\ " , " / " ) ;
const int slash_index = file_path . lastIndexOf ( ' / ' ) ;
2012-02-20 21:30:53 +04:00
if ( slash_index = = - 1 )
2010-12-30 22:12:03 +03:00
return file_path ;
2011-01-23 11:34:57 +03:00
return file_path . mid ( slash_index + 1 ) ;
2010-12-30 22:12:03 +03:00
}
2011-03-10 21:34:28 +03:00
2011-04-04 21:16:34 +04:00
QString misc : : parseHtmlLinks ( const QString & raw_text )
{
QString result = raw_text ;
QRegExp reURL ( " ( \\ s|^) " //start with whitespace or beginning of line
" ( "
" ( " //case 1 -- URL with scheme
" (http(s?)) \\ :// " //start with scheme
" ([a-zA-Z0-9_-]+ \\ .)+ " // domainpart. at least one of these must exist
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]+) " // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy)
" ) "
" | "
" ( " //case 2a -- no scheme, contains common TLD example.com
" ([a-zA-Z0-9_-]+ \\ .)+ " // domainpart. at least one of these must exist
" (?= " // must be followed by TLD
" AERO|aero| " //N.B. assertions are non-capturing
" ARPA|arpa| "
" ASIA|asia| "
" BIZ|biz| "
" CAT|cat| "
" COM|com| "
" COOP|coop| "
" EDU|edu| "
" GOV|gov| "
" INFO|info| "
" INT|int| "
" JOBS|jobs| "
" MIL|mil| "
" MOBI|mobi| "
" MUSEUM|museum| "
" NAME|name| "
" NET|net| "
" ORG|org| "
" PRO|pro| "
" RO|ro| "
" RU|ru| "
" TEL|tel| "
" TRAVEL|travel "
" ) "
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]+) " // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy)
" ) "
" | "
" ( " // case 2b no scheme, no TLD, must have at least 2 aphanum strings plus uncommon TLD string --> del.icio.us
" ([a-zA-Z0-9_-]+ \\ .){2,} " //2 or more domainpart. --> del.icio.
" [a-zA-Z]{2,} " //one ab (2 char or longer) --> us
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]*) " // everything to 1st non-URI char, maybe nothing in case of del.icio.us/path
" ) "
" ) "
) ;
// Capture links
result . replace ( reURL , " \\ 1<a href= \" \\ 2 \" > \\ 2</a> " ) ;
// Capture links without scheme
QRegExp reNoScheme ( " <a \\ s+href= \" (?!http(s?) ) ( [ a - zA - Z0 - 9 \ \ ? % = & / _ \ \ . - : # ] + ) \ \ s * \ " > " ) ;
result . replace ( reNoScheme , " <a href= \" http:// \\ 1 \" > " ) ;
return result ;
}