ZeroNet/plugins/Trayicon/TrayiconPlugin.py

144 lines
4.6 KiB
Python
Raw Normal View History

import time
import os
import sys
import atexit
from Plugin import PluginManager
from Config import config
2016-12-03 13:42:23 +01:00
from Translate import Translate
allow_reload = False # No source reload supported in this plugin
2016-12-03 13:42:23 +01:00
if "_" not in locals():
_ = Translate("plugins/Trayicon/languages/")
@PluginManager.registerTo("Actions")
class ActionsPlugin(object):
def main(self):
global notificationicon, winfolders
from lib import notificationicon, winfolders
import gevent.threadpool
self.main = sys.modules["main"]
fs_encoding = sys.getfilesystemencoding()
icon = notificationicon.NotificationIcon(
os.path.join(os.path.dirname(os.path.abspath(__file__).decode(fs_encoding)), 'trayicon.ico'),
"ZeroNet %s" % config.version
)
self.icon = icon
if not config.debug: # Hide console if not in debug mode
notificationicon.hideConsole()
self.console = False
else:
self.console = True
@atexit.register
def hideIcon():
icon.die()
ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1"
icon.items = (
(self.titleIp, False),
(self.titleConnections, False),
(self.titleTransfer, False),
(self.titleConsole, self.toggleConsole),
(self.titleAutorun, self.toggleAutorun),
"--",
2016-12-03 13:42:23 +01:00
(_["ZeroNet Twitter"], lambda: self.opensite("https://twitter.com/HelloZeroNet")),
(_["ZeroNet Reddit"], lambda: self.opensite("http://www.reddit.com/r/zeronet/")),
(_["ZeroNet Github"], lambda: self.opensite("https://github.com/HelloZeroNet/ZeroNet")),
(_["Report bug/request feature"], lambda: self.opensite("https://github.com/HelloZeroNet/ZeroNet/issues")),
"--",
2016-12-03 13:42:23 +01:00
(_["!Open ZeroNet"], lambda: self.opensite("http://%s:%s/%s" % (ui_ip, config.ui_port, config.homepage) )),
"--",
2016-12-03 13:42:23 +01:00
(_["Quit"], self.quit),
)
icon.clicked = lambda: self.opensite("http://%s:%s/%s" % (ui_ip, config.ui_port, config.homepage) )
gevent.threadpool.start_new_thread(icon._run, ()) # Start in real thread (not gevent compatible)
super(ActionsPlugin, self).main()
icon._die = True
def quit(self):
self.icon.die()
time.sleep(0.1)
sys.exit()
# self.main.ui_server.stop()
# self.main.file_server.stop()
def opensite(self, url):
import webbrowser
webbrowser.open(url, new=0)
def titleIp(self):
2016-12-03 13:42:23 +01:00
title = "!IP: %s " % config.ip_external
if self.main.file_server.port_opened:
2016-12-03 13:42:23 +01:00
title += _["(active)"]
else:
2016-12-03 13:42:23 +01:00
title += _["(passive)"]
return title
def titleConnections(self):
2016-12-03 13:42:23 +01:00
title = _["Connections: %s"] % len(self.main.file_server.connections)
return title
def titleTransfer(self):
2016-12-03 13:42:23 +01:00
title = _["Received: %.2f MB | Sent: %.2f MB"] % (
float(self.main.file_server.bytes_recv) / 1024 / 1024,
float(self.main.file_server.bytes_sent) / 1024 / 1024
)
return title
def titleConsole(self):
2016-12-03 13:42:23 +01:00
translate = _["Show console window"]
if self.console:
2016-12-03 13:42:23 +01:00
return "+" + translate
else:
2016-12-03 13:42:23 +01:00
return translate
def toggleConsole(self):
if self.console:
notificationicon.hideConsole()
self.console = False
else:
notificationicon.showConsole()
self.console = True
def getAutorunPath(self):
return "%s\\zeronet.cmd" % winfolders.get(winfolders.STARTUP)
def formatAutorun(self):
args = sys.argv[:]
args.insert(0, sys.executable)
if sys.platform == 'win32':
args = ['"%s"' % arg for arg in args]
cmd = " ".join(args)
# Dont open browser on autorun
cmd = cmd.replace("start.py", "zeronet.py").replace('"--open_browser"', "").replace('"default_browser"', "").strip()
2015-10-26 01:03:37 +01:00
return "@echo off\ncd /D %s\n%s" % (os.getcwd(), cmd)
def isAutorunEnabled(self):
path = self.getAutorunPath()
return os.path.isfile(path) and open(path).read() == self.formatAutorun()
def titleAutorun(self):
2016-12-03 13:42:23 +01:00
translate = _["Start ZeroNet when Windows starts"]
if self.isAutorunEnabled():
2016-12-03 13:42:23 +01:00
return "+" + translate
else:
2016-12-03 13:42:23 +01:00
return translate
def toggleAutorun(self):
if self.isAutorunEnabled():
os.unlink(self.getAutorunPath())
else:
open(self.getAutorunPath(), "w").write(self.formatAutorun())