2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 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
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-06 00:27:26 +00:00
|
|
|
#include <QFileInfo>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h> /* mime64 */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2010-08-01 18:55:04 +00:00
|
|
|
#include "add-data.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "file-tree.h"
|
2013-02-09 04:05:03 +00:00
|
|
|
#include "freespace-label.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "session.h"
|
|
|
|
#include "torrent.h"
|
2010-05-13 23:54:32 +00:00
|
|
|
#include "utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::OptionsDialog (Session& session, const Prefs& prefs, const AddData& addme, QWidget * parent):
|
2013-02-09 21:47:42 +00:00
|
|
|
QDialog (parent, Qt::Dialog),
|
|
|
|
mySession (session),
|
|
|
|
myAdd (addme),
|
|
|
|
myHaveInfo (false),
|
2014-12-29 04:03:56 +00:00
|
|
|
myVerifyButton (nullptr),
|
|
|
|
myVerifyFile (nullptr),
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyHash (QCryptographicHash::Sha1),
|
|
|
|
myEditTimer (this)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.setupUi (this);
|
2013-02-09 21:47:42 +00:00
|
|
|
|
2013-02-09 23:11:17 +00:00
|
|
|
QString title;
|
|
|
|
if (myAdd.type == AddData::FILENAME)
|
|
|
|
title = tr ("Open Torrent from File");
|
|
|
|
else
|
|
|
|
title = tr ("Open Torrent from URL or Magnet Link");
|
|
|
|
setWindowTitle (title);
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
myEditTimer.setInterval (2000);
|
|
|
|
myEditTimer.setSingleShot (true);
|
2014-12-31 21:00:34 +00:00
|
|
|
connect (&myEditTimer, SIGNAL (timeout ()), this, SLOT (onDestinationChanged ()));
|
2013-02-09 23:11:17 +00:00
|
|
|
|
|
|
|
if (myAdd.type == AddData::FILENAME)
|
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.sourceStack->setCurrentWidget (ui.sourceButton);
|
2014-12-31 21:00:34 +00:00
|
|
|
ui.sourceButton->setMode (TrPathButton::FileMode);
|
|
|
|
ui.sourceButton->setTitle (tr ("Open Torrent"));
|
|
|
|
ui.sourceButton->setNameFilter (tr ("Torrent Files (*.torrent);;All Files (*.*)"));
|
|
|
|
ui.sourceButton->setPath (myAdd.filename);
|
|
|
|
connect (ui.sourceButton, SIGNAL (pathChanged (QString)), this, SLOT (onSourceChanged ()));
|
2013-02-09 23:11:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.sourceStack->setCurrentWidget (ui.sourceEdit);
|
|
|
|
ui.sourceEdit->setText (myAdd.readableName ());
|
|
|
|
ui.sourceEdit->selectAll ();
|
2014-12-31 21:00:34 +00:00
|
|
|
connect (ui.sourceEdit, SIGNAL (editingFinished ()), this, SLOT (onSourceChanged ()));
|
2013-02-09 23:11:17 +00:00
|
|
|
}
|
2013-02-09 21:47:42 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.sourceStack->setFixedHeight (ui.sourceStack->currentWidget ()->sizeHint ().height ());
|
|
|
|
ui.sourceLabel->setBuddy (ui.sourceStack->currentWidget ());
|
|
|
|
|
|
|
|
const QFontMetrics fontMetrics (font ());
|
2013-07-27 21:58:14 +00:00
|
|
|
const int width = fontMetrics.size (0, QString::fromUtf8 ("This is a pretty long torrent filename indeed.torrent")).width ();
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.sourceStack->setMinimumWidth (width);
|
2013-02-09 21:47:42 +00:00
|
|
|
|
2014-12-31 21:00:34 +00:00
|
|
|
const QString downloadDir (Utils::removeTrailingDirSeparator (prefs.getString (Prefs::DOWNLOAD_DIR)));
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.freeSpaceLabel->setSession (mySession);
|
2014-12-31 21:00:34 +00:00
|
|
|
ui.freeSpaceLabel->setPath (downloadDir);
|
2013-02-09 21:47:42 +00:00
|
|
|
|
|
|
|
if (session.isLocal ())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.destinationStack->setCurrentWidget (ui.destinationButton);
|
2014-12-31 21:00:34 +00:00
|
|
|
ui.destinationButton->setMode (TrPathButton::DirectoryMode);
|
|
|
|
ui.destinationButton->setTitle (tr ("Select Destination"));
|
|
|
|
ui.destinationButton->setPath (downloadDir);
|
|
|
|
myLocalDestination = downloadDir;
|
|
|
|
connect (ui.destinationButton, SIGNAL (pathChanged (QString)), this, SLOT (onDestinationChanged ()));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-02-09 21:47:42 +00:00
|
|
|
else
|
2012-07-09 23:18:40 +00:00
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.destinationStack->setCurrentWidget (ui.destinationEdit);
|
|
|
|
ui.destinationEdit->setText (downloadDir);
|
|
|
|
ui.freeSpaceLabel->setPath (downloadDir);
|
|
|
|
connect (ui.destinationEdit, SIGNAL (textEdited (QString)), &myEditTimer, SLOT (start ()));
|
2014-12-31 21:00:34 +00:00
|
|
|
connect (ui.destinationEdit, SIGNAL (editingFinished ()), this, SLOT (onDestinationChanged ()));
|
2012-07-09 23:18:40 +00:00
|
|
|
}
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.destinationStack->setFixedHeight (ui.destinationStack->currentWidget ()->sizeHint ().height ());
|
|
|
|
ui.destinationLabel->setBuddy (ui.destinationStack->currentWidget ());
|
2013-02-09 21:47:42 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->setEditable (false);
|
2013-02-09 21:47:42 +00:00
|
|
|
if (!session.isLocal ())
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->hideColumn (2); // hide the % done, since we've no way of knowing
|
|
|
|
|
|
|
|
ui.priorityCombo->addItem (tr ("High"), TR_PRI_HIGH);
|
|
|
|
ui.priorityCombo->addItem (tr ("Normal"), TR_PRI_NORMAL);
|
|
|
|
ui.priorityCombo->addItem (tr ("Low"), TR_PRI_LOW);
|
|
|
|
ui.priorityCombo->setCurrentIndex (1); // Normal
|
2013-02-09 21:47:42 +00:00
|
|
|
|
|
|
|
if (session.isLocal ())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
myVerifyButton = new QPushButton (tr ("&Verify Local Data"), this);
|
|
|
|
ui.dialogButtons->addButton (myVerifyButton, QDialogButtonBox::ActionRole);
|
|
|
|
connect (myVerifyButton, SIGNAL (clicked (bool)), this, SLOT (onVerify ()));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.startCheck->setChecked (prefs.getBool (Prefs::START));
|
|
|
|
ui.trashCheck->setChecked (prefs.getBool (Prefs::TRASH_ORIGINAL));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
connect (ui.dialogButtons, SIGNAL (rejected ()), this, SLOT (deleteLater ()));
|
|
|
|
connect (ui.dialogButtons, SIGNAL (accepted ()), this, SLOT (onAccepted ()));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
connect (ui.filesView, SIGNAL (priorityChanged (QSet<int>, int)), this, SLOT (onPriorityChanged (QSet<int>, int)));
|
|
|
|
connect (ui.filesView, SIGNAL (wantedChanged (QSet<int>, bool)), this, SLOT (onWantedChanged (QSet<int>, bool)));
|
2009-05-03 22:00:56 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
connect (&myVerifyTimer, SIGNAL (timeout ()), this, SLOT (onTimeout ()));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
reload ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::~OptionsDialog ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
clearInfo ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::clearInfo ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
if (myHaveInfo)
|
|
|
|
tr_metainfoFree (&myInfo);
|
|
|
|
|
|
|
|
myHaveInfo = false;
|
|
|
|
myFiles.clear ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::reload ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
clearInfo ();
|
|
|
|
clearVerify ();
|
|
|
|
|
|
|
|
tr_ctor * ctor = tr_ctorNew (0);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
switch (myAdd.type)
|
|
|
|
{
|
|
|
|
case AddData::MAGNET:
|
|
|
|
tr_ctorSetMetainfoFromMagnetLink (ctor, myAdd.magnet.toUtf8 ().constData ());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddData::FILENAME:
|
|
|
|
tr_ctorSetMetainfoFromFile (ctor, myAdd.filename.toUtf8 ().constData ());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddData::METAINFO:
|
2014-12-29 04:03:56 +00:00
|
|
|
tr_ctorSetMetainfo (ctor, reinterpret_cast<const quint8*> (myAdd.metainfo.constData ()), myAdd.metainfo.size ());
|
2013-02-09 21:47:42 +00:00
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2010-05-13 23:54:32 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
const int err = tr_torrentParse (ctor, &myInfo);
|
|
|
|
myHaveInfo = !err;
|
|
|
|
tr_ctorFree (ctor);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->clear ();
|
2013-02-09 21:47:42 +00:00
|
|
|
myFiles.clear ();
|
|
|
|
myPriorities.clear ();
|
|
|
|
myWanted.clear ();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
const bool haveFilesToShow = myHaveInfo && myInfo.fileCount > 0;
|
|
|
|
|
|
|
|
ui.filesView->setVisible (haveFilesToShow);
|
|
|
|
if (myVerifyButton != nullptr)
|
|
|
|
myVerifyButton->setVisible (haveFilesToShow);
|
|
|
|
layout ()->setSizeConstraint (haveFilesToShow ? QLayout::SetDefaultConstraint : QLayout::SetFixedSize);
|
2013-02-10 22:44:25 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (myHaveInfo)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
myPriorities.insert (0, myInfo.fileCount, TR_PRI_NORMAL);
|
|
|
|
myWanted.insert (0, myInfo.fileCount, true);
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
for (tr_file_index_t i = 0; i < myInfo.fileCount; ++i)
|
2013-02-09 21:47:42 +00:00
|
|
|
{
|
|
|
|
TrFile file;
|
|
|
|
file.index = i;
|
|
|
|
file.priority = myPriorities[i];
|
|
|
|
file.wanted = myWanted[i];
|
|
|
|
file.size = myInfo.files[i].length;
|
|
|
|
file.have = 0;
|
|
|
|
file.filename = QString::fromUtf8 (myInfo.files[i].name);
|
|
|
|
myFiles.append (file);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->update (myFiles);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::onPriorityChanged (const QSet<int>& fileIndices, int priority)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
foreach (int i, fileIndices)
|
|
|
|
myPriorities[i] = priority;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::onWantedChanged (const QSet<int>& fileIndices, bool isWanted)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
foreach (int i, fileIndices)
|
|
|
|
myWanted[i] = isWanted;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::onAccepted ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
// rpc spec section 3.4 "adding a torrent"
|
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict (&args, 10);
|
2013-02-09 21:47:42 +00:00
|
|
|
QString downloadDir;
|
|
|
|
|
|
|
|
// "download-dir"
|
2014-12-29 04:03:56 +00:00
|
|
|
if (ui.destinationStack->currentWidget () == ui.destinationButton)
|
2013-02-09 23:11:17 +00:00
|
|
|
downloadDir = myLocalDestination.absolutePath ();
|
2013-02-09 21:47:42 +00:00
|
|
|
else
|
2014-12-29 04:03:56 +00:00
|
|
|
downloadDir = ui.destinationEdit->text ();
|
2013-02-09 23:11:17 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variantDictAddStr (&args, TR_KEY_download_dir, downloadDir.toUtf8 ().constData ());
|
2013-02-09 21:47:42 +00:00
|
|
|
|
|
|
|
// paused
|
2014-12-29 04:03:56 +00:00
|
|
|
tr_variantDictAddBool (&args, TR_KEY_paused, !ui.startCheck->isChecked ());
|
2013-02-09 21:47:42 +00:00
|
|
|
|
|
|
|
// priority
|
2014-12-29 04:03:56 +00:00
|
|
|
const int index = ui.priorityCombo->currentIndex ();
|
|
|
|
const int priority = ui.priorityCombo->itemData (index).toInt ();
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variantDictAddInt (&args, TR_KEY_bandwidthPriority, priority);
|
2013-02-09 21:47:42 +00:00
|
|
|
|
|
|
|
// files-unwanted
|
|
|
|
int count = myWanted.count (false);
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variant * l = tr_variantDictAddList (&args, TR_KEY_files_unwanted, count);
|
2014-12-29 04:03:56 +00:00
|
|
|
for (int i = 0, n = myWanted.size (); i < n; ++i)
|
|
|
|
{
|
|
|
|
if (myWanted.at (i) == false)
|
|
|
|
tr_variantListAddInt (l, i);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
// priority-low
|
|
|
|
count = myPriorities.count (TR_PRI_LOW);
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variant * l = tr_variantDictAddList (&args, TR_KEY_priority_low, count);
|
2014-12-29 04:03:56 +00:00
|
|
|
for (int i = 0, n = myPriorities.size (); i < n; ++i)
|
|
|
|
{
|
|
|
|
if (myPriorities.at (i) == TR_PRI_LOW)
|
|
|
|
tr_variantListAddInt (l, i);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
// priority-high
|
|
|
|
count = myPriorities.count (TR_PRI_HIGH);
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variant * l = tr_variantDictAddList (&args, TR_KEY_priority_high, count);
|
2014-12-29 04:03:56 +00:00
|
|
|
for (int i = 0, n = myPriorities.size (); i < n; ++i)
|
|
|
|
{
|
|
|
|
if (myPriorities.at (i) == TR_PRI_HIGH)
|
|
|
|
tr_variantListAddInt (l, i);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
mySession.addTorrent (myAdd, &args, ui.trashCheck->isChecked ());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
deleteLater ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-31 21:00:34 +00:00
|
|
|
OptionsDialog::onSourceChanged ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-31 21:00:34 +00:00
|
|
|
if (ui.sourceStack->currentWidget () == ui.sourceButton)
|
|
|
|
myAdd.set (ui.sourceButton->path ());
|
|
|
|
else
|
|
|
|
myAdd.set (ui.sourceEdit->text ());
|
2014-12-29 04:03:56 +00:00
|
|
|
|
|
|
|
reload ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 23:11:17 +00:00
|
|
|
void
|
2014-12-31 21:00:34 +00:00
|
|
|
OptionsDialog::onDestinationChanged ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-31 21:00:34 +00:00
|
|
|
if (ui.destinationStack->currentWidget () == ui.destinationButton)
|
|
|
|
{
|
|
|
|
myLocalDestination = ui.destinationButton->path ();
|
|
|
|
ui.freeSpaceLabel->setPath (myLocalDestination.absolutePath ());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui.freeSpaceLabel->setPath (ui.destinationEdit->text ());
|
|
|
|
}
|
2013-02-09 04:42:07 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** VERIFY
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::clearVerify ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyHash.reset ();
|
|
|
|
myVerifyFile.close ();
|
|
|
|
myVerifyFilePos = 0;
|
|
|
|
myVerifyFlags.clear ();
|
|
|
|
myVerifyFileIndex = 0;
|
|
|
|
myVerifyPieceIndex = 0;
|
|
|
|
myVerifyPiecePos = 0;
|
|
|
|
myVerifyTimer.stop ();
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
for (int i = 0, n = myFiles.size (); i < n; ++i)
|
2013-02-09 21:47:42 +00:00
|
|
|
myFiles[i].have = 0;
|
|
|
|
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->update (myFiles);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::onVerify ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
clearVerify ();
|
|
|
|
myVerifyFlags.insert (0, myInfo.pieceCount, false);
|
|
|
|
myVerifyTimer.setSingleShot (false);
|
|
|
|
myVerifyTimer.start (0);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t getPieceSize (const tr_info * info, tr_piece_index_t pieceIndex)
|
2013-02-09 21:47:42 +00:00
|
|
|
{
|
|
|
|
if (pieceIndex != info->pieceCount - 1)
|
|
|
|
return info->pieceSize;
|
|
|
|
return info->totalSize % info->pieceSize;
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-29 04:03:56 +00:00
|
|
|
OptionsDialog::onTimeout ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
if (myFiles.isEmpty ())
|
2013-02-10 22:44:25 +00:00
|
|
|
{
|
|
|
|
myVerifyTimer.stop ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
const tr_file * file = &myInfo.files[myVerifyFileIndex];
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (!myVerifyFilePos && !myVerifyFile.isOpen ())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 23:11:17 +00:00
|
|
|
const QFileInfo fileInfo (myLocalDestination, QString::fromUtf8 (file->name));
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyFile.setFileName (fileInfo.absoluteFilePath ());
|
|
|
|
myVerifyFile.open (QIODevice::ReadOnly);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 00:37:37 +00:00
|
|
|
int64_t leftInPiece = getPieceSize (&myInfo, myVerifyPieceIndex) - myVerifyPiecePos;
|
|
|
|
int64_t leftInFile = file->length - myVerifyFilePos;
|
2014-12-29 04:03:56 +00:00
|
|
|
int64_t bytesThisPass = qMin (leftInFile, leftInPiece);
|
|
|
|
bytesThisPass = qMin (bytesThisPass, static_cast<int64_t> (sizeof (myVerifyBuf)));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (myVerifyFile.isOpen () && myVerifyFile.seek (myVerifyFilePos))
|
|
|
|
{
|
2013-08-29 00:37:37 +00:00
|
|
|
int64_t numRead = myVerifyFile.read (myVerifyBuf, bytesThisPass);
|
2013-02-09 21:47:42 +00:00
|
|
|
if (numRead == bytesThisPass)
|
|
|
|
myVerifyHash.addData (myVerifyBuf, numRead);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
leftInPiece -= bytesThisPass;
|
|
|
|
leftInFile -= bytesThisPass;
|
|
|
|
myVerifyPiecePos += bytesThisPass;
|
|
|
|
myVerifyFilePos += bytesThisPass;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyBins[myVerifyFileIndex] += bytesThisPass;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (leftInPiece == 0)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
const QByteArray result (myVerifyHash.result ());
|
|
|
|
const bool matches = !memcmp (result.constData (),
|
|
|
|
myInfo.pieces[myVerifyPieceIndex].hash,
|
|
|
|
SHA_DIGEST_LENGTH);
|
|
|
|
myVerifyFlags[myVerifyPieceIndex] = matches;
|
|
|
|
myVerifyPiecePos = 0;
|
|
|
|
++myVerifyPieceIndex;
|
|
|
|
myVerifyHash.reset ();
|
|
|
|
|
|
|
|
FileList changedFiles;
|
|
|
|
if (matches)
|
|
|
|
{
|
2014-12-29 04:03:56 +00:00
|
|
|
for (auto i = myVerifyBins.begin (), end = myVerifyBins.end (); i != end; ++i)
|
2013-02-09 21:47:42 +00:00
|
|
|
{
|
|
|
|
TrFile& f (myFiles[i.key ()]);
|
|
|
|
f.have += i.value ();
|
|
|
|
changedFiles.append (f);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-29 04:03:56 +00:00
|
|
|
ui.filesView->update (changedFiles);
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyBins.clear ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (leftInFile == 0)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
myVerifyFile.close ();
|
|
|
|
++myVerifyFileIndex;
|
|
|
|
myVerifyFilePos = 0;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
bool done = myVerifyPieceIndex >= myInfo.pieceCount;
|
|
|
|
if (done)
|
2010-08-04 16:25:30 +00:00
|
|
|
{
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t have = 0;
|
2013-02-09 21:47:42 +00:00
|
|
|
foreach (const TrFile& f, myFiles)
|
|
|
|
have += f.have;
|
2010-08-04 16:25:30 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (!have) // everything failed
|
2010-08-04 16:25:30 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
// did the user accidentally specify the child directory instead of the parent?
|
2014-12-27 14:07:14 +00:00
|
|
|
const QStringList tokens = QString::fromUtf8 (file->name).split ('/');
|
2014-12-29 04:03:56 +00:00
|
|
|
if (!tokens.empty () && myLocalDestination.dirName () == tokens.at (0))
|
2010-08-04 16:25:30 +00:00
|
|
|
{
|
2013-02-09 21:47:42 +00:00
|
|
|
// move up one directory and try again
|
2013-02-09 23:11:17 +00:00
|
|
|
myLocalDestination.cdUp ();
|
2013-02-09 21:47:42 +00:00
|
|
|
onVerify ();
|
|
|
|
done = false;
|
2010-08-04 16:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-02-09 21:47:42 +00:00
|
|
|
if (done)
|
|
|
|
myVerifyTimer.stop ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|