menu_screen.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import os
  2. import socket
  3. from base_screen import BaseScreen
  4. from ..graphic_utils import ListView
  5. class MenuScreen(BaseScreen):
  6. def __init__(self, size, base_size, manager, fonts):
  7. BaseScreen.__init__(self, size, base_size, manager, fonts)
  8. self.ip = None
  9. self.list = ListView((0, 0), size, base_size, fonts['base'])
  10. self.list_items = ["Exit Mopidy", "Shutdown", "Restart", "IP: "]
  11. self.list.set_list(self.list_items)
  12. def should_update(self):
  13. return self.list.should_update()
  14. def update(self, screen, update_type, rects):
  15. update_all = (update_type == BaseScreen.update_all)
  16. self.list.render(screen, update_all, rects)
  17. def touch_event(self, event):
  18. clicked = self.list.touch_event(event)
  19. if clicked is not None:
  20. if clicked == 0:
  21. os.system("pkill mopidy")
  22. elif clicked == 1:
  23. if os.system("gksu -- shutdown now -h") != 0:
  24. os.system("sudo shutdown now -h")
  25. elif clicked == 2:
  26. if os.system("gksu -- shutdown -r now") != 0:
  27. os.system("sudo shutdown -r now")
  28. elif clicked == 3:
  29. self.check_connection()
  30. # Will check internet connection
  31. def check_connection(self):
  32. try:
  33. self.manager.set_connection(False, True)
  34. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  35. s.connect(("8.8.8.8", 80))
  36. self.ip = s.getsockname()[0]
  37. s.close()
  38. self.list_items[3] = "IP: " + self.ip
  39. self.list.set_list(self.list_items)
  40. self.manager.set_connection(True, False)
  41. except socket.error:
  42. s.close()
  43. self.ip = None
  44. self.list_items[2] = "IP: No internet"
  45. self.list.set_list(self.list_items)
  46. self.manager.set_connection(False, False)