# -*-python-*-
# vim: ts=4

import handlerbase

class DesignerHandler(handlerbase.HandlerBase):
	def __init__(self):
		pass

	def extensions(self):
		return ['.ui']
	
	def source(self, program, base, ext):

		rulef = program.amfile.rulef
		
		sourcedir = ''
		if program.amfile.in_srcdir(base + ext):
			sourcedir = rulef.source
		else:
			sourcedir = rulef.build
	
		rules = ["@echo 'creating %s.cpp'" % (rulef.build + base)]
		if not base in program.ui_files:
			if not program.amfile.get_opt("qtonly"):
				rules.append("@echo '#include <kdialog.h>' > %s%s.cpp" % (rulef.build, base))
				rules.append("@echo '#include <klocale.h>' >> %s%s.cpp" % (rulef.build, base))
				canon_source = utilities.canon_name(base)
				rules.append("@adir=`cd %s && pwd` ;\\" % sourcedir)
				rules.append("$(UIC) -tr ${UIC_TR} -i %s.h $$adir/%s.ui > %s%s.cpp.temp ; ret=$$?; \\" % (base, base, rulef.build, base))
				line = 'sed -e "s,${UIC_TR}( \"\" ),QString::null,g" %s%s.cpp.temp ' % (rulef.build, base)
				line = line + '| sed -e "s,image\([0-9][0-9]*\)_data,img\\1_%s,g" ' % canon_source
				line = line + '>> %s%s.cpp ;\\' % (rulef.build, base)
				rules.append( line )
				rules.append('rm -f %s%s.cpp.temp ;\\' % (rulef.build, base))
			else:
				rules.append( "@$(UIC) -i %s.h %s.ui > %s%s.cpp ; ret=$$? \\"
							  % (base, sourcedir + base, rulef.build, base) )
			rules.append( "if test \"$$ret\" = 0; then echo '#include \"%s.moc\"' >> %s%s.cpp; else rm -f %s%s.cpp; exit $$ret; fi" % (base, rulef.build, base, rulef.build, base) )

			rulef.insertTarget(rulef.build + base + ".cpp",
							   [sourcedir + base + ".ui", rulef.build + base + ".h",
								rulef.build + base + ".moc"],
							   rules)

			rulef.insertTarget(rulef.build + base + ".h", [sourcedir + base + ".ui"],
							   ["@echo 'creating %s.h'" % (rulef.build + base),
								"@$(UIC) -o $@ $?"])

			program.ui_files.append(base)
			self.__check_includes(base + ".h", program)
			program.handle_source(base, ".cpp")
			program.cleanfiles.extend([base + ".h", base + ".cpp"])

	def __check_includes(self, headerfile, program):

		import utilities
		cppfiles, hfiles = utilities.list_source_files(program.amfile.dirname)

		includere = re.compile("[ \t]*#include\s*[<\"]" + headerfile + "[>\"]")
		for (base, ext) in hfiles:
			file = base + ext
			lines = open(program.amfile.dirname + "/" + file).readlines()
			for line in lines:
				match = includere.match(line)
				if match:
					program.amfile.add_define(program.canon_name + "_COMPILE_FIRST",
											  headerfile)
					return

		rulef = program.amfile.rulef
		
		# no h file found so far - find cpp files
		for (base, ext) in cppfiles:
			file = base + ext
			lines = open(program.amfile.dirname + "/" + file).readlines()
			for line in lines:
				match = includere.match(line)
				if match:
					rulef.insertTarget(rulef.build + base + ".o", rulef.build + headerfile)
					rulef.insertTarget(rulef.build + base + ".lo", rulef.build + headerfile)
					
register_handler(DesignerHandler())
