#!/usr/bin/env python
# Beryl simplified settings manager
#
# beryl-settings-simple.in
#
# Copyright (c) 2006 Robert Carr <racarr@beryl-project.org>
#
# This program 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.
#
# This program 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.



import gobject,berylsettings,os,gettext,pygtk,gtk
import gtk.gdk
_=gettext.gettext
gdk=gtk.gdk

APP = 'beryl-settings-simple'
DIR = 'locale'
import locale
import gettext
locale.setlocale(locale.LC_ALL,"")
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
_ = gettext.gettext


DATADIR="/usr/local/share"
IMAGEDIR="/usr/local/share/beryl-settings-simple/"
PREFIX="/usr/local"
VERSION="0.2.1"



class SettingsManager:
		
	def destroy(self,widget,data=None):
		gtk.main_quit()
 	def delete_event(self,widget,event,data=None):
		return False
	
	def setdefs(self):
		for plugin in self.Ctx.Plugins:
			for setting in plugin.Settings:
				setting.ResetToDefault()
	
	def scaleChanged(self,widget,data,event=None):
		
		print "Setting value %s" % widget.value
		descriptions=[_("Slow and steady wins the race\n"),_("Easy on the eyes, but still fun\n"),_("Not too much, but a bit zippy\n"),_("For the special effects lover\n"),_("Hollywood got nothing\n")]
		pixmaps=[IMAGEDIR+"/blue_1.png",IMAGEDIR+"/green_2.png",IMAGEDIR+"/yellow_3.png",IMAGEDIR+"/orange_4.png",IMAGEDIR+"/red_5.png"]
		data[0].set_from_pixbuf(gdk.pixbuf_new_from_file_at_size(pixmaps[int(widget.value)-1],32,32))
		data[1].set_markup("<i>%s</i>"%(descriptions[int(widget.value)-1],))
		home = os.getenv("HOME")
		r=open(home+'/.beryl/scale','r')
		if(float(r.read())==widget.value):
			return
		
		f=open(home+'/.beryl/scale','w')
		f.write(str(widget.value))
		f.close()
		if widget.value==1:
			self.setdefs()
			self.level1()
		if widget.value==2:
			self.setdefs()
			self.level2()
		if widget.value==3:
			self.setdefs()
			self.level3()
		if widget.value==4:
			self.setdefs()
			self.level4()
		if widget.value==5:
			self.setdefs()
			self.level5()
		self.Ctx.Write()
		berylsettings.send_reload()		
	def level1(self):
		self.Ctx.Import(IMAGEDIR+"/level1.Profile",True)
			
	def level2(self):
		self.Ctx.Import(IMAGEDIR+"/level2.Profile",True)

	
	def level3(self):
		self.Ctx.Import(IMAGEDIR+"/level3.Profile",True)

	
	def level4(self):
		self.Ctx.Import(IMAGEDIR+"/level4.Profile",True)

	def level5(self):
		self.Ctx.Import(IMAGEDIR+"/level5.Profile",True)

	
	def updateBools(self,widget,data=None):
		print "Toggle"
		if data==0:
							
				self.opacify.Setting("____plugin_enabled").Value=not self.opacify.Setting("____plugin_enabled").Value
		if data==1:
				try:
						self.annotate.Setting("____plugin_enabled").Value=not self.opacify.Setting("____plugin_enabled").Value
				except:
						print "No annotate"
		if data==2:
			if widget.get_active():
				self.resize.Setting("resize_mode").Value=self.resize.Setting("resize_mode").Restrictions[0]
			else:
				self.resize.Setting("resize_mode").Value=self.resize.Setting("resize_mode").Restrictions[3]

		self.Ctx.Write()
		berylsettings.send_reload()
	
	def numDesktops(self,widget,data=None):
		self.core.Setting("size").Value=(widget.get_value_as_int())
		self.Ctx.Write()
		berylsettings.send_reload()

	def reloadImages(self,widget,data=None):
		if data==0:
			et=self.cube.Setting("images_top").Value
			eb=self.cube.Setting("images_bottom").Value
			et[0]=widget.get_text()
			eb[0]=widget.get_text()
			self.cube.Setting("images_top").Value=et
			self.cube.Setting("images_bottom").Value=eb
		if data==1:
			if widget.get_text():
				self.cube.Setting("skydome").Value=True
				
				self.cube.Setting("skydome_image").Value=widget.get_text()
			else:
				self.cube.Setting("skydome").Value=False
				
		self.Ctx.Write()
		berylsettings.send_reload()
	
	def reloadAnimation(self,animation,widget):
		print animation
		animations=self.animation.Setting(animation).Restrictions
		self.animation.Setting(animation).Value=animations[widget.get_active()]
		self.Ctx.Write()
		berylsettings.send_reload()
	
	def reloadAnimations(self,widget,data=None):

		if data==0:
			self.reloadAnimation("create1_effect",widget)
		if data==1:
			self.reloadAnimation("close1_effect",widget)
		if data==2:
			self.reloadAnimation("minimize_effect",widget)
		if data==3:
			self.reloadAnimation("unminimize_effect",widget)
	
	def getSet(self,corner):
		actions=("initiate","initiate_app","initiate_all","initiate_current_head")
		i=0
		while i < 4:
			val=self.scale.Setting(actions[i]).Value
			if val[3].count(corner) > 0:
				return i+1
			i+=1
		return 0
	
	def clearCorners(self,corner):
		actions=("No","initiate","initiate_app","initiate_all","initiate_current_head")
		i=1
		while i < 5:
			val=self.scale.Setting(actions[i]).Value
			
			if val[3].count(corner) > 0:			
				val=(val[0],val[1],val[2],[])
				self.scale.Setting(actions[i]).Value=val
			i+=1
	
	def setCorner(self,corner,widget):
		print corner
		action = widget.get_active()
		actions= ("No","initiate","initiate_app","initiate_all","initiate_current_head")
		set=self.scale.Setting(actions[action])
		self.clearCorners(corner)
		set.Value=(set.Value[0],set.Value[1],set.Value[2],[corner])
		
	def cornerSet(self,widget,data=None):
		scaleOpts = ("No Action", "Initiate ", "Current App", "All workspaces", "Current head" )
		actions= ("No","initiate","initiate_app","initiate_all","initiate_current_head")
		action = widget.get_active()
		data-=1
		corners=("TopLeft","TopRight","BottomLeft","BottomRight")
		if action == 0: 
			print "clear"
			self.clearCorners(corners[data])
			print "Corner: " + corners[data]
			self.Ctx.Write()
			berylsettings.send_reload()
			return;
		
		self.setCorner(corners[data],widget)

		self.Ctx.Write()
		berylsettings.send_reload()
		
	
	def draw_advanced(self,widget=None,data=None):
		for widget in self.windowBox.get_children():
			self.windowBox.remove(widget)
		topBox=gtk.HBox()
		anims=(_("Create"),_("Close"),_("Minimize"),_("Restore"))
		animBox=gtk.VBox(spacing=5, homogeneous=False)
		
		self.windowBox.pack_start(topBox,False,False)
		topBox.pack_start(animBox,False,False)
		
		animLabel=gtk.Label()
		animLabel.set_markup(_("<b>Animations:</b>"))
		
		animBox.pack_start(animLabel,False,False,padding=10)
		animsettings=("create1_effect","close1_effect","minimize_effect","unminimize_effect")
		i=0
		for animation in anims:
			animOverride=gtk.HBox(homogeneous=False)
			animOverride.show()
			animBox.pack_start(animOverride,False,False,padding=3)
			name = gtk.Label()
			name.set_markup(animation)
			name.show()
			animOverride.pack_start(name,False,False,padding=10)
			combo=gtk.combo_box_new_text()
			for anim in self.animation.Setting(animsettings[i]).Restrictions:
				combo.append_text(anim)
			combo.connect("changed",self.reloadAnimations,i)
			
			combo.set_active(self.animation.Setting(animsettings[i]).Restrictions.index( 
			self.animation.Setting(animsettings[i]).Value ))

			i+=1
			combo.show()
			animOverride.pack_start(combo,True,True,padding=20)
		animicon=gtk.Image()
		animbuf=gdk.pixbuf_new_from_file_at_size(IMAGEDIR+"/beryl-settings-section-animation.svg",80,80)
		animicon.set_from_pixbuf(animbuf)
		animicon.show()
		
		vsep=gtk.VSeparator()
		vsep.show()
		
		animBox.pack_start(animicon,False,False)
		
		topBox.pack_start(vsep,False,False,padding=10)
		
		cubeBox=gtk.VBox(spacing=5)
		
		topBox.pack_start(cubeBox,False,False)
		
		cubeLabel=gtk.Label()
		cubeLabel.set_markup("<b>Desktop Cube:</b>")
		cubeLabel.show()
		cubeBox.pack_start(cubeLabel,False,False,padding=10)
		i=0
		for n in (_("Cap Image"),_("Skydome Image")):
			settingBox=gtk.HBox()
			settingBox.show()
			cubeBox.pack_start(settingBox,False,False,padding=3)
			name=gtk.Label()
			name.set_markup(n)
			name.show()
			settingBox.pack_start(name,False,False,padding=5)
			entry=gtk.Entry()
			entry.show()	
			entry.connect("changed",self.reloadImages,i)
			settingBox.pack_start(entry,False,False,padding=5)
			if i==0:
				entry.set_text(self.cube.Setting("images_top").Value[0])
			if i==1:
				if self.cube.Setting("skydome").Value:
					entry.set_text(self.cube.Setting("skydome_image").Value)
			i+=1
		
		spin=gtk.SpinButton(gtk.Adjustment(value=1,lower=1,upper=32,step_incr=1,page_incr=4),1,0)
		spin.show()
		spin.connect("changed",self.numDesktops,None)
		settingBox=gtk.HBox()
		settingBox.show()
		cubeBox.pack_start(settingBox,False,False,padding=3)
		name=gtk.Label()
		name.set_markup(_("Number of desktops "))
		name.show()
		settingBox.pack_start(name,False,False,padding=10)
		settingBox.pack_start(spin,False,False)
		
		cubeicon=gtk.Image()
		cubebuf=gdk.pixbuf_new_from_file_at_size(IMAGEDIR+"/beryl-settings-section-cube.svg",80,80)
		cubeicon.set_from_pixbuf(cubebuf)
		cubeicon.show()
		
		cubeBox.pack_start(cubeicon,False,False,padding=42)
		
		hsep=gtk.HSeparator()
		hsep.show()
		
		bottomBox=gtk.HBox()
						
		self.windowBox.pack_start(hsep)
		
		self.windowBox.pack_start(bottomBox,False,False, padding=5)
		
		i=0
		for checkbox in (_("Opacify"),_("Draw on screen"),_("Classic resize")):
			check=gtk.CheckButton(checkbox)
			check.show()
			check.connect("clicked",self.updateBools,i)
			bottomBox.pack_start(check,False,False,padding=35)
			if i==0:
				check.set_active(self.opacify.Setting("____plugin_enabled").Value)
			if i==1:
				try:
						check.set_active(self.annotate.Setting("____plugin_enabled").Value)
				except:
						print "No annotate"
			if i==2:
		       	        check.set_active(self.resize.Setting("resize_mode").Value=="Normal")
			i+=1
			
		buttonBox=gtk.VBox()
		buttonBox.show()
		buttonImage=gtk.image_new_from_stock(gtk.STOCK_GO_BACK, gtk.ICON_SIZE_LARGE_TOOLBAR)
		buttonImage.show()
		
		l=gtk.Label()
		l.set_markup(_("    <b>Main</b>    "))
		l.show()
		
		buttonBox.pack_start(buttonImage,False,False)
		buttonBox.pack_start(l,False,False)
		
		spin.set_value ( self.core.Setting("size").Value )
		
		buttonEvent=gtk.EventBox()
		
		buttonEvent.add(buttonBox)
		
		buttonEvent.show()
		
		buttonEvent.connect("button_press_event", self.show_basic)
		
		bottomBox.pack_end(buttonEvent,False,False,padding=0)
		self.window.add(self.windowBox)
		self.windowBox.show()
		bottomBox.show()	
		animBox.show()
		cubeBox.show()
		topBox.show()
		animLabel.show()
	def show_basic(self,widget=None,data=None):
		elimg = gtk.Image();

		home = os.getenv("HOME")
		for widget in self.windowBox.get_children():
			self.windowBox.remove(widget)
		effectBox = gtk.VBox()
		self.windowBox.pack_start(effectBox,False,False)
		
		effectLabelBox = gtk.HBox()
		
		for widget in effectBox:
			effectLabelBox.remove(widget)
		
		el = gtk.Label()
		el.set_markup(_("<b>Effects level</b>"))
		effectLabelBox.pack_start(el,False,False)
		h=gtk.HSeparator()
		h.show()
		effectLabelBox.pack_start(h,True,True,padding=10)
		effectLabelBox.pack_end(elimg,False,False)
		effectBox.pack_start(effectLabelBox,False,False)
		
		adj=gtk.Adjustment(value=1,lower=1,upper=5,step_incr=1)
		
		effectScale = gtk.HScale(adj)
		effectScale.set_digits(0)
		effectScaleLabel = gtk.Label()
		try:
			home = os.getenv("HOME")
			f=open(home+'/.beryl/scale','r')
			val=(f.read())
			adj.set_value(float(val))
		except: pass
		
		self.scaleChanged(adj,(elimg,effectScaleLabel))
		
		f=open(home+'/.beryl/scale','r')
		val=(f.read())
		adj.set_value(float(val))
		
		adj.connect("value_changed",self.scaleChanged,(elimg,effectScaleLabel))

		
		effectBox.pack_start(effectScale,False,False)
		effectBox.pack_start(effectScaleLabel,False,False)
		
		hotcornersBox=gtk.VBox()
		
		hcLabel=gtk.Label()
		hcLabel.set_markup(_("<b>Hotcorners</b>"))
		
		hcLabel.show()
		
		hotcornersBox.pack_start(hcLabel,False,False,padding=5)
		
		self.windowBox.pack_start(hotcornersBox,False,False)
		scaleOpts = (_("No Action"), _("Initiate "), _("Current App"), _("All workspaces"), _("Current head") )
		upperHotcorners = gtk.HBox()
		combo1=gtk.combo_box_new_text()
		for scale in scaleOpts:
			combo1.append_text(scale)
		combo1.connect("changed",self.cornerSet,1)
		combo2=gtk.combo_box_new_text()
		for scale in scaleOpts:
			combo2.append_text(scale)
		combo1.show()
		combo2.show()
		combo2.connect("changed",self.cornerSet,2)
		upperHotcorners.pack_start(combo1,False,False,padding=20)
		upperHotcorners.pack_end(combo2,False,False,padding=20)
		
		hotcornersBox.pack_start(upperHotcorners,False,False)
		
		imageHotcornersBox=gtk.HBox()
		
		hotcornerImage=gtk.Image()
		hotcornerPix=gdk.pixbuf_new_from_file_at_size(IMAGEDIR+"/hotcorners_racarr.svg",130,130)
		hotcornerImage.set_from_pixbuf(hotcornerPix)
		
		imageHotcornersBox.pack_start(hotcornerImage,False,False,padding=230)
		
		hotcornersBox.pack_start(imageHotcornersBox,False,False,padding=10)
		
		lowerHotcorners = gtk.HBox()
		lcombo1=gtk.combo_box_new_text()
		lcombo2=gtk.combo_box_new_text()
		lcombo1.show()
		lcombo2.show()
		lcombo1.connect("changed",self.cornerSet,3)
		lcombo2.connect("changed",self.cornerSet,4)
		for scale in scaleOpts:
			lcombo1.append_text(scale)
			lcombo2.append_text(scale)
		lowerHotcorners.pack_start(lcombo1,False,False,padding=20)
		lowerHotcorners.pack_end(lcombo2,False,False,padding=20)
		
		hotcornersBox.pack_start(lowerHotcorners,False,False)
		
		buttonBox=gtk.HBox()
				
		combo1.set_active(self.getSet('TopLeft'))
		combo2.set_active(self.getSet('TopRight'))
		lcombo1.set_active(self.getSet('BottomLeft'))
		lcombo2.set_active(self.getSet('BottomRight'))
		
		buttonSmallBox=gtk.VBox()

		buttonImage = gtk.image_new_from_stock(gtk.STOCK_GO_FORWARD, gtk.ICON_SIZE_LARGE_TOOLBAR)

		buttonSmallBox.pack_start(buttonImage,False,False)
		
		buttonLabel=gtk.Label()
		buttonLabel.set_markup(_(" <b>Advanced</b> "))
		buttonEventBox=gtk.EventBox()
		
		buttonSmallBox.pack_start(buttonLabel,False,False)
		
		hs=gtk.HSeparator()
		hs.show()
		
		buttonBox.pack_start(hs,True,True,padding=10)
		buttonEventBox.add(buttonSmallBox)
		
		buttonEventBox.connect("button_press_event", self.draw_advanced)
		
		buttonEventBox.show()
		buttonBox.pack_end(buttonEventBox,False,False)
		
		self.windowBox.pack_end(buttonBox,False,False,padding=5)
		
		self.windowBox.show()
	
		toshow=(buttonLabel,buttonBox,effectBox,buttonImage,buttonSmallBox,lowerHotcorners,elimg,imageHotcornersBox,hotcornerImage,upperHotcorners,hotcornersBox,el,self.window,effectLabelBox,effectScale,effectScaleLabel,self.windowBox)
		for widg in toshow:
			widg.show()
	

	def __init__(self):
		home=os.getenv("HOME")
		if not os.path.exists(home+'/.beryl/scale'):
			f=open(home+'/.beryl/scale','w')
			f.write('3.0')
			f.close()
		self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect("delete_event",self.delete_event)
		self.window.connect("destroy",self.destroy)
		self.window.set_border_width(0)
		self.window.set_title("BSM--")
		self.window.set_icon (gdk.pixbuf_new_from_file(IMAGEDIR+"beryl-settings.svg"))
		self.window.set_resizable(False)
		self.windowBox = gtk.VBox()


		self.Ctx=berylsettings.Context()
		self.Ctx.Read()
		try:
			self.resize=self.Ctx.Plugin("resize")
			self.scale=self.Ctx.Plugin("scale")
			self.animation=self.Ctx.Plugin("animation")
			self.opacify=self.Ctx.Plugin("opacify")
			self.cube=self.Ctx.Plugin("cube")
			self.core=self.Ctx.Plugin("_")
			self.annotate=self.Ctx.Plugin("annotate")
		except:
			print "Couldn't load plugins"
		self.window.add(self.windowBox)
		self.show_basic()
		self.window.set_size_request(self.window.get_size()[0]+10,self.window.get_size()[1]+100)

		print self.window.get_size()
	def main(self):
		gtk.main()
		
if __name__=="__main__":
	settings = SettingsManager()
	settings.main()
