# -*- 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/>.

# Author(s): xsacha, XRevan86, raveit65

import sys, os, time
from PyQt4 import QtCore, QtGui
if tuple(map(int, (QtCore.QT_VERSION_STR.split('.')))) < (4, 6, 0):
	raise ImportError('Qt 4.6.x or later required')
try:
	from PyQt4 import QtWidgets
except ImportError:
	from PyQt4 import QtGui as QtWidgets
from FusionIcon.start import wms, apps, options, decorators, init
from FusionIcon.execute import run

class Build(QtWidgets.QApplication):
	def reload_wm(self):
		wms.restart()

	def toggleWM(self, wm):
		if wms.active != wm:
			wms.active = wm
			wms.start()

	def toggleOP(self, option):
		options[option].enabled = not options[option].enabled
		if wms.active == 'compiz':
			wms.start()

	def toggleWD(self, decorator):
		decorators.active = decorator

	def __init__(self, parent=None):
		QtCore.QObject.__init__(self, parent)
		self.setApplicationName('Fusion Icon')
		image = QtGui.QIcon()
		for iconName in ('fusion-icon', 'image-missing'):
			if QtGui.QIcon.hasThemeIcon(iconName):
				image = QtGui.QIcon.fromTheme(iconName)
				break
		self.Tray = QtWidgets.QSystemTrayIcon(image)
		self.Tray.setToolTip('Fusion Icon')
		self.Tray.managerMenu = QtWidgets.QMenu()
		self.Tray.optionsMenu = QtWidgets.QMenu()
		self.Tray.decoratorMenu = QtWidgets.QMenu()
		self.groupManager = QtWidgets.QActionGroup(self.Tray.managerMenu)
		self.groupDecorator = QtWidgets.QActionGroup(self.Tray.decoratorMenu)

		for wm in wms.ordered_list:
			actionWM = self.groupManager.addAction(self.Tray.managerMenu.addAction(wms[wm].label, lambda val=wm : self.toggleWM(val)))
			actionWM.setCheckable(True)
			if wms.active == wm:
				actionWM.setChecked(True)

		for option in options:
			actionOP = self.Tray.optionsMenu.addAction(options[option].label, lambda val=option: self.toggleOP(val))
			actionOP.setCheckable(True)
			if not options[option].sensitive:
				actionOP.setEnabled(False)
			actionOP.setChecked(options[option].enabled)

		for decorator in decorators:
			actionWD = self.groupDecorator.addAction(self.Tray.decoratorMenu.addAction(decorators[decorator].label, lambda val=decorator: self.toggleWD(val)))
			actionWD.setCheckable(True)
			if decorators.active == decorator:
				actionWD.setChecked(True)

		self.Tray.menu = QtWidgets.QMenu()

		if 'ccsm' in apps:
			self.Tray.menu.addAction(QtGui.QIcon.fromTheme('preferences-system'), apps['ccsm'].label, lambda: run(['ccsm']))
		if 'emerald theme manager' in apps:
			self.Tray.menu.addAction(QtGui.QIcon.fromTheme('emerald-theme-manager', QtGui.QIcon('/usr/share/pixmaps/emerald-theme-manager-icon.png')), \
			  apps['emerald theme manager'].label, lambda: run(apps['emerald theme manager'].command))
		if 'ccsm' in apps or 'emerald theme manager' in apps:
			self.Tray.menu.addSeparator()

		self.Tray.menu.addAction(QtGui.QIcon.fromTheme('view-refresh'), 'Reload Window Manager', self.reload_wm)
		self.Tray.menu.addAction(QtGui.QIcon.fromTheme('view-restore'), 'Select Window Manager').setMenu(self.Tray.managerMenu)
		self.Tray.menu.addAction(QtGui.QIcon.fromTheme('document-properties'), 'Compiz Options').setMenu(self.Tray.optionsMenu)
		self.Tray.menu.addAction(QtGui.QIcon.fromTheme('window-new'), 'Select Window Decorator').setMenu(self.Tray.decoratorMenu)
		self.Tray.menu.addSeparator()
		self.Tray.menu.addAction(QtGui.QIcon.fromTheme('application-exit'), 'Quit', self.exit)
		self.Tray.setContextMenu(self.Tray.menu)
		self.Tray.show()
		self.Tray.activated.connect(self.showMenu)
		init()

	def showMenu(self, reason):
		# Show the menu on left clicking as well.
		if reason == QtWidgets.QSystemTrayIcon.Trigger:
			pos = None
			if self.Tray.geometry().isValid():
				rect = self.Tray.geometry()
				pos = QtCore.QPoint(rect.left() + (rect.width() / 2),
				                    rect.bottom() + 1)
			elif int(QtCore.QT_VERSION_STR.split('.')[0]) >= 5:
				# With SNI this will be used, unless Qt4 (sni-qt).
				pos = QtGui.QCursor.pos()

			if pos:
				self.Tray.contextMenu().popup(pos)

Build(sys.argv).exec_()
