2007-03-05 16:55:23 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez
2007-03-05 16:55:23 +03:00
*
2007-07-14 18:31:59 +04:00
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
2007-03-05 16:55:23 +03:00
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2007-07-14 18:31:59 +04:00
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
2009-04-05 21:00:55 +04:00
* In addition , as a special exception , the copyright holders give permission to
* link this program with the OpenSSL project ' s " OpenSSL " library ( or with
* modified versions of it that use the same license as the " OpenSSL " library ) ,
* and distribute the linked executables . You must obey the GNU General Public
* License in all respects for all of the code used other than " OpenSSL " . If you
* modify file ( s ) , you may extend this exception to your version of the file ( s ) ,
* but you are not obligated to do so . If you do not wish to do so , delete this
* exception statement from your version .
*
2007-07-14 18:31:59 +04:00
* Contact : chris @ qbittorrent . org
2007-03-05 16:55:23 +03:00
*/
2007-03-27 22:49:29 +04:00
# include <QDir>
2009-07-12 13:08:38 +04:00
# include <QDateTime>
2007-03-27 22:49:29 +04:00
# include <QString>
2007-07-30 17:56:31 +04:00
# include <QTimer>
2008-12-26 16:17:08 +03:00
# include <QFileSystemWatcher>
2008-05-18 13:50:35 +04:00
# include <QSettings>
2008-12-26 16:17:08 +03:00
# include <QMutex>
2007-03-27 22:49:29 +04:00
2007-09-29 12:00:14 +04:00
# include "bittorrent.h"
# include "misc.h"
# include "downloadThread.h"
2008-05-18 00:32:03 +04:00
# include "filterParserThread.h"
2008-11-02 22:22:10 +03:00
# include <libtorrent/extensions/ut_metadata.hpp>
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>
# include <libtorrent/extensions/metadata_transfer.hpp>
2007-07-23 16:46:36 +04:00
# include <libtorrent/entry.hpp>
# include <libtorrent/bencode.hpp>
# include <libtorrent/identify_client.hpp>
# include <libtorrent/alert_types.hpp>
2007-08-26 20:25:22 +04:00
# include <libtorrent/torrent_info.hpp>
2007-08-02 00:01:06 +04:00
# include <boost/filesystem/exception.hpp>
2007-07-23 16:46:36 +04:00
2007-08-29 19:13:20 +04:00
# define MAX_TRACKER_ERRORS 2
2009-07-12 11:23:11 +04:00
# define MAX_RATIO 100.
2007-06-29 19:23:15 +04:00
2007-03-05 16:55:23 +03:00
// Main constructor
2009-07-12 11:23:11 +04:00
bittorrent : : bittorrent ( ) : DHTEnabled ( false ) , preAllocateAll ( false ) , addInPause ( false ) , maxConnecsPerTorrent ( 500 ) , maxUploadsPerTorrent ( 4 ) , ratio_limit ( - 1 ) , UPnPEnabled ( false ) , NATPMPEnabled ( false ) , LSDEnabled ( false ) , queueingEnabled ( false ) {
2007-04-07 11:43:57 +04:00
// To avoid some exceptions
fs : : path : : default_name_check ( fs : : no_check ) ;
2007-03-05 16:55:23 +03:00
// Creating bittorrent session
2008-05-18 13:50:35 +04:00
// Check if we should spoof azureus
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
if ( settings . value ( QString : : fromUtf8 ( " AzureusSpoof " ) , false ) . toBool ( ) ) {
2009-01-05 23:41:35 +03:00
s = new session ( fingerprint ( " AZ " , 3 , 0 , 5 , 2 ) , 0 ) ;
2008-05-18 13:50:35 +04:00
} else {
2009-01-05 23:41:35 +03:00
s = new session ( fingerprint ( " qB " , VERSION_MAJOR , VERSION_MINOR , VERSION_BUGFIX , 0 ) , 0 ) ;
2008-05-18 13:50:35 +04:00
}
2007-03-05 16:55:23 +03:00
// Set severity level of libtorrent session
2008-11-02 02:58:53 +03:00
//s->set_alert_mask(alert::all_categories & ~alert::progress_notification);
s - > set_alert_mask ( alert : : error_notification | alert : : peer_notification | alert : : port_mapping_notification | alert : : storage_notification | alert : : tracker_notification | alert : : status_notification | alert : : ip_block_notification ) ;
2008-11-02 16:19:27 +03:00
// Load previous state
loadSessionState ( ) ;
2008-11-02 22:22:10 +03:00
// Enabling plugins
2007-03-05 16:55:23 +03:00
s - > add_extension ( & create_metadata_plugin ) ;
2008-11-02 22:22:10 +03:00
s - > add_extension ( & create_ut_metadata_plugin ) ;
s - > add_extension ( & create_ut_pex_plugin ) ;
s - > add_extension ( & create_smart_ban_plugin ) ;
2007-07-30 17:56:31 +04:00
timerAlerts = new QTimer ( ) ;
connect ( timerAlerts , SIGNAL ( timeout ( ) ) , this , SLOT ( readAlerts ( ) ) ) ;
timerAlerts - > start ( 3000 ) ;
2007-03-08 01:36:01 +03:00
// To download from urls
downloader = new downloadThread ( this ) ;
2007-07-22 13:47:27 +04:00
connect ( downloader , SIGNAL ( downloadFinished ( QString , QString ) ) , this , SLOT ( processDownloadedFile ( QString , QString ) ) ) ;
2007-08-26 20:25:22 +04:00
connect ( downloader , SIGNAL ( downloadFailure ( QString , QString ) ) , this , SLOT ( handleDownloadFailure ( QString , QString ) ) ) ;
2008-05-18 00:32:03 +04:00
BigRatioTimer = 0 ;
filterParser = 0 ;
2008-12-26 16:17:08 +03:00
FSWatcher = 0 ;
2007-08-26 20:25:22 +04:00
qDebug ( " * BTSession constructed " ) ;
2007-03-08 01:36:01 +03:00
}
2007-03-05 16:55:23 +03:00
// Main destructor
2007-08-20 10:29:18 +04:00
bittorrent : : ~ bittorrent ( ) {
2008-05-18 16:01:42 +04:00
qDebug ( " BTSession deletion " ) ;
2008-11-02 16:19:27 +03:00
// Do some BT related saving
2008-12-27 13:02:42 +03:00
// XXX: Done in GUI now (earlier = safer)
/*saveDHTEntry();
2008-11-02 16:19:27 +03:00
saveSessionState ( ) ;
2009-01-06 00:19:55 +03:00
tResumeData ( ) ; */
2007-07-29 18:54:39 +04:00
// Disable directory scanning
2007-03-05 16:55:23 +03:00
disableDirectoryScanning ( ) ;
2007-07-29 18:54:39 +04:00
// Delete our objects
2007-07-30 17:56:31 +04:00
delete timerAlerts ;
2008-09-13 11:33:41 +04:00
if ( BigRatioTimer )
2007-11-25 00:55:19 +03:00
delete BigRatioTimer ;
2008-09-13 11:33:41 +04:00
if ( filterParser )
2008-05-18 00:32:03 +04:00
delete filterParser ;
2007-03-08 01:36:01 +03:00
delete downloader ;
2008-12-26 16:39:11 +03:00
if ( FSWatcher ) {
delete FSWatcher ;
delete FSMutex ;
}
2007-08-08 03:15:46 +04:00
// Delete BT session
2007-11-24 15:11:12 +03:00
qDebug ( " Deleting session " ) ;
2007-03-05 16:55:23 +03:00
delete s ;
2007-11-24 15:11:12 +03:00
qDebug ( " Session deleted " ) ;
2007-03-05 16:55:23 +03:00
}
2007-09-08 21:07:29 +04:00
void bittorrent : : preAllocateAllFiles ( bool b ) {
2007-11-03 01:56:07 +03:00
bool change = ( preAllocateAll ! = b ) ;
if ( change ) {
qDebug ( " PreAllocateAll changed, reloading all torrents! " ) ;
preAllocateAll = b ;
2007-09-08 21:07:29 +04:00
}
}
2007-09-09 13:09:24 +04:00
void bittorrent : : deleteBigRatios ( ) {
2009-07-12 11:23:11 +04:00
if ( ratio_limit = = - 1 ) return ;
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_seed ( ) ) {
QString hash = h . hash ( ) ;
2009-07-12 11:23:11 +04:00
float ratio = getRealRatio ( hash ) ;
if ( ratio < = MAX_RATIO & & ratio > ratio_limit ) {
2008-12-27 00:18:33 +03:00
QString fileName = h . name ( ) ;
addConsoleMessage ( tr ( " %1 reached the maximum ratio you set. " ) . arg ( fileName ) ) ;
deleteTorrent ( hash ) ;
//emit torrent_ratio_deleted(fileName);
}
}
2007-09-09 13:09:24 +04:00
}
}
2007-08-20 10:29:18 +04:00
void bittorrent : : setDownloadLimit ( QString hash , long val ) {
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-07-30 20:57:28 +04:00
if ( h . is_valid ( ) )
h . set_download_limit ( val ) ;
2007-07-14 17:38:29 +04:00
saveTorrentSpeedLimits ( hash ) ;
}
2008-07-15 02:01:05 +04:00
bool bittorrent : : isQueueingEnabled ( ) const {
return queueingEnabled ;
2008-07-14 23:20:18 +04:00
}
2008-07-15 00:01:21 +04:00
void bittorrent : : increaseDlTorrentPriority ( QString hash ) {
2008-08-02 23:46:02 +04:00
Q_ASSERT ( queueingEnabled ) ;
2008-11-02 13:47:59 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2009-01-03 22:47:03 +03:00
if ( h . queue_position ( ) > 0 )
h . queue_position_up ( ) ;
2008-07-14 23:20:18 +04:00
}
2008-07-15 00:01:21 +04:00
void bittorrent : : decreaseDlTorrentPriority ( QString hash ) {
2008-08-02 23:46:02 +04:00
Q_ASSERT ( queueingEnabled ) ;
2008-11-02 13:47:59 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
h . queue_position_down ( ) ;
2008-07-14 23:20:18 +04:00
}
2007-08-20 10:29:18 +04:00
void bittorrent : : setUploadLimit ( QString hash , long val ) {
2007-08-04 10:23:44 +04:00
qDebug ( " Set upload limit rate to %ld " , val ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-07-30 20:57:28 +04:00
if ( h . is_valid ( ) )
h . set_upload_limit ( val ) ;
2007-07-14 17:38:29 +04:00
saveTorrentSpeedLimits ( hash ) ;
}
2007-08-26 20:25:22 +04:00
void bittorrent : : handleDownloadFailure ( QString url , QString reason ) {
2007-07-21 00:18:18 +04:00
emit downloadFromUrlFailure ( url , reason ) ;
}
2007-09-08 21:07:29 +04:00
void bittorrent : : startTorrentsInPause ( bool b ) {
addInPause = b ;
}
2008-07-15 02:01:05 +04:00
void bittorrent : : setQueueingEnabled ( bool enable ) {
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
}
}
int bittorrent : : getDlTorrentPriority ( QString hash ) const {
2008-07-15 02:01:05 +04:00
Q_ASSERT ( queueingEnabled ) ;
2008-11-02 13:47:59 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
return h . queue_position ( ) ;
2008-07-15 02:01:05 +04:00
}
2008-11-02 13:47:59 +03:00
int bittorrent : : getUpTorrentPriority ( QString hash ) const {
2008-07-15 02:01:05 +04:00
Q_ASSERT ( queueingEnabled ) ;
2008-11-02 13:47:59 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
return h . queue_position ( ) ;
2008-07-14 23:20:18 +04:00
}
2007-11-25 00:55:19 +03:00
// Calculate the ETA using GASA
// GASA: global Average Speed Algorithm
qlonglong bittorrent : : getETA ( QString hash ) const {
2008-11-02 16:07:38 +03:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) ) return - 1 ;
switch ( h . state ( ) ) {
2008-11-02 00:42:56 +03:00
case torrent_status : : downloading : {
2008-11-02 16:07:38 +03:00
if ( h . active_time ( ) = = 0 )
return - 1 ;
double avg_speed = ( double ) h . all_time_download ( ) / h . active_time ( ) ;
return ( qlonglong ) floor ( ( double ) ( h . actual_size ( ) - h . total_wanted_done ( ) ) / avg_speed ) ;
}
default :
2007-11-25 00:55:19 +03:00
return - 1 ;
2007-06-29 19:23:15 +04:00
}
}
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > bittorrent : : getTorrents ( ) const {
return s - > get_torrents ( ) ;
}
2007-03-05 16:55:23 +03:00
// Return the torrent handle, given its hash
2007-08-20 10:29:18 +04:00
QTorrentHandle bittorrent : : getTorrentHandle ( QString hash ) const {
return QTorrentHandle ( s - > find_torrent ( misc : : fromString < sha1_hash > ( ( hash . toStdString ( ) ) ) ) ) ;
2007-03-05 16:55:23 +03:00
}
2008-05-18 13:26:02 +04:00
unsigned int bittorrent : : getFinishedPausedTorrentsNb ( ) const {
unsigned int nbPaused = 0 ;
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_seed ( ) & & h . is_paused ( ) ) {
+ + nbPaused ;
}
2008-05-18 13:26:02 +04:00
}
return nbPaused ;
}
unsigned int bittorrent : : getUnfinishedPausedTorrentsNb ( ) const {
unsigned int nbPaused = 0 ;
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( ! h . is_seed ( ) & & h . is_paused ( ) ) {
+ + nbPaused ;
}
2008-05-18 13:26:02 +04:00
}
return nbPaused ;
}
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
2007-08-20 10:29:18 +04:00
void bittorrent : : deleteTorrent ( QString hash , bool permanent ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Deleting torrent with hash: %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
2007-03-05 20:35:38 +03:00
return ;
}
2007-08-20 10:29:18 +04:00
QString savePath = h . save_path ( ) ;
QString fileName = h . name ( ) ;
2007-10-19 20:00:42 +04:00
// Remove it from session
2008-11-02 00:42:56 +03:00
if ( permanent )
s - > remove_torrent ( h . get_torrent_handle ( ) , session : : delete_files ) ;
else
s - > remove_torrent ( h . get_torrent_handle ( ) ) ;
2007-03-05 16:55:23 +03:00
// Remove it from torrent backup directory
2007-03-05 20:35:38 +03:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2007-07-31 12:13:38 +04:00
QStringList filters ;
filters < < hash + " .* " ;
QStringList files = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & file , files ) {
2007-07-31 12:13:38 +04:00
torrentBackup . remove ( file ) ;
}
2007-07-27 17:58:12 +04:00
// Remove tracker errors
2007-07-30 20:57:28 +04:00
trackersErrors . remove ( hash ) ;
2008-09-07 15:31:29 +04:00
if ( permanent )
addConsoleMessage ( tr ( " '%1' was removed permanently. " , " 'xxx.avi' was removed permanently. " ) . arg ( fileName ) ) ;
else
addConsoleMessage ( tr ( " '%1' was removed. " , " '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
}
2008-05-16 11:10:50 +04:00
void bittorrent : : pauseAllTorrents ( ) {
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( ! h . is_paused ( ) ) {
h . pause ( ) ;
2008-12-29 21:36:54 +03:00
emit pausedTorrent ( h ) ;
2008-12-27 00:18:33 +03:00
}
}
2008-05-16 11:10:50 +04:00
}
void bittorrent : : resumeAllTorrents ( ) {
2008-12-27 00:18:33 +03:00
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_paused ( ) ) {
h . resume ( ) ;
2008-12-29 21:36:54 +03:00
emit resumedTorrent ( h ) ;
2008-12-27 00:18:33 +03:00
}
}
2008-05-16 11:10:50 +04:00
}
2008-12-27 13:14:16 +03:00
void bittorrent : : pauseTorrent ( QString hash ) {
QTorrentHandle h = getTorrentHandle ( hash ) ;
2008-12-29 21:36:54 +03:00
if ( ! h . is_paused ( ) ) {
2008-12-27 13:14:16 +03:00
h . pause ( ) ;
2008-12-29 21:36:54 +03:00
emit pausedTorrent ( h ) ;
}
2008-12-27 13:14:16 +03:00
}
void bittorrent : : resumeTorrent ( QString hash ) {
QTorrentHandle h = getTorrentHandle ( hash ) ;
2008-12-29 21:36:54 +03:00
if ( h . is_paused ( ) ) {
2008-12-27 13:14:16 +03:00
h . resume ( ) ;
2008-12-29 21:36:54 +03:00
emit resumedTorrent ( h ) ;
}
2008-12-27 13:14:16 +03:00
}
2007-08-20 10:29:18 +04:00
void bittorrent : : loadWebSeeds ( QString hash ) {
QFile urlseeds_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .urlseeds " ) ;
2007-07-27 13:53:55 +04:00
if ( ! urlseeds_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) return ;
QByteArray urlseeds_lines = urlseeds_file . readAll ( ) ;
urlseeds_file . close ( ) ;
QList < QByteArray > url_seeds = urlseeds_lines . split ( ' \n ' ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-08-18 09:55:09 +04:00
// First remove from the torrent the url seeds that were deleted
// in a previous session
QStringList seeds_to_delete ;
2007-08-20 10:29:18 +04:00
QStringList existing_seeds = h . url_seeds ( ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & existing_seed , existing_seeds ) {
2009-07-12 12:13:00 +04:00
if ( ! url_seeds . contains ( existing_seed . toLocal8Bit ( ) ) ) {
2007-08-18 09:55:09 +04:00
seeds_to_delete < < existing_seed ;
}
}
2009-01-24 22:20:09 +03:00
foreach ( const QString & existing_seed , seeds_to_delete ) {
2007-08-20 10:29:18 +04:00
h . remove_url_seed ( existing_seed ) ;
2007-08-18 09:55:09 +04:00
}
// Add the ones that were added in a previous session
2009-01-24 22:20:09 +03:00
foreach ( const QByteArray & url_seed , url_seeds ) {
2007-08-20 10:29:18 +04:00
if ( ! url_seed . isEmpty ( ) ) {
2007-08-18 09:55:09 +04:00
// XXX: Should we check if it is already in the list before adding it
// or is libtorrent clever enough to know
2007-08-20 10:29:18 +04:00
h . add_url_seed ( url_seed ) ;
2007-08-18 09:55:09 +04:00
}
2007-07-27 13:53:55 +04:00
}
}
2007-03-05 16:55:23 +03:00
// Add a torrent to the bittorrent session
2008-12-30 12:02:06 +03:00
QTorrentHandle bittorrent : : addTorrent ( QString path , bool fromScanDir , QString from_url , bool ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h ;
2007-03-05 16:55:23 +03:00
bool fastResume = false ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2008-12-26 16:17:08 +03:00
QString file , dest_file ;
2007-03-05 16:55:23 +03:00
// Checking if BT_backup Dir exists
// create it if it is not
2007-08-20 10:29:18 +04:00
if ( ! torrentBackup . exists ( ) ) {
if ( ! torrentBackup . mkpath ( torrentBackup . path ( ) ) ) {
2009-07-12 12:13:00 +04:00
std : : cerr < < " Couldn't create the directory: ' " < < torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) < < " ' \n " ;
2007-03-05 20:35:38 +03:00
exit ( 1 ) ;
2007-03-05 16:55:23 +03:00
}
}
// Processing torrents
2007-09-08 21:07:29 +04:00
file = path . trimmed ( ) . replace ( " file:// " , " " , Qt : : CaseInsensitive ) ;
2007-08-20 10:29:18 +04:00
if ( file . isEmpty ( ) ) {
2008-12-30 03:34:41 +03:00
return h ;
2007-03-05 16:55:23 +03:00
}
2007-09-09 11:44:22 +04:00
Q_ASSERT ( ! file . startsWith ( " http:// " , Qt : : CaseInsensitive ) & & ! file . startsWith ( " https:// " , Qt : : CaseInsensitive ) & & ! file . startsWith ( " ftp:// " , Qt : : CaseInsensitive ) ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " Adding %s to download list " , file . toLocal8Bit ( ) . data ( ) ) ;
2008-11-02 00:42:56 +03:00
boost : : intrusive_ptr < torrent_info > t ;
try {
// Getting torrent file informations
2009-07-12 12:13:00 +04:00
t = new torrent_info ( file . toLocal8Bit ( ) . data ( ) ) ;
2008-11-02 00:42:56 +03:00
} catch ( std : : exception & ) {
if ( ! from_url . isNull ( ) ) {
addConsoleMessage ( tr ( " Unable to decode torrent file: '%1' " , " e.g: Unable to decode torrent file: '/home/y/xxx.torrent' " ) . arg ( from_url ) , QString : : fromUtf8 ( " red " ) ) ;
//emit invalidTorrent(from_url);
QFile : : remove ( file ) ;
} else {
addConsoleMessage ( tr ( " Unable to decode torrent file: '%1' " , " e.g: Unable to decode torrent file: '/home/y/xxx.torrent' " ) . arg ( file ) , QString : : fromUtf8 ( " red " ) ) ;
//emit invalidTorrent(file);
}
addConsoleMessage ( tr ( " This file is either corrupted or this isn't a torrent. " ) , QString : : fromUtf8 ( " red " ) ) ;
if ( fromScanDir ) {
2008-12-26 16:17:08 +03:00
// Remove file
QFile : : remove ( file ) ;
2008-11-02 00:42:56 +03:00
}
2008-12-30 03:34:41 +03:00
return h ;
2008-11-02 00:42:56 +03:00
}
qDebug ( " -> Hash: %s " , misc : : toString ( t - > info_hash ( ) ) . c_str ( ) ) ;
qDebug ( " -> Name: %s " , t - > name ( ) . c_str ( ) ) ;
QString hash = misc : : toQString ( t - > info_hash ( ) ) ;
if ( file . startsWith ( torrentBackup . path ( ) ) ) {
2007-08-26 22:50:51 +04:00
QFileInfo fi ( file ) ;
QString old_hash = fi . baseName ( ) ;
if ( old_hash ! = hash ) {
2009-07-12 12:13:00 +04:00
qDebug ( " * ERROR: Strange, hash changed from %s to %s " , old_hash . toLocal8Bit ( ) . data ( ) , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-08-26 22:50:51 +04:00
}
2008-11-02 00:42:56 +03:00
}
// Check if torrent is already in download list
if ( s - > find_torrent ( t - > info_hash ( ) ) . is_valid ( ) ) {
2007-08-26 22:41:45 +04:00
qDebug ( " /! \\ Torrent is already in download list " ) ;
2007-03-05 16:55:23 +03:00
// Update info Bar
2007-08-20 10:29:18 +04:00
if ( ! fromScanDir ) {
2008-11-02 00:42:56 +03:00
if ( ! from_url . isNull ( ) ) {
// If download from url, remove temp file
QFile : : remove ( file ) ;
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( from_url ) ) ;
//emit duplicateTorrent(from_url);
} else {
addConsoleMessage ( tr ( " '%1' is already in download list. " , " e.g: 'xxx.avi' is already in download list. " ) . arg ( file ) ) ;
//emit duplicateTorrent(file);
}
2007-03-05 16:55:23 +03:00
} else {
2008-11-02 00:42:56 +03:00
// Delete torrent from scan dir
QFile : : remove ( file ) ;
2007-03-05 16:55:23 +03:00
}
2008-12-30 03:34:41 +03:00
return h ;
2008-11-02 00:42:56 +03:00
}
add_torrent_params p ;
//Getting fast resume data if existing
std : : vector < char > buf ;
2009-07-12 12:13:00 +04:00
qDebug ( " Trying to load fastresume data: %s " , ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) ) ;
if ( load_file ( ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + QString ( " .fastresume " ) ) . toLocal8Bit ( ) . data ( ) , buf ) = = 0 ) {
2008-11-02 00:42:56 +03:00
fastResume = true ;
p . resume_data = & buf ;
2008-11-02 02:58:53 +03:00
qDebug ( " Successfuly loaded " ) ;
2008-11-02 00:42:56 +03:00
}
QString savePath = getSavePath ( hash ) ;
// Save save_path to hard drive
QFile savepath_file ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " BT_backup " ) + QDir : : separator ( ) + hash + QString : : fromUtf8 ( " .savepath " ) ) ;
if ( ! savepath_file . exists ( ) ) {
2008-10-04 18:01:55 +04:00
savepath_file . open ( QIODevice : : WriteOnly | QIODevice : : Text ) ;
2009-07-12 12:13:00 +04:00
savepath_file . write ( savePath . toLocal8Bit ( ) ) ;
2008-10-04 18:01:55 +04:00
savepath_file . close ( ) ;
2008-11-02 00:42:56 +03:00
}
2009-03-08 19:26:02 +03:00
if ( defaultTempPath . isEmpty ( ) ) {
2009-07-12 12:13:00 +04:00
p . save_path = savePath . toLocal8Bit ( ) . data ( ) ;
2009-03-08 19:26:02 +03:00
} else {
2009-07-12 12:13:00 +04:00
p . save_path = defaultTempPath . toLocal8Bit ( ) . data ( ) ;
2009-03-08 19:26:02 +03:00
}
2008-11-02 00:42:56 +03:00
p . ti = t ;
// Preallocate all?
if ( preAllocateAll )
p . storage_mode = storage_mode_allocate ;
else
p . storage_mode = storage_mode_sparse ;
// Start in pause
p . paused = true ;
p . duplicate_is_error = false ; // Already checked
2008-12-27 16:04:06 +03:00
p . auto_managed = false ; // Because it is added in paused state
2008-11-02 00:42:56 +03:00
// Adding torrent to bittorrent session
2008-12-28 00:25:58 +03:00
try {
h = QTorrentHandle ( s - > add_torrent ( p ) ) ;
} 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 ( ) ) {
2007-03-05 16:55:23 +03:00
// No need to keep on, it failed.
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
2007-07-22 13:47:27 +04:00
// If download from url, remove temp file
if ( ! from_url . isNull ( ) ) QFile : : remove ( file ) ;
2008-12-30 03:34:41 +03:00
return h ;
2008-11-02 00:42:56 +03:00
}
// Connections limit per torrent
h . set_max_connections ( maxConnecsPerTorrent ) ;
// Uploads limit per torrent
h . set_max_uploads ( maxUploadsPerTorrent ) ;
// Load filtered files
loadFilesPriorities ( h ) ;
// Load custom url seeds
loadWebSeeds ( hash ) ;
// Load speed limit from hard drive
loadTorrentSpeedLimits ( hash ) ;
// Load trackers
bool loaded_trackers = loadTrackerFile ( hash ) ;
// Doing this to order trackers well
if ( ! loaded_trackers ) {
2007-03-25 22:39:27 +04:00
saveTrackerFile ( hash ) ;
loadTrackerFile ( hash ) ;
2008-11-02 00:42:56 +03:00
}
QString newFile = torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .torrent " ;
if ( file ! = newFile ) {
2007-03-05 16:55:23 +03:00
// Delete file from torrentBackup directory in case it exists because
// QFile::copy() do not overwrite
QFile : : remove ( newFile ) ;
// Copy it to torrentBackup directory
QFile : : copy ( file , newFile ) ;
2008-11-02 00:42:56 +03:00
}
// Incremental download
if ( QFile : : exists ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .incremental " ) ) {
2007-09-04 08:18:51 +04:00
qDebug ( " Incremental download enabled for %s " , t - > name ( ) . c_str ( ) ) ;
2008-11-02 00:42:56 +03:00
h . set_sequential_download ( true ) ;
}
2008-12-30 03:34:41 +03:00
if ( ! addInPause & & ! fastResume ) {
2008-09-13 22:53:58 +04:00
// Start torrent because it was added in paused state
h . resume ( ) ;
2008-11-02 00:42:56 +03:00
}
// If download from url, remove temp file
if ( ! from_url . isNull ( ) ) QFile : : remove ( file ) ;
// Delete from scan dir to avoid trying to download it again
if ( fromScanDir ) {
2007-03-05 16:55:23 +03:00
QFile : : remove ( file ) ;
2008-11-02 00:42:56 +03:00
}
// Send torrent addition signal
if ( ! from_url . isNull ( ) ) {
2008-09-07 15:31:29 +04:00
if ( fastResume )
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " '%1' resumed. (fast resume) " , " '/home/y/xxx.torrent' was resumed. (fast resume) " ) . arg ( from_url ) ) ;
2008-09-07 15:31:29 +04:00
else
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " '%1' added to download list. " , " '/home/y/xxx.torrent' was added to download list. " ) . arg ( from_url ) ) ;
} else {
2008-09-07 15:31:29 +04:00
if ( fastResume )
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " '%1' resumed. (fast resume) " , " '/home/y/xxx.torrent' was resumed. (fast resume) " ) . arg ( file ) ) ;
2008-09-07 15:31:29 +04:00
else
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " '%1' added to download list. " , " '/home/y/xxx.torrent' was added to download list. " ) . arg ( file ) ) ;
2008-01-04 23:43:28 +03:00
}
2008-12-29 21:36:54 +03:00
emit addedTorrent ( h ) ;
2008-12-30 03:34:41 +03:00
return h ;
2007-03-05 16:55:23 +03:00
}
2007-04-12 23:51:39 +04:00
// Check in .priorities file if the user filtered files
2007-03-05 16:55:23 +03:00
// in this torrent.
2007-08-20 10:29:18 +04:00
bool bittorrent : : has_filtered_files ( QString hash ) const {
QFile pieces_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .priorities " ) ;
2007-03-05 16:55:23 +03:00
// Read saved file
2007-08-20 10:29:18 +04:00
if ( ! pieces_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
2007-03-05 16:55:23 +03:00
return false ;
}
2007-04-12 23:51:39 +04:00
QByteArray pieces_text = pieces_file . readAll ( ) ;
2007-03-05 16:55:23 +03:00
pieces_file . close ( ) ;
2007-04-12 23:51:39 +04:00
QList < QByteArray > pieces_priorities_list = pieces_text . split ( ' \n ' ) ;
unsigned int listSize = pieces_priorities_list . size ( ) ;
2007-08-20 10:29:18 +04:00
for ( unsigned int i = 0 ; i < listSize - 1 ; + + i ) {
2007-04-12 23:51:39 +04:00
int priority = pieces_priorities_list . at ( i ) . toInt ( ) ;
2007-08-20 10:29:18 +04:00
if ( priority < 0 | | priority > 7 ) {
2007-04-12 23:51:39 +04:00
priority = 1 ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
if ( ! priority ) {
2007-03-05 16:55:23 +03:00
return true ;
}
}
return false ;
}
2007-08-20 10:29:18 +04:00
// Set the maximum number of opened connections
void bittorrent : : setMaxConnections ( int maxConnec ) {
s - > set_max_connections ( maxConnec ) ;
}
2007-09-09 11:44:22 +04:00
void bittorrent : : setMaxConnectionsPerTorrent ( int max ) {
maxConnecsPerTorrent = max ;
// Apply this to all session torrents
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
unsigned int nbHandles = handles . size ( ) ;
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_max_connections ( max ) ;
}
}
void bittorrent : : setMaxUploadsPerTorrent ( int max ) {
maxUploadsPerTorrent = max ;
// Apply this to all session torrents
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
unsigned int nbHandles = handles . size ( ) ;
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_max_uploads ( max ) ;
}
}
2007-03-05 16:55:23 +03:00
// Return DHT state
bool bittorrent : : isDHTEnabled ( ) const {
return DHTEnabled ;
}
2007-09-08 21:07:29 +04:00
void bittorrent : : enableUPnP ( bool b ) {
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
}
}
2007-09-08 21:07:29 +04:00
void bittorrent : : enableNATPMP ( bool b ) {
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
}
}
void bittorrent : : enableLSD ( bool b ) {
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
}
}
2008-11-02 16:19:27 +03:00
void bittorrent : : loadSessionState ( ) {
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ifstream ses_state_file ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " ses_state " ) ) . toLocal8Bit ( ) . data ( )
2008-11-02 16:19:27 +03:00
, std : : ios_base : : binary ) ;
ses_state_file . unsetf ( std : : ios_base : : skipws ) ;
s - > load_state ( bdecode (
std : : istream_iterator < char > ( ses_state_file )
, std : : istream_iterator < char > ( ) ) ) ;
}
void bittorrent : : saveSessionState ( ) {
qDebug ( " Saving session state to disk... " ) ;
entry session_state = s - > state ( ) ;
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ofstream out ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " ses_state " ) ) . toLocal8Bit ( ) . data ( )
2008-11-02 16:19:27 +03:00
, std : : ios_base : : binary ) ;
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , session_state ) ;
}
2007-09-08 21:07:29 +04:00
// Enable DHT
2007-10-28 00:53:09 +04:00
bool bittorrent : : enableDHT ( bool b ) {
2007-09-08 21:07:29 +04:00
if ( b ) {
if ( ! DHTEnabled ) {
entry dht_state ;
2008-12-15 20:28:05 +03:00
QString dht_state_path = misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " dht_state " ) ;
if ( QFile : : exists ( dht_state_path ) ) {
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ifstream dht_state_file ( dht_state_path . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2008-12-15 20:28:05 +03:00
dht_state_file . unsetf ( std : : ios_base : : skipws ) ;
try {
dht_state = bdecode ( std : : istream_iterator < char > ( dht_state_file ) , std : : istream_iterator < char > ( ) ) ;
} catch ( std : : exception & ) { }
}
2007-10-28 00:53:09 +04:00
try {
2008-05-16 11:10:50 +04:00
s - > start_dht ( dht_state ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.bittorrent.com " ) , 6881 ) ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.utorrent.com " ) , 6881 ) ) ;
s - > add_dht_router ( std : : make_pair ( std : : string ( " router.bitcomet.com " ) , 6881 ) ) ;
DHTEnabled = true ;
qDebug ( " DHT enabled " ) ;
2007-10-28 00:53:09 +04:00
} 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
}
2007-08-20 10:29:18 +04:00
void bittorrent : : saveTorrentSpeedLimits ( QString hash ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Saving speedLimits file for %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-07-14 17:38:29 +04:00
int download_limit = h . download_limit ( ) ;
int upload_limit = h . upload_limit ( ) ;
QFile speeds_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .speedLimits " ) ;
2007-08-20 10:29:18 +04:00
if ( ! speeds_file . open ( QIODevice : : WriteOnly | QIODevice : : Text ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " * Error: Couldn't open speed limits file for torrent: %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-07-14 17:38:29 +04:00
return ;
}
2007-08-20 10:29:18 +04:00
speeds_file . write ( misc : : toQByteArray ( download_limit ) + QByteArray ( " " ) + misc : : toQByteArray ( upload_limit ) ) ;
2007-07-14 17:38:29 +04:00
speeds_file . close ( ) ;
}
2007-08-20 10:29:18 +04:00
void bittorrent : : loadTorrentSpeedLimits ( QString hash ) {
2009-07-12 12:13:00 +04:00
// qDebug("Loading speedLimits file for %s", hash.toLocal8Bit().data());
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-07-14 17:38:29 +04:00
QFile speeds_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .speedLimits " ) ;
2007-08-20 10:29:18 +04:00
if ( ! speeds_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
2007-07-14 17:38:29 +04:00
return ;
}
QByteArray speed_limits = speeds_file . readAll ( ) ;
speeds_file . close ( ) ;
QList < QByteArray > speeds = speed_limits . split ( ' ' ) ;
2007-08-20 10:29:18 +04:00
if ( speeds . size ( ) ! = 2 ) {
2007-07-14 17:38:29 +04:00
std : : cerr < < " Invalid .speedLimits file for " < < hash . toStdString ( ) < < ' \n ' ;
return ;
}
h . set_download_limit ( speeds . at ( 0 ) . toInt ( ) ) ;
h . set_upload_limit ( speeds . at ( 1 ) . toInt ( ) ) ;
}
2007-04-12 23:51:39 +04:00
// Read pieces priorities from .priorities file
2007-08-20 10:29:18 +04:00
// and ask QTorrentHandle to consider them
void bittorrent : : loadFilesPriorities ( QTorrentHandle & h ) {
2007-11-06 23:55:23 +03:00
qDebug ( " Applying pieces priorities " ) ;
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 16:55:23 +03:00
return ;
}
2007-08-20 10:29:18 +04:00
unsigned int nbFiles = h . num_files ( ) ;
QString hash = h . hash ( ) ;
QFile pieces_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .priorities " ) ;
2007-08-26 21:48:38 +04:00
if ( ! pieces_file . exists ( ) ) {
return ;
}
2007-03-05 16:55:23 +03:00
// Read saved file
2007-08-20 10:29:18 +04:00
if ( ! pieces_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " * Error: Couldn't open priorities file: %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-03-05 16:55:23 +03:00
return ;
}
2007-04-12 23:51:39 +04:00
QByteArray pieces_priorities = pieces_file . readAll ( ) ;
2007-03-05 16:55:23 +03:00
pieces_file . close ( ) ;
2007-04-12 23:51:39 +04:00
QList < QByteArray > pieces_priorities_list = pieces_priorities . split ( ' \n ' ) ;
2007-08-20 10:29:18 +04:00
if ( ( unsigned int ) pieces_priorities_list . size ( ) ! = nbFiles + 1 ) {
2007-07-03 13:04:04 +04:00
std : : cerr < < " * Error: Corrupted priorities file \n " ;
2007-03-05 16:55:23 +03:00
return ;
}
2007-07-04 02:39:49 +04:00
std : : vector < int > v ;
2007-08-20 10:29:18 +04:00
for ( unsigned int i = 0 ; i < nbFiles ; + + i ) {
2007-04-12 23:51:39 +04:00
int priority = pieces_priorities_list . at ( i ) . toInt ( ) ;
2007-08-20 10:29:18 +04:00
if ( priority < 0 | | priority > 7 ) {
2007-04-12 23:51:39 +04:00
priority = 1 ;
2007-03-05 16:55:23 +03:00
}
2007-12-30 21:06:51 +03:00
qDebug ( " Setting piece piority to %d " , priority ) ;
2007-07-04 02:39:49 +04:00
v . push_back ( priority ) ;
2007-03-05 16:55:23 +03:00
}
2007-07-04 02:39:49 +04:00
h . prioritize_files ( v ) ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
float bittorrent : : getRealRatio ( QString hash ) const {
QTorrentHandle h = getTorrentHandle ( hash ) ;
2008-11-02 01:07:14 +03:00
Q_ASSERT ( h . all_time_download ( ) > = 0 ) ;
Q_ASSERT ( h . all_time_upload ( ) > = 0 ) ;
if ( h . all_time_download ( ) = = 0 ) {
2009-07-12 12:21:06 +04:00
if ( h . all_time_upload ( ) = = 0 )
return 0 ;
return 101 ;
2008-11-02 01:07:14 +03:00
}
float ratio = ( float ) h . all_time_upload ( ) / ( float ) h . all_time_download ( ) ;
2007-08-20 10:29:18 +04:00
Q_ASSERT ( ratio > = 0. ) ;
2009-01-23 22:26:22 +03:00
if ( ratio > 100. )
ratio = 100. ;
2007-08-20 10:29:18 +04:00
return ratio ;
}
2007-11-23 00:50:22 +03:00
// Only save fast resume data for unfinished and unpaused torrents (Optimization)
// Called periodically and on exit
2008-11-02 01:07:14 +03:00
void bittorrent : : saveFastResumeData ( ) {
2008-11-02 02:58:53 +03:00
// Stop listening for alerts
timerAlerts - > stop ( ) ;
int num_resume_data = 0 ;
2008-12-30 00:10:31 +03:00
// Pause session
2008-11-02 02:58:53 +03:00
s - > pause ( ) ;
std : : vector < torrent_handle > torrents = s - > get_torrents ( ) ;
2008-12-30 00:10:31 +03:00
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
2009-01-06 00:19:55 +03:00
if ( ! h . is_valid ( ) | | ! h . has_metadata ( ) ) continue ;
if ( isQueueingEnabled ( ) )
saveTorrentPriority ( h . hash ( ) , h . queue_position ( ) ) ;
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 ) {
2008-11-02 02:58:53 +03:00
alert const * a = s - > wait_for_alert ( seconds ( 30 ) ) ;
2008-12-30 00:10:31 +03:00
if ( a = = 0 ) {
2008-11-02 02:58:53 +03:00
std : : cerr < < " aborting with " < < num_resume_data < < " outstanding "
" torrents to save resume data for " < < std : : endl ;
break ;
}
2008-12-30 00:10:31 +03:00
// Saving fastresume data can fail
2008-12-30 00:22:26 +03:00
save_resume_data_failed_alert const * rda = dynamic_cast < save_resume_data_failed_alert const * > ( a ) ;
if ( rda ) {
2008-12-30 00:10:31 +03:00
- - num_resume_data ;
s - > pop_alert ( ) ;
2008-12-30 00:22:26 +03:00
// Remove torrent from session
s - > remove_torrent ( rda - > handle ) ;
2008-12-30 00:10:31 +03:00
continue ;
}
2008-11-02 02:58:53 +03:00
save_resume_data_alert const * rd = dynamic_cast < save_resume_data_alert const * > ( a ) ;
if ( ! rd ) {
s - > pop_alert ( ) ;
continue ;
}
2008-12-30 00:10:31 +03:00
// Saving fast resume data was successful
2008-11-02 02:58:53 +03:00
- - num_resume_data ;
if ( ! rd - > resume_data ) continue ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QTorrentHandle h ( rd - > handle ) ;
2008-12-30 00:10:31 +03:00
// Remove old fastresume file if it exists
2008-11-02 02:58:53 +03:00
QFile : : remove ( torrentBackup . path ( ) + QDir : : separator ( ) + h . hash ( ) + " .fastresume " ) ;
QString file = h . hash ( ) + " .fastresume " ;
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ofstream out ( fs : : path ( torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) ) / file . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2008-11-02 02:58:53 +03:00
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , * rd - > resume_data ) ;
2008-12-30 00:22:26 +03:00
// Remove torrent from session
s - > remove_torrent ( rd - > handle ) ;
2008-11-02 02:58:53 +03:00
s - > pop_alert ( ) ;
2008-08-26 10:39:57 +04:00
}
2007-03-05 16:55:23 +03:00
}
2008-09-07 15:31:29 +04:00
QStringList bittorrent : : getConsoleMessages ( ) const {
return consoleMessages ;
}
QStringList bittorrent : : getPeerBanMessages ( ) const {
return peerBanMessages ;
}
void bittorrent : : addConsoleMessage ( QString msg , QColor color ) {
if ( consoleMessages . size ( ) > 100 ) {
consoleMessages . removeFirst ( ) ;
}
2009-07-12 13:08:38 +04:00
consoleMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - <font color=' " ) + color . name ( ) + QString : : fromUtf8 ( " '><i> " ) + msg + QString : : fromUtf8 ( " </i></font> " ) ) ;
2008-09-07 15:31:29 +04:00
}
void bittorrent : : addPeerBanMessage ( QString ip , bool from_ipfilter ) {
if ( peerBanMessages . size ( ) > 100 ) {
peerBanMessages . removeFirst ( ) ;
}
if ( from_ipfilter )
2009-07-12 13:08:38 +04:00
peerBanMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - " ) + tr ( " <font color='red'>%1</font> <i>was blocked due to your IP filter</i> " , " x.y.z.w was blocked " ) . arg ( ip ) ) ;
2008-09-07 15:31:29 +04:00
else
2009-07-12 13:08:38 +04:00
peerBanMessages . append ( QString : : fromUtf8 ( " <font color='grey'> " ) + QDateTime : : currentDateTime ( ) . toString ( QString : : fromUtf8 ( " dd/MM/yyyy hh:mm:ss " ) ) + QString : : fromUtf8 ( " </font> - " ) + tr ( " <font color='red'>%1</font> <i>was banned due to corrupt pieces</i> " , " x.y.z.w was banned " ) . arg ( ip ) ) ;
2008-09-07 15:31:29 +04:00
}
2007-07-22 13:47:27 +04:00
bool bittorrent : : isFilePreviewPossible ( QString hash ) const {
2007-03-05 16:55:23 +03:00
// See if there are supported files in the torrent
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
2007-03-05 20:35:38 +03:00
return false ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
unsigned int nbFiles = h . num_files ( ) ;
for ( unsigned int i = 0 ; i < nbFiles ; + + i ) {
QString fileName = h . file_at ( i ) ;
2007-08-29 20:07:37 +04:00
QString extension = fileName . split ( ' . ' ) . last ( ) ;
if ( misc : : isPreviewable ( extension ) )
2007-03-05 16:55:23 +03:00
return true ;
}
return false ;
}
// Scan the first level of the directory for torrent files
// and add them to download list
2008-12-26 16:17:08 +03:00
void bittorrent : : scanDirectory ( QString scan_dir ) {
FSMutex - > lock ( ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " Scanning directory: %s " , scan_dir . toLocal8Bit ( ) . data ( ) ) ;
2008-12-26 16:17:08 +03:00
QDir dir ( scan_dir ) ;
QStringList filters ;
filters < < " *.torrent " ;
QStringList files = dir . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & file , files ) {
2007-03-05 16:55:23 +03:00
QString fullPath = dir . path ( ) + QDir : : separator ( ) + file ;
2008-12-26 16:17:08 +03:00
QFile torrent ( fullPath ) ;
if ( torrent . size ( ) ! = 0 ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Adding for scan_dir: %s " , fullPath . toLocal8Bit ( ) . data ( ) ) ;
2008-12-26 16:17:08 +03:00
addTorrent ( fullPath , true ) ;
} else {
2009-07-12 12:13:00 +04:00
qDebug ( " Ignoring empty file: %s " , fullPath . toLocal8Bit ( ) . data ( ) ) ;
2008-12-26 16:17:08 +03:00
}
2007-03-05 16:55:23 +03:00
}
2008-12-26 16:17:08 +03:00
FSMutex - > unlock ( ) ;
2007-03-05 16:55:23 +03:00
}
2007-08-20 10:29:18 +04:00
void bittorrent : : setDefaultSavePath ( QString savepath ) {
2007-03-08 01:36:01 +03:00
defaultSavePath = savepath ;
}
2009-03-08 19:26:02 +03:00
void bittorrent : : setDefaultTempPath ( QString temppath ) {
if ( defaultTempPath = = temppath )
return ;
if ( temppath . isEmpty ( ) ) {
// Disabling temp dir
// Moving all torrents to their destination folder
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
h . move_storage ( getSavePath ( h . hash ( ) ) ) ;
}
} else {
std : : vector < torrent_handle > torrents = getTorrents ( ) ;
std : : vector < torrent_handle > : : iterator torrentIT ;
for ( torrentIT = torrents . begin ( ) ; torrentIT ! = torrents . end ( ) ; torrentIT + + ) {
QTorrentHandle h = QTorrentHandle ( * torrentIT ) ;
if ( ! h . is_valid ( ) ) continue ;
2009-03-17 20:38:41 +03:00
if ( ! h . is_seed ( ) )
h . move_storage ( temppath ) ;
2009-03-08 19:26:02 +03:00
}
}
defaultTempPath = temppath ;
}
2007-03-05 16:55:23 +03:00
// Enable directory scanning
2008-12-26 16:17:08 +03:00
void bittorrent : : enableDirectoryScanning ( QString scan_dir ) {
if ( ! scan_dir . isEmpty ( ) ) {
2008-12-27 15:12:13 +03:00
if ( FSWatcher = = 0 ) {
FSMutex = new QMutex ( ) ;
FSWatcher = new QFileSystemWatcher ( QStringList ( scan_dir ) , this ) ;
connect ( FSWatcher , SIGNAL ( directoryChanged ( QString ) ) , this , SLOT ( scanDirectory ( QString ) ) ) ;
// Initial scan
scanDirectory ( scan_dir ) ;
} else {
QString old_scan_dir = FSWatcher - > directories ( ) . first ( ) ;
if ( old_scan_dir ! = scan_dir ) {
FSWatcher - > removePath ( old_scan_dir ) ;
FSWatcher - > addPath ( scan_dir ) ;
// Initial scan
scanDirectory ( scan_dir ) ;
}
}
2007-03-05 16:55:23 +03:00
}
}
// Disable directory scanning
2007-08-20 10:29:18 +04:00
void bittorrent : : disableDirectoryScanning ( ) {
2008-12-26 16:17:08 +03:00
if ( FSWatcher ) {
delete FSWatcher ;
delete FSMutex ;
2007-03-05 16:55:23 +03:00
}
}
// Set the ports range in which is chosen the port the bittorrent
// session will listen to
2007-08-20 10:29:18 +04:00
void bittorrent : : setListeningPortsRange ( std : : pair < unsigned short , unsigned short > ports ) {
2007-03-05 16:55:23 +03:00
s - > listen_on ( ports ) ;
}
// Set download rate limit
// -1 to disable
2007-08-20 10:29:18 +04:00
void bittorrent : : setDownloadRateLimit ( long rate ) {
2007-08-04 10:23:44 +04:00
qDebug ( " Setting a global download rate limit at %ld " , rate ) ;
2007-03-05 16:55:23 +03:00
s - > set_download_rate_limit ( rate ) ;
}
2007-07-14 14:50:38 +04:00
session * bittorrent : : getSession ( ) const {
return s ;
}
2007-03-05 16:55:23 +03:00
// Set upload rate limit
// -1 to disable
2007-08-20 10:29:18 +04:00
void bittorrent : : setUploadRateLimit ( long rate ) {
2007-08-08 11:35:57 +04:00
qDebug ( " set upload_limit to %fkb/s " , rate / 1024. ) ;
2007-03-05 16:55:23 +03:00
s - > set_upload_rate_limit ( rate ) ;
}
// libtorrent allow to adjust ratio for each torrent
// This function will apply to same ratio to all torrents
2007-08-20 10:29:18 +04:00
void bittorrent : : setGlobalRatio ( float ratio ) {
2007-09-09 13:09:24 +04:00
if ( ratio ! = - 1 & & ratio < 1. ) ratio = 1. ;
2007-09-16 18:16:42 +04:00
if ( ratio = = - 1 ) {
// 0 means unlimited for libtorrent
ratio = 0 ;
}
2007-03-05 16:55:23 +03:00
std : : vector < torrent_handle > handles = s - > get_torrents ( ) ;
2007-07-31 13:17:59 +04:00
unsigned int nbHandles = handles . size ( ) ;
2007-08-20 10:29:18 +04:00
for ( unsigned int i = 0 ; i < nbHandles ; + + i ) {
QTorrentHandle h = handles [ i ] ;
if ( ! h . is_valid ( ) ) {
2007-03-11 21:01:34 +03:00
qDebug ( " /! \\ Error: Invalid handle " ) ;
continue ;
}
h . set_ratio ( ratio ) ;
2007-03-05 16:55:23 +03:00
}
}
2007-03-05 20:35:38 +03:00
2007-09-09 13:09:24 +04:00
// Torrents will a ratio superior to the given value will
// be automatically deleted
void bittorrent : : setDeleteRatio ( float ratio ) {
if ( ratio ! = - 1 & & ratio < 1. ) ratio = 1. ;
2009-07-12 11:23:11 +04:00
if ( ratio_limit = = - 1 & & ratio ! = - 1 ) {
2007-11-25 00:55:19 +03:00
Q_ASSERT ( ! BigRatioTimer ) ;
BigRatioTimer = new QTimer ( this ) ;
connect ( BigRatioTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( deleteBigRatios ( ) ) ) ;
BigRatioTimer - > start ( 5000 ) ;
} else {
2009-07-12 11:23:11 +04:00
if ( ratio_limit ! = - 1 & & ratio = = - 1 ) {
2007-11-25 00:55:19 +03:00
delete BigRatioTimer ;
}
}
2009-07-12 11:23:11 +04:00
if ( ratio_limit ! = ratio ) {
ratio_limit = ratio ;
qDebug ( " * Set deleteRatio to %.1f " , ratio_limit ) ;
2007-11-25 00:55:19 +03:00
deleteBigRatios ( ) ;
}
2007-09-09 13:09:24 +04:00
}
2007-08-20 10:29:18 +04:00
bool bittorrent : : loadTrackerFile ( QString hash ) {
2007-03-25 20:18:57 +04:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QFile tracker_file ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .trackers " ) ;
2007-03-25 22:39:27 +04:00
if ( ! tracker_file . exists ( ) ) return false ;
2007-03-25 20:18:57 +04:00
tracker_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
2007-08-20 10:29:18 +04:00
QStringList lines = QString : : fromUtf8 ( tracker_file . readAll ( ) . data ( ) ) . split ( " \n " ) ;
2007-03-25 20:18:57 +04:00
std : : vector < announce_entry > trackers ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & line , lines ) {
2007-03-25 20:18:57 +04:00
QStringList parts = line . split ( " | " ) ;
if ( parts . size ( ) ! = 2 ) continue ;
announce_entry t ( parts [ 0 ] . toStdString ( ) ) ;
t . tier = parts [ 1 ] . toInt ( ) ;
trackers . push_back ( t ) ;
}
2008-09-13 11:33:41 +04:00
if ( ! trackers . empty ( ) ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-03-25 20:18:57 +04:00
h . replace_trackers ( trackers ) ;
2008-04-13 18:15:19 +04:00
h . force_reannounce ( ) ;
2007-03-25 22:39:27 +04:00
return true ;
} else {
return false ;
2007-03-25 20:18:57 +04:00
}
}
2007-08-20 10:29:18 +04:00
void bittorrent : : saveTrackerFile ( QString hash ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Saving tracker file for %s " , hash . toLocal8Bit ( ) . data ( ) ) ;
2007-03-25 20:18:57 +04:00
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QFile tracker_file ( torrentBackup . path ( ) + QDir : : separator ( ) + hash + " .trackers " ) ;
2007-08-20 10:29:18 +04:00
if ( tracker_file . exists ( ) ) {
2007-03-25 20:18:57 +04:00
tracker_file . remove ( ) ;
}
tracker_file . open ( QIODevice : : WriteOnly | QIODevice : : Text ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = getTorrentHandle ( hash ) ;
2007-03-25 20:18:57 +04:00
std : : vector < announce_entry > trackers = h . trackers ( ) ;
2007-08-20 10:29:18 +04:00
for ( unsigned int i = 0 ; i < trackers . size ( ) ; + + i ) {
2007-03-25 22:39:27 +04:00
tracker_file . write ( QByteArray ( trackers [ i ] . url . c_str ( ) ) + QByteArray ( " | " ) + QByteArray ( misc : : toString ( i ) . c_str ( ) ) + QByteArray ( " \n " ) ) ;
2007-03-25 20:18:57 +04:00
}
tracker_file . close ( ) ;
}
2009-07-14 11:50:00 +04:00
// Set DHT port (>= 1000 or 0 if same as BT)
2007-08-20 10:29:18 +04:00
void bittorrent : : setDHTPort ( int dht_port ) {
2009-07-14 11:50:00 +04:00
if ( dht_port = = 0 or dht_port > = 1000 ) {
2007-03-05 20:35:38 +03:00
struct dht_settings DHTSettings ;
DHTSettings . service_port = dht_port ;
s - > set_dht_settings ( DHTSettings ) ;
qDebug ( " Set DHT Port to %d " , dht_port ) ;
}
}
// Enable IP Filtering
2008-05-18 00:32:03 +04:00
void bittorrent : : enableIPFilter ( QString filter ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Enabling IPFiler " ) ;
2008-05-18 00:32:03 +04:00
if ( ! filterParser ) {
filterParser = new FilterParserThread ( this , s ) ;
}
if ( filterPath . isEmpty ( ) | | filterPath ! = filter ) {
filterPath = filter ;
filterParser - > processFilterFile ( filter ) ;
}
2007-03-05 20:35:38 +03:00
}
// Disable IP Filtering
2007-08-20 10:29:18 +04:00
void bittorrent : : disableIPFilter ( ) {
qDebug ( " Disabling IPFilter " ) ;
2007-03-05 20:35:38 +03:00
s - > set_ip_filter ( ip_filter ( ) ) ;
2008-05-18 00:32:03 +04:00
if ( filterParser ) {
delete filterParser ;
}
filterPath = " " ;
2007-03-05 20:35:38 +03:00
}
2007-05-02 17:52:29 +04:00
// Set BT session settings (user_agent)
2007-08-20 10:29:18 +04:00
void bittorrent : : setSessionSettings ( session_settings sessionSettings ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Set session settings " ) ;
2007-03-05 20:35:38 +03:00
s - > set_settings ( sessionSettings ) ;
}
2007-05-02 17:52:29 +04:00
// Set Proxy
2007-08-20 10:29:18 +04:00
void bittorrent : : setProxySettings ( proxy_settings proxySettings , bool trackers , bool peers , bool web_seeds , bool dht ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Set Proxy settings " ) ;
2009-01-04 01:49:21 +03:00
proxy_settings ps_null ;
ps_null . type = proxy_settings : : none ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting trackers proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( trackers )
s - > set_tracker_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_tracker_proxy ( ps_null ) ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting peers proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( peers )
s - > set_peer_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_peer_proxy ( ps_null ) ;
2009-01-04 18:37:13 +03:00
qDebug ( " Setting web seeds proxy " ) ;
2007-05-14 00:02:30 +04:00
if ( web_seeds )
s - > set_web_seed_proxy ( proxySettings ) ;
2009-01-04 01:49:21 +03:00
else
s - > set_web_seed_proxy ( ps_null ) ;
if ( DHTEnabled ) {
2009-01-06 23:38:52 +03:00
qDebug ( " Setting DHT proxy, %d " , dht ) ;
2009-01-04 01:49:21 +03:00
if ( dht )
s - > set_dht_proxy ( proxySettings ) ;
else
s - > set_dht_proxy ( ps_null ) ;
2007-05-14 00:02:30 +04:00
}
2007-05-02 17:52:29 +04:00
}
2007-03-05 20:35:38 +03:00
// Read alerts sent by the bittorrent session
2007-08-20 10:29:18 +04:00
void bittorrent : : readAlerts ( ) {
2007-03-05 20:35:38 +03:00
// look at session alerts and display some infos
std : : auto_ptr < alert > a = s - > pop_alert ( ) ;
2007-08-20 10:29:18 +04:00
while ( a . get ( ) ) {
if ( torrent_finished_alert * p = dynamic_cast < torrent_finished_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
2008-12-27 00:26:06 +03:00
emit finishedTorrent ( h ) ;
2007-08-27 13:24:22 +04:00
QString hash = h . hash ( ) ;
2008-12-27 16:04:06 +03:00
// Create .finished file if necessary
2008-12-27 00:18:33 +03:00
QFile finished_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .finished " ) ;
finished_file . open ( QIODevice : : WriteOnly | QIODevice : : Text ) ;
finished_file . close ( ) ;
2009-03-08 19:26:02 +03:00
// Move to download directory if necessary
if ( ! defaultTempPath . isEmpty ( ) ) {
// Check if directory is different
QDir current_dir ( h . save_path ( ) ) ;
QDir save_dir ( getSavePath ( h . hash ( ) ) ) ;
if ( current_dir ! = save_dir ) {
h . move_storage ( save_dir . path ( ) ) ;
}
}
2009-03-31 02:01:33 +04:00
h . save_resume_data ( ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " Received finished alert for %s " , h . name ( ) . toLocal8Bit ( ) . data ( ) ) ;
2007-08-27 13:24:22 +04:00
}
2007-03-05 20:35:38 +03:00
}
2008-11-02 00:42:56 +03:00
else if ( save_resume_data_alert * p = dynamic_cast < save_resume_data_alert * > ( a . get ( ) ) ) {
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
QTorrentHandle h ( p - > handle ) ;
2008-11-02 02:58:53 +03:00
QString file = h . hash ( ) + " .fastresume " ;
2009-01-04 18:37:13 +03:00
// Delete old fastresume file if necessary
if ( QFile : : exists ( file ) )
QFile : : remove ( file ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " Saving fastresume data in %s " , file . toLocal8Bit ( ) . data ( ) ) ;
2008-11-02 00:42:56 +03:00
if ( p - > resume_data )
{
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ofstream out ( fs : : path ( torrentBackup . path ( ) . toLocal8Bit ( ) . data ( ) ) / file . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2008-11-02 00:42:56 +03:00
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , * p - > resume_data ) ;
}
}
2007-08-20 10:29:18 +04:00
else if ( file_error_alert * p = dynamic_cast < file_error_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2009-01-15 18:54:03 +03:00
h . auto_managed ( false ) ;
2009-03-27 00:08:10 +03:00
std : : cerr < < " File Error: " < < p - > message ( ) . c_str ( ) < < std : : endl ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) )
2009-04-18 00:04:09 +04:00
emit fullDiskError ( h , misc : : toQString ( p - > message ( ) ) ) ;
2007-03-05 20:35:38 +03:00
}
2007-08-20 10:29:18 +04:00
else if ( dynamic_cast < listen_failed_alert * > ( a . get ( ) ) ) {
2007-03-05 20:35:38 +03:00
// Level: fatal
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Couldn't listen on any of the given ports. " ) , QString : : fromUtf8 ( " red " ) ) ;
//emit portListeningFailure();
2007-03-05 20:35:38 +03:00
}
2008-11-02 00:42:56 +03:00
else if ( tracker_error_alert * p = dynamic_cast < tracker_error_alert * > ( a . get ( ) ) ) {
2007-03-05 20:35:38 +03:00
// Level: fatal
2007-08-20 10:29:18 +04:00
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
// Authentication
2008-05-15 23:48:15 +04:00
if ( p - > status_code ! = 401 ) {
QString hash = h . hash ( ) ;
2008-05-17 02:27:36 +04:00
qDebug ( " Received a tracker error for %s " , p - > url . c_str ( ) ) ;
2008-05-15 23:48:15 +04:00
QHash < QString , QString > errors = trackersErrors . value ( hash , QHash < QString , QString > ( ) ) ;
// p->url requires at least libtorrent v0.13.1
2008-11-02 00:42:56 +03:00
errors [ misc : : toQString ( p - > url ) ] = QString : : fromUtf8 ( a - > message ( ) . c_str ( ) ) ;
2008-05-15 23:48:15 +04:00
trackersErrors [ hash ] = errors ;
} else {
2007-08-27 13:24:22 +04:00
emit trackerAuthenticationRequired ( h ) ;
}
2007-03-05 20:35:38 +03:00
}
}
2008-05-15 23:48:15 +04:00
else if ( tracker_reply_alert * p = dynamic_cast < tracker_reply_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
if ( h . is_valid ( ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Received a tracker reply from %s " , ( const char * ) h . current_tracker ( ) . toLocal8Bit ( ) ) ;
2008-05-15 23:48:15 +04:00
QString hash = h . hash ( ) ;
QHash < QString , QString > errors = trackersErrors . value ( hash , QHash < QString , QString > ( ) ) ;
// p->url requires at least libtorrent v0.13.1
errors . remove ( h . current_tracker ( ) ) ;
trackersErrors [ hash ] = errors ;
}
}
2007-12-31 19:57:35 +03:00
else if ( portmap_error_alert * p = dynamic_cast < portmap_error_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping failure, message: %1 " ) . arg ( QString ( p - > message ( ) . c_str ( ) ) ) , QColor ( " red " ) ) ;
2008-09-07 15:31:29 +04:00
//emit UPnPError(QString(p->msg().c_str()));
2007-12-31 19:57:35 +03:00
}
else if ( portmap_alert * p = dynamic_cast < portmap_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
qDebug ( " UPnP Success, msg: %s " , p - > message ( ) . c_str ( ) ) ;
addConsoleMessage ( tr ( " UPnP/NAT-PMP: Port mapping successful, message: %1 " ) . arg ( QString ( p - > message ( ) . c_str ( ) ) ) , QColor ( " blue " ) ) ;
2008-09-07 15:31:29 +04:00
//emit UPnPSuccess(QString(p->msg().c_str()));
2007-12-31 19:57:35 +03:00
}
2007-08-20 10:29:18 +04:00
else if ( peer_blocked_alert * p = dynamic_cast < peer_blocked_alert * > ( a . get ( ) ) ) {
2008-09-07 15:31:29 +04:00
addPeerBanMessage ( QString ( p - > ip . to_string ( ) . c_str ( ) ) , true ) ;
//emit peerBlocked(QString::fromUtf8(p->ip.to_string().c_str()));
}
else if ( peer_ban_alert * p = dynamic_cast < peer_ban_alert * > ( a . get ( ) ) ) {
addPeerBanMessage ( QString ( p - > ip . address ( ) . to_string ( ) . c_str ( ) ) , false ) ;
//emit peerBlocked(QString::fromUtf8(p->ip.to_string().c_str()));
2007-07-04 10:42:36 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( fastresume_rejected_alert * p = dynamic_cast < fastresume_rejected_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " /! \\ Fast resume failed for %s, reason: %s " , h . name ( ) . toLocal8Bit ( ) . data ( ) , p - > message ( ) . c_str ( ) ) ;
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Fast resume data was rejected for torrent %1, checking again... " ) . arg ( h . name ( ) ) , QString : : fromUtf8 ( " red " ) ) ;
//emit fastResumeDataRejected(h.name());
2007-08-27 13:24:22 +04:00
}
2007-07-24 11:13:40 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( url_seed_alert * p = dynamic_cast < url_seed_alert * > ( a . get ( ) ) ) {
2008-11-02 00:42:56 +03:00
addConsoleMessage ( tr ( " Url seed lookup failed for url: %1, message: %2 " ) . arg ( QString : : fromUtf8 ( p - > url . c_str ( ) ) ) . arg ( QString : : fromUtf8 ( p - > message ( ) . c_str ( ) ) ) , QString : : fromUtf8 ( " red " ) ) ;
2008-09-07 15:31:29 +04:00
//emit urlSeedProblem(QString::fromUtf8(p->url.c_str()), QString::fromUtf8(p->msg().c_str()));
2007-07-27 13:53:55 +04:00
}
2007-08-20 10:29:18 +04:00
else if ( torrent_checked_alert * p = dynamic_cast < torrent_checked_alert * > ( a . get ( ) ) ) {
QTorrentHandle h ( p - > handle ) ;
2007-08-27 13:24:22 +04:00
if ( h . is_valid ( ) ) {
QString hash = h . hash ( ) ;
2009-07-12 12:13:00 +04:00
qDebug ( " %s have just finished checking " , hash . toLocal8Bit ( ) . data ( ) ) ;
2008-12-27 01:11:40 +03:00
emit torrentFinishedChecking ( h ) ;
2007-08-17 06:03:13 +04:00
}
}
2007-03-05 20:35:38 +03:00
a = s - > pop_alert ( ) ;
}
}
2008-05-15 23:48:15 +04:00
QHash < QString , QString > bittorrent : : getTrackersErrors ( QString hash ) const {
return trackersErrors . value ( hash , QHash < QString , QString > ( ) ) ;
2007-07-27 17:58:12 +04:00
}
2007-03-08 01:36:01 +03:00
int bittorrent : : getListenPort ( ) const {
return s - > listen_port ( ) ;
}
session_status bittorrent : : getSessionStatus ( ) const {
return s - > status ( ) ;
}
2007-08-20 10:29:18 +04:00
QString bittorrent : : getSavePath ( QString hash ) {
2007-03-05 20:35:38 +03:00
QFile savepath_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .savepath " ) ;
QByteArray line ;
QString savePath ;
2007-08-20 10:29:18 +04:00
if ( savepath_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
2007-03-05 20:35:38 +03:00
line = savepath_file . readAll ( ) ;
savepath_file . close ( ) ;
2007-08-26 22:41:45 +04:00
qDebug ( " -> Save path: %s " , line . data ( ) ) ;
2007-03-05 20:35:38 +03:00
savePath = QString : : fromUtf8 ( line . data ( ) ) ;
} else {
2007-03-08 01:36:01 +03:00
// use default save path
2007-11-06 20:40:44 +03:00
qDebug ( " Using default save path because none was set " ) ;
2007-03-08 01:36:01 +03:00
savePath = defaultSavePath ;
2007-03-05 20:35:38 +03:00
}
// Checking if savePath Dir exists
// create it if it is not
QDir saveDir ( savePath ) ;
2007-08-20 10:29:18 +04:00
if ( ! saveDir . exists ( ) ) {
if ( ! saveDir . mkpath ( saveDir . path ( ) ) ) {
2009-07-12 12:13:00 +04:00
std : : cerr < < " Couldn't create the save directory: " < < saveDir . path ( ) . toLocal8Bit ( ) . data ( ) < < " \n " ;
2007-03-08 01:36:01 +03:00
// XXX: handle this better
2007-03-05 20:35:38 +03:00
return QDir : : homePath ( ) ;
}
}
return savePath ;
}
2007-03-08 01:36:01 +03:00
// Take an url string to a torrent file,
// download the torrent file to a tmp location, then
// add it to download list
2007-08-20 10:29:18 +04:00
void bittorrent : : downloadFromUrl ( QString url ) {
2008-09-07 15:31:29 +04:00
addConsoleMessage ( tr ( " Downloading '%1', please wait... " , " e.g: Downloading 'xxx.torrent', please wait... " ) . arg ( url ) , QPalette : : WindowText ) ;
//emit aboutToDownloadFromUrl(url);
2007-03-08 01:36:01 +03:00
// Launch downloader thread
downloader - > downloadUrl ( url ) ;
}
2008-09-14 14:14:54 +04:00
void bittorrent : : downloadUrlAndSkipDialog ( QString url ) {
//emit aboutToDownloadFromUrl(url);
url_skippingDlg < < url ;
// Launch downloader thread
downloader - > downloadUrl ( url ) ;
}
2007-03-08 01:36:01 +03:00
// Add to bittorrent session the downloaded torrent file
2007-08-20 10:29:18 +04:00
void bittorrent : : processDownloadedFile ( QString url , QString file_path ) {
2008-09-14 14:14:54 +04:00
int index = url_skippingDlg . indexOf ( url ) ;
if ( index < 0 ) {
// Add file to torrent download list
emit newDownloadedTorrent ( file_path , url ) ;
} else {
url_skippingDlg . removeAt ( index ) ;
addTorrent ( file_path , false , url , false ) ;
}
2007-03-08 01:36:01 +03:00
}
2007-08-20 10:29:18 +04:00
void bittorrent : : downloadFromURLList ( const QStringList & url_list ) {
2007-07-03 13:04:04 +04:00
qDebug ( " DownloadFromUrlList " ) ;
2009-01-24 22:20:09 +03:00
foreach ( const QString url , url_list ) {
2007-03-08 01:36:01 +03:00
downloadFromUrl ( url ) ;
}
}
// Return current download rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
float bittorrent : : getPayloadDownloadRate ( ) const {
session_status sessionStatus = s - > status ( ) ;
return sessionStatus . payload_download_rate ;
}
// Return current upload rate for the BT
// session. Payload means that it only take into
// account "useful" part of the rate
float bittorrent : : getPayloadUploadRate ( ) const {
session_status sessionStatus = s - > status ( ) ;
return sessionStatus . payload_upload_rate ;
}
// Save DHT entry to hard drive
2007-08-20 10:29:18 +04:00
void bittorrent : : saveDHTEntry ( ) {
2007-03-08 01:36:01 +03:00
// Save DHT entry
2007-08-20 10:29:18 +04:00
if ( DHTEnabled ) {
2007-03-08 01:36:01 +03:00
try {
entry dht_state = s - > dht_state ( ) ;
2009-07-12 12:13:00 +04:00
boost : : filesystem : : ofstream out ( ( misc : : qBittorrentPath ( ) + QString : : fromUtf8 ( " dht_state " ) ) . toLocal8Bit ( ) . data ( ) , std : : ios_base : : binary ) ;
2007-03-08 01:36:01 +03:00
out . unsetf ( std : : ios_base : : skipws ) ;
bencode ( std : : ostream_iterator < char > ( out ) , dht_state ) ;
2007-07-29 18:54:39 +04:00
qDebug ( " DHT entry saved " ) ;
2007-08-20 10:29:18 +04:00
} catch ( std : : exception & e ) {
2007-03-08 01:36:01 +03:00
std : : cerr < < e . what ( ) < < " \n " ;
}
}
}
2007-08-20 10:29:18 +04:00
void bittorrent : : applyEncryptionSettings ( pe_settings se ) {
2007-06-16 03:02:35 +04:00
qDebug ( " Applying encryption settings " ) ;
2007-06-16 00:35:07 +04:00
s - > set_pe_settings ( se ) ;
}
2009-01-06 00:19:55 +03:00
void bittorrent : : saveTorrentPriority ( QString hash , int prio ) {
// Write .queued file
QFile prio_file ( misc : : qBittorrentPath ( ) + " BT_backup " + QDir : : separator ( ) + hash + " .prio " ) ;
prio_file . open ( QIODevice : : WriteOnly | QIODevice : : Text ) ;
prio_file . write ( QByteArray : : number ( prio ) ) ;
prio_file . close ( ) ;
}
2007-10-10 21:34:52 +04:00
// Will fast resume torrents in
2007-03-08 01:36:01 +03:00
// backup directory
2007-08-20 10:29:18 +04:00
void bittorrent : : resumeUnfinishedTorrents ( ) {
2007-03-08 01:36:01 +03:00
qDebug ( " Resuming unfinished torrents " ) ;
QDir torrentBackup ( misc : : qBittorrentPath ( ) + " BT_backup " ) ;
2009-01-06 00:19:55 +03:00
QStringList fileNames ;
2007-03-08 01:36:01 +03:00
// Scan torrentBackup directory
2007-07-31 12:21:51 +04:00
QStringList filters ;
filters < < " *.torrent " ;
fileNames = torrentBackup . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
2009-01-06 00:19:55 +03:00
if ( isQueueingEnabled ( ) ) {
QList < QPair < int , QString > > filePaths ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & fileName , fileNames ) {
2009-01-06 00:19:55 +03:00
QString filePath = torrentBackup . path ( ) + QDir : : separator ( ) + fileName ;
int prio = 99999 ;
// Get priority
QString prioPath = filePath ;
prioPath . replace ( " .torrent " , " .prio " ) ;
if ( QFile : : exists ( prioPath ) ) {
QFile prio_file ( prioPath ) ;
if ( prio_file . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
bool ok = false ;
prio = prio_file . readAll ( ) . toInt ( & ok ) ;
if ( ! ok )
prio = 99999 ;
prio_file . close ( ) ;
}
}
misc : : insertSort2 < QString > ( filePaths , qMakePair ( prio , filePath ) ) ;
}
// Resume downloads
QPair < int , QString > fileName ;
foreach ( fileName , filePaths ) {
addTorrent ( fileName . second , false , QString ( ) , true ) ;
}
} else {
QStringList filePaths ;
2009-01-24 22:20:09 +03:00
foreach ( const QString & fileName , fileNames ) {
2009-01-06 00:19:55 +03:00
filePaths . append ( torrentBackup . path ( ) + QDir : : separator ( ) + fileName ) ;
}
// Resume downloads
2009-01-24 22:20:09 +03:00
foreach ( const QString & fileName , filePaths ) {
2009-01-06 00:19:55 +03:00
addTorrent ( fileName , false , QString ( ) , true ) ;
}
}
2007-03-08 01:36:01 +03:00
qDebug ( " Unfinished torrents resumed " ) ;
}