|
|
@ -42,55 +42,23 @@ using namespace boost::filesystem; |
|
|
|
|
|
|
|
|
|
|
|
createtorrent::createtorrent(QWidget *parent): QDialog(parent){ |
|
|
|
createtorrent::createtorrent(QWidget *parent): QDialog(parent){ |
|
|
|
setupUi(this); |
|
|
|
setupUi(this); |
|
|
|
addTracker_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png"))); |
|
|
|
|
|
|
|
removeTracker_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png"))); |
|
|
|
|
|
|
|
addURLSeed_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png"))); |
|
|
|
|
|
|
|
removeURLSeed_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png"))); |
|
|
|
|
|
|
|
removeFolder_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png"))); |
|
|
|
|
|
|
|
addFolder_button->setIcon(QIcon(QString::fromUtf8(":/Icons/add_folder.png"))); |
|
|
|
|
|
|
|
addFile_button->setIcon(QIcon(QString::fromUtf8(":/Icons/add_file.png"))); |
|
|
|
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose); |
|
|
|
setAttribute(Qt::WA_DeleteOnClose); |
|
|
|
show(); |
|
|
|
show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_browse_destination_clicked(){ |
|
|
|
|
|
|
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), QDir::homePath(), tr("Torrent Files")+QString::fromUtf8(" (*.torrent)")); |
|
|
|
|
|
|
|
if(!destination.isEmpty()){ |
|
|
|
|
|
|
|
if(!destination.endsWith(QString::fromUtf8(".torrent"))) |
|
|
|
|
|
|
|
destination += QString::fromUtf8(".torrent"); |
|
|
|
|
|
|
|
txt_destination->setText(destination); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_addFolder_button_clicked(){ |
|
|
|
void createtorrent::on_addFolder_button_clicked(){ |
|
|
|
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), QDir::homePath(), QFileDialog::ShowDirsOnly); |
|
|
|
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), QDir::homePath(), QFileDialog::ShowDirsOnly); |
|
|
|
if(!dir.isEmpty()) { |
|
|
|
if(!dir.isEmpty()) |
|
|
|
input_list->addItem(dir); |
|
|
|
textInputPath->setText(dir); |
|
|
|
addFolder_button->setEnabled(false); |
|
|
|
|
|
|
|
addFile_button->setEnabled(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_addFile_button_clicked(){ |
|
|
|
void createtorrent::on_addFile_button_clicked(){ |
|
|
|
QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), QDir::homePath(), QString(), 0, QFileDialog::ShowDirsOnly); |
|
|
|
QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), QDir::homePath(), QString(), 0, QFileDialog::ShowDirsOnly); |
|
|
|
if(!file.isEmpty()) { |
|
|
|
if(!file.isEmpty()) |
|
|
|
input_list->addItem(file); |
|
|
|
textInputPath->setText(file); |
|
|
|
addFolder_button->setEnabled(false); |
|
|
|
|
|
|
|
addFile_button->setEnabled(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_removeFolder_button_clicked(){ |
|
|
|
void createtorrent::on_removeTracker_button_clicked() { |
|
|
|
QModelIndexList selectedIndexes = input_list->selectionModel()->selectedIndexes(); |
|
|
|
|
|
|
|
if(!selectedIndexes.size()) return; |
|
|
|
|
|
|
|
Q_ASSERT(selectedIndexes.size() == 1); |
|
|
|
|
|
|
|
QListWidgetItem *item = input_list->takeItem(selectedIndexes.first().row()); |
|
|
|
|
|
|
|
delete item; |
|
|
|
|
|
|
|
addFolder_button->setEnabled(true); |
|
|
|
|
|
|
|
addFile_button->setEnabled(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_removeTracker_button_clicked(){ |
|
|
|
|
|
|
|
QModelIndexList selectedIndexes = trackers_list->selectionModel()->selectedIndexes(); |
|
|
|
QModelIndexList selectedIndexes = trackers_list->selectionModel()->selectedIndexes(); |
|
|
|
for(int i=selectedIndexes.size()-1; i>=0; --i){ |
|
|
|
for(int i=selectedIndexes.size()-1; i>=0; --i){ |
|
|
|
QListWidgetItem *item = trackers_list->takeItem(selectedIndexes.at(i).row()); |
|
|
|
QListWidgetItem *item = trackers_list->takeItem(selectedIndexes.at(i).row()); |
|
|
@ -98,7 +66,28 @@ void createtorrent::on_removeTracker_button_clicked(){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_addTracker_button_clicked(){ |
|
|
|
int createtorrent::getPieceSize() const { |
|
|
|
|
|
|
|
switch(comboPieceSize->currentIndex()) { |
|
|
|
|
|
|
|
case 0: |
|
|
|
|
|
|
|
return 32*1024; |
|
|
|
|
|
|
|
case 1: |
|
|
|
|
|
|
|
return 64*1024; |
|
|
|
|
|
|
|
case 2: |
|
|
|
|
|
|
|
return 128*1024; |
|
|
|
|
|
|
|
case 3: |
|
|
|
|
|
|
|
return 256*1024; |
|
|
|
|
|
|
|
case 4: |
|
|
|
|
|
|
|
return 512*1024; |
|
|
|
|
|
|
|
case 5: |
|
|
|
|
|
|
|
return 1024*1024; |
|
|
|
|
|
|
|
case 6: |
|
|
|
|
|
|
|
return 2048*1024; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return 256*1024; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void createtorrent::on_addTracker_button_clicked() { |
|
|
|
bool ok; |
|
|
|
bool ok; |
|
|
|
QString URL = QInputDialog::getText(this, tr("Please type an announce URL"), |
|
|
|
QString URL = QInputDialog::getText(this, tr("Please type an announce URL"), |
|
|
|
tr("Announce URL:", "Tracker URL"), QLineEdit::Normal, |
|
|
|
tr("Announce URL:", "Tracker URL"), QLineEdit::Normal, |
|
|
@ -151,61 +140,67 @@ QStringList createtorrent::allItems(QListWidget *list){ |
|
|
|
|
|
|
|
|
|
|
|
// Main function that create a .torrent file
|
|
|
|
// Main function that create a .torrent file
|
|
|
|
void createtorrent::on_createButton_clicked(){ |
|
|
|
void createtorrent::on_createButton_clicked(){ |
|
|
|
QString destination = txt_destination->text(); |
|
|
|
QString input = textInputPath->text().trimmed(); |
|
|
|
if(destination.isEmpty()){ |
|
|
|
if(input.isEmpty() == 0){ |
|
|
|
QMessageBox::critical(0, tr("No destination path set"), tr("Please type a destination path first")); |
|
|
|
QMessageBox::critical(0, tr("No input path set"), tr("Please type an input path first")); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
QStringList input = allItems(input_list); |
|
|
|
QStringList trackers = allItems(trackers_list); |
|
|
|
if(input.size() == 0){ |
|
|
|
if(!trackers.size()){ |
|
|
|
QMessageBox::critical(0, tr("No input path set"), tr("Please type an input path first")); |
|
|
|
QMessageBox::critical(0, tr("No tracker path set"), tr("Please set at least one tracker")); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), QDir::homePath(), tr("Torrent Files")+QString::fromUtf8(" (*.torrent)")); |
|
|
|
|
|
|
|
if(!destination.isEmpty()) { |
|
|
|
|
|
|
|
if(!destination.endsWith(QString::fromUtf8(".torrent"))) |
|
|
|
|
|
|
|
destination += QString::fromUtf8(".torrent"); |
|
|
|
|
|
|
|
} else { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
char const* creator_str = "qBittorrent "VERSION; |
|
|
|
char const* creator_str = "qBittorrent "VERSION; |
|
|
|
try { |
|
|
|
try { |
|
|
|
torrent_info t; |
|
|
|
boost::intrusive_ptr<torrent_info> t(new torrent_info); |
|
|
|
ofstream out(complete(path((const char*)destination.toUtf8())), std::ios_base::binary); |
|
|
|
ofstream out(complete(path((const char*)destination.toUtf8())), std::ios_base::binary); |
|
|
|
path full_path; |
|
|
|
path full_path; |
|
|
|
// Adding files to the torrent
|
|
|
|
// Adding files to the torrent
|
|
|
|
QString input_path; |
|
|
|
full_path = complete(path(input.toUtf8().data())); |
|
|
|
foreach(input_path, input){ |
|
|
|
add_files(*t, full_path.branch_path(), full_path.leaf()); |
|
|
|
full_path = complete(path(input_path.toUtf8().data())); |
|
|
|
// Set piece size
|
|
|
|
add_files(t, full_path.branch_path(), full_path.leaf()); |
|
|
|
int piece_size = getPieceSize(); |
|
|
|
} |
|
|
|
t->set_piece_size(piece_size); |
|
|
|
int piece_size = 256 * 1024; |
|
|
|
|
|
|
|
t.set_piece_size(piece_size); |
|
|
|
|
|
|
|
// Add url seeds
|
|
|
|
// Add url seeds
|
|
|
|
QStringList urlSeeds = allItems(URLSeeds_list); |
|
|
|
QStringList urlSeeds = allItems(URLSeeds_list); |
|
|
|
QString seed; |
|
|
|
QString seed; |
|
|
|
foreach(seed, urlSeeds){ |
|
|
|
foreach(seed, urlSeeds){ |
|
|
|
t.add_url_seed(seed.toUtf8().data()); |
|
|
|
t->add_url_seed(seed.toUtf8().data()); |
|
|
|
} |
|
|
|
} |
|
|
|
QStringList trackers = allItems(trackers_list); |
|
|
|
|
|
|
|
for(int i=0; i<trackers.size(); ++i){ |
|
|
|
for(int i=0; i<trackers.size(); ++i){ |
|
|
|
t.add_tracker(trackers.at(i).toUtf8().data()); |
|
|
|
t->add_tracker(trackers.at(i).toUtf8().data()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
file_pool fp; |
|
|
|
file_pool fp; |
|
|
|
boost::scoped_ptr<storage_interface> st(default_storage_constructor(t, full_path.branch_path(), fp)); |
|
|
|
boost::scoped_ptr<storage_interface> st(default_storage_constructor(t, full_path.branch_path(), fp)); |
|
|
|
int num = t.num_pieces(); |
|
|
|
int num = t->num_pieces(); |
|
|
|
std::vector<char> buf(piece_size); |
|
|
|
std::vector<char> buf(piece_size); |
|
|
|
for (int i = 0; i < num; ++i) { |
|
|
|
for (int i = 0; i < num; ++i) { |
|
|
|
st->read(&buf[0], i, 0, t.piece_size(i)); |
|
|
|
st->read(&buf[0], i, 0, t->piece_size(i)); |
|
|
|
hasher h(&buf[0], t.piece_size(i)); |
|
|
|
hasher h(&buf[0], t->piece_size(i)); |
|
|
|
t.set_hash(i, h.final()); |
|
|
|
t->set_hash(i, h.final()); |
|
|
|
} |
|
|
|
} |
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
|
|
|
// torrent_info structure
|
|
|
|
t.set_creator(creator_str); |
|
|
|
t->set_creator(creator_str); |
|
|
|
t.set_comment((const char*)txt_comment->toPlainText().toUtf8()); |
|
|
|
t->set_comment((const char*)txt_comment->toPlainText().toUtf8()); |
|
|
|
// Is private ?
|
|
|
|
// Is private ?
|
|
|
|
if(check_private->isChecked()){ |
|
|
|
if(check_private->isChecked()){ |
|
|
|
t.set_priv(true); |
|
|
|
t->set_priv(true); |
|
|
|
} |
|
|
|
} |
|
|
|
// create the torrent and print it to out
|
|
|
|
// create the torrent and print it to out
|
|
|
|
entry e = t.create_torrent(); |
|
|
|
entry e = t->create_torrent(); |
|
|
|
libtorrent::bencode(std::ostream_iterator<char>(out), e); |
|
|
|
libtorrent::bencode(std::ostream_iterator<char>(out), e); |
|
|
|
|
|
|
|
if(checkStartSeeding->isChecked()) |
|
|
|
|
|
|
|
emit torrent_to_seed(destination); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (std::exception& e){ |
|
|
|
catch (std::exception& e){ |
|
|
|
std::cerr << e.what() << "\n"; |
|
|
|
std::cerr << e.what() << "\n"; |
|
|
|