File indexing completed on 2024-06-23 04:03:38

0001 /*
0002  * filetransfer.h - File Transfer
0003  * Copyright (C) 2004  Justin Karneges
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * either version 2
0009    of the License, or (at your option) any later version.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0019  *
0020  */
0021 
0022 #ifndef XMPP_FILETRANSFER_H
0023 #define XMPP_FILETRANSFER_H
0024 
0025 #include "im.h"
0026 
0027 namespace XMPP
0028 {
0029     class S5BConnection;
0030     struct FTRequest;
0031 
0032     /*class AbstractFileTransfer 
0033     {
0034         public:
0035             // Receive
0036             virtual Jid peer() const = 0;
0037             virtual QString fileName() const = 0;
0038             virtual qlonglong fileSize() const = 0;
0039             virtual QString description() const { return ""; }
0040             virtual bool rangeSupported() const { return false; }
0041             virtual void accept(qlonglong offset=0, qlonglong length=0) = 0;
0042     };*/
0043 
0044     class FileTransfer : public QObject /*, public AbstractFileTransfer */
0045     {
0046         Q_OBJECT
0047     public:
0048         enum { ErrReject, ErrNeg, ErrConnect, ErrProxy, ErrStream, Err400 };
0049         enum { Idle, Requesting, Connecting, WaitingForAccept, Active };
0050         ~FileTransfer() override;
0051 
0052         FileTransfer *copy() const;
0053 
0054         void setProxy(const Jid &proxy);
0055 
0056         // send
0057         void sendFile(const Jid &to, const QString &fname, qlonglong size, const QString &desc);
0058         qlonglong offset() const;
0059         qlonglong length() const;
0060         int dataSizeNeeded() const;
0061         void writeFileData(const QByteArray &a);
0062 
0063         // receive
0064         Jid peer() const;
0065         QString fileName() const;
0066         qlonglong fileSize() const;
0067         QString description() const;
0068         bool rangeSupported() const;
0069         void accept(qlonglong offset=0, qlonglong length=0);
0070 
0071         // both
0072         void close(); // reject, or stop sending/receiving
0073         S5BConnection *s5bConnection() const; // active link
0074 
0075     signals:
0076         void accepted(); // indicates S5BConnection has started
0077         void connected();
0078         void readyRead(const QByteArray &a);
0079         void bytesWritten(int);
0080         void error(int);
0081 
0082     private slots:
0083         void ft_finished();
0084         void s5b_connected();
0085         void s5b_connectionClosed();
0086         void s5b_readyRead();
0087         void s5b_bytesWritten(int);
0088         void s5b_error(int);
0089         void doAccept();
0090 
0091     private:
0092         class Private;
0093         Private *d;
0094 
0095         void reset();
0096 
0097         friend class FileTransferManager;
0098         FileTransfer(FileTransferManager *, QObject *parent=0);
0099         FileTransfer(const FileTransfer& other);
0100         void man_waitForAccept(const FTRequest &req);
0101         void takeConnection(S5BConnection *c);
0102     };
0103 
0104     class FileTransferManager : public QObject
0105     {
0106         Q_OBJECT
0107     public:
0108         FileTransferManager(Client *);
0109         ~FileTransferManager() override;
0110 
0111         bool isActive(const FileTransfer *ft) const;
0112 
0113         Client *client() const;
0114         FileTransfer *createTransfer();
0115         FileTransfer *takeIncoming();
0116 
0117     signals:
0118         void incomingReady();
0119 
0120     private slots:
0121         void pft_incoming(const FTRequest &req);
0122 
0123     private:
0124         class Private;
0125         Private *d;
0126 
0127         friend class Client;
0128         void s5b_incomingReady(S5BConnection *);
0129 
0130         friend class FileTransfer;
0131         QString link(FileTransfer *);
0132         void con_accept(FileTransfer *);
0133         void con_reject(FileTransfer *);
0134         void unlink(FileTransfer *);
0135     };
0136 
0137     class JT_FT : public Task
0138     {
0139         Q_OBJECT
0140     public:
0141         JT_FT(Task *parent);
0142         ~JT_FT() override;
0143 
0144         void request(const Jid &to, const QString &id, const QString &fname, qlonglong size, const QString &desc, const QStringList &streamTypes);
0145         qlonglong rangeOffset() const;
0146         qlonglong rangeLength() const;
0147         QString streamType() const;
0148 
0149         void onGo() override;
0150         bool take(const QDomElement &) override;
0151 
0152     private:
0153         class Private;
0154         Private *d;
0155     };
0156 
0157     struct FTRequest
0158     {
0159         Jid from;
0160         QString iq_id, id;
0161         QString fname;
0162         qlonglong size;
0163         QString desc;
0164         bool rangeSupported;
0165         QStringList streamTypes;
0166     };
0167     class JT_PushFT : public Task
0168     {
0169         Q_OBJECT
0170     public:
0171         JT_PushFT(Task *parent);
0172         ~JT_PushFT() override;
0173 
0174         void respondSuccess(const Jid &to, const QString &id, qlonglong rangeOffset, qlonglong rangeLength, const QString &streamType);
0175         void respondError(const Jid &to, const QString &id, int code, const QString &str);
0176 
0177         bool take(const QDomElement &) override;
0178 
0179     signals:
0180         void incoming(const FTRequest &req);
0181     };
0182 }
0183 
0184 #endif