Пример #1
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;
}