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