1 /*
2  * filetransfer.h - File Transfer
3  * Copyright (C) 2004  Justin Karneges
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef XMPP_FILETRANSFER_H
22 #define XMPP_FILETRANSFER_H
23 
24 #include "im.h"
25 
26 namespace XMPP
27 {
28 	//class BSConnection;
29 	class BSConnection;
30 	class BytestreamManager;
31 	struct FTRequest;
32 
33 	/*class AbstractFileTransfer
34 	{
35 		public:
36 			// Receive
37 			virtual Jid peer() const = 0;
38 			virtual QString fileName() const = 0;
39 			virtual qlonglong fileSize() const = 0;
40 			virtual QString description() const { return ""; }
41 			virtual bool rangeSupported() const { return false; }
42 			virtual void accept(qlonglong offset=0, qlonglong length=0) = 0;
43 	};*/
44 
45 	class FTThumbnail
46 	{
47 	public:
FTThumbnail()48 		inline FTThumbnail() : width(0), height(0) {}
49 		// data - for outgoing it's actual image data. for incoming - cid
50 		inline FTThumbnail(const QByteArray &data,
51 						   const QString &mimeType = QString(),
52 						   quint32 width = 0, quint32 height = 0) :
data(data)53 			data(data), mimeType(mimeType),
54 			width(width), height(height) { }
isNull()55 		inline bool isNull() const { return data.isNull(); }
56 
57 		QByteArray data;
58 		QString mimeType;
59 		quint32 width;
60 		quint32 height;
61 	};
62 
63 	class FileTransfer : public QObject /*, public AbstractFileTransfer */
64 	{
65 		Q_OBJECT
66 	public:
67 		enum { ErrReject, ErrNeg, ErrConnect, ErrProxy, ErrStream, Err400 };
68 		enum { Idle, Requesting, Connecting, WaitingForAccept, Active };
69 		~FileTransfer();
70 
71 		FileTransfer *copy() const;
72 
73 		void setProxy(const Jid &proxy);
74 
75 		// send
76 		void sendFile(const Jid &to, const QString &fname, qlonglong size, const QString &desc, const FTThumbnail &thumb);
77 		qlonglong offset() const;
78 		qlonglong length() const;
79 		int dataSizeNeeded() const;
80 		void writeFileData(const QByteArray &a);
81 		const FTThumbnail &thumbnail() const;
82 
83 		// receive
84 		Jid peer() const;
85 		QString fileName() const;
86 		qlonglong fileSize() const;
87 		QString description() const;
88 		bool rangeSupported() const;
89 		void accept(qlonglong offset=0, qlonglong length=0);
90 
91 		// both
92 		void close(); // reject, or stop sending/receiving
93 		BSConnection *bsConnection() const; // active link
94 
95 	signals:
96 		void accepted(); // indicates BSConnection has started
97 		void connected();
98 		void readyRead(const QByteArray &a);
99 		void bytesWritten(qint64);
100 		void error(int);
101 
102 	private slots:
103 		void ft_finished();
104 		void stream_connected();
105 		void stream_connectionClosed();
106 		void stream_readyRead();
107 		void stream_bytesWritten(qint64);
108 		void stream_error(int);
109 		void doAccept();
110 		void reset();
111 
112 	private:
113 		class Private;
114 		Private *d;
115 
116 		friend class FileTransferManager;
117 		FileTransfer(FileTransferManager *, QObject *parent=0);
118 		FileTransfer(const FileTransfer& other);
119 		void man_waitForAccept(const FTRequest &req, const QString &streamType);
120 		void takeConnection(BSConnection *c);
121 	};
122 
123 	class FileTransferManager : public QObject
124 	{
125 		Q_OBJECT
126 	public:
127 		FileTransferManager(Client *);
128 		~FileTransferManager();
129 
130 		bool isActive(const FileTransfer *ft) const;
131 		void setDisabled(const QString &ns, bool state = true);
132 
133 		Client *client() const;
134 		FileTransfer *createTransfer();
135 		FileTransfer *takeIncoming();
136 
137 	signals:
138 		void incomingReady();
139 
140 	private slots:
141 		void pft_incoming(const FTRequest &req);
142 
143 	private:
144 		class Private;
145 		Private *d;
146 
147 		friend class Client;
148 		void stream_incomingReady(BSConnection *);
149 
150 		friend class FileTransfer;
151 		BytestreamManager* streamManager(const QString &ns) const;
152 		QStringList streamPriority() const;
153 		QString link(FileTransfer *);
154 		void con_accept(FileTransfer *);
155 		void con_reject(FileTransfer *);
156 		void unlink(FileTransfer *);
157 	};
158 
159 	class JT_FT : public Task
160 	{
161 		Q_OBJECT
162 	public:
163 		JT_FT(Task *parent);
164 		~JT_FT();
165 
166 		void request(const Jid &to, const QString &id, const QString &fname,
167 					 qlonglong size, const QString &desc,
168 					 const QStringList &streamTypes, const FTThumbnail &thumb);
169 		qlonglong rangeOffset() const;
170 		qlonglong rangeLength() const;
171 		QString streamType() const;
172 
173 		void onGo();
174 		bool take(const QDomElement &);
175 
176 	private:
177 		class Private;
178 		Private *d;
179 	};
180 
181 	struct FTRequest
182 	{
183 		Jid from;
184 		QString iq_id, id;
185 		QString fname;
186 		qlonglong size;
187 		QString desc;
188 		bool rangeSupported;
189 		QStringList streamTypes;
190 		FTThumbnail thumbnail;
191 	};
192 	class JT_PushFT : public Task
193 	{
194 		Q_OBJECT
195 	public:
196 		JT_PushFT(Task *parent);
197 		~JT_PushFT();
198 
199 		void respondSuccess(const Jid &to, const QString &id, qlonglong rangeOffset, qlonglong rangeLength, const QString &streamType);
200 		void respondError(const Jid &to, const QString &id,
201 						  Stanza::Error::ErrorCond cond, const QString &str);
202 
203 		bool take(const QDomElement &);
204 
205 	signals:
206 		void incoming(const FTRequest &req);
207 	};
208 }
209 
210 #endif
211