summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/kftpsession.cpp
blob: cf9dba2fc87bac778cc44468ff268562479405d1 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*
 * 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 "kftpsession.h"
#include "kftpapi.h"
#include "browser/detailsview.h"
#include "browser/treeview.h"
#include "browser/view.h"
#include "kftpbookmarks.h"
#include "misc.h"
#include "widgets/systemtray.h"
#include "mainactions.h"

#include "misc/kftpconfig.h"
#include "misc/filter.h"

#include <ntqdir.h>
#include <ntqobjectlist.h>

#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <kpassdlg.h>
#include <kstaticdeleter.h>

using namespace KFTPGrabberBase;
using namespace KFTPEngine;
using namespace KFTPCore::Filter;

namespace KFTPSession {

//////////////////////////////////////////////////////////////////
////////////////////////    Connection     ///////////////////////
//////////////////////////////////////////////////////////////////

Connection::Connection(Session *session, bool primary)
  : TQObject(session),
    m_primary(primary),
    m_busy(false),
    m_aborting(false),
    m_scanning(false)
{
  // Create the actual connection client
  m_client = new KFTPEngine::Thread();
  
  connect(m_client->eventHandler(), SIGNAL(engineEvent(KFTPEngine::Event*)), this, SLOT(slotEngineEvent(KFTPEngine::Event*)));

  // If this is not a core session connection, connect
  if (!primary) {
    // Connect to the server
    KURL url = session->getClient()->socket()->getCurrentUrl();
    
    KFTPBookmarks::Manager::self()->setupClient(session->getSite(), m_client);
    m_client->connect(url);
  }
}

Connection::~Connection()
{
  delete m_client;
}

bool Connection::isConnected()
{
  return !static_cast<Session*>(parent())->isRemote() || m_client->socket()->isConnected();
}

void Connection::acquire(KFTPQueue::Transfer *transfer)
{
  if (m_busy || !static_cast<Session*>(parent())->isRemote())
    return;

  m_curTransfer = transfer;
  m_busy = true;

  connect(transfer, SIGNAL(transferComplete(long)), this, SLOT(slotTransferCompleted()));
  connect(transfer, SIGNAL(transferAbort(long)), this, SLOT(slotTransferCompleted()));

  emit connectionAcquired();
}

void Connection::remove()
{
  // Disconnect all signals from the transfer
  if (m_curTransfer)
    m_curTransfer->TQObject::disconnect(this);

  m_curTransfer = 0L;
  m_busy = false;

  emit connectionRemoved();
  emit static_cast<Session*>(parent())->freeConnectionAvailable();
}

void Connection::abort()
{
  if (m_aborting || !m_client->socket()->isBusy())
    return;

  // Emit the signal before aborting
  emit aborting();

  // Abort transfer
  m_aborting = true;
  m_client->abort();
  m_aborting = false;
}

void Connection::scanDirectory(KFTPQueue::Transfer *parent)
{
  // Lock the connection and the transfer
  acquire(parent);
  parent->lock();
  
  m_scanning = true;
  
  if (isConnected())
    m_client->scan(parent->getSourceUrl());
}

void Connection::addScannedDirectory(KFTPEngine::DirectoryTree *tree, KFTPQueue::Transfer *parent)
{
  // Directories
  DirectoryTree::DirIterator dirEnd = tree->directories()->end();
  for (DirectoryTree::DirIterator i = tree->directories()->begin(); i != dirEnd; i++) {
    KURL sourceUrlBase = parent->getSourceUrl();
    KURL destUrlBase = parent->getDestUrl();
    
    sourceUrlBase.addPath((*i)->info().filename());
    destUrlBase.addPath((*i)->info().filename());
    
    // Check if we should skip this entry
    const ActionChain *actionChain = Filters::self()->process(sourceUrlBase, 0, true);
     
    if (actionChain && actionChain->getAction(Action::Skip))
      continue;
    
    // Add directory transfer
    KFTPQueue::TransferDir *transfer = new KFTPQueue::TransferDir(parent);
    transfer->setSourceUrl(sourceUrlBase);
    transfer->setDestUrl(destUrlBase);
    transfer->setTransferType(parent->getTransferType());
    transfer->setId(KFTPQueue::Manager::self()->nextTransferId());
    
    emit KFTPQueue::Manager::self()->newTransfer(transfer);
    
    addScannedDirectory(*i, transfer);
    
    if (KFTPCore::Config::skipEmptyDirs() && !transfer->hasChildren())
      KFTPQueue::Manager::self()->removeTransfer(transfer, false);
  }
  
  // Files
  DirectoryTree::FileIterator fileEnd = tree->files()->end();
  for (DirectoryTree::FileIterator i = tree->files()->begin(); i != fileEnd; i++) {
    KURL sourceUrlBase = parent->getSourceUrl();
    KURL destUrlBase = parent->getDestUrl();
    
    sourceUrlBase.addPath((*i).filename());
    destUrlBase.addPath((*i).filename());
    
    // Check if we should skip this entry
    const ActionChain *actionChain = Filters::self()->process(sourceUrlBase, (*i).size(), false);
     
    if (actionChain && actionChain->getAction(Action::Skip))
      continue;
    
    // Add file transfer
    KFTPQueue::TransferFile *transfer = new KFTPQueue::TransferFile(parent);
    transfer->addSize((*i).size());
    transfer->setSourceUrl(sourceUrlBase);
    transfer->setDestUrl(destUrlBase);
    transfer->setTransferType(parent->getTransferType());
    transfer->setId(KFTPQueue::Manager::self()->nextTransferId());
    
    emit KFTPQueue::Manager::self()->newTransfer(transfer);
  }
}

void Connection::slotEngineEvent(KFTPEngine::Event *event)
{
  switch (event->type()) {
    case Event::EventDisconnect: {
      emit connectionLost(this);
      break;
    }
    case Event::EventConnect: {
      emit connectionEstablished();
      
      if (m_scanning) {
        // Connected successfully, let's scan
        m_client->scan(m_curTransfer->getSourceUrl());
      }
      break;
    }
    case Event::EventError: {
      ErrorCode error = event->getParameter(0).asErrorCode();
      
      if (m_scanning && (error == ConnectFailed || error == LoginFailed || error == OperationFailed)) {
        // Scanning should be aborted, since there was an error
        m_scanning = false;
        m_curTransfer->unlock();
        remove();
        
        emit static_cast<Session*>(parent())->dirScanDone();
      }
      break;
    }
    case Event::EventScanComplete: {
      if (m_scanning) {
        // We have the listing
        DirectoryTree *tree = static_cast<DirectoryTree*>(event->getParameter(0).asData());
        addScannedDirectory(tree, m_curTransfer);
        delete tree;
        
        m_scanning = false;
        m_curTransfer->unlock();
        remove();
        
        emit static_cast<Session*>(parent())->dirScanDone();
      }
      break;
    }
    default: break;
  }
}

void Connection::slotTransferCompleted()
{
  // Remove the lock
  remove();
}

void Connection::reconnect()
{
  if (!m_client->socket()->isConnected()) {
    KFTPBookmarks::Manager::self()->setupClient(static_cast<Session*>(parent())->getSite(), m_client);
    m_client->connect(m_client->socket()->getCurrentUrl());
  }
}

////////////////////////////////////////////////////////
////////////////////    Session     ////////////////////
////////////////////////////////////////////////////////

Session::Session(Side side)
  : TQObject(),
    m_side(side),
    m_remote(false),
    m_aborting(false),
    m_registred(false),
    m_site(0)
{
  // Register this session
  Manager::self()->registerSession(this);
}

Session::~Session()
{
}

KFTPEngine::Thread *Session::getClient()
{
  // Return the first (core) connection's client
  return m_connections.at(0)->getClient();
}

bool Session::isConnected()
{
  // If there are no connections, just check if the session is remote
  if (m_connections.count() == 0)
    return !m_remote;
  
  return m_connections.at(0)->isConnected();
}

void Session::slotClientEngineEvent(KFTPEngine::Event *event)
{
  switch (event->type()) {
    case Event::EventConnect: {
      // ***************************************************************************
      // ****************************** EventConnect *******************************
      // ***************************************************************************
      m_remote = true;
      m_aborting = false;
      m_lastUrl = getClient()->socket()->getCurrentUrl();
      
      TQString siteName;
      if (m_site)
        siteName = m_site->getAttribute("name");
      else
        siteName = m_lastUrl.host();
  
      Manager::self()->getTabs(m_side)->changeTab(m_fileView, siteName);
      Manager::self()->getStatTabs()->changeTab(m_log, i18n("Log (%1)").arg(siteName));
      Manager::self()->getStatTabs()->showPage(m_log);
      Manager::self()->doEmitUpdate();
      
      KURL homeUrl = getClient()->socket()->getCurrentUrl();
      
      if (m_site && !m_site->getProperty("defremotepath").isEmpty())
        homeUrl.setPath(m_site->getProperty("defremotepath"));
      else
        homeUrl.setPath(getClient()->socket()->getDefaultDirectory());
      
      m_fileView->setHomeUrl(homeUrl);
      m_fileView->goHome();
      
      Session *opposite = Manager::self()->getActive(oppositeSide(m_side));
      
      if (m_site && !opposite->isRemote()) {
        TQString localPath = m_site->getProperty("deflocalpath");
        
        if (!localPath.isEmpty())
          opposite->getFileView()->openUrl(KURL(localPath));
      }
      break;
    }
    case Event::EventDisconnect: {
      // ***************************************************************************
      // **************************** EventDisconnect ******************************
      // ***************************************************************************
      m_remote = false;
      m_aborting = false;
    
      Manager::self()->getTabs(m_side)->changeTab(m_fileView, i18n("Local Session"));
      Manager::self()->getStatTabs()->changeTab(m_log, "[" + i18n("Log") + "]");
      Manager::self()->doEmitUpdate();
      
      m_fileView->setHomeUrl(KURL(KFTPCore::Config::defLocalDir()));
      m_fileView->goHome();
      break;
    }
    case Event::EventCommand: m_log->ftpLog(1, event->getParameter(0).asString());  break;
    case Event::EventMultiline: m_log->ftpLog(2, event->getParameter(0).asString()); break;
    case Event::EventResponse: m_log->ftpLog(0, event->getParameter(0).asString()); break;
    case Event::EventMessage: m_log->ftpLog(3, event->getParameter(0).asString()); break;
    case Event::EventRetrySuccess: {
      // ***************************************************************************
      // ************************** EventRetrySuccess ******************************
      // ***************************************************************************
      if (KFTPCore::Config::showRetrySuccessBalloon()) {
        KFTPWidgets::SystemTray::self()->showBalloon(i18n("Connection with %1 has been successfully established.").arg(getClient()->socket()->getCurrentUrl().host()));
      }
      break;
    }
    case Event::EventReloadNeeded: {
      // We should only do refreshes if the queue is not being processed
      if (KFTPQueue::Manager::self()->getNumRunning() == 0)
        m_fileView->reload();
      break;
    }
    case Event::EventPubkeyPassword: {
      // A public-key authentication password was requested
      TQCString pass;
      int ret = KPasswordDialog::getPassword(pass, i18n("Please provide your private key decryption password."));
      
      if (ret == KPasswordDialog::Accepted) {
        PubkeyWakeupEvent *event = new PubkeyWakeupEvent();
        event->password = pass;
        
        getClient()->wakeup(event);
      } else {
        getClient()->abort();
      }
      break;
    }
    default: break;
  }
}

void Session::scanDirectory(KFTPQueue::Transfer *parent, Connection *connection)
{
  // Go trough all files in path and add them as transfers that have parent as their parent
  // transfer
  KURL path = parent->getSourceUrl();

  if (path.isLocalFile()) {
    connect(new DirectoryScanner(parent), SIGNAL(completed()), this, SIGNAL(dirScanDone()));
  } else if (m_remote) {
    if (!connection) {
      if (!isFreeConnection()) {
        emit dirScanDone();
        return;
      }
      
      // Assign a new connection (it might be unconnected!)
      connection = assignConnection();
    }
    
    connection->scanDirectory(parent);
  }
}

void Session::abort()
{
  if (m_aborting)
    return;

  m_aborting = true;

  emit aborting();

  // Abort all connections
  Connection *conn;
  for (conn = m_connections.first(); conn; conn = m_connections.next()) {
    conn->abort();
  }

  m_aborting = false;
}

void Session::reconnect(const KURL &url)
{
  // Set the reconnect url
  m_reconnectUrl = url;
  
  if (m_remote && getClient()->socket()->isConnected()) {
    abort();
    
    connect(getClient()->eventHandler(), SIGNAL(disconnected()), this, SLOT(slotStartReconnect()));
    getClient()->disconnect();
  } else {
    // The session is already disconnected, just call the slot
    slotStartReconnect();
  }
}

void Session::slotStartReconnect()
{
  disconnect(getClient()->eventHandler(), SIGNAL(disconnected()), this, SLOT(slotStartReconnect()));
  
  // Reconnect only if this is a remote url
  if (!m_reconnectUrl.isLocalFile()) {
    KFTPBookmarks::Manager::self()->setupClient(m_site, getClient());
    getClient()->connect(m_reconnectUrl);
  }
  
  // Invalidate the url
  m_reconnectUrl = KURL();
}

int Session::getMaxThreadCount()
{
  // First get the global thread count
  int count = KFTPCore::Config::threadCount();
  
  if (!KFTPCore::Config::threadUsePrimary())
    count++;

  // Try to see if threads are disabled for this site
  if (count > 1 && isRemote()) {
    if (m_site && m_site->getIntProperty("disableThreads"))
      return 1;
  }

  return count;
}

bool Session::isFreeConnection()
{
  unsigned int max = getMaxThreadCount();
  unsigned int free = 0;

  if ((m_connections.count() < max && max > 1) || !isRemote())
    return true;

  Connection *conn;
  for (conn = m_connections.first(); conn; conn = m_connections.next()) {
    if (!conn->isBusy() && (!conn->isPrimary() || KFTPCore::Config::threadUsePrimary() || max == 1))
      free++;
  }

  return free > 0;
}

Connection *Session::assignConnection()
{
  unsigned int max = getMaxThreadCount();

  if (m_connections.count() == 0) {
    // We need a new core connection
    Connection *conn = new Connection(this, true);
    m_connections.append(conn);

    Manager::self()->doEmitUpdate();

    return conn;
  } else {
    // Find a free connection
    Connection *conn;
    for (conn = m_connections.first(); conn; conn = m_connections.next()) {
      if (!conn->isBusy() && (!conn->isPrimary() || KFTPCore::Config::threadUsePrimary() || max == 1))
        return conn;
    }

    // No free connection has been found, but we may be able to create
    // another (if we are within limits)
    if (m_connections.count() < max) {
      conn = new Connection(this);
      m_connections.append(conn);

      Manager::self()->doEmitUpdate();

      return conn;
    }
  }

  return 0;
}

void Session::disconnectAllConnections()
{
  // Abort any possible transfers first
  abort();
  
  // Now disconnect all connections
  Connection *conn;

  for (conn = m_connections.first(); conn; conn = m_connections.next()) {
    if (conn->getClient()->socket()->isConnected()) {
      conn->getClient()->disconnect();
    }
  }
}

////////////////////////////////////////////////////////
/////////////////////// Manager ////////////////////////
////////////////////////////////////////////////////////

Manager *Manager::m_self = 0;

Manager *Manager::self()
{
  return m_self;
}

Manager::Manager(TQObject *parent, TQTabWidget *stat, KFTPTabWidget *left, KFTPTabWidget *right)
  : TQObject(parent),
    m_statTabs(stat),
    m_leftTabs(left),
    m_rightTabs(right),
    m_active(0),
    m_leftActive(0),
    m_rightActive(0)
{
  Manager::m_self = this;

  // Connect some signals
  connect(left, SIGNAL(currentChanged(TQWidget*)), this, SLOT(slotActiveChanged(TQWidget*)));
  connect(right, SIGNAL(currentChanged(TQWidget*)), this, SLOT(slotActiveChanged(TQWidget*)));

  connect(left, SIGNAL(closeRequest(TQWidget*)), this, SLOT(slotSessionCloseRequest(TQWidget*)));
  connect(right, SIGNAL(closeRequest(TQWidget*)), this, SLOT(slotSessionCloseRequest(TQWidget*)));
}

void Manager::registerSession(Session *session)
{
  m_active = session;
  
  // Create some new stuff and assign it to the session
  session->assignConnection();
  session->m_fileView = new KFTPWidgets::Browser::View(0L, "", session->getClient(), session);
  session->m_log = new KFTPWidgets::LogView();
  
  // Install event filters
  session->getFileView()->getDetailsView()->installEventFilter(this);
  session->getFileView()->getTreeView()->installEventFilter(this);
  session->getFileView()->m_toolBarFirst->installEventFilter(this);
  session->getFileView()->m_toolBarSecond->installEventFilter(this);

  connect(session->getFileView()->getDetailsView(), SIGNAL(clicked(TQListViewItem*)), this, SLOT(slotSwitchFocus()));
  connect(session->getFileView()->getTreeView(), SIGNAL(clicked(TQListViewItem*)), this, SLOT(slotSwitchFocus()));
  connect(session->getFileView()->m_toolBarFirst, SIGNAL(clicked(int)), this, SLOT(slotSwitchFocus()));
  connect(session->getFileView()->m_toolBarSecond, SIGNAL(clicked(int)), this, SLOT(slotSwitchFocus()));

  // Connect some signals
  connect(session->getClient()->eventHandler(), SIGNAL(engineEvent(KFTPEngine::Event*)), session, SLOT(slotClientEngineEvent(KFTPEngine::Event*)));

  // Assign GUI positions
  m_statTabs->addTab(session->m_log, "[" + i18n("Log") + "]");
  getTabs(session->m_side)->addTab(session->m_fileView, KFTPGrabberBase::loadSmallIcon("computer"), i18n("Session"));

  // Actually add the session
  m_sessionList.append(session);
  session->m_registred = true;
}

KFTPWidgets::Browser::View *Manager::getActiveView()
{
  if (m_active)
    return m_active->getFileView();
  
  return 0;
}

Session *Manager::getActiveSession()
{
  return m_active;
}

bool Manager::eventFilter(TQObject *object, TQEvent *event)
{
  if (event->type() == TQEvent::FocusIn || event->type() == TQEvent::MouseButtonPress) {
    switchFocusToObject(object);
  }
  
  return false;
}

void Manager::slotSwitchFocus()
{
  switchFocusToObject(TQObject::sender());
}

void Manager::switchFocusToObject(const TQObject *object)
{
  if (!object)
    return;
  
  for (;;) {
    if (object->isA("KFTPWidgets::Browser::View"))
      break;
    
    if (!(object = object->parent()))
      break;
  }
  
  if (object) {
    // We have the proper object
    Session *session = find(static_cast<const KFTPWidgets::Browser::View*>(object));
    
    if (session && session != m_active) {
      m_active = session;
      
      // Open the current session's log tab
      if (session->isRemote())
        m_statTabs->showPage(session->getLog());
    }
  }
}

void Manager::unregisterSession(Session *session)
{
  // Destroy all objects related to the session and remove it
  getTabs(session->m_side)->removePage(session->m_fileView);
  m_statTabs->removePage(session->m_log);

  if (session->getClient()->socket()->isConnected()) {
    session->abort();
    session->getClient()->disconnect();
  }

  // Delete objects
  session->m_fileView->deleteLater();
  session->m_log->deleteLater();

  // Actually remove the session
  m_sessionList.remove(session);
  delete session;
  
  // Emit update
  emit update();
}

void Manager::doEmitUpdate()
{
  emit update();
}

void Manager::disconnectAllSessions()
{
  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next())
    i->disconnectAllConnections();
}

Session *Manager::find(KFTPEngine::Thread *client)
{
  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    if (i->getClient() == client)
      return i;
  }

  return 0L;
}

Session *Manager::find(KFTPWidgets::Browser::View *fileView)
{
  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    if (i->m_fileView == fileView)
      return i;
  }

  return 0L;
}

Session *Manager::find(KFTPWidgets::LogView *log)
{
  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    if (i->m_log == log)
      return i;
  }

  return 0L;
}

Session *Manager::find(const KURL &url, bool mustUnlock)
{
  if (url.isLocalFile())
    return find(true);

  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    KURL tmp = i->getClient()->socket()->getCurrentUrl();
    tmp.setPath(url.path());

    if (tmp == url && i->isRemote() && i->isConnected() && (!mustUnlock || i->isFreeConnection()))
      return i;
  }
  
  return 0L;
}

Session *Manager::find(bool local)
{
  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    if (i->m_remote != local)
      return i;
  }

  return 0L;
}

Session *Manager::findLast(const KURL &url, Side side)
{
  if (url.isLocalFile())
    return find(true);

  Session *i;

  for (i = m_sessionList.first(); i; i = m_sessionList.next()) {
    KURL tmp = i->m_lastUrl;
    tmp.setPath(url.path());

    if (tmp == url && !i->isRemote() && (i->getSide() || side == IgnoreSide))
      return i;
  }
  
  return 0L;
}

Session *Manager::spawnLocalSession(Side side, bool forceNew)
{
  // Creates a new local session
  Session *session = 0L;

  if (forceNew || (session = find(true)) == 0L || (session->m_side != side && side != IgnoreSide)) {
    side = side == IgnoreSide ? LeftSide : side;

    session = new Session(side);
    session->m_remote = false;
    getTabs(side)->changeTab(session->m_fileView, i18n("Local Session"));
    getStatTabs()->changeTab(session->m_log, "[" + i18n("Log") + "]");
  }

  setActive(session);
  return session;
}

Session *Manager::spawnRemoteSession(Side side, const KURL &remoteUrl, KFTPBookmarks::Site *site, bool mustUnlock)
{
  // Creates a new remote session and connects it to the correct server
  Session *session;

  if (remoteUrl.isLocalFile())
    return spawnLocalSession(side);

  if ((session = find(remoteUrl, mustUnlock)) == 0L || (session->m_side != side && side != IgnoreSide)) {
    // Try to find the session that was last connected to this URL
    if ((session = findLast(remoteUrl, side)) == 0L) {
      // Attempt to reuse a local session if one exists one the right side
      session = getActive(RightSide);
      
      if (session->isRemote()) {
        side = side == IgnoreSide ? RightSide : side;
        session = new Session(side);
      }
    }
  
    // Try to find the site by url if it is not set
    if (!site)
      site = KFTPBookmarks::Manager::self()->findSite(remoteUrl);

    // Set properties
    session->m_remote = true;
    session->m_site = site;
    m_active = session;

    KFTPBookmarks::Manager::self()->setupClient(site, session->getClient());
    session->getClient()->connect(remoteUrl);
  }

  return session;
}

void Manager::setActive(Session *session)
{
  // Make a session active on its own side ;)
  Session *oldActive = getActive(session->m_side);

  oldActive ? oldActive->m_active = false : 0;
  session->m_active = true;

  switch (session->m_side) {
    case LeftSide: m_leftActive = session; break;
    case RightSide: m_rightActive = session; break;
    case IgnoreSide: tqDebug("Invalid side specified!"); return;
  }

  // Refresh the GUI
  getTabs(session->m_side)->showPage(session->m_fileView);
}

Session *Manager::getActive(Side side)
{
  switch (side) {
    case LeftSide: return m_leftActive;
    case RightSide: return m_rightActive;
    case IgnoreSide: tqDebug("Invalid side specified!"); break;
  }

  return NULL;
}

KFTPTabWidget *Manager::getTabs(Side side)
{
  switch (side) {
    case LeftSide: return m_leftTabs;
    case RightSide: return m_rightTabs;
    case IgnoreSide: tqDebug("Invalid side specified!"); break;
  }

  return NULL;
}

void Manager::slotActiveChanged(TQWidget *page)
{
  Session *session = find(static_cast<KFTPWidgets::Browser::View*>(page));
  setActive(session);
}

void Manager::slotSessionCloseRequest(TQWidget *page)
{
  Session *session = find(static_cast<KFTPWidgets::Browser::View*>(page));

  if (getTabs(session->m_side)->count() == 1) {
    KMessageBox::error(0L, i18n("At least one session must remain open on each side."));
    return;
  }

  if ((session->m_remote && session->getClient()->socket()->isBusy()) || !session->isFreeConnection()) {
    KMessageBox::error(0L, i18n("Please finish all transfers before closing the session."));
    return;
  } else {
    // Remove the session
    if (session->getClient()->socket()->isConnected()) {
      if (KMessageBox::questionYesNo(0L, i18n("This session is currently connected. Are you sure you wish to disconnect?"), i18n("Close Session")) == KMessageBox::No)
        return;
    }

    unregisterSession(session);
  }
}

}

#include "kftpsession.moc"