summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/widgets/queueview/queueview.cpp
blob: daec26e9e271be329d2430f8f1519ddcbf2477a0 (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
/*
 * 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>
 * Copyright (C) 2005 Markus Brueffer <markus@brueffer.de>
 *
 * 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 "misc.h"

#include "listview.h"
#include "queueview.h"
#include "kftpqueue.h"
#include "kftpapi.h"
#include "queueeditor.h"
#include "widgets/searchdialog.h"
#include "misc/config.h"

#include <tdeapplication.h>
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <tdeio/job.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kmimetype.h>
#include <tdepopupmenu.h>
#include <tdelistviewsearchline.h>
#include <kdebug.h>

#include <ntqspinbox.h>
#include <ntqtooltip.h>

using namespace KFTPGrabberBase;
using namespace KFTPCore;

namespace KFTPWidgets {

QueueViewItem::QueueViewItem(QueueView *view, KFTPQueue::QueueObject *object, TQListView *parent)
  : TQListViewItem(parent),
    m_lastChild(0),
    m_queueObject(object),
    m_queueView(view),
    m_queueId(object->getId())
{
  init();
}

QueueViewItem::QueueViewItem(QueueView *view, KFTPQueue::QueueObject *object, TQListViewItem *parent)
  : TQListViewItem(parent, static_cast<QueueViewItem*>(parent)->lastChild()),
    m_lastChild(0),
    m_queueObject(object),
    m_queueView(view),
    m_queueId(object->getId())
{
  init();
}

QueueViewItem::~QueueViewItem()
{
  m_queueView->m_queuedItems.remove(m_queueId);
}

void QueueViewItem::insertItem(TQListViewItem *newChild)
{
  TQListViewItem::insertItem(newChild);
  m_lastChild = newChild;
}

void QueueViewItem::takeItem(TQListViewItem *item)
{
  if (item == m_lastChild) {
    TQListViewItem *above = item->itemAbove();
    
    if (above->parent() == m_lastChild->parent())
      m_lastChild = above;
    else
      m_lastChild = 0;
  }
  
  TQListViewItem::takeItem(item);
}

void QueueViewItem::moveUp()
{
  TQListViewItem *above = itemAbove();
  
  if (above && above->parent() == TQListViewItem::parent()) {
    TQListViewItem *previous = above->itemAbove();
    
    if (previous && previous->parent() == TQListViewItem::parent()) {
      QueueViewItem *parent = static_cast<QueueViewItem*>(TQListViewItem::parent());
      moveItem(previous);
      
      if (parent && this == parent->lastChild())
        parent->m_lastChild = above;
    } else {
      moveToTop();
    }
  }
}

void QueueViewItem::moveDown()
{
  QueueViewItem *parent = static_cast<QueueViewItem*>(TQListViewItem::parent());
  QueueViewItem *next = static_cast<QueueViewItem*>(nextSibling());
  
  if (next) {
    moveItem(next);
    
    if (parent && parent->lastChild() == next)
      parent->m_lastChild = this;
  }
}

void QueueViewItem::moveToTop()
{
  QueueViewItem *parent = static_cast<QueueViewItem*>(TQListViewItem::parent());
  
  // Just reinsert the item
  if (parent) {
    if (this == parent->lastChild())
      parent->m_lastChild = itemAbove();
    
    parent->TQListViewItem::takeItem(this);
    parent->TQListViewItem::insertItem(this);
  } else {
    ListView *view = m_queueView->m_queue;
    view->TQListView::takeItem(this);
    view->TQListView::insertItem(this);
  }
}

void QueueViewItem::moveToBottom()
{
  QueueViewItem *parent = static_cast<QueueViewItem*>(TQListViewItem::parent());
  
  // Just reinsert the item
  if (parent) {
    TQListViewItem *last = parent->lastChild();
    
    parent->takeItem(this);
    parent->insertItem(this);
    moveItem(last);
  } else {
    ListView *view = m_queueView->m_queue;
    view->takeItem(this);
    view->insertItem(this);
  }
}

void QueueViewItem::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int alignment)
{
  TQColorGroup _cg(cg);
  TQColor c = _cg.text();

  TQColor n_color;
  
  if (m_queueObject && m_queueObject->isTransfer()) {
    switch (m_queueObject->getStatus()) {
      case KFTPQueue::Transfer::Running: n_color.setRgb(255, 0, 0); break;
      case KFTPQueue::Transfer::Connecting: n_color.setRgb(0, 0, 255); break;
      case KFTPQueue::Transfer::Waiting: n_color.setRgb(0, 0, 255); break;
      case KFTPQueue::Transfer::Locked: n_color.setRgb(0, 150, 0); break;
      default: break;
    }
  }
  
  if (n_color.isValid())
    _cg.setColor(TQColorGroup::Text, n_color);

  TQListViewItem::paintCell(p, _cg, column, width, alignment);
  _cg.setColor(TQColorGroup::Text, c);
}

void QueueViewItem::init()
{
  if (m_queueObject->isTransfer()) {
    KFTPQueue::Transfer *transfer = static_cast<KFTPQueue::Transfer*>(getObject());

    setText(0, transfer->getSourceUrl().fileName());
    setText(1, TDEIO::convertSize(transfer->getActualSize()));
    setText(2, transfer->getSourceUrl().pathOrURL());
    setText(3, transfer->getDestUrl().pathOrURL());
    
    // Icon
    TQString iconText;
    if (transfer->isDir()) {
      iconText = "folder";
    } else {
      KMimeType::Ptr theType = KMimeType::findByURL("/" + transfer->getSourceUrl().path(), 0, false, true);
      iconText = theType->icon(TQString::null, false);
    }
  
    setPixmap(0, loadSmallPixmap(iconText));
  } else if (m_queueObject->getType() == KFTPQueue::QueueObject::Site) {
    KFTPQueue::Site *site = static_cast<KFTPQueue::Site*>(getObject());
    
    setText(0, TQString("%1:%2").arg(site->getUrl().host()).arg(site->getUrl().port()));
    setText(1, TDEIO::convertSize(site->getActualSize()));
    
    // Set the pixmap
    setPixmap(0, loadSmallPixmap("server"));
  }
}

void QueueViewItem::refresh()
{
  if (!m_queueObject)
    return;

  if (m_queueObject->isTransfer()) {
    KFTPQueue::Transfer *transfer = static_cast<KFTPQueue::Transfer*>(getObject());
    
    // Speed
    bool noSpeed = transfer->getStatus() == KFTPQueue::Transfer::Waiting || transfer->getStatus() == KFTPQueue::Transfer::Connecting;
    TQString speed;
    if ((!transfer->isDir() || !isOpen()) && transfer->isRunning() && !noSpeed) {
      speed.sprintf( "%lld B/s", (transfer->getSpeed()) );
  
      if (transfer->getSpeed() > 1024)
        speed.sprintf( "%lld KB/s", (transfer->getSpeed() / 1024) );
      
      if (transfer->getSpeed() > 1024*1024)
        speed.sprintf("%lld MB/s", (transfer->getSpeed() / 1024) / 1024);
    
      if (transfer->getSpeed() == 0 && transfer->getTransferType() != KFTPQueue::FXP)
        speed = i18n("stalled");
      
      if (transfer->getSpeed() == 0 && transfer->getTransferType() == KFTPQueue::FXP)
        speed = i18n("running");
    }
  
    // ETA
    TQString eta;
  
    if (transfer->isRunning() && transfer->getSpeed() > 0) {
      eta = TDEIO::convertSeconds(TDEIO::calculateRemainingSeconds(transfer->getSize(),
                                                               transfer->getCompleted(),
                                                               transfer->getSpeed()));
    } else {
      eta = TQString::null;
    }
  
    // Set the columns
    setText(0, transfer->getSourceUrl().fileName());
    setText(1, TDEIO::convertSize(transfer->getActualSize()));
    setText(2, transfer->getSourceUrl().pathOrURL());
    setText(3, transfer->getDestUrl().pathOrURL());
    setText(5, speed);
    setText(6, eta);
    
    // Don't show the file:// for local src/dest
    if (transfer->getSourceUrl().isLocalFile()) {
      setText(2, transfer->getSourceUrl().path());
    } else if (transfer->getDestUrl().isLocalFile()) {
      setText(3, transfer->getDestUrl().path());
    }
  
    // Progress
    int progress;
    int r_progress;
  
    if (transfer->getSize() == 0)
      progress = 0;
    else
      progress = transfer->getCompleted()*100/transfer->getSize();
  
    if (transfer->getResumed() == 0)
      r_progress = 0;
    else
      r_progress = transfer->getResumed()*100/transfer->getSize();
  
    if (transfer->getStatus() == KFTPQueue::Transfer::Waiting) {
      // Transfer is waiting for a free connection
      setText(4, i18n("Waiting for connection..."));
      setPixmap(4, NULL);
    } else if (transfer->getStatus() == KFTPQueue::Transfer::Connecting) {
      // Transfer is not yet connected,
      setText(4, i18n("Connecting..."));
      setPixmap(4, NULL);
    } else if (progress > 0) {
      setPixmap(4, createProgressPixmap(progress, r_progress));
  
      TQString progressText;
      progressText.sprintf("%d %%", progress);
      setText(4, progressText);
    } else {
      setPixmap(4, NULL);
      setText(4, TQString::null);
    }
  
    // Icon
    TQString iconText;
    if (transfer->isDir()) {
      iconText = "folder";
    } else {
      KMimeType::Ptr theType = KMimeType::findByURL("/" + transfer->getSourceUrl().path(), 0, false, true);
      iconText = theType->icon(TQString::null, false);
    }
  
    setPixmap(0, loadSmallPixmap(iconText));
  } else if (m_queueObject->getType() == KFTPQueue::QueueObject::Site) {
    KFTPQueue::Site *site = static_cast<KFTPQueue::Site*>(getObject());
    
    // Speed
    TQString speed;
    speed.sprintf( "%lld B/s", (site->getSpeed()) );

    if (site->getSpeed() > 1024)
      speed.sprintf( "%lld KB/s", (site->getSpeed() / 1024) );
    
    if (site->getSpeed() > 1024*1024)
      speed.sprintf("%lld MB/s", (site->getSpeed() / 1024) / 1024);
  
    if (site->getSpeed() == 0)
      speed = TQString::null;
  
    // ETA
    TQString eta;
  
    if (site->isRunning() && site->getSpeed() > 0) {
      eta = TDEIO::convertSeconds(TDEIO::calculateRemainingSeconds(site->getSize(),
                                                               site->getCompleted(),
                                                               site->getSpeed()));
    } else {
      eta = "";
    }
    
    // Progress
    if (site->isRunning()) {
      int progress = 0;
    
      if (site->getSize() > 0)
        progress = site->getCompleted()*100/site->getSize();
      
      setPixmap(4, createProgressPixmap(progress, 0));
    
      TQString progressText;
      progressText.sprintf("%d %%", progress);
      setText(4, progressText);
    } else {
      setPixmap(4, NULL);
      setText(4, TQString::null);
    }
    
    // Set the columns
    setText(0, TQString("%1:%2").arg(site->getUrl().host()).arg(site->getUrl().port()));
    setText(1, TDEIO::convertSize(site->getActualSize()));
    setText(5, speed);
    setText(6, eta);
    
    // Set the pixmap
    setPixmap(0, loadSmallPixmap("server"));
  }
}

QueueListView::QueueListView(TQWidget *parent)
  : ListView(parent)
{
}

void QueueListView::insertItem(TQListViewItem *item)
{
  TQListViewItem *last = lastChild();
  TQListView::insertItem(item);
  
  if (last)
    item->moveItem(last);
}

QueueView::QueueView(TQWidget *parent, const char *name)
 : TQWidget(parent, name)
{
  TQVBoxLayout *layout = new TQVBoxLayout(this);

  m_toolBar = new TDEToolBar(this, "queueToolBar");
  m_toolBar->setIconSize(16);
  layout->addWidget(m_toolBar);
  
  m_searchToolBar = new TDEToolBar(this, "searchToolBar");
  m_searchToolBar->setEnableContextMenu(false);
  m_searchToolBar->setMovingEnabled(false);
  m_searchToolBar->setFullSize(true);

  // Create the erase button
  m_searchToolBar->insertButton(TQApplication::reverseLayout() ? "clear_left" :"locationbar_erase", 0, SIGNAL(clicked()), this, SLOT(slotSearchEraseClicked()), true);
  
  // Create the labels
  TQLabel *searchLabel = new TQLabel(i18n("Filter: "), m_searchToolBar);
  m_searchToolBar->insertWidget(1, 35, searchLabel);
  
  // Create the list view
  m_queue = new QueueListView(this);
  
  // Create the search field
  m_searchField = new TDEListViewSearchLine(m_searchToolBar, m_queue);
  
  // Do some more stuff
  m_searchToolBar->setItemAutoSized(1, true);
  m_searchToolBar->setStretchableWidget(m_searchField);
  m_searchToolBar->updateRects(true);
  m_searchToolBar->hide();
  
  layout->addWidget(m_searchToolBar);
  
  // Create the columns
  m_queue->addColumn(i18n("Name"), 150);
  m_queue->addColumn(i18n("Size"), 75);
  m_queue->addColumn(i18n("Source"), 250);
  m_queue->addColumn(i18n("Destination"), 250);
  m_queue->addColumn(i18n("Progress"), 140);
  m_queue->addColumn(i18n("Speed"), 70);
  m_queue->addColumn(i18n("ETA"), 80);
  
  // Text when there is nothing queued
  m_queue->setEmptyListText(i18n("You do not have any files in the queue."));

  // Multi-select
  m_queue->setSelectionModeExt(TDEListView::FileManager);
  m_queue->setAllColumnsShowFocus(true);
  m_queue->setRootIsDecorated(true);
  m_queue->TQListView::setSorting(-1);
  m_queue->TQListView::setSortColumn(-1);
  
  layout->addWidget(m_queue);

  // The signals
  connect(m_queue, SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)), this,
          SLOT(contextMenuRequested(TDEListView*, TQListViewItem*, const TQPoint&)));
  connect(m_queue, SIGNAL(selectionChanged()), this, SLOT(updateActions()));

  // Let us be up-to-date
  connect(KFTPQueue::Manager::self(), SIGNAL(transferRemoved(long)), this, SLOT(slotObjectRemoved(long)));
  connect(KFTPQueue::Manager::self(), SIGNAL(siteRemoved(long)), this, SLOT(slotObjectRemoved(long)));
  connect(KFTPQueue::Manager::self(), SIGNAL(newTransfer(KFTPQueue::Transfer*)), this, SLOT(slotTransferAdded(KFTPQueue::Transfer*)));
  connect(KFTPQueue::Manager::self(), SIGNAL(newSite(KFTPQueue::Site*)), this, SLOT(slotSiteAdded(KFTPQueue::Site*)));
  connect(KFTPQueue::Manager::self(), SIGNAL(queueUpdate()), this, SLOT(updateActions()));
  
  // Load the listview layout
  loadLayout();
  
  // Create the context menu actions
  initActions();
  initToolBar();
  updateActions();
  
  setMinimumHeight(150);
}

void QueueView::saveLayout()
{
  m_queue->saveLayout(kapp->config(), "queueViewLayout");
}

void QueueView::loadLayout()
{
  m_queue->restoreLayout(kapp->config(), "queueViewLayout");
}

void QueueView::initToolBar()
{
  // Plug all actions
  m_loadAction->plug(m_toolBar);
  m_saveAction->plug(m_toolBar);
  m_toolBar->insertSeparator();
  m_startAction->plug(m_toolBar);
  m_pauseAction->plug(m_toolBar);
  m_stopAction->plug(m_toolBar);
  m_toolBar->insertSeparator();
  m_addAction->plug(m_toolBar);
  m_removeAction->plug(m_toolBar);
  m_searchAction->plug(m_toolBar);
  m_toolBar->insertSeparator();
  m_filterAction->plug(m_toolBar);
  
  // Create speed control widgets
  m_toolBar->insertSeparator();
  
  TQSpinBox *downloadSpeed = new TQSpinBox(0, 10240, 1, m_toolBar);
  TQToolTip::add(downloadSpeed, i18n("Limit download transfer speed"));
  m_toolBar->insertWidget(1, 35, new TQLabel(i18n("Down: "), m_toolBar));
  m_toolBar->insertWidget(2, 35, downloadSpeed);
  downloadSpeed->setValue(Config::downloadSpeedLimit());
  connect(downloadSpeed, SIGNAL(valueChanged(int)), this, SLOT(slotDownloadLimitChanged(int)));
  
  m_toolBar->insertSeparator();
  
  TQSpinBox *uploadSpeed = new TQSpinBox(0, 10240, 1, m_toolBar);
  TQToolTip::add(uploadSpeed, i18n("Limit upload transfer speed"));
  m_toolBar->insertWidget(3, 35, new TQLabel(i18n("Up: "), m_toolBar));
  m_toolBar->insertWidget(4, 35, uploadSpeed);
  uploadSpeed->setValue(Config::uploadSpeedLimit());
  connect(uploadSpeed, SIGNAL(valueChanged(int)), this, SLOT(slotUploadLimitChanged(int)));
  
  // Create thread count control widget
  m_toolBar->insertSeparator();
  
  TQSpinBox *threadCount = new TQSpinBox(1, 10, 1, m_toolBar);
  TQToolTip::add(threadCount, i18n("Per-session transfer thread count"));
  m_toolBar->insertWidget(5, 35, new TQLabel(i18n("Threads: "), m_toolBar));
  m_toolBar->insertWidget(6, 35, threadCount);
  threadCount->setValue(Config::threadCount());
  connect(threadCount, SIGNAL(valueChanged(int)), this, SLOT(slotThreadCountChanged(int)));
}

void QueueView::slotDownloadLimitChanged(int value)
{
  Config::setDownloadSpeedLimit(value);
  Config::self()->emitChange();
}

void QueueView::slotUploadLimitChanged(int value)
{
  Config::setUploadSpeedLimit(value);
  Config::self()->emitChange();
}

void QueueView::slotThreadCountChanged(int value)
{
  Config::setThreadCount(value);
  Config::self()->emitChange();
}

void QueueView::initActions()
{
  m_actionCollection = new TDEActionCollection(this, this);
  
  // Create all the actions
  m_launchAction = new TDEAction(i18n("&Start Transfer"), "launch", TDEShortcut(), this, SLOT(slotLaunch()), m_actionCollection, "launch");
  m_abortAction = new TDEAction(i18n("&Abort Transfer"), TDEShortcut(), this, SLOT(slotAbort()), m_actionCollection, "abort");
  m_removeAction = new TDEAction(i18n("&Remove"), "edit-delete", TDEShortcut(TQt::Key_Delete), this, SLOT(slotRemove()), m_actionCollection, "remove");
  m_removeAllAction = new TDEAction(i18n("Remove &All"), TDEShortcut(), this, SLOT(slotRemoveAll()), m_actionCollection, "removeAll");
  m_moveUpAction = new TDEAction(i18n("Move &Up"), "go-up", TDEShortcut(), this, SLOT(slotMoveUp()), m_actionCollection, "moveUp");
  m_moveDownAction = new TDEAction(i18n("Move &Down"), "go-down", TDEShortcut("del"), this, SLOT(slotMoveDown()), m_actionCollection, "moveDown");
  m_moveTopAction = new TDEAction(i18n("Move To &Top"), "go-top", TDEShortcut(), this, SLOT(slotMoveTop()), m_actionCollection, "moveTop");
  m_moveBottomAction = new TDEAction(i18n("Move To &Bottom"), "go-bottom", TDEShortcut(), this, SLOT(slotMoveBottom()), m_actionCollection, "moveBottom");
  m_editAction = new TDEAction(i18n("&Change Transfer Info"), TDEShortcut(), this, SLOT(slotEdit()), m_actionCollection, "changeTransfer");

  // Create the toolbar actions
  m_loadAction = new TDEAction(i18n("&Load Queue From File"), "document-open", TDEShortcut(), this, SLOT(slotLoad()), m_actionCollection, "load");
  m_saveAction = new TDEAction(i18n("&Save Queue to File"), "document-save-as", TDEShortcut(), this, SLOT(slotSave()), m_actionCollection, "save");
  m_startAction = new TDEAction(i18n("S&tart"), "media-playback-start", TDEShortcut(), this, SLOT(slotStart()), m_actionCollection, "start");
  m_pauseAction = new TDEAction(i18n("&Pause"), "media-playback-pause", TDEShortcut(), this, SLOT(slotPause()), m_actionCollection, "pause");
  m_stopAction = new TDEAction(i18n("St&op"), "media-playback-stop", TDEShortcut(), this, SLOT(slotStop()), m_actionCollection, "stop");
  m_addAction = new TDEAction(i18n("&Add Transfer..."), "document-new", TDEShortcut(), this, SLOT(slotAdd()), m_actionCollection, "add");
  m_searchAction = new TDEAction(i18n("&Search && Replace..."), "edit-find", TDEShortcut(), this, SLOT(slotSearch()), m_actionCollection, "search");
  m_filterAction = new TDEToggleAction(i18n("Show &Filter"), "filter", TDEShortcut(), this, SLOT(slotFilter()), m_actionCollection, "filter");
    
  m_saveAction->setEnabled( false );
  m_startAction->setEnabled(false);
  m_pauseAction->setEnabled(false);
  m_stopAction->setEnabled(false);
  m_addAction->setEnabled(false);
  m_removeAction->setEnabled(false);
  m_searchAction->setEnabled(false);
  m_filterAction->setEnabled(true);
}

void QueueView::updateActions()
{
  m_startAction->setEnabled(!KFTPQueue::Manager::self()->isProcessing() && KFTPQueue::Manager::self()->topLevelObject()->hasChildren() && !KFTPQueue::Manager::self()->getNumRunning());
  m_stopAction->setEnabled(KFTPQueue::Manager::self()->isProcessing());
  m_removeAllAction->setEnabled(!KFTPQueue::Manager::self()->isProcessing());
  
  TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
  QueueViewItem *firstItem = static_cast<QueueViewItem*>(selection.first());
  
  m_removeAction->setEnabled((bool) firstItem);
  
  if (!firstItem || !firstItem->getObject())
    return;
    
  bool locked = firstItem->getObject()->isLocked();
  bool parentRunning = false;
  
  if (firstItem->getObject()->hasParentObject())
    parentRunning = firstItem->getObject()->parentObject()->isRunning();
  
  m_launchAction->setEnabled(!firstItem->getObject()->isRunning() && !KFTPQueue::Manager::self()->isProcessing() && !locked);
  m_abortAction->setEnabled(firstItem->getObject()->isRunning() && !KFTPQueue::Manager::self()->isProcessing());
  m_removeAction->setEnabled(!firstItem->getObject()->isRunning() && !KFTPQueue::Manager::self()->isProcessing() && !locked);
  m_editAction->setEnabled(!firstItem->getObject()->isRunning() && firstItem->getObject()->parentObject()->getType() == KFTPQueue::QueueObject::Site && !locked);
  
  // Only allow moving of multi selections if they have the same parent
  bool allowMove = true;
  for (TQListViewItem *i = selection.first(); i; i = selection.next()) {
    if (i->parent() != static_cast<TQListViewItem*>(firstItem)->parent()) {
      allowMove = false;
      break;
    }
  }
  
  m_moveUpAction->setEnabled(allowMove && KFTPQueue::Manager::self()->canBeMovedUp(firstItem->getObject()) && !locked);
  m_moveDownAction->setEnabled(allowMove && KFTPQueue::Manager::self()->canBeMovedDown(static_cast<QueueViewItem*>(selection.last())->getObject()) && !locked);
  
  m_moveTopAction->setEnabled(allowMove && KFTPQueue::Manager::self()->canBeMovedUp(firstItem->getObject()) && !locked);
  m_moveBottomAction->setEnabled(allowMove && KFTPQueue::Manager::self()->canBeMovedDown(static_cast<QueueViewItem*>(selection.last())->getObject()) && !locked);
}

void QueueView::slotSiteAdded(KFTPQueue::Site *site)
{
  // The site should be inserted top-level
  m_queuedItems.insert(site->getId(), new QueueViewItem(this, site, m_queue));
  connect(site, SIGNAL(objectUpdated()), this, SLOT(slotObjectUpdated()));
}

void QueueView::slotTransferAdded(KFTPQueue::Transfer *transfer)
{
  // This transfer should be inserted under some other transfer
  QueueViewItem *parent = m_queuedItems.find(transfer->parentObject()->getId());
  
  if (parent) {
    m_queuedItems.insert(transfer->getId(), new QueueViewItem(this, transfer, parent));
    connect(transfer, SIGNAL(objectUpdated()), this, SLOT(slotObjectUpdated()));
  }
  
  // Update actions
  m_saveAction->setEnabled(true);
  m_removeAllAction->setEnabled(true);
  m_searchAction->setEnabled(true);
}

void QueueView::slotObjectRemoved(long id)
{
  // Delete the transfer
  QueueViewItem *item = m_queuedItems.find(id);
  
  if (item)
    delete item;

  // Update actions
  bool empty = (m_queue->childCount() == 0);

  m_saveAction->setEnabled(!empty);
  if (empty) m_removeAction->setEnabled(false);
  m_removeAllAction->setEnabled(!empty);
  m_searchAction->setEnabled(!empty);
}

void QueueView::slotObjectUpdated()
{
  KFTPQueue::QueueObject *object = (KFTPQueue::QueueObject*) TQObject::sender();
  
  if (object) {
    QueueViewItem *item = m_queuedItems.find(object->getId());
    
    if (item)
      item->refresh();
  }
}

void QueueView::contextMenuRequested(TDEListView*, TQListViewItem* item, const TQPoint& p)
{
  if (!item)
    return;

  QueueViewItem *firstItem = static_cast<QueueViewItem*>(m_queue->selectedItems().first());
  TDEPopupMenu *contextMenu = new TDEPopupMenu(m_queue);
  
  // Populate context menu
  if (firstItem->getObject()->isTransfer()) {
    contextMenu->insertTitle(item->text(0) + ((m_queue->selectedItems().count() > 1) ? "..." : "" ));
    m_launchAction->plug(contextMenu);
    m_abortAction->plug(contextMenu);
    contextMenu->insertSeparator();
    m_removeAction->plug(contextMenu);
    m_removeAllAction->plug(contextMenu);
    contextMenu->insertSeparator();
    m_moveTopAction->plug(contextMenu);
    m_moveUpAction->plug(contextMenu);
    m_moveDownAction->plug(contextMenu);
    m_moveBottomAction->plug(contextMenu);
    contextMenu->insertSeparator();
    m_editAction->plug(contextMenu);
  } else if (firstItem->getObject()->getType() == KFTPQueue::QueueObject::Site) {
    contextMenu->insertTitle(i18n("Site"));
    m_launchAction->plug(contextMenu);
    m_abortAction->plug(contextMenu);
    contextMenu->insertSeparator();
    m_moveUpAction->plug(contextMenu);
    m_moveDownAction->plug(contextMenu);
  }
  
  // Update the actions
  updateActions();
  
  // Show the context menu
  contextMenu->exec(p);
}

void QueueView::slotLaunch()
{
  // Reset a possible preconfigured default action
  KFTPQueue::Manager::self()->setDefaultFileExistsAction();
  
  static_cast<QueueViewItem*>(m_queue->selectedItems().first())->getObject()->execute();
}

void QueueView::slotAbort()
{
  static_cast<QueueViewItem*>(m_queue->selectedItems().first())->getObject()->abort();
}

void QueueView::slotRemove()
{
  if (KMessageBox::questionYesNo(this, i18n("Are you sure you want to remove queued file(s)?")) == KMessageBox::Yes) {
    KFTPQueue::Manager::self()->setEmitUpdate(false);
   
    TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
    for (TQListViewItem *item = selection.first(); item; item = selection.next()) {
      if (item && static_cast<QueueViewItem*>(item)->getObject())
        KFTPQueue::Manager::self()->removeTransfer(static_cast<KFTPQueue::Transfer*>(static_cast<QueueViewItem*>(item)->getObject()));
    }
    
    KFTPQueue::Manager::self()->setEmitUpdate(true);
    KFTPQueue::Manager::self()->doEmitUpdate();
  }
}

void QueueView::slotRemoveAll()
{
  if (KMessageBox::questionYesNo(this, i18n("Are you sure you want to remove ALL queued files?")) == KMessageBox::Yes) {
    KFTPQueue::Manager::self()->clearQueue();
  }
}

void QueueView::slotMoveUp()
{
  TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
  
  for (TQListViewItem *item = selection.first(); item; item = selection.next()) {
    QueueViewItem *queueItem = static_cast<QueueViewItem*>(item);
    
    // Move the transfer
    KFTPQueue::Manager::self()->moveTransferUp(queueItem->getObject());
    queueItem->moveUp();
  }
}

void QueueView::slotMoveDown()
{
  TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
  
  for (TQListViewItem *item = selection.last(); item; item = selection.prev()) {
    QueueViewItem *queueItem = static_cast<QueueViewItem*>(item);
    
    // Move the transfer
    KFTPQueue::Manager::self()->moveTransferDown(queueItem->getObject());
    queueItem->moveDown();
  }
}

void QueueView::slotMoveTop()
{
  TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
  
  for (TQListViewItem *item = selection.last(); item; item = selection.prev()) {
    QueueViewItem *queueItem = static_cast<QueueViewItem*>(item);
    
    // Move the transfer
    KFTPQueue::Manager::self()->moveTransferTop(queueItem->getObject());
    queueItem->moveToTop();
  }
}

void QueueView::slotMoveBottom()
{
  TQPtrList<TQListViewItem> selection = m_queue->selectedItems();
  
  for (TQListViewItem *item = selection.first(); item; item = selection.next()) {
    QueueViewItem *queueItem = static_cast<QueueViewItem*>(item);
    
    // Move the transfer
    KFTPQueue::Manager::self()->moveTransferBottom(queueItem->getObject());
    queueItem->moveToBottom();
  }
}

void QueueView::slotEdit()
{
  QueueEditor *editor = new QueueEditor(this);
  
  QueueViewItem* item = static_cast<QueueViewItem*>(m_queue->selectedItems().first());
  editor->setData(static_cast<KFTPQueue::Transfer*>(item->getObject()));
  
  // Show the queue editor
  if (editor->exec() == TQDialog::Accepted) {
    editor->saveData();
    
    KFTPQueue::Manager::self()->revalidateTransfer(static_cast<KFTPQueue::Transfer*>(item->getObject()));
    KFTPQueue::Manager::self()->doEmitUpdate();
    item->refresh();
  }
}

void QueueView::slotSearch()
{
  SearchDialog *dialog = new SearchDialog();
  
  dialog->exec();
  delete dialog;
}

void QueueView::slotLoad()
{
  if (m_queue->childCount() && KMessageBox::warningContinueCancel(0L, i18n("Loading a new queue will overwrite the existing one; are you sure you want to continue?"), i18n("Load Queue")) == KMessageBox::Cancel)
    return;

  TQString loadPath = KFileDialog::getOpenFileName();
  
  if (!loadPath.isEmpty()) {
    KFTPQueue::Manager::self()->getConverter()->importQueue(loadPath);
  }
}

void QueueView::slotSave()
{
  TQString savePath = KFileDialog::getSaveFileName();
  
  if (!savePath.isEmpty()) {
    KFTPQueue::Manager::self()->getConverter()->exportQueue(savePath);
  }
}

void QueueView::slotStart()
{
  // Begin queue processing
  KFTPQueue::Manager::self()->start();
}

void QueueView::slotPause()
{
}

void QueueView::slotStop()
{
  // Abort queue processing
  KFTPQueue::Manager::self()->abort();
}

void QueueView::slotAdd()
{
}

void QueueView::slotSearchEraseClicked()
{
  m_searchField->clear();
}

void QueueView::slotFilter()
{
  if (m_filterAction->isChecked())
    m_searchToolBar->show();
  else
    m_searchToolBar->hide();
}

}

#include "queueview.moc"