"""Subclass of WebPreferences, which is generated by wxFormBuilder."""

import wx
import app

# Implementing WebPreferences
class WebPreferences( app.WebPreferences ):
	def __init__( self, parent, **kwargs ):
		self._use_proxy = kwargs.get('use_proxy',False)
		self._http_proxy = kwargs.get('http_proxy','')
		self._https_proxy = kwargs.get('https_proxy','')
		self._ftp_proxy = kwargs.get('ftp_proxy','')
		self._use_auto = kwargs.get('auto',True)
		# -- enable this when multiproxy is done:
		# self._use_same = kwargs.get('same',False)
		# -- delete this when multiproxy is done:
		self._use_same = True
		super(WebPreferences, self).__init__(parent)
		self.UpdateData()
	
	def UpdateData(self):
		"""Setup control values"""
		if self._use_proxy:
			self.m_radioBtn5.SetValue(True)
			self.st_http_proxy.Enable(True)
			self.m_http_proxy.Enable(True)
			# -- enable this when multiproxy is done
			# self.m_checkBox6.Enable(True)
			# detailed = not self.m_checkBox6.GetValue()
			# self.st_https_proxy.Enable(detailed)
			# self.m_https_proxy.Enable(detailed)
			# self.st_ftp_proxy.Enable(detailed)
			# self.m_ftp_proxy.Enable(detailed)
		else:
			self.m_radioBtn4.SetValue(True)
			self.st_http_proxy.Enable(False)
			self.m_http_proxy.Enable(False)
			self.m_checkBox6.Enable(False)
			self.st_https_proxy.Enable(False)
			self.m_https_proxy.Enable(False)
			self.st_ftp_proxy.Enable(False)
			self.m_ftp_proxy.Enable(False)
		self.m_http_proxy.SetValue(self._http_proxy)
		self.m_https_proxy.SetValue(self._https_proxy)
		self.m_ftp_proxy.SetValue(self._ftp_proxy)
		self.m_checkBox6.SetValue(self._use_same)
		
	def GetData(self):
		"""Get data from controls"""
		self._use_proxy = self.m_radioBtn5.GetValue()
		self._http_proxy = self.m_http_proxy.GetValue()
		self._https_proxy = self.m_https_proxy.GetValue()
		self._ftp_proxy = self.m_ftp_proxy.GetValue()
		# -- enable this when multiproxy is done
		# self._use_same = self.m_checkBox6.GetValue()		
		
	# Handlers for WebPreferences events.
	def OnAutoNetwork( self, event ):
		# disable all controls
		self.st_http_proxy.Enable(False)
		self.m_http_proxy.Enable(False)
		# -- enable this when multiproxy is done
		# self.m_checkBox6.Enable(False)
		# self.st_https_proxy.Enable(False)
		# self.m_https_proxy.Enable(False)
		# self.st_ftp_proxy.Enable(False)
		# self.m_ftp_proxy.Enable(False)
		self.m_button4.Enable(True)
	
	def OnManualProxy( self, event ):
		self.st_http_proxy.Enable(True)
		self.m_http_proxy.Enable(True)
		# -- enable this when multiproxy is done
		# self.m_checkBox6.Enable(True)
		# detailed = not self.m_checkBox6.GetValue()
		# self.st_https_proxy.Enable(detailed)
		# self.m_https_proxy.Enable(detailed)
		# self.st_ftp_proxy.Enable(detailed)
		# self.m_ftp_proxy.Enable(detailed)
		self.m_button4.Enable(True)
	
	def OnChangeHttpProxy( self, event ):
		self.m_button4.Enable(True)
	
	def OnSameProxy( self, event ):
		# -- enable this when multiproxy is done
		# detailed = not self.m_checkBox6.GetValue()
		# self.st_https_proxy.Enable(detailed)
		# self.m_https_proxy.Enable(detailed)
		# self.st_ftp_proxy.Enable(detailed)
		# self.m_ftp_proxy.Enable(detailed)
		# self.m_button4.Enable(True)
		pass
	
	def OnChangeHttpsProxy( self, event ):
		self.m_button4.Enable(True)
	
	def OnChangeFtpProxy( self, event ):
		self.m_button4.Enable(True)

	def OnApply( self, event ):
		import ctypes
		import platform
		self.GetData()
		s_plat = platform.platform() 
		if 'Linux' not in s_plat and 'linux' not in s_plat:
			wx.MessageBox('Proxy is only supported in linux','Error',
				wx.OK | wx.CENTER | wx.ICON_ERROR, self)
			return
		if self._use_proxy:
			try:	
				soup = ctypes.cdll.LoadLibrary('libsoup-2.4.so')
				webkit = ctypes.cdll.LoadLibrary('libwebkitgtk-1.0.so')
				gobject = ctypes.cdll.LoadLibrary('libgobject-2.0.so')
				if '64bit' in platform.architecture():
					webkit.webkit_get_default_session.restype = ctypes.c_uint64
					soup.soup_uri_new.restype = ctypes.c_uint64
					gobject.g_object_set.argtypes=(ctypes.c_uint64, ctypes.c_char_p, ctypes.c_uint64, ctypes.c_uint64)
					soup.soup_uri_free.argtypes=(ctypes.c_uint64,)
				else:
					#untested
					webkit.webkit_get_default_session.restype = ctypes.c_uint32
					soup.soup_uri_new.restype = ctypes.c_uint32
					gobject.g_object_set.argtypes=(ctypes.c_uint32, ctypes.c_char_p, ctypes.c_uint32, ctypes.c_uint32)
					soup.soup_uri_free.argtypes=(ctypes.c_uint32,)
				proxy_uri = soup.soup_uri_new('http://{proxy}'.format(proxy=self._http_proxy))
				session = webkit.webkit_get_default_session()
				gobject.g_object_set(session, "proxy-uri", proxy_uri, 0)
				soup.soup_uri_free(proxy_uri)
			except:
				wx.MessageBox('Failed','Error', wx.OK | wx.CENTER | wx.ICON_ERROR, self)
				return
		self.m_button4.Enable(False)

	
