# -*- python -*-
# This file is part of Fusion-icon.

# Fusion-icon is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fusion-icon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Based on compiz-icon, Copyright 2007 Felix Bellanger <keeguon@gmail.com>
#
# Original author: crdlb
# GI port: kozec
# AppIndicator port: XRevan86
# author: raveit65

import os, time, gi
try:
    gi.require_version('Gdk', '2.0')
    gi.require_version('Gtk', '2.0')
    gi.require_version('AppIndicator', '0.1')
except ValueError as e:
    # enable interface fallbacking (works only for ImportError)
    raise ImportError(e)
from gi.repository import Gdk, Gtk
from gi.repository import AppIndicator as AppIndicator
from FusionIcon.start import wms, apps, options, decorators, init

class TrayMenu(Gtk.Menu):

	def __init__(self):
		Gtk.Menu.__init__(self)

		#CCSM
		if 'ccsm' in apps:
			item = ApplicationItem('ccsm')
			item.set_image(Gtk.Image.new_from_icon_name('preferences-system', Gtk.IconSize.MENU))
			self.append(item)

		#Emerald Theme Manager
		if 'emerald theme manager' in apps:
			item = ApplicationItem('emerald theme manager')
			if Gtk.IconTheme.get_default().lookup_icon('emerald-theme-manager-icon', 0, Gtk.IconLookupFlags.USE_BUILTIN) is not None:
				item.set_image(Gtk.Image.new_from_icon_name('emerald-theme-manager-icon', Gtk.IconSize.MENU))
			else:
				item.set_image(Gtk.Image.new_from_icon_name('emerald-theme-manager', Gtk.IconSize.MENU))
			self.append(item)

		if 'ccsm' in apps or 'emerald theme manager' in apps:
			item = Gtk.SeparatorMenuItem()
			self.append(item)

		#Reload
		item = Gtk.ImageMenuItem(label='Reload Window Manager')
		item.connect('activate', self.reload_activate)
		item.set_image(Gtk.Image.new_from_icon_name('view-refresh', Gtk.IconSize.MENU))
		if not wms:
			item.set_sensitive(False)
		self.append(item)

		#Window Manager
		item = Gtk.ImageMenuItem(label='Select Window Manager')
		item.set_image(Gtk.Image.new_from_icon_name('view-restore', Gtk.IconSize.MENU))
		submenu = WindowManagerMenu()
		item.set_submenu(submenu)
		if not wms:
			item.set_sensitive(False)
		self.append(item)

		#Compiz Options
		item = Gtk.ImageMenuItem(label='Compiz Options')
		item.set_image(Gtk.Image.new_from_icon_name('document-properties', Gtk.IconSize.MENU))
		submenu = CompizOptionMenu()
		item.set_submenu(submenu)
		if not options:
			item.set_sensitive(False)
		else:
			self.append(item)

		#Window Decorator
		item = Gtk.ImageMenuItem(label='Select Window Decorator')
		item.set_image(Gtk.Image.new_from_icon_name('window-new', Gtk.IconSize.MENU))
		submenu = CompizDecoratorMenu()
		item.set_submenu(submenu)
		if not decorators:
			item.set_sensitive(False)
		self.append(item)

		item = Gtk.SeparatorMenuItem()
		self.append(item)

		item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT)
		item.connect('activate', self.quit_activate)
		self.append(item)

		self.action_item = Gtk.MenuItem()
		self.action_item.connect('activate', self.show_menu_activate)
		self.action_item.show()
		# Fool libappindicator into thinking this is menu's child.
		self.action_item.set_parent(self)

	def show_menu_activate(self, widget):
		self.show_all()
		try:
			self.popup(None, None, None, None, 1, Gdk.CURRENT_TIME)
		except AttributeError:
			pass

	def reload_activate(self, widget):
		wms.restart()

	def quit_activate(self, widget):
		Gtk.main_quit()

class ApplicationItem(Gtk.ImageMenuItem):

	def __init__(self, app):
		Gtk.ImageMenuItem.__init__(self, label=apps[app].label)

		self.app = app
		if app not in apps:
			self.set_sensitive(False)
		self.connect('activate', self.activate)

	def activate(self, widget):
		apps[self.app].launch()

class WindowManagerItem(Gtk.RadioMenuItem):

	def __init__(self, wm, group):
		Gtk.RadioMenuItem.__init__(self, label=' %s' %wms[wm].label, group=group)

		self.wm = wm
		if wms.active == wm:
			self.set_active(True)
		self.connect('activate', self.activate)

	def activate(self, widget):
		if widget.get_active():
			wms.active = self.wm
			wms.start()

class WindowManagerMenu(Gtk.Menu):

	def __init__(self):
		Gtk.Menu.__init__(self)

		prev = None
		for wm in wms.ordered_list:
			item = WindowManagerItem(wm, prev)
			self.append(item)
			prev = item

class CompizOptionItem(Gtk.CheckMenuItem):

	def __init__(self, option):
		Gtk.CheckMenuItem.__init__(self, label=' %s' %options[option].label)

		self.option = option
		self.set_active(options[option].enabled)
		if not options[option].sensitive:
			self.set_sensitive(False)
		self.connect('activate', self.activate)

	def activate(self, widget):
		options[self.option].enabled = widget.get_active()
		if wms.active == 'compiz':
			wms.start()

class CompizOptionMenu(Gtk.Menu):

	def __init__(self):
		Gtk.Menu.__init__(self)

		for option in options:
			item = CompizOptionItem(option)
			self.append(item)

class CompizDecoratorItem(Gtk.RadioMenuItem):

	def __init__(self, decorator, group):
		Gtk.RadioMenuItem.__init__(self, label=' %s' %decorators[decorator].label, group=group)

		self.decorator = decorator
		if decorators.active == decorator:
			self.set_active(True)
		self.connect('activate', self.activate)

	def activate(self, widget):
		if widget.get_active():
			decorators.active = self.decorator

class CompizDecoratorMenu(Gtk.Menu):

	def __init__(self):
		Gtk.Menu.__init__(self)

		prev = None
		for decorator in decorators:
			item = CompizDecoratorItem(decorator, prev)
			item.set_name(str(decorator))
			self.append(item)
			prev = item

icon = AppIndicator.Indicator.new('Fusion Icon', 'fusion-icon', AppIndicator.IndicatorCategory.APPLICATION_STATUS)
icon.set_status(AppIndicator.IndicatorStatus.ACTIVE)
menu = TrayMenu()
icon.set_menu(menu)
icon.set_secondary_activate_target(menu.action_item)
menu.show_all()

# active wm (possibly) starts here
init()
Gtk.main()
