TrayiconPlugin.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import time
  2. import os
  3. import sys
  4. import atexit
  5. from Plugin import PluginManager
  6. from Config import config
  7. allow_reload = False # No source reload supported in this plugin
  8. @PluginManager.registerTo("Actions")
  9. class ActionsPlugin(object):
  10. def main(self):
  11. global notificationicon, winfolders
  12. from lib import notificationicon, winfolders
  13. import gevent.threadpool
  14. self.main = sys.modules["main"]
  15. fs_encoding = sys.getfilesystemencoding()
  16. icon = notificationicon.NotificationIcon(
  17. os.path.join(os.path.dirname(os.path.abspath(__file__).decode(fs_encoding)), 'trayicon.ico'),
  18. "ZeroNet %s" % config.version
  19. )
  20. self.icon = icon
  21. if not config.debug: # Hide console if not in debug mode
  22. notificationicon.hideConsole()
  23. self.console = False
  24. else:
  25. self.console = True
  26. @atexit.register
  27. def hideIcon():
  28. icon.die()
  29. ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1"
  30. icon.items = (
  31. (self.titleIp, False),
  32. (self.titleConnections, False),
  33. (self.titleTransfer, False),
  34. (self.titleConsole, self.toggleConsole),
  35. (self.titleAutorun, self.toggleAutorun),
  36. "--",
  37. ("ZeroNet Twitter", lambda: self.opensite("https://twitter.com/HelloZeroNet")),
  38. ("ZeroNet Reddit", lambda: self.opensite("http://www.reddit.com/r/zeronet/")),
  39. ("ZeroNet Github", lambda: self.opensite("https://github.com/HelloZeroNet/ZeroNet")),
  40. ("Report bug/request feature", lambda: self.opensite("https://github.com/HelloZeroNet/ZeroNet/issues")),
  41. "--",
  42. ("!Open ZeroNet", lambda: self.opensite("http://%s:%s/%s" % (ui_ip, config.ui_port, config.homepage) )),
  43. "--",
  44. ("Quit", self.quit),
  45. )
  46. icon.clicked = lambda: self.opensite("http://%s:%s/%s" % (ui_ip, config.ui_port, config.homepage) )
  47. gevent.threadpool.start_new_thread(icon._run, ()) # Start in real thread (not gevent compatible)
  48. super(ActionsPlugin, self).main()
  49. icon._die = True
  50. def quit(self):
  51. self.icon.die()
  52. time.sleep(0.1)
  53. sys.exit()
  54. # self.main.ui_server.stop()
  55. # self.main.file_server.stop()
  56. def opensite(self, url):
  57. import webbrowser
  58. webbrowser.open(url, new=0)
  59. def titleIp(self):
  60. title = "!IP: %s" % config.ip_external
  61. if self.main.file_server.port_opened:
  62. title += " (active)"
  63. else:
  64. title += " (passive)"
  65. return title
  66. def titleConnections(self):
  67. title = "Connections: %s" % len(self.main.file_server.connections)
  68. return title
  69. def titleTransfer(self):
  70. title = "Received: %.2f MB | Sent: %.2f MB" % (
  71. float(self.main.file_server.bytes_recv) / 1024 / 1024,
  72. float(self.main.file_server.bytes_sent) / 1024 / 1024
  73. )
  74. return title
  75. def titleConsole(self):
  76. if self.console:
  77. return "+Show console window"
  78. else:
  79. return "Show console window"
  80. def toggleConsole(self):
  81. if self.console:
  82. notificationicon.hideConsole()
  83. self.console = False
  84. else:
  85. notificationicon.showConsole()
  86. self.console = True
  87. def getAutorunPath(self):
  88. return "%s\\zeronet.cmd" % winfolders.get(winfolders.STARTUP)
  89. def formatAutorun(self):
  90. args = sys.argv[:]
  91. args.insert(0, sys.executable)
  92. if sys.platform == 'win32':
  93. args = ['"%s"' % arg for arg in args]
  94. cmd = " ".join(args)
  95. # Dont open browser on autorun
  96. cmd = cmd.replace("start.py", "zeronet.py").replace('"--open_browser"', "").replace('"default_browser"', "").strip()
  97. return "@echo off\ncd /D %s\n%s" % (os.getcwd(), cmd)
  98. def isAutorunEnabled(self):
  99. path = self.getAutorunPath()
  100. return os.path.isfile(path) and open(path).read() == self.formatAutorun()
  101. def titleAutorun(self):
  102. if self.isAutorunEnabled():
  103. return "+Start ZeroNet when Windows starts"
  104. else:
  105. return "Start ZeroNet when Windows starts"
  106. def toggleAutorun(self):
  107. if self.isAutorunEnabled():
  108. os.unlink(self.getAutorunPath())
  109. else:
  110. open(self.getAutorunPath(), "w").write(self.formatAutorun())