diff options
Diffstat (limited to 'src/modules/torrent')
-rw-r--r-- | src/modules/torrent/Makefile.am | 30 | ||||
-rw-r--r-- | src/modules/torrent/libkvitorrent.cpp | 904 | ||||
-rw-r--r-- | src/modules/torrent/tc_interface.cpp | 31 | ||||
-rw-r--r-- | src/modules/torrent/tc_interface.h | 164 | ||||
-rw-r--r-- | src/modules/torrent/tc_ktorrentdcopinterface.cpp | 500 | ||||
-rw-r--r-- | src/modules/torrent/tc_ktorrentdcopinterface.h | 131 | ||||
-rw-r--r-- | src/modules/torrent/tc_statusbarapplet.cpp | 69 | ||||
-rw-r--r-- | src/modules/torrent/tc_statusbarapplet.h | 26 |
8 files changed, 1855 insertions, 0 deletions
diff --git a/src/modules/torrent/Makefile.am b/src/modules/torrent/Makefile.am new file mode 100644 index 00000000..ab0d80cb --- /dev/null +++ b/src/modules/torrent/Makefile.am @@ -0,0 +1,30 @@ +############################################################################### +# KVirc IRC client Makefile - 10.03.2000 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +AM_CPPFLAGS = -I$(SS_TOPSRCDIR)/src/kvilib/include/ -I$(SS_TOPSRCDIR)/src/kvirc/include/ \ +$(SS_INCDIRS) $(SS_CPPFLAGS) -DGLOBAL_KVIRC_DIR=\"$(globalkvircdir)\" + +pluglib_LTLIBRARIES = libkvitorrent.la + +#%.moc: %.h +# $(SS_QT_MOC) $< -o $@ + +libkvitorrent_la_LDFLAGS = -avoid-version -module + +libkvitorrent_la_SOURCES = libkvitorrent.cpp \ + tc_interface.cpp \ + tc_ktorrentdcopinterface.cpp \ + tc_statusbarapplet.cpp + +libkvitorrent_la_LIBADD = $(SS_LIBLINK) ../../kvilib/build/libkvilib.la + +noinst_HEADERS = tc_interface.h \ + tc_ktorrentdcopinterface.h \ + tc_statusbarapplet.h + +%.moc: %.h + $(SS_QT_MOC) $< -o $@ + +tc_ktorrentdcopinterface.cpp: tc_ktorrentdcopinterface.moc +tc_statusbarapplet.cpp: tc_statusbarapplet.moc diff --git a/src/modules/torrent/libkvitorrent.cpp b/src/modules/torrent/libkvitorrent.cpp new file mode 100644 index 00000000..9a929483 --- /dev/null +++ b/src/modules/torrent/libkvitorrent.cpp @@ -0,0 +1,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) + { + QString szOut; + QString szNam = d->name(); + KviQString::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")); \ + QString 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; \ + QString 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 \ + QString 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 \ + QString 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 \ + QString 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) +{ + QString 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_KDE_SUPPORT + g_pDescriptorList->append(new KviKTorrentDCOPInterfaceDescriptor); +#endif // COMPILE_KDE_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) +{ + debug("torrent module ctrl"); +/* if(kvi_strEqualCI(operation,"getAvailableMediaPlayers")) + { + // we expect param to be a pointer to QStringList + QStringList * l = (QStringList *)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 +) diff --git a/src/modules/torrent/tc_interface.cpp b/src/modules/torrent/tc_interface.cpp new file mode 100644 index 00000000..9474a254 --- /dev/null +++ b/src/modules/torrent/tc_interface.cpp @@ -0,0 +1,31 @@ +//============================================================================= +// +// Common interface for BitTorrent clients. +// +// File : tc_interface.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) +// Copyright (C) 2007 Alexander Stillich (torque at pltn dot org) +// +// 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 "tc_interface.h" +#include "kvi_locale.h" + +KviTorrentInterface *KviTorrentInterface::m_selected = 0; diff --git a/src/modules/torrent/tc_interface.h b/src/modules/torrent/tc_interface.h new file mode 100644 index 00000000..797ea428 --- /dev/null +++ b/src/modules/torrent/tc_interface.h @@ -0,0 +1,164 @@ +#ifndef _TC_INTERFACE_H_ +#define _TC_INTERFACE_H_ + +//============================================================================= +// +// Common interface for BitTorrent clients. +// +// File : tc_interface.h +// 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) +// Copyright (C) 2007 Alexander Stillich (torque at pltn dot org) +// +// 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_settings.h" +#include "kvi_qstring.h" +#include <qobject.h> + +class KviTorrentInterface : public QObject +{ + +public: + + KviTorrentInterface() {} + virtual ~KviTorrentInterface() {} + + virtual int detect() = 0; + + // returns number of torrents in client + virtual int count()=0; + +/* + // path of torrent file + virtual QCString getTorrentFile(int i)=0; + + // directory where data is downloaded to + virtual QCString getTorrentDataDir(int i)=0; +*/ + // number of files in torrent + virtual int fileCount(int i)=0; + // name of file in torrent + virtual QString fileName(int i, int file)=0; + // returns file priority (low, normal, high) + virtual QString filePriority(int i, int file)=0; + // sets file priority + virtual bool setFilePriority(int i, int file, const QString &prio)=0; + + virtual bool start(int i)=0; + virtual bool stop(int i)=0; + + virtual bool announce(int i)=0; + + virtual bool startAll()=0; + virtual bool stopAll()=0; +/* + // remove torrent from client + virtual bool removeTorrent(int i)=0; + + virtual bool addTorrent(const QCString &mrl); +*/ + // returns state of torrent number i (Stopped, Stalled, Seeding, Downloading) + // this uses getTorrentInfo() to obtain the state and then + // returns it as string + virtual QString state(int i)=0; + + // name of torrent as displayed in client + // uses getTorrentInfo() + virtual QString name(int i)=0; + + virtual float speedUp()=0; + virtual float speedDown()=0; + + virtual float trafficUp()=0; + virtual float trafficDown()=0; + + virtual int maxUploadSpeed()=0; + virtual int maxDownloadSpeed()=0; + + virtual bool setMaxUploadSpeed(int kbytes_per_sec)=0; + virtual bool setMaxDownloadSpeed(int kbytes_per_sec)=0; + + QString lastError() { return m_lastError; } + + static void select(KviTorrentInterface *i) { m_selected = i; } + static KviTorrentInterface *selected() { return m_selected; } + +protected: + + QString m_lastError; + + static KviTorrentInterface *m_selected; +}; + +class KviTorrentInterfaceDescriptor +{ +public: + KviTorrentInterfaceDescriptor() {}; + virtual ~KviTorrentInterfaceDescriptor() {}; +public: + virtual const QString & name() = 0; + virtual const QString & description() = 0; + virtual KviTorrentInterface * instance() = 0; +}; + +#define TORR_DECLARE_DESCRIPTOR(_interfaceclass) \ + class _interfaceclass ## Descriptor : public KviTorrentInterfaceDescriptor \ + { \ + public: \ + _interfaceclass ## Descriptor(); \ + virtual ~_interfaceclass ## Descriptor(); \ + protected: \ + _interfaceclass * m_pInstance; \ + QString m_szName; \ + QString m_szDescription; \ + public: \ + virtual const QString & name(); \ + virtual const QString & description(); \ + virtual KviTorrentInterface * instance(); \ + }; + +#define TORR_IMPLEMENT_DESCRIPTOR(_interfaceclass,_name,_description) \ + _interfaceclass ## Descriptor::_interfaceclass ## Descriptor() \ + : KviTorrentInterfaceDescriptor() \ + { \ + m_pInstance = 0; \ + m_szName = _name; \ + m_szDescription = _description; \ + } \ + _interfaceclass ## Descriptor::~_interfaceclass ## Descriptor() \ + { \ + delete m_pInstance; \ + } \ + const QString & _interfaceclass ## Descriptor::name() \ + { \ + return m_szName; \ + } \ + const QString & _interfaceclass ## Descriptor::description() \ + { \ + return m_szDescription; \ + } \ + KviTorrentInterface * _interfaceclass ## Descriptor::instance() \ + { \ + if (!m_pInstance) m_pInstance = new _interfaceclass(); \ + return m_pInstance; \ + } + +#endif // _TC_INTERFACE_H_ + diff --git a/src/modules/torrent/tc_ktorrentdcopinterface.cpp b/src/modules/torrent/tc_ktorrentdcopinterface.cpp new file mode 100644 index 00000000..8e770bfc --- /dev/null +++ b/src/modules/torrent/tc_ktorrentdcopinterface.cpp @@ -0,0 +1,500 @@ +//============================================================================= +// +// Common interface for BitTorrent clients. +// +// File : torr_ktorrentdcopinterface.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) +// Copyright (C) 2007 Alexander Stillich (torque at pltn dot org) +// +// 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. +// +//============================================================================= + +#ifdef COMPILE_KDE_SUPPORT + +#include "kvi_app.h" + + +#include "dcopclient.h" // <-- this include should be hidden in kvilib + +#include "tc_ktorrentdcopinterface.h" + + +#include "kvi_locale.h" +#include <qtimer.h> + +TORR_IMPLEMENT_DESCRIPTOR( + KviKTorrentDCOPInterface, + "ktorrentdcop", + __tr2qs_ctx( + "An interface to KDE's excellent KTorrent client.\n" \ + "Download it from http://www.ktorrent.org\n", + "torrent" + ) +) + +#define ERROR_MSG \ + QCString msg; \ + if (!findRunningApp(m_szAppId)) \ + msg = "KTorrent's isn't running!"; \ + else \ + msg = "Something's wrong here! KTorrent's DCOP interface has probably changed."; \ + m_lastError = __tr2qs_ctx(QString(msg), "torrent"); \ + debug("%s (%s:%d): %s", __PRETTY_FUNCTION__, __FILE__, __LINE__, (const char*)msg); \ + +#define ERROR_MSG_RANGE(I, SIZE) \ + KviQString::sprintf(m_lastError, __tr2qs_ctx("Index out of range: %d [0-%d]!", "torrent"), I, (SIZE>0)?(SIZE-1):0); \ + debug("%s (%s:%d): Index out of range: %d [0-%d]!", __PRETTY_FUNCTION__ , __FILE__, __LINE__, I, (SIZE>0)?(SIZE-1):0); + +#define ERROR_RET_BOOL \ + { \ + ERROR_MSG \ + return false; \ + } + +#define ERROR_RET_NUM \ + { \ + ERROR_MSG \ + return -1; \ + } + +// TODO: unused? +#define ERROR_RET_STRING \ + { \ + ERROR_MSG \ + return ""; \ + } + +#define ERROR_RET_VOID \ + { \ + ERROR_MSG \ + return; \ + } + +#define CHECK_RANGE_BOOL(I, SIZE) \ + if (I<0 || I>=SIZE) \ + { \ + ERROR_MSG_RANGE(I, SIZE) \ + return false; \ + } + +#define CHECK_RANGE_INT(I, SIZE) \ + if (I<0 || I>=SIZE) \ + { \ + ERROR_MSG_RANGE(I, SIZE) \ + return -1; \ + } + +#define CHECK_RANGE_STRING(I, SIZE) \ + if (I<0 || I>=SIZE) \ + { \ + ERROR_MSG_RANGE(I, SIZE) \ + return ""; \ + } + +KviKTorrentDCOPInterface::KviKTorrentDCOPInterface() + : KviDCOPHelper(false, "ktorrent") +{ + printf("KviKTorrentDCOPInterface\n"); + + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(slotTimer())); + timer->start(250, FALSE); + + // make sure we have a list of files, + // otherwise functions dealing would + // fail if called the first time + slotTimer(); +} + +KviKTorrentDCOPInterface::~KviKTorrentDCOPInterface() +{ + printf("~KviKTorrentDCOPInterface\n"); +} + +void KviKTorrentDCOPInterface::slotTimer() +{ + if (!findRunningApp(m_szAppId)) + return; + + m_ti.clear(); + + KviValueList<int> ret; + if (!qvalueListIntRetIntDCOPCall("KTorrent", "getTorrentNumbers(int)", ret, 0)) + ERROR_RET_VOID + + for (int i=0; i<ret.size(); i++) + { + KviQCStringList info; + if (!qcstringListRetIntDCOPCall("KTorrent", "getTorrentInfo(int)", info, ret[i])) + ERROR_RET_VOID + + if (info.size() == 0) + continue; + + TorrentInfo item; + if (!makeTorrentInfo(item, info)) + return; + + item.num = ret[i]; + m_ti.append(item); + } + + qHeapSort(m_ti); +} + +int KviKTorrentDCOPInterface::detect() +{ + if (!findRunningApp(m_szAppId)) + return 0; + + return 100; +} + +bool makeSize(float &sz, const QString &s, const QString &u) +{ + bool ok; + sz = s.toFloat(&ok); + if (!ok) + return false; + + if (u == "B") + ; + else + if (u == "KB") + sz = sz * 1024.0; + else + if (u == "MB") + sz = sz * 1024.0 * 1024.0; + else + if (u == "GB") + sz = sz * 1024.0 * 1024.0 * 1024.0; + else + return false; + + return true; +} + +bool KviKTorrentDCOPInterface::start(int i) +{ + CHECK_RANGE_BOOL(i, m_ti.size()) + + debug("starting %s [%d]", (const char*)m_ti[i].name, m_ti[i].num); + if (!voidRetIntDCOPCall("KTorrent", "start(int)", m_ti[i].num)) + ERROR_RET_BOOL + + return true; +} + +bool KviKTorrentDCOPInterface::stop(int i) +{ + CHECK_RANGE_BOOL(i, m_ti.size()) + + debug("stopping %s [%d]", (const char*)m_ti[i].name, m_ti[i].num); + if (!voidRetIntBoolDCOPCall("KTorrent", "stop(int, bool)", m_ti[i].num, true)) + ERROR_RET_BOOL + + return true; +} + +bool KviKTorrentDCOPInterface::announce(int i) +{ + CHECK_RANGE_BOOL(i, m_ti.size()) + + debug("announcing %s [%d]", (const char*)m_ti[i].name, m_ti[i].num); + if (!voidRetIntDCOPCall("KTorrent", "announce(int)", m_ti[i].num)) + ERROR_RET_BOOL + return true; +} + +QString KviKTorrentDCOPInterface::state(int i) +{ + CHECK_RANGE_STRING(i, m_ti.size()) + + return m_ti[i].state; +} + +QString KviKTorrentDCOPInterface::name(int i) +{ + CHECK_RANGE_STRING(i, m_ti.size()) + + return m_ti[i].name; +} + +int KviKTorrentDCOPInterface::fileCount(int i) +{ + CHECK_RANGE_INT(i, m_ti.size()) + + int ret; + if (!intRetIntDCOPCall("KTorrent", "getFileCount(int)", ret, m_ti[i].num)) + ERROR_RET_NUM + + return ret; +} + +QString KviKTorrentDCOPInterface::fileName(int i, int file) +{ + CHECK_RANGE_STRING(i, m_ti.size()) + + QCStringList ret; + if (!qcstringListRetIntDCOPCall("KTorrent", "getFileNames(int)", ret, m_ti[i].num)) + ERROR_RET_STRING + + CHECK_RANGE_STRING(file, ret.size()) + + return ret[file]; +} + +QString KviKTorrentDCOPInterface::filePriority(int i, int file) +{ + CHECK_RANGE_STRING(i, m_ti.size()) + + QValueList<int> ret; + if (!qvalueListIntRetIntDCOPCall("KTorrent", "getFilePriorities(int)",ret, m_ti[i].num)) + ERROR_RET_STRING + + CHECK_RANGE_STRING(file, ret.size()) + + debug("prio: %d", ret[file]); + switch (ret[file]) + { + case 1: return "low"; + case 2: return "normal"; + case 3: return "high"; + } + + ERROR_RET_STRING +} + +bool KviKTorrentDCOPInterface::setFilePriority(int i, int file, const QString &prio) +{ + CHECK_RANGE_BOOL(i, m_ti.size()) + + int prion; + if (prio == "low") + prion = 1; + else + if (prio == "normal") + prion = 2; + else + if (prio == "high") + prion = 1; + else + ERROR_RET_BOOL + + if (!voidRetIntIntIntDCOPCall("KTorrent", "setFilePriority(int,int,int)", m_ti[i].num, file, prion)) + ERROR_RET_BOOL + + return true; +} + +bool KviKTorrentDCOPInterface::startAll() +{ + if (!voidRetIntDCOPCall("KTorrent", "startAll(int)", 3)) + ERROR_RET_BOOL + + return true; +} + +bool KviKTorrentDCOPInterface::stopAll() +{ + if (!voidRetIntDCOPCall("KTorrent", "stopAll(int)", 3)) + ERROR_RET_BOOL + + return true; +} + +int KviKTorrentDCOPInterface::count() +{ + return m_ti.size(); +} + +float KviKTorrentDCOPInterface::speedUp() +{ + KviQCStringList ret; + if (!qcstringListRetVoidDCOPCall("KTorrent", "getInfo()", ret)) + ERROR_RET_NUM + + QStringList tmp = QStringList::split(" ", ret[2]); + if (tmp.size() != 8) + ERROR_RET_NUM + + bool ok; + float f = tmp[6].toFloat(&ok); + if (!ok) + ERROR_RET_NUM + + return f; +} + +float KviKTorrentDCOPInterface::speedDown() +{ + KviQCStringList ret; + if (!qcstringListRetVoidDCOPCall("KTorrent", "getInfo()", ret)) + ERROR_RET_NUM + + QStringList tmp = QStringList::split(" ", ret[2]); + if (tmp.size() != 8) + ERROR_RET_NUM + + bool ok; + float f = tmp[2].toFloat(&ok); + if (!ok) + ERROR_RET_NUM + + return f; +} + +float KviKTorrentDCOPInterface::trafficUp() +{ + KviQCStringList ret; + if (!qcstringListRetVoidDCOPCall("KTorrent", "getInfo()", ret)) + ERROR_RET_NUM + + QStringList tmp = QStringList::split(" ", ret[1]); + if (tmp.size() != 8) + ERROR_RET_NUM + + float f; + if (!makeSize(f, tmp[6], tmp[7])) + ERROR_RET_NUM + + return f; +} + +float KviKTorrentDCOPInterface::trafficDown() +{ + KviQCStringList ret; + if (!qcstringListRetVoidDCOPCall("KTorrent", "getInfo()", ret)) + ERROR_RET_NUM + + QStringList tmp = QStringList::split(" ", ret[1]); + if (tmp.size() != 8) + ERROR_RET_NUM + + float f; + if (!makeSize(f, tmp[2], tmp[3])) + ERROR_RET_NUM + + return f; +} + +bool KviKTorrentDCOPInterface::makeTorrentInfo(TorrentInfo &ti, const KviQCStringList &ret) +{ + if (ret.size() != 10) + ERROR_RET_BOOL + + ti.name = ret[0]; + if (ret[1] == "Seeding") + ti.state = "Seeding"; + else + if (ret[1] == "Stalled") + ti.state = "Stalled"; + else + if (ret[1] == "Downloading") + ti.state = "Downloading"; + else + ti.state = "Stopped"; + + + QStringList tmp; + tmp = QStringList::split(" ", ret[2]); + if (tmp.size()!=2 || !makeSize(ti.trafficDown, tmp[0], tmp[1])) + ERROR_RET_BOOL + + tmp = QStringList::split(" ", ret[3]); + if (tmp.size()!=2 || !makeSize(ti.size, tmp[0], tmp[1])) + ERROR_RET_BOOL + + tmp = QStringList::split(" ", ret[4]); + if (tmp.size()!=2 || !makeSize(ti.trafficUp, tmp[0], tmp[1])) + ERROR_RET_BOOL + + + bool ok; + tmp = QStringList::split(" ", ret[5]); + if (tmp.size()!=2) + ERROR_RET_BOOL + ti.speedDown = tmp[0].toFloat(&ok); + if (!ok) + ERROR_RET_BOOL + + tmp = QStringList::split(" ", ret[6]); + if (tmp.size()!=2) + ERROR_RET_BOOL + ti.speedUp = tmp[0].toFloat(&ok); + if (!ok) + ERROR_RET_BOOL + + + // torrent name, status, downloaded, size, uploaded, down spd, up spd, none, peers, % complete + ti.peers = ret[8].toInt(&ok); + if (!ok) + ERROR_RET_BOOL + + tmp = QStringList::split(" ", ret[9]); + if (tmp.size()!=2) + ERROR_RET_BOOL + ti.percent = tmp[0].toFloat(&ok); + if (!ok) + ERROR_RET_BOOL + + return true; +} + +int KviKTorrentDCOPInterface::maxUploadSpeed() +{ + int ret; + if (!intRetVoidDCOPCall("KTorrent", "maxUploadRate()", ret)) + { + ERROR_MSG + return -1; + } + return ret; +} + +int KviKTorrentDCOPInterface::maxDownloadSpeed() +{ + int ret; + if (!intRetVoidDCOPCall("KTorrent", "maxDownloadRate()", ret)) + { + ERROR_MSG + return -1; + } + return ret; +} + +bool KviKTorrentDCOPInterface::setMaxUploadSpeed(int kbytes_per_sec) +{ + if (!voidRetIntDCOPCall("KTorrent", "setMaxUploadSpeed(int)", kbytes_per_sec)) + ERROR_RET_BOOL + return true; +} + +bool KviKTorrentDCOPInterface::setMaxDownloadSpeed(int kbytes_per_sec) +{ + if (!voidRetIntDCOPCall("KTorrent", "setMaxDownloadSpeed(int)", kbytes_per_sec)) + ERROR_RET_BOOL + return true; +} + + +#include "tc_ktorrentdcopinterface.moc" + +#endif // COMPILE_KDE_SUPPORT + diff --git a/src/modules/torrent/tc_ktorrentdcopinterface.h b/src/modules/torrent/tc_ktorrentdcopinterface.h new file mode 100644 index 00000000..8f39810f --- /dev/null +++ b/src/modules/torrent/tc_ktorrentdcopinterface.h @@ -0,0 +1,131 @@ +#ifndef _TC_KTORRENTDCOPINTERFACE_H_ +#define _TC_KTORRENTDCOPINTERFACE_H_ + +//============================================================================= +// +// DCOP interface for KTorrent client. +// +// File : torr_ktorrentdcopinterface.h +// 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) +// Copyright (C) 2007 Alexander Stillich (torque at pltn dot org) +// +// 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 "tc_interface.h" + +#ifdef COMPILE_KDE_SUPPORT + +#include <kvi_dcophelper.h> + +// DCOP interface to KTorrent. this has 'DCOP' in its name +// because in kde4 there will be a D-BUS interface. +class KviKTorrentDCOPInterface : public KviTorrentInterface, + private KviDCOPHelper +{ + Q_OBJECT + +public: + + KviKTorrentDCOPInterface(); + virtual ~KviKTorrentDCOPInterface(); + + virtual int detect(); + + virtual int count(); + + virtual bool start(int i); + virtual bool stop(int i); + virtual bool announce(int i); + virtual QString state(int i); + virtual QString name(int i); + + virtual int fileCount(int i); + virtual QString fileName(int i, int file); + virtual QString filePriority(int i, int file); + virtual bool setFilePriority(int i, int file, const QString &prio); + + virtual bool startAll(); + virtual bool stopAll(); + + virtual int maxUploadSpeed(); + virtual int maxDownloadSpeed(); + + virtual bool setMaxUploadSpeed(int kbytes_per_sec); + virtual bool setMaxDownloadSpeed(int kbytes_per_sec); + + virtual float speedUp(); + virtual float speedDown(); + + virtual float trafficUp(); + virtual float trafficDown(); + +private slots: + + // polls client and extracts information. + // this is done because the order of torrents returned changes + // each time a torrent's state changes. + // we want to present a consistent list (in terms of indices) to + // the user, so we extract the info and sort it by name. + // otherwise the user would have a hard time figuring out what's + // going on. we could sort each time a function working with + // torrents is called, but this would be horribly slow ... + void slotTimer(); + +private: + + struct TorrentInfo + { + // internal number + int num; + // name displayed in ktorrent + QString name; + // state of torrent + QString state; + // bytes + float size; + // bytes + float trafficUp; + // bytes + float trafficDown; + // KB/s + float speedUp; + // KB/s + float speedDown; + // percent complete + float percent; + // number of peers + int peers; + + bool operator<(const TorrentInfo &ti) { return name < ti.name; } + }; + + QValueList<TorrentInfo> m_ti; + +private: + + bool makeTorrentInfo(TorrentInfo &ti, const KviQCStringList &ret); +}; + +TORR_DECLARE_DESCRIPTOR(KviKTorrentDCOPInterface) + +#endif // COMPILE_KDE_SUPPORT + +#endif // _TC_KTORRENTDCOPINTERFACE_H_ diff --git a/src/modules/torrent/tc_statusbarapplet.cpp b/src/modules/torrent/tc_statusbarapplet.cpp new file mode 100644 index 00000000..5d23a299 --- /dev/null +++ b/src/modules/torrent/tc_statusbarapplet.cpp @@ -0,0 +1,69 @@ +#include "qtimer.h" +#include "kvi_locale.h" +#include "kvi_iconmanager.h" +#include "tc_interface.h" +#include "tc_statusbarapplet.h" + +KviTorrentStatusBarApplet::KviTorrentStatusBarApplet(KviStatusBar *parent, KviStatusBarAppletDescriptor *desc) + : KviStatusBarApplet(parent, desc) +{ + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(update())); + timer->start(250, FALSE); +// updateDisplay(); +// + setText("torrent client"); +} + +KviTorrentStatusBarApplet::~KviTorrentStatusBarApplet() +{ +} + +static KviStatusBarApplet *CreateTorrentClientApplet(KviStatusBar *bar, KviStatusBarAppletDescriptor *desc) +{ + debug("CreateTorrentClientApplet"); + return new KviTorrentStatusBarApplet(bar, desc); +} + +void KviTorrentStatusBarApplet::selfRegister(KviStatusBar *bar) +{ + KviStatusBarAppletDescriptor *d = new KviStatusBarAppletDescriptor( + __tr2qs("Torrent Client"), + "torrentapplet", + CreateTorrentClientApplet, + "torrent", *(g_pIconManager->getSmallIcon(KVI_SMALLICON_AWAY))); + + bar->registerAppletDescriptor(d); +} + +QString formatSize(float sz) +{ + if (sz >= 1024.0f*1024.0f*1024.0f) + return QString("%1 GB").arg(sz / (1024.0f*1024.0f*1024.0f), 2, 'f', 2); + if (sz >= 1024.0f*1024.0f) + return QString("%1 MB").arg(sz / (1024.0f*1024.0f), 2, 'f', 2); + if (sz >= 1024.0f) + return QString("%1 KB").arg(sz / 1024.0f, 2, 'f', 2); + return QString("%1 B").arg(sz, 2, 'f', 2); +} + +void KviTorrentStatusBarApplet::update() +{ + if (KviTorrentInterface::selected()) + { + QString msg = QString("up: %1 K/s (%2), dn: %3 K/s (%4)") + .arg(KviTorrentInterface::selected()->speedUp(), 2) + .arg(formatSize(KviTorrentInterface::selected()->trafficUp())) + .arg(KviTorrentInterface::selected()->speedDown(), 2) + .arg(formatSize(KviTorrentInterface::selected()->trafficDown())); + + setText(msg); + + } else + { + setText(__tr2qs_ctx("No client selected!", "torrent")); + } +} + +#include "tc_statusbarapplet.moc" + diff --git a/src/modules/torrent/tc_statusbarapplet.h b/src/modules/torrent/tc_statusbarapplet.h new file mode 100644 index 00000000..03dd6d0d --- /dev/null +++ b/src/modules/torrent/tc_statusbarapplet.h @@ -0,0 +1,26 @@ +#ifndef _TC_STATUSBARAPPLET_H_ +#define _TC_STATUSBARAPPLET_H_ + +#include "kvi_statusbar.h" +#include "kvi_statusbarapplet.h" + +// TODO: check if it gets destroyed +// TODO: proper icon! +class KviTorrentStatusBarApplet : public KviStatusBarApplet +{ + Q_OBJECT + +public: + + KviTorrentStatusBarApplet(KviStatusBar *parent, KviStatusBarAppletDescriptor *desc); + virtual ~KviTorrentStatusBarApplet(); + + static void selfRegister(KviStatusBar *bar); + +private slots: + + void update(); + +}; + +#endif // _TC_STATUSBARAPPLET_H_ |