Browse Source

Allow to load TorrentInfo from data buffer

adaptive-webui-19844
Vladimir Golovnev (Glassez) 7 years ago
parent
commit
69df8174b9
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 27
      src/base/bittorrent/torrentinfo.cpp
  2. 4
      src/base/bittorrent/torrentinfo.h
  3. 2
      src/gui/addnewtorrentdialog.cpp

27
src/base/bittorrent/torrentinfo.cpp

@ -60,23 +60,32 @@ TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other) @@ -60,23 +60,32 @@ TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other)
return *this;
}
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString &error)
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
{
error.clear();
libt::error_code ec;
TorrentInfo info(NativePtr(new libt::torrent_info(Utils::Fs::toNativePath(path).toStdString(), ec)));
if (ec) {
error = QString::fromUtf8(ec.message().c_str());
qDebug("Cannot load .torrent file: %s", qUtf8Printable(error));
TorrentInfo info(NativePtr(new libt::torrent_info(data.constData(), data.size(), ec)));
if (error) {
if (ec)
*error = QString::fromStdString(ec.message());
else
error->clear();
}
return info;
}
TorrentInfo TorrentInfo::loadFromFile(const QString &path)
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
{
QString error;
return loadFromFile(path, error);
libt::error_code ec;
TorrentInfo info(NativePtr(new libt::torrent_info(Utils::Fs::toNativePath(path).toStdString(), ec)));
if (error) {
if (ec)
*error = QString::fromStdString(ec.message());
else
error->clear();
}
return info;
}
bool TorrentInfo::isValid() const

4
src/base/bittorrent/torrentinfo.h

@ -63,8 +63,8 @@ namespace BitTorrent @@ -63,8 +63,8 @@ namespace BitTorrent
explicit TorrentInfo(NativeConstPtr nativeInfo = NativeConstPtr());
TorrentInfo(const TorrentInfo &other);
static TorrentInfo loadFromFile(const QString &path, QString &error);
static TorrentInfo loadFromFile(const QString &path);
static TorrentInfo load(const QByteArray &data, QString *error = nullptr) noexcept;
static TorrentInfo loadFromFile(const QString &path, QString *error = nullptr) noexcept;
TorrentInfo &operator=(const TorrentInfo &other);

2
src/gui/addnewtorrentdialog.cpp

@ -286,7 +286,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString &torrentPath) @@ -286,7 +286,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString &torrentPath)
m_hasMetadata = true;
QString error;
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(m_filePath, error);
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(m_filePath, &error);
if (!m_torrentInfo.isValid()) {
MessageBoxRaised::critical(this, tr("Invalid torrent"), tr("Failed to load the torrent: %1.\nError: %2", "Don't remove the '\n' characters. They insert a newline.").arg(Utils::Fs::toNativePath(m_filePath)).arg(error));
return false;

Loading…
Cancel
Save