2007-03-05 16:55:23 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez
2007-03-05 16:55:23 +03:00
*
2007-07-14 18:31:59 +04:00
* 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 .
2007-03-05 16:55:23 +03:00
*
* 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
2007-07-14 18:31:59 +04:00
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
2009-04-05 21:00:55 +04:00
* 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 .
*
2007-07-14 18:31:59 +04:00
* Contact : chris @ qbittorrent . org
2007-03-05 16:55:23 +03:00
*/
2007-03-27 22:49:29 +04:00
# include <QDir>
2009-07-12 13:08:38 +04:00
# include <QDateTime>
2007-03-27 22:49:29 +04:00
# include <QString>
2007-07-30 17:56:31 +04:00
# include <QTimer>
2008-05-18 13:50:35 +04:00
# include <QSettings>
2007-03-27 22:49:29 +04:00
2009-11-12 21:24:51 +03:00
# include "filesystemwatcher.h"
2007-09-29 12:00:14 +04:00
# include "bittorrent.h"
# include "misc.h"
2009-11-20 10:48:44 +03:00
# include "downloadthread.h"
# include "filterparserthread.h"
2009-11-14 22:08:28 +03:00
# include "preferences.h"
2009-11-15 23:03:23 +03:00
# include "geoip.h"
2009-11-20 10:48:44 +03:00
# include "torrentpersistentdata.h"
2009-11-18 20:46:59 +03:00
# include "httpserver.h"
2008-11-02 22:22:10 +03:00
# include <libtorrent/extensions/ut_metadata.hpp>
2009-11-18 13:29:20 +03:00
# ifdef LIBTORRENT_0_15
2009-11-18 20:46:59 +03:00
# include <libtorrent/extensions/lt_trackers.hpp>
2009-11-18 13:29:20 +03:00
# endif
2007-07-23 16:46:36 +04:00
# include <libtorrent/extensions/ut_pex.hpp>
2008-11-02 22:22:10 +03:00
# include <libtorrent/extensions/smart_ban.hpp>
2009-10-24 18:36:17 +04:00
//#include <libtorrent/extensions/metadata_transfer.hpp>
2007-07-23 16:46:36 +04:00
# include <libtorrent/entry.hpp>
# include <libtorrent/bencode.hpp>
# include <libtorrent/identify_client.hpp>
# include <libtorrent/alert_types.hpp>
2007-08-26 20:25:22 +04:00
# include <libtorrent/torrent_info.hpp>
2007-08-02 00:01:06 +04:00
# include <boost/filesystem/exception.hpp>
2007-07-23 16:46:36 +04:00
2007-08-29 19:13:20 +04:00
# define MAX_TRACKER_ERRORS 2
2009-07-12 11:23:11 +04:00
# define MAX_RATIO 100.
2009-11-14 22:08:28 +03:00
enum ProxyType { HTTP = 1 , SOCKS5 = 2 , HTTP_PW = 3 , SOCKS5_PW = 4 } ;
2007-06-29 19:23:15 +04:00
2007-03-05 16:55:23 +03:00
// Main constructor
2009-11-20 11:01:59 +03:00
Bittorrent : : Bittorrent ( ) : preAllocateAll ( false ) , addInPause ( false ) , ratio_limit ( - 1 ) , UPnPEnabled ( false ) , NATPMPEnabled ( false ) , LSDEnabled ( false ) , DHTEnabled ( false ) , queueingEnabled ( false ) , geoipDBLoaded ( false ) {
2009-11-15 13:59:11 +03:00
resolve_countries = false ;
2007-04-07 11:43:57 +04:00
// To avoid some exceptions
fs : : path : : default_name_check ( fs : : no_check ) ;
2009-11-20 11:01:59 +03:00
// Creating Bittorrent session
2009-08-27 07:13:39 +04:00
// Check if we should spoof utorrent
2009-11-14 22:08:28 +03:00
if ( Preferences : : isUtorrentSpoofingEnabled ( ) ) {
2009-11-06 17:30:14 +03:00
s = new session ( fingerprint ( " UT " , 1 , 8 , 5 , 0 ) , 0 ) ;
qDebug ( " Peer ID: %s " , fingerprint ( " UT " , 1 , 8 , 5 , 0 ) . to_string ( ) . c_str ( ) ) ;
2008-05-18 13:50:35 +04:00
} else {
2009-01-05 23:41:35 +03:00
s = new session ( fingerprint ( " qB " , VERSION_MAJOR , VERSION_MINOR , VERSION_BUGFIX , 0 ) , 0 ) ;
2009-09-21 01:25:17 +04:00
qDebug ( " Peer ID: %s " , fingerprint ( " qB " , VERSION_MAJOR , VERSION_MINOR , VERSION_BUGFIX , 0 ) . to_string ( ) . c_str ( ) ) ;
2008-05-18 13:50:35 +04:00
}
2009-09-03 15:29:15 +04:00
2007-03-05 16:55:23 +03:00
// Set severity level of libtorrent session
2008-11-02 02:58:53 +03:00
//s->set_alert_mask(alert::all_categories & ~alert::progress_notification);
s - > set_alert_mask ( alert : : error_notification | alert : : peer_notification | alert : : port_mapping_notification | alert : : storage_notification | alert : : tracker_notification | alert : : status_notification | alert : : ip_block_notification ) ;
2008-11-02 16:19:27 +03:00
// Load previous state
loadSessionState ( ) ;
2008-11-02 22:22:10 +03:00
// Enabling plugins
2009-10-24 18:28:00 +04:00
//s->add_extension(&create_metadata_plugin);
2008-11-02 22:22:10 +03:00
s - > add_extension ( & create_ut_metadata_plugin ) ;
2009-11-18 13:29:20 +03:00
# ifdef LIBTORRENT_0_15
2009-10-24 18:28:00 +04:00
s - > add_extension ( create_lt_trackers_plugin ) ;
2009-11-18 13:29:20 +03:00
# endif
2008-11-02 22:22:10 +03:00
s - > add_extension ( & create_ut_pex_plugin ) ;
s - > add_extension ( & create_smart_ban_plugin ) ;
2007-07-30 17:56:31 +04:00
timerAlerts = new QTimer ( ) ;
connect ( timerAlerts , SIGNAL ( timeout ( ) ) , this , SLOT ( readAlerts ( ) ) ) ;
timerAlerts - > start ( 3000 ) ;
2007-03-08 01:36:01 +03:00
// To download from urls
downloader = new downloadThread ( this ) ;
2007-07-22 13:47:27 +04:00
connect ( downloader , SIGNAL ( downloadFinished ( QString , QString ) ) , this , SLOT ( processDownloadedFile ( QString , QString ) ) ) ;
2007-08-26 20:25:22 +04:00
connect ( downloader , SIGNAL ( downloadFailure ( QString , QString ) ) , this , SLOT ( handleDownloadFailure ( QString , QString ) ) ) ;
2009-11-14 22:08:28 +03:00
// Apply user settings to Bittorrent session
configureSession ( ) ;
2007-08-26 20:25:22 +04:00
qDebug ( " * BTSession constructed " ) ;
2007-03-08 01:36:01 +03:00
}
2007-03-05 16:55:23 +03:00
// Main destructor
2009-11-20 11:01:59 +03:00
Bittorrent : : ~ Bittorrent ( ) {
2008-05-18 16:01:42 +04:00
qDebug ( " BTSession deletion " ) ;
2008-11-02 16:19:27 +03:00
// Do some BT related saving
2009-11-18 15:35:55 +03:00
saveDHTEntry ( ) ;
2008-11-02 16:19:27 +03:00
saveSessionState ( ) ;
2009-11-18 15:35:55 +03:00
saveFastResumeData ( ) ;
2007-07-29 18:54:39 +04:00
// Disable directory scanning
2007-03-05 16:55:23 +03:00
disableDirectoryScanning ( ) ;
2007-07-29 18:54:39 +04:00
// Delete our objects
2007-07-30 17:56:31 +04:00
delete timerAlerts ;
2008-09-13 11:33:41 +04:00
if ( BigRatioTimer )
2007-11-25 00:55:19 +03:00
delete BigRatioTimer ;
2008-09-13 11:33:41 +04:00
if ( filterParser )
2008-05-18 00:32:03 +04:00
delete filterParser ;
2007-03-08 01:36:01 +03:00
delete downloader ;
2008-12-26 16:39:11 +03:00
if ( FSWatcher ) {
2009-08-16 07:09:20 +04:00
delete FSWatcher ;
2008-12-26 16:39:11 +03:00
}
2009-11-18 20:46:59 +03:00
// HTTP Server
if ( httpServer )
delete httpServer ;
2009-11-16 23:28:58 +03:00
if ( timerETA )
delete timerETA ;
2007-08-08 03:15:46 +04:00
// Delete BT session
2007-11-24 15:11:12 +03:00
qDebug ( " Deleting session " ) ;
2007-03-05 16:55:23 +03:00
delete s ;
2007-11-24 15:11:12 +03:00
qDebug ( " Session deleted " ) ;
2007-03-05 16:55:23 +03:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : preAllocateAllFiles ( bool b ) {
2007-11-03 01:56:07 +03:00
bool change = ( preAllocateAll ! = b ) ;
if ( change ) {
qDebug ( " PreAllocateAll changed, reloading all torrents! " ) ;
preAllocateAll = b ;
2007-09-08 21:07:29 +04:00
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : deleteBigRatios ( ) {
2009-07-12 11:23:11 +04:00
if ( ratio_limit = = - 1 ) return ;
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_seed ( ) ) {
QString hash = h . hash ( ) ;
float ratio = getRealRatio ( hash ) ;
if ( ratio < = MAX_RATIO & & ratio > ratio_limit ) {
QString fileName = h . name ( ) ;
addConsoleMessage ( tr ( " %1 reached the maximum ratio you set. " ) . arg ( fileName ) ) ;
deleteTorrent ( hash ) ;
//emit torrent_ratio_deleted(fileName);
}
2007-09-09 13:09:24 +04:00
}
2009-08-16 07:09:20 +04:00
}
2007-09-09 13:09:24 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setDownloadLimit ( QString hash , long val ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2009-08-27 13:07:33 +04:00
if ( h . is_valid ( ) ) {
2007-07-30 20:57:28 +04:00
h . set_download_limit ( val ) ;
2009-08-27 13:07:33 +04:00
}
2007-07-14 17:38:29 +04:00
}
2009-11-20 11:01:59 +03:00
bool Bittorrent : : isQueueingEnabled ( ) const {
2008-07-15 02:01:05 +04:00
return queueingEnabled ;
2008-07-14 23:20:18 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setUploadLimit ( QString hash , long val ) {
2007-08-04 10:23:44 +04:00
qDebug ( " Set upload limit rate to %ld " , val ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2009-08-27 13:07:33 +04:00
if ( h . is_valid ( ) ) {
2007-07-30 20:57:28 +04:00
h . set_upload_limit ( val ) ;
2009-08-27 13:07:33 +04:00
}
2007-07-14 17:38:29 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : handleDownloadFailure ( QString url , QString reason ) {
2007-07-21 00:18:18 +04:00
emit downloadFromUrlFailure ( url , reason ) ;
2009-08-21 11:40:57 +04:00
// Clean up
int index = url_skippingDlg . indexOf ( url ) ;
if ( index > = 0 )
url_skippingDlg . removeAt ( index ) ;
if ( savepath_fromurl . contains ( url ) )
savepath_fromurl . remove ( url ) ;
2007-07-21 00:18:18 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : startTorrentsInPause ( bool b ) {
2007-09-08 21:07:29 +04:00
addInPause = b ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setQueueingEnabled ( bool enable ) {
2008-07-15 02:01:05 +04:00
if ( queueingEnabled ! = enable ) {
2008-08-19 04:28:44 +04:00
qDebug ( " Queueing system is changing state... " ) ;
2008-07-15 02:01:05 +04:00
queueingEnabled = enable ;
2008-07-14 23:20:18 +04:00
}
}
2009-11-14 22:08:28 +03:00
// Set BT session configuration
2009-11-20 11:01:59 +03:00
void Bittorrent : : configureSession ( ) {
2009-11-14 22:08:28 +03:00
qDebug ( " Configuring session " ) ;
// Downloads
// * Save path
2009-11-19 11:14:04 +03:00
defaultSavePath = Preferences : : getSavePath ( ) ;
2009-11-14 22:08:28 +03:00
if ( Preferences : : isTempPathEnabled ( ) ) {
setDefaultTempPath ( Preferences : : getTempPath ( ) ) ;
} else {
setDefaultTempPath ( QString : : null ) ;
}
preAllocateAllFiles ( Preferences : : preAllocateAllFiles ( ) ) ;
startTorrentsInPause ( Preferences : : addTorrentsInPause ( ) ) ;
// * Scan dir
QString scan_dir = Preferences : : getScanDir ( ) ;
if ( scan_dir . isEmpty ( ) ) {
disableDirectoryScanning ( ) ;
} else {
//Interval first
enableDirectoryScanning ( scan_dir ) ;
}
// Connection
// * Ports binding
unsigned short old_listenPort = getListenPort ( ) ;
setListeningPort ( Preferences : : getSessionPort ( ) ) ;
unsigned short new_listenPort = getListenPort ( ) ;
if ( new_listenPort ! = old_listenPort ) {
addConsoleMessage ( tr ( " qBittorrent is bound to port: TCP/%1 " , " e.g: qBittorrent is bound to port: 6881 " ) . arg ( misc : : toQString ( new_listenPort ) ) ) ;
}
// * Global download limit
int down_limit = Preferences : : getGlobalDownloadLimit ( ) ;
if ( down_limit < = 0 ) {
// Download limit disabled
setDownloadRateLimit ( - 1 ) ;
} else {
// Enabled
setDownloadRateLimit ( down_limit * 1024 ) ;
}
int up_limit = Preferences : : getGlobalUploadLimit ( ) ;
// * Global Upload limit
if ( up_limit < = 0 ) {
// Upload limit disabled
setUploadRateLimit ( - 1 ) ;
} else {
// Enabled
setUploadRateLimit ( up_limit * 1024 ) ;
}
2009-11-15 13:59:11 +03:00
// Resolve countries
2009-11-15 23:03:23 +03:00
qDebug ( " Loading country resolution settings " ) ;
2009-11-15 13:59:11 +03:00
bool new_resolv_countries = Preferences : : resolvePeerCountries ( ) ;
if ( resolve_countries ! = new_resolv_countries ) {
2009-11-15 23:03:23 +03:00
qDebug ( " in country reoslution settings " ) ;
2009-11-15 13:59:11 +03:00
resolve_countries = new_resolv_countries ;
2009-11-15 23:03:23 +03:00
if ( resolve_countries & & ! geoipDBLoaded ) {
qDebug ( " Loading geoip database " ) ;
GeoIP : : loadDatabase ( s ) ;
geoipDBLoaded = true ;
}
2009-11-15 13:59:11 +03:00
// Update torrent handles
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( h . is_valid ( ) )
h . resolve_countries ( resolve_countries ) ;
}
}
2009-11-14 22:08:28 +03:00
// * UPnP
if ( Preferences : : isUPnPEnabled ( ) ) {
enableUPnP ( true ) ;
addConsoleMessage ( tr ( " UPnP support [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
enableUPnP ( false ) ;
addConsoleMessage ( tr ( " UPnP support [OFF] " ) , QString : : fromUtf8 ( " blue " ) ) ;
}
// * NAT-PMP
if ( Preferences : : isNATPMPEnabled ( ) ) {
enableNATPMP ( true ) ;
addConsoleMessage ( tr ( " NAT-PMP support [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
enableNATPMP ( false ) ;
addConsoleMessage ( tr ( " NAT-PMP support [OFF] " ) , QString : : fromUtf8 ( " blue " ) ) ;
}
// * Session settings
session_settings sessionSettings ;
if ( Preferences : : isUtorrentSpoofingEnabled ( ) ) {
sessionSettings . user_agent = " uTorrent/1850 " ;
} else {
sessionSettings . user_agent = " qBittorrent " VERSION ;
}
sessionSettings . upnp_ignore_nonrouters = true ;
sessionSettings . use_dht_as_fallback = false ;
2009-11-19 18:50:57 +03:00
//sessionSettings.announce_to_all_trackers = true;
2009-11-19 11:43:18 +03:00
sessionSettings . announce_to_all_tiers = true ; //uTorrent behavior
2009-11-20 11:01:59 +03:00
// To keep same behavior as in qBittorrent v1.2.0
2009-11-14 22:08:28 +03:00
sessionSettings . rate_limit_ip_overhead = false ;
// Queueing System
if ( Preferences : : isQueueingSystemEnabled ( ) ) {
sessionSettings . active_downloads = Preferences : : getMaxActiveDownloads ( ) ;
sessionSettings . active_seeds = Preferences : : getMaxActiveUploads ( ) ;
sessionSettings . active_limit = Preferences : : getMaxActiveTorrents ( ) ;
sessionSettings . dont_count_slow_torrents = false ;
setQueueingEnabled ( true ) ;
} else {
sessionSettings . active_downloads = - 1 ;
sessionSettings . active_seeds = - 1 ;
sessionSettings . active_limit = - 1 ;
setQueueingEnabled ( false ) ;
}
setSessionSettings ( sessionSettings ) ;
// Bittorrent
// * Max connections limit
setMaxConnections ( Preferences : : getMaxConnecs ( ) ) ;
// * Max connections per torrent limit
setMaxConnectionsPerTorrent ( Preferences : : getMaxConnecsPerTorrent ( ) ) ;
// * Max uploads per torrent limit
setMaxUploadsPerTorrent ( Preferences : : getMaxUploadsPerTorrent ( ) ) ;
// * DHT
if ( Preferences : : isDHTEnabled ( ) ) {
// Set DHT Port
if ( enableDHT ( true ) ) {
int dht_port = new_listenPort ;
if ( ! Preferences : : isDHTPortSameAsBT ( ) )
dht_port = Preferences : : getDHTPort ( ) ;
setDHTPort ( dht_port ) ;
addConsoleMessage ( tr ( " DHT support [ON], port: UDP/%1 " ) . arg ( dht_port ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
addConsoleMessage ( tr ( " DHT support [OFF] " ) , QString : : fromUtf8 ( " red " ) ) ;
}
} else {
enableDHT ( false ) ;
addConsoleMessage ( tr ( " DHT support [OFF] " ) , QString : : fromUtf8 ( " blue " ) ) ;
}
// * PeX
addConsoleMessage ( tr ( " PeX support [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
// * LSD
if ( Preferences : : isLSDEnabled ( ) ) {
enableLSD ( true ) ;
addConsoleMessage ( tr ( " Local Peer Discovery [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
enableLSD ( false ) ;
addConsoleMessage ( tr ( " Local Peer Discovery support [OFF] " ) , QString : : fromUtf8 ( " blue " ) ) ;
}
// * Encryption
int encryptionState = Preferences : : getEncryptionSetting ( ) ;
// The most secure, rc4 only so that all streams and encrypted
pe_settings encryptionSettings ;
encryptionSettings . allowed_enc_level = pe_settings : : rc4 ;
encryptionSettings . prefer_rc4 = true ;
switch ( encryptionState ) {
case 0 : //Enabled
encryptionSettings . out_enc_policy = pe_settings : : enabled ;
encryptionSettings . in_enc_policy = pe_settings : : enabled ;
addConsoleMessage ( tr ( " Encryption support [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
break ;
case 1 : // Forced
encryptionSettings . out_enc_policy = pe_settings : : forced ;
encryptionSettings . in_enc_policy = pe_settings : : forced ;
addConsoleMessage ( tr ( " Encryption support [FORCED] " ) , QString : : fromUtf8 ( " blue " ) ) ;
break ;
default : // Disabled
encryptionSettings . out_enc_policy = pe_settings : : disabled ;
encryptionSettings . in_enc_policy = pe_settings : : disabled ;
addConsoleMessage ( tr ( " Encryption support [OFF] " ) , QString : : fromUtf8 ( " blue " ) ) ;
}
applyEncryptionSettings ( encryptionSettings ) ;
// * Desired ratio
setGlobalRatio ( Preferences : : getDesiredRatio ( ) ) ;
// * Maximum ratio
setDeleteRatio ( Preferences : : getDeleteRatio ( ) ) ;
// Ip Filter
2009-11-17 19:02:35 +03:00
FilterParserThread : : processFilterList ( s , Preferences : : bannedIPs ( ) ) ;
2009-11-14 22:08:28 +03:00
if ( Preferences : : isFilteringEnabled ( ) ) {
enableIPFilter ( Preferences : : getFilter ( ) ) ;
} else {
disableIPFilter ( ) ;
}
2009-11-18 20:46:59 +03:00
// Update Web UI
if ( Preferences : : isWebUiEnabled ( ) ) {
quint16 port = Preferences : : getWebUiPort ( ) ;
QString username = Preferences : : getWebUiUsername ( ) ;
QString password = Preferences : : getWebUiPassword ( ) ;
initWebUi ( username , password , port ) ;
} else if ( httpServer ) {
delete httpServer ;
}
2009-11-14 22:08:28 +03:00
// * Proxy settings
proxy_settings proxySettings ;
if ( Preferences : : isProxyEnabled ( ) ) {
qDebug ( " Enabling P2P proxy " ) ;
proxySettings . hostname = Preferences : : getProxyIp ( ) . toStdString ( ) ;
qDebug ( " hostname is %s " , proxySettings . hostname . c_str ( ) ) ;
proxySettings . port = Preferences : : getProxyPort ( ) ;
qDebug ( " port is %d " , proxySettings . port ) ;
if ( Preferences : : isProxyAuthEnabled ( ) ) {
proxySettings . username = Preferences : : getProxyUsername ( ) . toStdString ( ) ;
proxySettings . password = Preferences : : getProxyPassword ( ) . toStdString ( ) ;
qDebug ( " username is %s " , proxySettings . username . c_str ( ) ) ;
qDebug ( " password is %s " , proxySettings . password . c_str ( ) ) ;
}
switch ( Preferences : : getProxyType ( ) ) {
case HTTP :
qDebug ( " type: http " ) ;
proxySettings . type = proxy_settings : : http ;
break ;
case HTTP_PW :
qDebug ( " type: http_pw " ) ;
proxySettings . type = proxy_settings : : http_pw ;
break ;
case SOCKS5 :
qDebug ( " type: socks5 " ) ;
proxySettings . type = proxy_settings : : socks5 ;
break ;
default :
qDebug ( " type: socks5_pw " ) ;
proxySettings . type = proxy_settings : : socks5_pw ;
break ;
}
setProxySettings ( proxySettings , Preferences : : useProxyForTrackers ( ) , Preferences : : useProxyForPeers ( ) , Preferences : : useProxyForWebseeds ( ) , Preferences : : useProxyForDHT ( ) ) ;
} else {
qDebug ( " Disabling P2P proxy " ) ;
setProxySettings ( proxySettings , false , false , false , false ) ;
}
if ( Preferences : : isHTTPProxyEnabled ( ) ) {
qDebug ( " Enabling Search HTTP proxy " ) ;
// HTTP Proxy
QString proxy_str ;
switch ( Preferences : : getHTTPProxyType ( ) ) {
case HTTP_PW :
proxy_str = " http:// " + Preferences : : getHTTPProxyUsername ( ) + " : " + Preferences : : getHTTPProxyPassword ( ) + " @ " + Preferences : : getHTTPProxyIp ( ) + " : " + QString : : number ( Preferences : : getHTTPProxyPort ( ) ) ;
break ;
default :
proxy_str = " http:// " + Preferences : : getHTTPProxyIp ( ) + " : " + QString : : number ( Preferences : : getHTTPProxyPort ( ) ) ;
}
// We need this for urllib in search engine plugins
# ifdef Q_WS_WIN
char proxystr [ 512 ] ;
snprintf ( proxystr , 512 , " http_proxy=%s " , proxy_str . toLocal8Bit ( ) . data ( ) ) ;
putenv ( proxystr ) ;
# else
qDebug ( " HTTP: proxy string: %s " , proxy_str . toLocal8Bit ( ) . data ( ) ) ;
setenv ( " http_proxy " , proxy_str . toLocal8Bit ( ) . data ( ) , 1 ) ;
# endif
} else {
qDebug ( " Disabling search proxy " ) ;
# ifdef Q_WS_WIN
putenv ( " http_proxy= " ) ;
# else
unsetenv ( " http_proxy " ) ;
# endif
}
qDebug ( " Session configured " ) ;
}
2009-11-20 11:01:59 +03:00
bool Bittorrent : : initWebUi ( QString username , QString password , int port ) {
2009-11-18 20:46:59 +03:00
if ( httpServer )
httpServer - > close ( ) ;
else
httpServer = new HttpServer ( this , 3000 , this ) ;
httpServer - > setAuthorization ( username , password ) ;
bool success = httpServer - > listen ( QHostAddress : : Any , port ) ;
if ( success )
qDebug ( " Web UI listening on port %d " , port ) ;
else
addConsoleMessage ( tr ( " Web User Interface Error - Unable to bind Web UI to port %1 " ) . arg ( port ) , QColor ( " red " ) ) ;
return success ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : takeETASamples ( ) {
2009-11-16 23:28:58 +03:00
bool change = false ; ;
foreach ( const QString & hash , ETA_samples . keys ( ) ) {
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( h . is_valid ( ) & & ! h . is_paused ( ) & & ! h . is_seed ( ) ) {
QList < int > samples = ETA_samples . value ( h . hash ( ) , QList < int > ( ) ) ;
if ( samples . size ( ) > = MAX_SAMPLES )
samples . removeFirst ( ) ;
samples . append ( h . download_payload_rate ( ) ) ;
ETA_samples [ h . hash ( ) ] = samples ;
change = true ;
} else {
ETA_samples . remove ( hash ) ;
2007-06-29 19:23:15 +04:00
}
2009-11-16 23:28:58 +03:00
}
if ( ! change & & timerETA ) {
delete timerETA ;
}
}
// This algorithm was inspired from KTorrent - http://www.ktorrent.org
// Calculate the ETA using a combination of several algorithms:
// GASA: Global Average Speed Algorithm
// CSA: Current Speed Algorithm
// WINX: Window of X Algorithm
2009-11-20 11:01:59 +03:00
qlonglong Bittorrent : : getETA ( QString hash ) {
2009-11-16 23:28:58 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) | | h . state ( ) ! = torrent_status : : downloading | | ! h . active_time ( ) )
2009-08-16 07:09:20 +04:00
return - 1 ;
2009-11-16 23:28:58 +03:00
// See if the torrent is going to be completed soon
qulonglong bytes_left = h . actual_size ( ) - h . total_wanted_done ( ) ;
if ( h . actual_size ( ) > 10485760L ) { // Size > 10MiB
if ( h . progress ( ) > = ( float ) 0.99 & & bytes_left < 10485760L ) { // Progress>99% but less than 10MB left.
// Compute by taking samples
if ( ! ETA_samples . contains ( h . hash ( ) ) ) {
ETA_samples [ h . hash ( ) ] = QList < int > ( ) ;
}
if ( ! timerETA ) {
timerETA = new QTimer ( this ) ;
connect ( timerETA , SIGNAL ( timeout ( ) ) , this , SLOT ( takeETASamples ( ) ) ) ;
timerETA - > start ( ) ;
} else {
QList < int > samples = ETA_samples . value ( h . hash ( ) , QList < int > ( ) ) ;
int nb_samples = samples . size ( ) ;
if ( nb_samples > 3 ) {
long sum_samples = 0 ;
foreach ( int val , samples ) {
sum_samples + = val ;
}
// Use WINX
return ( qlonglong ) ( ( ( double ) bytes_left ) / ( ( ( double ) sum_samples ) / ( ( double ) nb_samples ) ) ) ;
}
}
}
2009-08-16 07:09:20 +04:00
}
2009-11-16 23:28:58 +03:00
// Normal case: Use GASA
double avg_speed = ( double ) h . all_time_download ( ) / h . active_time ( ) ;
return ( qlonglong ) floor ( ( double ) ( bytes_left ) / avg_speed ) ;
2007-06-29 19:23:15 +04:00
}
2009-11-20 11:01:59 +03:00
std : : vector < torrent_handle > Bittorrent : : getTorrents ( ) const {
2009-08-16 07:09:20 +04:00
return s - > get_torrents ( ) ;
2008-12-27 00:18:33 +03:00
}
2007-03-05 16:55:23 +03:00
// Return the torrent handle, given its hash
2009-11-20 11:01:59 +03:00
QTorrentHandle Bittorrent : : getTorrentHandle ( QString hash ) const {
2007-08-20 10:29:18 +04:00
return QTorrentHandle ( s - > find_torrent ( misc : : fromString < sha1_hash > ( ( hash . toStdString ( ) ) ) ) ) ;
2007-03-05 16:55:23 +03:00
}
2009-11-20 11:01:59 +03:00
bool Bittorrent : : hasActiveTorrents ( ) const {
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
2009-11-07 22:55:33 +03:00
if ( h . is_valid ( ) & & ! h . is_paused ( ) & & ! h . is_queued ( ) )
return true ;
2009-08-16 07:09:20 +04:00
}
2009-11-07 22:55:33 +03:00
return false ;
2008-05-18 13:26:02 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : banIP ( QString ip ) {
2009-11-17 19:02:35 +03:00
FilterParserThread : : processFilterList ( s , QStringList ( ip ) ) ;
Preferences : : banIP ( ip ) ;
}
2007-03-05 16:55:23 +03:00
// Delete a torrent from the session, given its hash
// permanent = true means that the torrent will be removed from the hard-drive too
2009-11-20 11:01:59 +03:00
void Bittorrent : : deleteTorrent ( QString hash , bool delete_local_files ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Deleting torrent with hash: %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
2007-03-05 20:35:38 +03:00
return ;
}
2007-08-20 10:29:18 +04:00
QString savePath = h . save_path ( ) ;
QString fileName = h . name ( ) ;
2007-10-19 20:00:42 +04:00
// Remove it from session
2009-11-18 15:11:15 +03:00
if ( delete_local_files )
2009-08-16 07:09:20 +04:00
s - > remove_torrent ( h . get_torrent_handle ( ) , session : : delete_files ) ;
2008-11-02 00:42:56 +03:00
else
s - > remove_torrent ( h . get_torrent_handle ( ) ) ;
2007-03-05 16:55:23 +03:00
// Remove it from torrent backup directory
2007-03-05 20:35:38 +03:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2007-07-31 12:13:38 +04:00
QStringList filters ;
filters < < hash + " .* " ;
QStringList files = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & file , files ) {
2007-07-31 12:13:38 +04:00
torrentBackup . remove ( file ) ;
}
2009-08-16 07:09:20 +04:00
TorrentPersistentData : : deletePersistentData ( hash ) ;
2007-07-27 17:58:12 +04:00
// Remove tracker errors
2009-11-19 18:04:43 +03:00
trackersInfos . remove ( hash ) ;
2009-11-18 15:11:15 +03:00
if ( delete_local_files )
addConsoleMessage ( tr ( " '%1' was removed from transfer list and hard disk. " , " 'xxx.avi' was removed... " ) . arg ( fileName ) ) ;
2008-09-07 15:31:29 +04:00
else
2009-11-18 15:11:15 +03:00
addConsoleMessage ( tr ( " '%1' was removed from transfer list. " , " 'xxx.avi' was removed... " ) . arg ( fileName ) ) ;
2008-05-16 11:10:50 +04:00
emit deletedTorrent ( hash ) ;
2007-03-05 16:55:23 +03:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : pauseAllTorrents ( ) {
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( ! h . is_paused ( ) ) {
h . pause ( ) ;
emit pausedTorrent ( h ) ;
2008-12-27 00:18:33 +03:00
}
2009-08-16 07:09:20 +04:00
}
2008-05-16 11:10:50 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : resumeAllTorrents ( ) {
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_paused ( ) ) {
h . resume ( ) ;
emit resumedTorrent ( h ) ;
2008-12-27 00:18:33 +03:00
}
2009-08-16 07:09:20 +04:00
}
2008-05-16 11:10:50 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : pauseTorrent ( QString hash ) {
2009-08-16 07:09:20 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_paused ( ) ) {
h . pause ( ) ;
emit pausedTorrent ( h ) ;
}
2008-12-27 13:14:16 +03:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : resumeTorrent ( QString hash ) {
2009-08-16 07:09:20 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( h . is_paused ( ) ) {
h . resume ( ) ;
emit resumedTorrent ( h ) ;
}
2008-12-27 13:14:16 +03:00
}
2009-11-20 11:01:59 +03:00
QTorrentHandle Bittorrent : : addMagnetUri ( QString magnet_uri , bool resumed ) {
2009-08-17 09:14:03 +04:00
QTorrentHandle h ;
QString hash = misc : : magnetUriToHash ( magnet_uri ) ;
if ( hash . isEmpty ( ) ) {
addConsoleMessage ( tr ( " '%1' is not a valid magnet URI. " ) . arg ( magnet_uri ) ) ;
return h ;
}
if ( resumed ) {
2009-08-21 11:40:57 +04:00
qDebug ( " Resuming magnet URI: %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
} else {
qDebug ( " Adding new magnet URI " ) ;
}
bool fastResume = false ;
Q_ASSERT ( magnet_uri . startsWith ( " magnet: " ) ) ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
// Checking if BT_backup Dir exists
// create it if it is not
if ( ! torrentBackup . exists ( ) ) {
if ( ! torrentBackup . mkpath ( torrentBackup . path ( ) ) ) {
std : : cerr < < " Couldn't create the directory: ' " < < torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) < < " ' \n " ;
exit ( 1 ) ;
}
}
// Check if torrent is already in download list
2009-08-21 11:40:57 +04:00
if ( s - > find_torrent ( sha1_hash ( hash . toLocal8Bit ( ) . data ( ) ) ) . is_valid ( ) ) {
2009-08-17 09:14:03 +04:00
qDebug ( " /! \\ Torrent is already in download list " ) ;
// Update info Bar
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( magnet_uri ) ) ;
return h ;
}
add_torrent_params p ;
//Getting fast resume data if existing
std : : vector < char > buf ;
if ( resumed ) {
qDebug ( " Trying to load fastresume data: %s " , ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) ) ;
if ( load_file ( ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) , buf ) = = 0 ) {
fastResume = true ;
p . resume_data = & buf ;
qDebug ( " Successfuly loaded " ) ;
}
}
QString savePath = getSavePath ( hash ) ;
2009-08-21 11:40:57 +04:00
qDebug ( " addMagnetURI: using save_path: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
if ( defaultTempPath . isEmpty ( ) | | ( resumed & & TorrentPersistentData : : isSeed ( hash ) ) ) {
p . save_path = savePath . toLocal8Bit ( ) . data ( ) ;
} else {
p . save_path = defaultTempPath . toLocal8Bit ( ) . data ( ) ;
}
// Preallocate all?
if ( preAllocateAll )
p . storage_mode = storage_mode_allocate ;
else
p . storage_mode = storage_mode_sparse ;
// Start in pause
2009-08-17 09:23:58 +04:00
p . paused = false ;
2009-08-17 09:14:03 +04:00
p . duplicate_is_error = false ; // Already checked
p . auto_managed = false ; // Because it is added in paused state
2009-11-20 11:01:59 +03:00
// Adding torrent to Bittorrent session
2009-08-17 09:14:03 +04:00
try {
h = QTorrentHandle ( add_magnet_uri ( * s , magnet_uri . toStdString ( ) , p ) ) ;
} catch ( std : : exception e ) {
qDebug ( " Error: %s " , e . what ( ) ) ;
}
// Check if it worked
if ( ! h . is_valid ( ) ) {
// No need to keep on, it failed.
qDebug ( " /! \\ Error: Invalid handle " ) ;
return h ;
}
Q_ASSERT ( h . hash ( ) = = hash ) ;
// Connections limit per torrent
2009-11-17 16:11:32 +03:00
h . set_max_connections ( Preferences : : getMaxConnecsPerTorrent ( ) ) ;
2009-08-17 09:14:03 +04:00
// Uploads limit per torrent
2009-11-17 16:11:32 +03:00
h . set_max_uploads ( Preferences : : getMaxUploadsPerTorrent ( ) ) ;
2009-11-15 13:59:11 +03:00
// Resolve countries
h . resolve_countries ( resolve_countries ) ;
2009-08-17 09:14:03 +04:00
// Load filtered files
2009-11-20 21:29:13 +03:00
if ( ! resumed ) {
2009-08-17 09:14:03 +04:00
// Sequential download
if ( TorrentTempData : : hasTempData ( hash ) ) {
qDebug ( " addMagnetUri: Setting download as sequential (from tmp data) " ) ;
h . set_sequential_download ( TorrentTempData : : isSequential ( hash ) ) ;
}
// Save persistent data for new torrent
Q_ASSERT ( h . is_valid ( ) ) ;
2009-08-21 11:40:57 +04:00
qDebug ( " addMagnetUri: hash: %s " , h . hash ( ) . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
TorrentPersistentData : : saveTorrentPersistentData ( h , true ) ;
qDebug ( " Persistent data saved " ) ;
// Save save_path
if ( ! defaultTempPath . isEmpty ( ) ) {
2009-08-21 11:40:57 +04:00
qDebug ( " addMagnetUri: Saving save_path in persistent data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
TorrentPersistentData : : saveSavePath ( hash , savePath ) ;
}
}
if ( ! addInPause & & ! fastResume ) {
// Start torrent because it was added in paused state
h . resume ( ) ;
}
// Send torrent addition signal
if ( fastResume )
addConsoleMessage ( tr ( " '%1' resumed. (fast resume) " , " '/home/y/xxx.torrent' was resumed. (fast resume) " ) . arg ( magnet_uri ) ) ;
else
addConsoleMessage ( tr ( " '%1' added to download list. " , " '/home/y/xxx.torrent' was added to download list. " ) . arg ( magnet_uri ) ) ;
emit addedTorrent ( h ) ;
return h ;
}
2009-11-20 11:01:59 +03:00
// Add a torrent to the Bittorrent session
QTorrentHandle Bittorrent : : addTorrent ( QString path , bool fromScanDir , QString from_url , bool resumed ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h ;
2007-03-05 16:55:23 +03:00
bool fastResume = false ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2009-08-16 07:09:20 +04:00
QString file , dest_file , hash ;
boost : : intrusive_ptr < torrent_info > t ;
2007-03-05 16:55:23 +03:00
// Checking if BT_backup Dir exists
// create it if it is not
2007-08-20 10:29:18 +04:00
if ( ! torrentBackup . exists ( ) ) {
if ( ! torrentBackup . mkpath ( torrentBackup . path ( ) ) ) {
2009-07-12 12:13:00 +04:00
std : : cerr < < " Couldn't create the directory: ' " < < torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) < < " ' \n " ;
2007-03-05 20:35:38 +03:00
exit ( 1 ) ;
2007-03-05 16:55:23 +03:00
}
}
// Processing torrents
2007-09-08 21:07:29 +04:00
file = path . trimmed ( ) . replace ( " file:// " , " " , Qt : : CaseInsensitive ) ;
2007-08-20 10:29:18 +04:00
if ( file . isEmpty ( ) ) {
2008-12-30 03:34:41 +03:00
return h ;
2007-03-05 16:55:23 +03:00
}
2007-09-09 11:44:22 +04:00
Q_ASSERT ( ! file . startsWith ( " http:// " , Qt : : CaseInsensitive ) & & ! file . startsWith ( " https:// " , Qt : : CaseInsensitive ) & & ! file . startsWith ( " ftp:// " , Qt : : CaseInsensitive ) ) ;
2009-08-16 07:09:20 +04:00
2009-07-12 12:13:00 +04:00
qDebug ( " Adding %s to download list " , file . toLocal8Bit ( ) . data ( ) ) ;
2008-11-02 00:42:56 +03:00
try {
2009-08-16 07:09:20 +04:00
// Getting torrent file informations
t = new torrent_info ( file . toLocal8Bit ( ) . data ( ) ) ;
2008-11-02 00:42:56 +03:00
} catch ( std : : exception & ) {
2009-08-16 07:09:20 +04:00
if ( ! from_url . isNull ( ) ) {
addConsoleMessage ( tr ( " Unable to decode torrent file: '%1' " , " e.g: Unable to decode torrent file: '/home/y/xxx.torrent' " ) . arg ( from_url ) , QString : : fromUtf8 ( " red " ) ) ;
//emit invalidTorrent(from_url);
QFile : : remove ( file ) ;
} else {
addConsoleMessage ( tr ( " Unable to decode torrent file: '%1' " , " e.g: Unable to decode torrent file: '/home/y/xxx.torrent' " ) . arg ( file ) , QString : : fromUtf8 ( " red " ) ) ;
//emit invalidTorrent(file);
}
addConsoleMessage ( tr ( " This file is either corrupted or this isn't a torrent. " ) , QString : : fromUtf8 ( " red " ) ) ;
if ( fromScanDir ) {
// Remove file
QFile : : remove ( file ) ;
}
return h ;
2008-11-02 00:42:56 +03:00
}
qDebug ( " -> Hash: %s " , misc : : toString ( t - > info_hash ( ) ) . c_str ( ) ) ;
qDebug ( " -> Name: %s " , t - > name ( ) . c_str ( ) ) ;
2009-08-16 07:09:20 +04:00
hash = misc : : toQString ( t - > info_hash ( ) ) ;
2009-08-17 09:14:03 +04:00
2008-11-02 00:42:56 +03:00
// Check if torrent is already in download list
if ( s - > find_torrent ( t - > info_hash ( ) ) . is_valid ( ) ) {
2009-08-16 07:09:20 +04:00
qDebug ( " /! \\ Torrent is already in download list " ) ;
// Update info Bar
if ( ! fromScanDir ) {
if ( ! from_url . isNull ( ) ) {
// If download from url, remove temp file
QFile : : remove ( file ) ;
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( from_url ) ) ;
//emit duplicateTorrent(from_url);
2007-03-05 16:55:23 +03:00
} else {
2009-08-16 07:09:20 +04:00
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( file ) ) ;
//emit duplicateTorrent(file);
2007-03-05 16:55:23 +03:00
}
2009-08-16 07:09:20 +04:00
} else {
// Delete torrent from scan dir
QFile : : remove ( file ) ;
}
return h ;
2008-11-02 00:42:56 +03:00
}
add_torrent_params p ;
//Getting fast resume data if existing
std : : vector < char > buf ;
2009-08-16 07:09:20 +04:00
if ( resumed ) {
qDebug ( " Trying to load fastresume data: %s " , ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) ) ;
if ( load_file ( ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) , buf ) = = 0 ) {
2008-11-02 00:42:56 +03:00
fastResume = true ;
p . resume_data = & buf ;
2008-11-02 02:58:53 +03:00
qDebug ( " Successfuly loaded " ) ;
2009-08-16 07:09:20 +04:00
}
2008-11-02 00:42:56 +03:00
}
2009-08-21 11:40:57 +04:00
QString savePath ;
if ( ! from_url . isEmpty ( ) & & savepath_fromurl . contains ( from_url ) ) {
// Enforcing the save path defined before URL download (from RSS for example)
savePath = savepath_fromurl . take ( from_url ) ;
} else {
savePath = getSavePath ( hash ) ;
}
qDebug ( " addTorrent: using save_path: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
if ( defaultTempPath . isEmpty ( ) | | ( resumed & & TorrentPersistentData : : isSeed ( hash ) ) ) {
2009-07-12 12:13:00 +04:00
p . save_path = savePath . toLocal8Bit ( ) . data ( ) ;
2009-03-08 19:26:02 +03:00
} else {
2009-07-12 12:13:00 +04:00
p . save_path = defaultTempPath . toLocal8Bit ( ) . data ( ) ;
2009-03-08 19:26:02 +03:00
}
2009-11-06 17:30:14 +03:00
2009-11-18 13:29:20 +03:00
# ifdef LIBTORRENT_0_15
2009-11-06 17:30:14 +03:00
// Skip checking and directly start seeding (new in libtorrent v0.15)
if ( TorrentTempData : : isSeedingMode ( hash ) ) {
p . seed_mode = true ;
} else {
p . seed_mode = false ;
}
2009-11-18 13:29:20 +03:00
# endif
2009-08-27 07:13:39 +04:00
// TODO: Remove in v1.6.0: For backward compatibility only
if ( QFile : : exists ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .finished " ) ) {
p . save_path = savePath . toLocal8Bit ( ) . data ( ) ;
QFile : : remove ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .finished " ) ;
}
2008-11-02 00:42:56 +03:00
p . ti = t ;
// Preallocate all?
if ( preAllocateAll )
p . storage_mode = storage_mode_allocate ;
else
p . storage_mode = storage_mode_sparse ;
// Start in pause
p . paused = true ;
p . duplicate_is_error = false ; // Already checked
2008-12-27 16:04:06 +03:00
p . auto_managed = false ; // Because it is added in paused state
2009-11-20 11:01:59 +03:00
// Adding torrent to Bittorrent session
2008-12-28 00:25:58 +03:00
try {
2009-08-16 07:09:20 +04:00
h = QTorrentHandle ( s - > add_torrent ( p ) ) ;
2008-12-28 00:25:58 +03:00
} catch ( std : : exception e ) {
qDebug ( " Error: %s " , e . what ( ) ) ;
}
2008-11-02 00:42:56 +03:00
// Check if it worked
if ( ! h . is_valid ( ) ) {
2009-08-16 07:09:20 +04:00
// No need to keep on, it failed.
qDebug ( " /! \\ Error: Invalid handle " ) ;
// If download from url, remove temp file
if ( ! from_url . isNull ( ) ) QFile : : remove ( file ) ;
return h ;
2008-11-02 00:42:56 +03:00
}
2009-11-20 21:36:34 +03:00
// FIXME: Remove this debug
std : : vector < announce_entry > trackers = h . trackers ( ) ;
std : : vector < announce_entry > : : iterator it ;
for ( it = trackers . begin ( ) ; it ! = trackers . end ( ) ; it + + ) {
qDebug ( " * Tracker: %s " , it - > url . c_str ( ) ) ;
}
2008-11-02 00:42:56 +03:00
// Connections limit per torrent
2009-11-17 16:11:32 +03:00
h . set_max_connections ( Preferences : : getMaxConnecsPerTorrent ( ) ) ;
2008-11-02 00:42:56 +03:00
// Uploads limit per torrent
2009-11-17 16:11:32 +03:00
h . set_max_uploads ( Preferences : : getMaxUploadsPerTorrent ( ) ) ;
2009-11-15 13:59:11 +03:00
// Resolve countries
2009-11-15 15:57:25 +03:00
qDebug ( " AddTorrent: Resolve_countries: %d " , ( int ) resolve_countries ) ;
2009-11-15 13:59:11 +03:00
h . resolve_countries ( resolve_countries ) ;
2009-11-20 21:29:13 +03:00
if ( ! resumed ) {
2009-08-16 07:09:20 +04:00
// Sequential download
if ( TorrentTempData : : hasTempData ( hash ) ) {
qDebug ( " addTorrent: Setting download as sequential (from tmp data) " ) ;
h . set_sequential_download ( TorrentTempData : : isSequential ( hash ) ) ;
}
// Save persistent data for new torrent
TorrentPersistentData : : saveTorrentPersistentData ( h ) ;
// Save save_path
if ( ! defaultTempPath . isEmpty ( ) ) {
2009-08-21 11:40:57 +04:00
qDebug ( " addTorrent: Saving save_path in persistent data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
TorrentPersistentData : : saveSavePath ( hash , savePath ) ;
}
2008-11-02 00:42:56 +03:00
}
QString newFile = torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " ;
if ( file ! = newFile ) {
2009-08-16 07:09:20 +04:00
// Delete file from torrentBackup directory in case it exists because
// QFile::copy() do not overwrite
QFile : : remove ( newFile ) ;
// Copy it to torrentBackup directory
QFile : : copy ( file , newFile ) ;
2008-11-02 00:42:56 +03:00
}
2008-12-30 03:34:41 +03:00
if ( ! addInPause & & ! fastResume ) {
2009-08-16 07:09:20 +04:00
// Start torrent because it was added in paused state
h . resume ( ) ;
2008-11-02 00:42:56 +03:00
}
// If download from url, remove temp file
if ( ! from_url . isNull ( ) ) QFile : : remove ( file ) ;
// Delete from scan dir to avoid trying to download it again
if ( fromScanDir ) {
2009-08-16 07:09:20 +04:00
QFile : : remove ( file ) ;
2008-11-02 00:42:56 +03:00
}
// Send torrent addition signal
if ( ! from_url . isNull ( ) ) {
2009-08-16 07:09:20 +04:00
if ( fastResume )
addConsoleMessage ( tr ( " '%1' resumed. (fast resume) " , " '/home/y/xxx.torrent' was resumed. (fast resume) " ) . arg ( from_url ) ) ;
else
addConsoleMessage ( tr ( " '%1' added to download list. " , " '/home/y/xxx.torrent' was added to download list. " ) . arg ( from_url ) ) ;
2008-11-02 00:42:56 +03:00
} else {
2009-08-16 07:09:20 +04:00
if ( fastResume )
addConsoleMessage ( tr ( " '%1' resumed. (fast resume) " , " '/home/y/xxx.torrent' was resumed. (fast resume) " ) . arg ( file ) ) ;
else
addConsoleMessage ( tr ( " '%1' added to download list. " , " '/home/y/xxx.torrent' was added to download list. " ) . arg ( file ) ) ;
2008-01-04 23:43:28 +03:00
}
2008-12-29 21:36:54 +03:00
emit addedTorrent ( h ) ;
2008-12-30 03:34:41 +03:00
return h ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
// Set the maximum number of opened connections
2009-11-20 11:01:59 +03:00
void Bittorrent : : setMaxConnections ( int maxConnec ) {
2007-08-20 10:29:18 +04:00
s - > set_max_connections ( maxConnec ) ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setMaxConnectionsPerTorrent ( int max ) {
2007-09-09 11:44:22 +04:00
// Apply this to all session torrents
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
unsigned int nbHandles = handles . size ( ) ;
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_max_connections ( max ) ;
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setMaxUploadsPerTorrent ( int max ) {
2007-09-09 11:44:22 +04:00
// Apply this to all session torrents
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
unsigned int nbHandles = handles . size ( ) ;
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_max_uploads ( max ) ;
}
}
2007-03-05 16:55:23 +03:00
// Return DHT state
2009-11-20 11:01:59 +03:00
bool Bittorrent : : isDHTEnabled ( ) const {
2007-03-05 16:55:23 +03:00
return DHTEnabled ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : enableUPnP ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
2007-11-23 18:20:26 +03:00
if ( ! UPnPEnabled ) {
qDebug ( " Enabling UPnP " ) ;
s - > start_upnp ( ) ;
UPnPEnabled = true ;
}
2007-09-08 21:07:29 +04:00
} else {
2007-11-23 18:20:26 +03:00
if ( UPnPEnabled ) {
qDebug ( " Disabling UPnP " ) ;
s - > stop_upnp ( ) ;
UPnPEnabled = false ;
}
2007-03-05 16:55:23 +03:00
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : enableNATPMP ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
2007-11-23 18:20:26 +03:00
if ( ! NATPMPEnabled ) {
qDebug ( " Enabling NAT-PMP " ) ;
s - > start_natpmp ( ) ;
NATPMPEnabled = true ;
}
2007-09-08 21:07:29 +04:00
} else {
2007-11-23 18:20:26 +03:00
if ( NATPMPEnabled ) {
qDebug ( " Disabling NAT-PMP " ) ;
s - > stop_natpmp ( ) ;
NATPMPEnabled = false ;
}
2007-09-08 21:07:29 +04:00
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : enableLSD ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
2007-11-23 18:20:26 +03:00
if ( ! LSDEnabled ) {
qDebug ( " Enabling LSD " ) ;
s - > start_lsd ( ) ;
LSDEnabled = true ;
}
2007-09-08 21:07:29 +04:00
} else {
2007-11-23 18:20:26 +03:00
if ( LSDEnabled ) {
qDebug ( " Disabling LSD " ) ;
s - > stop_lsd ( ) ;
LSDEnabled = false ;
}
2007-09-08 21:07:29 +04:00
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : loadSessionState ( ) {
2009-08-16 07:09:20 +04:00
boost : : filesystem : : ifstream ses_state_file ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " ses_state " ) ) . toLocal8Bit ( ) . data ( )
, std : : ios_base : : binary ) ;
ses_state_file . unsetf ( std : : ios_base : : skipws ) ;
s - > load_state ( bdecode (
std : : istream_iterator < char > ( ses_state_file )
, std : : istream_iterator < char > ( ) ) ) ;
2008-11-02 16:19:27 +03:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : saveSessionState ( ) {
2009-08-16 07:09:20 +04:00
qDebug ( " Saving session state to disk... " ) ;
entry session_state = s - > state ( ) ;
boost : : filesystem : : ofstream out ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " ses_state " ) ) . toLocal8Bit ( ) . data ( )
, std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , session_state ) ;
2008-11-02 16:19:27 +03:00
}
2007-09-08 21:07:29 +04:00
// Enable DHT
2009-11-20 11:01:59 +03:00
bool Bittorrent : : enableDHT ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
if ( ! DHTEnabled ) {
entry dht_state ;
2008-12-15 20:28:05 +03:00
QString dht_state_path = misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " dht_state " ) ;
if ( QFile : : exists ( dht_state_path ) ) {
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ifstream dht_state_file ( dht_state_path . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2008-12-15 20:28:05 +03:00
dht_state_file . unsetf ( std : : ios_base : : skipws ) ;
try {
dht_state = bdecode ( std : : istream_iterator < char > ( dht_state_file ) , std : : istream_iterator < char > ( ) ) ;
} catch ( std : : exception & ) { }
2009-08-16 07:09:20 +04:00
}
2007-10-28 00:53:09 +04:00
try {
2009-08-16 07:09:20 +04:00
s - > start_dht ( dht_state ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.bittorrent.com " ) , 6881 ) ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.utorrent.com " ) , 6881 ) ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.bitcomet.com " ) , 6881 ) ) ;
DHTEnabled = true ;
qDebug ( " DHT enabled " ) ;
} catch ( std : : exception e ) {
qDebug ( " Could not enable DHT, reason: %s " , e . what ( ) ) ;
return false ;
}
2007-09-08 21:07:29 +04:00
}
} else {
if ( DHTEnabled ) {
DHTEnabled = false ;
s - > stop_dht ( ) ;
qDebug ( " DHT disabled " ) ;
}
2007-03-05 16:55:23 +03:00
}
2007-10-28 00:53:09 +04:00
return true ;
2007-03-05 16:55:23 +03:00
}
2009-11-20 11:01:59 +03:00
float Bittorrent : : getRealRatio ( QString hash ) const {
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2008-11-02 01:07:14 +03:00
Q_ASSERT ( h . all_time_download ( ) > = 0 ) ;
Q_ASSERT ( h . all_time_upload ( ) > = 0 ) ;
if ( h . all_time_download ( ) = = 0 ) {
2009-07-12 12:21:06 +04:00
if ( h . all_time_upload ( ) = = 0 )
return 0 ;
return 101 ;
2008-11-02 01:07:14 +03:00
}
float ratio = ( float ) h . all_time_upload ( ) / ( float ) h . all_time_download ( ) ;
2007-08-20 10:29:18 +04:00
Q_ASSERT ( ratio > = 0. ) ;
2009-01-23 22:26:22 +03:00
if ( ratio > 100. )
ratio = 100. ;
2007-08-20 10:29:18 +04:00
return ratio ;
}
2007-11-23 00:50:22 +03:00
// Only save fast resume data for unfinished and unpaused torrents (Optimization)
// Called periodically and on exit
2009-11-20 11:01:59 +03:00
void Bittorrent : : saveFastResumeData ( ) {
2008-11-02 02:58:53 +03:00
// Stop listening for alerts
timerAlerts - > stop ( ) ;
int num_resume_data = 0 ;
2008-12-30 00:10:31 +03:00
// Pause session
2008-11-02 02:58:53 +03:00
s - > pause ( ) ;
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2008-12-30 00:10:31 +03:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
2009-01-06 00:19:55 +03:00
if ( ! h . is_valid ( ) | | ! h . has_metadata ( ) ) continue ;
if ( isQueueingEnabled ( ) )
2009-08-16 07:09:20 +04:00
TorrentPersistentData : : savePriority ( h ) ;
2009-11-20 21:36:34 +03:00
// Actually with should save fast resume data for paused files too
//if(h.is_paused()) continue;
2008-11-02 03:04:38 +03:00
if ( h . state ( ) = = torrent_status : : checking_files | | h . state ( ) = = torrent_status : : queued_for_checking ) continue ;
2008-11-02 02:58:53 +03:00
h . save_resume_data ( ) ;
+ + num_resume_data ;
}
2008-12-30 00:10:31 +03:00
while ( num_resume_data > 0 ) {
2009-08-16 07:09:20 +04:00
alert const * a = s - > wait_for_alert ( seconds ( 30 ) ) ;
if ( a = = 0 ) {
std : : cerr < < " aborting with " < < num_resume_data < < " outstanding "
" torrents to save resume data for " < < std : : endl ;
break ;
}
// Saving fastresume data can fail
save_resume_data_failed_alert const * rda = dynamic_cast < save_resume_data_failed_alert const * > ( a ) ;
if ( rda ) {
2008-11-02 02:58:53 +03:00
- - num_resume_data ;
2009-08-16 07:09:20 +04:00
s - > pop_alert ( ) ;
2008-12-30 00:22:26 +03:00
// Remove torrent from session
2009-08-16 07:09:20 +04:00
s - > remove_torrent ( rda - > handle ) ;
continue ;
}
save_resume_data_alert const * rd = dynamic_cast < save_resume_data_alert const * > ( a ) ;
if ( ! rd ) {
2008-11-02 02:58:53 +03:00
s - > pop_alert ( ) ;
2009-08-16 07:09:20 +04:00
continue ;
}
// Saving fast resume data was successful
- - num_resume_data ;
if ( ! rd - > resume_data ) continue ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QTorrentHandle h ( rd - > handle ) ;
// Remove old fastresume file if it exists
QFile : : remove ( torrentBackup . path ( ) + QDir : : separator ( ) + h . hash ( ) + " .fastresume " ) ;
QString file = h . hash ( ) + " .fastresume " ;
boost : : filesystem : : ofstream out ( fs : : path ( torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) ) / file . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , * rd - > resume_data ) ;
// Remove torrent from session
s - > remove_torrent ( rd - > handle ) ;
s - > pop_alert ( ) ;
2008-08-26 10:39:57 +04:00
}
2007-03-05 16:55:23 +03:00
}
2009-11-20 11:01:59 +03:00
QStringList Bittorrent : : getConsoleMessages ( ) const {
2008-09-07 15:31:29 +04:00
return consoleMessages ;
}
2009-11-20 11:01:59 +03:00
QStringList Bittorrent : : getPeerBanMessages ( ) const {
2008-09-07 15:31:29 +04:00
return peerBanMessages ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : addConsoleMessage ( QString msg , QColor color ) {
2008-09-07 15:31:29 +04:00
if ( consoleMessages . size ( ) > 100 ) {
consoleMessages . removeFirst ( ) ;
}
2009-07-12 13:08:38 +04:00
consoleMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - <font color=' " ) + color . name ( ) + QString : : fromUtf8 ( " '><i> " ) + msg + QString : : fromUtf8 ( " </i></font> " ) ) ;
2008-09-07 15:31:29 +04:00
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : addPeerBanMessage ( QString ip , bool from_ipfilter ) {
2008-09-07 15:31:29 +04:00
if ( peerBanMessages . size ( ) > 100 ) {
peerBanMessages . removeFirst ( ) ;
}
if ( from_ipfilter )
2009-07-12 13:08:38 +04:00
peerBanMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - " ) + tr ( " <font color='red'>%1</font> <i>was blocked due to your IP filter</i> " , " x.y.z.w was blocked " ) . arg ( ip ) ) ;
2008-09-07 15:31:29 +04:00
else
2009-07-12 13:08:38 +04:00
peerBanMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - " ) + tr ( " <font color='red'>%1</font> <i>was banned due to corrupt pieces</i> " , " x.y.z.w was banned " ) . arg ( ip ) ) ;
2008-09-07 15:31:29 +04:00
}
2009-11-20 11:01:59 +03:00
bool Bittorrent : : isFilePreviewPossible ( QString hash ) const {
2007-03-05 16:55:23 +03:00
// See if there are supported files in the torrent
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
2007-03-05 20:35:38 +03:00
return false ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
unsigned int nbFiles = h . num_files ( ) ;
for ( unsigned int i = 0 ; i < nbFiles ; + + i ) {
QString fileName = h . file_at ( i ) ;
2007-08-29 20:07:37 +04:00
QString extension = fileName . split ( ' . ' ) . last ( ) ;
if ( misc : : isPreviewable ( extension ) )
2007-03-05 16:55:23 +03:00
return true ;
}
return false ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : addTorrentsFromScanFolder ( QStringList & pathList ) {
2009-11-12 21:24:51 +03:00
QString dir_path = FSWatcher - > directories ( ) . first ( ) ;
foreach ( const QString & file , pathList ) {
QString fullPath = dir_path + QDir : : separator ( ) + file ;
2009-09-26 13:06:17 +04:00
try {
torrent_info t ( fullPath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
addTorrent ( fullPath , true ) ;
2009-09-26 13:06:17 +04:00
} catch ( std : : exception & ) {
qDebug ( " Ignoring incomplete torrent file: %s " , fullPath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
}
2007-03-05 16:55:23 +03:00
}
}
2009-11-20 11:01:59 +03:00
QString Bittorrent : : getDefaultSavePath ( ) const {
2009-08-21 11:40:57 +04:00
return defaultSavePath ;
}
2009-11-20 11:01:59 +03:00
bool Bittorrent : : useTemporaryFolder ( ) const {
2009-08-16 07:09:20 +04:00
return ! defaultTempPath . isEmpty ( ) ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : setDefaultTempPath ( QString temppath ) {
2009-03-08 19:26:02 +03:00
if ( defaultTempPath = = temppath )
return ;
if ( temppath . isEmpty ( ) ) {
// Disabling temp dir
// Moving all torrents to their destination folder
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
h . move_storage ( getSavePath ( h . hash ( ) ) ) ;
}
} else {
2009-11-19 11:14:04 +03:00
// Moving all downloading torrents to temporary save path
2009-03-08 19:26:02 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
2009-03-17 20:38:41 +03:00
if ( ! h . is_seed ( ) )
h . move_storage ( temppath ) ;
2009-03-08 19:26:02 +03:00
}
}
defaultTempPath = temppath ;
}
2007-03-05 16:55:23 +03:00
// Enable directory scanning
2009-11-20 11:01:59 +03:00
void Bittorrent : : enableDirectoryScanning ( QString scan_dir ) {
2008-12-26 16:17:08 +03:00
if ( ! scan_dir . isEmpty ( ) ) {
2009-09-30 22:36:47 +04:00
QDir newDir ( scan_dir ) ;
if ( ! newDir . exists ( ) ) {
qDebug ( " Scan dir %s does not exist, create it " , scan_dir . toUtf8 ( ) . data ( ) ) ;
newDir . mkpath ( scan_dir ) ;
}
2008-12-27 15:12:13 +03:00
if ( FSWatcher = = 0 ) {
2009-11-12 21:24:51 +03:00
// Set up folder watching
FSWatcher = new FileSystemWatcher ( scan_dir , this ) ;
connect ( FSWatcher , SIGNAL ( torrentsAdded ( QStringList & ) ) , this , SLOT ( addTorrentsFromScanFolder ( QStringList & ) ) ) ;
2009-08-16 07:09:20 +04:00
} else {
2009-09-30 22:36:47 +04:00
QString old_scan_dir = " " ;
if ( ! FSWatcher - > directories ( ) . empty ( ) )
old_scan_dir = FSWatcher - > directories ( ) . first ( ) ;
2009-11-12 21:24:51 +03:00
if ( QDir ( old_scan_dir ) ! = QDir ( scan_dir ) ) {
2009-09-30 22:36:47 +04:00
if ( ! old_scan_dir . isEmpty ( ) )
FSWatcher - > removePath ( old_scan_dir ) ;
2009-08-16 07:09:20 +04:00
FSWatcher - > addPath ( scan_dir ) ;
}
2008-12-27 15:12:13 +03:00
}
2007-03-05 16:55:23 +03:00
}
}
// Disable directory scanning
2009-11-20 11:01:59 +03:00
void Bittorrent : : disableDirectoryScanning ( ) {
2008-12-26 16:17:08 +03:00
if ( FSWatcher ) {
2009-08-16 07:09:20 +04:00
delete FSWatcher ;
2007-03-05 16:55:23 +03:00
}
}
2009-11-20 11:01:59 +03:00
// Set the ports range in which is chosen the port the Bittorrent
2007-03-05 16:55:23 +03:00
// session will listen to
2009-11-20 11:01:59 +03:00
void Bittorrent : : setListeningPort ( int port ) {
2009-08-25 13:19:02 +04:00
std : : pair < int , int > ports ( port , port ) ;
2007-03-05 16:55:23 +03:00
s - > listen_on ( ports ) ;
}
// Set download rate limit
// -1 to disable
2009-11-20 11:01:59 +03:00
void Bittorrent : : setDownloadRateLimit ( long rate ) {
2007-08-04 10:23:44 +04:00
qDebug ( " Setting a global download rate limit at %ld " , rate ) ;
2007-03-05 16:55:23 +03:00
s - > set_download_rate_limit ( rate ) ;
}
2009-11-20 11:01:59 +03:00
session * Bittorrent : : getSession ( ) const {
2007-07-14 14:50:38 +04:00
return s ;
}
2007-03-05 16:55:23 +03:00
// Set upload rate limit
// -1 to disable
2009-11-20 11:01:59 +03:00
void Bittorrent : : setUploadRateLimit ( long rate ) {
2007-08-08 11:35:57 +04:00
qDebug ( " set upload_limit to %fkb/s " , rate / 1024. ) ;
2007-03-05 16:55:23 +03:00
s - > set_upload_rate_limit ( rate ) ;
}
// libtorrent allow to adjust ratio for each torrent
// This function will apply to same ratio to all torrents
2009-11-20 11:01:59 +03:00
void Bittorrent : : setGlobalRatio ( float ratio ) {
2007-09-09 13:09:24 +04:00
if ( ratio ! = - 1 & & ratio < 1. ) ratio = 1. ;
2007-09-16 18:16:42 +04:00
if ( ratio = = - 1 ) {
// 0 means unlimited for libtorrent
ratio = 0 ;
}
2007-03-05 16:55:23 +03:00
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
2007-07-31 13:17:59 +04:00
unsigned int nbHandles = handles . size ( ) ;
2007-08-20 10:29:18 +04:00
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_ratio ( ratio ) ;
2007-03-05 16:55:23 +03:00
}
}
2007-03-05 20:35:38 +03:00
2007-09-09 13:09:24 +04:00
// Torrents will a ratio superior to the given value will
// be automatically deleted
2009-11-20 11:01:59 +03:00
void Bittorrent : : setDeleteRatio ( float ratio ) {
2007-09-09 13:09:24 +04:00
if ( ratio ! = - 1 & & ratio < 1. ) ratio = 1. ;
2009-07-12 11:23:11 +04:00
if ( ratio_limit = = - 1 & & ratio ! = - 1 ) {
2007-11-25 00:55:19 +03:00
Q_ASSERT ( ! BigRatioTimer ) ;
BigRatioTimer = new QTimer ( this ) ;
connect ( BigRatioTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( deleteBigRatios ( ) ) ) ;
BigRatioTimer - > start ( 5000 ) ;
} else {
2009-07-12 11:23:11 +04:00
if ( ratio_limit ! = - 1 & & ratio = = - 1 ) {
2007-11-25 00:55:19 +03:00
delete BigRatioTimer ;
}
}
2009-07-12 11:23:11 +04:00
if ( ratio_limit ! = ratio ) {
ratio_limit = ratio ;
qDebug ( " * Set deleteRatio to %.1f " , ratio_limit ) ;
2007-11-25 00:55:19 +03:00
deleteBigRatios ( ) ;
}
2007-09-09 13:09:24 +04:00
}
2009-07-14 11:50:00 +04:00
// Set DHT port (>= 1000 or 0 if same as BT)
2009-11-20 11:01:59 +03:00
void Bittorrent : : setDHTPort ( int dht_port ) {
2009-07-14 11:50:00 +04:00
if ( dht_port = = 0 or dht_port > = 1000 ) {
2007-03-05 20:35:38 +03:00
struct dht_settings DHTSettings ;
DHTSettings . service_port = dht_port ;
s - > set_dht_settings ( DHTSettings ) ;
qDebug ( " Set DHT Port to %d " , dht_port ) ;
}
}
// Enable IP Filtering
2009-11-20 11:01:59 +03:00
void Bittorrent : : enableIPFilter ( QString filter ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Enabling IPFiler " ) ;
2008-05-18 00:32:03 +04:00
if ( ! filterParser ) {
filterParser = new FilterParserThread ( this , s ) ;
}
if ( filterPath . isEmpty ( ) | | filterPath ! = filter ) {
filterPath = filter ;
filterParser - > processFilterFile ( filter ) ;
}
2007-03-05 20:35:38 +03:00
}
// Disable IP Filtering
2009-11-20 11:01:59 +03:00
void Bittorrent : : disableIPFilter ( ) {
2007-08-20 10:29:18 +04:00
qDebug ( " Disabling IPFilter " ) ;
2007-03-05 20:35:38 +03:00
s - > set_ip_filter ( ip_filter ( ) ) ;
2008-05-18 00:32:03 +04:00
if ( filterParser ) {
delete filterParser ;
}
filterPath = " " ;
2007-03-05 20:35:38 +03:00
}
2007-05-02 17:52:29 +04:00
// Set BT session settings (user_agent)
2009-11-20 11:01:59 +03:00
void Bittorrent : : setSessionSettings ( session_settings sessionSettings ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Set session settings " ) ;
2007-03-05 20:35:38 +03:00
s - > set_settings ( sessionSettings ) ;
}
2007-05-02 17:52:29 +04:00
// Set Proxy
2009-11-20 11:01:59 +03:00
void Bittorrent : : setProxySettings ( proxy_settings proxySettings , bool trackers , bool peers , bool web_seeds , bool dht ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Set Proxy settings " ) ;
2009-01-04 01:49:21 +03:00
proxy_settings ps_null ;
ps_null . type = proxy_settings : : none ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting trackers proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( trackers )
s - > set_tracker_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_tracker_proxy ( ps_null ) ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting peers proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( peers )
s - > set_peer_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_peer_proxy ( ps_null ) ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting web seeds proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( web_seeds )
s - > set_web_seed_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_web_seed_proxy ( ps_null ) ;
if ( DHTEnabled ) {
2009-01-06 23:38:52 +03:00
qDebug ( " Setting DHT proxy, %d " , dht ) ;
2009-01-04 01:49:21 +03:00
if ( dht )
s - > set_dht_proxy ( proxySettings ) ;
else
s - > set_dht_proxy ( ps_null ) ;
2007-05-14 00:02:30 +04:00
}
2007-05-02 17:52:29 +04:00
}
2009-11-20 11:01:59 +03:00
// Read alerts sent by the Bittorrent session
void Bittorrent : : readAlerts ( ) {
2007-03-05 20:35:38 +03:00
// look at session alerts and display some infos
std : : auto_ptr < alert > a = s - > pop_alert ( ) ;
2007-08-20 10:29:18 +04:00
while ( a . get ( ) ) {
if ( torrent_finished_alert * p = dynamic_cast < torrent_finished_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2009-09-21 23:08:33 +04:00
if ( h . is_valid ( ) ) {
2008-12-27 00:26:06 +03:00
emit finishedTorrent ( h ) ;
2007-08-27 13:24:22 +04:00
QString hash = h . hash ( ) ;
2009-08-16 07:09:20 +04:00
// Remember finished state
TorrentPersistentData : : saveSeedStatus ( h ) ;
2009-03-08 19:26:02 +03:00
// Move to download directory if necessary
if ( ! defaultTempPath . isEmpty ( ) ) {
// Check if directory is different
QDir current_dir ( h . save_path ( ) ) ;
QDir save_dir ( getSavePath ( h . hash ( ) ) ) ;
if ( current_dir ! = save_dir ) {
h . move_storage ( save_dir . path ( ) ) ;
}
}
2009-03-31 02:01:33 +04:00
h . save_resume_data ( ) ;
2009-08-31 12:51:55 +04:00
// Check if there are torrent files inside
for ( int i = 0 ; i < h . get_torrent_info ( ) . num_files ( ) ; + + i ) {
QString torrent_relpath = misc : : toQString ( h . get_torrent_info ( ) . file_at ( i ) . path ) ;
if ( torrent_relpath . endsWith ( " .torrent " ) ) {
addConsoleMessage ( tr ( " Recursive download of file %1 embedded in torrent %2 " , " Recursive download of test.torrent embedded in torrent test2 " ) . arg ( torrent_relpath ) . arg ( h . name ( ) ) ) ;
QString torrent_fullpath = h . save_path ( ) + QDir : : separator ( ) + torrent_relpath ;
boost : : intrusive_ptr < torrent_info > t ;
try {
t = new torrent_info ( torrent_fullpath . toLocal8Bit ( ) . data ( ) ) ;
QString sub_hash = misc : : toQString ( t - > info_hash ( ) ) ;
// Passing the save path along to the sub torrent file
TorrentTempData : : setSavePath ( sub_hash , h . save_path ( ) ) ;
addTorrent ( torrent_fullpath ) ;
} catch ( std : : exception & ) {
qDebug ( " Caught error loading torrent " ) ;
addConsoleMessage ( tr ( " Unable to decode %1 torrent file. " ) . arg ( torrent_fullpath ) , QString : : fromUtf8 ( " red " ) ) ;
}
2009-08-21 15:26:58 +04:00
}
}
2009-07-12 12:13:00 +04:00
qDebug ( " Received finished alert for %s " , h . name ( ) . toLocal8Bit ( ) . data ( ) ) ;
2007-08-27 13:24:22 +04:00
}
2007-03-05 20:35:38 +03:00
}
2008-11-02 00:42:56 +03:00
else if ( save_resume_data_alert * p = dynamic_cast < save_resume_data_alert * > ( a . get ( ) ) ) {
2009-08-16 07:09:20 +04:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QTorrentHandle h ( p - > handle ) ;
2009-09-21 23:08:33 +04:00
if ( h . is_valid ( ) ) {
QString file = h . hash ( ) + " .fastresume " ;
// Delete old fastresume file if necessary
if ( QFile : : exists ( file ) )
QFile : : remove ( file ) ;
qDebug ( " Saving fastresume data in %s " , file . toLocal8Bit ( ) . data ( ) ) ;
if ( p - > resume_data )
{
boost : : filesystem : : ofstream out ( fs : : path ( torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) ) / file . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , * p - > resume_data ) ;
}
2009-08-16 07:09:20 +04:00
}
2008-11-02 00:42:56 +03:00
}
2009-08-17 09:14:03 +04:00
else if ( metadata_received_alert * p = dynamic_cast < metadata_received_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2009-08-17 12:08:51 +04:00
if ( h . is_valid ( ) ) {
2009-08-21 11:40:57 +04:00
qDebug ( " Received metadata for %s " , h . hash ( ) . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 12:08:51 +04:00
emit metadataReceived ( h ) ;
if ( h . is_paused ( ) ) {
// XXX: Unfortunately libtorrent-rasterbar does not send a torrent_paused_alert
// and the torrent can be paused when metadata is received
emit torrentPaused ( h ) ;
}
}
2009-08-17 09:14:03 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( file_error_alert * p = dynamic_cast < file_error_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2009-09-21 23:08:33 +04:00
if ( h . is_valid ( ) ) {
h . auto_managed ( false ) ;
std : : cerr < < " File Error: " < < p - > message ( ) . c_str ( ) < < std : : endl ;
2009-11-17 11:15:26 +03:00
if ( h . is_valid ( ) ) {
2009-09-21 23:08:33 +04:00
emit fullDiskError ( h , misc : : toQString ( p - > message ( ) ) ) ;
2009-11-17 11:15:26 +03:00
h . pause ( ) ;
emit torrentPaused ( h ) ;
}
2009-09-21 23:08:33 +04:00
}
2007-03-05 20:35:38 +03:00
}
2007-08-20 10:29:18 +04:00
else if ( dynamic_cast < listen_failed_alert * > ( a . get ( ) ) ) {
2007-03-05 20:35:38 +03:00
// Level: fatal
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Couldn't listen on any of the given ports. " ) , QString : : fromUtf8 ( " red " ) ) ;
//emit portListeningFailure();
2007-03-05 20:35:38 +03:00
}
2009-09-21 01:25:17 +04:00
/*else if (torrent_paused_alert* p = dynamic_cast<torrent_paused_alert*>(a.get())) {
2009-08-17 12:08:51 +04:00
QTorrentHandle h ( p - > handle ) ;
2009-08-21 11:40:57 +04:00
qDebug ( " Received a torrent_paused_alert for %s " , h . hash ( ) . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 12:08:51 +04:00
if ( h . is_valid ( ) ) {
emit torrentPaused ( h ) ;
}
2009-09-21 01:25:17 +04:00
} */
2008-11-02 00:42:56 +03:00
else if ( tracker_error_alert * p = dynamic_cast < tracker_error_alert * > ( a . get ( ) ) ) {
2007-03-05 20:35:38 +03:00
// Level: fatal
2007-08-20 10:29:18 +04:00
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
// Authentication
2008-05-15 23:48:15 +04:00
if ( p - > status_code ! = 401 ) {
2009-11-19 11:14:04 +03:00
qDebug ( " Received a tracker error for %s: %s " , p - > url . c_str ( ) , p - > msg . c_str ( ) ) ;
2009-11-19 18:04:43 +03:00
QString tracker_url = misc : : toQString ( p - > url ) ;
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
TrackerInfos data = trackers_data . value ( tracker_url , TrackerInfos ( tracker_url ) ) ;
data . last_message = misc : : toQString ( p - > msg ) ;
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
2008-05-15 23:48:15 +04:00
} else {
2007-08-27 13:24:22 +04:00
emit trackerAuthenticationRequired ( h ) ;
}
2007-03-05 20:35:38 +03:00
}
}
2008-05-15 23:48:15 +04:00
else if ( tracker_reply_alert * p = dynamic_cast < tracker_reply_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
2009-11-19 11:36:03 +03:00
qDebug ( " Received a tracker reply from %s " , p - > url . c_str ( ) ) ;
2009-11-19 11:14:04 +03:00
// Connection was successful now. Remove possible old errors
2009-11-19 18:04:43 +03:00
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
QString tracker_url = misc : : toQString ( p - > url ) ;
TrackerInfos data = trackers_data . value ( tracker_url , TrackerInfos ( tracker_url ) ) ;
data . last_message = " " ; // Reset error/warning message
data . num_peers = p - > num_peers ;
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
2009-11-19 11:14:04 +03:00
}
} else if ( tracker_warning_alert * p = dynamic_cast < tracker_warning_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
2009-11-19 18:04:43 +03:00
// Connection was successful now but there is a warning message
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
QString tracker_url = misc : : toQString ( p - > url ) ;
TrackerInfos data = trackers_data . value ( tracker_url , TrackerInfos ( tracker_url ) ) ;
data . last_message = misc : : toQString ( p - > msg ) ; // Store warning message
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
2009-11-19 11:36:03 +03:00
qDebug ( " Received a tracker warning from %s: %s " , p - > url . c_str ( ) , p - > msg . c_str ( ) ) ;
2008-05-15 23:48:15 +04:00
}
2009-11-19 18:43:00 +03:00
} else if ( dht_reply_alert * p = dynamic_cast < dht_reply_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
// Connection was successful now but there is a warning message
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
TrackerInfos data = trackers_data . value ( " dht " , TrackerInfos ( " dht " ) ) ;
data . num_peers = p - > num_peers ;
trackers_data . insert ( " dht " , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
}
2008-05-15 23:48:15 +04:00
}
2007-12-31 19:57:35 +03:00
else if ( portmap_error_alert * p = dynamic_cast < portmap_error_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping failure, message: %1 " ) . arg ( QString ( p - > message ( ) . c_str ( ) ) ) , QColor ( " red " ) ) ;
2008-09-07 15:31:29 +04:00
//emit UPnPError(QString(p->msg().c_str()));
2007-12-31 19:57:35 +03:00
}
else if ( portmap_alert * p = dynamic_cast < portmap_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
qDebug ( " UPnP Success, msg: %s " , p - > message ( ) . c_str ( ) ) ;
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping successful, message: %1 " ) . arg ( QString ( p - > message ( ) . c_str ( ) ) ) , QColor ( " blue " ) ) ;
2008-09-07 15:31:29 +04:00
//emit UPnPSuccess(QString(p->msg().c_str()));
2007-12-31 19:57:35 +03:00
}
2007-08-20 10:29:18 +04:00
else if ( peer_blocked_alert * p = dynamic_cast < peer_blocked_alert * > ( a . get ( ) ) ) {
2008-09-07 15:31:29 +04:00
addPeerBanMessage ( QString ( p - > ip . to_string ( ) . c_str ( ) ) , true ) ;
//emit peerBlocked(QString::fromUtf8(p->ip.to_string().c_str()));
}
else if ( peer_ban_alert * p = dynamic_cast < peer_ban_alert * > ( a . get ( ) ) ) {
addPeerBanMessage ( QString ( p - > ip . address ( ) . to_string ( ) . c_str ( ) ) , false ) ;
//emit peerBlocked(QString::fromUtf8(p->ip.to_string().c_str()));
2007-07-04 10:42:36 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( fastresume_rejected_alert * p = dynamic_cast < fastresume_rejected_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " /! \\ Fast resume failed for %s, reason: %s " , h . name ( ) . toLocal8Bit ( ) . data ( ) , p - > message ( ) . c_str ( ) ) ;
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Fast resume data was rejected for torrent %1, checking again... " ) . arg ( h . name ( ) ) , QString : : fromUtf8 ( " red " ) ) ;
//emit fastResumeDataRejected(h.name());
2007-08-27 13:24:22 +04:00
}
2007-07-24 11:13:40 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( url_seed_alert * p = dynamic_cast < url_seed_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " Url seed lookup failed for url: %1, message: %2 " ) . arg ( QString : : fromUtf8 ( p - > url . c_str ( ) ) ) . arg ( QString : : fromUtf8 ( p - > message ( ) . c_str ( ) ) ) , QString : : fromUtf8 ( " red " ) ) ;
2008-09-07 15:31:29 +04:00
//emit urlSeedProblem(QString::fromUtf8(p->url.c_str()), QString::fromUtf8(p->msg().c_str()));
2007-07-27 13:53:55 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( torrent_checked_alert * p = dynamic_cast < torrent_checked_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
QString hash = h . hash ( ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " %s have just finished checking " , hash . toLocal8Bit ( ) . data ( ) ) ;
2009-08-06 12:58:49 +04:00
// Move to temp directory if necessary
if ( ! h . is_seed ( ) & & ! defaultTempPath . isEmpty ( ) ) {
// Check if directory is different
QDir current_dir ( h . save_path ( ) ) ;
QDir save_dir ( getSavePath ( h . hash ( ) ) ) ;
if ( current_dir = = save_dir ) {
h . move_storage ( defaultTempPath ) ;
}
}
2008-12-27 01:11:40 +03:00
emit torrentFinishedChecking ( h ) ;
2009-08-17 09:23:58 +04:00
emit metadataReceived ( h ) ;
2007-08-17 06:03:13 +04:00
}
}
2007-03-05 20:35:38 +03:00
a = s - > pop_alert ( ) ;
}
}
2009-11-20 11:01:59 +03:00
QHash < QString , TrackerInfos > Bittorrent : : getTrackersInfo ( QString hash ) const {
2009-11-19 18:04:43 +03:00
return trackersInfos . value ( hash , QHash < QString , TrackerInfos > ( ) ) ;
2007-07-27 17:58:12 +04:00
}
2009-11-20 11:01:59 +03:00
int Bittorrent : : getListenPort ( ) const {
2007-03-08 01:36:01 +03:00
return s - > listen_port ( ) ;
}
2009-11-20 11:01:59 +03:00
session_status Bittorrent : : getSessionStatus ( ) const {
2007-03-08 01:36:01 +03:00
return s - > status ( ) ;
}
2009-11-20 11:01:59 +03:00
QString Bittorrent : : getSavePath ( QString hash ) {
2007-03-05 20:35:38 +03:00
QString savePath ;
2009-08-16 07:09:20 +04:00
if ( TorrentTempData : : hasTempData ( hash ) ) {
savePath = TorrentTempData : : getSavePath ( hash ) ;
2009-08-21 11:40:57 +04:00
qDebug ( " getSavePath, got save_path from temp data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
} else {
savePath = TorrentPersistentData : : getSavePath ( hash ) ;
2009-08-21 11:40:57 +04:00
qDebug ( " getSavePath, got save_path from persistent data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
2009-08-16 07:09:20 +04:00
}
if ( savePath . isEmpty ( ) ) {
// use default save path if no other can be found
2009-08-21 11:40:57 +04:00
qDebug ( " Using default save path because none was set: %s " , defaultSavePath . toLocal8Bit ( ) . data ( ) ) ;
2007-03-08 01:36:01 +03:00
savePath = defaultSavePath ;
2007-03-05 20:35:38 +03:00
}
// Checking if savePath Dir exists
// create it if it is not
QDir saveDir ( savePath ) ;
2007-08-20 10:29:18 +04:00
if ( ! saveDir . exists ( ) ) {
if ( ! saveDir . mkpath ( saveDir . path ( ) ) ) {
2009-07-12 12:13:00 +04:00
std : : cerr < < " Couldn't create the save directory: " < < saveDir . path ( ) . toLocal8Bit ( ) . data ( ) < < " \n " ;
2007-03-08 01:36:01 +03:00
// XXX: handle this better
2007-03-05 20:35:38 +03:00
return QDir : : homePath ( ) ;
}
}
return savePath ;
}
2007-03-08 01:36:01 +03:00
// Take an url string to a torrent file,
// download the torrent file to a tmp location, then
// add it to download list
2009-11-20 11:01:59 +03:00
void Bittorrent : : downloadFromUrl ( QString url ) {
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Downloading '%1', please wait... " , " e.g: Downloading 'xxx.torrent', please wait... " ) . arg ( url ) , QPalette : : WindowText ) ;
//emit aboutToDownloadFromUrl(url);
2007-03-08 01:36:01 +03:00
// Launch downloader thread
downloader - > downloadUrl ( url ) ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : downloadFromURLList ( const QStringList & urls ) {
2009-09-07 15:48:31 +04:00
foreach ( const QString & url , urls ) {
downloadFromUrl ( url ) ;
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : addMagnetSkipAddDlg ( QString uri ) {
2009-08-21 18:05:03 +04:00
addMagnetUri ( uri , false ) ;
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : downloadUrlAndSkipDialog ( QString url , QString save_path ) {
2008-09-14 14:14:54 +04:00
//emit aboutToDownloadFromUrl(url);
2009-08-21 11:40:57 +04:00
if ( ! save_path . isEmpty ( ) )
savepath_fromurl [ url ] = save_path ;
2008-09-14 14:14:54 +04:00
url_skippingDlg < < url ;
// Launch downloader thread
downloader - > downloadUrl ( url ) ;
}
2009-11-20 11:01:59 +03:00
// Add to Bittorrent session the downloaded torrent file
void Bittorrent : : processDownloadedFile ( QString url , QString file_path ) {
2008-09-14 14:14:54 +04:00
int index = url_skippingDlg . indexOf ( url ) ;
if ( index < 0 ) {
// Add file to torrent download list
emit newDownloadedTorrent ( file_path , url ) ;
} else {
url_skippingDlg . removeAt ( index ) ;
addTorrent ( file_path , false , url , false ) ;
}
2007-03-08 01:36:01 +03:00
}
// Return current download rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
2009-11-20 11:01:59 +03:00
float Bittorrent : : getPayloadDownloadRate ( ) const {
2007-03-08 01:36:01 +03:00
session_status sessionStatus = s - > status ( ) ;
return sessionStatus . payload_download_rate ;
}
// Return current upload rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
2009-11-20 11:01:59 +03:00
float Bittorrent : : getPayloadUploadRate ( ) const {
2007-03-08 01:36:01 +03:00
session_status sessionStatus = s - > status ( ) ;
return sessionStatus . payload_upload_rate ;
}
// Save DHT entry to hard drive
2009-11-20 11:01:59 +03:00
void Bittorrent : : saveDHTEntry ( ) {
2007-03-08 01:36:01 +03:00
// Save DHT entry
2007-08-20 10:29:18 +04:00
if ( DHTEnabled ) {
2007-03-08 01:36:01 +03:00
try {
entry dht_state = s - > dht_state ( ) ;
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ofstream out ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " dht_state " ) ) . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2007-03-08 01:36:01 +03:00
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , dht_state ) ;
2007-07-29 18:54:39 +04:00
qDebug ( " DHT entry saved " ) ;
2007-08-20 10:29:18 +04:00
} catch ( std : : exception & e ) {
2007-03-08 01:36:01 +03:00
std : : cerr < < e . what ( ) < < " \n " ;
}
}
}
2009-11-20 11:01:59 +03:00
void Bittorrent : : applyEncryptionSettings ( pe_settings se ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Applying encryption settings " ) ;
2007-06-16 00:35:07 +04:00
s - > set_pe_settings ( se ) ;
}
2007-10-10 21:34:52 +04:00
// Will fast resume torrents in
2007-03-08 01:36:01 +03:00
// backup directory
2009-11-20 11:01:59 +03:00
void Bittorrent : : startUpTorrents ( ) {
2007-03-08 01:36:01 +03:00
qDebug ( " Resuming unfinished torrents " ) ;
2009-11-06 17:30:14 +03:00
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
2007-03-08 01:36:01 +03:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2009-01-06 00:19:55 +03:00
QStringList fileNames ;
2009-08-16 07:09:20 +04:00
QStringList known_torrents = TorrentPersistentData : : knownTorrents ( ) ;
2009-11-06 17:30:14 +03:00
if ( known_torrents . empty ( ) & & ! settings . value ( " v1_4_x_torrent_imported " , false ) . toBool ( ) ) {
2009-08-27 07:13:39 +04:00
qDebug ( " No known torrent, importing old torrents " ) ;
importOldTorrents ( ) ;
return ;
}
2009-11-06 17:30:14 +03:00
// Safety measure because some people reported torrent loss since
// we switch the v1.5 way of resuming torrents on startup
QStringList filters ;
filters < < " *.torrent " ;
QStringList torrents_on_hd = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
foreach ( QString hash , torrents_on_hd ) {
hash . chop ( 8 ) ; // remove trailing .torrent
if ( ! known_torrents . contains ( hash ) ) {
std : : cerr < < " ERROR Detected!!! Adding back torrent " < < hash . toLocal8Bit ( ) . data ( ) < < " which got lost for some reason. " < < std : : endl ;
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
}
}
// End of safety measure
2009-08-27 07:13:39 +04:00
qDebug ( " Starting up torrents " ) ;
2009-01-06 00:19:55 +03:00
if ( isQueueingEnabled ( ) ) {
2009-08-17 09:14:03 +04:00
QList < QPair < int , QString > > hashes ;
2009-08-16 07:09:20 +04:00
foreach ( const QString & hash , known_torrents ) {
QString filePath ;
if ( TorrentPersistentData : : isMagnet ( hash ) ) {
filePath = TorrentPersistentData : : getMagnetUri ( hash ) ;
} else {
filePath = torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " ;
2009-01-06 00:19:55 +03:00
}
2009-08-16 07:09:20 +04:00
int prio = TorrentPersistentData : : getPriority ( hash ) ;
2009-08-17 09:14:03 +04:00
misc : : insertSort2 < QString > ( hashes , qMakePair ( prio , hash ) ) ;
2009-01-06 00:19:55 +03:00
}
2009-08-16 07:09:20 +04:00
// Resume downloads
2009-08-17 09:14:03 +04:00
QPair < int , QString > couple ;
foreach ( couple , hashes ) {
QString hash = couple . second ;
2009-08-21 11:40:57 +04:00
qDebug ( " Starting up torrent %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
if ( TorrentPersistentData : : isMagnet ( hash ) ) {
addMagnetUri ( TorrentPersistentData : : getMagnetUri ( hash ) , true ) ;
} else {
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
}
2009-08-16 07:09:20 +04:00
}
} else {
// Resume downloads
2009-08-17 09:14:03 +04:00
foreach ( const QString & hash , known_torrents ) {
2009-08-21 11:40:57 +04:00
qDebug ( " Starting up torrent %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2009-08-17 09:14:03 +04:00
if ( TorrentPersistentData : : isMagnet ( hash ) )
addMagnetUri ( TorrentPersistentData : : getMagnetUri ( hash ) , true ) ;
else
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
2009-08-16 07:09:20 +04:00
}
}
2007-03-08 01:36:01 +03:00
qDebug ( " Unfinished torrents resumed " ) ;
}
2009-08-27 12:43:19 +04:00
// Import torrents temp data from v1.4.0 or earlier: save_path, filtered pieces
// TODO: Remove in qBittorrent v1.6.0
2009-11-20 11:01:59 +03:00
void Bittorrent : : importOldTempData ( QString torrent_path ) {
2009-08-27 12:43:19 +04:00
// Create torrent hash
boost : : intrusive_ptr < torrent_info > t ;
try {
t = new torrent_info ( torrent_path . toLocal8Bit ( ) . data ( ) ) ;
QString hash = misc : : toQString ( t - > info_hash ( ) ) ;
// Load save path
QFile savepath_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .savepath " ) ;
QByteArray line ;
QString savePath ;
if ( savepath_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
line = savepath_file . readAll ( ) ;
savepath_file . close ( ) ;
qDebug ( " -> Save path: %s " , line . data ( ) ) ;
savePath = QString : : fromUtf8 ( line . data ( ) ) ;
qDebug ( " Imported the following save path: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
TorrentTempData : : setSavePath ( hash , savePath ) ;
}
// Load pieces priority
QFile pieces_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .priorities " ) ;
if ( pieces_file . exists ( ) ) {
// Read saved file
if ( pieces_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
QByteArray pieces_priorities = pieces_file . readAll ( ) ;
pieces_file . close ( ) ;
QList < QByteArray > pieces_priorities_list = pieces_priorities . split ( ' \n ' ) ;
std : : vector < int > pp ;
for ( int i = 0 ; i < t - > num_files ( ) ; + + i ) {
int priority = pieces_priorities_list . at ( i ) . toInt ( ) ;
if ( priority < 0 | | priority > 7 ) {
priority = 1 ;
}
2009-11-18 21:07:31 +03:00
//qDebug("Setting piece piority to %d", priority);
2009-08-27 12:43:19 +04:00
pp . push_back ( priority ) ;
}
TorrentTempData : : setFilesPriority ( hash , pp ) ;
qDebug ( " Successfuly imported pieces_priority " ) ;
}
}
// Load sequential
if ( QFile : : exists ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .incremental " ) ) {
qDebug ( " Imported torrent was sequential " ) ;
TorrentTempData : : setSequential ( hash , true ) ;
}
} catch ( std : : exception & ) {
}
}
// Trackers, web seeds, speed limits
// TODO: Remove in qBittorrent v1.6.0
2009-11-20 11:01:59 +03:00
void Bittorrent : : applyFormerAttributeFiles ( QTorrentHandle h ) {
2009-08-27 12:43:19 +04:00
// Load trackers
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QFile tracker_file ( torrentBackup . path ( ) + QDir : : separator ( ) + h . hash ( ) + " .trackers " ) ;
if ( tracker_file . exists ( ) ) {
if ( tracker_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
QStringList lines = QString : : fromUtf8 ( tracker_file . readAll ( ) . data ( ) ) . split ( " \n " ) ;
std : : vector < announce_entry > trackers ;
foreach ( const QString & line , lines ) {
QStringList parts = line . split ( " | " ) ;
if ( parts . size ( ) ! = 2 ) continue ;
announce_entry t ( parts [ 0 ] . toStdString ( ) ) ;
t . tier = parts [ 1 ] . toInt ( ) ;
trackers . push_back ( t ) ;
}
if ( ! trackers . empty ( ) ) {
h . replace_trackers ( trackers ) ;
h . force_reannounce ( ) ;
}
}
}
// Load Web seeds
QFile urlseeds_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + h . hash ( ) + " .urlseeds " ) ;
if ( urlseeds_file . exists ( ) ) {
if ( urlseeds_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
QByteArray urlseeds_lines = urlseeds_file . readAll ( ) ;
urlseeds_file . close ( ) ;
QList < QByteArray > url_seeds = urlseeds_lines . split ( ' \n ' ) ;
// First remove from the torrent the url seeds that were deleted
// in a previous session
QStringList seeds_to_delete ;
QStringList existing_seeds = h . url_seeds ( ) ;
foreach ( const QString & existing_seed , existing_seeds ) {
if ( ! url_seeds . contains ( existing_seed . toLocal8Bit ( ) ) ) {
seeds_to_delete < < existing_seed ;
}
}
foreach ( const QString & existing_seed , seeds_to_delete ) {
h . remove_url_seed ( existing_seed ) ;
}
// Add the ones that were added in a previous session
foreach ( const QByteArray & url_seed , url_seeds ) {
if ( ! url_seed . isEmpty ( ) ) {
// XXX: Should we check if it is already in the list before adding it
// or is libtorrent clever enough to know
h . add_url_seed ( url_seed ) ;
}
}
}
}
2009-08-27 13:07:33 +04:00
// Load speed limits
QFile speeds_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + h . hash ( ) + " .speedLimits " ) ;
if ( speeds_file . exists ( ) ) {
if ( speeds_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
QByteArray speed_limits = speeds_file . readAll ( ) ;
speeds_file . close ( ) ;
QList < QByteArray > speeds = speed_limits . split ( ' ' ) ;
if ( speeds . size ( ) ! = 2 ) {
std : : cerr < < " Invalid .speedLimits file for " < < h . hash ( ) . toStdString ( ) < < ' \n ' ;
return ;
}
h . set_download_limit ( speeds . at ( 0 ) . toInt ( ) ) ;
h . set_upload_limit ( speeds . at ( 1 ) . toInt ( ) ) ;
}
}
2009-08-27 12:43:19 +04:00
}
// Import torrents from v1.4.0 or earlier
// TODO: Remove in qBittorrent v1.6.0
2009-11-20 11:01:59 +03:00
void Bittorrent : : importOldTorrents ( ) {
2009-08-27 12:43:19 +04:00
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
2009-11-06 17:30:14 +03:00
Q_ASSERT ( ! settings . value ( " v1_4_x_torrent_imported " , false ) . toBool ( ) ) ;
// Import old torrent
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QStringList fileNames ;
QStringList filters ;
filters < < " *.torrent " ;
fileNames = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
if ( isQueueingEnabled ( ) ) {
QList < QPair < int , QString > > filePaths ;
foreach ( const QString & fileName , fileNames ) {
QString filePath = torrentBackup . path ( ) + QDir : : separator ( ) + fileName ;
int prio = 99999 ;
// Get priority
QString prioPath = filePath ;
prioPath . replace ( " .torrent " , " .prio " ) ;
if ( QFile : : exists ( prioPath ) ) {
QFile prio_file ( prioPath ) ;
if ( prio_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
bool ok = false ;
prio = prio_file . readAll ( ) . toInt ( & ok ) ;
if ( ! ok )
prio = 99999 ;
prio_file . close ( ) ;
2009-08-27 12:43:19 +04:00
}
}
2009-11-06 17:30:14 +03:00
misc : : insertSort2 < QString > ( filePaths , qMakePair ( prio , filePath ) ) ;
}
// Resume downloads
QPair < int , QString > fileName ;
foreach ( fileName , filePaths ) {
importOldTempData ( fileName . second ) ;
QTorrentHandle h = addTorrent ( fileName . second , false , QString ( ) , true ) ;
// Sequential download
if ( TorrentTempData : : hasTempData ( h . hash ( ) ) ) {
qDebug ( " addTorrent: Setting download as sequential (from tmp data) " ) ;
h . set_sequential_download ( TorrentTempData : : isSequential ( h . hash ( ) ) ) ;
2009-08-27 12:43:19 +04:00
}
2009-11-06 17:30:14 +03:00
applyFormerAttributeFiles ( h ) ;
QString savePath = TorrentTempData : : getSavePath ( h . hash ( ) ) ;
// Save persistent data for new torrent
TorrentPersistentData : : saveTorrentPersistentData ( h ) ;
// Save save_path
if ( ! defaultTempPath . isEmpty ( ) & & ! savePath . isNull ( ) ) {
qDebug ( " addTorrent: Saving save_path in persistent data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
TorrentPersistentData : : saveSavePath ( h . hash ( ) , savePath ) ;
2009-08-27 12:43:19 +04:00
}
2009-11-06 17:30:14 +03:00
}
} else {
QStringList filePaths ;
foreach ( const QString & fileName , fileNames ) {
filePaths . append ( torrentBackup . path ( ) + QDir : : separator ( ) + fileName ) ;
}
// Resume downloads
foreach ( const QString & fileName , filePaths ) {
importOldTempData ( fileName ) ;
QTorrentHandle h = addTorrent ( fileName , false , QString ( ) , true ) ;
// Sequential download
if ( TorrentTempData : : hasTempData ( h . hash ( ) ) ) {
qDebug ( " addTorrent: Setting download as sequential (from tmp data) " ) ;
h . set_sequential_download ( TorrentTempData : : isSequential ( h . hash ( ) ) ) ;
}
applyFormerAttributeFiles ( h ) ;
QString savePath = TorrentTempData : : getSavePath ( h . hash ( ) ) ;
// Save persistent data for new torrent
TorrentPersistentData : : saveTorrentPersistentData ( h ) ;
// Save save_path
if ( ! defaultTempPath . isEmpty ( ) & & ! savePath . isNull ( ) ) {
qDebug ( " addTorrent: Saving save_path in persistent data: %s " , savePath . toLocal8Bit ( ) . data ( ) ) ;
TorrentPersistentData : : saveSavePath ( h . hash ( ) , savePath ) ;
2009-08-27 12:43:19 +04:00
}
}
}
2009-11-06 17:30:14 +03:00
settings . setValue ( " v1_4_x_torrent_imported " , true ) ;
std : : cout < < " Successfully imported torrents from v1.4.x (or previous) instance " < < std : : endl ;
2009-08-27 12:43:19 +04:00
}