@ -102,6 +102,10 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
@@ -102,6 +102,10 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
m_ui - > savePath - > setDialogCaption ( tr ( " Choose save path " ) ) ;
m_ui - > savePath - > setMaxVisibleItems ( 20 ) ;
# ifdef Q_OS_MAC
setModal ( true ) ;
# endif
auto session = BitTorrent : : Session : : instance ( ) ;
if ( m_torrentParams . addPaused = = TriStateBool : : True )
@ -233,10 +237,9 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
@@ -233,10 +237,9 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
if ( Net : : DownloadManager : : hasSupportedScheme ( source ) ) {
// Launch downloader
// TODO: Don't save loaded torrent to file, just use downloaded data!
Net : : DownloadHandler * handler = Net : : DownloadManager : : instance ( ) - > download (
Net : : DownloadRequest ( source ) . limit ( 10485760 /* 10MB */ ) . handleRedirectToMagnet ( true ) . saveToFile ( true ) ) ;
connect ( handler , static_cast < void ( Net : : DownloadHandler : : * ) ( const QString & , const QString & ) > ( & Net : : DownloadHandler : : downloadFinished )
Net : : DownloadRequest ( source ) . limit ( 10485760 /* 10MB */ ) . handleRedirectToMagnet ( true ) ) ;
connect ( handler , static_cast < void ( Net : : DownloadHandler : : * ) ( const QString & , const QByteArray & ) > ( & Net : : DownloadHandler : : downloadFinished )
, dlg , & AddNewTorrentDialog : : handleDownloadFinished ) ;
connect ( handler , & Net : : DownloadHandler : : downloadFailed , dlg , & AddNewTorrentDialog : : handleDownloadFailed ) ;
connect ( handler , & Net : : DownloadHandler : : redirectedToMagnet , dlg , & AddNewTorrentDialog : : handleRedirectedToMagnet ) ;
@ -246,53 +249,42 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
@@ -246,53 +249,42 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
const BitTorrent : : MagnetUri magnetUri ( source ) ;
const bool isLoaded = magnetUri . isValid ( )
? dlg - > loadMagnet ( magnetUri )
: dlg - > loadTorrent ( source ) ;
: dlg - > loadTorrentFile ( source ) ;
if ( isLoaded ) {
# ifdef Q_OS_MAC
dlg - > exec ( ) ;
# else
if ( isLoaded )
dlg - > open ( ) ;
# endif
}
else {
else
delete dlg ;
}
}
void AddNewTorrentDialog : : show ( const QString & source , QWidget * parent )
{
show ( source , BitTorrent : : AddTorrentParams ( ) , parent ) ;
}
bool AddNewTorrentDialog : : loadTorrent ( const QString & torrentPath )
bool AddNewTorrentDialog : : loadTorrentFile ( const QString & torrentPath )
{
if ( torrentPath . startsWith ( " file:// " , Qt : : CaseInsensitive ) )
m_filePath = QUrl : : fromEncoded ( torrentPath . toLocal8Bit ( ) ) . toLocalFile ( ) ;
else
m_filePath = torrentPath ;
const QString decodedPath = torrentPath . startsWith ( " file:// " , Qt : : CaseInsensitive )
? QUrl : : fromEncoded ( torrentPath . toLocal8Bit ( ) ) . toLocalFile ( )
: torrentPath ;
if ( ! QFile : : exists ( m_filePath ) ) {
RaisedMessageBox : : critical ( this , tr ( " I/O Error " ) , tr ( " The torrent file '%1' does not exist. " ) . arg ( Utils : : Fs : : toNativePath ( m_filePath ) ) ) ;
QString error ;
m_torrentInfo = BitTorrent : : TorrentInfo : : loadFromFile ( decodedPath , & error ) ;
if ( ! m_torrentInfo . isValid ( ) ) {
RaisedMessageBox : : critical ( this , tr ( " Invalid torrent " )
, tr ( " Failed to load the torrent: %1. \n Error: %2 " , " Don't remove the ' \n ' characters. They insert a newline. " )
. arg ( Utils : : Fs : : toNativePath ( decodedPath ) , error ) ) ;
return false ;
}
QFileInfo fileinfo ( m_filePath ) ;
if ( ! fileinfo . isReadable ( ) ) {
RaisedMessageBox : : critical ( this , tr ( " I/O Error " ) , tr ( " The torrent file '%1' cannot be read from the disk. Probably you don't have enough permissions. " ) . arg ( Utils : : Fs : : toNativePath ( m_filePath ) ) ) ;
return false ;
}
m_torrentGuard . reset ( new TorrentFileGuard ( decodedPath ) ) ;
m_hasMetadata = true ;
QString error ;
m_torrentInfo = BitTorrent : : TorrentInfo : : loadFromFile ( m_filePath , & error ) ;
if ( ! m_torrentInfo . isValid ( ) ) {
RaisedMessageBox : : critical ( this , tr ( " Invalid torrent " ) , tr ( " Failed to load the torrent: %1. \n Error: %2 " , " Don't remove the ' \n ' characters. They insert a newline. " )
. arg ( Utils : : Fs : : toNativePath ( m_filePath ) , error ) ) ;
return false ;
return loadTorrentImpl ( ) ;
}
m_torrentGuard . reset ( new TorrentFileGuard ( m_filePath ) ) ;
bool AddNewTorrentDialog : : loadTorrentImpl ( )
{
m_hasMetadata = true ;
m_hash = m_torrentInfo . hash ( ) ;
// Prevent showing the dialog if download is already present
@ -779,16 +771,26 @@ void AddNewTorrentDialog::handleDownloadFailed(const QString &url, const QString
@@ -779,16 +771,26 @@ void AddNewTorrentDialog::handleDownloadFailed(const QString &url, const QString
void AddNewTorrentDialog : : handleRedirectedToMagnet ( const QString & url , const QString & magnetUri )
{
Q_UNUSED ( url )
if ( loadMagnet ( BitTorrent : : MagnetUri ( magnetUri ) ) )
open ( ) ;
else
this - > deleteLater ( ) ;
}
void AddNewTorrentDialog : : handleDownloadFinished ( const QString & url , const QString & filePath )
void AddNewTorrentDialog : : handleDownloadFinished ( const QString & url , const QByteArray & data )
{
Q_UNUSED ( url )
if ( loadTorrent ( filePath ) )
QString error ;
m_torrentInfo = BitTorrent : : TorrentInfo : : load ( data , & error ) ;
if ( ! m_torrentInfo . isValid ( ) ) {
RaisedMessageBox : : critical ( this , tr ( " Invalid torrent " ) , tr ( " Failed to load from URL: %1. \n Error: %2 " )
. arg ( url , error ) ) ;
return ;
}
m_torrentGuard . reset ( new TorrentFileGuard ) ;
if ( loadTorrentImpl ( ) )
open ( ) ;
else
this - > deleteLater ( ) ;