QXmpp Version:0.3.0
QXmppTransferManager.h
00001 /*
00002  * Copyright (C) 2008-2011 The QXmpp developers
00003  *
00004  * Author:
00005  *  Jeremy Lainé
00006  *
00007  * Source:
00008  *  http://code.google.com/p/qxmpp
00009  *
00010  * This file is a part of QXmpp library.
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  */
00023 
00024 #ifndef QXMPPTRANSFERMANAGER_H
00025 #define QXMPPTRANSFERMANAGER_H
00026 
00027 #include <QDateTime>
00028 #include <QUrl>
00029 #include <QVariant>
00030 
00031 #include "QXmppClientExtension.h"
00032 #include "QXmppIq.h"
00033 #include "QXmppByteStreamIq.h"
00034 
00035 class QTcpSocket;
00036 class QXmppByteStreamIq;
00037 class QXmppIbbCloseIq;
00038 class QXmppIbbDataIq;
00039 class QXmppIbbOpenIq;
00040 class QXmppSocksClient;
00041 class QXmppSocksServer;
00042 class QXmppStreamInitiationIq;
00043 class QXmppTransferJobPrivate;
00044 
00045 class QXmppTransferFileInfo
00046 {
00047 public:
00048     QXmppTransferFileInfo();
00049 
00050     QDateTime date() const;
00051     void setDate(const QDateTime &date);
00052 
00053     QByteArray hash() const;
00054     void setHash(const QByteArray &hash);
00055 
00056     QString name() const;
00057     void setName(const QString &name);
00058 
00059     qint64 size() const;
00060     void setSize(qint64 size);
00061 
00062     bool operator==(const QXmppTransferFileInfo &other) const;
00063 
00064 private:
00065     QDateTime m_date;
00066     QByteArray m_hash;
00067     QString m_name;
00068     qint64 m_size;
00069 };
00070 
00075 
00076 class QXmppTransferJob : public QXmppLoggable
00077 {
00078     Q_OBJECT
00079     Q_ENUMS(Direction Error State)
00080     Q_FLAGS(Method Methods)
00081     Q_PROPERTY(Direction direction READ direction CONSTANT)
00082     Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
00083     Q_PROPERTY(QString jid READ jid CONSTANT)
00084     Q_PROPERTY(Method method READ method CONSTANT)
00085     Q_PROPERTY(State state READ state NOTIFY stateChanged)
00086 
00087     Q_PROPERTY(QString fileName READ fileName CONSTANT)
00088     Q_PROPERTY(qint64 fileSize READ fileSize CONSTANT)
00089 
00090 public:
00092     enum Direction
00093     {
00094         IncomingDirection, 
00095         OutgoingDirection, 
00096     };
00097 
00099     enum Error
00100     {
00101         NoError = 0,      
00102         AbortError,       
00103         FileAccessError,  
00104         FileCorruptError, 
00105         ProtocolError,    
00106     };
00107 
00109     enum Method
00110     {
00111         NoMethod = 0,     
00112         InBandMethod = 1, 
00113         SocksMethod = 2,  
00114         AnyMethod = 3,    
00115     };
00116     Q_DECLARE_FLAGS(Methods, Method)
00117 
00118     
00119     enum State
00120     {
00121         OfferState = 0,    
00122         StartState = 1,    
00123         TransferState = 2, 
00124         FinishedState = 3, 
00125     };
00126 
00127     ~QXmppTransferJob();
00128 
00129     QVariant data(int role) const;
00130     void setData(int role, const QVariant &value);
00131 
00132     QXmppTransferJob::Direction direction() const;
00133     QXmppTransferJob::Error error() const;
00134     QString jid() const;
00135     QXmppTransferJob::Method method() const;
00136     QString sid() const;
00137     qint64 speed() const;
00138     QXmppTransferJob::State state() const;
00139 
00140     // XEP-0096 : File transfer
00141     QXmppTransferFileInfo fileInfo() const;
00142     QUrl localFileUrl() const;
00143     void setLocalFileUrl(const QUrl &localFileUrl);
00144 
00146     QDateTime fileDate() const;
00147     QByteArray fileHash() const;
00148     QString fileName() const;
00149     qint64 fileSize() const;
00151 
00152 signals:
00155     void error(QXmppTransferJob::Error error);
00156 
00164     void finished();
00165 
00167     void localFileUrlChanged(const QUrl &localFileUrl);
00168 
00170     void progress(qint64 done, qint64 total);
00171 
00173     void stateChanged(QXmppTransferJob::State state);
00174 
00175 public slots:
00176     void abort();
00177     void accept(const QString &filePath);
00178     void accept(QIODevice *output);
00179 
00180 private slots:
00181     void disconnected();
00182     void receiveData();
00183     void sendData();
00184     void slotTerminated();
00185 
00186 private:
00187     QXmppTransferJob(const QString &jid, QXmppTransferJob::Direction direction, QObject *parent);
00188     void checkData();
00189     void setState(QXmppTransferJob::State state);
00190     void terminate(QXmppTransferJob::Error error);
00191     bool writeData(const QByteArray &data);
00192 
00193     QXmppTransferJobPrivate *const d;
00194     friend class QXmppTransferManager;
00195 };
00196 
00213  
00214 class QXmppTransferManager : public QXmppClientExtension
00215 {
00216     Q_OBJECT
00217     Q_PROPERTY(QString proxy READ proxy WRITE setProxy)
00218     Q_PROPERTY(bool proxyOnly READ proxyOnly WRITE setProxyOnly)
00219     Q_PROPERTY(QXmppTransferJob::Methods supportedMethods READ supportedMethods WRITE setSupportedMethods)
00220 
00221 public:
00222     QXmppTransferManager();
00223 
00224     QString proxy() const;
00225     void setProxy(const QString &proxyJid);
00226 
00227     bool proxyOnly() const;
00228     void setProxyOnly(bool proxyOnly);
00229 
00230     QXmppTransferJob::Methods supportedMethods() const;
00231     void setSupportedMethods(QXmppTransferJob::Methods methods);
00232 
00234     QStringList discoveryFeatures() const;
00235     bool handleStanza(const QDomElement &element);
00237 
00238 signals:
00243     void fileReceived(QXmppTransferJob *job);
00244 
00246     void jobStarted(QXmppTransferJob *job);
00247 
00251     void jobFinished(QXmppTransferJob *job);
00252 
00253 public slots:
00254     QXmppTransferJob *sendFile(const QString &jid, const QString &filePath, const QString &sid = QString());
00255     QXmppTransferJob *sendFile(const QString &jid, QIODevice *device, const QXmppTransferFileInfo &fileInfo, const QString &sid = QString());
00256 
00257 protected:
00259     void setClient(QXmppClient* client);
00261 
00262 private slots:
00263     void iqReceived(const QXmppIq&);
00264     void jobDestroyed(QObject *object);
00265     void jobError(QXmppTransferJob::Error error);
00266     void onJobFinished();
00267     void onJobStateChanged(QXmppTransferJob::State state);
00268     void socksServerConnected(QTcpSocket *socket, const QString &hostName, quint16 port);
00269 
00270 private:
00271     QXmppTransferJob *getJobByRequestId(QXmppTransferJob::Direction direction, const QString &jid, const QString &id);
00272     QXmppTransferJob *getJobBySid(QXmppTransferJob::Direction, const QString &jid, const QString &sid);
00273     void byteStreamIqReceived(const QXmppByteStreamIq&);
00274     void byteStreamResponseReceived(const QXmppIq&);
00275     void byteStreamResultReceived(const QXmppByteStreamIq&);
00276     void byteStreamSetReceived(const QXmppByteStreamIq&);
00277     void ibbCloseIqReceived(const QXmppIbbCloseIq&);
00278     void ibbDataIqReceived(const QXmppIbbDataIq&);
00279     void ibbOpenIqReceived(const QXmppIbbOpenIq&);
00280     void ibbResponseReceived(const QXmppIq&);
00281     void streamInitiationIqReceived(const QXmppStreamInitiationIq&);
00282     void streamInitiationResultReceived(const QXmppStreamInitiationIq&);
00283     void streamInitiationSetReceived(const QXmppStreamInitiationIq&);
00284     void socksServerSendOffer(QXmppTransferJob *job);
00285 
00286     int m_ibbBlockSize;
00287     QList<QXmppTransferJob*> m_jobs;
00288     QString m_proxy;
00289     bool m_proxyOnly;
00290     QXmppSocksServer *m_socksServer;
00291     QXmppTransferJob::Methods m_supportedMethods;
00292 };
00293 
00294 Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppTransferJob::Methods)
00295 
00296 #endif
 All Classes Functions Enumerations Enumerator Properties