#@+leo-ver=4
#@+node:@file mod_autosave.py
"""Autosave the Leo document every so often"""

# By Paul Paterson.

from leoPlugins import *
from leoGlobals import *

import ConfigParser
import time, os

#@+others
#@+node:applyConfiguration
def applyConfiguration(config=None):
	
	"""Called when the user presses the "Apply" button on the Properties form"""

	global LAST_AUTOSAVE, ACTIVE, AUTOSAVE_INTERVAL

	if config is None:
		fileName = os.path.join(app().loadDir,"../","plugins","mod_autosave.ini")
		config = ConfigParser.ConfigParser()
		config.read(fileName)

	ACTIVE = config.get("Main", "Active")
	AUTOSAVE_INTERVAL = int(config.get("Main", "Interval"))
#@nonl
#@-node:applyConfiguration
#@+node:autosave
def autosave(tag, keywords):
	
	"""Save the current document if it has a name"""

	global LAST_AUTOSAVE

	if ACTIVE == "Yes":
		if time.time() - LAST_AUTOSAVE > AUTOSAVE_INTERVAL:
			c = top()
			if c.mFileName and c.changed:
				es("Autosave: %s" % time.ctime(),color="orange")
				c.fileCommands.save(c.mFileName)
			LAST_AUTOSAVE = time.time()
#@nonl
#@-node:autosave
#@-others

if 0: # Register the handlers...

	AUTOSAVE_INTERVAL = 600
	ACTIVE = "Yes"
	LAST_AUTOSAVE = time.time()
	applyConfiguration()

	__version__ = "0.2"
	registerHandler("idle", autosave)
	es("auto save enabled",color="orange")
#@-node:@file mod_autosave.py
#@-leo
