Merge pull request #188 from digitalcircuit/ircv3-fix-usermode
[quassel.git] / src / common / transfer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef TRANSFER_H
22 #define TRANSFER_H
23
24 #include <QHostAddress>
25 #include <QUuid>
26
27 #include "peer.h"
28 #include "syncableobject.h"
29
30 class Transfer : public SyncableObject
31 {
32     Q_OBJECT
33     SYNCABLE_OBJECT
34
35     Q_PROPERTY(QUuid uuid READ uuid);
36     Q_PROPERTY(Transfer::Status status READ status WRITE setStatus NOTIFY statusChanged);
37     Q_PROPERTY(Transfer::Direction direction READ direction WRITE setDirection NOTIFY directionChanged);
38     Q_PROPERTY(QHostAddress address READ address WRITE setAddress NOTIFY addressChanged);
39     Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged);
40     Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged);
41     Q_PROPERTY(quint64 fileSize READ fileSize WRITE setFileSize NOTIFY fileSizeChanged);
42     Q_PROPERTY(QString nick READ nick WRITE setNick NOTIFY nickChanged);
43
44 public:
45     enum class Status {
46         New,
47         Pending,
48         Connecting,
49         Transferring,
50         Paused,
51         Completed,
52         Failed,
53         Rejected
54     };
55     Q_ENUMS(State)
56
57     enum class Direction {
58         Send,
59         Receive
60     };
61     Q_ENUMS(Direction)
62
63     Transfer(const QUuid &uuid, QObject *parent = 0); // for creating a syncable object client-side
64     Transfer(Direction direction, const QString &nick, const QString &fileName, const QHostAddress &address, quint16 port, quint64 size = 0, QObject *parent = 0);
65     inline const QMetaObject *syncMetaObject() const override { return &staticMetaObject; }
66
67     QUuid uuid() const;
68     Status status() const;
69     QString prettyStatus() const;
70     Direction direction() const;
71     QString fileName() const;
72     QHostAddress address() const;
73     quint16 port() const;
74     quint64 fileSize() const;
75     QString nick() const;
76
77     virtual quint64 transferred() const = 0;
78
79 public slots:
80     // called on the client side
81     virtual void accept(const QString &savePath) const { Q_UNUSED(savePath); }
82     virtual void reject() const {}
83
84     // called on the core side through sync calls
85     virtual void requestAccepted(PeerPtr peer) { Q_UNUSED(peer); }
86     virtual void requestRejected(PeerPtr peer) { Q_UNUSED(peer); }
87
88 signals:
89     void statusChanged(Transfer::Status state);
90     void directionChanged(Transfer::Direction direction);
91     void addressChanged(const QHostAddress &address);
92     void portChanged(quint16 port);
93     void fileNameChanged(const QString &fileName);
94     void fileSizeChanged(quint64 fileSize);
95     void transferredChanged(quint64 transferred);
96     void nickChanged(const QString &nick);
97
98     void error(const QString &errorString);
99
100     void accepted(PeerPtr peer = 0) const;
101     void rejected(PeerPtr peer = 0) const;
102
103 protected slots:
104     void setStatus(Transfer::Status status);
105     void setError(const QString &errorString);
106
107     // called on the client side through sync calls
108     virtual void dataReceived(PeerPtr, const QByteArray &data) { Q_UNUSED(data); }
109
110     virtual void cleanUp() = 0;
111
112 private:
113     void init();
114
115     void setDirection(Direction direction);
116     void setAddress(const QHostAddress &address);
117     void setPort(quint16 port);
118     void setFileName(const QString &fileName);
119     void setFileSize(quint64 fileSize);
120     void setNick(const QString &nick);
121
122
123     Status _status;
124     Direction _direction;
125     QString _fileName;
126     QHostAddress _address;
127     quint16 _port;
128     quint64 _fileSize;
129     QString _nick;
130     QUuid _uuid;
131 };
132
133 Q_DECLARE_METATYPE(Transfer::Status)
134 Q_DECLARE_METATYPE(Transfer::Direction)
135
136 QDataStream &operator<<(QDataStream &out, Transfer::Status state);
137 QDataStream &operator>>(QDataStream &in, Transfer::Status &state);
138 QDataStream &operator<<(QDataStream &out, Transfer::Direction direction);
139 QDataStream &operator>>(QDataStream &in, Transfer::Direction &direction);
140
141 #endif