2006-10-24 01:42:37 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez
2006-10-24 01:42:37 +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-10-24 01:42:37 +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-10-24 01:42:37 +04:00
*/
# ifndef TORRENTADDITION_H
# define TORRENTADDITION_H
# include <QDir>
# include <QFileDialog>
# include <QFile>
# include <fstream>
# include <QMessageBox>
2006-10-24 02:18:06 +04:00
# include <QMenu>
2006-11-15 00:17:51 +03:00
# include <QSettings>
2007-04-13 00:28:10 +04:00
# include <QHeaderView>
2009-12-08 20:48:33 +03:00
# include <QApplication>
# include <QDesktopWidget>
2010-01-01 13:55:13 +03:00
# include <QInputDialog>
2006-10-24 01:42:37 +04:00
# include <libtorrent/session.hpp>
# include <libtorrent/bencode.hpp>
2008-09-07 15:31:29 +04:00
# include "bittorrent.h"
2006-10-24 01:42:37 +04:00
# include "misc.h"
2009-11-20 10:48:44 +03:00
# include "proplistdelegate.h"
2009-11-20 11:20:05 +03:00
# include "ui_torrentadditiondlg.h"
2009-11-20 10:48:44 +03:00
# include "torrentpersistentdata.h"
# include "torrentfilesmodel.h"
2009-12-22 22:17:27 +03:00
# include "preferences.h"
2010-01-07 21:17:20 +03:00
# include "GUI.h"
# include "transferlistwidget.h"
2006-10-24 01:42:37 +04:00
using namespace libtorrent ;
2010-06-05 17:43:36 +04:00
Q_DECLARE_METATYPE ( QList < int > )
2006-10-24 01:42:37 +04:00
class torrentAdditionDialog : public QDialog , private Ui_addTorrentDialog {
Q_OBJECT
2009-08-17 17:17:52 +04:00
private :
2009-11-20 11:01:59 +03:00
Bittorrent * BTSession ;
2009-08-17 17:17:52 +04:00
QString fileName ;
QString hash ;
QString filePath ;
QString from_url ;
2010-04-10 18:50:11 +04:00
QString defaultSavePath ;
QString old_label ;
bool appendLabelToSavePath ;
2009-11-11 17:06:07 +03:00
TorrentFilesModel * PropListModel ;
2009-08-17 17:17:52 +04:00
PropListDelegate * PropDelegate ;
unsigned int nbFiles ;
boost : : intrusive_ptr < torrent_info > t ;
2010-01-01 13:55:13 +03:00
QStringList files_path ;
2010-01-03 17:20:31 +03:00
bool is_magnet ;
2006-10-24 01:42:37 +04:00
2009-08-17 17:17:52 +04:00
public :
2010-04-10 18:50:11 +04:00
torrentAdditionDialog ( GUI * parent , Bittorrent * _BTSession ) : QDialog ( ( QWidget * ) parent ) , old_label ( " " ) {
2009-08-17 17:17:52 +04:00
setupUi ( this ) ;
setAttribute ( Qt : : WA_DeleteOnClose ) ;
2010-01-07 21:17:20 +03:00
connect ( this , SIGNAL ( torrentPaused ( QTorrentHandle & ) ) , parent - > getTransferList ( ) , SLOT ( pauseTorrent ( QTorrentHandle & ) ) ) ;
2009-08-17 17:17:52 +04:00
BTSession = _BTSession ;
// Set Properties list model
2009-11-11 17:06:07 +03:00
PropListModel = new TorrentFilesModel ( ) ;
2009-08-17 17:17:52 +04:00
torrentContentList - > setModel ( PropListModel ) ;
torrentContentList - > hideColumn ( PROGRESS ) ;
PropDelegate = new PropListDelegate ( ) ;
torrentContentList - > setItemDelegate ( PropDelegate ) ;
connect ( torrentContentList , SIGNAL ( clicked ( const QModelIndex & ) ) , torrentContentList , SLOT ( edit ( const QModelIndex & ) ) ) ;
2010-01-01 13:55:13 +03:00
connect ( torrentContentList , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayContentListMenu ( const QPoint & ) ) ) ;
2009-08-17 17:17:52 +04:00
connect ( collapseAllButton , SIGNAL ( clicked ( ) ) , torrentContentList , SLOT ( collapseAll ( ) ) ) ;
connect ( expandAllButton , SIGNAL ( clicked ( ) ) , torrentContentList , SLOT ( expandAll ( ) ) ) ;
2010-04-10 18:50:11 +04:00
connect ( comboLabel , SIGNAL ( editTextChanged ( QString ) ) , this , SLOT ( updateLabelInSavePath ( QString ) ) ) ;
connect ( comboLabel , SIGNAL ( currentIndexChanged ( QString ) ) , this , SLOT ( updateLabelInSavePath ( QString ) ) ) ;
2009-11-11 17:19:44 +03:00
// Remember columns width
readSettings ( ) ;
2009-08-17 17:17:52 +04:00
//torrentContentList->header()->setResizeMode(0, QHeaderView::Stretch);
2010-04-10 18:50:11 +04:00
defaultSavePath = Preferences : : getSavePath ( ) ;
appendLabelToSavePath = Preferences : : appendTorrentLabel ( ) ;
2010-06-04 01:08:28 +04:00
QString display_path = defaultSavePath ;
# ifdef Q_WS_WIN
display_path = display_path . replace ( " / " , " \\ " ) ;
# endif
savePathTxt - > setText ( display_path ) ;
2009-12-22 22:17:27 +03:00
if ( Preferences : : addTorrentsInPause ( ) ) {
2009-08-17 17:17:52 +04:00
addInPause - > setChecked ( true ) ;
2010-01-20 11:35:36 +03:00
//addInPause->setEnabled(false);
2008-12-23 18:07:29 +03:00
}
2009-11-18 13:29:20 +03:00
# ifndef LIBTORRENT_0_15
2010-01-03 17:59:14 +03:00
addInSeed - > setVisible ( false ) ;
2009-11-18 13:29:20 +03:00
# endif
2009-08-17 17:17:52 +04:00
}
2008-12-23 18:07:29 +03:00
2009-08-17 17:17:52 +04:00
~ torrentAdditionDialog ( ) {
2009-11-11 17:19:44 +03:00
saveSettings ( ) ;
2009-08-17 17:17:52 +04:00
delete PropDelegate ;
delete PropListModel ;
}
2006-10-24 01:42:37 +04:00
2009-11-11 17:19:44 +03:00
void readSettings ( ) {
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
2009-12-08 20:48:33 +03:00
// Restore size and position
resize ( settings . value ( QString : : fromUtf8 ( " TorrentAdditionDlg/size " ) , size ( ) ) . toSize ( ) ) ;
2010-01-22 01:23:13 +03:00
move ( settings . value ( QString : : fromUtf8 ( " TorrentAdditionDlg/pos " ) , misc : : screenCenter ( this ) ) . toPoint ( ) ) ;
2009-12-08 20:48:33 +03:00
// Restore column width
2010-06-05 17:43:36 +04:00
const QList < int > & contentColsWidths = settings . value ( QString : : fromUtf8 ( " TorrentAdditionDlg/filesColsWidth " ) ) . value < QList < int > > ( ) ;
2009-11-11 17:19:44 +03:00
if ( contentColsWidths . empty ( ) ) {
torrentContentList - > header ( ) - > resizeSection ( 0 , 200 ) ;
} else {
for ( int i = 0 ; i < contentColsWidths . size ( ) ; + + i ) {
2010-06-05 17:43:36 +04:00
torrentContentList - > setColumnWidth ( i , contentColsWidths . at ( i ) ) ;
2009-11-11 17:19:44 +03:00
}
}
}
void saveSettings ( ) {
2010-01-03 17:59:14 +03:00
if ( is_magnet ) return ;
2009-11-11 17:19:44 +03:00
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
2010-06-05 17:43:36 +04:00
QList < int > contentColsWidths ;
2009-11-11 17:19:44 +03:00
// -1 because we hid PROGRESS column
for ( int i = 0 ; i < PropListModel - > columnCount ( ) - 1 ; + + i ) {
contentColsWidths . append ( torrentContentList - > columnWidth ( i ) ) ;
}
2010-06-05 17:43:36 +04:00
settings . setValue ( QString : : fromUtf8 ( " TorrentAdditionDlg/filesColsWidth " ) , QVariant : : fromValue < QList < int > > ( contentColsWidths ) ) ;
2009-12-08 20:48:33 +03:00
settings . setValue ( " TorrentAdditionDlg/size " , size ( ) ) ;
settings . setValue ( " TorrentAdditionDlg/pos " , pos ( ) ) ;
2009-11-11 17:19:44 +03:00
}
2010-01-03 17:20:31 +03:00
void showLoadMagnetURI ( QString magnet_uri ) {
2010-01-03 17:59:14 +03:00
show ( ) ;
2010-01-03 17:20:31 +03:00
is_magnet = true ;
this - > from_url = magnet_uri ;
2010-01-03 17:59:14 +03:00
int hidden_height = 0 ;
2010-01-03 17:20:31 +03:00
// Disable useless widgets
2010-01-03 17:59:14 +03:00
hidden_height + = torrentContentList - > height ( ) ;
2010-01-03 17:20:31 +03:00
torrentContentList - > setVisible ( false ) ;
2010-01-03 17:59:14 +03:00
hidden_height + = torrentContentLbl - > height ( ) ;
2010-01-03 17:20:31 +03:00
torrentContentLbl - > setVisible ( false ) ;
2010-01-03 17:59:14 +03:00
hidden_height + = collapseAllButton - > height ( ) ;
2010-01-03 17:20:31 +03:00
collapseAllButton - > setVisible ( false ) ;
expandAllButton - > setVisible ( false ) ;
// Get torrent hash
hash = misc : : magnetUriToHash ( magnet_uri ) ;
if ( hash . isEmpty ( ) ) {
BTSession - > addConsoleMessage ( tr ( " Unable to decode magnet link: " ) + QString : : fromUtf8 ( " ' " ) + from_url + QString : : fromUtf8 ( " ' " ) , QString : : fromUtf8 ( " red " ) ) ;
return ;
}
fileName = misc : : magnetUriToName ( magnet_uri ) ;
2010-04-06 13:17:33 +04:00
if ( fileName . isEmpty ( ) ) {
fileName = tr ( " Magnet Link " ) ;
} else {
QString save_path = savePathTxt - > text ( ) ;
if ( ! save_path . endsWith ( QDir : : separator ( ) ) )
save_path + = QDir : : separator ( ) ;
savePathTxt - > setText ( save_path + fileName ) ;
}
2010-01-03 17:20:31 +03:00
fileNameLbl - > setText ( QString : : fromUtf8 ( " <center><b> " ) + fileName + QString : : fromUtf8 ( " </b></center> " ) ) ;
// Update display
updateDiskSpaceLabels ( ) ;
// Load custom labels
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
settings . beginGroup ( QString : : fromUtf8 ( " TransferListFilters " ) ) ;
QStringList customLabels = settings . value ( " customLabels " , QStringList ( ) ) . toStringList ( ) ;
comboLabel - > addItem ( " " ) ;
foreach ( const QString & label , customLabels ) {
comboLabel - > addItem ( label ) ;
}
// Show dialog
2010-01-03 17:59:14 +03:00
//show();
setMinimumSize ( 0 , 0 ) ;
resize ( width ( ) , height ( ) - hidden_height ) ;
2010-01-03 17:20:31 +03:00
}
2010-03-04 23:19:25 +03:00
void showLoad ( QString filePath , QString from_url = QString : : null ) {
2010-01-03 17:20:31 +03:00
is_magnet = false ;
2009-08-17 17:17:52 +04:00
if ( ! QFile : : exists ( filePath ) ) {
close ( ) ;
return ;
}
this - > filePath = filePath ;
this - > from_url = from_url ;
// Getting torrent file informations
try {
t = new torrent_info ( filePath . toLocal8Bit ( ) . data ( ) ) ;
2010-01-03 16:00:16 +03:00
if ( ! t - > is_valid ( ) )
throw std : : exception ( ) ;
2009-08-17 17:17:52 +04:00
} catch ( std : : exception & ) {
qDebug ( " Caught error loading torrent " ) ;
if ( ! from_url . isNull ( ) ) {
BTSession - > addConsoleMessage ( tr ( " Unable to decode torrent file: " ) + QString : : fromUtf8 ( " ' " ) + from_url + QString : : fromUtf8 ( " ' " ) , QString : : fromUtf8 ( " red " ) ) ;
QFile : : remove ( filePath ) ;
} else {
BTSession - > addConsoleMessage ( tr ( " Unable to decode torrent file: " ) + QString : : fromUtf8 ( " ' " ) + filePath + QString : : fromUtf8 ( " ' " ) , QString : : fromUtf8 ( " red " ) ) ;
2007-09-16 13:19:22 +04:00
}
2009-08-17 17:17:52 +04:00
close ( ) ;
return ;
2007-09-16 13:19:22 +04:00
}
2010-04-06 13:17:33 +04:00
QString root_folder = misc : : truncateRootFolder ( t ) ;
if ( ! root_folder . isEmpty ( ) ) {
QString save_path = savePathTxt - > text ( ) ;
if ( ! save_path . endsWith ( QDir : : separator ( ) ) )
save_path + = QDir : : separator ( ) ;
savePathTxt - > setText ( save_path + root_folder ) ;
}
2009-08-17 17:17:52 +04:00
nbFiles = t - > num_files ( ) ;
// Setting file name
2010-06-04 00:08:19 +04:00
fileName = misc : : toQStringU ( t - > name ( ) ) ;
2009-08-17 17:17:52 +04:00
hash = misc : : toQString ( t - > info_hash ( ) ) ;
// Use left() to remove .old extension
QString newFileName ;
if ( fileName . endsWith ( QString : : fromUtf8 ( " .old " ) ) ) {
newFileName = fileName . left ( fileName . size ( ) - 4 ) ;
} else {
newFileName = fileName ;
}
fileNameLbl - > setText ( QString : : fromUtf8 ( " <center><b> " ) + newFileName + QString : : fromUtf8 ( " </b></center> " ) ) ;
// List files in torrent
2009-11-11 17:06:07 +03:00
PropListModel - > setupModelData ( * t ) ;
// Expand first item if possible
torrentContentList - > expand ( PropListModel - > index ( 0 , 0 ) ) ;
connect ( PropDelegate , SIGNAL ( filteredFilesChanged ( ) ) , this , SLOT ( updateDiskSpaceLabels ( ) ) ) ;
2009-08-17 17:17:52 +04:00
//torrentContentList->expandAll();
2009-09-07 15:48:31 +04:00
connect ( savePathTxt , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( updateDiskSpaceLabels ( ) ) ) ;
2009-08-17 17:17:52 +04:00
updateDiskSpaceLabels ( ) ;
2009-12-15 22:52:43 +03:00
// Load custom labels
QSettings settings ( QString : : fromUtf8 ( " qBittorrent " ) , QString : : fromUtf8 ( " qBittorrent " ) ) ;
settings . beginGroup ( QString : : fromUtf8 ( " TransferListFilters " ) ) ;
2010-03-04 23:19:25 +03:00
const QStringList & customLabels = settings . value ( " customLabels " , QStringList ( ) ) . toStringList ( ) ;
2009-12-18 18:13:39 +03:00
comboLabel - > addItem ( " " ) ;
2009-12-15 22:52:43 +03:00
foreach ( const QString & label , customLabels ) {
comboLabel - > addItem ( label ) ;
}
2010-01-01 13:55:13 +03:00
// Loads files path in the torrent
for ( uint i = 0 ; i < nbFiles ; + + i ) {
2010-06-04 00:08:19 +04:00
files_path < < misc : : toQStringU ( t - > file_at ( i ) . path . string ( ) ) ;
2010-01-01 13:55:13 +03:00
}
2009-12-15 22:52:43 +03:00
// Show the dialog
2009-08-17 17:17:52 +04:00
show ( ) ;
}
2007-09-16 13:19:22 +04:00
2009-08-17 17:17:52 +04:00
public slots :
2010-01-01 13:55:13 +03:00
void displayContentListMenu ( const QPoint & ) {
QMenu myFilesLlistMenu ;
2010-03-04 23:19:25 +03:00
const QModelIndexList & selectedRows = torrentContentList - > selectionModel ( ) - > selectedRows ( 0 ) ;
2010-01-01 13:55:13 +03:00
QAction * actRename = 0 ;
if ( selectedRows . size ( ) = = 1 ) {
actRename = myFilesLlistMenu . addAction ( QIcon ( QString : : fromUtf8 ( " :/Icons/oxygen/edit_clear.png " ) ) , tr ( " Rename... " ) ) ;
2010-02-16 23:50:44 +03:00
myFilesLlistMenu . addSeparator ( ) ;
2009-08-17 17:17:52 +04:00
}
2010-02-16 23:50:44 +03:00
QMenu subMenu ;
subMenu . setTitle ( tr ( " Priority " ) ) ;
subMenu . addAction ( actionNormal ) ;
subMenu . addAction ( actionHigh ) ;
subMenu . addAction ( actionMaximum ) ;
myFilesLlistMenu . addMenu ( & subMenu ) ;
2010-01-01 13:55:13 +03:00
// Call menu
QAction * act = myFilesLlistMenu . exec ( QCursor : : pos ( ) ) ;
if ( act ) {
if ( act = = actRename ) {
renameSelectedFile ( ) ;
2010-02-16 23:50:44 +03:00
} else {
int prio = 1 ;
if ( act = = actionHigh ) {
prio = HIGH ;
} else {
if ( act = = actionMaximum ) {
prio = MAXIMUM ;
}
}
qDebug ( " Setting files priority " ) ;
2010-03-04 23:19:25 +03:00
foreach ( const QModelIndex & index , selectedRows ) {
2010-02-16 23:50:44 +03:00
qDebug ( " Setting priority(%d) for file at row %d " , prio , index . row ( ) ) ;
PropListModel - > setData ( PropListModel - > index ( index . row ( ) , PRIORITY , index . parent ( ) ) , prio ) ;
}
2006-10-24 01:42:37 +04:00
}
}
2009-08-17 17:17:52 +04:00
}
2006-10-24 01:42:37 +04:00
2010-01-01 13:55:13 +03:00
void renameSelectedFile ( ) {
2010-03-04 23:19:25 +03:00
const QModelIndexList & selectedIndexes = torrentContentList - > selectionModel ( ) - > selectedRows ( 0 ) ;
2010-01-01 13:55:13 +03:00
Q_ASSERT ( selectedIndexes . size ( ) = = 1 ) ;
2010-03-04 23:19:25 +03:00
const QModelIndex & index = selectedIndexes . first ( ) ;
2010-01-01 13:55:13 +03:00
// Ask for new name
bool ok ;
2010-03-04 23:19:25 +03:00
const QString & new_name_last = QInputDialog : : getText ( this , tr ( " Rename the file " ) ,
2010-04-10 18:50:11 +04:00
tr ( " New name: " ) , QLineEdit : : Normal ,
index . data ( ) . toString ( ) , & ok ) ;
2010-01-01 13:55:13 +03:00
if ( ok & & ! new_name_last . isEmpty ( ) ) {
2010-01-05 15:28:42 +03:00
if ( ! misc : : isValidFileSystemName ( new_name_last ) ) {
QMessageBox : : warning ( this , tr ( " The file could not be renamed " ) ,
tr ( " This file name contains forbidden characters, please choose a different one. " ) ,
QMessageBox : : Ok ) ;
return ;
}
2010-01-01 13:55:13 +03:00
if ( PropListModel - > getType ( index ) = = TFILE ) {
// File renaming
2010-03-04 23:19:25 +03:00
const uint file_index = PropListModel - > getFileIndex ( index ) ;
const QString & old_name = files_path . at ( file_index ) ;
2010-01-01 13:55:13 +03:00
QStringList path_items = old_name . split ( QDir : : separator ( ) ) ;
path_items . removeLast ( ) ;
path_items < < new_name_last ;
2010-03-04 23:19:25 +03:00
const QString & new_name = path_items . join ( QDir : : separator ( ) ) ;
2010-01-01 13:55:13 +03:00
if ( old_name = = new_name ) {
qDebug ( " Name did not change " ) ;
return ;
}
// Check if that name is already used
for ( uint i = 0 ; i < nbFiles ; + + i ) {
if ( i = = file_index ) continue ;
2010-03-30 15:35:20 +04:00
# if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS)
if ( files_path . at ( i ) . compare ( new_name , Qt : : CaseSensitive ) = = 0 ) {
2010-01-01 13:55:13 +03:00
# else
2010-03-30 15:35:20 +04:00
if ( files_path . at ( i ) . compare ( new_name , Qt : : CaseInsensitive ) = = 0 ) {
2010-01-01 13:55:13 +03:00
# endif
// Display error message
QMessageBox : : warning ( this , tr ( " The file could not be renamed " ) ,
tr ( " This name is already in use in this folder. Please use a different name. " ) ,
QMessageBox : : Ok ) ;
return ;
}
}
2010-03-04 23:19:25 +03:00
qDebug ( " Renaming %s to %s " , qPrintable ( old_name ) , qPrintable ( new_name ) ) ;
2010-01-01 13:55:13 +03:00
// Rename file in files_path
files_path . replace ( file_index , new_name ) ;
// Rename in torrent files model too
PropListModel - > setData ( index , new_name_last ) ;
} else {
// Folder renaming
QStringList path_items ;
path_items < < index . data ( ) . toString ( ) ;
QModelIndex parent = PropListModel - > parent ( index ) ;
while ( parent . isValid ( ) ) {
path_items . prepend ( parent . data ( ) . toString ( ) ) ;
parent = PropListModel - > parent ( parent ) ;
}
2010-03-04 23:19:25 +03:00
const QString & old_path = path_items . join ( QDir : : separator ( ) ) ;
2010-01-01 13:55:13 +03:00
path_items . removeLast ( ) ;
path_items < < new_name_last ;
QString new_path = path_items . join ( QDir : : separator ( ) ) ;
2010-01-21 21:33:53 +03:00
if ( ! new_path . endsWith ( QDir : : separator ( ) ) ) new_path + = QDir : : separator ( ) ;
2010-01-01 13:55:13 +03:00
// Check for overwriting
for ( uint i = 0 ; i < nbFiles ; + + i ) {
2010-03-04 23:19:25 +03:00
const QString & current_name = files_path . at ( i ) ;
2010-03-30 15:35:20 +04:00
# if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS)
if ( current_name . startsWith ( new_path , Qt : : CaseSensitive ) ) {
2010-01-01 13:55:13 +03:00
# else
2010-03-30 15:35:20 +04:00
if ( current_name . startsWith ( new_path , Qt : : CaseInsensitive ) ) {
2010-01-01 13:55:13 +03:00
# endif
QMessageBox : : warning ( this , tr ( " The folder could not be renamed " ) ,
tr ( " This name is already in use in this folder. Please use a different name. " ) ,
QMessageBox : : Ok ) ;
return ;
}
}
// Replace path in all files
for ( uint i = 0 ; i < nbFiles ; + + i ) {
2010-03-04 23:19:25 +03:00
const QString & current_name = files_path . at ( i ) ;
2010-01-20 01:35:54 +03:00
if ( current_name . startsWith ( old_path ) ) {
QString new_name = current_name ;
new_name . replace ( 0 , old_path . length ( ) , new_path ) ;
2010-03-04 23:19:25 +03:00
qDebug ( " Rename %s to %s " , qPrintable ( current_name ) , qPrintable ( new_name ) ) ;
2010-01-20 01:35:54 +03:00
// Rename in files_path
files_path . replace ( i , new_name ) ;
}
2010-01-01 13:55:13 +03:00
}
// Rename folder in torrent files model too
PropListModel - > setData ( index , new_name_last ) ;
}
}
}
2009-08-17 17:17:52 +04:00
2010-01-01 13:55:13 +03:00
void updateDiskSpaceLabels ( ) {
2010-03-04 23:19:25 +03:00
const long long available = misc : : freeDiskSpaceOnPath ( misc : : expandPath ( savePathTxt - > text ( ) ) ) ;
2010-01-01 13:55:13 +03:00
lbl_disk_space - > setText ( misc : : friendlyUnit ( available ) ) ;
2010-01-03 17:20:31 +03:00
if ( ! is_magnet ) {
// Determine torrent size
qulonglong torrent_size = 0 ;
2010-03-04 23:19:25 +03:00
const unsigned int nbFiles = t - > num_files ( ) ;
const std : : vector < int > & priorities = PropListModel - > getFilesPriorities ( nbFiles ) ;
2007-07-31 19:37:14 +04:00
2010-01-03 17:20:31 +03:00
for ( unsigned int i = 0 ; i < nbFiles ; + + i ) {
if ( priorities [ i ] > 0 )
torrent_size + = t - > file_at ( i ) . size ;
}
lbl_torrent_size - > setText ( misc : : friendlyUnit ( torrent_size ) ) ;
2007-07-20 17:21:46 +04:00
2010-01-03 17:20:31 +03:00
// Check if free space is sufficient
if ( available > 0 ) {
if ( ( unsigned long long ) available > torrent_size ) {
// Space is sufficient
label_space_msg - > setText ( tr ( " (%1 left after torrent download) " , " e.g. (100MiB left after torrent download) " ) . arg ( misc : : friendlyUnit ( available - torrent_size ) ) ) ;
} else {
// Space is unsufficient
label_space_msg - > setText ( " <font color= \" red \" > " + tr ( " (%1 more are required to download) " , " e.g. (100MiB more are required to download) " ) . arg ( misc : : friendlyUnit ( torrent_size - available ) ) + " </font> " ) ;
}
2010-01-01 13:55:13 +03:00
} else {
2010-01-03 17:20:31 +03:00
// Available disk space is unknown
label_space_msg - > setText ( " " ) ;
2010-01-01 13:55:13 +03:00
}
}
}
2009-08-17 17:17:52 +04:00
2010-01-01 13:55:13 +03:00
void on_browseButton_clicked ( ) {
QString dir ;
2010-06-04 01:08:28 +04:00
QString save_path = savePathTxt - > text ( ) ;
# ifdef Q_WS_WIN
save_path = save_path . replace ( " \\ " , " / " ) ;
# endif
save_path = misc : : expandPath ( save_path ) ;
2010-03-04 23:19:25 +03:00
const QDir & saveDir ( save_path ) ;
2010-01-01 13:55:13 +03:00
if ( ! save_path . isEmpty ( ) & & saveDir . exists ( ) ) {
dir = QFileDialog : : getExistingDirectory ( this , tr ( " Choose save path " ) , saveDir . absolutePath ( ) ) ;
} else {
dir = QFileDialog : : getExistingDirectory ( this , tr ( " Choose save path " ) , QDir : : homePath ( ) ) ;
}
if ( ! dir . isNull ( ) ) {
2010-06-04 01:08:28 +04:00
# ifdef Q_WS_WIN
dir = dir . replace ( " / " , " \\ " ) ;
# endif
2010-01-01 13:55:13 +03:00
savePathTxt - > setText ( dir ) ;
}
2006-10-24 01:42:37 +04:00
}
2010-01-01 13:55:13 +03:00
void on_CancelButton_clicked ( ) {
close ( ) ;
2009-11-07 11:29:57 +03:00
}
2010-01-01 13:55:13 +03:00
bool allFiltered ( ) const {
return PropListModel - > allFiltered ( ) ;
}
void savePiecesPriorities ( ) {
qDebug ( " Saving pieces priorities " ) ;
2010-03-04 23:19:25 +03:00
const std : : vector < int > & priorities = PropListModel - > getFilesPriorities ( t - > num_files ( ) ) ;
2010-01-01 13:55:13 +03:00
TorrentTempData : : setFilesPriority ( hash , priorities ) ;
}
void on_OkButton_clicked ( ) {
if ( savePathTxt - > text ( ) . trimmed ( ) . isEmpty ( ) ) {
QMessageBox : : critical ( 0 , tr ( " Empty save path " ) , tr ( " Please enter a save path " ) ) ;
return ;
}
2010-06-04 01:08:28 +04:00
QString save_path = savePathTxt - > text ( ) ;
# ifdef Q_WS_WIN
save_path = save_path . replace ( " \\ " , " / " ) ;
# endif
QDir savePath ( misc : : expandPath ( save_path ) ) ;
2010-01-01 13:55:13 +03:00
// Check if savePath exists
if ( ! savePath . exists ( ) ) {
if ( ! savePath . mkpath ( savePath . path ( ) ) ) {
QMessageBox : : critical ( 0 , tr ( " Save path creation error " ) , tr ( " Could not create the save path " ) ) ;
return ;
}
}
2010-03-04 23:19:25 +03:00
const QString & current_label = comboLabel - > currentText ( ) . trimmed ( ) ;
2010-01-11 23:09:32 +03:00
if ( ! current_label . isEmpty ( ) & & ! misc : : isValidFileSystemName ( current_label ) ) {
2010-01-05 15:18:17 +03:00
QMessageBox : : warning ( this , tr ( " Invalid label name " ) , tr ( " Please don't use any special characters in the label name. " ) ) ;
return ;
}
2010-01-01 13:55:13 +03:00
// Save savepath
TorrentTempData : : setSavePath ( hash , savePath . path ( ) ) ;
2010-03-04 23:19:25 +03:00
qDebug ( " Torrent label is: %s " , qPrintable ( comboLabel - > currentText ( ) . trimmed ( ) ) ) ;
2010-01-11 23:09:32 +03:00
if ( ! current_label . isEmpty ( ) )
TorrentTempData : : setLabel ( hash , current_label ) ;
2010-01-01 13:55:13 +03:00
// Is download sequential?
TorrentTempData : : setSequential ( hash , checkIncrementalDL - > isChecked ( ) ) ;
// Save files path
// Loads files path in the torrent
2010-01-03 17:20:31 +03:00
if ( ! is_magnet ) {
bool path_changed = false ;
for ( uint i = 0 ; i < nbFiles ; + + i ) {
2010-03-30 15:35:20 +04:00
# if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS)
2010-06-04 00:08:19 +04:00
if ( files_path . at ( i ) . compare ( misc : : toQStringU ( t - > file_at ( i ) . path . string ( ) ) , Qt : : CaseSensitive ) ! = 0 ) {
2010-01-01 13:55:13 +03:00
# else
2010-06-04 00:08:19 +04:00
if ( files_path . at ( i ) . compare ( misc : : toQStringU ( t - > file_at ( i ) . path . string ( ) ) , Qt : : CaseInsensitive ) ! = 0 ) {
2009-11-18 13:29:20 +03:00
# endif
2010-01-03 17:20:31 +03:00
path_changed = true ;
break ;
}
}
if ( path_changed ) {
2010-06-04 00:08:19 +04:00
qDebug ( " Changing files paths " ) ;
2010-01-03 17:20:31 +03:00
TorrentTempData : : setFilesPath ( hash , files_path ) ;
2010-01-01 13:55:13 +03:00
}
}
# ifdef LIBTORRENT_0_15
// Skip file checking and directly start seeding
if ( addInSeed - > isChecked ( ) ) {
// Check if local file(s) actually exist
2010-06-04 00:08:19 +04:00
if ( is_magnet | | savePath . exists ( misc : : toQStringU ( t - > name ( ) ) ) ) {
2010-01-01 13:55:13 +03:00
TorrentTempData : : setSeedingMode ( hash , true ) ;
} else {
QMessageBox : : warning ( 0 , tr ( " Seeding mode error " ) , tr ( " You chose to skip file checking. However, local files do not seem to exist in the current destionation folder. Please disable this feature or update the save path. " ) ) ;
return ;
}
}
# endif
// Check if there is at least one selected file
2010-01-03 17:20:31 +03:00
if ( ! is_magnet & & allFiltered ( ) ) {
2010-01-01 13:55:13 +03:00
QMessageBox : : warning ( 0 , tr ( " Invalid file selection " ) , tr ( " You must select at least one file in the torrent " ) ) ;
return ;
}
// save filtered files
2010-01-03 17:20:31 +03:00
if ( ! is_magnet )
savePiecesPriorities ( ) ;
2010-01-01 13:55:13 +03:00
// Add to download list
2010-01-03 17:20:31 +03:00
QTorrentHandle h ;
if ( is_magnet )
h = BTSession - > addMagnetUri ( from_url , false ) ;
else
h = BTSession - > addTorrent ( filePath , false , from_url ) ;
2010-01-07 21:17:20 +03:00
if ( addInPause - > isChecked ( ) & & h . is_valid ( ) ) {
2010-01-01 13:55:13 +03:00
h . pause ( ) ;
2010-01-07 21:17:20 +03:00
emit torrentPaused ( h ) ;
}
2010-01-01 13:55:13 +03:00
close ( ) ;
}
2010-01-07 21:17:20 +03:00
2010-04-10 18:50:11 +04:00
public slots :
void updateLabelInSavePath ( QString label ) {
if ( appendLabelToSavePath ) {
savePathTxt - > setText ( misc : : updateLabelInSavePath ( defaultSavePath , savePathTxt - > text ( ) , old_label , label ) ) ;
old_label = label ;
}
}
2010-01-07 21:17:20 +03:00
signals :
void torrentPaused ( QTorrentHandle & h ) ;
2010-01-01 13:55:13 +03:00
} ;
2006-10-24 01:42:37 +04:00
# endif