From 9f50015ec47c67cb30acbd126fd0278195754b08 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 19 Jan 2015 17:27:40 +0000 Subject: Tox: added missing header git-svn-id: http://svn.miranda-ng.org/main/trunk@11878 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_transfer.h | 163 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 protocols/Tox/src/tox_transfer.h (limited to 'protocols/Tox') diff --git a/protocols/Tox/src/tox_transfer.h b/protocols/Tox/src/tox_transfer.h new file mode 100644 index 0000000000..5f3e2cbc9d --- /dev/null +++ b/protocols/Tox/src/tox_transfer.h @@ -0,0 +1,163 @@ +#ifndef _TOX_TRANSFERS_H_ +#define _TOX_TRANSFERS_H_ + +enum FILE_TRANSFER_STATUS +{ + NONE, + STARTED, + PAUSED, + FAILED, + CANCELED, + FINISHED, + DESTROYED +}; + +struct FileTransferParam +{ + PROTOFILETRANSFERSTATUS pfts; + FILE_TRANSFER_STATUS status; + FILE *hFile; + int friendNumber; + int fileNumber; + + FileTransferParam(int friendNumber, int fileNumber, const TCHAR* fileName, size_t fileSize) + { + status = NONE; + hFile = NULL; + this->friendNumber = friendNumber; + this->fileNumber = fileNumber; + + pfts.cbSize = sizeof(PROTOFILETRANSFERSTATUS); + pfts.flags = PFTS_TCHAR; + pfts.totalFiles = 1; + pfts.ptszFiles = (TCHAR**)mir_alloc(sizeof(TCHAR*)*(pfts.totalFiles + 1)); + pfts.ptszFiles[0] = pfts.tszCurrentFile = mir_tstrdup(fileName); + pfts.ptszFiles[pfts.totalFiles] = NULL; + pfts.totalBytes = pfts.currentFileSize = fileSize; + pfts.totalProgress = pfts.currentFileProgress = 0; + pfts.currentFileNumber = 0; + pfts.tszWorkingDir = NULL; + } + + bool OpenFile(const TCHAR *mode) + { + hFile = _tfopen(pfts.tszCurrentFile, mode); + return hFile != NULL; + } + + void Start(Tox *tox) + { + status = STARTED; + tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_ACCEPT, NULL, 0); + } + + void Resume(Tox *tox) + { + status = STARTED; + tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_RESUME_BROKEN, (uint8_t*)&pfts.currentFileProgress, sizeof(uint64_t)); + } + + void Fail(Tox *tox) + { + status = FAILED; + tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0); + } + + void Cancel(Tox *tox) + { + status = FINISHED; + tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0); + } + + void Finish(Tox *tox) + { + status = FINISHED; + tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_FINISHED, NULL, 0); + } + + void RenameName(const TCHAR* fileName) + { + pfts.ptszFiles[0] = replaceStrT(pfts.tszCurrentFile, fileName); + } + + uint8_t GetDirection() const + { + return pfts.flags & PFTS_SENDING ? 0 : 1; + } + + ~FileTransferParam() + { + status = DESTROYED; + if (pfts.tszWorkingDir != NULL) + { + mir_free(pfts.tszWorkingDir); + } + mir_free(pfts.pszFiles[0]); + mir_free(pfts.pszFiles); + if (hFile) + { + fclose(hFile); + } + } +}; + +class CTransferList +{ +private: + std::map transfers; + +public: + int Count() const + { + return transfers.size(); + } + + void Add(FileTransferParam *transfer) + { + if (transfers.find(transfer->fileNumber) == transfers.end()) + { + transfers[transfer->fileNumber] = transfer; + } + } + + FileTransferParam * Get(uint8_t fileNumber) + { + if (transfers.find(fileNumber) == transfers.end()) + { + return transfers.at(fileNumber); + } + return NULL; + } + + FileTransferParam * At(int index) + { + if (Count() < index) + { + std::map::iterator it = transfers.begin(); + std::advance(it, index); + return it->second; + } + return NULL; + } + + void Remove(uint8_t fileNumber) + { + if (transfers.find(fileNumber) != transfers.end()) + { + FileTransferParam *transfer = transfers.at(fileNumber); + transfers.erase(fileNumber); + delete transfer; + } + } + + void Remove(FileTransferParam *transfer) + { + if (transfers.find(transfer->fileNumber) != transfers.end()) + { + transfers.erase(transfer->fileNumber); + delete transfer; + } + } +}; + +#endif //_TOX_TRANSFERS_H_ \ No newline at end of file -- cgit v1.2.3