diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index ca6976b85..950d772c3 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -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 diff --git a/src/base/bittorrent/torrentinfo.h b/src/base/bittorrent/torrentinfo.h index 6730d2f11..cb40c3927 100644 --- a/src/base/bittorrent/torrentinfo.h +++ b/src/base/bittorrent/torrentinfo.h @@ -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); diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 4f2c7250c..0af079e49 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -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;