file-transfer-chat-message-modifier.h 3.46 KB
Newer Older
1 2
/*
 * file-transfer-chat-message-modifier.h
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

20 21
#ifndef _L_FILE_TRANSFER_CHAT_MESSAGE_MODIFIER_H_
#define _L_FILE_TRANSFER_CHAT_MESSAGE_MODIFIER_H_
22

23 24
#include <belle-sip/belle-sip.h>

25
#include "chat-message-modifier.h"
26
#include "utils/background-task.h"
27 28 29 30 31

// =============================================================================

LINPHONE_BEGIN_NAMESPACE

32
class ChatRoom;
33
class Core;
34 35
class FileContent;
class FileTransferContent;
36 37

class FileTransferChatMessageModifier : public ChatMessageModifier {
38
public:
39
	FileTransferChatMessageModifier (belle_http_provider_t *prov);
Ghislain MARY's avatar
Ghislain MARY committed
40
	~FileTransferChatMessageModifier ();
41 42 43

	Result encode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
	Result decode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
44 45 46

	belle_http_request_t *getHttpRequest() const;
	void setHttpRequest(belle_http_request_t *request);
47

48 49 50 51 52 53 54
	int onSendBody (belle_sip_user_body_handler_t *bh, belle_sip_message_t *m, size_t offset, uint8_t *buffer, size_t *size);
	void onSendEnd (belle_sip_user_body_handler_t *bh);
	void fileUploadBackgroundTaskEnded();
	void fileTransferOnProgress (belle_sip_body_handler_t *bh, belle_sip_message_t *m, size_t offset, size_t total);
	void processResponseFromPostFile (const belle_http_response_event_t *event);
	void processIoErrorUpload (const belle_sip_io_error_event_t *event);
	void processAuthRequestedUpload (const belle_sip_auth_event *event);
55

56 57 58 59 60 61 62 63 64 65
	void onRecvBody(belle_sip_user_body_handler_t *bh, belle_sip_message_t *m, size_t offset, uint8_t *buffer, size_t size);
	void onRecvEnd(belle_sip_user_body_handler_t *bh);
	void processResponseHeadersFromGetFile(const belle_http_response_event_t *event);
	void processAuthRequestedDownload(const belle_sip_auth_event *event);
	void processIoErrorDownload(const belle_sip_io_error_event_t *event);
	void processResponseFromGetFile(const belle_http_response_event_t *event);

	int downloadFile(const std::shared_ptr<ChatMessage> &message, FileTransferContent *fileTransferContent);
	void cancelFileTransfer();
	bool isFileTransferInProgressAndValid();
66
	std::string createFakeFileTransferFromUrl(const std::string &url);
67

68
private:
69 70 71 72 73 74
	int uploadFile();
	int startHttpTransfer(const std::string &url, const std::string &action, belle_http_request_listener_callbacks_t *cbs);
	void fileUploadBeginBackgroundTask();
	void fileUploadEndBackgroundTask();

	void releaseHttpRequest();
75 76 77 78 79 80

	std::weak_ptr<ChatMessage> chatMessage;
	FileContent* currentFileContentToTransfer;

	belle_http_request_t *httpRequest = nullptr;
	belle_http_request_listener_t *httpListener = nullptr;
81
	belle_http_provider_t *provider  = nullptr;
82 83

	BackgroundTask bgTask;
84 85 86 87
};

LINPHONE_END_NAMESPACE

88
#endif // ifndef _L_FILE_TRANSFER_CHAT_MESSAGE_MODIFIER_H_