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>
2010-05-11 19:53:14 +04:00
# include <QNetworkInterface>
# include <QHostAddress>
# include <QNetworkAddressEntry>
2010-08-20 17:20:23 +04:00
# include <QProcess>
2009-11-28 21:23:17 +03:00
# include <stdlib.h>
2007-03-27 22:49:29 +04:00
2010-08-19 22:47:57 +04:00
# include "smtp.h"
2009-11-12 21:24:51 +03:00
# include "filesystemwatcher.h"
2010-10-09 18:06:35 +04:00
# include "qbtsession.h"
2007-09-29 12:00:14 +04:00
# 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"
2010-02-28 18:15:00 +03:00
# include "scannedfoldersmodel.h"
2010-01-03 01:20:37 +03:00
# ifndef DISABLE_GUI
2010-01-03 02:03:46 +03:00
# include "geoip.h"
2010-01-03 01:20:37 +03:00
# endif
2009-11-20 10:48:44 +03:00
# include "torrentpersistentdata.h"
2009-11-18 20:46:59 +03:00
# include "httpserver.h"
2010-07-16 19:03:18 +04:00
# include "qinisettings.h"
2010-01-24 14:57:15 +03:00
# include "bandwidthscheduler.h"
2008-11-02 22:22:10 +03:00
# include <libtorrent/extensions/ut_metadata.hpp>
2010-06-07 12:32:41 +04:00
# include <libtorrent/version.hpp>
# if LIBTORRENT_VERSION_MINOR > 14
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>
2010-03-04 22:29:48 +03:00
# include <queue>
2007-07-23 16:46:36 +04:00
2010-03-03 20:27:25 +03:00
const int MAX_TRACKER_ERRORS = 2 ;
const float MAX_RATIO = 100. ;
2009-12-30 18:07:51 +03:00
enum ProxyType { HTTP = 1 , SOCKS5 = 2 , HTTP_PW = 3 , SOCKS5_PW = 4 , SOCKS4 = 5 } ;
2010-01-01 23:29:03 +03:00
enum VersionType { NORMAL , ALPHA , BETA , RELEASE_CANDIDATE , DEVEL } ;
2007-03-05 16:55:23 +03:00
// Main constructor
2010-10-17 18:46:01 +04:00
QBtSession : : QBtSession ( )
2010-03-20 14:30:11 +03:00
: m_scanFolders ( ScanFoldersModel : : instance ( this ) ) ,
2010-10-17 15:45:53 +04:00
preAllocateAll ( false ) , addInPause ( false ) , ratio_limit ( - 1 ) ,
UPnPEnabled ( false ) , NATPMPEnabled ( false ) , LSDEnabled ( false ) ,
DHTEnabled ( false ) , current_dht_port ( 0 ) , queueingEnabled ( false ) ,
torrentExport ( false ) , exiting ( false )
# ifndef DISABLE_GUI
2010-03-20 14:30:11 +03:00
, geoipDBLoaded ( false ) , resolve_countries ( false )
2010-10-17 15:45:53 +04:00
# endif
2010-03-03 20:27:25 +03:00
{
2010-10-16 01:55:56 +04:00
m_tracker = 0 ;
2007-04-07 11:43:57 +04:00
// To avoid some exceptions
fs : : path : : default_name_check ( fs : : no_check ) ;
2010-01-02 14:22:44 +03:00
// For backward compatibility
// Move .qBittorrent content to XDG folder
// TODO: Remove after some releases (introduced in v2.1.0)
misc : : moveToXDGFolders ( ) ;
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-12-30 16:10:06 +03:00
QList < int > version ;
version < < VERSION_MAJOR ;
version < < VERSION_MINOR ;
version < < VERSION_BUGFIX ;
2010-01-01 23:29:03 +03:00
version < < VERSION_TYPE ;
2010-07-24 21:35:29 +04:00
const QString peer_id = " qB " ;
2009-12-30 16:10:06 +03:00
// Construct session
2010-03-04 23:19:25 +03:00
s = new session ( fingerprint ( peer_id . toLocal8Bit ( ) . constData ( ) , version . at ( 0 ) , version . at ( 1 ) , version . at ( 2 ) , version . at ( 3 ) ) , 0 ) ;
std : : cout < < " Peer ID: " < < fingerprint ( peer_id . toLocal8Bit ( ) . constData ( ) , version . at ( 0 ) , version . at ( 1 ) , version . at ( 2 ) , version . at ( 3 ) ) . to_string ( ) < < std : : endl ;
addConsoleMessage ( " Peer ID: " + misc : : toQString ( fingerprint ( peer_id . toLocal8Bit ( ) . constData ( ) , version . at ( 0 ) , version . at ( 1 ) , version . at ( 2 ) , version . at ( 3 ) ) . to_string ( ) ) ) ;
2009-09-03 15:29:15 +04:00
2007-03-05 16:55:23 +03:00
// Set severity level of libtorrent session
2010-06-22 22:40:30 +04:00
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 | alert : : progress_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 ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2009-10-24 18:28:00 +04:00
s - > add_extension ( create_lt_trackers_plugin ) ;
2009-11-18 13:29:20 +03:00
# endif
2009-12-17 21:00:21 +03:00
if ( Preferences : : isPeXEnabled ( ) ) {
PeXEnabled = true ;
s - > add_extension ( & create_ut_pex_plugin ) ;
} else {
PeXEnabled = false ;
}
2008-11-02 22:22:10 +03:00
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 ) ;
2010-03-21 00:21:42 +03:00
connect ( & resumeDataTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( saveTempFastResumeData ( ) ) ) ;
resumeDataTimer . start ( 180000 ) ; // 3min
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-12-17 23:28:30 +03:00
appendLabelToSavePath = Preferences : : appendTorrentLabel ( ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2009-12-18 16:36:47 +03:00
appendqBExtension = Preferences : : useIncompleteFilesExtension ( ) ;
# endif
2010-03-08 22:19:17 +03:00
connect ( m_scanFolders , SIGNAL ( torrentsAdded ( QStringList & ) ) , this , SLOT ( addTorrentsFromScanFolder ( QStringList & ) ) ) ;
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
}
2010-10-17 18:46:01 +04:00
session_proxy QBtSession : : asyncDeletion ( ) {
2009-12-01 00:30:14 +03:00
qDebug ( " Bittorrent session async deletion IN " ) ;
exiting = true ;
2008-11-02 16:19:27 +03:00
// Do some BT related saving
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2009-11-18 15:35:55 +03:00
saveDHTEntry ( ) ;
2010-04-06 20:52:17 +04:00
# endif
2008-11-02 16:19:27 +03:00
saveSessionState ( ) ;
2009-11-18 15:35:55 +03:00
saveFastResumeData ( ) ;
2009-11-28 18:03:27 +03:00
// Delete session
session_proxy sp = s - > abort ( ) ;
delete s ;
2009-12-01 00:30:14 +03:00
qDebug ( " Bittorrent session async deletion OUT " ) ;
return sp ;
}
// Main destructor
2010-10-17 18:46:01 +04:00
QBtSession : : ~ QBtSession ( ) {
2009-12-01 00:30:14 +03:00
qDebug ( " BTSession destructor IN " ) ;
if ( ! exiting ) {
// Do some BT related saving
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2009-12-01 00:30:14 +03:00
saveDHTEntry ( ) ;
2010-04-06 20:52:17 +04:00
# endif
2009-12-01 00:30:14 +03:00
saveSessionState ( ) ;
saveFastResumeData ( ) ;
2010-10-02 00:03:27 +04:00
qDebug ( " Deleting the session " ) ;
2009-12-01 00:30:14 +03:00
// Delete session
session_proxy sp = s - > abort ( ) ;
delete s ;
}
2007-07-29 18:54:39 +04:00
// Delete our objects
2010-10-16 01:55:56 +04:00
if ( m_tracker )
delete m_tracker ;
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 ;
2010-01-24 14:57:15 +03:00
if ( bd_scheduler )
delete bd_scheduler ;
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 ;
2009-12-01 00:30:14 +03:00
qDebug ( " BTSession destructor OUT " ) ;
2007-03-05 16:55:23 +03:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : preAllocateAllFiles ( bool b ) {
2010-03-03 20:27:25 +03:00
const bool change = ( preAllocateAll ! = b ) ;
2007-11-03 01:56:07 +03:00
if ( change ) {
qDebug ( " PreAllocateAll changed, reloading all torrents! " ) ;
preAllocateAll = b ;
2007-09-08 21:07:29 +04:00
}
}
2010-10-17 18:46:01 +04:00
ScanFoldersModel * QBtSession : : getScanFoldersModel ( ) const {
2010-03-18 22:25:16 +03:00
return m_scanFolders ;
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : isPexEnabled ( ) const {
2010-06-28 01:07:12 +04:00
return PeXEnabled ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : processBigRatios ( ) {
2010-08-20 18:17:46 +04:00
if ( ratio_limit < 0 ) return ;
2010-06-22 21:46:07 +04:00
qDebug ( " Process big ratios... " ) ;
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
2010-03-12 00:43:09 +03:00
const QTorrentHandle h ( * torrentIT ) ;
2009-08-16 07:09:20 +04:00
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_seed ( ) ) {
2010-03-03 20:27:25 +03:00
const QString hash = h . hash ( ) ;
const float ratio = getRealRatio ( hash ) ;
2010-06-22 21:46:07 +04:00
qDebug ( " Ratio: %f (limit: %f) " , ratio , ratio_limit ) ;
if ( ratio < = MAX_RATIO & & ratio > = ratio_limit ) {
2010-06-22 21:06:11 +04:00
if ( high_ratio_action = = REMOVE_ACTION ) {
2010-06-22 21:46:07 +04:00
addConsoleMessage ( tr ( " %1 reached the maximum ratio you set. " ) . arg ( h . name ( ) ) ) ;
2010-06-22 21:06:11 +04:00
addConsoleMessage ( tr ( " Removing torrent %1... " ) . arg ( h . name ( ) ) ) ;
deleteTorrent ( hash ) ;
} else {
// Pause it
2010-06-22 21:46:07 +04:00
if ( ! h . is_paused ( ) ) {
addConsoleMessage ( tr ( " %1 reached the maximum ratio you set. " ) . arg ( h . name ( ) ) ) ;
addConsoleMessage ( tr ( " Pausing torrent %1... " ) . arg ( h . name ( ) ) ) ;
pauseTorrent ( hash ) ;
}
2010-06-22 21:06:11 +04:00
}
2009-08-16 07:09:20 +04:00
//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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : isQueueingEnabled ( ) const {
2008-07-15 02:01:05 +04:00
return queueingEnabled ;
2008-07-14 23:20:18 +04:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
2010-07-23 18:05:53 +04:00
const QUrl qurl = QUrl : : fromEncoded ( url . toLocal8Bit ( ) ) ;
2010-03-03 20:27:25 +03:00
const int index = url_skippingDlg . indexOf ( qurl ) ;
2009-08-21 11:40:57 +04:00
if ( index > = 0 )
url_skippingDlg . removeAt ( index ) ;
2009-12-06 17:41:05 +03:00
if ( savepath_fromurl . contains ( qurl ) )
savepath_fromurl . remove ( qurl ) ;
2007-07-21 00:18:18 +04:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : startTorrentsInPause ( bool b ) {
2007-09-08 21:07:29 +04:00
addInPause = b ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
2010-10-17 18:46:01 +04:00
void QBtSession : : 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 ) ;
}
2009-12-17 23:28:30 +03:00
setAppendLabelToSavePath ( Preferences : : appendTorrentLabel ( ) ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2009-12-18 16:36:47 +03:00
setAppendqBExtension ( Preferences : : useIncompleteFilesExtension ( ) ) ;
# endif
2009-11-14 22:08:28 +03:00
preAllocateAllFiles ( Preferences : : preAllocateAllFiles ( ) ) ;
startTorrentsInPause ( Preferences : : addTorrentsInPause ( ) ) ;
2010-02-28 18:15:00 +03:00
// * Scan dirs
2010-07-23 18:05:53 +04:00
const QStringList scan_dirs = Preferences : : getScanDirs ( ) ;
2010-06-05 22:59:05 +04:00
QList < bool > downloadInDirList = Preferences : : getDownloadInScanDirs ( ) ;
2010-03-22 21:50:04 +03:00
while ( scan_dirs . size ( ) > downloadInDirList . size ( ) ) {
2010-06-05 22:59:05 +04:00
downloadInDirList < < false ;
2010-02-28 18:15:00 +03:00
}
2010-03-22 21:50:04 +03:00
int i = 0 ;
foreach ( const QString & dir , scan_dirs ) {
2010-06-05 22:59:05 +04:00
m_scanFolders - > addPath ( dir , downloadInDirList . at ( i ) ) ;
2010-03-22 21:50:04 +03:00
+ + i ;
2009-11-14 22:08:28 +03:00
}
2010-01-31 16:09:46 +03:00
// * Export Dir
2010-03-03 20:27:25 +03:00
const bool newTorrentExport = Preferences : : isTorrentExportEnabled ( ) ;
2010-01-31 16:09:46 +03:00
if ( torrentExport ! = newTorrentExport ) {
torrentExport = newTorrentExport ;
if ( torrentExport ) {
qDebug ( " Torrent export is enabled, exporting the current torrents " ) ;
exportTorrentFiles ( Preferences : : getExportDir ( ) ) ;
}
}
2009-11-14 22:08:28 +03:00
// Connection
// * Ports binding
2010-03-03 20:27:25 +03:00
const unsigned short old_listenPort = getListenPort ( ) ;
const unsigned short new_listenPort = Preferences : : getSessionPort ( ) ;
2009-12-29 12:59:32 +03:00
if ( old_listenPort ! = new_listenPort ) {
setListeningPort ( new_listenPort ) ;
2010-03-03 20:27:25 +03:00
addConsoleMessage ( tr ( " qBittorrent is bound to port: TCP/%1 " , " e.g: qBittorrent is bound to port: 6881 " ) . arg ( QString : : number ( new_listenPort ) ) ) ;
2009-11-14 22:08:28 +03:00
}
// * Global download limit
2010-03-03 20:27:25 +03:00
const bool alternative_speeds = Preferences : : isAltBandwidthEnabled ( ) ;
2010-01-23 23:45:14 +03:00
int down_limit ;
if ( alternative_speeds )
down_limit = Preferences : : getAltGlobalDownloadLimit ( ) ;
else
down_limit = Preferences : : getGlobalDownloadLimit ( ) ;
2009-11-14 22:08:28 +03:00
if ( down_limit < = 0 ) {
// Download limit disabled
setDownloadRateLimit ( - 1 ) ;
} else {
// Enabled
setDownloadRateLimit ( down_limit * 1024 ) ;
}
2010-01-23 23:45:14 +03:00
int up_limit ;
if ( alternative_speeds )
up_limit = Preferences : : getAltGlobalUploadLimit ( ) ;
else
up_limit = Preferences : : getGlobalUploadLimit ( ) ;
2009-11-14 22:08:28 +03:00
// * Global Upload limit
if ( up_limit < = 0 ) {
// Upload limit disabled
setUploadRateLimit ( - 1 ) ;
} else {
// Enabled
setUploadRateLimit ( up_limit * 1024 ) ;
}
2010-01-24 14:57:15 +03:00
if ( Preferences : : isSchedulerEnabled ( ) ) {
if ( ! bd_scheduler ) {
bd_scheduler = new BandwidthScheduler ( this ) ;
connect ( bd_scheduler , SIGNAL ( switchToAlternativeMode ( bool ) ) , this , SLOT ( useAlternativeSpeedsLimit ( bool ) ) ) ;
}
bd_scheduler - > start ( ) ;
} else {
if ( bd_scheduler ) delete bd_scheduler ;
}
2010-01-03 01:20:37 +03:00
# ifndef DISABLE_GUI
2009-11-15 13:59:11 +03:00
// Resolve countries
2009-11-15 23:03:23 +03:00
qDebug ( " Loading country resolution settings " ) ;
2010-03-03 20:27:25 +03:00
const bool new_resolv_countries = Preferences : : resolvePeerCountries ( ) ;
2009-11-15 13:59:11 +03:00
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
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2009-11-15 13:59:11 +03:00
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 ) ;
}
}
2010-01-03 01:20:37 +03:00
# endif
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 ;
2010-07-24 21:35:29 +04:00
sessionSettings . user_agent = " qBittorrent " VERSION ;
2009-12-30 16:10:06 +03:00
std : : cout < < " HTTP user agent is " < < sessionSettings . user_agent < < std : : endl ;
addConsoleMessage ( tr ( " HTTP user agent is %1 " ) . arg ( misc : : toQString ( sessionSettings . user_agent ) ) ) ;
2009-11-14 22:08:28 +03:00
sessionSettings . upnp_ignore_nonrouters = true ;
sessionSettings . use_dht_as_fallback = false ;
2009-11-28 13:36:34 +03:00
// To prevent ISPs from blocking seeding
sessionSettings . lazy_bitfields = true ;
2009-11-28 18:03:27 +03:00
// Speed up exit
sessionSettings . stop_tracker_timeout = 1 ;
2009-11-19 18:50:57 +03:00
//sessionSettings.announce_to_all_trackers = true;
2009-12-02 21:19:16 +03:00
sessionSettings . auto_scrape_interval = 1200 ; // 20 minutes
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-04-09 22:24:57 +04:00
sessionSettings . announce_to_all_trackers = true ;
2009-11-19 11:43:18 +03:00
sessionSettings . announce_to_all_tiers = true ; //uTorrent behavior
2009-12-02 21:19:16 +03:00
sessionSettings . auto_scrape_min_interval = 900 ; // 15 minutes
2009-11-22 13:35:16 +03:00
# endif
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 ;
2009-12-17 20:37:54 +03:00
sessionSettings . cache_size = Preferences : : diskCacheSize ( ) * 64 ;
addConsoleMessage ( tr ( " Using a disk cache size of %1 MiB " ) . arg ( Preferences : : diskCacheSize ( ) ) ) ;
2009-11-14 22:08:28 +03:00
// 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 ) ;
}
2010-01-31 18:27:54 +03:00
// Outgoing ports
sessionSettings . outgoing_ports = std : : make_pair ( Preferences : : outgoingPortsMin ( ) , Preferences : : outgoingPortsMax ( ) ) ;
2009-11-14 22:08:28 +03:00
setSessionSettings ( sessionSettings ) ;
2010-01-31 18:42:24 +03:00
// Ignore limits on LAN
sessionSettings . ignore_limits_on_local_network = Preferences : : ignoreLimitsOnLAN ( ) ;
2010-01-31 18:57:07 +03:00
// Include overhead in transfer limits
sessionSettings . rate_limit_ip_overhead = Preferences : : includeOverheadInLimits ( ) ;
2009-11-14 22:08:28 +03:00
// Bittorrent
2010-03-25 22:16:43 +03:00
// * Max Half-open connections
s - > set_max_half_open_connections ( Preferences : : getMaxHalfOpenConnections ( ) ) ;
2009-11-14 22:08:28 +03:00
// * 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 ) ) {
2009-12-29 12:59:32 +03:00
int dht_port ;
if ( Preferences : : isDHTPortSameAsBT ( ) )
2009-12-29 13:04:41 +03:00
dht_port = 0 ;
2009-12-29 12:59:32 +03:00
else
2009-11-14 22:08:28 +03:00
dht_port = Preferences : : getDHTPort ( ) ;
setDHTPort ( dht_port ) ;
2009-12-30 19:47:03 +03:00
if ( dht_port = = 0 ) dht_port = new_listenPort ;
2009-11-14 22:08:28 +03:00
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
2009-12-17 21:00:21 +03:00
if ( PeXEnabled ) {
addConsoleMessage ( tr ( " PeX support [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
addConsoleMessage ( tr ( " PeX support [OFF] " ) , QString : : fromUtf8 ( " red " ) ) ;
}
if ( PeXEnabled ! = Preferences : : isPeXEnabled ( ) ) {
addConsoleMessage ( tr ( " Restart is required to toggle PeX support " ) , QString : : fromUtf8 ( " red " ) ) ;
}
2009-11-14 22:08:28 +03:00
// * 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
2010-03-03 20:27:25 +03:00
const int encryptionState = Preferences : : getEncryptionSetting ( ) ;
2009-11-14 22:08:28 +03:00
// 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 ) ;
// * Maximum ratio
2010-06-22 21:06:11 +04:00
high_ratio_action = Preferences : : getMaxRatioAction ( ) ;
setMaxRatio ( Preferences : : getMaxRatio ( ) ) ;
2009-11-14 22:08:28 +03:00
// 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 ( ) ) {
2010-03-03 20:27:25 +03:00
const quint16 port = Preferences : : getWebUiPort ( ) ;
const QString username = Preferences : : getWebUiUsername ( ) ;
const QString password = Preferences : : getWebUiPassword ( ) ;
2009-11-18 20:46:59 +03:00
initWebUi ( username , password , port ) ;
} else if ( httpServer ) {
delete httpServer ;
}
2009-11-14 22:08:28 +03:00
// * Proxy settings
proxy_settings proxySettings ;
2009-12-30 13:53:58 +03:00
if ( Preferences : : isPeerProxyEnabled ( ) ) {
2009-11-14 22:08:28 +03:00
qDebug ( " Enabling P2P proxy " ) ;
2009-12-30 13:53:58 +03:00
proxySettings . hostname = Preferences : : getPeerProxyIp ( ) . toStdString ( ) ;
2009-11-14 22:08:28 +03:00
qDebug ( " hostname is %s " , proxySettings . hostname . c_str ( ) ) ;
2009-12-30 13:53:58 +03:00
proxySettings . port = Preferences : : getPeerProxyPort ( ) ;
2009-11-14 22:08:28 +03:00
qDebug ( " port is %d " , proxySettings . port ) ;
2009-12-30 13:53:58 +03:00
if ( Preferences : : isPeerProxyAuthEnabled ( ) ) {
proxySettings . username = Preferences : : getPeerProxyUsername ( ) . toStdString ( ) ;
proxySettings . password = Preferences : : getPeerProxyPassword ( ) . toStdString ( ) ;
2009-11-14 22:08:28 +03:00
qDebug ( " username is %s " , proxySettings . username . c_str ( ) ) ;
qDebug ( " password is %s " , proxySettings . password . c_str ( ) ) ;
}
}
2009-12-30 13:53:58 +03:00
switch ( Preferences : : getPeerProxyType ( ) ) {
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 SOCKS4 :
proxySettings . type = proxy_settings : : socks4 ;
case SOCKS5 :
qDebug ( " type: socks5 " ) ;
proxySettings . type = proxy_settings : : socks5 ;
break ;
case SOCKS5_PW :
qDebug ( " type: socks5_pw " ) ;
proxySettings . type = proxy_settings : : socks5_pw ;
break ;
default :
proxySettings . type = proxy_settings : : none ;
}
setPeerProxySettings ( proxySettings ) ;
// HTTP Proxy
proxy_settings http_proxySettings ;
2010-01-08 23:15:08 +03:00
qDebug ( " HTTP Communications proxy type: %d " , Preferences : : getHTTPProxyType ( ) ) ;
2009-12-30 13:53:58 +03:00
switch ( Preferences : : getHTTPProxyType ( ) ) {
case HTTP_PW :
http_proxySettings . type = proxy_settings : : http_pw ;
http_proxySettings . username = Preferences : : getHTTPProxyUsername ( ) . toStdString ( ) ;
http_proxySettings . password = Preferences : : getHTTPProxyPassword ( ) . toStdString ( ) ;
http_proxySettings . hostname = Preferences : : getHTTPProxyIp ( ) . toStdString ( ) ;
http_proxySettings . port = Preferences : : getHTTPProxyPort ( ) ;
break ;
case HTTP :
http_proxySettings . type = proxy_settings : : http ;
http_proxySettings . hostname = Preferences : : getHTTPProxyIp ( ) . toStdString ( ) ;
http_proxySettings . port = Preferences : : getHTTPProxyPort ( ) ;
break ;
2010-01-07 23:35:12 +03:00
case SOCKS5 :
http_proxySettings . type = proxy_settings : : socks5 ;
http_proxySettings . hostname = Preferences : : getHTTPProxyIp ( ) . toStdString ( ) ;
http_proxySettings . port = Preferences : : getHTTPProxyPort ( ) ;
break ;
case SOCKS5_PW :
http_proxySettings . type = proxy_settings : : socks5_pw ;
http_proxySettings . username = Preferences : : getHTTPProxyUsername ( ) . toStdString ( ) ;
http_proxySettings . password = Preferences : : getHTTPProxyPassword ( ) . toStdString ( ) ;
http_proxySettings . hostname = Preferences : : getHTTPProxyIp ( ) . toStdString ( ) ;
http_proxySettings . port = Preferences : : getHTTPProxyPort ( ) ;
break ;
2009-12-30 13:53:58 +03:00
default :
http_proxySettings . type = proxy_settings : : none ;
}
setHTTPProxySettings ( http_proxySettings ) ;
2010-10-16 01:55:56 +04:00
// Tracker
if ( Preferences : : isTrackerEnabled ( ) ) {
if ( ! m_tracker ) {
m_tracker = new QTracker ( this ) ;
}
if ( m_tracker - > start ( ) ) {
addConsoleMessage ( tr ( " Embedded Tracker [ON] " ) , QString : : fromUtf8 ( " blue " ) ) ;
} else {
addConsoleMessage ( tr ( " Failed to start the embedded tracker! " ) , QString : : fromUtf8 ( " red " ) ) ;
}
} else {
addConsoleMessage ( tr ( " Embedded Tracker [OFF] " ) ) ;
if ( m_tracker )
delete m_tracker ;
}
2009-11-14 22:08:28 +03:00
qDebug ( " Session configured " ) ;
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : initWebUi ( QString username , QString password , int port ) {
2009-12-30 12:46:06 +03:00
if ( httpServer ) {
if ( httpServer - > serverPort ( ) ! = port ) {
httpServer - > close ( ) ;
}
} else {
2009-11-18 20:46:59 +03:00
httpServer = new HttpServer ( this , 3000 , this ) ;
2009-12-30 12:46:06 +03:00
}
2009-11-18 20:46:59 +03:00
httpServer - > setAuthorization ( username , password ) ;
2009-12-30 12:46:06 +03:00
bool success = true ;
if ( ! httpServer - > isListening ( ) ) {
success = httpServer - > listen ( QHostAddress : : Any , port ) ;
if ( success )
2009-12-30 12:49:56 +03:00
addConsoleMessage ( tr ( " The Web UI is listening on port %1 " ) . arg ( port ) ) ;
2009-12-30 12:46:06 +03:00
else
2010-01-03 01:20:37 +03:00
addConsoleMessage ( tr ( " Web User Interface Error - Unable to bind Web UI to port %1 " ) . arg ( port ) , " red " ) ;
2009-12-30 12:46:06 +03:00
}
return success ;
2009-11-18 20:46:59 +03:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : useAlternativeSpeedsLimit ( bool alternative ) {
2010-01-24 14:57:15 +03:00
// Save new state to remember it on startup
Preferences : : setAltBandwidthEnabled ( alternative ) ;
// Apply settings to the bittorrent session
if ( alternative ) {
s - > set_download_rate_limit ( Preferences : : getAltGlobalDownloadLimit ( ) * 1024 ) ;
s - > set_upload_rate_limit ( Preferences : : getAltGlobalUploadLimit ( ) * 1024 ) ;
} else {
2010-06-25 01:34:31 +04:00
int down_limit = Preferences : : getGlobalDownloadLimit ( ) ;
if ( down_limit < = 0 ) {
down_limit = - 1 ;
} else {
down_limit * = 1024 ;
}
s - > set_download_rate_limit ( down_limit ) ;
int up_limit = Preferences : : getGlobalUploadLimit ( ) ;
if ( up_limit < = 0 ) {
up_limit = - 1 ;
} else {
up_limit * = 1024 ;
}
s - > set_upload_rate_limit ( up_limit ) ;
2010-01-24 14:57:15 +03:00
}
emit alternativeSpeedsModeChanged ( alternative ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : takeETASamples ( ) {
2009-11-16 23:28:58 +03:00
bool change = false ; ;
foreach ( const QString & hash , ETA_samples . keys ( ) ) {
2010-07-23 18:05:53 +04:00
const QTorrentHandle h = getTorrentHandle ( hash ) ;
2009-11-16 23:28:58 +03:00
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
2010-10-17 18:46:01 +04:00
qlonglong QBtSession : : getETA ( QString hash ) {
2010-07-23 18:05:53 +04:00
const QTorrentHandle h = getTorrentHandle ( hash ) ;
2009-11-16 23:28:58 +03:00
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
2010-03-03 20:27:25 +03:00
const qulonglong bytes_left = h . actual_size ( ) - h . total_wanted_done ( ) ;
2009-11-16 23:28:58 +03:00
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 {
2010-07-23 18:05:53 +04:00
const QList < int > samples = ETA_samples . value ( h . hash ( ) , QList < int > ( ) ) ;
2010-03-03 20:27:25 +03:00
const int nb_samples = samples . size ( ) ;
2009-11-16 23:28:58 +03:00
if ( nb_samples > 3 ) {
long sum_samples = 0 ;
2010-03-03 20:27:25 +03:00
foreach ( const int val , samples ) {
2009-11-16 23:28:58 +03:00
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
}
2007-03-05 16:55:23 +03:00
// Return the torrent handle, given its hash
2010-10-17 18:46:01 +04:00
QTorrentHandle QBtSession : : getTorrentHandle ( QString hash ) const {
2010-03-03 20:27:25 +03:00
return QTorrentHandle ( s - > find_torrent ( misc : : QStringToSha1 ( hash ) ) ) ;
2007-03-05 16:55:23 +03:00
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : hasActiveTorrents ( ) const {
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2009-08-16 07:09:20 +04:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
2010-04-05 23:03:00 +04:00
const QTorrentHandle h ( * 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
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : hasDownloadingTorrents ( ) const {
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2010-08-16 21:35:32 +04:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
2010-08-20 13:02:27 +04:00
if ( torrentIT - > is_valid ( ) ) {
try {
const torrent_status : : state_t state = torrentIT - > status ( ) . state ;
if ( state ! = torrent_status : : finished & & state ! = torrent_status : : seeding )
return true ;
} catch ( std : : exception ) { }
}
2010-08-16 21:35:32 +04:00
}
return false ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
2010-10-17 18:46:01 +04:00
void QBtSession : : deleteTorrent ( QString hash , bool delete_local_files ) {
2010-03-04 23:19:25 +03:00
qDebug ( " Deleting torrent with hash: %s " , qPrintable ( hash ) ) ;
2010-07-23 18:05:53 +04:00
const QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-08-20 10:29:18 +04:00
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 ;
}
2010-07-23 18:05:53 +04:00
const QString fileName ( h . name ( ) ) ;
2007-10-19 20:00:42 +04:00
// Remove it from session
2010-07-18 00:55:40 +04:00
if ( delete_local_files ) {
2010-07-19 22:17:37 +04:00
QDir save_dir ( h . save_path ( ) ) ;
if ( save_dir ! = QDir ( defaultSavePath ) & & ( defaultTempPath . isEmpty ( ) | | save_dir ! = QDir ( defaultTempPath ) ) )
savePathsToRemove [ hash ] = save_dir . absolutePath ( ) ;
2009-08-16 07:09:20 +04:00
s - > remove_torrent ( h . get_torrent_handle ( ) , session : : delete_files ) ;
2010-07-18 00:55:40 +04:00
} else {
2008-11-02 00:42:56 +03:00
s - > remove_torrent ( h . get_torrent_handle ( ) ) ;
2010-07-18 00:55:40 +04:00
}
2007-03-05 16:55:23 +03:00
// Remove it from torrent backup directory
2010-01-02 14:22:44 +03:00
QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
2007-07-31 12:13:38 +04:00
QStringList filters ;
filters < < hash + " .* " ;
2010-07-23 18:05:53 +04:00
const QStringList files = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & file , files ) {
2010-07-26 12:58:12 +04:00
misc : : safeRemove ( torrentBackup . absoluteFilePath ( file ) ) ;
2007-07-31 12:13:38 +04:00
}
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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : pauseAllTorrents ( ) {
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
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 ) ;
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
}
2010-10-17 18:46:01 +04:00
std : : vector < torrent_handle > QBtSession : : getTorrents ( ) const {
2010-08-20 13:02:27 +04:00
return s - > get_torrents ( ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : resumeAllTorrents ( ) {
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
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 ) ;
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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : loadFastResumeData ( QString hash , std : : vector < char > & buf ) {
const QString fastresume_path = QDir ( misc : : BTBackupLocation ( ) ) . absoluteFilePath ( hash + QString ( " .fastresume " ) ) ;
qDebug ( " Trying to load fastresume data: %s " , qPrintable ( fastresume_path ) ) ;
return ( load_file ( qPrintable ( fastresume_path ) , buf ) = = 0 ) ;
}
void QBtSession : : loadTorrentSettings ( QTorrentHandle h ) {
// Connections limit per torrent
h . set_max_connections ( Preferences : : getMaxConnecsPerTorrent ( ) ) ;
// Uploads limit per torrent
h . set_max_uploads ( Preferences : : getMaxUploadsPerTorrent ( ) ) ;
# ifndef DISABLE_GUI
// Resolve countries
h . resolve_countries ( resolve_countries ) ;
# endif
}
QTorrentHandle QBtSession : : addMagnetUri ( QString magnet_uri , bool resumed ) {
2009-08-17 09:14:03 +04:00
QTorrentHandle h ;
2010-07-23 18:05:53 +04:00
const QString hash ( misc : : magnetUriToHash ( magnet_uri ) ) ;
2009-08-17 09:14:03 +04:00
if ( hash . isEmpty ( ) ) {
addConsoleMessage ( tr ( " '%1' is not a valid magnet URI. " ) . arg ( magnet_uri ) ) ;
return h ;
}
2010-04-05 23:03:00 +04:00
const QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
2009-08-17 09:14:03 +04:00
if ( resumed ) {
2010-01-20 00:47:53 +03:00
// Load metadata
2010-10-17 18:46:01 +04:00
const QString torrent_path = torrentBackup . absoluteFilePath ( hash + " .torrent " ) ;
if ( QFile : : exists ( torrent_path ) )
return addTorrent ( torrent_path , false , QString : : null , true ) ;
2009-08-17 09:14:03 +04:00
}
2010-10-17 18:46:01 +04:00
qDebug ( " Adding a magnet URI: %s " , qPrintable ( hash ) ) ;
Q_ASSERT ( magnet_uri . startsWith ( " magnet: " , Qt : : CaseInsensitive ) ) ;
2009-08-17 09:14:03 +04:00
2010-10-17 18:46:01 +04:00
// Check for duplicate torrent
if ( s - > find_torrent ( misc : : toSha1Hash ( hash ) ) . is_valid ( ) ) {
2009-08-17 09:14:03 +04:00
qDebug ( " /! \\ Torrent is already in download list " ) ;
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( magnet_uri ) ) ;
return h ;
}
2010-10-17 18:46:01 +04:00
add_torrent_params p = initializeAddTorrentParams ( hash ) ;
2009-08-17 09:14:03 +04:00
//Getting fast resume data if existing
2010-10-17 18:46:01 +04:00
bool fastResume = false ;
2010-10-17 19:00:31 +04:00
std : : vector < char > buf ; // Needs to stay in the function scope
2009-08-17 09:14:03 +04:00
if ( resumed ) {
2010-10-17 18:46:01 +04:00
if ( loadFastResumeData ( hash , buf ) ) {
2009-08-17 09:14:03 +04:00
fastResume = true ;
p . resume_data = & buf ;
2010-10-17 18:46:01 +04:00
qDebug ( " Successfully loaded fast resume data " ) ;
2009-08-17 09:14:03 +04:00
}
}
2010-10-17 18:46:01 +04:00
// Get save path
2010-04-06 13:17:33 +04:00
QString torrent_name = misc : : magnetUriToName ( magnet_uri ) ;
2010-07-23 18:05:53 +04:00
const QString savePath ( getSavePath ( hash , false , QString : : null , torrent_name ) ) ;
2010-10-17 18:50:38 +04:00
if ( ! defaultTempPath . isEmpty ( ) & & ! TorrentPersistentData : : isSeed ( hash ) & & resumed ) {
2010-01-05 00:16:33 +03:00
qDebug ( " addMagnetURI: Temp folder is enabled. " ) ;
2010-07-08 18:50:23 +04:00
QString torrent_tmp_path = defaultTempPath . replace ( " \\ " , " / " ) ;
2010-09-25 20:01:07 +04:00
p . save_path = torrent_tmp_path . toUtf8 ( ) . constData ( ) ;
2010-07-16 23:53:47 +04:00
// Check if save path exists, creating it otherwise
if ( ! QDir ( torrent_tmp_path ) . exists ( ) )
QDir ( ) . mkpath ( torrent_tmp_path ) ;
qDebug ( " addMagnetURI: using save_path: %s " , qPrintable ( torrent_tmp_path ) ) ;
2010-01-05 00:16:33 +03:00
} else {
2010-09-25 20:01:07 +04:00
p . save_path = savePath . toUtf8 ( ) . constData ( ) ;
2010-07-16 23:53:47 +04:00
// Check if save path exists, creating it otherwise
2010-10-17 18:46:01 +04:00
if ( ! QDir ( savePath ) . exists ( ) ) QDir ( ) . mkpath ( savePath ) ;
2010-03-03 20:27:25 +03:00
qDebug ( " addMagnetURI: using save_path: %s " , qPrintable ( savePath ) ) ;
2009-08-17 09:14:03 +04:00
}
2010-01-05 00:16:33 +03:00
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 ) ;
2010-01-05 00:16:33 +03:00
// If temp path is enabled, move torrent
2010-10-17 18:50:38 +04:00
if ( ! defaultTempPath . isEmpty ( ) & & ! resumed ) {
2010-01-05 00:16:33 +03:00
qDebug ( " Temp folder is enabled, moving new torrent to temp folder " ) ;
h . move_storage ( defaultTempPath ) ;
2010-10-17 18:50:38 +04:00
}
2010-01-05 00:16:33 +03:00
2010-10-17 18:46:01 +04:00
loadTorrentSettings ( h ) ;
2009-08-17 09:14:03 +04:00
// Load filtered files
2009-11-20 21:29:13 +03:00
if ( ! resumed ) {
2010-10-17 18:46:01 +04:00
loadTorrentTempData ( h , savePath , true ) ;
2009-08-17 09:14:03 +04:00
}
2010-01-20 21:53:11 +03:00
if ( ! fastResume & & ( ! addInPause | | ( Preferences : : useAdditionDialog ( ) ) ) ) {
2009-08-17 09:14:03 +04:00
// 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
2010-10-17 18:46:01 +04:00
QTorrentHandle QBtSession : : addTorrent ( QString path , bool fromScanDir , QString from_url , bool resumed ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h ;
2010-10-17 18:46:01 +04:00
// Check if BT_backup directory exists
2010-04-05 23:03:00 +04:00
const QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
2010-10-17 18:46:01 +04:00
if ( ! torrentBackup . exists ( ) ) return h ;
2007-03-05 16:55:23 +03:00
2010-10-17 18:46:01 +04:00
// Fix the input path if necessary
2010-05-30 21:51:40 +04:00
# ifdef Q_WS_WIN
2010-06-04 00:08:19 +04:00
// Windows hack
2010-10-17 18:46:01 +04:00
if ( ! path . endsWith ( " .torrent " ) )
if ( QFile : : rename ( path , path + " .torrent " ) ) path + = " .torrent " ;
2010-05-30 21:51:40 +04:00
# endif
2010-07-25 19:30:26 +04:00
# ifdef Q_WS_WIN
const QString file = path . trimmed ( ) . replace ( " file:/// " , " " , Qt : : CaseInsensitive ) ;
# else
2010-04-05 23:03:00 +04:00
const QString file = path . trimmed ( ) . replace ( " file:// " , " " , Qt : : CaseInsensitive ) ;
2010-07-25 19:30:26 +04:00
# endif
2010-10-17 18:46:01 +04:00
if ( file . isEmpty ( ) ) return h ;
2010-10-17 16:18:34 +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
2010-03-03 20:27:25 +03:00
qDebug ( " Adding %s to download list " , qPrintable ( file ) ) ;
2010-10-17 18:46:01 +04:00
boost : : intrusive_ptr < torrent_info > t ;
2008-11-02 00:42:56 +03:00
try {
2009-08-16 07:09:20 +04:00
// Getting torrent file informations
2010-06-06 17:58:43 +04:00
t = new torrent_info ( file . toUtf8 ( ) . constData ( ) ) ;
2010-01-03 16:00:16 +03:00
if ( ! t - > is_valid ( ) )
throw std : : exception ( ) ;
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);
2010-07-24 23:22:55 +04:00
misc : : safeRemove ( file ) ;
2009-08-16 07:09:20 +04:00
} 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
2010-07-24 23:22:55 +04:00
misc : : safeRemove ( file ) ;
2009-08-16 07:09:20 +04:00
}
return h ;
2008-11-02 00:42:56 +03:00
}
2010-10-17 18:46:01 +04:00
const QString hash = misc : : toQString ( t - > info_hash ( ) ) ;
2009-08-17 09:14:03 +04:00
2010-03-03 20:27:25 +03:00
qDebug ( " -> Hash: %s " , qPrintable ( hash ) ) ;
qDebug ( " -> Name: %s " , t - > name ( ) . c_str ( ) ) ;
2010-10-17 18:46:01 +04:00
// Check for duplicate
2008-11-02 00:42:56 +03:00
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
2010-03-11 19:37:49 +03:00
if ( ! from_url . isNull ( ) ) {
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( from_url ) ) ;
} else {
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( file ) ) ;
}
// Check if the torrent contains trackers or url seeds we don't know about
// and add them
2010-10-17 16:18:34 +04:00
mergeTorrents ( getTorrentHandle ( hash ) , t ) ;
2010-10-17 18:46:01 +04:00
// Delete file if temporary
if ( ! from_url . isNull ( ) | | fromScanDir ) misc : : safeRemove ( file ) ;
2009-08-16 07:09:20 +04:00
return h ;
2008-11-02 00:42:56 +03:00
}
2010-10-17 18:46:01 +04:00
2010-06-23 19:14:38 +04:00
// Check number of files
if ( t - > num_files ( ) < 1 ) {
addConsoleMessage ( tr ( " Error: The torrent %1 does not contain any file. " ) . arg ( misc : : toQStringU ( t - > name ( ) ) ) ) ;
2010-10-17 18:46:01 +04:00
// Delete file if temporary
if ( ! from_url . isNull ( ) | | fromScanDir ) misc : : safeRemove ( file ) ;
2010-06-23 19:14:38 +04:00
return h ;
}
2010-10-17 18:46:01 +04:00
2010-06-23 19:14:38 +04:00
// Actually add the torrent
2010-04-06 13:17:33 +04:00
QString root_folder = misc : : truncateRootFolder ( t ) ;
2010-10-09 12:56:07 +04:00
qDebug ( " Truncated root folder: %s " , qPrintable ( root_folder ) ) ;
2010-10-17 18:46:01 +04:00
add_torrent_params p = initializeAddTorrentParams ( hash ) ;
p . ti = t ;
// Get fast resume data if existing
bool fastResume = false ;
2010-10-17 19:00:31 +04:00
std : : vector < char > buf ; // Needs to stay in the function scope
2009-08-16 07:09:20 +04:00
if ( resumed ) {
2010-10-17 18:46:01 +04:00
if ( loadFastResumeData ( hash , buf ) ) {
2008-11-02 00:42:56 +03:00
fastResume = true ;
p . resume_data = & buf ;
2010-10-17 18:46:01 +04:00
qDebug ( " Successfully loaded fast resume data " ) ;
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 ;
2009-12-06 17:41:05 +03:00
if ( ! from_url . isEmpty ( ) & & savepath_fromurl . contains ( QUrl : : fromEncoded ( from_url . toLocal8Bit ( ) ) ) ) {
2009-08-21 11:40:57 +04:00
// Enforcing the save path defined before URL download (from RSS for example)
2009-12-06 17:41:05 +03:00
savePath = savepath_fromurl . take ( QUrl : : fromEncoded ( from_url . toLocal8Bit ( ) ) ) ;
2009-08-21 11:40:57 +04:00
} else {
2010-04-06 13:17:33 +04:00
savePath = getSavePath ( hash , fromScanDir , path , root_folder ) ;
2009-08-21 11:40:57 +04:00
}
2010-10-17 18:50:38 +04:00
if ( ! defaultTempPath . isEmpty ( ) & & ! TorrentPersistentData : : isSeed ( hash ) & & resumed ) {
2010-01-05 00:16:33 +03:00
qDebug ( " addTorrent::Temp folder is enabled. " ) ;
2010-07-08 18:50:23 +04:00
QString torrent_tmp_path = defaultTempPath . replace ( " \\ " , " / " ) ;
if ( ! root_folder . isEmpty ( ) ) {
if ( ! torrent_tmp_path . endsWith ( " / " ) ) torrent_tmp_path + = " / " ;
torrent_tmp_path + = root_folder ;
}
2010-09-25 20:01:07 +04:00
p . save_path = torrent_tmp_path . toUtf8 ( ) . constData ( ) ;
2010-07-16 23:53:47 +04:00
// Check if save path exists, creating it otherwise
2010-10-17 18:46:01 +04:00
if ( ! QDir ( torrent_tmp_path ) . exists ( ) ) QDir ( ) . mkpath ( torrent_tmp_path ) ;
2010-07-16 23:53:47 +04:00
qDebug ( " addTorrent: using save_path: %s " , qPrintable ( torrent_tmp_path ) ) ;
2010-01-05 00:16:33 +03:00
} else {
2010-09-25 20:01:07 +04:00
p . save_path = savePath . toUtf8 ( ) . constData ( ) ;
2010-07-16 23:53:47 +04:00
// Check if save path exists, creating it otherwise
2010-10-17 18:46:01 +04:00
if ( ! QDir ( savePath ) . exists ( ) ) QDir ( ) . mkpath ( savePath ) ;
2010-03-03 20:27:25 +03:00
qDebug ( " addTorrent: using save_path: %s " , qPrintable ( savePath ) ) ;
2009-03-08 19:26:02 +03:00
}
2009-11-06 17:30:14 +03:00
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
qDebug ( " /! \\ Error: Invalid handle " ) ;
2010-07-24 23:22:55 +04:00
if ( ! from_url . isNull ( ) ) misc : : safeRemove ( file ) ;
2009-08-16 07:09:20 +04:00
return h ;
2008-11-02 00:42:56 +03:00
}
2010-07-16 23:53:47 +04:00
// Remember root folder
TorrentPersistentData : : setRootFolder ( hash , root_folder ) ;
2009-12-08 20:20:28 +03:00
2010-01-05 00:16:33 +03:00
// If temp path is enabled, move torrent
2010-10-17 18:50:38 +04:00
if ( ! defaultTempPath . isEmpty ( ) & & ! resumed ) {
2010-01-05 00:16:33 +03:00
qDebug ( " Temp folder is enabled, moving new torrent to temp folder " ) ;
2010-07-16 23:53:47 +04:00
QString torrent_tmp_path = defaultTempPath . replace ( " \\ " , " / " ) ;
if ( ! root_folder . isEmpty ( ) ) {
if ( ! torrent_tmp_path . endsWith ( " / " ) ) torrent_tmp_path + = " / " ;
torrent_tmp_path + = root_folder ;
}
h . move_storage ( torrent_tmp_path ) ;
2010-10-17 18:50:38 +04:00
}
2010-01-05 00:16:33 +03:00
2010-10-17 18:46:01 +04:00
loadTorrentSettings ( h ) ;
2009-11-20 21:29:13 +03:00
if ( ! resumed ) {
2010-10-17 18:46:01 +04:00
loadTorrentTempData ( h , savePath , false ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2009-12-18 16:36:47 +03:00
// Append .!qB to incomplete files
if ( appendqBExtension )
appendqBextensionToTorrent ( h , true ) ;
# endif
2010-01-31 16:09:46 +03:00
// Backup torrent file
2010-07-23 18:05:53 +04:00
const QString newFile = torrentBackup . absoluteFilePath ( hash + " .torrent " ) ;
2010-10-17 18:46:01 +04:00
if ( file ! = newFile )
2010-01-31 16:09:46 +03:00
QFile : : copy ( file , newFile ) ;
// Copy the torrent file to the export folder
2010-10-17 18:46:01 +04:00
if ( torrentExport )
exportTorrentFile ( h ) ;
2008-11-02 00:42:56 +03:00
}
2010-10-17 18:46:01 +04:00
2010-01-20 21:53:11 +03:00
if ( ! fastResume & & ( ! addInPause | | ( Preferences : : useAdditionDialog ( ) & & ! fromScanDir ) ) ) {
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
}
2010-10-17 18:46:01 +04:00
// If temporary file, remove it
if ( ! from_url . isNull ( ) | | fromScanDir ) misc : : safeRemove ( file ) ;
// Display console message
2008-11-02 00:42:56 +03:00
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
}
2010-10-17 18:46:01 +04:00
// Send torrent addition signal
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
}
2010-10-17 18:46:01 +04:00
void QBtSession : : exportTorrentFile ( QTorrentHandle h ) {
Q_ASSERT ( torrentExport ) ;
QString torrent_path = QDir ( misc : : BTBackupLocation ( ) ) . absoluteFilePath ( h . hash ( ) + " .torrent " ) ;
QDir exportPath ( Preferences : : getExportDir ( ) ) ;
if ( exportPath . exists ( ) | | exportPath . mkpath ( exportPath . absolutePath ( ) ) ) {
QString new_torrent_path = exportPath . absoluteFilePath ( h . name ( ) + " .torrent " ) ;
if ( QFile : : exists ( new_torrent_path ) & & misc : : sameFiles ( torrent_path , new_torrent_path ) ) {
// Append hash to torrent name to make it unique
new_torrent_path = exportPath . absoluteFilePath ( h . name ( ) + " - " + h . hash ( ) + " .torrent " ) ;
}
QFile : : copy ( torrent_path , new_torrent_path ) ;
//h.save_torrent_file(torrent_path);
}
}
add_torrent_params QBtSession : : initializeAddTorrentParams ( QString hash ) {
add_torrent_params p ;
// Seeding mode
# if LIBTORRENT_VERSION_MINOR > 14
// Skip checking and directly start seeding (new in libtorrent v0.15)
if ( TorrentTempData : : isSeedingMode ( hash ) )
p . seed_mode = true ;
else
p . seed_mode = false ;
# endif
// Preallocation mode
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
p . auto_managed = false ; // Because it is added in paused state
return p ;
}
void QBtSession : : loadTorrentTempData ( QTorrentHandle h , QString savePath , bool magnet ) {
const QString hash = h . hash ( ) ;
// Sequential download
if ( ! TorrentTempData : : hasTempData ( hash ) ) return ;
// sequential download
h . set_sequential_download ( TorrentTempData : : isSequential ( hash ) ) ;
// The following is useless for newly added magnet
if ( ! magnet ) {
// Files priorities
vector < int > fp ;
TorrentTempData : : getFilesPriority ( hash , fp ) ;
h . prioritize_files ( fp ) ;
// Update file names
const QStringList files_path = TorrentTempData : : getFilesPath ( hash ) ;
bool force_recheck = false ;
if ( files_path . size ( ) = = h . num_files ( ) ) {
for ( int i = 0 ; i < h . num_files ( ) ; + + i ) {
QString old_path = h . files_path ( ) . at ( i ) ;
old_path = old_path . replace ( " \\ " , " / " ) ;
if ( ! QFile : : exists ( old_path ) ) {
// Remove old parent folder manually since we will
// not get a file_renamed alert
QStringList parts = old_path . split ( " / " , QString : : SkipEmptyParts ) ;
parts . removeLast ( ) ;
if ( ! parts . empty ( ) )
QDir ( ) . rmpath ( parts . join ( " / " ) ) ;
}
const QString & path = files_path . at ( i ) ;
if ( ! force_recheck & & QDir ( h . save_path ( ) ) . exists ( path ) )
force_recheck = true ;
qDebug ( " Renaming file to %s " , qPrintable ( path ) ) ;
h . rename_file ( i , path ) ;
}
// Force recheck
if ( force_recheck ) h . force_recheck ( ) ;
}
}
// Save persistent data for new torrent
if ( defaultTempPath . isEmpty ( ) )
TorrentPersistentData : : saveTorrentPersistentData ( h , QString : : null , magnet ) ;
else
TorrentPersistentData : : saveTorrentPersistentData ( h , savePath , magnet ) ;
}
void QBtSession : : mergeTorrents ( QTorrentHandle h_ex , boost : : intrusive_ptr < torrent_info > t ) {
2010-10-17 16:18:34 +04:00
// Check if the torrent contains trackers or url seeds we don't know about
// and add them
if ( ! h_ex . is_valid ( ) ) return ;
2010-10-17 18:46:01 +04:00
std : : vector < announce_entry > old_trackers = h_ex . trackers ( ) ;
std : : vector < announce_entry > new_trackers = t - > trackers ( ) ;
bool trackers_added = false ;
for ( std : : vector < announce_entry > : : iterator it = new_trackers . begin ( ) ; it ! = new_trackers . end ( ) ; it + + ) {
std : : string tracker_url = it - > url ;
bool found = false ;
for ( std : : vector < announce_entry > : : iterator itold = old_trackers . begin ( ) ; itold ! = old_trackers . end ( ) ; itold + + ) {
if ( tracker_url = = itold - > url ) {
found = true ;
break ;
2010-10-17 16:18:34 +04:00
}
}
2010-10-17 18:46:01 +04:00
if ( found ) {
trackers_added = true ;
announce_entry entry ( tracker_url ) ;
h_ex . add_tracker ( entry ) ;
2010-10-17 16:18:34 +04:00
}
2010-10-17 18:46:01 +04:00
}
if ( trackers_added ) {
addConsoleMessage ( tr ( " Note: new trackers were added to the existing torrent. " ) ) ;
}
bool urlseeds_added = false ;
const QStringList old_urlseeds = h_ex . url_seeds ( ) ;
std : : vector < std : : string > new_urlseeds = t - > url_seeds ( ) ;
for ( std : : vector < std : : string > : : iterator it = new_urlseeds . begin ( ) ; it ! = new_urlseeds . end ( ) ; it + + ) {
const QString new_url = misc : : toQString ( it - > c_str ( ) ) ;
if ( ! old_urlseeds . contains ( new_url ) ) {
urlseeds_added = true ;
h_ex . add_url_seed ( new_url ) ;
2010-10-17 16:18:34 +04:00
}
2010-10-17 18:46:01 +04:00
}
if ( urlseeds_added ) {
addConsoleMessage ( tr ( " Note: new URL seeds were added to the existing torrent. " ) ) ;
}
2010-10-17 16:18:34 +04:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : exportTorrentFiles ( QString path ) {
2010-01-31 16:09:46 +03:00
Q_ASSERT ( torrentExport ) ;
QDir exportDir ( path ) ;
if ( ! exportDir . exists ( ) ) {
if ( ! exportDir . mkpath ( exportDir . absolutePath ( ) ) ) {
2010-03-03 20:27:25 +03:00
std : : cerr < < " Error: Could not create torrent export directory: " < < qPrintable ( exportDir . absolutePath ( ) ) < < std : : endl ;
2010-01-31 16:09:46 +03:00
return ;
}
}
QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
std : : vector < torrent_handle > : : iterator itr ;
for ( itr = handles . begin ( ) ; itr ! = handles . end ( ) ; itr + + ) {
2010-03-12 00:43:09 +03:00
const QTorrentHandle h ( * itr ) ;
2010-01-31 16:09:46 +03:00
if ( ! h . is_valid ( ) ) {
std : : cerr < < " Torrent Export: torrent is invalid, skipping... " < < std : : endl ;
continue ;
}
2010-07-23 18:05:53 +04:00
const QString src_path ( torrentBackup . absoluteFilePath ( h . hash ( ) + " .torrent " ) ) ;
2010-01-31 16:09:46 +03:00
if ( QFile : : exists ( src_path ) ) {
QString dst_path = exportDir . absoluteFilePath ( h . name ( ) + " .torrent " ) ;
if ( QFile : : exists ( dst_path ) ) {
if ( ! misc : : sameFiles ( src_path , dst_path ) ) {
dst_path = exportDir . absoluteFilePath ( h . name ( ) + " - " + h . hash ( ) + " .torrent " ) ;
} else {
qDebug ( " Torrent Export: Destination file exists, skipping... " ) ;
continue ;
}
}
2010-03-03 20:27:25 +03:00
qDebug ( " Export Torrent: %s -> %s " , qPrintable ( src_path ) , qPrintable ( dst_path ) ) ;
2010-01-31 16:09:46 +03:00
QFile : : copy ( src_path , dst_path ) ;
} else {
2010-03-03 20:27:25 +03:00
std : : cerr < < " Error: could not export torrent " < < qPrintable ( h . hash ( ) ) < < " , maybe it has not metadata yet. " < < std : : endl ;
2010-01-31 16:09:46 +03:00
}
}
}
2007-08-20 10:29:18 +04:00
// Set the maximum number of opened connections
2010-10-17 18:46:01 +04:00
void QBtSession : : setMaxConnections ( int maxConnec ) {
2007-08-20 10:29:18 +04:00
s - > set_max_connections ( maxConnec ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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 ( ) ;
2010-07-18 16:53:16 +04:00
std : : vector < torrent_handle > : : const_iterator it ;
for ( it = handles . begin ( ) ; it ! = handles . end ( ) ; it + + ) {
if ( ! it - > is_valid ( ) )
2007-09-09 11:44:22 +04:00
continue ;
2010-07-18 16:53:16 +04:00
try {
it - > set_max_connections ( max ) ;
} catch ( std : : exception ) { }
2007-09-09 11:44:22 +04:00
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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 ( ) ;
2010-07-18 16:53:16 +04:00
std : : vector < torrent_handle > : : const_iterator it ;
for ( it = handles . begin ( ) ; it ! = handles . end ( ) ; it + + ) {
if ( ! it - > is_valid ( ) )
2007-09-09 11:44:22 +04:00
continue ;
2010-07-18 16:53:16 +04:00
try {
it - > set_max_uploads ( max ) ;
} catch ( std : : exception ) { }
2007-09-09 11:44:22 +04:00
}
}
2007-03-05 16:55:23 +03:00
// Return DHT state
2010-10-17 18:46:01 +04:00
bool QBtSession : : isDHTEnabled ( ) const {
2007-03-05 16:55:23 +03:00
return DHTEnabled ;
}
2010-10-17 18:46:01 +04:00
bool QBtSession : : isLSDEnabled ( ) const {
2009-11-21 18:18:09 +03:00
return LSDEnabled ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : 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
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : loadSessionState ( ) {
2010-04-05 23:32:44 +04:00
const QString state_path = misc : : cacheLocation ( ) + QDir : : separator ( ) + QString : : fromUtf8 ( " ses_state " ) ;
2010-06-05 17:43:36 +04:00
if ( ! QFile : : exists ( state_path ) ) return ;
2010-06-14 13:55:35 +04:00
if ( QFile ( state_path ) . size ( ) = = 0 ) {
2010-07-18 16:53:16 +04:00
// Remove empty invalid state file
2010-07-24 23:22:55 +04:00
misc : : safeRemove ( state_path ) ;
2010-06-14 13:55:35 +04:00
return ;
}
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-04-06 20:52:17 +04:00
std : : vector < char > in ;
if ( load_file ( state_path . toLocal8Bit ( ) . constData ( ) , in ) = = 0 )
{
lazy_entry e ;
if ( lazy_bdecode ( & in [ 0 ] , & in [ 0 ] + in . size ( ) , e ) = = 0 )
s - > load_state ( e ) ;
}
# else
2010-03-07 00:11:47 +03:00
boost : : filesystem : : ifstream ses_state_file ( state_path . toLocal8Bit ( ) . constData ( )
2009-08-16 07:09:20 +04:00
, std : : ios_base : : binary ) ;
ses_state_file . unsetf ( std : : ios_base : : skipws ) ;
s - > load_state ( bdecode (
2010-10-17 15:45:53 +04:00
std : : istream_iterator < char > ( ses_state_file )
, std : : istream_iterator < char > ( ) ) ) ;
2010-04-06 20:52:17 +04:00
# endif
2008-11-02 16:19:27 +03:00
}
2010-10-17 18:46:01 +04:00
void QBtSession : : saveSessionState ( ) {
2009-08-16 07:09:20 +04:00
qDebug ( " Saving session state to disk... " ) ;
2010-04-05 23:32:44 +04:00
const QString state_path = misc : : cacheLocation ( ) + QDir : : separator ( ) + QString : : fromUtf8 ( " ses_state " ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-04-06 20:52:17 +04:00
entry session_state ;
s - > save_state ( session_state ) ;
std : : vector < char > out ;
bencode ( std : : back_inserter ( out ) , session_state ) ;
file f ;
error_code ec ;
2010-06-14 13:55:35 +04:00
if ( ! f . open ( state_path . toLocal8Bit ( ) . constData ( ) , file : : write_only , ec ) ) return ;
if ( ec ) return ;
file : : iovec_t b = { & out [ 0 ] , out . size ( ) } ;
f . writev ( 0 , & b , 1 , ec ) ;
2010-04-06 20:52:17 +04:00
# else
entry session_state = s - > state ( ) ;
2010-03-07 00:11:47 +03:00
boost : : filesystem : : ofstream out ( state_path . toLocal8Bit ( ) . constData ( )
2009-08-16 07:09:20 +04:00
, std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , session_state ) ;
2010-04-06 20:52:17 +04:00
# endif
2008-11-02 16:19:27 +03:00
}
2007-09-08 21:07:29 +04:00
// Enable DHT
2010-10-17 18:46:01 +04:00
bool QBtSession : : enableDHT ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
if ( ! DHTEnabled ) {
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2007-09-08 21:07:29 +04:00
entry dht_state ;
2010-04-05 23:03:00 +04:00
const QString dht_state_path = misc : : cacheLocation ( ) + QDir : : separator ( ) + QString : : fromUtf8 ( " dht_state " ) ;
2008-12-15 20:28:05 +03:00
if ( QFile : : exists ( dht_state_path ) ) {
2010-03-04 23:19:25 +03:00
boost : : filesystem : : ifstream dht_state_file ( dht_state_path . toLocal8Bit ( ) . constData ( ) , 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
}
2010-04-06 20:52:17 +04:00
# endif
2007-10-28 00:53:09 +04:00
try {
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-04-06 20:52:17 +04:00
s - > start_dht ( ) ;
# else
2009-08-16 07:09:20 +04:00
s - > start_dht ( dht_state ) ;
2010-04-06 20:52:17 +04:00
# endif
2009-08-16 07:09:20 +04:00
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 ) ) ;
2010-07-18 16:53:16 +04:00
s - > add_dht_router ( std : : make_pair ( std : : string ( " dht.transmissionbt.com " ) , 6881 ) ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " dht.aelitis.com " ) , 6881 ) ) ; // Vuze
2009-08-16 07:09:20 +04:00
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
}
2010-10-17 18:46:01 +04:00
float QBtSession : : getRealRatio ( QString hash ) const {
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2010-03-03 20:27:25 +03:00
if ( ! h . is_valid ( ) ) {
return 0. ;
}
2010-02-17 11:10:57 +03:00
Q_ASSERT ( h . total_done ( ) > = 0 ) ;
2008-11-02 01:07:14 +03:00
Q_ASSERT ( h . all_time_upload ( ) > = 0 ) ;
2010-02-17 11:10:57 +03:00
if ( h . total_done ( ) = = 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
}
2010-02-17 11:10:57 +03:00
float ratio = ( float ) h . all_time_upload ( ) / ( float ) h . total_done ( ) ;
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 ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : saveTempFastResumeData ( ) {
2010-03-21 00:21:42 +03:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
2010-07-18 16:53:16 +04:00
try {
2010-08-21 13:08:12 +04:00
if ( ! h . is_valid ( ) | | ! h . has_metadata ( ) /*|| h.is_seed() || h.is_paused()*/ ) continue ;
2010-07-18 16:53:16 +04:00
if ( h . state ( ) = = torrent_status : : checking_files | | h . state ( ) = = torrent_status : : queued_for_checking ) continue ;
qDebug ( " Saving fastresume data for %s " , qPrintable ( h . name ( ) ) ) ;
h . save_resume_data ( ) ;
} catch ( std : : exception e ) { }
2010-03-21 00:21:42 +03:00
}
}
2007-11-23 00:50:22 +03:00
// Only save fast resume data for unfinished and unpaused torrents (Optimization)
// Called periodically and on exit
2010-10-17 18:46:01 +04:00
void QBtSession : : saveFastResumeData ( ) {
2010-10-02 00:03:27 +04:00
qDebug ( " Saving fast resume data... " ) ;
2008-11-02 02:58:53 +03:00
// Stop listening for alerts
2010-03-21 00:21:42 +03:00
resumeDataTimer . stop ( ) ;
2008-11-02 02:58:53 +03:00
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 "
2010-10-17 15:45:53 +04:00
" torrents to save resume data for " < < std : : endl ;
2009-08-16 07:09:20 +04:00
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 ( ) ;
2009-11-28 16:50:16 +03:00
try {
// Remove torrent from session
s - > remove_torrent ( rda - > handle ) ;
} catch ( libtorrent : : libtorrent_exception ) { }
2009-08-16 07:09:20 +04:00
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 ;
2010-01-02 14:22:44 +03:00
QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
2010-03-12 00:43:09 +03:00
const QTorrentHandle h ( rd - > handle ) ;
2009-11-26 14:59:40 +03:00
if ( ! h . is_valid ( ) ) continue ;
2009-08-16 07:09:20 +04:00
// Remove old fastresume file if it exists
2010-07-21 17:06:09 +04:00
const QString file = torrentBackup . absoluteFilePath ( h . hash ( ) + " .fastresume " ) ;
if ( QFile : : exists ( file ) )
2010-07-24 23:22:55 +04:00
misc : : safeRemove ( file ) ;
2010-07-21 17:06:09 +04:00
boost : : filesystem : : ofstream out ( boost : : filesystem : : path ( file . toLocal8Bit ( ) . constData ( ) ) , std : : ios_base : : binary ) ;
2009-08-16 07:09:20 +04:00
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
}
2010-10-17 18:46:01 +04:00
QStringList QBtSession : : getConsoleMessages ( ) const {
2008-09-07 15:31:29 +04:00
return consoleMessages ;
}
2010-10-17 18:46:01 +04:00
QStringList QBtSession : : getPeerBanMessages ( ) const {
2008-09-07 15:31:29 +04:00
return peerBanMessages ;
}
2010-01-03 01:20:37 +03:00
# ifdef DISABLE_GUI
2010-10-17 18:46:01 +04:00
void QBtSession : : addConsoleMessage ( QString msg , QString ) {
2010-01-03 01:20:37 +03:00
# else
2010-10-17 18:46:01 +04:00
void QBtSession : : addConsoleMessage ( QString msg , QColor color ) {
2010-10-17 15:45:53 +04:00
if ( consoleMessages . size ( ) > 100 ) {
consoleMessages . removeFirst ( ) ;
}
2010-06-22 23:17:47 +04:00
# if defined(Q_WS_WIN) || defined(Q_OS_OS2)
2010-10-17 15:45:53 +04:00
msg = msg . replace ( " / " , " \\ " ) ;
2010-06-04 01:08:28 +04:00
# endif
2010-10-17 15:45:53 +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> " ) ) ;
2010-01-03 01:20:37 +03:00
# endif
2010-10-17 15:45:53 +04:00
emit newConsoleMessage ( QDateTime : : currentDateTime ( ) . toString ( " dd/MM/yyyy hh:mm:ss " ) + " - " + msg ) ;
}
2008-09-07 15:31:29 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : addPeerBanMessage ( QString ip , bool from_ipfilter ) {
2010-10-17 15:45:53 +04:00
if ( peerBanMessages . size ( ) > 100 ) {
peerBanMessages . removeFirst ( ) ;
2008-09-07 15:31:29 +04:00
}
2010-10-17 15:45:53 +04:00
if ( from_ipfilter )
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 ) ) ;
else
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
2010-10-17 18:46:01 +04:00
bool QBtSession : : isFilePreviewPossible ( QString hash ) const {
2010-10-17 15:45:53 +04:00
// See if there are supported files in the torrent
const QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) | | ! h . has_metadata ( ) ) {
2007-03-05 20:35:38 +03:00
return false ;
2007-03-05 16:55:23 +03:00
}
2010-10-17 15:45:53 +04:00
const unsigned int nbFiles = h . num_files ( ) ;
for ( unsigned int i = 0 ; i < nbFiles ; + + i ) {
QString extension = h . file_at ( i ) . split ( ' . ' ) . last ( ) ;
if ( misc : : isPreviewable ( extension ) )
return true ;
}
return false ;
}
2007-03-05 16:55:23 +03:00
2010-10-17 18:46:01 +04:00
void QBtSession : : addTorrentsFromScanFolder ( QStringList & pathList ) {
2010-10-17 15:45:53 +04:00
foreach ( const QString & file , pathList ) {
qDebug ( " File %s added " , qPrintable ( file ) ) ;
try {
torrent_info t ( file . toUtf8 ( ) . constData ( ) ) ;
if ( t . is_valid ( ) )
addTorrent ( file , true ) ;
} catch ( std : : exception & ) {
qDebug ( " Ignoring incomplete torrent file: %s " , qPrintable ( file ) ) ;
2009-08-16 07:09:20 +04:00
}
2007-03-05 16:55:23 +03:00
}
2010-10-17 15:45:53 +04:00
}
2007-03-05 16:55:23 +03:00
2010-10-17 18:46:01 +04:00
QString QBtSession : : getDefaultSavePath ( ) const {
2010-10-17 15:45:53 +04:00
return defaultSavePath ;
}
2009-08-21 11:40:57 +04:00
2010-10-17 18:46:01 +04:00
bool QBtSession : : useTemporaryFolder ( ) const {
2010-10-17 15:45:53 +04:00
return ! defaultTempPath . isEmpty ( ) ;
}
2009-08-16 07:09:20 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : setDefaultTempPath ( QString temppath ) {
2010-10-17 15:45:53 +04:00
if ( defaultTempPath = = temppath )
return ;
if ( temppath . isEmpty ( ) ) {
// Disabling temp dir
// Moving all torrents to their destination folder
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
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 {
qDebug ( " Enabling default temp path... " ) ;
// Moving all downloading torrents to temporary save path
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
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 root_folder = TorrentPersistentData : : getRootFolder ( h . hash ( ) ) ;
QString torrent_tmp_path = temppath . replace ( " \\ " , " / " ) ;
if ( ! root_folder . isEmpty ( ) ) {
if ( ! torrent_tmp_path . endsWith ( " / " ) ) torrent_tmp_path + = " / " ;
torrent_tmp_path + = root_folder ;
2010-07-18 01:12:13 +04:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " Moving torrent to its temp save path: %s " , qPrintable ( torrent_tmp_path ) ) ;
h . move_storage ( torrent_tmp_path ) ;
2010-01-03 02:03:46 +03:00
}
2009-03-08 19:26:02 +03:00
}
}
2010-10-17 15:45:53 +04:00
defaultTempPath = temppath ;
}
2009-03-08 19:26:02 +03:00
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 18:46:01 +04:00
void QBtSession : : appendqBextensionToTorrent ( QTorrentHandle & h , bool append ) {
2010-10-17 15:45:53 +04:00
if ( ! h . is_valid ( ) | | ! h . has_metadata ( ) ) return ;
std : : vector < size_type > fp ;
h . file_progress ( fp ) ;
for ( int i = 0 ; i < h . num_files ( ) ; + + i ) {
if ( append ) {
const qulonglong file_size = h . filesize_at ( i ) ;
if ( file_size > 0 & & ( fp [ i ] / ( double ) file_size ) < 1. ) {
const QString name = misc : : toQStringU ( h . get_torrent_info ( ) . file_at ( i ) . path . string ( ) ) ;
if ( ! name . endsWith ( " .!qB " ) ) {
const QString new_name = name + " .!qB " ;
qDebug ( " Renaming %s to %s " , qPrintable ( name ) , qPrintable ( new_name ) ) ;
h . rename_file ( i , new_name ) ;
2009-12-18 16:36:47 +03:00
}
}
2010-10-17 15:45:53 +04:00
} else {
QString name = misc : : toQStringU ( h . get_torrent_info ( ) . file_at ( i ) . path . string ( ) ) ;
if ( name . endsWith ( " .!qB " ) ) {
const QString old_name = name ;
name . chop ( 4 ) ;
qDebug ( " Renaming %s to %s " , qPrintable ( old_name ) , qPrintable ( name ) ) ;
h . rename_file ( i , name ) ;
}
2009-12-18 16:36:47 +03:00
}
}
2010-10-17 15:45:53 +04:00
}
2009-12-18 16:36:47 +03:00
# endif
2010-10-17 18:46:01 +04:00
void QBtSession : : changeLabelInTorrentSavePath ( QTorrentHandle & h , QString old_label , QString new_label ) {
2010-10-17 15:45:53 +04:00
if ( ! h . is_valid ( ) ) return ;
if ( ! appendLabelToSavePath ) return ;
QString old_save_path = TorrentPersistentData : : getSavePath ( h . hash ( ) ) ;
if ( ! old_save_path . startsWith ( defaultSavePath ) ) return ;
QString new_save_path = misc : : updateLabelInSavePath ( defaultSavePath , old_save_path , old_label , new_label ) ;
if ( new_save_path ! = old_save_path ) {
// Move storage
qDebug ( " Moving storage to %s " , qPrintable ( new_save_path ) ) ;
QDir ( ) . mkpath ( new_save_path ) ;
h . move_storage ( new_save_path ) ;
2009-12-17 23:28:30 +03:00
}
2010-10-17 15:45:53 +04:00
}
2009-12-17 23:28:30 +03:00
2010-10-17 18:46:01 +04:00
void QBtSession : : appendLabelToTorrentSavePath ( QTorrentHandle & h ) {
2010-10-17 15:45:53 +04:00
if ( ! h . is_valid ( ) ) return ;
const QString label = TorrentPersistentData : : getLabel ( h . hash ( ) ) ;
if ( label . isEmpty ( ) ) return ;
// Current save path
QString old_save_path = TorrentPersistentData : : getSavePath ( h . hash ( ) ) ;
QString new_save_path = misc : : updateLabelInSavePath ( defaultSavePath , old_save_path , " " , label ) ;
if ( old_save_path ! = new_save_path ) {
// Move storage
QDir ( ) . mkpath ( new_save_path ) ;
h . move_storage ( new_save_path ) ;
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : setAppendLabelToSavePath ( bool append ) {
2010-10-17 15:45:53 +04:00
if ( appendLabelToSavePath ! = append ) {
appendLabelToSavePath = ! appendLabelToSavePath ;
if ( appendLabelToSavePath ) {
// Move torrents storage to sub folder with label name
2010-08-20 13:02:27 +04:00
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2010-01-03 02:03:46 +03:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
2010-10-17 15:45:53 +04:00
appendLabelToTorrentSavePath ( h ) ;
2010-01-03 02:03:46 +03:00
}
2009-12-18 16:36:47 +03:00
}
}
2010-10-17 15:45:53 +04:00
}
2009-12-18 16:36:47 +03:00
2010-10-17 15:45:53 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 18:46:01 +04:00
void QBtSession : : setAppendqBExtension ( bool append ) {
2010-10-17 15:45:53 +04:00
if ( appendqBExtension ! = append ) {
appendqBExtension = ! appendqBExtension ;
// append or remove .!qB extension for incomplete files
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
appendqBextensionToTorrent ( h , appendqBExtension ) ;
2010-05-11 19:53:14 +04:00
}
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
}
# endif
2007-03-05 16:55:23 +03:00
2010-10-17 15:45:53 +04:00
// Set the ports range in which is chosen the port the Bittorrent
// session will listen to
2010-10-17 18:46:01 +04:00
void QBtSession : : setListeningPort ( int port ) {
2010-10-17 15:45:53 +04:00
std : : pair < int , int > ports ( port , port ) ;
const QString & iface_name = Preferences : : getNetworkInterface ( ) ;
if ( iface_name . isEmpty ( ) ) {
s - > listen_on ( ports ) ;
return ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
QNetworkInterface network_iface = QNetworkInterface : : interfaceFromName ( iface_name ) ;
if ( ! network_iface . isValid ( ) ) {
s - > listen_on ( ports ) ;
return ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
QString ip = " 127.0.0.1 " ;
if ( ! network_iface . addressEntries ( ) . isEmpty ( ) ) {
ip = network_iface . addressEntries ( ) . first ( ) . ip ( ) . toString ( ) ;
2007-09-16 18:16:42 +04:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " Listening on interface %s with ip %s " , qPrintable ( iface_name ) , qPrintable ( ip ) ) ;
s - > listen_on ( ports , ip . toLocal8Bit ( ) . constData ( ) ) ;
}
2010-01-03 02:03:46 +03:00
2010-10-17 15:45:53 +04:00
// Set download rate limit
// -1 to disable
2010-10-17 18:46:01 +04:00
void QBtSession : : setDownloadRateLimit ( long rate ) {
2010-10-17 15:45:53 +04:00
Q_ASSERT ( rate = = - 1 | | rate > = 0 ) ;
qDebug ( " Setting a global download rate limit at %ld " , rate ) ;
s - > set_download_rate_limit ( rate ) ;
}
2007-09-09 13:09:24 +04:00
2010-10-17 18:46:01 +04:00
session * QBtSession : : getSession ( ) const {
2010-10-17 15:45:53 +04:00
return s ;
}
2007-03-05 20:35:38 +03:00
2010-10-17 15:45:53 +04:00
// Set upload rate limit
// -1 to disable
2010-10-17 18:46:01 +04:00
void QBtSession : : setUploadRateLimit ( long rate ) {
2010-10-17 15:45:53 +04:00
Q_ASSERT ( rate = = - 1 | | rate > = 0 ) ;
s - > set_upload_rate_limit ( rate ) ;
}
// Torrents will a ratio superior to the given value will
// be automatically deleted
2010-10-17 18:46:01 +04:00
void QBtSession : : setMaxRatio ( float ratio ) {
2010-10-17 15:45:53 +04:00
if ( ratio < 0 ) ratio = - 1. ;
if ( ratio_limit = = - 1 & & ratio ! = - 1 ) {
Q_ASSERT ( ! BigRatioTimer ) ;
BigRatioTimer = new QTimer ( this ) ;
connect ( BigRatioTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( processBigRatios ( ) ) ) ;
BigRatioTimer - > start ( 5000 ) ;
} else {
if ( ratio_limit ! = - 1 & & ratio = = - 1 ) {
delete BigRatioTimer ;
2010-01-03 02:03:46 +03:00
}
2008-05-18 00:32:03 +04:00
}
2010-10-17 15:45:53 +04:00
if ( ratio_limit ! = ratio ) {
ratio_limit = ratio ;
qDebug ( " * Set deleteRatio to %.1f " , ratio_limit ) ;
processBigRatios ( ) ;
}
}
2007-03-05 20:35:38 +03:00
2010-10-17 15:45:53 +04:00
// Set DHT port (>= 1 or 0 if same as BT)
2010-10-17 18:46:01 +04:00
void QBtSession : : setDHTPort ( int dht_port ) {
2010-10-17 15:45:53 +04:00
if ( dht_port > = 0 ) {
if ( dht_port = = current_dht_port ) return ;
struct dht_settings DHTSettings ;
DHTSettings . service_port = dht_port ;
s - > set_dht_settings ( DHTSettings ) ;
current_dht_port = dht_port ;
qDebug ( " Set DHT Port to %d " , dht_port ) ;
2008-05-18 00:32:03 +04:00
}
2010-10-17 15:45:53 +04:00
}
2007-03-05 20:35:38 +03:00
2010-10-17 15:45:53 +04:00
// Enable IP Filtering
2010-10-17 18:46:01 +04:00
void QBtSession : : enableIPFilter ( QString filter ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Enabling IPFiler " ) ;
if ( ! filterParser ) {
filterParser = new FilterParserThread ( this , s ) ;
}
if ( filterPath . isEmpty ( ) | | filterPath ! = filter ) {
filterPath = filter ;
filterParser - > processFilterFile ( filter ) ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
}
2007-03-05 20:35:38 +03:00
2010-10-17 15:45:53 +04:00
// Disable IP Filtering
2010-10-17 18:46:01 +04:00
void QBtSession : : disableIPFilter ( ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Disabling IPFilter " ) ;
s - > set_ip_filter ( ip_filter ( ) ) ;
if ( filterParser ) {
delete filterParser ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
filterPath = " " ;
}
2009-12-30 13:53:58 +03:00
2010-10-17 15:45:53 +04:00
// Set BT session settings (user_agent)
2010-10-17 18:46:01 +04:00
void QBtSession : : setSessionSettings ( const session_settings & sessionSettings ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Set session settings " ) ;
s - > set_settings ( sessionSettings ) ;
}
// Set Proxy
2010-10-17 18:46:01 +04:00
void QBtSession : : setPeerProxySettings ( const proxy_settings & proxySettings ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Set Peer Proxy settings " ) ;
s - > set_peer_proxy ( proxySettings ) ;
s - > set_dht_proxy ( proxySettings ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : setHTTPProxySettings ( const proxy_settings & proxySettings ) {
2010-10-17 15:45:53 +04:00
s - > set_tracker_proxy ( proxySettings ) ;
s - > set_web_seed_proxy ( proxySettings ) ;
QString proxy_str ;
switch ( proxySettings . type ) {
case proxy_settings : : http_pw :
proxy_str = " http:// " + misc : : toQString ( proxySettings . username ) + " : " + misc : : toQString ( proxySettings . password ) + " @ " + misc : : toQString ( proxySettings . hostname ) + " : " + QString : : number ( proxySettings . port ) ;
break ;
case proxy_settings : : http :
proxy_str = " http:// " + misc : : toQString ( proxySettings . hostname ) + " : " + QString : : number ( proxySettings . port ) ;
break ;
case proxy_settings : : socks5 :
proxy_str = misc : : toQString ( proxySettings . hostname ) + " : " + QString : : number ( proxySettings . port ) ;
break ;
case proxy_settings : : socks5_pw :
proxy_str = misc : : toQString ( proxySettings . username ) + " : " + misc : : toQString ( proxySettings . password ) + " @ " + misc : : toQString ( proxySettings . hostname ) + " : " + QString : : number ( proxySettings . port ) ;
break ;
default :
qDebug ( " Disabling HTTP communications proxy " ) ;
2009-12-30 13:53:58 +03:00
# ifdef Q_WS_WIN
2010-10-17 15:45:53 +04:00
putenv ( " http_proxy= " ) ;
putenv ( " sock_proxy= " ) ;
2009-12-30 13:53:58 +03:00
# else
2010-10-17 15:45:53 +04:00
unsetenv ( " http_proxy " ) ;
unsetenv ( " sock_proxy " ) ;
2009-12-30 13:53:58 +03:00
# endif
2010-10-17 15:45:53 +04:00
return ;
}
// We need this for urllib in search engine plugins
2009-12-30 13:53:58 +03:00
# ifdef Q_WS_WIN
2010-10-17 15:45:53 +04:00
QString type_str ;
if ( proxySettings . type = = proxy_settings : : socks5 | | proxySettings . type = = proxy_settings : : socks5_pw )
type_str = " sock_proxy " ;
else
type_str = " http_proxy " ;
QString tmp = type_str + " = " + proxy_str ;
putenv ( tmp . toLocal8Bit ( ) . constData ( ) ) ;
2009-12-30 13:53:58 +03:00
# else
2010-10-17 15:45:53 +04:00
qDebug ( " HTTP communications proxy string: %s " , qPrintable ( proxy_str ) ) ;
if ( proxySettings . type = = proxy_settings : : socks5 | | proxySettings . type = = proxy_settings : : socks5_pw )
setenv ( " sock_proxy " , proxy_str . toLocal8Bit ( ) . constData ( ) , 1 ) ;
else
setenv ( " http_proxy " , proxy_str . toLocal8Bit ( ) . constData ( ) , 1 ) ;
2009-12-30 13:53:58 +03:00
# endif
2010-10-17 15:45:53 +04:00
}
2007-05-02 17:52:29 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : recursiveTorrentDownload ( const QTorrentHandle & h ) {
2010-10-17 15:45:53 +04:00
torrent_info : : file_iterator it ;
for ( it = h . get_torrent_info ( ) . begin_files ( ) ; it ! = h . get_torrent_info ( ) . end_files ( ) ; it + + ) {
const QString torrent_relpath = misc : : toQStringU ( it - > path . string ( ) ) ;
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 ( ) ) ) ;
const QString torrent_fullpath = h . save_path ( ) + QDir : : separator ( ) + torrent_relpath ;
try {
boost : : intrusive_ptr < torrent_info > t = new torrent_info ( torrent_fullpath . toUtf8 ( ) . constData ( ) ) ;
const 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 " ) ) ;
2010-03-20 14:30:11 +03:00
}
}
}
2010-10-17 15:45:53 +04:00
}
2010-08-20 17:20:23 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : cleanUpAutoRunProcess ( int ) {
2010-10-17 15:45:53 +04:00
sender ( ) - > deleteLater ( ) ;
}
2010-08-20 17:20:23 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : autoRunExternalProgram ( QTorrentHandle h , bool async ) {
2010-10-17 15:45:53 +04:00
if ( ! h . is_valid ( ) ) return ;
QString program = Preferences : : getAutoRunProgram ( ) . trimmed ( ) ;
if ( program . isEmpty ( ) ) return ;
// Replace %f by torrent path
QString torrent_path ;
if ( h . num_files ( ) = = 1 )
torrent_path = h . firstFileSavePath ( ) ;
else
torrent_path = h . save_path ( ) ;
program . replace ( " %f " , torrent_path ) ;
QProcess * process = new QProcess ;
if ( async ) {
connect ( process , SIGNAL ( finished ( int ) ) , this , SLOT ( cleanUpAutoRunProcess ( int ) ) ) ;
process - > start ( program ) ;
} else {
process - > execute ( program ) ;
delete process ;
2010-08-20 17:20:23 +04:00
}
2010-10-17 15:45:53 +04:00
}
2010-08-20 17:20:23 +04:00
2010-10-17 18:46:01 +04:00
void QBtSession : : sendNotificationEmail ( QTorrentHandle h ) {
2010-10-17 15:45:53 +04:00
// Prepare mail content
QString content = tr ( " Torrent name: %1 " ) . arg ( h . name ( ) ) + " \n " ;
content + = tr ( " Torrent size: %1 " ) . arg ( misc : : friendlyUnit ( h . actual_size ( ) ) ) + " \n " ;
content + = tr ( " Save path: %1 " ) . arg ( TorrentPersistentData : : getSavePath ( h . hash ( ) ) ) + " \n \n " ;
content + = tr ( " The torrent was downloaded in %1. " , " The torrent was downloaded in 1 hour and 20 seconds " ) . arg ( misc : : userFriendlyDuration ( h . active_time ( ) ) ) + " \n \n \n " ;
content + = tr ( " Thank you for using qBittorrent. " ) + " \n " ;
// Send the notification email
new Smtp ( " notification@qbittorrent.org " , Preferences : : getMailNotificationEmail ( ) , tr ( " [qBittorrent] %1 has finished downloading " ) . arg ( h . name ( ) ) , content ) ;
}
2010-03-20 14:30:11 +03:00
2010-10-17 15:45:53 +04:00
// Read alerts sent by the Bittorrent session
2010-10-17 18:46:01 +04:00
void QBtSession : : readAlerts ( ) {
2010-10-17 15:45:53 +04:00
// look at session alerts and display some infos
std : : auto_ptr < alert > a = s - > pop_alert ( ) ;
while ( a . get ( ) ) {
if ( torrent_finished_alert * p = dynamic_cast < torrent_finished_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
const QString hash = h . hash ( ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 15:45:53 +04:00
// Remove .!qB extension if necessary
if ( appendqBExtension )
appendqBextensionToTorrent ( h , false ) ;
2010-01-26 11:02:15 +03:00
# endif
2010-10-17 15:45:53 +04:00
const bool was_already_seeded = TorrentPersistentData : : isSeed ( hash ) ;
if ( ! was_already_seeded ) {
h . save_resume_data ( ) ;
qDebug ( " Checking if the torrent contains torrent files to download " ) ;
// Check if there are torrent files inside
for ( torrent_info : : file_iterator it = h . get_torrent_info ( ) . begin_files ( ) ; it ! = h . get_torrent_info ( ) . end_files ( ) ; it + + ) {
qDebug ( " File path: %s " , it - > path . string ( ) . c_str ( ) ) ;
const QString torrent_relpath = misc : : toQStringU ( it - > path . string ( ) ) . replace ( " \\ " , " / " ) ;
if ( torrent_relpath . endsWith ( " .torrent " , Qt : : CaseInsensitive ) ) {
qDebug ( " Found possible recursive torrent download. " ) ;
const QString torrent_fullpath = h . save_path ( ) + " / " + torrent_relpath ;
qDebug ( " Full subtorrent path is %s " , qPrintable ( torrent_fullpath ) ) ;
try {
boost : : intrusive_ptr < torrent_info > t = new torrent_info ( torrent_fullpath . toUtf8 ( ) . constData ( ) ) ;
if ( t - > is_valid ( ) ) {
qDebug ( " emitting recursiveTorrentDownloadPossible() " ) ;
emit recursiveTorrentDownloadPossible ( h ) ;
break ;
2010-02-04 22:16:04 +03:00
}
2010-10-17 15:45:53 +04:00
} catch ( std : : exception & ) {
qDebug ( " Caught error loading torrent " ) ;
addConsoleMessage ( tr ( " Unable to decode %1 torrent file. " ) . arg ( torrent_fullpath ) , QString : : fromUtf8 ( " red " ) ) ;
2010-01-03 02:03:46 +03:00
}
2009-08-31 12:51:55 +04:00
}
2010-10-17 15:45:53 +04:00
}
// Move to download directory if necessary
if ( ! defaultTempPath . isEmpty ( ) ) {
// Check if directory is different
const QDir current_dir ( h . save_path ( ) ) ;
const QDir save_dir ( getSavePath ( hash ) ) ;
if ( current_dir ! = save_dir ) {
h . move_storage ( save_dir . absolutePath ( ) ) ;
2010-10-02 16:52:06 +04:00
}
2010-10-17 15:45:53 +04:00
}
// Remember finished state
TorrentPersistentData : : saveSeedStatus ( h ) ;
// Recheck if the user asked to
if ( Preferences : : recheckTorrentsOnCompletion ( ) ) {
h . force_recheck ( ) ;
}
emit finishedTorrent ( h ) ;
qDebug ( " Received finished alert for %s " , qPrintable ( h . name ( ) ) ) ;
bool will_shutdown = ( Preferences : : shutdownWhenDownloadsComplete ( ) | | Preferences : : shutdownqBTWhenDownloadsComplete ( ) )
& & ! hasDownloadingTorrents ( ) ;
// AutoRun program
if ( Preferences : : isAutoRunEnabled ( ) )
autoRunExternalProgram ( h , will_shutdown ) ;
// Mail notification
if ( Preferences : : isMailNotificationEnabled ( ) )
sendNotificationEmail ( h ) ;
// Auto-Shutdown
if ( will_shutdown ) {
if ( Preferences : : shutdownWhenDownloadsComplete ( ) ) {
qDebug ( " Preparing for auto-shutdown because all downloads are complete! " ) ;
2010-08-16 21:35:32 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2010-10-17 15:45:53 +04:00
saveDHTEntry ( ) ;
2010-08-16 21:35:32 +04:00
# endif
2010-10-17 15:45:53 +04:00
qDebug ( " Saving session state " ) ;
saveSessionState ( ) ;
qDebug ( " Saving fast resume data " ) ;
saveFastResumeData ( ) ;
qDebug ( " Sending computer shutdown signal " ) ;
misc : : shutdownComputer ( ) ;
2010-08-16 21:35:32 +04:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " Exiting the application " ) ;
qApp - > exit ( ) ;
return ;
2010-08-16 21:35:32 +04:00
}
2009-08-21 15:26:58 +04:00
}
2007-08-27 13:24:22 +04:00
}
2010-10-17 15:45:53 +04:00
}
else if ( save_resume_data_alert * p = dynamic_cast < save_resume_data_alert * > ( a . get ( ) ) ) {
const QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
const QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
const QString file = torrentBackup . absoluteFilePath ( h . hash ( ) + " .fastresume " ) ;
// Delete old fastresume file if necessary
if ( QFile : : exists ( file ) )
misc : : safeRemove ( file ) ;
qDebug ( " Saving fastresume data in %s " , qPrintable ( file ) ) ;
if ( p - > resume_data ) {
boost : : filesystem : : ofstream out ( boost : : filesystem : : path ( file . toLocal8Bit ( ) . constData ( ) ) , std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , * p - > resume_data ) ;
2009-09-21 23:08:33 +04:00
}
2009-08-16 07:09:20 +04:00
}
2010-10-17 15:45:53 +04:00
}
else if ( file_renamed_alert * p = dynamic_cast < file_renamed_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
if ( h . num_files ( ) > 1 ) {
// Check if folders were renamed
QStringList old_path_parts = misc : : toQStringU ( h . get_torrent_info ( ) . orig_files ( ) . at ( p - > index ) . path . string ( ) ) . split ( " / " ) ;
old_path_parts . removeLast ( ) ;
QString old_path = old_path_parts . join ( " / " ) ;
QStringList new_path_parts = misc : : toQStringU ( p - > name ) . split ( " / " ) ;
new_path_parts . removeLast ( ) ;
if ( ! new_path_parts . isEmpty ( ) & & old_path ! = new_path_parts . join ( " / " ) ) {
qDebug ( " Old_path(%s) != new_path(%s) " , qPrintable ( old_path ) , qPrintable ( new_path_parts . join ( " / " ) ) ) ;
old_path = h . save_path ( ) + " / " + old_path ;
qDebug ( " Detected folder renaming, attempt to delete old folder: %s " , qPrintable ( old_path ) ) ;
QDir ( ) . rmpath ( old_path ) ;
2010-06-04 00:08:19 +04:00
}
2010-10-17 15:45:53 +04:00
} else {
// Single-file torrent
// Renaming a file corresponds to changing the save path
emit savePathChanged ( h ) ;
2010-06-04 00:08:19 +04:00
}
}
2010-10-17 15:45:53 +04:00
}
else if ( torrent_deleted_alert * p = dynamic_cast < torrent_deleted_alert * > ( a . get ( ) ) ) {
qDebug ( " A torrent was deleted from the hard disk, attempting to remove the root folder too... " ) ;
QString hash ;
2010-07-18 00:55:40 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 15:45:53 +04:00
hash = misc : : toQString ( p - > info_hash ) ;
2010-07-18 00:55:40 +04:00
# else
2010-10-17 15:45:53 +04:00
// Unfortunately libtorrent v0.14 does not provide the hash,
// only the torrent handle that is often invalid when it arrives
try {
if ( p - > handle . is_valid ( ) ) {
hash = misc : : toQString ( p - > handle . info_hash ( ) ) ;
}
} catch ( std : : exception ) { }
2010-07-18 00:55:40 +04:00
# endif
2010-10-17 15:45:53 +04:00
if ( ! hash . isEmpty ( ) ) {
if ( savePathsToRemove . contains ( hash ) ) {
misc : : removeEmptyTree ( savePathsToRemove . take ( hash ) ) ;
}
} else {
// XXX: Fallback
QStringList hashes_deleted ;
foreach ( const QString & key , savePathsToRemove . keys ( ) ) {
// Attempt to delete
misc : : removeEmptyTree ( savePathsToRemove [ key ] ) ;
if ( ! QDir ( savePathsToRemove [ key ] ) . exists ( ) ) {
hashes_deleted < < key ;
2010-07-18 00:55:40 +04:00
}
}
2010-10-17 15:45:53 +04:00
// Clean up
foreach ( const QString & key , hashes_deleted ) {
savePathsToRemove . remove ( key ) ;
}
2010-07-18 00:55:40 +04:00
}
2010-10-17 15:45:53 +04:00
}
else if ( storage_moved_alert * p = dynamic_cast < storage_moved_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
// Attempt to remove old folder if empty
const QString & old_save_path = TorrentPersistentData : : getPreviousPath ( h . hash ( ) ) ;
const QString new_save_path = QString : : fromLocal8Bit ( p - > path . c_str ( ) ) ;
qDebug ( " Torrent moved from %s to %s " , qPrintable ( old_save_path ) , qPrintable ( new_save_path ) ) ;
QDir old_save_dir ( old_save_path ) ;
if ( old_save_dir ! = QDir ( defaultSavePath ) & & old_save_dir ! = QDir ( defaultTempPath ) ) {
qDebug ( " Attempting to remove %s " , qPrintable ( old_save_path ) ) ;
misc : : removeEmptyTree ( old_save_path ) ;
}
if ( defaultTempPath . isEmpty ( ) | | ! new_save_path . startsWith ( defaultTempPath ) ) {
TorrentPersistentData : : saveSavePath ( h . hash ( ) , new_save_path ) ;
2010-04-10 18:50:11 +04:00
}
2010-10-17 15:45:53 +04:00
emit savePathChanged ( h ) ;
//h.force_recheck();
2010-01-05 00:16:33 +03:00
}
2010-10-17 15:45:53 +04:00
}
else if ( metadata_received_alert * p = dynamic_cast < metadata_received_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
qDebug ( " Received metadata for %s " , qPrintable ( h . hash ( ) ) ) ;
// Save metadata
const QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
if ( ! QFile : : exists ( torrentBackup . absoluteFilePath ( h . hash ( ) + QString ( " .torrent " ) ) ) )
h . save_torrent_file ( torrentBackup . absoluteFilePath ( h . hash ( ) + QString ( " .torrent " ) ) ) ;
// Copy the torrent file to the export folder
2010-10-17 18:46:01 +04:00
if ( torrentExport )
exportTorrentFile ( h ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 15:45:53 +04:00
// Append .!qB to incomplete files
if ( appendqBExtension )
appendqBextensionToTorrent ( h , true ) ;
2010-04-06 13:17:33 +04:00
# endif
2010-10-17 15:45:53 +04:00
// Truncate root folder
const QString root_folder = misc : : truncateRootFolder ( p - > handle ) ;
TorrentPersistentData : : setRootFolder ( h . hash ( ) , root_folder ) ;
// Move to a subfolder corresponding to the torrent root folder if necessary
if ( ! root_folder . isEmpty ( ) ) {
if ( ! h . is_seed ( ) & & ! defaultTempPath . isEmpty ( ) ) {
QString torrent_tmp_path = defaultTempPath . replace ( " \\ " , " / " ) ;
if ( ! torrent_tmp_path . endsWith ( " / " ) ) torrent_tmp_path + = " / " ;
torrent_tmp_path + = root_folder ;
h . move_storage ( torrent_tmp_path ) ;
} else {
QString save_path = h . save_path ( ) . replace ( " \\ " , " / " ) ;
if ( ! save_path . endsWith ( " / " ) ) save_path + = " / " ;
save_path + = root_folder ;
h . move_storage ( save_path ) ;
2010-01-03 02:03:46 +03:00
}
2009-08-17 12:08:51 +04:00
}
2010-10-17 15:45:53 +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 pausedTorrent ( h ) ;
}
2009-08-17 12:08:51 +04:00
}
2010-10-17 15:45:53 +04:00
}
else if ( file_error_alert * p = dynamic_cast < file_error_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
h . auto_managed ( false ) ;
std : : cerr < < " File Error: " < < p - > message ( ) . c_str ( ) < < std : : endl ;
addConsoleMessage ( tr ( " An I/O error occured, '%1' paused. " ) . arg ( h . name ( ) ) ) ;
addConsoleMessage ( tr ( " Reason: %1 " ) . arg ( misc : : toQString ( p - > message ( ) ) ) ) ;
2009-11-17 11:15:26 +03:00
if ( h . is_valid ( ) ) {
2010-10-17 15:45:53 +04:00
emit fullDiskError ( h , misc : : toQString ( p - > message ( ) ) ) ;
//h.pause();
emit pausedTorrent ( h ) ;
2009-11-17 11:15:26 +03:00
}
2009-09-21 23:08:33 +04:00
}
2010-10-17 15:45:53 +04:00
}
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR > 14
2010-10-17 15:45:53 +04:00
else if ( file_completed_alert * p = dynamic_cast < file_completed_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
qDebug ( " A file completed download in torrent %s " , qPrintable ( h . name ( ) ) ) ;
if ( appendqBExtension ) {
qDebug ( " appendqBTExtension is true " ) ;
QString name = misc : : toQStringU ( h . get_torrent_info ( ) . file_at ( p - > index ) . path . string ( ) ) ;
if ( name . endsWith ( " .!qB " ) ) {
const QString old_name = name ;
name . chop ( 4 ) ;
qDebug ( " Renaming %s to %s " , qPrintable ( old_name ) , qPrintable ( name ) ) ;
h . rename_file ( p - > index , name ) ;
2009-12-18 16:36:47 +03:00
}
}
2010-10-17 15:45:53 +04:00
}
2009-12-18 16:36:47 +03:00
# endif
2010-10-17 15:45:53 +04:00
else if ( torrent_paused_alert * p = dynamic_cast < torrent_paused_alert * > ( a . get ( ) ) ) {
if ( p - > handle . is_valid ( ) ) {
2010-03-04 22:29:48 +03:00
QTorrentHandle h ( p - > handle ) ;
2010-10-17 15:45:53 +04:00
if ( ! h . has_error ( ) )
h . save_resume_data ( ) ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
}
else if ( tracker_error_alert * p = dynamic_cast < tracker_error_alert * > ( a . get ( ) ) ) {
// Level: fatal
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
// Authentication
if ( p - > status_code ! = 401 ) {
qDebug ( " Received a tracker error for %s: %s " , p - > url . c_str ( ) , p - > msg . c_str ( ) ) ;
2010-07-23 18:05:53 +04:00
const QString tracker_url = misc : : toQString ( p - > url ) ;
2010-01-03 02:03:46 +03:00
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
TrackerInfos data = trackers_data . value ( tracker_url , TrackerInfos ( tracker_url ) ) ;
2010-10-17 15:45:53 +04:00
data . last_message = misc : : toQString ( p - > msg ) ;
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2010-10-17 15:45:53 +04:00
data . verified = false ;
+ + data . fail_count ;
2009-11-23 10:18:28 +03:00
# endif
2010-01-03 02:03:46 +03:00
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
2010-10-17 15:45:53 +04:00
} else {
emit trackerAuthenticationRequired ( h ) ;
2010-01-03 02:03:46 +03:00
}
2009-11-19 11:14:04 +03:00
}
2010-10-17 15:45:53 +04:00
}
else if ( tracker_reply_alert * p = dynamic_cast < tracker_reply_alert * > ( a . get ( ) ) ) {
const QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
qDebug ( " Received a tracker reply from %s (Num_peers=%d) " , p - > url . c_str ( ) , p - > num_peers ) ;
// Connection was successful now. Remove possible old errors
QHash < QString , TrackerInfos > trackers_data = trackersInfos . value ( h . hash ( ) , QHash < QString , TrackerInfos > ( ) ) ;
const 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 ;
# if LIBTORRENT_VERSION_MINOR < 15
data . fail_count = 0 ;
data . verified = true ;
# endif
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
} else if ( tracker_warning_alert * p = dynamic_cast < tracker_warning_alert * > ( a . get ( ) ) ) {
const 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 > ( ) ) ;
const 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
# if LIBTORRENT_VERSION_MINOR < 15
data . verified = true ;
data . fail_count = 0 ;
# endif
trackers_data . insert ( tracker_url , data ) ;
trackersInfos [ h . hash ( ) ] = trackers_data ;
qDebug ( " Received a tracker warning from %s: %s " , p - > url . c_str ( ) , p - > msg . c_str ( ) ) ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
}
else if ( portmap_error_alert * p = dynamic_cast < portmap_error_alert * > ( a . get ( ) ) ) {
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping failure, message: %1 " ) . arg ( misc : : toQString ( p - > message ( ) ) ) , " red " ) ;
//emit UPnPError(QString(p->msg().c_str()));
}
else if ( portmap_alert * p = dynamic_cast < portmap_alert * > ( a . get ( ) ) ) {
qDebug ( " UPnP Success, msg: %s " , p - > message ( ) . c_str ( ) ) ;
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping successful, message: %1 " ) . arg ( misc : : toQString ( p - > message ( ) ) ) , " blue " ) ;
//emit UPnPSuccess(QString(p->msg().c_str()));
}
else if ( peer_blocked_alert * p = dynamic_cast < peer_blocked_alert * > ( a . get ( ) ) ) {
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()));
}
else if ( fastresume_rejected_alert * p = dynamic_cast < fastresume_rejected_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
qDebug ( " /! \\ Fast resume failed for %s, reason: %s " , qPrintable ( h . name ( ) ) , p - > message ( ) . c_str ( ) ) ;
2010-06-23 15:59:28 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2010-10-17 15:45:53 +04:00
QString msg = QString : : fromLocal8Bit ( p - > message ( ) . c_str ( ) ) ;
if ( msg . contains ( " filesize " , Qt : : CaseInsensitive ) & & msg . contains ( " mismatch " , Qt : : CaseInsensitive ) & & TorrentPersistentData : : isSeed ( h . hash ( ) ) & & h . has_missing_files ( ) ) {
2010-06-23 15:59:28 +04:00
# else
2010-10-17 15:45:53 +04:00
if ( p - > error . value ( ) = = 134 & & TorrentPersistentData : : isSeed ( h . hash ( ) ) & & h . has_missing_files ( ) ) {
2010-06-23 15:59:28 +04:00
# endif
2010-10-17 15:45:53 +04:00
const QString hash = h . hash ( ) ;
// Mismatching file size (files were probably moved
addConsoleMessage ( tr ( " File sizes mismatch for torrent %1, pausing it. " ) . arg ( h . name ( ) ) ) ;
TorrentPersistentData : : setErrorState ( hash , true ) ;
pauseTorrent ( hash ) ;
} else {
addConsoleMessage ( tr ( " Fast resume data was rejected for torrent %1, checking again... " ) . arg ( h . name ( ) ) , QString : : fromUtf8 ( " red " ) ) ;
addConsoleMessage ( tr ( " Reason: %1 " ) . arg ( misc : : toQString ( p - > message ( ) ) ) ) ;
2010-06-27 21:58:08 +04:00
}
2010-10-17 15:45:53 +04:00
}
}
else if ( url_seed_alert * p = dynamic_cast < url_seed_alert * > ( a . get ( ) ) ) {
addConsoleMessage ( tr ( " Url seed lookup failed for url: %1, message: %2 " ) . arg ( misc : : toQString ( p - > url ) ) . arg ( misc : : toQString ( p - > message ( ) ) ) , QString : : fromUtf8 ( " red " ) ) ;
//emit urlSeedProblem(QString::fromUtf8(p->url.c_str()), QString::fromUtf8(p->msg().c_str()));
}
else if ( torrent_checked_alert * p = dynamic_cast < torrent_checked_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
const QString hash = h . hash ( ) ;
qDebug ( " %s have just finished checking " , qPrintable ( hash ) ) ;
// Save seed status
TorrentPersistentData : : saveSeedStatus ( h ) ;
// Move to temp directory if necessary
if ( ! h . is_seed ( ) & & ! defaultTempPath . isEmpty ( ) ) {
// Check if directory is different
const QDir current_dir ( h . save_path ( ) ) ;
const QDir save_dir ( getSavePath ( h . hash ( ) ) ) ;
if ( current_dir = = save_dir ) {
QString root_folder = TorrentPersistentData : : getRootFolder ( hash ) ;
QString torrent_tmp_path = defaultTempPath . replace ( " \\ " , " / " ) ;
if ( ! root_folder . isEmpty ( ) ) {
if ( ! torrent_tmp_path . endsWith ( " / " ) ) torrent_tmp_path + = " / " ;
torrent_tmp_path + = root_folder ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
h . move_storage ( torrent_tmp_path ) ;
2010-01-20 21:02:26 +03:00
}
2009-08-06 12:58:49 +04:00
}
2010-10-17 15:45:53 +04:00
emit torrentFinishedChecking ( h ) ;
if ( torrentsToPausedAfterChecking . contains ( hash ) ) {
torrentsToPausedAfterChecking . removeOne ( hash ) ;
h . pause ( ) ;
emit pausedTorrent ( h ) ;
}
2007-08-17 06:03:13 +04:00
}
}
2010-10-17 15:45:53 +04:00
a = s - > pop_alert ( ) ;
}
}
2007-03-05 20:35:38 +03:00
2010-10-17 18:46:01 +04:00
void QBtSession : : recheckTorrent ( QString hash ) {
2010-10-17 15:45:53 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( h . is_valid ( ) & & h . has_metadata ( ) ) {
if ( h . is_paused ( ) ) {
if ( ! torrentsToPausedAfterChecking . contains ( h . hash ( ) ) ) {
torrentsToPausedAfterChecking < < h . hash ( ) ;
h . resume ( ) ;
2010-01-20 21:02:26 +03:00
}
}
2010-10-17 15:45:53 +04:00
h . force_recheck ( ) ;
}
}
2010-01-20 21:02:26 +03:00
2010-10-17 18:46:01 +04:00
QHash < QString , TrackerInfos > QBtSession : : getTrackersInfo ( QString hash ) const {
2010-10-17 15:45:53 +04:00
return trackersInfos . value ( hash , QHash < QString , TrackerInfos > ( ) ) ;
}
2007-07-27 17:58:12 +04:00
2010-10-17 18:46:01 +04:00
int QBtSession : : getListenPort ( ) const {
2010-10-17 15:45:53 +04:00
qDebug ( " LISTEN PORT: %d " , s - > listen_port ( ) ) ;
return s - > listen_port ( ) ;
}
2007-03-08 01:36:01 +03:00
2010-10-17 18:46:01 +04:00
session_status QBtSession : : getSessionStatus ( ) const {
2010-10-17 15:45:53 +04:00
return s - > status ( ) ;
}
2007-03-08 01:36:01 +03:00
2010-10-17 18:46:01 +04:00
QString QBtSession : : getSavePath ( QString hash , bool fromScanDir , QString filePath , QString root_folder ) {
2010-10-17 15:45:53 +04:00
QString savePath ;
if ( TorrentTempData : : hasTempData ( hash ) ) {
savePath = TorrentTempData : : getSavePath ( hash ) ;
if ( savePath . isEmpty ( ) ) {
savePath = defaultSavePath ;
}
if ( appendLabelToSavePath ) {
qDebug ( " appendLabelToSavePath is true " ) ;
const QString label = TorrentTempData : : getLabel ( hash ) ;
if ( ! label . isEmpty ( ) ) {
savePath = misc : : updateLabelInSavePath ( defaultSavePath , savePath , " " , label ) ;
2009-12-17 23:28:30 +03:00
}
2010-06-27 21:58:08 +04:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " getSavePath, got save_path from temp data: %s " , qPrintable ( savePath ) ) ;
} else {
savePath = TorrentPersistentData : : getSavePath ( hash ) ;
qDebug ( " SavePath got from persistant data is %s " , qPrintable ( savePath ) ) ;
bool append_root_folder = false ;
if ( savePath . isEmpty ( ) ) {
if ( fromScanDir & & m_scanFolders - > downloadInTorrentFolder ( filePath ) )
savePath = QFileInfo ( filePath ) . dir ( ) . path ( ) ;
else {
savePath = defaultSavePath ;
append_root_folder = true ;
}
} else {
QIniSettings settings ( " qBittorrent " , " qBittorrent " ) ;
if ( ! settings . value ( " ported_to_new_savepath_system " , false ) . toBool ( ) ) {
append_root_folder = true ;
2010-06-27 21:58:08 +04:00
}
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
if ( ! fromScanDir & & appendLabelToSavePath ) {
const QString label = TorrentPersistentData : : getLabel ( hash ) ;
if ( ! label . isEmpty ( ) ) {
qDebug ( " Torrent label is %s " , qPrintable ( label ) ) ;
savePath = misc : : updateLabelInSavePath ( defaultSavePath , savePath , " " , label ) ;
}
2010-06-27 21:58:08 +04:00
}
2010-10-17 15:45:53 +04:00
if ( append_root_folder & & ! root_folder . isEmpty ( ) ) {
// Append torrent root folder to the save path
if ( ! savePath . endsWith ( QDir : : separator ( ) ) )
savePath + = QDir : : separator ( ) ;
savePath + = root_folder ;
qDebug ( " Torrent root folder is %s " , qPrintable ( root_folder ) ) ;
TorrentPersistentData : : saveSavePath ( hash , savePath ) ;
2010-06-27 21:58:08 +04:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " getSavePath, got save_path from persistent data: %s " , qPrintable ( savePath ) ) ;
}
// Clean path
savePath = savePath . replace ( " \\ " , " / " ) ;
savePath = misc : : expandPath ( savePath ) ;
if ( ! savePath . endsWith ( " / " ) )
savePath + = " / " ;
return savePath ;
}
2008-09-14 14:14:54 +04:00
2010-10-17 15:45:53 +04:00
// Take an url string to a torrent file,
// download the torrent file to a tmp location, then
// add it to download list
2010-10-17 18:46:01 +04:00
void QBtSession : : downloadFromUrl ( QString url ) {
2010-10-17 15:45:53 +04:00
addConsoleMessage ( tr ( " Downloading '%1', please wait... " , " e.g: Downloading 'xxx.torrent', please wait... " ) . arg ( url )
# ifndef DISABLE_GUI
, QPalette : : WindowText
# endif
) ;
//emit aboutToDownloadFromUrl(url);
// Launch downloader thread
downloader - > downloadTorrentUrl ( url ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : downloadFromURLList ( const QStringList & urls ) {
2010-10-17 15:45:53 +04:00
foreach ( const QString & url , urls ) {
downloadFromUrl ( url ) ;
}
}
2010-10-17 18:46:01 +04:00
void QBtSession : : addMagnetSkipAddDlg ( QString uri ) {
2010-10-17 15:45:53 +04:00
addMagnetUri ( uri , false ) ;
}
2010-10-17 18:46:01 +04:00
void QBtSession : : downloadUrlAndSkipDialog ( QString url , QString save_path ) {
2010-10-17 15:45:53 +04:00
//emit aboutToDownloadFromUrl(url);
const QUrl qurl = QUrl : : fromEncoded ( url . toLocal8Bit ( ) ) ;
if ( ! save_path . isEmpty ( ) )
savepath_fromurl [ qurl ] = save_path ;
url_skippingDlg < < qurl ;
// Launch downloader thread
downloader - > downloadUrl ( url ) ;
}
// Add to Bittorrent session the downloaded torrent file
2010-10-17 18:46:01 +04:00
void QBtSession : : processDownloadedFile ( QString url , QString file_path ) {
2010-10-17 15:45:53 +04:00
const int index = url_skippingDlg . indexOf ( QUrl : : fromEncoded ( url . toLocal8Bit ( ) ) ) ;
if ( index < 0 ) {
// Add file to torrent download list
2010-05-30 21:51:40 +04:00
# ifdef Q_WS_WIN
2010-10-17 15:45:53 +04:00
// Windows hack
if ( ! file_path . endsWith ( " .torrent " ) ) {
Q_ASSERT ( QFile : : exists ( file_path ) ) ;
qDebug ( " Torrent name does not end with .torrent, fixing... " ) ;
if ( QFile : : rename ( file_path , file_path + " .torrent " ) ) {
file_path + = " .torrent " ;
2010-06-27 21:58:08 +04:00
} else {
2010-10-17 15:45:53 +04:00
qDebug ( " Failed to rename torrent file! " ) ;
2010-06-27 21:58:08 +04:00
}
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
qDebug ( " Downloading torrent at path: %s " , qPrintable ( file_path ) ) ;
# endif
emit newDownloadedTorrent ( file_path , url ) ;
} else {
url_skippingDlg . removeAt ( index ) ;
addTorrent ( file_path , false , url , false ) ;
}
}
2007-03-08 01:36:01 +03:00
2010-10-17 15:45:53 +04:00
// Return current download rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
2010-10-17 18:46:01 +04:00
float QBtSession : : getPayloadDownloadRate ( ) const {
2010-10-17 15:45:53 +04:00
return s - > status ( ) . payload_download_rate ;
}
2007-03-08 01:36:01 +03:00
2010-10-17 15:45:53 +04:00
// Return current upload rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
2010-10-17 18:46:01 +04:00
float QBtSession : : getPayloadUploadRate ( ) const {
2010-10-17 15:45:53 +04:00
return s - > status ( ) . payload_upload_rate ;
}
2007-03-08 01:36:01 +03:00
2010-06-07 12:32:41 +04:00
# if LIBTORRENT_VERSION_MINOR < 15
2010-10-17 15:45:53 +04:00
// Save DHT entry to hard drive
2010-10-17 18:46:01 +04:00
void QBtSession : : saveDHTEntry ( ) {
2010-10-17 15:45:53 +04:00
// Save DHT entry
if ( DHTEnabled ) {
try {
entry dht_state = s - > dht_state ( ) ;
const QString dht_path = misc : : cacheLocation ( ) + QDir : : separator ( ) + QString : : fromUtf8 ( " dht_state " ) ;
if ( QFile : : exists ( dht_path ) )
misc : : safeRemove ( dht_path ) ;
boost : : filesystem : : ofstream out ( dht_path . toLocal8Bit ( ) . constData ( ) , std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , dht_state ) ;
qDebug ( " DHT entry saved " ) ;
} catch ( std : : exception & e ) {
std : : cerr < < e . what ( ) < < std : : endl ;
2007-03-08 01:36:01 +03:00
}
2010-10-17 15:45:53 +04:00
}
}
2010-04-06 20:52:17 +04:00
# endif
2009-11-06 17:30:14 +03:00
2010-10-17 18:46:01 +04:00
void QBtSession : : applyEncryptionSettings ( pe_settings se ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Applying encryption settings " ) ;
s - > set_pe_settings ( se ) ;
}
2010-06-27 21:58:08 +04:00
2010-10-17 15:45:53 +04:00
// Will fast resume torrents in
// backup directory
2010-10-17 18:46:01 +04:00
void QBtSession : : startUpTorrents ( ) {
2010-10-17 15:45:53 +04:00
qDebug ( " Resuming unfinished torrents " ) ;
const QDir torrentBackup ( misc : : BTBackupLocation ( ) ) ;
const QStringList known_torrents = TorrentPersistentData : : knownTorrents ( ) ;
// Safety measure because some people reported torrent loss since
// we switch the v1.5 way of resuming torrents on startup
QStringList filters ;
filters < < " *.torrent " ;
const 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 ) ) {
qDebug ( " found torrent with hash: %s on hard disk " , qPrintable ( hash ) ) ;
std : : cerr < < " ERROR Detected!!! Adding back torrent " < < qPrintable ( hash ) < < " which got lost for some reason. " < < std : : endl ;
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
}
}
// End of safety measure
qDebug ( " Starting up torrents " ) ;
if ( isQueueingEnabled ( ) ) {
priority_queue < QPair < int , QString > , vector < QPair < int , QString > > , std : : greater < QPair < int , QString > > > torrent_queue ;
foreach ( const QString & hash , known_torrents ) {
QString filePath ;
if ( TorrentPersistentData : : isMagnet ( hash ) ) {
filePath = TorrentPersistentData : : getMagnetUri ( hash ) ;
} else {
filePath = torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " ;
2010-01-03 02:03:46 +03:00
}
2010-10-17 15:45:53 +04:00
const int prio = TorrentPersistentData : : getPriority ( hash ) ;
torrent_queue . push ( qMakePair ( prio , hash ) ) ;
}
qDebug ( " Priority_queue size: %ld " , ( long ) torrent_queue . size ( ) ) ;
// Resume downloads
while ( ! torrent_queue . empty ( ) ) {
const QString hash = torrent_queue . top ( ) . second ;
torrent_queue . pop ( ) ;
qDebug ( " Starting up torrent %s " , qPrintable ( hash ) ) ;
if ( TorrentPersistentData : : isMagnet ( hash ) ) {
addMagnetUri ( TorrentPersistentData : : getMagnetUri ( hash ) , true ) ;
2010-06-27 21:58:08 +04:00
} else {
2010-10-17 15:45:53 +04:00
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
2009-08-27 12:43:19 +04:00
}
}
2010-10-17 15:45:53 +04:00
} else {
// Resume downloads
foreach ( const QString & hash , known_torrents ) {
qDebug ( " Starting up torrent %s " , qPrintable ( hash ) ) ;
if ( TorrentPersistentData : : isMagnet ( hash ) )
addMagnetUri ( TorrentPersistentData : : getMagnetUri ( hash ) , true ) ;
else
addTorrent ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " , false , QString ( ) , true ) ;
}
}
QIniSettings settings ( " qBittorrent " , " qBittorrent " ) ;
settings . setValue ( " ported_to_new_savepath_system " , true ) ;
qDebug ( " Unfinished torrents resumed " ) ;
}