- Change leftover port uint's to quint16, save our memory! (yes just some bits)
[konversation:konversation.git] / src / dcc / transfer.h
1 /*
2   This class represents a DCC transfer.
3 */
4
5 /*
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10 */
11
12 /*
13   Copyright (C) 2002-2004 Dario Abatianni <eisfuchs@tigress.com>
14   Copyright (C) 2004-2007 Shintaro Matsuoka <shin@shoegazed.org>
15   Copyright (C) 2004,2005 John Tapsell <john@geola.co.uk>
16   Copyright (C) 2009 Bernd Buschinski <b.buschinski@web.de>
17 */
18
19 #ifndef TRANSFER_H
20 #define TRANSFER_H
21
22 #include <QDateTime>
23 #include <QObject>
24 #include <QTimer>
25
26 #include <kurl.h>
27 #include <kio/global.h>
28
29 namespace Konversation
30 {
31     namespace DCC
32     {
33         typedef qreal transferspeed_t;
34
35         class Transfer : public QObject
36         {
37             Q_OBJECT
38
39             public:
40                 enum Type
41                 {
42                     Receive = 1,
43                     Send    = 1 << 1
44                 };
45
46                 enum Status
47                 {
48                     Configuring = 0,                      // Not queud yet (this means that user can't see the item at this time)
49                     Queued,                               // Newly added DCC, waiting user's response
50                     Preparing,                            // Opening KIO to write received data
51                     WaitingRemote,                        // Waiting for remote host's response
52                     Connecting,                           // RECV: trying to connect to the server
53                     Transferring,
54                     Done,
55                     Failed,
56                     Aborted
57                 };
58
59                 enum UnavailableStatus
60                 {
61                     Calculating = -1,
62                     NotInTransfer = -2,
63                     InfiniteValue = -3
64                 };
65
66                 Transfer( Type dccType, QObject* parent );
67                 virtual ~Transfer();
68
69                 Type               getType()                  const;
70                 Status             getStatus()                const;
71                 const QString&     getStatusDetail()          const;
72                 QDateTime          getTimeOffer()             const;
73                 int                getConnectionId()          const;
74                 QString            getOwnIp()                 const;
75                 quint16            getOwnPort()               const;
76                 QString            getPartnerNick()           const;
77                 QString            getPartnerIp()             const;
78                 quint16            getPartnerPort()           const;
79                 QString            getFileName()              const;
80                 KIO::filesize_t    getFileSize()              const;
81                 KIO::fileoffset_t  getTransferringPosition()  const;
82                 KIO::fileoffset_t  getTransferStartPosition() const;
83                 KUrl               getFileURL()               const;
84                 bool               isResumed()                const;
85                 bool               isReverse()                const;
86                 QString            getReverseToken()          const;
87                 transferspeed_t    getAverageSpeed()          const;
88                 transferspeed_t    getCurrentSpeed()          const;
89                 int                getTimeLeft()              const;
90                 int                getProgress()              const;
91                 QDateTime          getTimeTransferStarted()   const;
92                 QDateTime          getTimeTransferFinished()  const;
93
94                 // common settings for DccTransferRecv / DccTransferSend
95
96                 // REQUIRED
97                 void setConnectionId( int connectionId );
98                 // REQUIRED
99                 void setPartnerNick( const QString& nick );
100
101                 void removedFromView();
102
103             signals:
104                 void transferStarted( Konversation::DCC::Transfer* item );
105                 //done is when the transfer is done, it will not get deleted after emiting this signal
106                 void done( Konversation::DCC::Transfer* item );
107                 void statusChanged( Konversation::DCC::Transfer* item, int newStatus, int oldStatus );
108                 //removed is when the transfer is removed from all visible views and ready to get deleted
109                 void removed( Konversation::DCC::Transfer* item );
110
111             public slots:
112                 virtual bool queue();
113                 virtual void start() {}
114                 virtual void abort() {}
115
116             protected:
117                 virtual void cleanUp();
118                 void failed(const QString& errorMessage = QString() );
119
120                 /**
121                  * setStatus behavior changed:
122                  * Now make sure to run functions that change transfer information before setStatus.
123                  * For example cleanUp();
124                  *
125                  * If you call setStatus(..) and change the "Started at:"-time afterwards,
126                  * the transferpanel won't notice it
127                  */
128                 void setStatus( Status status, const QString& statusDetail = QString() );
129                 void startTransferLogger();
130                 void finishTransferLogger();
131
132                 static QString transferFileName( const QString& fileName );
133                 static QString sanitizeFileName( const QString& fileName );
134                 static quint32 intel( quint32 value );
135
136             protected slots:
137                 void logTransfer();
138
139             protected:
140                 // transfer information
141                 Type m_type;
142                 Status m_status;
143                 QString m_statusDetail;
144                 bool m_resumed;
145                 bool m_reverse;
146                 QString m_reverseToken;
147                 KIO::fileoffset_t m_transferringPosition;
148                 KIO::fileoffset_t m_transferStartPosition;
149
150                 /*
151                 QValueList<QDateTime> m_transferTimeLog;  // write per packet to calc CPS
152                 QValueList<KIO::fileoffset_t> m_transferPositionLog;  // write per packet to calc CPS
153                 */
154
155                 // we'll communicate with the partner via this server
156                 int m_connectionId;
157                 QString m_partnerNick;
158                 QString m_partnerIp;                      // null when unknown
159                 quint16 m_partnerPort;
160                 QString m_ownIp;
161                 quint16 m_ownPort;
162
163                 unsigned long m_bufferSize;
164                 char* m_buffer;
165
166                 /**
167                  * The filename. Clean filename without any "../" or extra "
168                  */
169                 QString m_fileName;
170
171                 /** The file size of the complete file sending/recieving. */
172                 KIO::filesize_t m_fileSize;
173
174                 /**
175                  * If we are sending a file, this is the url of the file we are sending.
176                  * If we are recieving a file, this is the url of the file we are saving
177                  * to in the end (Temporararily it will be filename+".part" ).
178                  */
179                 KUrl m_fileURL;
180
181             private:
182                 void updateTransferMeters();
183
184             private:
185                 QDateTime m_timeOffer;
186                 QDateTime m_timeTransferStarted;
187                 //QDateTime m_timeLastActive;
188                 QDateTime m_timeTransferFinished;
189
190                 QTimer m_loggerTimer;
191                 QTime m_loggerBaseTime;  // for calculating CPS
192                 QList<int> m_transferLogTime;
193                 QList<KIO::fileoffset_t> m_transferLogPosition;
194
195                 transferspeed_t m_averageSpeed;
196                 transferspeed_t m_currentSpeed;
197                 int m_timeLeft;
198         };
199     }
200 }
201
202 #endif  // TRANSFER_H