import Options
import Utils
import platform

APPNAME='lo-menubar'
VERSION='0.1.1'

#-- OPTIONS --
def options(opt):
	#TODO: option for libreoffice prefix
	opt.load('compiler_cxx')
	opt.add_option('--libreoffice-prefix',
	               action='store',
	               default='/usr/lib/libreoffice',
	               dest="LO_PREFIX",
	               help='Libreoffice prefix')
   	opt.add_option('--ure-prefix',
	               action='store',
	               default='/usr/lib/ure',
	               dest="URE_PREFIX",
	               help='Libreoffice prefix')

#-- CONFIGURE TARGET --
def configure(conf):
	conf.load('compiler_cxx')
	
	uno_sdk_libpath = '%s/sdk/lib' % Options.options.LO_PREFIX
	
	conf.check_cxx(lib='uno_sal', uselib_store='SALLIB', libpath=uno_sdk_libpath, mandatory=True)
	conf.check_cxx(lib='uno_cppu', uselib_store='CPPULIB', libpath=uno_sdk_libpath, mandatory=True)
	#conf.check_cc(lib='uno_salhelpergcc3', uselib_store='SALHELPERLIB',  mandatory=True)
	conf.check_cxx(lib='uno_cppuhelpergcc3', uselib_store='CPPUHELPERLIB', libpath=uno_sdk_libpath,  mandatory=True)
        conf.check_cxx(header_name='boost/scoped_ptr.hpp')
	
	conf.find_program('cppumaker', var='CPPUMAKER', path_list=[Options.options.LO_PREFIX + '/sdk/bin'], mandatory=True)

	conf.env['TYPES_RDB'] = conf.find_file('types.rdb',
	                                       path_list=[Options.options.URE_PREFIX + '/share/misc/'],
	                                       mandatory=True)
	
	conf.env['OFFAPI_RDB'] = conf.find_file('offapi.rdb',
	                                        path_list=[Options.options.LO_PREFIX + '/program/types'],
	                                        mandatory=True)
	
	conf.check_cfg(package='gio-2.0', uselib_store='GIO',
	               atleast_version='2.26.0', args='--cflags --libs',
	               mandatory=True)

	conf.check_cfg(package='dbusmenu-glib-0.4', uselib_store='DBUSMENU',
	               atleast_version='0.3.99', args='--cflags --libs')	               
	conf.check_cfg(package='dbusmenu-gtk-0.4', uselib_store='DBUSMENUGTK',
	               atleast_version='0.3.99', args='--cflags --libs')
	conf.check_cfg(package='gtk+-2.0', uselib_store='GTK',
	               atleast_version='2.24.1', args='--cflags --libs')
	conf.check_cfg(package='atk', uselib_store='ATK',
	               atleast_version='1.33.6', args='--cflags --libs')
	conf.check_cfg(package='gdk-pixbuf-2.0', uselib_store='GDKPIXBUF',
	               atleast_version='2.23.0', args='--cflags --libs')
	conf.check_cfg(package='gdk-2.0', uselib_store='GDK',
	               atleast_version='2.24.1', args='--cflags --libs')	               
	conf.check_cfg(package='x11', uselib_store='X11', args='--cflags --libs')
	conf.check_cfg(package='xproto', uselib_store='XPROTO', args='--cflags --libs')

def cppumaker (bld):
	"""
	This helper generates the C++ headers for the IDL of the classes used.
	These headers are stored inside the build/lo/include directory.
	"""
	types = ["com.sun.star.awt.WindowAttribute",
				"com.sun.star.awt.Key",
				"com.sun.star.awt.KeyEvent",
				"com.sun.star.awt.MenuEvent",
				"com.sun.star.awt.KeyModifier",
				"com.sun.star.awt.XMessageBox",
				"com.sun.star.awt.XWindow2",
				"com.sun.star.awt.XWindowPeer",
				"com.sun.star.awt.XSystemDependentWindowPeer",
				"com.sun.star.awt.SystemDependentXWindow",
				"com.sun.star.awt.XMenu",
				"com.sun.star.awt.XMenuBar",
				"com.sun.star.awt.XMenuExtended",
				"com.sun.star.awt.XPopupMenu",
				"com.sun.star.awt.XPopupMenuExtended",
				"com.sun.star.beans.NamedValue",
				"com.sun.star.beans.PropertyValue",
				"com.sun.star.beans.XPropertySet",
				"com.sun.star.container.XContainerQuery",
				"com.sun.star.container.XIndexContainer",
				"com.sun.star.container.XIndexAccess",
				"com.sun.star.container.XNameAccess",
				"com.sun.star.container.NoSuchElementException",
				"com.sun.star.document.XEventBroadcaster",
				"com.sun.star.document.XEventListener",
				"com.sun.star.document.EventObject",
				"com.sun.star.frame.ControlCommand",
				"com.sun.star.frame.ControlEvent",
				"com.sun.star.frame.DispatchDescriptor",
				"com.sun.star.frame.XComponentLoader",
				"com.sun.star.frame.XController",
				"com.sun.star.frame.XControlNotificationListener",
				"com.sun.star.frame.XDispatch",
				"com.sun.star.frame.XDispatchProvider",
				"com.sun.star.frame.XDispatchHelper",
				"com.sun.star.frame.XFrame",
				"com.sun.star.frame.XFrameActionListener",
				"com.sun.star.frame.FrameActionEvent",
				"com.sun.star.frame.XLayoutManager",
				"com.sun.star.frame.XModel",
				"com.sun.star.frame.XStorable",
				"com.sun.star.frame.XStatusListener",
				"com.sun.star.frame.XModuleManager",
				"com.sun.star.frame.XStatusListener",
				"com.sun.star.frame.XPopupMenuController",
				"com.sun.star.frame.FeatureStateEvent",
				"com.sun.star.frame.FrameSearchFlag",
				"com.sun.star.frame.status.Visibility",
				"com.sun.star.lang.EventObject",
				"com.sun.star.lang.XInitialization",
				"com.sun.star.lang.XMultiServiceFactory",
				"com.sun.star.lang.XServiceInfo",
				"com.sun.star.lang.XSingleComponentFactory",
				"com.sun.star.lang.XSingleServiceFactory",
				"com.sun.star.lang.XTypeProvider",
				"com.sun.star.lang.SystemDependent",
				"com.sun.star.registry.XRegistryKey",
				"com.sun.star.sheet.XSpreadsheetView",
				"com.sun.star.system.SystemShellExecuteFlags",
				"com.sun.star.system.XSystemShellExecute",
				"com.sun.star.text.XTextCursor",
				"com.sun.star.text.XTextDocument",
				"com.sun.star.text.XTextViewCursor",
				"com.sun.star.text.XTextViewCursorSupplier",
				"com.sun.star.ui.XUIElement",
				"com.sun.star.ui.XUIElementSettings",
				"com.sun.star.ui.XAcceleratorConfiguration",
				"com.sun.star.ui.XUIConfigurationManagerSupplier",
				"com.sun.star.ui.XUIConfigurationManager",
				"com.sun.star.ui.XModuleUIConfigurationManagerSupplier",
				"com.sun.star.uno.XAggregation",
				"com.sun.star.uno.XComponentContext",
				"com.sun.star.uno.XWeak",
				"com.sun.star.util.URL",
				"com.sun.star.util.XURLTransformer",
				"com.sun.star.view.XSelectionSupplier",
				"com.sun.star.task.XJob"]
	
	types_db = '%s %s' % (bld.env.TYPES_RDB, bld.env.OFFAPI_RDB)

	types_str = "-T" + " -T".join(types)
	target = bld.path.get_bld().abspath() + '/lo/include'

	bld.exec_command ('%s -Gc -BUCR -O%s %s %s' % (bld.env.CPPUMAKER, target, types_str, types_db))
	
	return target

def build(bld):
	includes = ['/usr/include/libreoffice']
	
	includes.append (cppumaker (bld))
	
	env = bld.env.copy()
	pattern = env['cxxshlib_PATTERN']
	env['cxxshlib_PATTERN']	= '%s.uno' + env['cxxshlib_PATTERN'].split("%s")[-1]
	env.append_value('LINKFLAGS', '-Wl,--version-script=%s/data/component.uno.map,-z origin' % bld.path.abspath())
	env.append_value('CXXFLAGS', '-Wall')
	env.append_value('CXXFLAGS', '-g')
	
	target = 'menubar'
	
	bits = platform.architecture()[0][:-3]

	if bits == "64":
		lo_platform = "Linux_x86_64"
	else:
		lo_platform = "Linux_x86"
	
	bld.shlib(source=['src/DesktopJob.cxx',
	                  'src/FrameJob.cxx',
	                  'src/exports.cxx',
	                  'src/FrameHelper.cxx',
	                  'src/AwtKeyToDbusmenuString.cxx',
	                  'src/MenuItemInfo.cxx',
	                  'src/MenuItemStatusListener.cxx'],
	          target=target,
	          uselib=['SALLIB', 'CPPULIB', 'CPPUHELPERLIB', 'GIO', 'DBUSMENU', 'DBUSMENUGTK', 'GDKPIXBUF', 'GTK', 'GDK', 'ATK', 'X11', 'XPROTO'],
	          includes=includes,
	          defines=['UNX', 'SAL_UNX', 'CPPU_ENV=gcc3'],
	          install_path='%s/share/extensions/%s/%s' % (Options.options.LO_PREFIX, target, lo_platform),
	          env=env,
	          chmod=Utils.O644)
	          
	bld(features='subst',
	    source='data/manifest.xml.in',
	    target='manifest.xml',
	    install_path='%s/share/extensions/%s/META-INF' % (Options.options.LO_PREFIX, target),
	    PLATFORM=lo_platform,
	    COMPONENT=env['cxxshlib_PATTERN'] % target)
	    
	    
	bld.install_files('%s/share/extensions/%s' % (Options.options.LO_PREFIX, target), bld.path.ant_glob('data/*.xcu'))

## Post installation trigger, doesn't work
#	def post_install (ctx):
#		if ctx.cmd != 'install':
#			return
#		ctx.exec_command ('/usr/lib/ure/bin/regcomp -register -wop="vnd.sun.star.expand:\$OOO_BASE_DIR/program/" -r /usr/lib/libreoffice/basis3.3/program/appmenu.rdb -c /usr/lib/libreoffice/basis3.3/program/%s.uno.so' % 'menubar')
#	
#	bld.add_post_fun (post_install)

## Creating the OXT file
#def oxt (ctx):
#	from zipfile import ZipFile
#	zipfile = ZipFile('plugin.oxt', 'w')
	#count = len(ctx.path.abspath() + '/plugin/')
#	elements = ctx.path.ant_glob('plugin/**')
#	for f in elements:
#		zipfile.write (f.abspath(), f.abspath()[count:])
#	zipfile.close ()
