1 /* 2 Copyright © 2015-2019 by The qTox Project Contributors 3 4 This file is part of qTox, a Qt-based graphical interface for Tox. 5 6 qTox is libre software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 qTox is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with qTox. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 21 #ifndef COREFILE_H 22 #define COREFILE_H 23 24 #include <tox/tox.h> 25 26 #include "toxfile.h" 27 #include "src/core/core.h" 28 #include "src/core/toxpk.h" 29 #include "src/model/status.h" 30 31 #include "src/util/compatiblerecursivemutex.h" 32 33 #include <QHash> 34 #include <QMutex> 35 #include <QObject> 36 #include <QString> 37 38 #include <cstddef> 39 #include <cstdint> 40 #include <memory> 41 42 struct Tox; 43 class CoreFile; 44 45 using CoreFilePtr = std::unique_ptr<CoreFile>; 46 47 class CoreFile : public QObject 48 { 49 Q_OBJECT 50 51 public: 52 void handleAvatarOffer(uint32_t friendId, uint32_t fileId, bool accept); 53 static CoreFilePtr makeCoreFile(Core* core, Tox* tox, CompatibleRecursiveMutex& coreLoopLock); 54 55 void sendFile(uint32_t friendId, QString filename, QString filePath, 56 long long filesize); 57 void sendAvatarFile(uint32_t friendId, const QByteArray& data); 58 void pauseResumeFile(uint32_t friendId, uint32_t fileId); 59 void cancelFileSend(uint32_t friendId, uint32_t fileId); 60 61 void cancelFileRecv(uint32_t friendId, uint32_t fileId); 62 void rejectFileRecvRequest(uint32_t friendId, uint32_t fileId); 63 void acceptFileRecvRequest(uint32_t friendId, uint32_t fileId, QString path); 64 65 unsigned corefileIterationInterval(); 66 67 signals: 68 void fileSendStarted(ToxFile file); 69 void fileReceiveRequested(ToxFile file); 70 void fileTransferAccepted(ToxFile file); 71 void fileTransferCancelled(ToxFile file); 72 void fileTransferFinished(ToxFile file); 73 void fileUploadFinished(const QString& path); 74 void fileDownloadFinished(const QString& path); 75 void fileTransferPaused(ToxFile file); 76 void fileTransferInfo(ToxFile file); 77 void fileTransferRemotePausedUnpaused(ToxFile file, bool paused); 78 void fileTransferBrokenUnbroken(ToxFile file, bool broken); 79 void fileNameChanged(const ToxPk& friendPk); 80 void fileSendFailed(uint32_t friendId, const QString& fname); 81 82 private: 83 CoreFile(Tox* core, CompatibleRecursiveMutex& coreLoopLock); 84 85 ToxFile* findFile(uint32_t friendId, uint32_t fileId); 86 void addFile(uint32_t friendId, uint32_t fileId, const ToxFile& file); 87 void removeFile(uint32_t friendId, uint32_t fileId); getFriendKey(uint32_t friendId,uint32_t fileId)88 static constexpr uint64_t getFriendKey(uint32_t friendId, uint32_t fileId) 89 { 90 return (static_cast<std::uint64_t>(friendId) << 32) + fileId; 91 } 92 93 static void connectCallbacks(Tox& tox); 94 static void onFileReceiveCallback(Tox* tox, uint32_t friendId, uint32_t fileId, uint32_t kind, 95 uint64_t filesize, const uint8_t* fname, size_t fnameLen, 96 void* vCore); 97 static void onFileControlCallback(Tox* tox, uint32_t friendId, uint32_t fileId, 98 Tox_File_Control control, void* vCore); 99 static void onFileDataCallback(Tox* tox, uint32_t friendId, uint32_t fileId, uint64_t pos, 100 size_t length, void* vCore); 101 static void onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fileId, uint64_t position, 102 const uint8_t* data, size_t length, void* vCore); 103 104 static QString getCleanFileName(QString filename); 105 106 private slots: 107 void onConnectionStatusChanged(uint32_t friendId, Status::Status state); 108 109 private: 110 QHash<uint64_t, ToxFile> fileMap; 111 Tox* tox; 112 CompatibleRecursiveMutex* coreLoopLock = nullptr; 113 }; 114 115 #endif // COREFILE_H 116