2006-09-30 20:02:39 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2010-10-25 23:34:42 +04:00
* Copyright ( C ) 2010 Christophe Dumez
2006-09-30 20:02:39 +04: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 .
2006-09-30 20:02:39 +04: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
2006-09-30 20:02:39 +04:00
*/
2014-10-25 15:49:57 +04:00
# include <QDebug>
2006-09-30 20:02:39 +04:00
# include <QFileDialog>
# include <QMessageBox>
2010-10-23 00:13:22 +04:00
# include "torrentcreatordlg.h"
2015-05-06 14:53:27 +03:00
# include "core/utils/fs.h"
# include "core/utils/misc.h"
2015-04-13 19:19:53 +03:00
# include "core/preferences.h"
2015-04-19 18:17:47 +03:00
# include "guiiconprovider.h"
# include "core/bittorrent/session.h"
# include "core/bittorrent/torrentinfo.h"
# include "core/bittorrent/torrentcreatorthread.h"
2006-09-30 20:02:39 +04:00
2010-10-25 23:34:42 +04:00
const uint NB_PIECES_MIN = 1200 ;
const uint NB_PIECES_MAX = 2200 ;
2008-11-02 00:42:56 +03:00
2015-06-24 13:19:42 +03:00
TorrentCreatorDlg : : TorrentCreatorDlg ( QWidget * parent )
: QDialog ( parent )
, m_creatorThread ( 0 )
{
setupUi ( this ) ;
// Icons
addFile_button - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " document-new " ) ) ;
addFolder_button - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " folder-new " ) ) ;
createButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " document-save " ) ) ;
cancelButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " dialog-cancel " ) ) ;
2010-12-12 22:37:59 +03:00
2015-06-24 13:19:42 +03:00
setAttribute ( Qt : : WA_DeleteOnClose ) ;
setModal ( true ) ;
showProgressBar ( false ) ;
loadTrackerList ( ) ;
// Piece sizes
2015-06-24 14:04:11 +03:00
m_pieceSizes < < 16 < < 32 < < 64 < < 128 < < 256 < < 512 < < 1024 < < 2048 < < 4096 < < 8192 < < 16384 ;
2015-06-24 13:19:42 +03:00
loadSettings ( ) ;
show ( ) ;
2006-09-30 20:02:39 +04:00
}
2015-06-24 13:19:42 +03:00
TorrentCreatorDlg : : ~ TorrentCreatorDlg ( )
{
if ( m_creatorThread )
delete m_creatorThread ;
2007-12-31 19:57:35 +03:00
}
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : on_addFolder_button_clicked ( )
{
Preferences * const pref = Preferences : : instance ( ) ;
QString lastPath = pref - > getCreateTorLastAddPath ( ) ;
QString dir = QFileDialog : : getExistingDirectory ( this , tr ( " Select a folder to add to the torrent " ) , lastPath , QFileDialog : : ShowDirsOnly ) ;
if ( ! dir . isEmpty ( ) ) {
pref - > setCreateTorLastAddPath ( dir ) ;
textInputPath - > setText ( Utils : : Fs : : toNativePath ( dir ) ) ;
// Update piece size
if ( checkAutoPieceSize - > isChecked ( ) )
updateOptimalPieceSize ( ) ;
}
2007-04-04 22:55:38 +04:00
}
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : on_addFile_button_clicked ( )
{
Preferences * const pref = Preferences : : instance ( ) ;
QString lastPath = pref - > getCreateTorLastAddPath ( ) ;
QString file = QFileDialog : : getOpenFileName ( this , tr ( " Select a file to add to the torrent " ) , lastPath ) ;
if ( ! file . isEmpty ( ) ) {
pref - > setCreateTorLastAddPath ( Utils : : Fs : : branchPath ( file ) ) ;
textInputPath - > setText ( Utils : : Fs : : toNativePath ( file ) ) ;
// Update piece size
if ( checkAutoPieceSize - > isChecked ( ) )
updateOptimalPieceSize ( ) ;
}
2007-04-04 22:55:38 +04:00
}
2015-06-24 13:19:42 +03:00
int TorrentCreatorDlg : : getPieceSize ( ) const
{
return m_pieceSizes . at ( comboPieceSize - > currentIndex ( ) ) * 1024 ;
2007-04-04 22:55:38 +04:00
}
2006-09-30 20:02:39 +04:00
// Main function that create a .torrent file
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : on_createButton_clicked ( )
{
QString input = Utils : : Fs : : fromNativePath ( textInputPath - > text ( ) ) . trimmed ( ) ;
if ( input . endsWith ( " / " ) )
input . chop ( 1 ) ;
if ( input . isEmpty ( ) ) {
QMessageBox : : critical ( 0 , tr ( " No input path set " ) , tr ( " Please type an input path first " ) ) ;
return ;
}
QStringList trackers = trackers_list - > toPlainText ( ) . split ( " \n " ) ;
if ( ! trackers_list - > toPlainText ( ) . trimmed ( ) . isEmpty ( ) )
saveTrackerList ( ) ;
2010-08-13 18:02:19 +04:00
2015-06-24 13:19:42 +03:00
Preferences * const pref = Preferences : : instance ( ) ;
QString lastPath = pref - > getCreateTorLastSavePath ( ) ;
2015-08-08 16:21:41 +03:00
QString destination = QFileDialog : : getSaveFileName ( this , tr ( " Select destination torrent file " ) , lastPath , tr ( " Torrent Files (*.torrent) " ) ) ;
2015-06-24 13:19:42 +03:00
if ( destination . isEmpty ( ) )
return ;
2010-08-13 18:02:19 +04:00
2015-05-06 14:53:27 +03:00
pref - > setCreateTorLastSavePath ( Utils : : Fs : : branchPath ( destination ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! destination . toUpper ( ) . endsWith ( " .TORRENT " ) )
2015-06-24 13:19:42 +03:00
destination + = QString : : fromUtf8 ( " .torrent " ) ;
2007-12-31 19:57:35 +03:00
2015-06-24 13:19:42 +03:00
// Disable dialog
setInteractionEnabled ( false ) ;
showProgressBar ( true ) ;
// Set busy cursor
setCursor ( QCursor ( Qt : : WaitCursor ) ) ;
// Actually create the torrent
QStringList urlSeeds = URLSeeds_list - > toPlainText ( ) . split ( " \n " ) ;
QString comment = txt_comment - > toPlainText ( ) ;
// Create the creator thread
m_creatorThread = new BitTorrent : : TorrentCreatorThread ( this ) ;
connect ( m_creatorThread , SIGNAL ( creationSuccess ( QString , QString ) ) , this , SLOT ( handleCreationSuccess ( QString , QString ) ) ) ;
connect ( m_creatorThread , SIGNAL ( creationFailure ( QString ) ) , this , SLOT ( handleCreationFailure ( QString ) ) ) ;
connect ( m_creatorThread , SIGNAL ( updateProgress ( int ) ) , this , SLOT ( updateProgressBar ( int ) ) ) ;
m_creatorThread - > create ( input , destination , trackers , urlSeeds , comment , check_private - > isChecked ( ) , getPieceSize ( ) ) ;
2007-12-31 19:57:35 +03:00
}
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : handleCreationFailure ( QString msg )
{
// Remove busy cursor
setCursor ( QCursor ( Qt : : ArrowCursor ) ) ;
QMessageBox : : information ( 0 , tr ( " Torrent creation " ) , tr ( " Torrent creation was unsuccessful, reason: %1 " ) . arg ( msg ) ) ;
setInteractionEnabled ( true ) ;
showProgressBar ( false ) ;
}
2015-04-19 18:17:47 +03:00
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : handleCreationSuccess ( QString path , QString branch_path )
{
// Remove busy cursor
setCursor ( QCursor ( Qt : : ArrowCursor ) ) ;
if ( checkStartSeeding - > isChecked ( ) ) {
// Create save path temp data
BitTorrent : : TorrentInfo t = BitTorrent : : TorrentInfo : : loadFromFile ( Utils : : Fs : : toNativePath ( path ) ) ;
if ( ! t . isValid ( ) ) {
QMessageBox : : critical ( 0 , tr ( " Torrent creation " ) , tr ( " Created torrent file is invalid. It won't be added to download list. " ) ) ;
return ;
}
2015-04-19 18:17:47 +03:00
2015-06-24 13:19:42 +03:00
BitTorrent : : AddTorrentParams params ;
params . savePath = branch_path ;
params . skipChecking = true ;
params . ignoreShareRatio = checkIgnoreShareLimits - > isChecked ( ) ;
2015-04-19 18:17:47 +03:00
2015-06-24 13:19:42 +03:00
BitTorrent : : Session : : instance ( ) - > addTorrent ( t , params ) ;
}
2015-08-08 16:21:41 +03:00
QMessageBox : : information ( 0 , tr ( " Torrent creation " ) , tr ( " Torrent was created successfully: %1 " , " %1 is the path of the torrent " ) . arg ( Utils : : Fs : : toNativePath ( path ) ) ) ;
2015-06-24 13:19:42 +03:00
close ( ) ;
2007-12-31 19:57:35 +03:00
}
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : on_cancelButton_clicked ( )
{
// End torrent creation thread
if ( m_creatorThread & & m_creatorThread - > isRunning ( ) ) {
m_creatorThread - > abortCreation ( ) ;
m_creatorThread - > terminate ( ) ;
// Wait for termination
m_creatorThread - > wait ( ) ;
}
// Close the dialog
close ( ) ;
2010-06-17 21:52:14 +04:00
}
2015-06-24 13:19:42 +03:00
void TorrentCreatorDlg : : updateProgressBar ( int progress )
{
progressBar - > setValue ( progress ) ;
2007-12-31 19:57:35 +03:00
}
2010-10-25 23:34:42 +04:00
void TorrentCreatorDlg : : setInteractionEnabled ( bool enabled )
{
2015-06-24 13:19:42 +03:00
textInputPath - > setEnabled ( enabled ) ;
addFile_button - > setEnabled ( enabled ) ;
addFolder_button - > setEnabled ( enabled ) ;
trackers_list - > setEnabled ( enabled ) ;
URLSeeds_list - > setEnabled ( enabled ) ;
txt_comment - > setEnabled ( enabled ) ;
comboPieceSize - > setEnabled ( enabled ) ;
checkAutoPieceSize - > setEnabled ( enabled ) ;
check_private - > setEnabled ( enabled ) ;
checkStartSeeding - > setEnabled ( enabled ) ;
createButton - > setEnabled ( enabled ) ;
checkIgnoreShareLimits - > setEnabled ( enabled & & checkStartSeeding - > isChecked ( ) ) ;
2010-10-25 23:34:42 +04:00
}
void TorrentCreatorDlg : : showProgressBar ( bool show )
{
2015-06-24 13:19:42 +03:00
progressLbl - > setVisible ( show ) ;
progressBar - > setVisible ( show ) ;
2007-12-31 19:57:35 +03:00
}
2010-10-25 23:34:42 +04:00
void TorrentCreatorDlg : : on_checkAutoPieceSize_clicked ( bool checked )
{
2015-06-24 13:19:42 +03:00
comboPieceSize - > setEnabled ( ! checked ) ;
if ( checked )
updateOptimalPieceSize ( ) ;
2009-07-23 13:11:05 +04:00
}
2010-10-25 23:34:42 +04:00
void TorrentCreatorDlg : : updateOptimalPieceSize ( )
{
2015-06-24 13:19:42 +03:00
qint64 torrentSize = Utils : : Fs : : computePathSize ( textInputPath - > text ( ) ) ;
qDebug ( " Torrent size is %lld " , torrentSize ) ;
if ( torrentSize < 0 )
return ;
int i = 0 ;
qulonglong nb_pieces = 0 ;
do {
nb_pieces = ( double ) torrentSize / ( m_pieceSizes . at ( i ) * 1024. ) ;
qDebug ( " nb_pieces=%lld with piece_size=%s " , nb_pieces , qPrintable ( comboPieceSize - > itemText ( i ) ) ) ;
if ( nb_pieces < = NB_PIECES_MIN ) {
if ( i > 1 )
- - i ;
break ;
}
else if ( nb_pieces < NB_PIECES_MAX ) {
qDebug ( " Good, nb_pieces=%lld < %d " , nb_pieces + 1 , NB_PIECES_MAX ) ;
break ;
}
+ + i ;
} while ( i < ( m_pieceSizes . size ( ) - 1 ) ) ;
comboPieceSize - > setCurrentIndex ( i ) ;
2008-11-02 00:42:56 +03:00
}
2010-10-25 23:34:42 +04:00
void TorrentCreatorDlg : : saveTrackerList ( )
{
2015-06-24 13:19:42 +03:00
Preferences : : instance ( ) - > setCreateTorTrackers ( trackers_list - > toPlainText ( ) ) ;
2010-10-25 23:34:42 +04:00
}
2008-11-02 00:42:56 +03:00
2010-10-25 23:34:42 +04:00
void TorrentCreatorDlg : : loadTrackerList ( )
{
2015-06-24 13:19:42 +03:00
trackers_list - > setPlainText ( Preferences : : instance ( ) - > getCreateTorTrackers ( ) ) ;
2006-09-30 20:02:39 +04:00
}
2010-12-04 13:31:14 +03:00
void TorrentCreatorDlg : : saveSettings ( )
{
2015-06-24 13:19:42 +03:00
Preferences * const pref = Preferences : : instance ( ) ;
pref - > setCreateTorGeometry ( saveGeometry ( ) ) ;
pref - > setCreateTorIgnoreRatio ( checkIgnoreShareLimits - > isChecked ( ) ) ;
2010-12-04 13:31:14 +03:00
}
void TorrentCreatorDlg : : loadSettings ( )
{
2015-06-24 13:19:42 +03:00
const Preferences * const pref = Preferences : : instance ( ) ;
restoreGeometry ( pref - > getCreateTorGeometry ( ) ) ;
checkIgnoreShareLimits - > setChecked ( pref - > getCreateTorIgnoreRatio ( ) ) ;
2010-12-04 13:31:14 +03:00
}
void TorrentCreatorDlg : : closeEvent ( QCloseEvent * event )
{
2015-06-24 13:19:42 +03:00
qDebug ( ) < < Q_FUNC_INFO ;
saveSettings ( ) ;
QDialog : : closeEvent ( event ) ;
2010-12-04 13:31:14 +03:00
}