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

import os, sys
from iPXTools import *
from time import *

LITE		= 0

progName = 'iPodderX'

#defaults
VERSION		= 'Version 3.1 Build: 35 [%s] ' % sys.platform
USER_AGENT	= '<unknown>'
environs = os.environ
userPath = ''
#pList = ''
logPath = ''
logFile = ''
rssPath = ''
__timeBombFile = ''
defaultDir = ''
tempDir = ''
tmpDownloadDir = ''
historyFile			= ''
newHistoryFile		= ''
__userName 			= ''
pList				= ''
feedDetails			= []
totalBTFileSize     = 0.0
ranFromUI			= False
Prefs = []
ranQuotaTest		= False
histGUIDs = []
ranHistCheck = False
feedFile = ''
FeedListPrefs = {'iPodderXFeeds':{}}
historyURLs			= []
encGUIDs            = []
DEBUG = 0
SUPERDEBUG = 0
lastDLStepSize = 5
showExplicit = True
anonFeedback = True
onlyAudio = 0
moveAudio = 1
moveVideo = 1
moveImages = 1
deleteAudio = 0
deleteVideo	= 0
deleteImages = 0
torrentFiles = 1
torrentMinDownRate = 3000
torrentMaxBeatTime = 1000
torrentMaxUpRate = 80
torrentMinPort = 6881
torrentMaxPort = 6889
maxEntryAge = 3
organizeDownloads	= 0
quotaEnabled = 0
quotaSizeTaken = 0
quotaSize = 0
d = defaultDir
downloadDirectory = d
proxyServer = ''
proxyPort = ''
proxyUsername = ''
proxyPassword = ''
useProxyServer = 0
useProxyIE = 0
useProxyAuth = 0
globalProxySetting = ''
	

delList =[]

#set default
progName = 'iPodderX'

def initSettings():
	globals()['USER_AGENT']	= '%s/%s (http://slakinski.com)' % (progName, VERSION)
	
	globals()['userPath'] = os.path.expanduser("~")
	if sys.platform == 'darwin':
		globals()['pList'] = '%s/Library/Preferences/com.thunderstonemedia.%s.plist' % (userPath, progName)
		globals()['logPath'] = '%s/Library/Logs/' % userPath
		globals()['logFile'] = '%s%s.log' % (logPath, progName)
		globals()['rssPath'] = '%s/Library/Application Support/%s/' % (userPath, progName)
		globals()['__timeBombFile'] = '%s/Library/foo.txt' % userPath
		globals()['defaultDir'] = '%s/Documents/%s' % (userPath, progName)
		globals()['tempDir'] = '/tmp/'
		globals()['tmpDownloadDir'] = '%s/%sDownloads/' % (tempDir, progName)
		globals()['historyFile'] = '%shistory.plist' % rssPath
		globals()['newHistoryFile'] = '%shistory.dat' % rssPath
		globals()['__userName'] = globals()['userPath'].split('/')[len(globals()['userPath'].split('\\'))-1]
	elif sys.platform == 'win32':
		globals()['logPath'] = '%s\\Application Data\\%s\\' % (userPath, progName)
		globals()['logFile'] = '%s%s.log' % (logPath, progName)
		globals()['rssPath'] = '%s\\Application Data\\%s\\' % (userPath, progName)
		globals()['pList'] = '%siPXSettings.plist' % rssPath
		globals()['__timeBombFile'] = '%s\\System32\\foo.txt' % environs['WINDIR']
		globals()['defaultDir'] = '%s\\My Documents\\%s Downloads\\' % (userPath, progName)
		globals()['tempDir'] = '%s\\' % environs['TEMP']
		globals()['tmpDownloadDir']	= '%s\\%sDownloads\\' % (tempDir, progName)
		globals()['historyFile'] = '%s\\history.plist' % rssPath
		globals()['newHistoryFile']	= '%s\\history.dat' % rssPath
		globals()['__userName'] = globals()['userPath'].split('\\')[len(globals()['userPath'].split('\\'))-1]
		
		globals()['pList'] = '%siPXSettings.plist' % rssPath
	
	globals()['__userKey'] = __userName.encode('rot-13')
	if len(__userKey) < 8:
		counter =  8 - len(__userKey)
		for x in range(counter):
			globals()['__userKey'] = '%s!' % __userKey
	
	globals()['__DESKey'] = '%s3sE6$!&4' % __userKey[:8]
	
	globals()['Prefs'] = readplist(pList)
	
	globals()['feedFile'] = '%sfeeds.plist' % rssPath
	
	for i in range(10):
		if os.path.isfile(feedFile):
			if len(FeedListPrefs['iPodderXFeeds']) <=  0:
				globals()['FeedListPrefs'] = readplist(feedFile)
			else:
				break

	if Prefs.has_key('pyDebug'):
		globals()['DEBUG'] = int(Prefs['pyDebug'])
		
	if Prefs.has_key('lastDLStepSize'):
		globals()['lastDLStepSize'] = int(Prefs['lastDLStepSize'])
	
	if Prefs.has_key('showExplict'):
		globals()['showExplicit'] = Prefs['showExplicit']
	
	if Prefs.has_key('anonFeedback'):
		globals()['anonFeedback'] = int(Prefs['anonFeedback'])
	
	if Prefs.has_key('onlyAudio'):
		globals()['onlyAudio'] = int(Prefs['onlyAudio'])
	
	if Prefs.has_key('moveAudio'):
		globals()['moveAudio'] = int(Prefs['moveAudio'])
	
	if Prefs.has_key('moveVideo'):
		globals()['moveVideo'] = int(Prefs['moveVideo'])
	
	if Prefs.has_key('moveImages'):
		globals()['moveImages'] = int(Prefs['moveImages'])
	
	if Prefs.has_key('deleteAudio'):
		globals()['deleteAudio'] = int(Prefs['deleteAudio'])
	
	if Prefs.has_key('deleteVideo'):
		globals()['deleteVideo']	= int(Prefs['deleteVideo'])
	
	if Prefs.has_key('deleteImages'):
		globals()['deleteImages'] = int(Prefs['deleteImages'])
	
	if Prefs.has_key('torrentFiles'):
		globals()['torrentFiles'] = int(Prefs['torrentFiles'])
	
	if Prefs.has_key('torrentMinDownRate'):
		globals()['torrentMinDownRate'] = Prefs['torrentMinDownRate']
	
	if Prefs.has_key('torrentMaxBeatTime'):
		globals()['torrentMaxBeatTime'] = Prefs['torrentMaxBeatTime']
	
	if Prefs.has_key('torrentMaxBeatTime'):
		globals()['torrentMaxBeatTime'] = Prefs['torrentMaxBeatTime']
	
	if Prefs.has_key('torrentMinPort'):
		globals()['torrentMinPort'] = Prefs['torrentMinPort']
	
	if Prefs.has_key('torrentMaxPort'):
		globals()['torrentMaxPort'] = Prefs['torrentMaxPort']
	
	if Prefs.has_key('maxEntryAge'):
		globals()['maxEntryAge'] = Prefs['maxEntryAge']
	
	if Prefs.has_key('organizeDownloads'):
		globals()['organizeDownloads'] = int(Prefs['organizeDownloads'])
	
	if Prefs.has_key('quotaEnabled'):
		globals()['quotaEnabled'] = int(Prefs['quotaEnabled'])
	
	if Prefs.has_key('useProxyServer'):
		globals()['useProxyServer'] = int(Prefs['useProxyServer'])
	
	if Prefs.has_key('useProxyIE'):
		globals()['useProxyIE'] = int(Prefs['useProxyIE'])
	
	if Prefs.has_key('useProxyAuth'):
		globals()['useProxyAuth'] = int(Prefs['useProxyAuth'])
	
	if Prefs.has_key('proxyServer'):
		globals()['proxyServer'] = Prefs['proxyServer']
	
	if Prefs.has_key('proxyPort'):
		globals()['proxyPort'] = Prefs['proxyPort']
	
	if Prefs.has_key('proxyUsername'):
		globals()['proxyUsername'] = Prefs['proxyUsername']
	
	if Prefs.has_key('proxyPassword'):
		globals()['proxyPassword'] = Prefs['proxyPassword']
	
	if Prefs.has_key('quotaSize'):
		globals()['quotaSize'] = float(Prefs['quotaSize'])
		#convert quotaSize to bytes
		globals()['quotaSize'] = quotaSize * 1073741824
	
	if Prefs.has_key('downloadDirectory'):
		globals()['d'] = Prefs['downloadDirectory']
	if (globals()['d'] == ''):
		globals()['d'] = defaultDir
	globals()['d'] = d.replace('~', userPath)
	globals()['downloadDirectory'] = globals()['d']
	
	globals()['delList'] =[]

def checkDir(dir):
	if not os.path.isdir(dir):
		os.mkdir(dir)

def getVar(var):
	if var == '3DESKey':
		return __DESKey
	elif var == 'timeBomb':
		return __timeBombFile

def setVar(var):
	return