Merge branch 'master' into master_internal
[enigma2-plugins.git] / emission / src / EmissionBandwidth.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3 from Screens.Screen import Screen
4 from Components.ConfigList import ConfigListScreen
5
6 # GUI (Summary)
7 from Screens.Setup import SetupSummary
8
9 # GUI (Components)
10 from Components.ActionMap import ActionMap
11 from Components.Sources.StaticText import StaticText
12
13 # Configuration
14 from Components.config import config, getConfigListEntry, \
15         ConfigNumber, ConfigSelection, ConfigText, ConfigYesNo, NoSave
16
17 class EmissionBandwidth(Screen, ConfigListScreen):
18         def __init__(self, session, val, isTorrent, rpc_version):
19                 Screen.__init__(self, session)
20                 self.skinName = [ "EmissionBandwith", "Setup" ]
21
22                 # Summary
23                 self.setup_title = _("Bandwidth settings")
24                 self.onChangedEntry = []
25
26                 ConfigListScreen.__init__(self, [], session = session, on_change = self.changed)
27
28                 self.isTorrent = isTorrent
29                 if isTorrent:
30                         if rpc_version < 5:
31                                 downloadLimitMode = val.downloadLimitMode
32                                 uploadLimitMode = val.uploadLimitMode
33                                 modelist = [(0, _("Global Setting")), (2, _("Unlimited")), (1, _("Limit"))]
34                         else:
35                                 downloadLimitMode = val.downloadLimited
36                                 uploadLimitMode = val.uploadLimited
37                                 modelist = [(0, _("Global Setting")), (1, _("Limit"))] # XXX: this is a pure guess...
38
39                         self.downloadLimitMode = NoSave(ConfigSelection(choices = modelist, default = downloadLimitMode))
40                         self.downloadLimit = NoSave(ConfigNumber(default = val.downloadLimit))
41                         self.uploadLimitMode = NoSave(ConfigSelection(choices = modelist, default = uploadLimitMode))
42                         self.uploadLimit = NoSave(ConfigNumber(default = val.uploadLimit))
43                         self.maxConnectedPeers = NoSave(ConfigNumber(default = val.maxConnectedPeers))
44                 else: #if not isTorrent:
45                         if rpc_version < 5:
46                                 peerLimit = val.peer_limit
47                                 port = val.port
48                                 pex_allowed = val.pex_allowed
49                         else:
50                                 peerLimit = val.peer_limit_global
51                                 port = val.peer_port
52                                 pex_allowed = val.pex_enabled
53
54                         self.downloadLimitMode = NoSave(ConfigSelection(choices = [(0, _("Unlimited")), (1, _("Limit"))], default = val.speed_limit_down_enabled))
55                         self.downloadLimit = NoSave(ConfigNumber(default = val.speed_limit_down))
56                         self.uploadLimitMode = NoSave(ConfigSelection(choices = [(0, _("Unlimited")), (1, _("Limit"))], default = val.speed_limit_up_enabled))
57                         self.uploadLimit = NoSave(ConfigNumber(default = val.speed_limit_up))
58                         self.maxConnectedPeers = NoSave(ConfigNumber(default = peerLimit))
59                         self.encryption = NoSave(ConfigSelection(choices = [('required', _("required")), ('preferred', _("preferred")), ('tolerated', _("tolerated"))], default = val.encryption))
60                         self.download_dir = NoSave(ConfigText(default = val.download_dir, fixed_size = False))
61                         self.pex_allowed = NoSave(ConfigYesNo(default = pex_allowed))
62                         self.port = NoSave(ConfigNumber(default = port))
63                         self.port_forwarding_enabled = NoSave(ConfigYesNo(default = val.port_forwarding_enabled))
64
65                 self.downloadLimitMode.addNotifier(self.updateList, initial_call = False)
66                 self.uploadLimitMode.addNotifier(self.updateList, initial_call = False)
67
68                 self.updateList()
69
70                 # Initialize widgets
71                 self["key_green"] = StaticText(_("OK"))
72                 self["key_red"] = StaticText(_("Cancel"))
73                 self.full_title = _("%s bandwidth settings") % (isTorrent and str(val.name) or "eMission")
74
75                 # Define Actions
76                 self["actions"] = ActionMap(["SetupActions"],
77                         {
78                                 "cancel": self.keyCancel,
79                                 "save": self.keySave,
80                         }
81                 )
82
83                 # Trigger change
84                 self.changed()
85
86                 self.onLayoutFinish.append(self.setCustomTitle)
87
88         def updateList(self, *args):
89                 list = [
90                         getConfigListEntry(_("Download rate"), self.downloadLimitMode)
91                 ]
92                 if self.downloadLimitMode.value == 1:
93                         list.append(getConfigListEntry(_("Limit"), self.downloadLimit))
94                 list.append(getConfigListEntry(_("Upload rate"), self.uploadLimitMode))
95                 if self.uploadLimitMode.value == 1:
96                         list.append(getConfigListEntry(_("Limit"), self.uploadLimit))
97                 list.append(getConfigListEntry(_("Maximum Connections"), self.maxConnectedPeers))
98                 if not self.isTorrent:
99                         list.extend((
100                                 getConfigListEntry(_("Encryption"), self.encryption),
101                                 getConfigListEntry(_("Download directory"), self.download_dir),
102                                 getConfigListEntry(_("Allow PEX"), self.pex_allowed),
103                                 getConfigListEntry(_("Port"), self.port),
104                                 getConfigListEntry(_("Enable Port-Forwarding"), self.port_forwarding_enabled),
105                         ))
106                 self["config"].setList(list)
107
108         def setCustomTitle(self):
109                 self.setTitle(self.full_title)
110
111         def changed(self):
112                 for x in self.onChangedEntry:
113                         x()
114
115         def getCurrentEntry(self):
116                 return self["config"].getCurrent()[0]
117
118         def getCurrentValue(self):
119                 return str(self["config"].getCurrent()[1].getText())
120
121         def createSummary(self):
122                 return SetupSummary
123
124         def keySave(self):
125                 if self.downloadLimitMode.value == 2 or self.uploadLimitMode.value == 2:
126                         self.session.open(
127                                         MessageBox,
128                                         _("Setting \"Unlimited\" is not supported via RPC.\nEither abort or choose another limit to continue."),
129                                         type = MessageBox.TYPE_ERROR
130                         )
131                 else:
132                         # NOTE: transmissionrpc will take care of translating the arguments
133                         dict = {
134                                 'speed_limit_down_enabled': self.downloadLimitMode.value,
135                                 'speed_limit_down': self.downloadLimit.value,
136                                 'speed_limit_up_enabled': self.uploadLimitMode.value,
137                                 'speed_limit_up': self.uploadLimit.value,
138                                 'peer_limit': self.maxConnectedPeers.value,
139                         }
140                         if not self.isTorrent:
141                                 dict.update({
142                                         'encryption': self.encryption.value,
143                                         'download_dir': self.download_dir.value,
144                                         'pex_allowed': self.pex_allowed.value,
145                                         'port': self.port.value,
146                                         'port_forwarding_enabled': self.port_forwarding_enabled.value,
147                                 })
148                         self.close(dict)
149