Example #1
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;
}
Example #2
0
static JsonDict toJson(const QTorrentHandle& h)
{
  JsonDict ret;
  ret.add(KEY_TORRENT_HASH, h.hash());
  ret.add(KEY_TORRENT_NAME, h.name());
  ret.add(KEY_TORRENT_SIZE, misc::friendlyUnit(h.actual_size())); // FIXME: Should pass as Number, not formatted String (for sorting).
  ret.add(KEY_TORRENT_PROGRESS, (double)h.progress());
  ret.add(KEY_TORRENT_DLSPEED, misc::friendlyUnit(h.download_payload_rate(), true)); // FIXME: Should be passed as a Number
  ret.add(KEY_TORRENT_UPSPEED, misc::friendlyUnit(h.upload_payload_rate(), true)); // FIXME: Should be passed as a Number
  if (QBtSession::instance()->isQueueingEnabled() && h.queue_position() >= 0)
    ret.add(KEY_TORRENT_PRIORITY, QString::number(h.queue_position()));
  else
    ret.add(KEY_TORRENT_PRIORITY, "*");
  QString seeds = QString::number(h.num_seeds());
  if (h.num_complete() > 0)
    seeds += " ("+QString::number(h.num_complete())+")";
  ret.add(KEY_TORRENT_SEEDS, seeds);
  QString leechs = QString::number(h.num_peers() - h.num_seeds());
  if (h.num_incomplete() > 0)
    leechs += " ("+QString::number(h.num_incomplete())+")";
  ret.add(KEY_TORRENT_LEECHS, leechs);
  const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
  /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
  ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
  ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1));
  QString eta;
  QString state;
  if (h.is_paused()) {
    if (h.has_error())
      state = "error";
    else
      state = h.is_seed() ? "pausedUP" : "pausedDL";
  } else {
    if (QBtSession::instance()->isQueueingEnabled() && h.is_queued())
      state = h.is_seed() ? "queuedUP" : "queuedDL";
    else {
      switch (h.state()) {
      case torrent_status::finished:
      case torrent_status::seeding:
        state = h.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() ? "checkingUP" : "checkingDL";
        break;
      case torrent_status::downloading:
      case torrent_status::downloading_metadata:
        state = h.download_payload_rate() > 0 ? "downloading" : "stalledDL";
        eta = misc::userFriendlyDuration(QBtSession::instance()->getETA(h.hash()));
        break;
      default:
        qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state());
      }
    }
  }
  ret.add(KEY_TORRENT_ETA, eta.isEmpty() ? QString::fromUtf8("∞") : eta);
  ret.add(KEY_TORRENT_STATE, state);

  return ret;
}