diff --git a/src/aria2api.cc b/src/aria2api.cc index dc45a147..5ddb5b05 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -52,6 +52,9 @@ #include "LogFactory.h" #include "PieceStorage.h" #include "DownloadContext.h" +#include "FileEntry.h" +#include "BitfieldMan.h" +#include "DownloadContext.h" namespace aria2 { @@ -217,6 +220,93 @@ std::vector getActiveDownload(Session* session) return res; } +namespace { +template +void createUriEntry +(OutputIterator out, + InputIterator first, InputIterator last, + UriStatus status) +{ + for(; first != last; ++first) { + UriData uriData; + uriData.uri = *first; + uriData.status = status; + out++ = uriData; + } +} +} // namespace + +namespace { +template +void createUriEntry +(OutputIterator out, const SharedHandle& file) +{ + createUriEntry(out, + file->getSpentUris().begin(), + file->getSpentUris().end(), + URI_USED); + createUriEntry(out, + file->getRemainingUris().begin(), + file->getRemainingUris().end(), + URI_WAITING); +} +} // namespace + +namespace { +template +void createFileEntry +(OutputIterator out, + InputIterator first, InputIterator last, + const BitfieldMan* bf) +{ + size_t index = 1; + for(; first != last; ++first) { + FileData file; + file.index = index++; + file.path = (*first)->getPath(); + file.length = (*first)->getLength(); + file.completedLength = bf->getOffsetCompletedLength + ((*first)->getOffset(), (*first)->getLength()); + file.selected = (*first)->isRequested(); + createUriEntry(std::back_inserter(file.uris), *first); + out++ = file; + } +} +} // namespace + +namespace { +template +void createFileEntry +(OutputIterator out, + InputIterator first, InputIterator last, + int64_t totalLength, + int32_t pieceLength, + const std::string& bitfield) +{ + BitfieldMan bf(pieceLength, totalLength); + bf.setBitfield(reinterpret_cast(bitfield.data()), + bitfield.size()); + createFileEntry(out, first, last, &bf); +} +} // namespace + +namespace { +template +void createFileEntry +(OutputIterator out, + InputIterator first, InputIterator last, + int64_t totalLength, + int32_t pieceLength, + const SharedHandle& ps) +{ + BitfieldMan bf(pieceLength, totalLength); + if(ps) { + bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength()); + } + createFileEntry(out, first, last, &bf); +} +} // namespace + namespace { struct RequestGroupDH : public DownloadHandle { RequestGroupDH(const SharedHandle& group) @@ -290,6 +380,17 @@ struct RequestGroupDH : public DownloadHandle { { return group->getOption()->get(PREF_DIR); } + virtual std::vector getFiles() + { + std::vector res; + const SharedHandle& dctx = group->getDownloadContext(); + createFileEntry(std::back_inserter(res), + dctx->getFileEntries().begin(), + dctx->getFileEntries().end(), + dctx->getTotalLength(), dctx->getPieceLength(), + group->getPieceStorage()); + return res; + } SharedHandle group; TransferStat ts; }; @@ -360,6 +461,14 @@ struct DownloadResultDH : public DownloadHandle { { return dr->dir; } + virtual std::vector getFiles() + { + std::vector res; + createFileEntry(std::back_inserter(res), + dr->fileEntries.begin(), dr->fileEntries.end(), + dr->totalLength, dr->pieceLength, dr->bitfield); + return res; + } SharedHandle dr; }; } // namespace diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index eb43a072..16383047 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -134,6 +134,25 @@ int addUri(Session* session, // Returns the array of active download GID. std::vector getActiveDownload(Session* session); +enum UriStatus { + URI_USED, + URI_WAITING +}; + +struct UriData { + std::string uri; + UriStatus status; +}; + +struct FileData { + int index; + std::string path; + int64_t length; + int64_t completedLength; + bool selected; + std::vector uris; +}; + enum DOWNLOAD_STATUS { DOWNLOAD_ACTIVE, DOWNLOAD_WAITING, @@ -158,6 +177,7 @@ struct DownloadHandle { virtual const std::vector& getFollowedBy() = 0; virtual A2Gid getBelongsTo() = 0; virtual const std::string& getDir() = 0; + virtual std::vector getFiles() = 0; }; // Returns handle for the download denoted by the |gid|. The caller