common: Simplify SyncableObject macros and usage
[quassel.git] / src / common / transfer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #pragma once
22
23 #include <QHostAddress>
24 #include <QUuid>
25
26 #include "peer.h"
27 #include "syncableobject.h"
28
29 class Transfer : public SyncableObject
30 {
31     Q_OBJECT
32     SYNCABLE_OBJECT
33
34     Q_PROPERTY(QUuid uuid READ uuid)
35     Q_PROPERTY(Transfer::Status status READ status WRITE setStatus NOTIFY statusChanged)
36     Q_PROPERTY(Transfer::Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
37     Q_PROPERTY(QHostAddress address READ address WRITE setAddress NOTIFY addressChanged)
38     Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged)
39     Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
40     Q_PROPERTY(quint64 fileSize READ fileSize WRITE setFileSize NOTIFY fileSizeChanged)
41     Q_PROPERTY(QString nick READ nick WRITE setNick NOTIFY nickChanged)
42
43 public:
44     enum class Status {
45         New,
46         Pending,
47         Connecting,
48         Transferring,
49         Paused,
50         Completed,
51         Failed,
52         Rejected
53     };
54     Q_ENUMS(State)
55
56     enum class Direction {
57         Send,
58         Receive
59     };
60     Q_ENUMS(Direction)
61
62     Transfer(const QUuid &uuid, QObject *parent = 0); // for creating a syncable object client-side
63     Transfer(Direction direction, const QString &nick, const QString &fileName, const QHostAddress &address, quint16 port, quint64 size = 0, QObject *parent = 0);
64
65     QUuid uuid() const;
66     Status status() const;
67     QString prettyStatus() const;
68     Direction direction() const;
69     QString fileName() const;
70     QHostAddress address() const;
71     quint16 port() const;
72     quint64 fileSize() const;
73     QString nick() const;
74
75     virtual quint64 transferred() const = 0;
76
77 public slots:
78     // called on the client side
79     virtual void accept(const QString &savePath) const { Q_UNUSED(savePath); }
80     virtual void reject() const {}
81
82     // called on the core side through sync calls
83     virtual void requestAccepted(PeerPtr peer) { Q_UNUSED(peer); }
84     virtual void requestRejected(PeerPtr peer) { Q_UNUSED(peer); }
85
86 signals:
87     void statusChanged(Transfer::Status state);
88     void directionChanged(Transfer::Direction direction);
89     void addressChanged(const QHostAddress &address);
90     void portChanged(quint16 port);
91     void fileNameChanged(const QString &fileName);
92     void fileSizeChanged(quint64 fileSize);
93     void transferredChanged(quint64 transferred);
94     void nickChanged(const QString &nick);
95
96     void error(const QString &errorString);
97
98     void accepted(PeerPtr peer = 0) const;
99     void rejected(PeerPtr peer = 0) const;
100
101 protected slots:
102     void setStatus(Transfer::Status status);
103     void setError(const QString &errorString);
104
105     // called on the client side through sync calls
106     virtual void dataReceived(PeerPtr, const QByteArray &data) { Q_UNUSED(data); }
107
108     virtual void cleanUp() = 0;
109
110 private:
111     void init();
112
113     void setDirection(Direction direction);
114     void setAddress(const QHostAddress &address);
115     void setPort(quint16 port);
116     void setFileName(const QString &fileName);
117     void setFileSize(quint64 fileSize);
118     void setNick(const QString &nick);
119
120
121     Status _status;
122     Direction _direction;
123     QString _fileName;
124     QHostAddress _address;
125     quint16 _port;
126     quint64 _fileSize;
127     QString _nick;
128     QUuid _uuid;
129 };
130
131 Q_DECLARE_METATYPE(Transfer::Status)
132 Q_DECLARE_METATYPE(Transfer::Direction)
133
134 QDataStream &operator<<(QDataStream &out, Transfer::Status state);
135 QDataStream &operator>>(QDataStream &in, Transfer::Status &state);
136 QDataStream &operator<<(QDataStream &out, Transfer::Direction direction);
137 QDataStream &operator>>(QDataStream &in, Transfer::Direction &direction);