StatusLight.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. # Written by Matt Chisholm
  11. from BTL.platform import bttime
  12. from BTL.translation import _
  13. class StatusLight(object):
  14. initial_state = 'stopped'
  15. states = {
  16. # state : (stock icon name, label, tool tip),
  17. 'stopped' : ('stopped',
  18. _("Paused"),
  19. _("Paused")),
  20. 'empty' : ('stopped',
  21. _("No torrents"),
  22. _("No torrents")),
  23. 'starting' : ('starting',
  24. _("Checking for firewall..."),#_("Starting up..."),
  25. _("Starting download")),
  26. 'pre-natted': ('pre-natted',
  27. _("Checking for firewall..."),
  28. _("Online, checking for firewall")),
  29. 'running' : ('running',
  30. _("Online, ports open"),
  31. _("Online, running normally")),
  32. 'natted' : ('natted',
  33. _("Online, maybe firewalled"),
  34. _("Online, but downloads may be slow due to firewall/NAT")),
  35. 'broken' : ('broken',
  36. _("No network connection"),
  37. _("Check network connection")),
  38. }
  39. messages = {
  40. # message : default new state,
  41. 'stop' : 'stopped' ,
  42. 'empty' : 'empty' ,
  43. 'start' : 'starting' ,
  44. 'seen_peers' : 'pre-natted',
  45. 'seen_remote_peers' : 'running' ,
  46. 'broken' : 'broken' ,
  47. }
  48. transitions = {
  49. # state : { message : custom new state, },
  50. 'pre-natted' : { 'start' : 'pre-natted',
  51. 'seen_peers' : 'pre-natted',},
  52. 'running' : { 'start' : 'running' ,
  53. 'seen_peers' : 'running' ,},
  54. 'natted' : { 'start' : 'natted' ,
  55. 'seen_peers' : 'natted' ,},
  56. 'broken' : { 'start' : 'broken' ,},
  57. #TODO: add broken transitions
  58. }
  59. time_to_nat = 60 * 5 # 5 minutes
  60. def __init__(self):
  61. self.mystate = self.initial_state
  62. self.start_time = None
  63. def send_message(self, message):
  64. if message not in self.messages.keys():
  65. #print 'bad message', message
  66. return
  67. new_state = self.messages[message]
  68. if self.transitions.has_key(self.mystate):
  69. if self.transitions[self.mystate].has_key(message):
  70. new_state = self.transitions[self.mystate][message]
  71. # special pre-natted timeout logic
  72. if new_state == 'pre-natted':
  73. if (self.mystate == 'pre-natted' and
  74. bttime() - self.start_time > self.time_to_nat):
  75. # go to natted state after a while
  76. new_state = 'natted'
  77. elif self.mystate != 'pre-natted':
  78. # start pre-natted timer
  79. self.start_time = bttime()
  80. if new_state != self.mystate:
  81. #print 'changing state from', self.mystate, 'to', new_state
  82. self.mystate = new_state
  83. self.change_state()
  84. def change_state(self):
  85. pass
  86. def get_tip(self):
  87. return self.states[self.mystate][2]
  88. def get_label(self):
  89. return self.states[self.mystate][1]