summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/kftptransfer.cpp
blob: 82d44b71a2c7e8435d0f7c854d804c472472488c (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2004 by the KFTPGrabber developers
 * Copyright (C) 2003-2004 Jernej Kos <kostko@jweb-network.net>
 *
 * 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.
 */

#include "kftptransfer.h"
#include "kftptransferdir.h"
#include "widgets/systemtray.h"
#include "kftpqueue.h"
#include "kftpsession.h"

#include "misc/kftpconfig.h"

#include <tdelocale.h>

using namespace KFTPSession;

namespace KFTPQueue {

FailedTransfer::FailedTransfer(TQObject *parent, TransferFile *transfer, const TQString &error)
  : TQObject(parent),
    m_transfer(transfer),
    m_error(error)
{
  // Check if the transfer's site should be removed as well
  QueueObject *site = 0;
  if (transfer->parentObject()->getType() == QueueObject::Site && transfer->parentObject()->getChildrenList().count() == 1)
    site = transfer->parentObject();
  
  // Reparent the transfer, we can't remove it from the QueueObject's children list yet, because
  // that will cause an iterator reset for directories, so we'll do that later in the fail method.
  transfer->parentObject()->addSize(-transfer->m_actualSize);
  
  transfer->parent()->removeChild(transfer);
  insertChild(transfer);
  KFTPQueue::Manager::self()->getFailedTransferList()->append(this);

  // Check if the transfer's site should be removed as well
  if (site) {
    emit KFTPQueue::Manager::self()->siteRemoved(site->getId());
    delete site;
  }
  
  // Mark transfer as failed
  transfer->m_status = Transfer::Failed;
}

FailedTransfer::~ FailedTransfer()
{
}

TransferFile *FailedTransfer::restore()
{
  // Emit failed transfer removal
  emit KFTPQueue::Manager::self()->failedTransferRemoved(m_transfer->getId());
  
  // Add the transfer back to the queue
  removeChild(m_transfer);
  KFTPQueue::Manager::self()->insertTransfer(m_transfer);
  
  // Change the transfer's status, so it can be started
  m_transfer->m_status = Transfer::Stopped;
  
  // This object is now useless, so it shall be removed
  KFTPQueue::Manager::self()->getFailedTransferList()->remove(this);
  deleteLater();
  
  return m_transfer;
}

void FailedTransfer::fail(TransferFile *transfer, const TQString &error)
{
  // Should the transfer be retried
  if (KFTPCore::Config::failedAutoRetry() && transfer->m_retryCount < KFTPCore::Config::failedAutoRetryCount()) {
    // Semi-reset the current transfer
    transfer->addCompleted(-transfer->m_completed);
    
    transfer->m_retryCount++;
    transfer->m_resumed = 0;
    transfer->m_completed = 0;
    transfer->m_aborting = false;
    transfer->m_size = transfer->m_actualSize;
    
    transfer->setSpeed(0);
    
    // Restart the transfer
    transfer->delayedExecute();
  } else {
    // Abort the transfer
    transfer->blockSignals(true);
    transfer->abort();
    transfer->blockSignals(false);
    
    TQGuardedPtr<QueueObject> transferParent(transfer->parentObject());
    
    // Create a new failed transfer (will automaticly reparent the transfer)
    FailedTransfer *ft = new FailedTransfer(KFTPQueue::Manager::self(), transfer, error);
    
    // Notify others about transfer's "completion"
    emit transfer->transferComplete(transfer->m_id);
    
    // Now that the iterators have been incremented we can remove the child without
    // doing any harm to the queue processing.
    if (transferParent)
      transferParent->delChildObject(transfer);
    
    // Fake transfer removal and signal new failed transfer
    emit KFTPQueue::Manager::self()->transferRemoved(transfer->m_id);
    emit KFTPQueue::Manager::self()->failedTransferNew(ft);
  }
}

Transfer::Transfer(TQObject *parent, Type type)
 : QueueObject(parent, type),
   m_deleteMe(false),
   m_openAfterTransfer(false),
   m_srcSession(0),
   m_dstSession(0),
   m_srcConnection(0),
   m_dstConnection(0),
   m_retryCount(0)
{
}


Transfer::~Transfer()
{
}

Connection *Transfer::getOppositeConnection(Connection *conn)
{
  return m_srcConnection == conn ? m_dstConnection : m_srcConnection;
}

Connection *Transfer::remoteConnection()
{
  return m_srcConnection ? m_srcConnection : m_dstConnection;
}

Connection *Transfer::initializeSession(Session *session)
{
  if (!session->isFreeConnection()) {
    // We should wait for a connection to come
    connect(session, SIGNAL(freeConnectionAvailable()), this, SLOT(slotConnectionAvailable()));
    
    if (m_status != Waiting) {
      m_status = Waiting;
      emit objectUpdated();
    }
    
    return 0;
  }
  
  Connection *connection = session->assignConnection();
  connection->acquire(this);
  
  connect(connection->getClient()->eventHandler(), SIGNAL(connected()), this, SLOT(slotConnectionConnected()));
  
  return connection;
}

void Transfer::deinitializeConnections()
{
  if (m_srcConnection) {
    m_srcConnection->getClient()->eventHandler()->disconnect(this);
    m_srcConnection->remove();
  }
    
  if (m_dstConnection) {
    m_dstConnection->getClient()->eventHandler()->disconnect(this);
    m_dstConnection->remove();
  }
}

bool Transfer::assignSessions(Session *source, Session *destination)
{
  bool result = true;
  
  // We need a source session
  if (!m_sourceUrl.isLocalFile() && !m_srcConnection) {
    if (!source)
      m_srcSession = KFTPSession::Manager::self()->spawnRemoteSession(IgnoreSide, m_sourceUrl, 0);
    else
      m_srcSession = source;
    
    if (!(m_srcConnection = initializeSession(m_srcSession)))
      result = false;
  }
  
  // We need a destination session
  if (!m_destUrl.isLocalFile() && !m_dstConnection) {
    if (!destination)
      m_dstSession = KFTPSession::Manager::self()->spawnRemoteSession(m_srcSession ? oppositeSide(m_srcSession->getSide()) : IgnoreSide, m_destUrl, 0);
    else
      m_dstSession = destination;
    
    if (!(m_dstConnection = initializeSession(m_dstSession)))
      result = false;
  }
  
  return result;
}

bool Transfer::connectionsReady()
{
  return (m_sourceUrl.isLocalFile() || m_srcConnection) && (m_destUrl.isLocalFile() || m_dstConnection); 
}

void Transfer::slotConnectionAvailable()
{
  if (getStatus() != Waiting || (m_srcSession && !m_srcSession->isFreeConnection()) || (m_dstSession && !m_dstSession->isFreeConnection()))
    return;
  
  if (m_srcSession)
    m_srcSession->TQObject::disconnect(this, SLOT(slotConnectionAvailable()));
  
  if (m_dstSession)
    m_dstSession->TQObject::disconnect(this, SLOT(slotConnectionAvailable()));
  
  // Connection has become available, grab it now
  execute();
}

void Transfer::slotConnectionConnected()
{
  if ((m_srcConnection && !m_srcConnection->isConnected()) || (m_dstConnection && !m_dstConnection->isConnected()))
    return;
  
  // Everything is ready for immediate execution
  delayedExecute();
}

void Transfer::faceDestruction(bool abortSession)
{
  // This method is called before the object is deleted by the queue
  // manager.
  if (hasParentObject()) {
    parentObject()->addSize(-m_actualSize);
    parentObject()->delChildObject(this);
  }
    
  // Abort any dir scans that might be in progress
  if (abortSession) {
    KFTPSession::Session *session = KFTPSession::Manager::self()->find(this);
    
    if (session)
      session->abort();
  }
}

Transfer *Transfer::parentTransfer()
{
  if (!hasParentTransfer())
    return 0L;
  
  return static_cast<Transfer*>(parent());
}

void Transfer::resetTransfer()
{
  // Disconnect signals
  if (getStatus() != Waiting) {
    if (m_srcConnection)
      m_srcConnection->getClient()->eventHandler()->TQObject::disconnect(this, SLOT(slotConnectionConnected()));
    
    if (m_dstConnection)
      m_dstConnection->getClient()->eventHandler()->TQObject::disconnect(this, SLOT(slotConnectionConnected()));
  }
  
  // Reset connections & session pointers
  m_srcConnection = 0L;
  m_dstConnection = 0L;
  
  m_srcSession = 0L;
  m_dstSession = 0L;
  
  m_status = Stopped;
  m_resumed = 0;
  m_completed = 0;
  m_aborting = false;
  m_size = m_actualSize;
  m_retryCount = 0;
  
  // Set the transfer speed to zero
  setSpeed(0);
}

bool Transfer::canMove()
{
  return !isRunning();
}

void Transfer::update()
{
  KFTPQueue::Manager::self()->doEmitUpdate();
  emit objectUpdated();
}

void Transfer::abort()
{
  // Unlock if the transfer was locked
  unlock();
  
  // Set the aborting flag
  m_aborting = true;
  
  emit transferAbort(m_id);
}

void Transfer::showTransCompleteBalloon()
{
  // Show a balloon :P
  if (KFTPCore::Config::showBalloons() && !KFTPQueue::Manager::self()->isProcessing()) {
    if (!KFTPCore::Config::showBalloonWhenQueueEmpty() || (KFTPQueue::Manager::self()->topLevelObject()->getChildrenList().count() == 1 && !hasParentTransfer())) {
      TQString transCompleteStr = i18n("Transfer of the following files is complete:");
      transCompleteStr += "<br><i>";
      transCompleteStr += getSourceUrl().fileName();
      transCompleteStr += "</i>";
      KFTPWidgets::SystemTray::self()->showBalloon(transCompleteStr);
    }
  }
}

void Transfer::lock()
{
  // Transfers can only be locked if they are not currently running
  if (m_status == Stopped) {
    m_status = Locked;
    
    emit objectUpdated();
  }
}

void Transfer::unlock()
{
  if (isLocked()) {
    m_status = Stopped;
    
    emit objectUpdated();
  }
}

}
#include "kftptransfer.moc"