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