summaryrefslogtreecommitdiffstats
path: root/apps/ktorrent/ktorrentview.cpp
blob: e445fbb3eef7eedcac0c7e326732c26ab5b01e62 (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
/***************************************************************************
 *   Copyright (C) 2005,2006 by                                            *
 *   Joris Guisson <joris.guisson@gmail.com>                               *
 *   Ivan Vasic <ivasic@gmail.com>                                         *
 *                                                                         *
 *   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 WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  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 Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include <klocale.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <krun.h>
#include <kurl.h>
#include <kurldrag.h>
#include <kmessagebox.h>
#include <kstdguiitem.h>
#include <kfiledialog.h>

#include <interfaces/torrentinterface.h>
#include <torrent/globals.h>
#include <util/log.h>
#include <util/functions.h>
		
#include <groups/group.h>
#include <groups/torrentdrag.h>

#include <tqcursor.h>
#include <tqheader.h>
#include <tqvaluelist.h>
#include <tqlayout.h>
		
#include "ktorrentview.h"
#include "ktorrentviewitem.h"
#include "settings.h"
#include "scandialog.h"
#include "addpeerwidget.h"
#include "ktorrentviewmenu.h"
#include "speedlimitsdlg.h"
#include "filterbar.h"

using namespace bt;
using namespace kt;


TorrentView::TorrentView(KTorrentView* parent) : KListView(parent),ktview(parent)
{}
	
TorrentView::~TorrentView() 
{}
	
bool TorrentView::eventFilter(TQObject* watched, TQEvent* e)
{
	if((TQHeader*)watched == header())
	{
		switch(e->type())
		{
			case TQEvent::MouseButtonPress:
			{
				if(TQT_TQMOUSEEVENT(e)->button() == Qt::RightButton)
					ktview->m_headerMenu->popup(TQCursor::pos());

				break;
			}
			default:
				break;
		}
	}

	return KListView::eventFilter(watched, e);
}

KTorrentView::KTorrentView(TQWidget *parent)
	: TQWidget(parent),menu(0),current_group(0),running(0),total(0),view(0),filter_bar(0)
{
	TQVBoxLayout* tqlayout = new TQVBoxLayout(this);
	tqlayout->setAutoAdd(true);
	view = new TorrentView(this);
	filter_bar = new FilterBar(this);
	filter_bar->setHidden(true);
	
	setupColumns();
	
	connect(view,TQT_SIGNAL(executed(TQListViewItem* )),
			this,TQT_SLOT(onExecuted(TQListViewItem* )));
	
	connect(view,TQT_SIGNAL(currentChanged(TQListViewItem* )),
			this,TQT_SLOT(onExecuted(TQListViewItem* )));
	
	connect(view,TQT_SIGNAL(contextMenu(KListView*, TQListViewItem*, const TQPoint& )),
			this,TQT_SLOT(showContextMenu(KListView*, TQListViewItem*, const TQPoint& )));
	
	connect(view,TQT_SIGNAL(selectionChanged()),this,TQT_SLOT(onSelectionChanged()));

	menu = new KTorrentViewMenu(this);
	connect(menu,TQT_SIGNAL(groupItemActivated(const TQString&)),this,TQT_SLOT(gsmItemActived(const TQString&)));
	
	connect(m_headerMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(onColumnVisibilityChange( int )));

	view->setFrameShape(TQFrame::NoFrame);
}

KTorrentView::~KTorrentView()
{
}

void KTorrentView::insertColumn(TQString label, TQt::AlignmentFlags align)
{
	m_headerMenu->insertItem(label);
		
	int ind = view->addColumn(label);
	view->setColumnAlignment(ind, align);
}

void KTorrentView::setupColumns()
{
		//Header menu
	m_headerMenu = new KPopupMenu(view);
	m_headerMenu->setCheckable(true);
	m_headerMenu->insertTitle(i18n("Visible columns"));
	
	insertColumn(i18n("File"), TQt::AlignLeft);
	insertColumn(i18n("Status"), TQt::AlignLeft);
	insertColumn(i18n("Downloaded"), TQt::AlignRight);
	insertColumn(i18n("Size"), TQt::AlignRight); 
	insertColumn(i18n("Uploaded"), TQt::AlignRight);
	insertColumn(i18n("Down Speed"), TQt::AlignRight);
	insertColumn(i18n("Up Speed"), TQt::AlignRight);
	insertColumn(i18n("Time Left"), TQt::AlignCenter);
	insertColumn(i18n("Seeders"), TQt::AlignRight);
	insertColumn(i18n("Leechers"), TQt::AlignRight);
	insertColumn(i18n("% Complete"), TQt::AlignRight);
	insertColumn(i18n("Share Ratio"), TQt::AlignRight);
	insertColumn(i18n("Time Downloaded"), TQt::AlignRight);	
	insertColumn(i18n("Time Seeded"), TQt::AlignRight);
	
	view->setAllColumnsShowFocus(true);
	view->setShowSortIndicator(true);
	view->setAcceptDrops(true);
	view->setSelectionMode(TQListView::Extended);
	for (Uint32 i = 0;i < (Uint32)view->columns();i++)
	{
		view->setColumnWidth(i, 100);
		view->setColumnWidthMode(i,TQListView::Manual);
	}
}

void KTorrentView::setCurrentGroup(Group* group)
{
	if (current_group == group)
		return;
	
	current_group = group;
	
	running = 0; 
	total = 0;
	// go over the current items, if they still match keep them, else remove them
	// add new itesm if necessary
	TQMap<TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		KTorrentViewItem* tvi = i.data();
		TorrentInterface* tc = i.key();
		if (current_group && !current_group->isMember(tc))
		{
			if (tvi)
			{
				delete tvi;
				i.data() = 0;
			}
		}
		else if (!tvi)
		{
			tvi = new KTorrentViewItem(this,tc);
			i.data() = tvi;
		}
		
		if (i.data())
		{
			total++;
			if (tc->getStats().running)
				running++;
		}

		i++;
	}
	
	if (current_group)
		setCaption(TQString("%1 %2/%3").tqarg(current_group->groupName()).tqarg(running).tqarg(total));
	else
		setCaption(i18n("All Torrents %1/%2").tqarg(running).tqarg(total));
	
	onSelectionChanged();
	onExecuted(view->currentItem());
}

void KTorrentView::saveSettings(KConfig* cfg,int idx)
{
	TQString group = TQString("KTorrentView-%1").tqarg(idx);
	view->saveLayout(cfg,group);
	cfg->setGroup(group);
	filter_bar->saveSettings(cfg);
}


void KTorrentView::loadSettings(KConfig* cfg,int idx)
{
	TQString group = TQString("KTorrentView-%1").tqarg(idx);
	view->restoreLayout(cfg,group);
	view->setDragEnabled(true);

	for(int i=0; i < view->columns();++i)
	{
		bool visible = columnVisible(i);
		
		m_headerMenu->setItemChecked(m_headerMenu->idAt(i+1), visible);
		view->header()->setResizeEnabled(visible, i);
	}
	cfg->setGroup(group);
	filter_bar->loadSettings(cfg);
}


int KTorrentView::getNumRunning()
{
	int num = 0;
	TQMap<TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		KTorrentViewItem* tvi = i.data();
		if (tvi)
		{
			TorrentInterface* tc = tvi->getTC();
			num += tc->getStats().running ? 1 : 0;
		}
		i++;
	}
	return num;
}

bool KTorrentView::startDownload(kt::TorrentInterface* tc)
{
	if (tc && !tc->getStats().running)
	{
		wantToStart(tc);
		if (!tc->getStats().running && !tc->getStats().stopped_by_error)
		{
			if (tc->getStats().completed)
			{
				if (!tc->overMaxRatio() && !tc->overMaxSeedTime())
					return false;
			}
			else
				return false;
		}
	}
	return true;
}

void KTorrentView::stopDownload(kt::TorrentInterface* tc)
{
	if (tc && tc->getStats().running)
	{
		wantToStop(tc,true);
	}
}

void KTorrentView::showStartError()
{
	TQString err = i18n("Cannot start more than 1 download, ",
					   "Cannot start more than %n downloads, ",Settings::maxDownloads());
		
	err += i18n("and 1 seed. ","and %n seeds. ",Settings::maxSeeds());
	err += i18n("Go to Settings -> Configure KTorrent, if you want to change the limits.");
	KMessageBox::error(this,err);
}

void KTorrentView::startDownloads()
{
	bool err = false;
	
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (!startDownload(tc))
			err = true;;
	}

	/*
	if (err)
	{
		showStartError();
	}
	*/
	// make sure toolbuttons get updated
	onSelectionChanged();
}
	
void KTorrentView::stopDownloads()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		stopDownload(kvi->getTC());
	}
	
	// make sure toolbuttons get updated
	onSelectionChanged();
}

void KTorrentView::startAllDownloads()
{
	bool err = false;
	TQMap<TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		KTorrentViewItem* tvi = i.data();
		if (tvi && !startDownload(tvi->getTC()))
			err = true;
			
		i++;
	}
	/*
	if (err)
	{
		showStartError();
	}
	*/
	onSelectionChanged();
}

void KTorrentView::stopAllDownloads()
{
	TQMap<TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		KTorrentViewItem* tvi = i.data();
		if (tvi)
			stopDownload(tvi->getTC());
	
		i++;
	}
	onSelectionChanged();
}
	
void KTorrentView::removeDownloads()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy = false;
		if (tc && !tc->isCheckingData(dummy))
		{	
			const TorrentStats & s = tc->getStats();
			bool data_to = false;
			if (!s.completed)
			{
				TQString msg = i18n("The torrent %1 has not finished downloading, "
						"do you want to delete the incomplete data, too?").tqarg(s.torrent_name);
				int ret = KMessageBox::questionYesNoCancel(
						this,msg,i18n("Remove Download"),
						i18n("Delete Data"),i18n("Keep Data"));
				if (ret == KMessageBox::Cancel)
					return;
				else if (ret == KMessageBox::Yes)
					data_to = true;
			}
			wantToRemove(tc,data_to);
		}
	}
	
	// make sure toolbuttons get updated
	onSelectionChanged();
}

void KTorrentView::removeDownloadsAndData()
{
	TQString msg = i18n("You will lose all the downloaded data. Are you sure you want to do this?");
        // TODO: replace i18n("Remove") by KStdGuiItem::remove() in KDE4
	if (KMessageBox::warningYesNo(this,msg, i18n("Remove Torrent"), i18n("&Remove"), 
            KStdGuiItem::cancel()) == KMessageBox::No)
		return;
	
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc)
			wantToRemove(tc,true);
	}
	
	// make sure toolbuttons get updated
	onSelectionChanged();
}

void KTorrentView::manualAnnounce()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc && tc->getStats().running)
			tc->updateTracker();
	}
}

void KTorrentView::previewFiles() 
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc && tc->readyForPreview() && !tc->getStats().multi_file_torrent)
		{
			TQFileInfo fi(tc->getTorDir()+"cache");
			new KRun(KURL::fromPathOrURL(fi.readLink()), 0, true, true);
		}
	}
}

TorrentInterface* KTorrentView::getCurrentTC()
{
	KTorrentViewItem* tvi = dynamic_cast<KTorrentViewItem*>(view->currentItem());
	if (tvi)
		return tvi->getTC();
	else
		return 0;
}

void KTorrentView::onExecuted(TQListViewItem* item)
{
	KTorrentViewItem* tvi = dynamic_cast<KTorrentViewItem*>(item);
	if (tvi)
	{
		torrentClicked(tvi->getTC());
		currentChanged(tvi->getTC());
	}
	else
	{
		currentChanged(0);
	}
}

void KTorrentView::showContextMenu(KListView* ,TQListViewItem*,const TQPoint & p)
{
	updateGroupsSubMenu(menu->getGroupsSubMenu());
	menu->show(p);
}

void KTorrentView::addTorrent(TorrentInterface* tc)
{
	if (current_group && !current_group->isMember(tc))
	{
		items.insert(tc,0);
	}
	else
	{
		KTorrentViewItem* tvi = new KTorrentViewItem(this,tc);
		items.insert(tc,tvi);
		tvi->update();
		if (items.count() == 1)
			currentChanged(tc);
	}
}

void KTorrentView::removeTorrent(TorrentInterface* tc)
{
	TQMap<kt::TorrentInterface*,KTorrentViewItem*>::iterator i = items.find(tc);
	if (i != items.end())
	{
		KTorrentViewItem* tvi = i.data();
		items.erase(i);
		delete tvi;
		tvi = dynamic_cast<KTorrentViewItem*>(view->currentItem());
		if (tvi)
			currentChanged(tvi->getTC());
		else
			currentChanged(0);
	}
}


void KTorrentView::update()
{	
	Uint32 r = 0;
	Uint32 t = 0;
	
	TQMap<kt::TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		bool count = true;
		KTorrentViewItem* tvi = i.data();
		if (tvi)
			tvi->update();
		// check if the torrent still is part of the group
		kt::TorrentInterface* ti = i.key();
		
		bool member = (current_group->isMember(ti) && filter_bar->matchesFilter(ti));
		
		if (tvi && current_group && !member)
		{
			// torrent is no longer a member of this group so remove it from the view
			delete tvi;
			i.data() = 0;
			count = false;
		}
		else if (!tvi && (!current_group || member))
		{
			tvi = new KTorrentViewItem(this,ti);
			i.data() = tvi;
		}
		
		if (i.data())
		{
			t++;
			if (ti->getStats().running)
				r++;
		}
		
		i++;
	}
	
	if (running != r || total != t)
	{
		running = r;
		total = t;
		
		if (current_group)
			setCaption(TQString("%1 %2/%3").tqarg(current_group->groupName()).tqarg(running).tqarg(total));
		else
			setCaption(i18n("All Torrents %1/%2").tqarg(running).tqarg(total));
		onSelectionChanged();
	}
	
	view->sort();
}

bool KTorrentView::acceptDrag(TQDropEvent* event) const
{
	// accept uri drops only
	return KURLDrag::canDecode(event);
}

void KTorrentView::onSelectionChanged()
{
	int flags = 0;
	
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy;
		if (tc && !tc->isCheckingData(dummy))
		{
			const TorrentStats & s = tc->getStats();
			if (!s.running)
				flags |= START;
			else
				flags |= STOP;
			
			if (flags & (START|STOP))
				break;
		}
	}
	
	if (sel.count() > 0)
		flags |= REMOVE;
	
	if (sel.count() == 1)
		flags |= SCAN;
	
	if (running > 0)
		flags |= STOP_ALL;
	
	if (running == 0 && total > 0)
		flags |= START_ALL;
	
	updateActions(flags);
}

void KTorrentView::queueSlot()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy;
		if (tc && !tc->isCheckingData(dummy))
			emit queue(tc);
	}
}


void KTorrentView::checkDataIntegrity()
{	
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	if (sel.count() == 0)
		return;
	
	bool dummy = false;
	KTorrentViewItem* kvi = (KTorrentViewItem*)sel.first();
	TorrentInterface* tc = kvi->getTC();
	if (!tc->isCheckingData(dummy))
	{
		needsDataCheck(tc);
	}
	else
	{
		KMessageBox::error(0,i18n("You are already checking the data of the torrent %1 !").tqarg(tc->getStats().torrent_name));
	}
}

TQDragObject* KTorrentView::dragObject()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	if (sel.count() == 0)
		return 0;
	 
	return new TorrentDrag(this);
}

void KTorrentView::getSelection(TQValueList<kt::TorrentInterface*> & sel)
{
	TQPtrList<TQListViewItem> s = view->selectedItems();
	if (s.count() == 0)
		return;
	
	TQPtrList<TQListViewItem>::iterator i = s.begin();
	while (i != s.end())
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*i;
		TorrentInterface* tc = kvi->getTC();
		sel.append(tc);
		i++;
	}
}

void KTorrentView::removeFromGroup()
{
	TQPtrList<TQListViewItem> s = view->selectedItems();
	if (s.count() == 0 || !current_group || current_group->isStandardGroup())
		return;
	
	TQPtrList<TQListViewItem>::iterator i = s.begin();
	while (i != s.end())
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*i;
		TorrentInterface* tc = kvi->getTC();
		current_group->removeTorrent(tc);
		delete kvi;
		items[tc] = 0;
		i++;
	}
}

void KTorrentView::addSelectionToGroup(kt::Group* g)
{
	TQPtrList<TQListViewItem> s = view->selectedItems();
	if (s.count() == 0 || !g)
		return;
	
	TQPtrList<TQListViewItem>::iterator i = s.begin();
	while (i != s.end())
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*i;
		TorrentInterface* tc = kvi->getTC();
		g->addTorrent(tc);
		i++;
	}
}

void KTorrentView::showAddPeersWidget()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy;
		if (tc && !tc->isCheckingData(dummy))
		{
			AddPeerWidget dlg(tc, this);
			dlg.exec();
		}
	}
}

void KTorrentView::openOutputDirectory()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc)
		{
			if(tc->getStats().multi_file_torrent)
				new KRun(KURL::fromPathOrURL(tc->getStats().output_path), 0, true, true);
			else
				new KRun(KURL::fromPathOrURL(tc->getDataDir()), 0, true, true);
		}
	}
}

void KTorrentView::openTorXDirectory()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc)
		{
			new KRun(KURL::fromPathOrURL(tc->getTorDir()), 0, true, true);
		}
	}
}

void KTorrentView::setDownloadLocationSlot()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		if (tc)
		{
			TQString dn;
			dn = KFileDialog::getExistingDirectory(tc->getStats().output_path, this, i18n("Choose download location for %1").tqarg(tc->getStats().torrent_name));
								
			if(dn.isNull() || dn.isEmpty())
				continue;
								
			if(!dn.endsWith(bt::DirSeparator()))
				dn += bt::DirSeparator();
			
			if(!dn.isEmpty())
				tc->changeOutputDir(dn);
		}
	}
}

void KTorrentView::dhtSlot()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy;
		if (tc && !tc->isCheckingData(dummy))
		{
			bool on = tc->isFeatureEnabled(kt::DHT_FEATURE);
			tc->setFeatureEnabled(kt::DHT_FEATURE,!on);
		}
	}
}

void KTorrentView::utPexSlot()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	for (TQPtrList<TQListViewItem>::iterator itr = sel.begin(); itr != sel.end();itr++)
	{
		KTorrentViewItem* kvi = (KTorrentViewItem*)*itr;
		TorrentInterface* tc = kvi->getTC();
		bool dummy;
		if (tc && !tc->isCheckingData(dummy))
		{
			bool on = tc->isFeatureEnabled(kt::UT_PEX_FEATURE);
			tc->setFeatureEnabled(kt::UT_PEX_FEATURE,!on);
		}
	}
}

void KTorrentView::speedLimits()
{
	TQPtrList<TQListViewItem> sel = view->selectedItems();
	if (sel.count() != 1)
		return;
	
	KTorrentViewItem* kvi = (KTorrentViewItem*)sel.first();
	TorrentInterface* tc = kvi->getTC();
	SpeedLimitsDlg dlg(tc,0);
	dlg.show();
	dlg.exec();
}


void KTorrentView::columnHide(int index)
{
	view->hideColumn(index);
	view->header()->setResizeEnabled(FALSE, index);
}

void KTorrentView::columnShow(int index)
{
	view->setColumnWidth(index, 100);
	view->header()->setResizeEnabled(TRUE, index);
}

bool KTorrentView::columnVisible(int index)
{
	if (index < view->columns() && index >= 0) 
		return view->columnWidth(index) != 0;
	else
		return true;
}

void KTorrentView::onColumnVisibilityChange(int id)
{
	int mid = m_headerMenu->indexOf(id) - 1;
	if(mid == -1)
		return;

	bool visible = !columnVisible(mid);
	
	m_headerMenu->setItemChecked(id, visible);
	
	if(visible)
		columnShow(mid);
	else
		columnHide(mid);
}

void KTorrentView::gsmItemActived(const TQString & group)
{
	groupsSubMenuItemActivated(this,group);
}

void KTorrentView::updateCaption()
{
	Uint32 r = 0;
	Uint32 t = 0;
	TQMap<kt::TorrentInterface*,KTorrentViewItem*>::iterator i = items.begin();
	while (i != items.end())
	{
		if (!current_group || current_group->isMember(i.key()))
		{
			t++;
			if (i.key()->getStats().running)
				r++;
		}
		
		i++;
	}
	
	if (running != r || total != t)
	{
		running = r;
		total = t;
		
		if (current_group)
			setCaption(TQString("%1 %2/%3").tqarg(current_group->groupName()).tqarg(running).tqarg(total));
		else
			setCaption(i18n("All Torrents %1/%2").tqarg(running).tqarg(total));
	}
}

void KTorrentView::setupViewColumns()
{
	if (current_group->groupFlags() == kt::Group::UPLOADS_ONLY_GROUP)
	{
		// we are an uploads group so lets hide, downloaded, percentage, download speed and time left
		columnHide(2); // Downloaded
		columnHide(5); // Down speed
		columnHide(7); // Time left
		columnHide(10); // Percentage
	}
	else if (current_group->groupFlags() == kt::Group::DOWNLOADS_ONLY_GROUP)
	{
		columnHide(13); // Seed time is not necessary for a download
	}
	
	// make sure menu is OK
	for(int i=0; i< view->columns();++i)
	{
		bool visible = columnVisible(i);
		m_headerMenu->setItemChecked(m_headerMenu->idAt(i+1), visible);
		view->header()->setResizeEnabled(visible, i);
	}
}



void KTorrentView::toggleFilterBar()
{
	filter_bar->setHidden(!filter_bar->isHidden());
}

#include "ktorrentview.moc"