Esempio n. 1
0
void TorrentModel::handleTorrentUpdate(const QTorrentHandle &h)
{
  const int row = torrentRow(h.hash());
  if (row >= 0) {
    m_torrents[row]->refreshStatus(h.status(torrent_handle::query_accurate_download_counters));
    notifyTorrentChanged(row);
  }
}
Esempio n. 2
0
void TorrentModel::handleFinishedTorrent(const QTorrentHandle& h)
{
  const int row = torrentRow(h.hash());
  if (row < 0)
    return;

  // Update completion date
  m_torrents[row]->refreshStatus(h.status(torrent_handle::query_accurate_download_counters));
  notifyTorrentChanged(row);
}
Esempio n. 3
0
void TorrentModel::handleFinishedTorrent(const QTorrentHandle& h)
{
  const int row = torrentRow(h.hash());
  if (row < 0)
    return;

  // Update completion date
  m_torrents[row]->setData(TorrentModelItem::TR_SEED_DATE, QDateTime::currentDateTime(), Qt::DisplayRole);
  m_torrents[row]->refreshStatus(h.status(torrent_handle::query_accurate_download_counters));
  notifyTorrentChanged(row);
}
Esempio n. 4
0
TorrentModelItem::TorrentModelItem(const QTorrentHandle &h)
  : m_torrent(h)
  , m_lastStatus(h.status(torrent_handle::query_accurate_download_counters))
  , m_addedTime(TorrentPersistentData::getAddedDate(h.hash()))
  , m_label(TorrentPersistentData::getLabel(h.hash()))
  , m_name(TorrentPersistentData::getName(h.hash()))
  , m_hash(h.hash())
{
  if (m_name.isEmpty())
    m_name = h.name();
}
Esempio n. 5
0
void TorrentModel::handleTorrentUpdate(const QTorrentHandle &h)
{
    const int row = torrentRow(h.hash());
    if (row >= 0) {
        // This line changes the torrent name when magnet is retrieved.
        // When magnet link is added, "dn" parameter is used as name, but when metadata is retrieved
        // we change the name with the retrieved torrent name.
        m_torrents[row]->setData(TorrentModelItem::TR_NAME, h.name(), Qt::DisplayRole);

        m_torrents[row]->refreshStatus(h.status(torrent_handle::query_accurate_download_counters));
        notifyTorrentChanged(row);
    }
}
Esempio n. 6
0
TorrentModelItem::TorrentModelItem(const QTorrentHandle &h)
    : m_torrent(h)
    , m_lastStatus(h.status(torrent_handle::query_accurate_download_counters))
    , m_addedTime(TorrentPersistentData::instance()->getAddedDate(h.hash()))
    , m_label(TorrentPersistentData::instance()->getLabel(h.hash()))
    , m_name(TorrentPersistentData::instance()->getName(h.hash()))
    , m_hash(h.hash())
{
    if (m_name.isEmpty())
        m_name = h.name();
    // If name is empty show the hash. This happens when magnet isn't retrieved.
    if (m_name.isEmpty())
        m_name = h.hash();
}
Esempio n. 7
0
/**
 * Returns the properties for a torrent in JSON format.
 *
 * The return value is a JSON-formatted dictionary.
 * The dictionary keys are:
 *   - "save_path": Torrent save path
 *   - "creation_date": Torrent creation date
 *   - "piece_size": Torrent piece size
 *   - "comment": Torrent comment
 *   - "total_wasted": Total data wasted for torrent
 *   - "total_uploaded": Total data uploaded for torrent
 *   - "total_uploaded_session": Total data uploaded this session
 *   - "total_downloaded": Total data uploaded for torrent
 *   - "total_downloaded_session": Total data downloaded this session
 *   - "up_limit": Torrent upload limit
 *   - "dl_limit": Torrent download limit
 *   - "time_elapsed": Torrent elapsed time
 *   - "seeding_time": Torrent elapsed time while complete
 *   - "nb_connections": Torrent connection count
 *   - "nb_connections_limit": Torrent connection count limit
 *   - "share_ratio": Torrent share ratio
 */
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{
    CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
    try {
        QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);

        libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
        if (!status.has_metadata)
            return QByteArray();

        // Save path
        QString save_path = fsutils::toNativePath(TorrentPersistentData::instance()->getSavePath(hash));
        if (save_path.isEmpty())
            save_path = fsutils::toNativePath(h.save_path());
        data[KEY_PROP_SAVE_PATH] = save_path;
        data[KEY_PROP_CREATION_DATE] = h.creation_date_unix();
        data[KEY_PROP_PIECE_SIZE] = static_cast<qlonglong>(h.piece_length());
        data[KEY_PROP_COMMENT] = h.comment();
        data[KEY_PROP_WASTED] = static_cast<qlonglong>(status.total_failed_bytes + status.total_redundant_bytes);
        data[KEY_PROP_UPLOADED] = static_cast<qlonglong>(status.all_time_upload);
        data[KEY_PROP_UPLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_upload);
        data[KEY_PROP_DOWNLOADED] = static_cast<qlonglong>(status.all_time_download);
        data[KEY_PROP_DOWNLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_download);
        data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? -1 : h.upload_limit();
        data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? -1 : h.download_limit();
        data[KEY_PROP_TIME_ELAPSED] = status.active_time;
        data[KEY_PROP_SEEDING_TIME] = status.seeding_time;
        data[KEY_PROP_CONNECT_COUNT] = status.num_connections;
        data[KEY_PROP_CONNECT_COUNT_LIMIT] = status.connections_limit;
        const qreal ratio = QBtSession::instance()->getRealRatio(status);
        data[KEY_PROP_RATIO] = ratio > QBtSession::MAX_RATIO ? -1 : ratio;
    }
    catch (const std::exception& e) {
        qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
        return QByteArray();
    }

    return json::toJson(data);
}
Esempio n. 8
0
/**
 * Returns the properties for a torrent in JSON format.
 *
 * The return value is a JSON-formatted dictionary.
 * The dictionary keys are:
 *   - "save_path": Torrent save path
 *   - "creation_date": Torrent creation date
 *   - "piece_size": Torrent piece size
 *   - "comment": Torrent comment
 *   - "total_wasted": Total data wasted for torrent
 *   - "total_uploaded": Total data uploaded for torrent
 *   - "total_downloaded": Total data uploaded for torrent
 *   - "up_limit": Torrent upload limit
 *   - "dl_limit": Torrent download limit
 *   - "time_elapsed": Torrent elapsed time
 *   - "nb_connections": Torrent connection count
 *   - "share_ratio": Torrent share ratio
 */
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{
  CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
  try {
    QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);

    libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
    if (!status.has_metadata)
      return QByteArray();

    // Save path
    QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash));
    if (save_path.isEmpty())
      save_path = fsutils::toNativePath(h.save_path());
    data[KEY_PROP_SAVE_PATH] = save_path;
    data[KEY_PROP_CREATION_DATE] = h.creation_date();
    data[KEY_PROP_PIECE_SIZE] = misc::friendlyUnit(h.piece_length(), false, true);
    data[KEY_PROP_COMMENT] = h.comment();
    data[KEY_PROP_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes, false, true);
    data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload, false, true) + " (" + misc::friendlyUnit(status.total_payload_upload, false, true) + " " + tr("this session") + ")");
    data[KEY_PROP_DOWNLOADED] = QString(misc::friendlyUnit(status.all_time_download, false, true) + " (" + misc::friendlyUnit(status.total_payload_download, false, true) + " " + tr("this session") + ")");
    data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.upload_limit(), true, true);
    data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.download_limit(), true, true);
    QString elapsed_txt = misc::userFriendlyDuration(status.active_time);
    if (h.is_seed(status))
      elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(status.seeding_time))+")";
    data[KEY_PROP_TIME_ELAPSED] = elapsed_txt;
    data[KEY_PROP_CONNECT_COUNT] = QString(QString::number(status.num_connections) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(status.connections_limit)) + ")");
    const qreal ratio = QBtSession::instance()->getRealRatio(status);
    data[KEY_PROP_RATIO] = ratio > 100. ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1, false);
  } catch(const std::exception& e) {
    qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
    return QByteArray();
  }

  return json::toJson(data);
}
Esempio n. 9
0
static QVariantMap toMap(const QTorrentHandle& h)
{
  libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);

  QVariantMap ret;
  ret[KEY_TORRENT_HASH] =  h.hash();
  ret[KEY_TORRENT_NAME] =  h.name();
  ret[KEY_TORRENT_SIZE] =  misc::friendlyUnit(status.total_wanted, false, true); // FIXME: Should pass as Number, not formatted String (for sorting).
  ret[KEY_TORRENT_PROGRESS] =  (double)h.progress(status);
  ret[KEY_TORRENT_DLSPEED] = misc::friendlyUnit(status.download_payload_rate, true, true); // FIXME: Should be passed as a Number
  ret[KEY_TORRENT_UPSPEED] = misc::friendlyUnit(status.upload_payload_rate, true, true); // FIXME: Should be passed as a Number
  if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0)
    ret[KEY_TORRENT_PRIORITY] =  QString::number(h.queue_position(status));
  else
    ret[KEY_TORRENT_PRIORITY] =  "*";
  QString seeds = QString::number(status.num_seeds);
  if (status.num_complete > 0)
    seeds += " ("+QString::number(status.num_complete)+")";
  ret[KEY_TORRENT_SEEDS] =  seeds;
  QString leechs = QString::number(status.num_peers - status.num_seeds);
  if (status.num_incomplete > 0)
    leechs += " ("+QString::number(status.num_incomplete)+")";
  ret[KEY_TORRENT_LEECHS] =  leechs;
  const qreal ratio = QBtSession::instance()->getRealRatio(status);
  ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1, false);
  QString eta;
  QString state;
  if (h.is_paused(status)) {
    if (h.has_error(status))
      state = "error";
    else
      state = h.is_seed(status) ? "pausedUP" : "pausedDL";
  } else {
    if (QBtSession::instance()->isQueueingEnabled() && h.is_queued(status))
      state = h.is_seed(status) ? "queuedUP" : "queuedDL";
    else {
      switch (status.state) {
      case torrent_status::finished:
      case torrent_status::seeding:
        state = status.upload_payload_rate > 0 ? "uploading" : "stalledUP";
        break;
      case torrent_status::allocating:
      case torrent_status::checking_files:
      case torrent_status::queued_for_checking:
      case torrent_status::checking_resume_data:
        state = h.is_seed(status) ? "checkingUP" : "checkingDL";
        break;
      case torrent_status::downloading:
      case torrent_status::downloading_metadata:
        state = status.download_payload_rate > 0 ? "downloading" : "stalledDL";
        eta = misc::userFriendlyDuration(QBtSession::instance()->getETA(h.hash(), status));
        break;
      default:
        qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state());
      }
    }
  }
  ret[KEY_TORRENT_ETA] =  eta.isEmpty() ? QString::fromUtf8("∞") : eta;
  ret[KEY_TORRENT_STATE] =  state;

  return ret;
}