import os

#################################################################################
#       .desktop file parser			                                #
#       Copyright (C) 2005  Michael Sheldon                                     #
#                                                                               #
#       This program is free software; you can redistribute it and/or           #
#       modify it under the terms of the GNU General Public License             #
#       as published by the Free Software Foundation; either version 2          #
#       of the License, or (at your option) any later version.                  #
#                                                                               #
#       This program is distributed in the hope that it will be useful,         #
#       but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#       GNU General Public License for more details.                            #
#                                                                               #
#       You should have received a copy of the GNU General Public License       #
#       along with this program; if not, write to the Free Software             #
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,              #
#       MA  02110-1301, USA                                                     #
#################################################################################


class Item:

	def __init__(self):
		self.itemDict = {}

	def get(self, key):
		try:
			return self.itemDict[key]
		except:
			return None

class DeskItem(Item):
	
	def __init__(self, filename):
		self.itemDict = {}
		self.load(filename)

	def load(self, filename):
		file = open(filename)
		fileList = file.readlines()
		for item in fileList:
			if item.find("=") != -1:
				item = item.replace("\n","")
				key, content = item.split("=", 1)
				key = key.lower()
				self.itemDict[key] = content

	def clicked(self, event):
		#Run program with parameters
		prog = self.get("exec")
		params = prog.split(" ")
		if len(params) > 1:
			prog = params[0]
		else:
			params = [prog]
		os.spawnvp(os.P_NOWAIT, prog, params)


class DirItem(Item):

	def __init__(self, dirname, app):
		#remove last slash
		slashpos = dirname.find("/")
		name = dirname
		if slashpos == len(name) - 1:
			name = name[slashpos + 1:]
		name = name.split("/")
		name = name[len(name) - 1] #Just show highest directory name
		self.itemDict = { "dirname": dirname, "name" : name, "icon" : dirname + "/dirimage.png" }
		if name[len(name) - 2:] == "..":	#Back button
			self.itemDict = { "dirname" : dirname, "name" : "Back", "icon" : "back.png" }
		self.app = app

	def clicked(self, button):
		dirname = self.get("dirname")
		dirs = dirname.split("/")
		last = len(dirs) - 1
		cleandir = ""
		if dirs[last] == "..":
			del dirs[last - 1:]
		for dir in dirs:
			cleandir += dir + "/"
		self.itemDict["dirname"] = cleandir
		cleandir = cleandir[:len(cleandir) - 1]
		self.app.select_category(button, self)

