Exemple #1
0
/**
 * Returns the files in a torrent in JSON format.
 *
 * The return value is a JSON-formatted list of dictionaries.
 * The dictionary keys are:
 *   - "name": File name
 *   - "size": File size
 *   - "progress": File progress
 *   - "priority": File priority
 *   - "is_seed": Flag indicating if torrent is seeding/complete
 */
QByteArray btjson::getFilesForTorrent(const QString& hash)
{
    CACHED_VARIABLE_FOR_HASH(QVariantList, file_list, CACHE_DURATION_MS, hash);
    try {
        QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
        if (!h.has_metadata())
            return json::toJson(file_list);

        const std::vector<int> priorities = h.file_priorities();
        std::vector<size_type> fp;
        h.file_progress(fp);
        for (int i = 0; i < h.num_files(); ++i) {
            QVariantMap file_dict;
            QString fileName = h.filepath_at(i);
            if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
                fileName.chop(4);
            file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName);
            const size_type size = h.filesize_at(i);
            file_dict[KEY_FILE_SIZE] = static_cast<qlonglong>(size);
            file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.;
            file_dict[KEY_FILE_PRIORITY] = priorities[i];
            if (i == 0)
                file_dict[KEY_FILE_IS_SEED] = h.is_seed();

            file_list.append(file_dict);
        }
    }
    catch (const std::exception& e) {
        qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
        return QByteArray();
    }

    return json::toJson(file_list);
}
Exemple #2
0
/**
 * Returns the files in a torrent in JSON format.
 *
 * The return value is a JSON-formatted list of dictionaries.
 * The dictionary keys are:
 *   - "name": File name
 *   - "size": File size
 *   - "progress": File progress
 *   - "priority": File priority
 *   - "is_seed": Flag indicating if torrent is seeding/complete
 */
QString btjson::getFilesForTorrent(const QString& hash)
{
  CACHED_VARIABLE_FOR_HASH(JsonList, file_list, CACHE_DURATION_MS, hash);
  try {
    QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
    if (!h.has_metadata())
      return QString();

    const std::vector<int> priorities = h.file_priorities();
    std::vector<size_type> fp;
    h.file_progress(fp);
    for (int i = 0; i < h.num_files(); ++i) {
      JsonDict file_dict;
      QString fileName = h.filename_at(i);
      if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
        fileName.chop(4);
      file_dict.add(KEY_FILE_NAME, fileName);
      const size_type size = h.filesize_at(i);
      file_dict.add(KEY_FILE_SIZE, misc::friendlyUnit(size));
      file_dict.add(KEY_FILE_PROGRESS, (size > 0) ? (fp[i] / (double) size) : 1.);
      file_dict.add(KEY_FILE_PRIORITY, priorities[i]);
      if (i == 0)
        file_dict.add(KEY_FILE_IS_SEED, h.is_seed());

      file_list.append(file_dict);
    }
  } catch (const std::exception& e) {
    qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
    return QString();
  }

  return file_list.toString();
}