class PlainTextFile(PluginInterface.File):

# 	format = None
# 	lines = None
	characters = None
	contents = None
	encoding = None
	
	PlainTextFile_catalogable = [ "characters","contents","encoding" ]


	def __init__(self,filename,mimetype,volume):
		PluginInterface.File.__init__(self,filename,mimetype,volume)
# 		metadata = get_kde_metadata_expert().get_metadata(filename)
# 		if metadata.has_key("general.format"):
# 			self.format = metadata["general.format"]
# 		if metadata.has_key("general.lines"):
# 			self.lines = int(metadata["general.lines"])
# 		if metadata.has_key("general.characters"):
# 			self.characters = int(metadata["general.characters"])
		# now we'll read the file contents
		f = file(filename,"rb")
		c = f.read(-1)
		encoding = PluginInterface.detect_encoding(c)
		f.close()
		try:
			if encoding:
				self.encoding = encoding
				c = PluginInterface.transcode_to_platform_encoding(c,encoding)
				self.contents = PluginInterface.remove_whitespace(c)
				for char in self.contents:
					if ord(char) < 32 and char not in ['\t','\n','\r']:
						# these are control characters, inadmissible in text files
						# so we set the contents to None and then go on
						self.contents = None
						break
		except (UnicodeDecodeError,UnicodeEncodeError):
			PluginInterface.log("text-plain").info("%s is in an encoding unsupported by the platform",filename,exc_info=True)
		# FIXME we're just assuming text files are in platform encoding, and that is EVIL
		self.characters = len(c)

	def __str__(self):
		return "plain text file %s (%d characters)"%(self.get_filename(),self.characters)


def get_factory():
	return PlainTextFile

def get_mimetypes():
	return ["text/plain"]
