add per-torrent speed limits
This commit is contained in:
parent
9d32724a8e
commit
0a41a421b1
|
@ -0,0 +1,181 @@
|
||||||
|
# Copyright (C) 2007 - Marcos Pinto <markybob@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, 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 St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
plugin_name = _("Speed Limiter")
|
||||||
|
plugin_author = "Marcos Pinto"
|
||||||
|
plugin_version = "0.1"
|
||||||
|
plugin_description = _("Set the desired speed limit per torrent.")
|
||||||
|
|
||||||
|
def deluge_init(deluge_path):
|
||||||
|
global path
|
||||||
|
path = deluge_path
|
||||||
|
|
||||||
|
def enable(core, interface):
|
||||||
|
global path
|
||||||
|
return DesiredSpeed(path, core, interface)
|
||||||
|
|
||||||
|
### The Plugin ###
|
||||||
|
|
||||||
|
DEFAULT_PREFS = {
|
||||||
|
"up_speeds": [5, 10, 30, 80, 300],
|
||||||
|
"down_speeds": [5, 10, 30, 80, 300]
|
||||||
|
}
|
||||||
|
|
||||||
|
import deluge
|
||||||
|
import gtk, gtk.glade
|
||||||
|
|
||||||
|
class DesiredSpeed:
|
||||||
|
|
||||||
|
def __init__(self, path, core, interface):
|
||||||
|
self.path = path
|
||||||
|
self.core = core
|
||||||
|
self.interface = interface
|
||||||
|
self.set_up_speeds = {}
|
||||||
|
self.set_down_speeds = {}
|
||||||
|
self.callback_ids = []
|
||||||
|
|
||||||
|
self.config = deluge.pref.Preferences(filename=deluge.common.CONFIG_DIR + "/desired_speed.conf", global_defaults=False, defaults=DEFAULT_PREFS)
|
||||||
|
|
||||||
|
self.callback_ids.append(self.interface.torrent_menu.connect_after("realize", self.torrent_menu_show))
|
||||||
|
self.callback_ids.append(self.interface.torrent_menu.connect("show", self.torrent_menu_show))
|
||||||
|
self.callback_ids.append(self.interface.torrent_menu.connect("hide", self.torrent_menu_hide))
|
||||||
|
|
||||||
|
def torrent_menu_show(self, widget, data=None):
|
||||||
|
self.unique_ID = self.interface.get_selected_torrent()
|
||||||
|
|
||||||
|
self.down_image = gtk.Image()
|
||||||
|
self.down_image.set_from_file(deluge.common.get_pixmap('downloading16.png'))
|
||||||
|
self.down_speed_menuitem = gtk.ImageMenuItem(_("_Torrent Download Speed"))
|
||||||
|
self.down_speed_menuitem.set_image(self.down_image)
|
||||||
|
self.down_speed_menu = self.interface.build_menu_radio_list(self.config.get("down_speeds"), self.down_speed_clicked, self.get_torrent_desired_down_speed(), suffix=_("KiB/s"), show_notset=True, notset_lessthan=0, show_other=True)
|
||||||
|
|
||||||
|
self.down_speed_menuitem.set_submenu(self.down_speed_menu)
|
||||||
|
self.interface.torrent_menu.append(self.down_speed_menuitem)
|
||||||
|
self.down_speed_menuitem.show_all()
|
||||||
|
|
||||||
|
self.up_image = gtk.Image()
|
||||||
|
self.up_image.set_from_file(deluge.common.get_pixmap('seeding16.png'))
|
||||||
|
self.up_speed_menuitem = gtk.ImageMenuItem(_("_Torrent Upload Speed"))
|
||||||
|
self.up_speed_menuitem.set_image(self.up_image)
|
||||||
|
self.up_speed_menu = self.interface.build_menu_radio_list(self.config.get("up_speeds"), self.up_speed_clicked, self.get_torrent_desired_up_speed(), suffix=_("KiB/s"), show_notset=True, notset_lessthan=0, show_other=True)
|
||||||
|
|
||||||
|
self.up_speed_menuitem.set_submenu(self.up_speed_menu)
|
||||||
|
self.interface.torrent_menu.append(self.up_speed_menuitem)
|
||||||
|
self.up_speed_menuitem.show_all()
|
||||||
|
|
||||||
|
|
||||||
|
def torrent_menu_hide(self, widget):
|
||||||
|
try:
|
||||||
|
self.interface.torrent_menu.remove(self.up_speed_menuitem)
|
||||||
|
self.interface.torrent_menu.remove(self.down_speed_menuitem)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def unload(self):
|
||||||
|
# Disconnect all callbacks
|
||||||
|
for callback_id in self.callback_ids:
|
||||||
|
self.interface.torrent_menu.disconnect(callback_id)
|
||||||
|
|
||||||
|
self.callback_ids = []
|
||||||
|
|
||||||
|
# Reset all desired speeds in the core
|
||||||
|
for unique_ID, speed in self.set_up_speeds.items():
|
||||||
|
if speed >= 0:
|
||||||
|
self.core.set_per_upload_rate_limit(unique_ID, int(-1))
|
||||||
|
self.set_up_speeds = {}
|
||||||
|
|
||||||
|
for unique_ID, speed in self.set_down_speeds.items():
|
||||||
|
if speed >= 0:
|
||||||
|
self.core.set_per_download_rate_limit(unique_ID, int(-1))
|
||||||
|
|
||||||
|
self.set_down_speeds = {}
|
||||||
|
|
||||||
|
def up_speed_clicked(self, widget):
|
||||||
|
value = widget.get_children()[0].get_text().rstrip(" "+_("KiB/s"))
|
||||||
|
if value == _("Unlimited"):
|
||||||
|
value = -1
|
||||||
|
|
||||||
|
if value == _("Other..."):
|
||||||
|
dialog_glade = gtk.glade.XML(deluge.common.get_glade_file("dgtkpopups.glade"))
|
||||||
|
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
||||||
|
spin_title = dialog_glade.get_widget("spin_title")
|
||||||
|
spin_title.set_text(_("Torrent Upload Speed (KiB/s):"))
|
||||||
|
spin_speed = dialog_glade.get_widget("spin_speed")
|
||||||
|
spin_speed.set_value(self.get_torrent_desired_up_speed())
|
||||||
|
spin_speed.select_region(0, -1)
|
||||||
|
response = speed_dialog.run()
|
||||||
|
if response == 1: # OK Response
|
||||||
|
value = spin_speed.get_value()
|
||||||
|
else:
|
||||||
|
speed_dialog.destroy()
|
||||||
|
return
|
||||||
|
speed_dialog.destroy()
|
||||||
|
|
||||||
|
value = int(value)
|
||||||
|
|
||||||
|
self.core.set_per_upload_rate_limit(self.unique_ID, value)
|
||||||
|
self.set_up_speeds[self.unique_ID] = value
|
||||||
|
|
||||||
|
# Update the speeds list if necessary
|
||||||
|
if value not in self.config.get("up_speeds") and value >= 1:
|
||||||
|
self.config.get("up_speeds").insert(0, value)
|
||||||
|
self.config.get("up_speeds").pop()
|
||||||
|
|
||||||
|
def down_speed_clicked(self, widget):
|
||||||
|
value = widget.get_children()[0].get_text().rstrip(" "+_("KiB/s"))
|
||||||
|
if value == _("Unlimited"):
|
||||||
|
value = -1
|
||||||
|
|
||||||
|
if value == _("Other..."):
|
||||||
|
dialog_glade = gtk.glade.XML(deluge.common.get_glade_file("dgtkpopdowns.glade"))
|
||||||
|
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
||||||
|
spin_title = dialog_glade.get_widget("spin_title")
|
||||||
|
spin_title.set_text(_("Torrent Download Speed (KiB/s):"))
|
||||||
|
spin_speed = dialog_glade.get_widget("spin_speed")
|
||||||
|
spin_speed.set_value(self.get_torrent_desired_down_speed())
|
||||||
|
spin_speed.select_region(0, -1)
|
||||||
|
response = speed_dialog.run()
|
||||||
|
if response == 1: # OK Response
|
||||||
|
value = spin_speed.get_value()
|
||||||
|
else:
|
||||||
|
speed_dialog.destroy()
|
||||||
|
return
|
||||||
|
speed_dialog.destroy()
|
||||||
|
|
||||||
|
value = int(value)
|
||||||
|
|
||||||
|
self.core.set_per_download_rate_limit(self.unique_ID, value)
|
||||||
|
self.set_down_speeds[self.unique_ID] = value
|
||||||
|
|
||||||
|
# update the speeds list if necessary
|
||||||
|
if value not in self.config.get("down_speeds") and value >= 0:
|
||||||
|
self.config.get("down_speeds").insert(0, value)
|
||||||
|
self.config.get("down_speeds").pop()
|
||||||
|
|
||||||
|
def get_torrent_desired_up_speed(self):
|
||||||
|
if self.set_up_speeds.has_key(self.unique_ID):
|
||||||
|
return self.set_up_speeds[self.unique_ID]
|
||||||
|
else:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def get_torrent_desired_down_speed(self):
|
||||||
|
if self.set_down_speeds.has_key(self.unique_ID):
|
||||||
|
return self.set_down_speeds[self.unique_ID]
|
||||||
|
else:
|
||||||
|
return -1
|
|
@ -927,3 +927,12 @@ class Manager:
|
||||||
def set_priv(self, unique_ID, on_off):
|
def set_priv(self, unique_ID, on_off):
|
||||||
return deluge_core.set_priv(unique_ID, on_off)
|
return deluge_core.set_priv(unique_ID, on_off)
|
||||||
|
|
||||||
|
def set_per_upload_rate_limit(self, unique_ID, speed):
|
||||||
|
if speed != -1:
|
||||||
|
speed = speed * 1024
|
||||||
|
return deluge_core.set_per_upload_rate_limit(unique_ID, speed)
|
||||||
|
|
||||||
|
def set_per_download_rate_limit(self, unique_ID, speed):
|
||||||
|
if speed != -1:
|
||||||
|
speed = speed * 1024
|
||||||
|
return deluge_core.set_per_download_rate_limit(unique_ID, speed)
|
||||||
|
|
|
@ -472,10 +472,40 @@ static PyObject *torrent_set_upload_rate_limit(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
// printf("Capping upload to %d bytes per second\r\n", (int)arg);
|
// printf("Capping upload to %d bytes per second\r\n", (int)arg);
|
||||||
M_ses->set_upload_rate_limit(arg);
|
M_ses->set_upload_rate_limit(arg);
|
||||||
|
Py_INCREF(Py_None); return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *torrent_set_per_upload_rate_limit(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
python_long unique_ID, speed;
|
||||||
|
if (!PyArg_ParseTuple(args, "ii", &unique_ID, &speed))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
long index = get_index_from_unique_ID(unique_ID);
|
||||||
|
if (PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (M_torrents->at(index).handle.is_valid())
|
||||||
|
M_torrents->at(index).handle.set_upload_limit(speed);
|
||||||
|
|
||||||
Py_INCREF(Py_None); return Py_None;
|
Py_INCREF(Py_None); return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *torrent_set_per_download_rate_limit(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
python_long unique_ID, speed;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "ii", &unique_ID, &speed))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
long index = get_index_from_unique_ID(unique_ID);
|
||||||
|
if (PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
if (M_torrents->at(index).handle.is_valid())
|
||||||
|
M_torrents->at(index).handle.set_download_limit(speed);
|
||||||
|
|
||||||
|
Py_INCREF(Py_None); return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *torrent_set_listen_on(PyObject *self, PyObject *args)
|
static PyObject *torrent_set_listen_on(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
@ -1649,6 +1679,8 @@ static PyMethodDef deluge_core_methods[] =
|
||||||
{"set_max_half_open", torrent_set_max_half_open, METH_VARARGS, "."},
|
{"set_max_half_open", torrent_set_max_half_open, METH_VARARGS, "."},
|
||||||
{"set_download_rate_limit", torrent_set_download_rate_limit, METH_VARARGS, "."},
|
{"set_download_rate_limit", torrent_set_download_rate_limit, METH_VARARGS, "."},
|
||||||
{"set_upload_rate_limit", torrent_set_upload_rate_limit, METH_VARARGS, "."},
|
{"set_upload_rate_limit", torrent_set_upload_rate_limit, METH_VARARGS, "."},
|
||||||
|
{"set_per_upload_rate_limit", torrent_set_per_upload_rate_limit, METH_VARARGS, "."},
|
||||||
|
{"set_per_download_rate_limit", torrent_set_per_download_rate_limit, METH_VARARGS, "."},
|
||||||
{"set_listen_on", torrent_set_listen_on, METH_VARARGS, "."},
|
{"set_listen_on", torrent_set_listen_on, METH_VARARGS, "."},
|
||||||
{"is_listening", torrent_is_listening, METH_VARARGS, "."},
|
{"is_listening", torrent_is_listening, METH_VARARGS, "."},
|
||||||
{"listening_port", torrent_listening_port, METH_VARARGS, "."},
|
{"listening_port", torrent_listening_port, METH_VARARGS, "."},
|
||||||
|
|
|
@ -293,7 +293,7 @@ class DelugeGTK:
|
||||||
dialog_glade = gtk.glade.XML(common.get_glade_file("dgtkpopups.glade"))
|
dialog_glade = gtk.glade.XML(common.get_glade_file("dgtkpopups.glade"))
|
||||||
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
||||||
spin_title = dialog_glade.get_widget("spin_title")
|
spin_title = dialog_glade.get_widget("spin_title")
|
||||||
spin_title.set_text(_("Speed:"))
|
spin_title.set_text(_("Download Speed (KiB/s):"))
|
||||||
spin_speed = dialog_glade.get_widget("spin_speed")
|
spin_speed = dialog_glade.get_widget("spin_speed")
|
||||||
spin_speed.set_value(self.config.get("max_download_speed"))
|
spin_speed.set_value(self.config.get("max_download_speed"))
|
||||||
spin_speed.select_region(0, -1)
|
spin_speed.select_region(0, -1)
|
||||||
|
@ -316,6 +316,8 @@ class DelugeGTK:
|
||||||
if str_bwup == _("Other..."):
|
if str_bwup == _("Other..."):
|
||||||
dialog_glade = gtk.glade.XML(common.get_glade_file("dgtkpopups.glade"))
|
dialog_glade = gtk.glade.XML(common.get_glade_file("dgtkpopups.glade"))
|
||||||
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
speed_dialog = dialog_glade.get_widget("speed_dialog")
|
||||||
|
spin_title = dialog_glade.get_widget("spin_title")
|
||||||
|
spin_title.set_text(_("Upload Speed (KiB/s):"))
|
||||||
spin_speed = dialog_glade.get_widget("spin_speed")
|
spin_speed = dialog_glade.get_widget("spin_speed")
|
||||||
spin_speed.set_value(self.config.get("max_upload_speed"))
|
spin_speed.set_value(self.config.get("max_upload_speed"))
|
||||||
spin_speed.select_region(0, -1)
|
spin_speed.select_region(0, -1)
|
||||||
|
|
Loading…
Reference in New Issue