summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/kftptransfer.h
blob: a977b76711ec146a487872b5448af7dccf2c70fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2004 by the KFTPGrabber developers
 * Copyright (C) 2003-2004 Jernej Kos <[email protected]>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */

#ifndef KFTPQUEUEKFTPTRANSFER_H
#define KFTPQUEUEKFTPTRANSFER_H

#include "queueobject.h"

#include <tqobject.h>
#include <tqtimer.h>
#include <tqguardedptr.h>

#include <kurl.h>

namespace KFTPSession {
  class Session;
  class Connection;
}

namespace KFTPQueue {

enum TransferType {
    Download = 0,
    Upload = 1,
    FXP = 2
};

class TransferFile;

/**
 * This class represents a failed transfer. Such a transfer is removed
 * from queue so the error message can later be examined and the transfer
 * restarted.
 *
 * @author Jernej Kos
 */
class FailedTransfer : public TQObject
{
TQ_OBJECT
public:
    /**
     * Constructs a new failed transfer object. The actual transfer
     * will be reparented (the FailedTransfer object will become its
     * parent).
     */
    FailedTransfer(TQObject *parent, TransferFile *transfer, const TQString &error);
    ~FailedTransfer();
    
    /**
     * Returns the error message.
     *
     * @return The error message.
     */
    TQString getError() const { return m_error; }
    
    /**
     * Add this transfer back to the queue. The FailedTransfer object
     * will be destroyed afterwards!
     *
     * @return Pointer to the TransferFile object that was just restored.
     */
    TransferFile *restore();
    
    /**
     * Returns the actual transfer object that failed. This transfer is
     * marked as failed so execute() method can't be called!
     *
     * @return A KFTPQueue::TransferFile object.
     */
    TransferFile *getTransfer() const { return m_transfer; }
    
    /**
     * Use this method to declare a transfer as failed. The transfer will
     * be aborted, removed from queue and added to the failed transfer
     * list.
     *
     * @param transfer Pointer to the transfer object that failed.
     * @param error The error that ocurred.
     */
    static void fail(TransferFile *transfer, const TQString &error);
private:
    TQGuardedPtr<TransferFile> m_transfer;
    TQString m_error;
};

/**
 * This class is the base class for all transfers used in KFTPGrabber. It
 * provides some basic methods that are extended by KFTPQueue::TransferFile and
 * KFTPQueue::TransferDir for specific file or dir operations.
 *
 * @author Jernej Kos
 */
class Transfer : public QueueObject
{
friend class FailedTransfer;
friend class TransferDir;
friend class Manager;
friend class KFTPSession::Session;
friend class KFTPSession::Connection;
TQ_OBJECT
public:
    Transfer(TQObject *parent, Type type);
    ~Transfer();
    
    /**
     * Returns the source KURL of this transfer.
     *
     * @return Source url
     */
    KURL getSourceUrl() const { return m_sourceUrl; }
    
    /**
     * Returns the destination KURL of this transfer.
     *
     * @return Destination url
     */
    KURL getDestUrl() const { return m_destUrl; }
    
    /**
     * Set the source KURL of this transfer.
     *
     * @param url Source url wannabe
     */
    void setSourceUrl(const KURL &url) { m_sourceUrl = url; }
    
    /**
     * Set the destination url of this transfer.
     *
     * @param url Destination url wannabe
     */
    void setDestUrl(const KURL &url) { m_destUrl = url; }
        
    /**
     * Return the KFTPQueue::TransferType -- that is if this transfer is an Upload, Download
     * or FXP transfer.
     *
     * @return Upload, Download or FXP
     */
    TransferType getTransferType() const { return m_transferType; }
    
    /**
     * Set current KFTPQueue::TransferType -- that is Upload, Download or FXP
     *
     * @param type Upload, Download or FXP
     */
    void setTransferType(TransferType type) { m_transferType = type; }
    
    /**
     * Get the source session for this transfer.
     *
     * @return A valid KFTPSession::Session instance or 0 if not started
     */
    KFTPSession::Session *getSourceSession() const { return m_srcSession; }
    
    /**
     * Get the destination session for this transfer.
     *
     * @return A valid KFTPSession::Session instance or 0 if not started
     */
    KFTPSession::Session *getDestinationSession() const { return m_dstSession; }
    
    /**
     * Returns the connection opposite of one that is passed. So if you
     * pass the source connection, the destination one is returned and
     * vice-versa.
     *
     * @param conn The connection
     * @return The opposite Connection
     */
    KFTPSession::Connection *getOppositeConnection(KFTPSession::Connection *conn);
    
    /**
     * Returns the remote connection. If both connections are remote, this
     * method returns the source connection.
     *
     * @return The remote connection
     */
    KFTPSession::Connection *remoteConnection();
    
    /**
     * Is this transfer a child of another transfer ?
     *
     * @return true if this transfer is a child of another KFTPQueue::Transfer
     */
    bool hasParentTransfer() const { return parent()->inherits("KFTPQueue::Transfer"); }
    
    /**
     * Should a transfered file be automagicly opened after transfer ? This only applies for
     * download transfers.
     *
     * @param value The setting
     */
    void setOpenAfterTransfer(bool value) { m_openAfterTransfer = value; }
    
    /**
     * Is this transfer marked for deletion ?
     *
     * @return true if this transfer is marked for deletion
     */
    bool isDeleteMarked() const { return m_deleteMe; }
    
    /**
     * Get the transfer's parent transfer.
     *
     * @return Transfer's parent or NULL if isChild() returns false
     */
    Transfer *parentTransfer();
    
    /**
     * Lock this transfer for further changes. 
     */
    void lock();
    
    /**
     * Unlock a previously locked transfer.
     */
    void unlock();
    
    /**
     * Abort current transfer.
     */
    virtual void abort();
    
    /**
     * Just emits the objectUpdated() signal.
     */
    void emitUpdate() { emit objectUpdated(); }
    
    /**
     * Assign sessions to this transfer in advance (= before starting the
     * actual transfer). Both sessions must have free connections. If you
     * pass NULL to both parameters sessions will be looked up and might
     * be spawned.
     *
     * Note that the sessions MUST be the right ones based on the transfer's
     * URL, otherwise unexpected results will ocurr!
     *
     * @param source The source session
     * @param destination The destination session
     * @return True if the sessions are ready for immediate use
     */
    virtual bool assignSessions(KFTPSession::Session *source = 0, KFTPSession::Session *destination = 0);
    
    /**
     * This method returns true if both connections have been properly
     * initialized.
     */ 
    bool connectionsReady();
protected:
    bool m_deleteMe;
    bool m_openAfterTransfer;
    TransferType m_transferType;
    
    /* Source/destination URL */
    KURL m_sourceUrl;
    KURL m_destUrl;
    
    /* Transfer sessions */
    KFTPSession::Session *m_srcSession;
    KFTPSession::Session *m_dstSession;
    
    /* Source/destination connections */
    KFTPSession::Connection *m_srcConnection;
    KFTPSession::Connection *m_dstConnection;
    
    int m_retryCount;
    
    void showTransCompleteBalloon();
    void resetTransfer();
    
    void update();
    bool canMove();
    
    /**
     * This method gets called just before the transfer is removed.
     *
     * @param abortSession If true any session that this transfer is using is aborted
     */
    void faceDestruction(bool abortSession = true);
    
    /**
     * Initialize the specified session for use with this transfer.
     *
     * @param session The session to use
     * @return A valid Connection or NULL if one wasn't available
     */
    KFTPSession::Connection *initializeSession(KFTPSession::Session *session);
    
    /**
     * Deinitialize currently acquired connections. Do not call this method
     * unless you know what you are doing.
     */
    void deinitializeConnections();
private slots:
    void slotConnectionAvailable();
    void slotConnectionConnected();
signals:
    void transferStart(long id);
    void transferComplete(long id);
    void transferAbort(long id);
};

}

#endif