summaryrefslogtreecommitdiffstats
path: root/src/modules/torrent/libkvitorrent.cpp
blob: 2d4ab30ecf549dbeb3bd0876ca969ad85ec2a21d (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
//=============================================================================
//
//   File : libkvitorrent.cpp
//   Creation date : Fri Jan 1 15:42:25 2007 GMT by Alexander Stillich
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001-2005 Szymon Stefanek (pragma at kvirc dot 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 opinion) 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 "kvi_module.h"
#include "kvi_options.h"
#include "kvi_frame.h"

#include "tc_interface.h"
#include "tc_ktorrentdcopinterface.h"
#include "tc_statusbarapplet.h"

#include "kvi_locale.h"
#include "kvi_out.h"


static KviPointerList<KviTorrentInterfaceDescriptor> * g_pDescriptorList = 0;

static KviTorrentInterface *auto_detect_torrent_client(KviWindow * pOut = 0)
{
	int iBest = 0;
	KviTorrentInterface * pBest = 0;
	KviTorrentInterfaceDescriptor * d;
	KviTorrentInterfaceDescriptor * pDBest = 0;

	for (d=g_pDescriptorList->first(); d!=0; d=g_pDescriptorList->next())
	{
		// instance gets deleted by descriptor later
		KviTorrentInterface *i = d->instance();
		if (i)
		{
			int iScore = i->detect();
			if(iScore > iBest)
			{
				iBest = iScore;
				pBest = i;
				pDBest = d;
			}

			if(pOut)
			{
				pOut->output(KVI_OUT_TORRENT,
						   __tr2qs_ctx("Trying torrent client interface \"%Q\": score %d", "torrent"),
						   &d->name(),
						   iScore);
			}
		}
	}

/*	if(iBest < 90)
	{
		if(pOut)
			pOut->outputNoFmt(KVI_OUT_MULTIMEDIA,__tr2qs_ctx("Not sure about the results, trying a second, more agressive detection pass","mediaplayer"));
		// no sure player found... try again with a destructive test
		for(d = g_pDescriptorList->first();d;d = g_pDescriptorList->next())
		{
			KviMediaPlayerInterface * i = d->instance();
			if(i)
			{
				int iScore = i->detect(true);
				if(iScore > iBest)
				{
					iBest = iScore;
					pBest = i;
					pDBest = d;
				}
				if(pOut)
				{
					TQString szOut;
					TQString szNam = d->name();
					KviTQString::sprintf(szOut,__tr2qs_ctx("Trying media player interface \"%Q\": score %d","mediaplayer"),&(szNam),iScore);
					pOut->output(KVI_OUT_MULTIMEDIA,szOut);
				}
			}
		}
	}
*/
	if(pDBest)
	{
		KVI_OPTION_STRING(KviOption_stringPreferredTorrentClient) = pDBest->name();
		if(pOut)
			pOut->output(KVI_OUT_TORRENT,
					__tr2qs_ctx("Choosing torrent client interface \"%Q\"", "torrent"),
					&pDBest->name());

	} else
	{
		if(pOut)
			pOut->outputNoFmt(KVI_OUT_TORRENT,
					__tr2qs_ctx("Seems that there is no usable torrent client on this machine", "torrent"));
	}

	return pBest;
}

#define TC_KVS_FAIL_ON_NO_INTERFACE \
	if (!KviTorrentInterface::selected()) \
	{ \
		c->warning(__tr2qs_ctx("No torrent client interface selected. Try /torrent.detect", "torrent")); \
		return true; \
	}

#define TC_KVS_COMMAND(__name) static bool torrent_kvs_cmd_ ## __name (KviKvsModuleCommandCall * c)
#define TC_KVS_FUNCTION(__name) static bool torrent_kvs_fnc_ ## __name (KviKvsModuleFunctionCall * c)

#define TC_KVS_COMMAND_ERROR	\
			if (!c->hasSwitch('q',"quiet")) \
			{ \
				c->warning(__tr2qs_ctx("The selected torrent client interface failed to execute the requested function", "torrent")); \
				TQString tmp = __tr2qs_ctx("Last interface error: ", "torrent"); \
				tmp += KviTorrentInterface::selected()->lastError(); \
				c->warning(tmp); \
			} \

#define TC_KVS_SIMPLE_COMMAND(__name, __ifacecommand) \
	TC_KVS_COMMAND(__name) \
	{ \
		KVSM_PARAMETERS_BEGIN(c) \
		KVSM_PARAMETERS_END(c) \
		\
		TC_KVS_FAIL_ON_NO_INTERFACE \
		\
		if (!KviTorrentInterface::selected()->__ifacecommand()) \
		{ \
			TC_KVS_COMMAND_ERROR \
		} \
		return true; \
	}

#define TC_KVS_INT_COMMAND(__name, __ifacecommand, __argname) \
	TC_KVS_COMMAND(__name) \
	{ \
		kvs_int_t arg; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname, KVS_PT_INT, 0, arg) \
		KVSM_PARAMETERS_END(c) \
		\
		TC_KVS_FAIL_ON_NO_INTERFACE \
		\
		if (!KviTorrentInterface::selected()->__ifacecommand(arg)) \
		{ \
			TC_KVS_COMMAND_ERROR \
		} \
		return true; \
	}

#define TC_KVS_INT_INT_STRING_COMMAND(__name, __ifacecommand, __argname1, __argname2, __argname3) \
	TC_KVS_COMMAND(__name) \
	{ \
		kvs_int_t arg1; \
		kvs_int_t arg2; \
		TQString arg3; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname1, KVS_PT_INT, 0, arg1) \
			KVSM_PARAMETER(__argname2, KVS_PT_INT, 0, arg2) \
			KVSM_PARAMETER(__argname3, KVS_PT_STRING, 0, arg3) \
		KVSM_PARAMETERS_END(c) \
		\
		TC_KVS_FAIL_ON_NO_INTERFACE \
		\
		if (!KviTorrentInterface::selected()->__ifacecommand(arg1, arg2, arg3)) \
		{ \
			TC_KVS_COMMAND_ERROR \
		} \
		return true; \
	}

// TODO: error handling for functions

#define TC_KVS_STRINGRET_FUNCTION(__name, __ifacecommand) \
	TC_KVS_FUNCTION(__name) \
	{ \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		TQString szRet = KviTorrentInterface::selected()->__ifacecommand(); \
		c->returnValue()->setString(szRet); \
		return true; \
	}

#define TC_KVS_INTRET_FUNCTION(__name, __ifacecommand) \
	TC_KVS_FUNCTION(__name) \
	{ \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		kvs_int_t ret = KviTorrentInterface::selected()->__ifacecommand(); \
		c->returnValue()->setInteger(ret); \
		return true; \
	}

#define TC_KVS_FLOATRET_FUNCTION(__name, __ifacecommand) \
	TC_KVS_FUNCTION(__name) \
	{ \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		kvs_real_t ret = KviTorrentInterface::selected()->__ifacecommand(); \
		c->returnValue()->setReal(ret); \
		return true; \
	}

#define TC_KVS_INTRET_INT_FUNCTION(__name, __ifacecommand, __argname) \
	TC_KVS_FUNCTION(__name) \
	{ \
		kvs_int_t arg; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname, KVS_PT_INT, 0, arg) \
		KVSM_PARAMETERS_END(c) \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		kvs_int_t ret = KviTorrentInterface::selected()->__ifacecommand(arg); \
		c->returnValue()->setInteger(ret); \
		return true; \
	}

#define TC_KVS_STRINGRET_INT_FUNCTION(__name, __ifacecommand, __argname) \
	TC_KVS_FUNCTION(__name) \
	{ \
		kvs_int_t arg; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname, KVS_PT_INT, 0, arg) \
		KVSM_PARAMETERS_END(c) \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		TQString szRet = KviTorrentInterface::selected()->__ifacecommand(arg); \
		c->returnValue()->setString(szRet); \
		return true; \
	}

#define TC_KVS_STRINGRET_INT_INT_FUNCTION(__name, __ifacecommand, __argname1, __argname2) \
	TC_KVS_FUNCTION(__name) \
	{ \
		kvs_int_t arg1; \
		kvs_int_t arg2; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname1, KVS_PT_INT, 0, arg1) \
			KVSM_PARAMETER(__argname2, KVS_PT_INT, 0, arg2) \
		KVSM_PARAMETERS_END(c) \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		TQString szRet = KviTorrentInterface::selected()->__ifacecommand(arg1, arg2); \
		c->returnValue()->setString(szRet); \
		return true; \
	}

#define TC_KVS_INTRET_INT_INT_FUNCTION(__name, __ifacecommand, __argname1, __argname2) \
	TC_KVS_FUNCTION(__name) \
	{ \
		kvs_int_t arg1; \
		kvs_int_t arg2; \
		KVSM_PARAMETERS_BEGIN(c) \
			KVSM_PARAMETER(__argname1, KVS_PT_INT, 0, arg1) \
			KVSM_PARAMETER(__argname2, KVS_PT_INT, 0, arg2) \
		KVSM_PARAMETERS_END(c) \
		TC_KVS_FAIL_ON_NO_INTERFACE \
		kvs_int_t ret = KviTorrentInterface::selected()->__ifacecommand(arg1, arg2); \
		c->returnValue()->setInteger(ret); \
		return true; \
	}

TC_KVS_COMMAND(detect)
{
	KviTorrentInterface::select(auto_detect_torrent_client(c->hasSwitch('q',"quiet") ? 0 : c->window()));
	return true;
}

/*
	@doc: torrent.maxUploadSpeed
	@type:
		function
	@title:
		$torrent.maxUploadSpeed
	@short:
		Returns maximum upload speed set in client.
	@syntax:
		$torrent.maxUploadSpeed()
	@description:
		Returns maximum upload speed set in client.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INTRET_FUNCTION(maxUploadSpeed, maxUploadSpeed)


/*
	@doc: torrent.maxDownloadSpeed
	@type:
		function
	@title:
		$torrent.maxDownloadSpeed
	@short:
		Returns maximum download speed set in client.
	@syntax:
		$torrent.maxDownloadSpeed()
	@description:
		Returns maximum download speed set in client.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INTRET_FUNCTION(maxDownloadSpeed, maxDownloadSpeed)

/*
	@doc: torrent.setMaxUploadSpeed
	@type:
		command
	@title:
		torrent.setMaxUploadSpeed
	@short:
		Sets maximum upload speed
	@syntax:
		torrent.setMaxUploadSpeed <kbytes_per_sec>
	@description:
		Sets maximum upload speed
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INT_COMMAND(setMaxUploadSpeed, setMaxUploadSpeed, "kbytes_per_sec")

/*
	@doc: torrent.setMaxDownloadSpeed
	@type:
		command
	@title:
		torrent.setMaxDownloadSpeed
	@short:
		Sets maximum download speed
	@syntax:
		torrent.setMaxDownloadSpeed <kbytes_per_sec>
	@description:
		Sets maximum download speed
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INT_COMMAND(setMaxDownloadSpeed, setMaxDownloadSpeed, "kbytes_per_sec")

/*
	@doc: torrent.speedUp
	@type:
		function
	@title:
		$torrent.speedUp
	@short:
		Returns current total upload speed
	@syntax:
		$torrent.speedUp()
	@description:
		Returns current total upload speed
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_FLOATRET_FUNCTION(speedUp, speedUp)

/*
	@doc: torrent.speedDown
	@type:
		function
	@title:
		$torrent.speedDown
	@short:
		Returns current total download speed
	@syntax:
		$torrent.speedDown()
	@description:
		Returns current total download speed
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_FLOATRET_FUNCTION(speedDown, speedDown)

/*
	@doc: torrent.trafficUp
	@type:
		function
	@title:
		$torrent.trafficUp
	@short:
		Returns the total number of bytes uploaded
	@syntax:
		$torrent.trafficUp()
	@description:
		Returns the total number of bytes uploaded
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_FLOATRET_FUNCTION(trafficUp, trafficUp)

/*
	@doc: torrent.trafficDown
	@type:
		function
	@title:
		$torrent.trafficDown
	@short:
		Returns the total number of bytes download
	@syntax:
		$torrent.trafficDown()
	@description:
		Returns the total number of bytes download
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_FLOATRET_FUNCTION(trafficDown, trafficDown)

/*
	@doc: torrent.count
	@type:
		function
	@title:
		$torrent.count
	@short:
		Returns number of torrents in client
	@syntax:
		$torrent.name()
	@description:
		Returns number of torrents in client
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INTRET_FUNCTION(count, count)

/*
	@doc: torrent.name
	@type:
		function
	@title:
		$torrent.name
	@short:
		Returns torrent name as displayed in client
	@syntax:
		$torrent.name(torrent_number)
	@description:
		Returns torrent name as displayed in client
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_STRINGRET_INT_FUNCTION(name, name, "torrent_number")

/*
	@doc: torrent.start
	@type:
		command
	@title:
		torrent.start
	@short:
		Starts downloading of torrent <torrent_number>
	@syntax:
		torrent.start <torrent_number>
	@description:
		Starts downloading of torrent <torrent_number>
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.stop[/cmd], [cmd]torrent.stopAll[/cmd],
		[cmd]torrent.startAll[/cmd]
*/
TC_KVS_INT_COMMAND(start, start, "torrent_number")

/*
	@doc: torrent.stop
	@type:
		command
	@title:
		torrent.stop
	@short:
		Stops downloading of torrent <torrent_number>
	@syntax:
		torrent.stop <torrent_number>
	@description:
		Stops downloading of torrent <torrent_number>
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.stopAll[/cmd], [cmd]torrent.startAll[/cmd],
		[cmd]torrent.start[/cmd]
*/
TC_KVS_INT_COMMAND(stop, stop, "torrent_number")

/*
	@doc: torrent.announce
	@type:
		command
	@title:
		torrent.announce
	@short:
		Manually announces torrent to tracker
	@syntax:
		torrent.announce <torrent_number>
	@description:
		Manually announces torrent to tracker
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INT_COMMAND(announce, announce, "torrent_number")

/*
	@doc: torrent.fileCount
	@type:
		function
	@title:
		$torrent.fileCount
	@short:
		Returns the number of files in a torrent.
	@syntax:
		$torrent.fileCount <torrent_number>
	@description:
		Returns the number of files in a torrent.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INTRET_INT_FUNCTION(fileCount, fileCount, "torrent_number")

/*
	@doc: torrent.fileName
	@type:
		function
	@title:
		$torrent.fileName
	@short:
		Returns the name of a file in a torrent.
	@syntax:
		$torrent.fileName <torrent_number> <file_number>
	@description:
		Returns the name of a file in a torrent.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_STRINGRET_INT_INT_FUNCTION(fileName, fileName, "torrent_number", "file_number")

/*
	@doc: torrent.filePriority
	@type:
		function
	@title:
		$torrent.filePriority
	@short:
		Returns the priority of a file in a torrent.
	@syntax:
		$torrent.filePriority <torrent_number> <file_number>
	@description:
		Returns the priority of a file in a torrent.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_STRINGRET_INT_INT_FUNCTION(filePriority, filePriority, "torrent_number", "file_number")

/*
	@doc: torrent.setFilePriority
	@type:
		command
	@title:
		torrent.setFilePriority
	@short:
		Sets the priority of a file in a torrent.
	@syntax:
		torrent.setFilePriority <torrent_number> <file_number>
	@description:
		Sets the priority of a file in a torrent.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_INT_INT_STRING_COMMAND(setFilePriority, setFilePriority, "torrent_number", "file_number", "priority")

/*
	@doc: torrent.startAll
	@type:
		command
	@title:
		torrent.startAll
	@short:
		Starts downloading of all torrents
	@syntax:
		torrent.startAll
	@description:
		Starts downloading of all torrents
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.stopAll[/cmd], [cmd]torrent.start[/cmd],
		[cmd]torrent.stop[/cmd]
*/

TC_KVS_SIMPLE_COMMAND(startAll, startAll)

/*
	@doc: torrent.stopAll
	@type:
		command
	@title:
		torrent.stopAll
	@short:
		Stops downloading of all torrents
	@syntax:
		torrent.stopAll
	@description:
		Stops downloading of all torrents
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.startAll[/cmd], [cmd]torrent.start[/cmd],
		[cmd]torrent.stop[/cmd]
*/

TC_KVS_SIMPLE_COMMAND(stopAll, stopAll)


/*
	@doc: torrent.list
	@type:
		function
	@title:
		torrent.list
	@short:
		Returns a list of all torrents
	@syntax:
		$torrent.list()
	@description:
		Returns a list of all torrents
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.startAll[/cmd], [cmd]torrent.start[/cmd],
		[cmd]torrent.stop[/cmd]
*/

//MP_KVS_SIMPLE_COMMAND(list,list)

/*
	@doc: torrent.setClient
	@type:
		command
	@title:
		torrent.setClient
	@short:
		Sets the torrent client interface
	@syntax:
		torrent.setClient <client_name>
	@description:
		Sets the torrent client interface to be used by the
		torrent interface module. <interface_name> must be one
		of the client names returned by [cmd]torrent.clientList[/cmd]
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.detect[/cmd], [fnc]$torrent.client[/fnc]
*/

TC_KVS_COMMAND(setClient)
{
	TQString client;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("client", KVS_PT_STRING, 0, client)
	KVSM_PARAMETERS_END(c)

	for (KviTorrentInterfaceDescriptor *d = g_pDescriptorList->first(); d; d=g_pDescriptorList->next())
	{
		if (d->name() == client)
		{
			KviTorrentInterface::select(d->instance());
			KVI_OPTION_STRING(KviOption_stringPreferredTorrentClient) = client;

			if (!c->hasSwitch('q',"quiet"))
				c->window()->output(KVI_OUT_TORRENT,
							__tr2qs_ctx("Using client interface \"%Q\".", "torrent"),
							&client);
			return true;
		}
	}

	if (!c->hasSwitch('q',"quiet"))
		c->window()->output(KVI_OUT_TORRENT,
					__tr2qs_ctx("Invalid client interface \"%Q\"!", "torrent"),
					&client);

	return false;
}


/*
	@doc: torrent.client
	@type:
		function
	@title:
		$torrent.client
	@short:
		Returns the currently set torrent client interface
	@syntax:
		$torrent.client()
	@description:
		Returns the currently set torrent client interface.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.detect[/cmd], [cmd]torrent.setClient[/cmd]
*/
TC_KVS_FUNCTION(client)
{
	c->returnValue()->setString(KVI_OPTION_STRING(KviOption_stringPreferredTorrentClient));
	return true;
}

/*
	@doc: torrent.clientList
	@type:
		function
	@title:
		$torrent.clientList
	@short:
		Returns a list of all supported clients.
	@syntax:
		$torrent.clientList()
	@description:
		Returns a list of all supported clients.
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
		[cmd]torrent.detect[/cmd], [cmd]torrent.setClient[/cmd],
		[cmd]torrent.client[/cmd]
*/
TC_KVS_FUNCTION(clientList)
{
	KviKvsArray *pArray = new KviKvsArray();
	int id=0;

	for (KviTorrentInterfaceDescriptor *d=g_pDescriptorList->first(); d; d=g_pDescriptorList->next())
		pArray->set(id++, new KviKvsVariant(d->name()));

	c->returnValue()->setArray(pArray);
	return true;
}

/*
	@doc: torrent.state
	@type:
		function
	@title:
		$torrent.state
	@short:
		Returns state of torrent (Stopped, Stalled, Seeding, Downloading)
	@syntax:
		$torrent.state <torrent_number>
	@description:
		Returns state of torrent (Stopped, Stalled, Seeding, Downloading)
		Take a look at the [module:torrent]torrent client documentation[/module]
		for more details about how it works.[br]
	@seealso:
		[module:torrent]torrent client documentation[/module],
*/
TC_KVS_STRINGRET_INT_FUNCTION(state, state, "torrent_number")

static bool torrent_module_init(KviModule *m)
{
	#define TC_KVS_REGCMD(__name,__stringname) KVSM_REGISTER_SIMPLE_COMMAND(m, __stringname, torrent_kvs_cmd_ ## __name)
	#define TC_KVS_REGFNC(__name,__stringname) KVSM_REGISTER_FUNCTION(m, __stringname, torrent_kvs_fnc_ ## __name)

	TC_KVS_REGCMD(detect, "detect");
	TC_KVS_REGCMD(setClient, "setClient");
	TC_KVS_REGCMD(start, "start")
	TC_KVS_REGCMD(stop, "stop")
	TC_KVS_REGCMD(announce, "announce")
	TC_KVS_REGCMD(startAll, "startAll")
	TC_KVS_REGCMD(stopAll, "stopAll")
	TC_KVS_REGCMD(setMaxUploadSpeed, "setMaxUploadSpeed")
	TC_KVS_REGCMD(setMaxDownloadSpeed, "setMaxDownloadSpeed")
	TC_KVS_REGCMD(setFilePriority, "setFilePriority")
	TC_KVS_REGFNC(client, "client")
	TC_KVS_REGFNC(clientList, "clientList")
	TC_KVS_REGFNC(maxUploadSpeed, "maxUploadSpeed")
	TC_KVS_REGFNC(maxDownloadSpeed, "maxDownloadSpeed")
	TC_KVS_REGFNC(speedUp, "speedUp")
	TC_KVS_REGFNC(speedDown, "speedDown")
	TC_KVS_REGFNC(trafficUp, "trafficUp")
	TC_KVS_REGFNC(trafficDown, "trafficDown")
	TC_KVS_REGFNC(count, "count")
	TC_KVS_REGFNC(name, "name")
	TC_KVS_REGFNC(state, "state")
	TC_KVS_REGFNC(fileCount, "fileCount")
	TC_KVS_REGFNC(fileName, "fileName")
	TC_KVS_REGFNC(filePriority, "filePriority")

	g_pDescriptorList = new KviPointerList<KviTorrentInterfaceDescriptor>;
	g_pDescriptorList->setAutoDelete(true);

#ifdef COMPILE_TDE_SUPPORT
	g_pDescriptorList->append(new KviKTorrentDCOPInterfaceDescriptor);
#endif // COMPILE_TDE_SUPPORT

	KviTorrentInterface::select(0);

	if (g_pFrame->mainStatusBar())
		KviTorrentStatusBarApplet::selfRegister(g_pFrame->mainStatusBar());
	

	if(KVI_OPTION_STRING(KviOption_stringPreferredMediaPlayer) == "auto")
	{
		KviTorrentInterface::select(auto_detect_torrent_client());

	} else
	{
		for (KviTorrentInterfaceDescriptor *d=g_pDescriptorList->first(); d; d=g_pDescriptorList->next())
		{
			if (d->name() == KVI_OPTION_STRING(KviOption_stringPreferredTorrentClient))
				KviTorrentInterface::select(d->instance());
		}
	}

	return true;
}

static bool torrent_module_cleanup( KviModule * m )
{
	delete g_pDescriptorList;
	return true;
}

static bool torrent_module_can_unload( KviModule * m )
{
	return true;
}

static bool torrent_module_ctrl(KviModule * m,const char * operation,void * param)
{
	tqDebug("torrent module ctrl");
/*	if(kvi_strEqualCI(operation,"getAvailableMediaPlayers"))
	{
		// we expect param to be a pointer to TQStringList
		TQStringList * l = (TQStringList *)param;
		for(KviMediaPlayerInterfaceDescriptor * d = g_pDescriptorList->first();d;d = g_pDescriptorList->next())
		{
			l->append(d->name());
		}
		return true;
	}
	if(kvi_strEqualCI(operation,"detectMediaPlayer"))
	{
		auto_detect_player(0);
		return true;
	}
*/
	return false;
}



KVIRC_MODULE(
	"torrent",
	"1.0.0",
	"Copyright (C) 2007 Alexander Stillich (torque at pltn dot org)",
	"Interface to various torrent clients",
	torrent_module_init,
	torrent_module_can_unload,
	torrent_module_ctrl,
	torrent_module_cleanup
)