#!/usr/bin/env python
import findrox; findrox.version(2, 0, 0)
import rox, os, sys
from rox import Menu, g, filer

__builtins__._ = rox.i18n.translation(os.path.join(rox.app_dir, 'Messages'))

# globals
APP_NAME = 'System'
APP_SITE = 'rox.sourceforge.net'

# Options.xml processing
rox.setup_app_options(APP_NAME, site=APP_SITE)
Menu.set_save_name(APP_NAME, site=APP_SITE)

from processes import Processes
from filesystems import FileSystems
from rox.options import Option

#Options go here
SHOW_ALL = Option('show_all', False)
HIDE_SWAP = Option('hide_swap', False)
SHOW_TREE = Option('show_tree', True)
DRAW_TO_SCALE = Option('draw_to_scale', True)

#Enable notification of options changes
rox.app_options.notify()

try:
	from optparse import OptionParser

	parser = OptionParser()
	parser.add_option("--options",
		action="store_true", dest="edit_options", default=False,
		help="display options dialog")

	(options, args) = parser.parse_args()

	if options.edit_options:
		rox.edit_options()
		rox.mainloop()
		sys.exit(0)
except SystemExit: raise
except: pass

class MainWindow(rox.Dialog):
	def __init__(self):
		rox.Dialog.__init__(self)
		self.set_has_separator(False)
		self.add_button(g.STOCK_CLOSE, g.RESPONSE_CANCEL)
		self.add_button(g.STOCK_HELP, g.RESPONSE_HELP)
		self.set_default_response(g.RESPONSE_CANCEL)
		self.set_position(g.WIN_POS_CENTER)

		def response(window, response):
			if response == g.RESPONSE_HELP:
				filer.open_dir(os.path.join(rox.app_dir, 'Help'))
			else:
				window.destroy()
		self.connect('response', response)

		label = g.Label('')
		label.set_markup(_('<big>Filesystems</big>'))
		self.vbox.pack_start(label, False, True, 0)

		filesystems = FileSystems(DRAW_TO_SCALE.int_value)

		check = g.CheckButton(_('Draw bars to scale'))
		check.set_active(DRAW_TO_SCALE.int_value)
		self.vbox.pack_start(check, False, True, 0)
		check.connect('toggled', lambda b: filesystems.toggle_scaled())

		self.vbox.pack_start(filesystems, False, True, 0)

		label = g.Label('')
		label.set_markup(_('<big>Processes</big>'))
		self.vbox.pack_start(label, False, True, 0)

		# Processes
		show_all = bool(SHOW_ALL.int_value)
		hide_swap = bool(HIDE_SWAP.int_value)
		show_tree = bool(SHOW_TREE.int_value)
		
		proc = Processes(self, show_all, hide_swap, show_tree)

		hbox = g.HBox(False, 0)
		self.vbox.pack_start(hbox, False, True, 0)

		check = g.CheckButton(_('Show all processes'))
		check.set_active(show_all)
		hbox.pack_start(check, False, True, 0)
		check.connect('toggled', lambda c: proc.toggle_show_all())

		check = g.CheckButton(_('Ignore swap when scaling'))
		check.set_active(hide_swap)
		hbox.pack_start(check, False, True, 0)
		check.connect('toggled', lambda c: proc.toggle_hide_swap())

		check = g.CheckButton(_('Show process tree'))
		check.set_active(show_tree)
		hbox.pack_start(check, False, True, 0)
		check.connect('toggled', lambda c: proc.toggle_show_tree())

		self.vbox.pack_start(proc, True, True, 0)

		self.vbox.show_all()

		req = proc.get_best_size()
		proc.set_size_request(-1, req[1])

		req = self.size_request()
		max_height = g.gdk.screen_height() * 5 / 6
		self.set_default_size(-1, min(req[1] + 8, max_height))
		proc.set_size_request(-1, -1)

MainWindow().show()

rox.mainloop()
