#(c) 2004-2008 Thunderstone Media, LLC
#Creative Commons Attribution-Noncommercial 3.0 United States License
#
#Python Developyment By:
#
#Ray Slakinski
#August Trometer

import iPXSettings
from iPXTools import *
from types import *

class iPXError(Exception): pass
class OutOfRangeError(iPXError): pass
class NotIntegerError(iPXError): pass

def qmCacheCreate():
	import iPXClass
	
	printMSG('Creating SmartSpace Cache File...')
	try:
		feedsObj = iPXClass.Feeds()
		feedsObj.createQMCacheBridge()
	except Exception, msg:
		logIt('Create qmcache.dat failed...')
		logIt('ERRMSG: ' + str(msg))

def writeQuotaCache(feedID, guid, enc, encSize):
	import os, pickle
	
	rec = [feedID, guid, enc, encSize]
	cacheFile = iPXSettings.rssPath + 'qmcache.dat'
	cacheData = []
	
	if os.path.isfile(cacheFile):
		cacheData = pickle.load(open(cacheFile))
		cacheData.append(rec)
		pickle.dump(cacheData, open(cacheFile,'w'))
	else:
		cacheData = [rec]
		pickle.dump(cacheData, open(cacheFile,'w'))

def remCacheItem(enc):
	import os, pickle
	
	cacheFile = iPXSettings.rssPath + 'qmcache.dat'
	checkDir(iPXSettings.tempDir + '/iPXCache/')
	dailyCacheFile = iPXSettings.tempDir + '/iPXCache/qmcache-' + strftime('%m%d%Y',localtime()) + '.dat'
	
	if os.path.isfile(cacheFile):
		cacheData = pickle.load(open(cacheFile))
		delList = []
		enc = enc.replace(iPXSettings.downloadDirectory + '/', '')
		for rec in cacheData:
			if enc in rec[2]:
				delList.append(rec)
				logIt('Removing qmcache.dat record: ' + str(rec))
		
		for rec in delList:
			cacheData.remove(rec)
		pickle.dump(cacheData, open(cacheFile,'w'))
	
	if os.path.isfile(dailyCacheFile):
		delList = []
		dailyCacheItems = pickle.load(open(dailyCacheFile))
		for item in dailyCacheItems:
			if enc in item:
				delList.append(item)
		
		for rec in delList:
			dailyCacheItems.remove(rec)
		pickle.dump(dailyCacheItems, open(dailyCacheFile,'w'))

def readQuotaCache():
	import os, pickle
	
	cacheFile = iPXSettings.rssPath + 'qmcache.dat'
	cacheData = []
	
	if os.path.isfile(cacheFile):
		cacheData = pickle.load(open(cacheFile))
	
	return cacheData

def getAllowedDelList():
	import os, iPXClass, pickle
	
	sizeTaken = 0
	allowList = []
	tempAllowList = {}
	finalAllowList = []
	checkDir(iPXSettings.tempDir + '/iPXCache/')
	dailyCacheFie = iPXSettings.tempDir + '/iPXCache/qmcache-' + strftime('%m%d%Y',localtime()) + '.dat'
	
	if not os.path.isfile(iPXSettings.rssPath + 'qmcache.dat'):
		qmCacheCreate()
	if not os.path.isfile(dailyCacheFie):
		cacheList = readQuotaCache()
		logIt('Number of items in qmcache.dat file: ' + str(len(cacheList)))
		
		feedsObj = iPXClass.Feeds()
		for cacheItem in cacheList:
			sizeTaken = sizeTaken + int(cacheItem[3])
			hashFile = iPXSettings.rssPath + 'feedData/' + cacheItem[0] + '.ipxd'
			if os.path.isfile(hashFile):
				try:
					EntriesData = plistlib.Plist.fromFile(file(hashFile))
					if EntreiesData.has_key('entries'):
						for entry in EntriesData['entries']:
							if entry.guid == cacheItem[1]:
								if entry.has_key('read'):
									if entry['read'] == True:
										if entry.has_key('flagged'):
											if entry['flagged'] == False:
												allowList.append(cacheItem[2])
										else:
											allowList.append(cacheItem[2])
								else:
									allowList.append(cacheItem[2])
				except Exception, msg:
					logIt('getAllowedDelList failed...')
					logIt('ERRMSG: ' + str(msg))
			else:
				allowList.append(cacheItem[2])
		pickle.dump(allowList, open(dailyCacheFie,'w'))
	else:
		allowList = pickle.load(open(dailyCacheFie))
	
	counter = 0
	for enc in allowList:
		encFile = os.path.join(iPXSettings.downloadDirectory + '/' + enc)
		if os.path.isfile(encFile):
			fileCreateTime = os.path.getmtime(encFile)
			tempAllowList[fileCreateTime + counter] = encFile
			sizeTaken = sizeTaken + os.path.getsize(encFile)
			counter = counter + 1
		else:
			remCacheItem(enc)
	
	keys = tempAllowList.keys()
	keys.sort()
	for key in keys:
		finalAllowList.append(tempAllowList[key])
	
	logIt('Number of items in final del allow list: ' + str(len(finalAllowList)))
	
	return finalAllowList, sizeTaken

def getToQuota(size):
	import os, shutil, sys
	
	if size == '':
		size = 0
	elif size == None:
		size = 0
	elif type(size) == str:
		try:
			size = long(size)
		except Exception, msg:
			logIt('getToQuota size error: ' + str(msg))
			logIt('Size = ' + str(size))
			size = 0
	elif size < 0:
		raise OutOfRangeError, 'number out of range (must be positive whole number)'
	
	if len(iPXSettings.delList) > 0:
		plusSize = 1048576 * 50
		size = long(size) + plusSize
		
		logIt('QM: ' + str(iPXSettings.quotaSize))
		logIt('QM: ' + str(size))
		logIt('QM: =============')
		logIt('QM: ' + str(iPXSettings.quotaSize - (iPXSettings.quotaSizeTaken + size)))
		
		if (iPXSettings.quotaSize - (iPXSettings.quotaSizeTaken + size)) <= 0:
			for enc in iPXSettings.delList:
				if os.path.isfile(enc):
					logIt('QM: Removing "' + enc + '"')
					remCacheItem(enc)
					iPXSettings.quotaSizeTaken = iPXSettings.quotaSizeTaken - long(os.path.getsize(enc))
					logIt('quotaSizeTaken: ' + str(iPXSettings.quotaSizeTaken))
					if sys.platform == 'darwin':
						encNameSplit = enc.split('/')
						encName = encNameSplit[len(encNameSplit)-1]
						shutil.move(enc, iPXSettings.userPath + '/.Trash/' + encName)
					elif sys.platform == 'win32':
						os.unlink(enc)
				if (iPXSettings.quotaSize - (iPXSettings.quotaSizeTaken + size)) > 0:
					iPXSettings.quotaSizeTaken = iPXSettings.quotaSizeTaken + size
					break
	else:
		logIt('QM: delList holds ' + str(len(iPXSettings.delList)) + ' files, no action taken')

def main():
	import sys
		
	iPXSettings.initSettings()
	iPXSettings.DEBUG = 1
	iPXSettings.delList, iPXSettings.sizeTaken = getAllowedDelList()
	
	size = 0
	
	try:
		size = sys.argv[1]
	except:
		printMSG('Please secify a whole number ')
		sys.exit(0)
	
	try:
		size = long(size)
	except Exception, msg:
		printMSG('Invalid param "' + str(size) + '", please use a whole number')
		sys.exit(0)
	
	getToQuota(size)
	
if __name__ == '__main__':
	main()