2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-31 22:27:46 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
2013-07-27 21:58:14 +00:00
|
|
|
#include <QMimeData>
|
2009-10-05 19:53:38 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTimer>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/makemeta.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "ColumnResizer.h"
|
|
|
|
#include "Formatter.h"
|
|
|
|
#include "MakeDialog.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "ui_MakeProgressDialog.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-12-31 22:27:46 +00:00
|
|
|
namespace
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class MakeProgressDialog : public BaseDialog
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
MakeProgressDialog(Session& session, tr_metainfo_builder& builder, QWidget* parent = nullptr);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void onButtonBoxClicked(QAbstractButton*);
|
|
|
|
void onProgress();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Session& mySession;
|
|
|
|
tr_metainfo_builder& myBuilder;
|
|
|
|
Ui::MakeProgressDialog ui;
|
|
|
|
QTimer myTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
MakeProgressDialog::MakeProgressDialog(Session& session, tr_metainfo_builder& builder, QWidget* parent) :
|
|
|
|
BaseDialog(parent),
|
|
|
|
mySession(session),
|
|
|
|
myBuilder(builder)
|
2014-12-31 22:27:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.setupUi(this);
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(ui.dialogButtons, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonBoxClicked(QAbstractButton*)));
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(&myTimer, SIGNAL(timeout()), this, SLOT(onProgress()));
|
|
|
|
myTimer.start(100);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
onProgress();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeProgressDialog::onButtonBoxClicked(QAbstractButton* button)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (ui.dialogButtons->standardButton(button))
|
2009-10-05 19:53:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case QDialogButtonBox::Open:
|
|
|
|
mySession.addNewlyCreatedTorrent(QString::fromUtf8(myBuilder.outputFile),
|
|
|
|
QFileInfo(QString::fromUtf8(myBuilder.top)).dir().path());
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QDialogButtonBox::Abort:
|
2014-12-31 22:27:46 +00:00
|
|
|
myBuilder.abortFlag = true;
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default: // QDialogButtonBox::Ok:
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2009-10-05 19:53:38 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
close();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeProgressDialog::onProgress()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// progress bar
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_metainfo_builder const& b = myBuilder;
|
2017-04-30 16:25:26 +00:00
|
|
|
double const denom = b.pieceCount != 0 ? b.pieceCount : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.progressBar->setValue(static_cast<int>((100.0 * b.pieceIndex) / denom));
|
|
|
|
|
|
|
|
// progress label
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const top = QString::fromUtf8(b.top);
|
|
|
|
QString const base(QFileInfo(top).completeBaseName());
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
|
|
|
|
|
|
|
if (!b.isDone)
|
|
|
|
{
|
|
|
|
str = tr("Creating \"%1\"").arg(base);
|
|
|
|
}
|
|
|
|
else if (b.result == TR_MAKEMETA_OK)
|
|
|
|
{
|
|
|
|
str = tr("Created \"%1\"!").arg(base);
|
|
|
|
}
|
|
|
|
else if (b.result == TR_MAKEMETA_URL)
|
|
|
|
{
|
|
|
|
str = tr("Error: invalid announce URL \"%1\"").arg(QString::fromUtf8(b.errfile));
|
|
|
|
}
|
|
|
|
else if (b.result == TR_MAKEMETA_CANCELLED)
|
|
|
|
{
|
|
|
|
str = tr("Cancelled");
|
|
|
|
}
|
|
|
|
else if (b.result == TR_MAKEMETA_IO_READ)
|
|
|
|
{
|
|
|
|
str = tr("Error reading \"%1\": %2").arg(QString::fromUtf8(b.errfile)).
|
2019-02-10 11:05:16 +00:00
|
|
|
arg(QString::fromLocal8Bit(tr_strerror(b.my_errno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (b.result == TR_MAKEMETA_IO_WRITE)
|
|
|
|
{
|
|
|
|
str = tr("Error writing \"%1\": %2").arg(QString::fromUtf8(b.errfile)).
|
2019-02-10 11:05:16 +00:00
|
|
|
arg(QString::fromLocal8Bit(tr_strerror(b.my_errno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.progressLabel->setText(str);
|
|
|
|
|
|
|
|
// buttons
|
|
|
|
ui.dialogButtons->button(QDialogButtonBox::Abort)->setEnabled(!b.isDone);
|
|
|
|
ui.dialogButtons->button(QDialogButtonBox::Ok)->setEnabled(b.isDone);
|
|
|
|
ui.dialogButtons->button(QDialogButtonBox::Open)->setEnabled(b.isDone && b.result == TR_MAKEMETA_OK);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "MakeDialog.moc"
|
2014-12-31 22:27:46 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-10-05 19:53:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeDialog::makeTorrent()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myBuilder == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// get the tiers
|
|
|
|
int tier = 0;
|
|
|
|
QVector<tr_tracker_info> trackers;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& line : ui.trackersEdit->toPlainText().split(QLatin1Char('\n')))
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const announceUrl = line.trimmed();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (announceUrl.isEmpty())
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++tier;
|
2013-09-14 22:45:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_info tmp;
|
|
|
|
tmp.announce = tr_strdup(announceUrl.toUtf8().constData());
|
|
|
|
tmp.tier = tier;
|
|
|
|
trackers.append(tmp);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// the file to create
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const path = QString::fromUtf8(myBuilder->top);
|
|
|
|
QString const torrentName = QFileInfo(path).completeBaseName() + QLatin1String(".torrent");
|
|
|
|
QString const target = QDir(ui.destinationButton->path()).filePath(torrentName);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// comment
|
|
|
|
QString comment;
|
|
|
|
|
|
|
|
if (ui.commentCheck->isChecked())
|
|
|
|
{
|
|
|
|
comment = ui.commentEdit->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
// start making the torrent
|
2017-04-30 09:29:58 +00:00
|
|
|
tr_makeMetaInfo(myBuilder.get(), target.toUtf8().constData(), trackers.isEmpty() ? nullptr : trackers.data(),
|
|
|
|
trackers.size(), comment.isEmpty() ? nullptr : comment.toUtf8().constData(), ui.privateCheck->isChecked());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// pop up the dialog
|
|
|
|
MakeProgressDialog* dialog = new MakeProgressDialog(mySession, *myBuilder, this);
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->open();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2009-10-05 19:53:38 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString MakeDialog::getSource() const
|
2009-10-05 19:53:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return (ui.sourceFileRadio->isChecked() ? ui.sourceFileButton : ui.sourceFolderButton)->path();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeDialog::onSourceChanged()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myBuilder.reset();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const filename = getSource();
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!filename.isEmpty())
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myBuilder.reset(tr_metaInfoBuilderCreate(filename.toUtf8().constData()));
|
2013-09-14 22:45:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
if (myBuilder == nullptr)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
text = tr("<i>No source selected</i>");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
QString files = tr("%Ln File(s)", nullptr, myBuilder->fileCount);
|
|
|
|
QString pieces = tr("%Ln Piece(s)", nullptr, myBuilder->pieceCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
text = tr("%1 in %2; %3 @ %4").arg(Formatter::sizeToString(myBuilder->totalSize)).arg(files).arg(pieces).
|
2019-02-10 11:05:16 +00:00
|
|
|
arg(Formatter::sizeToString(myBuilder->pieceSize));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.sourceSizeLabel->setText(text);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
MakeDialog::MakeDialog(Session& session, QWidget* parent) :
|
|
|
|
BaseDialog(parent),
|
|
|
|
mySession(session),
|
|
|
|
myBuilder(nullptr, &tr_metaInfoBuilderFree)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.setupUi(this);
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.destinationButton->setMode(PathButton::DirectoryMode);
|
|
|
|
ui.destinationButton->setPath(QDir::homePath());
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.sourceFolderButton->setMode(PathButton::DirectoryMode);
|
|
|
|
ui.sourceFileButton->setMode(PathButton::FileMode);
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ColumnResizer* cr(new ColumnResizer(this));
|
|
|
|
cr->addLayout(ui.filesSectionLayout);
|
|
|
|
cr->addLayout(ui.propertiesSectionLayout);
|
|
|
|
cr->update();
|
2015-01-21 21:14:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
resize(minimumSizeHint());
|
2015-01-21 21:14:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(ui.sourceFolderRadio, SIGNAL(toggled(bool)), this, SLOT(onSourceChanged()));
|
|
|
|
connect(ui.sourceFolderButton, SIGNAL(pathChanged(QString)), this, SLOT(onSourceChanged()));
|
|
|
|
connect(ui.sourceFileRadio, SIGNAL(toggled(bool)), this, SLOT(onSourceChanged()));
|
|
|
|
connect(ui.sourceFileButton, SIGNAL(pathChanged(QString)), this, SLOT(onSourceChanged()));
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(ui.dialogButtons, SIGNAL(accepted()), this, SLOT(makeTorrent()));
|
|
|
|
connect(ui.dialogButtons, SIGNAL(rejected()), this, SLOT(close()));
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
onSourceChanged();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
MakeDialog::~MakeDialog()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
}
|
2010-08-02 20:55:11 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeDialog::dragEnterEvent(QDragEnterEvent* event)
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QMimeData const* mime = event->mimeData();
|
2010-08-02 20:55:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!mime->urls().isEmpty() && QFileInfo(mime->urls().front().path()).exists())
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
2010-08-02 20:55:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void MakeDialog::dropEvent(QDropEvent* event)
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const filename = event->mimeData()->urls().front().path();
|
|
|
|
QFileInfo const fileInfo(filename);
|
2010-08-02 20:55:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fileInfo.exists())
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fileInfo.isDir())
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.sourceFolderRadio->setChecked(true);
|
|
|
|
ui.sourceFolderButton->setPath(filename);
|
2010-08-02 20:55:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // it's a file
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ui.sourceFileRadio->setChecked(true);
|
|
|
|
ui.sourceFileButton->setPath(filename);
|
2010-08-02 20:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|