# -*- coding: utf-8 -*- 
#~ EditorCore v0.4.1 (13.06.08) by bystrousak@seznam.cz.
#~ Created in UliPad IDE.
#~
#~ Comments:
    #~ Thanks to google translator ;D

import cherrypy, os, shelve
import creole.creole2html as creole2html

## Definition of the functions
def ReturnFile(what):
    "Returns content of %what%."

    try:
        file= open(str(what), "r")
        data= file.read()
        file.close()

        return data
    except:
        return "Error! Failed to open "+ str(what)




## Definition of the objects
class Core(object):
    "Main core."

    def __init__(self):
        self.statusbar= "Ready"
        self.tree= ""
        self.project= ""
        self.projectlist= ""

        file= open("EditorSystem/head.html", "r")
        self.head= file.read()
        file.close()

    def index(self):
        "Returns main GUI page - EditorGUI.html."
        
        return ReturnFile("EditorSystem/GUI/EditorGUI.html")
    index.exposed = True

    def css(self):
        "Returns CSS file - Style.css."
        
        return ReturnFile("EditorSystem/GUI/Style.css")
    css.exposed = True

    def script(self):
        "Returns JavaScript file - Script.js."
        
        return ReturnFile("EditorSystem/GUI/Script.js")
    script.exposed = True

    def getprojects(self):
        "Returns names of the available projects (directories in /content)."
        
        self.projectlist= ""

        for i in os.listdir("content/"):
            if os.path.isdir("content/"+ i):
                self.projectlist+= "<input type= \"submit\" value=\""+ i+ "\" name= \"OpenProject"+ i
                self.projectlist+= "\" onclick= \"ShowHideById('OpenProjectWindow', 'hide'); OpenProject('"+ i+ "');\"><br>\n"
                
        return self.projectlist
    getprojects.exposed= True

    def getprojectsfordel(self):
        "Returns names of the available projects (directories in /content)."
        
        self.projectlistfordel= ""

        for i in os.listdir("content/"):
            if os.path.isdir("content/"+ i):
                self.projectlistfordel+= "<input type= \"submit\" value=\""+ i+ "\" name= \"DeleteProject"+ i
                self.projectlistfordel+= "\" onclick= \"ShowHideById('DeleteProjectWindow', 'hide'); DeleteProject('"+ i+ "');\"><br>\n"
                
        return self.projectlistfordel
    getprojectsfordel.exposed= True

    def getprojecttree(self, project_name= ""):
        "Returns tree of %project_name%."

        self.project= project_name

        self.tree= ""

        self.tree+= "<div id= \"Directory\">"
        self.tree+= "<div id= \"DirectoryName\" onclick= \"ShowHideByAttribute('name', 'DirectoryContent"+ project_name
        self.tree+= "', 'hide');\"><img src= \"img/dir_ico\"> "+ project_name+ "</div>\n"
        self.tree+= "<div id= \"DirectoryContent\" name= \"DirectoryContent"+ project_name+ "\">\n"
        self.Add("content/"+ project_name)
        self.tree+= "</div>\n</div>"
        
        return self.tree
    getprojecttree.exposed= True

    def gethtml(self, page):
        "Returns content of %page% (html source)."

        return creole2html.c2w(ReturnFile("content/"+ page))
    gethtml.exposed= True

    def getsource(self, page= "index.txt"):
        "Returns content of %page% (creole source)."
        
        return ReturnFile("content/"+ page)
    getsource.exposed= True

    def createproject(self, name):
        "In content/%name% and export/%name% create new directory with index file."

        os.mkdir("content/"+ name)
        file= open("content/"+ name+ "/index.txt", "w")
        file.write(ReturnFile("EditorSystem/index.txt"))
        file.close()

        os.mkdir("export/"+ name)
        file= open("export/"+ name+ "/index.html", "w")
        file.write(ReturnFile("EditorSystem/index.html"))
        file.close()

        return "Created"
    createproject.exposed= True

    def createpage(self, name):
        "Create new page with name %name% in project %self.project%."

        file= open("content/"+ self.project+ "/"+ name+ ".txt", "w")
        file.write("")
        file.close()
        
        return "Created"
    createpage.exposed= True

    def savepage(self, name, content= ""):
        "Save source to content/%self.project% and generate output to export/%self.project% directory."

        file= open("content/"+ name, "w")
        file.write(content)
        file.close()
        
        file= open("export/"+ name[:-3]+ "html", "w")
        file.write(self.head+ creole2html.c2h(content)+ "</body></html>")
        file.close()

        return "Saved!"
    savepage.exposed= True

    def saveproject(self):
        file= open("EditorSystem/ProjectNameConfig.cfg", "w")
        file.write(str(self.project))
        file.close()
        
        return "Saved!"
    saveproject.exposed= True

    def loadproject(self):
        file= open("EditorSystem/ProjectNameConfig.cfg", "r")
        self.project= file.read()
        file.close()

        return self.project
    loadproject.exposed= True

    def deletepage(self, name):
        try:
            os.remove("content/"+ self.project+ "/"+ name+ ".txt")
            os.remove("export/"+ self.project+ "/"+ name+ ".html")
            
            return "Deleted!"
        except:
            return "Failed! <script>alert('Failed!');</script>"
    deletepage.exposed= True

    def deleteproject(self, name):
        self.DeleteTree("content/"+ name)
        self.DeleteTree("export/"+ name)
        
        return "Deleted!"
    deleteproject.exposed= True

    def Add(self, path): 
        "Recursively through the directory structure and generates html tree."
        
        for i in os.listdir(path):
            if os.path.isfile(path+ "/"+ i):
                self.tree+= "<div id= \"File\" onclick= \"OpenPage('"+ path[8:]+ "/"+ i+ "');\"><img src= \"img/file_ico\"> "+ i[:-4]+ "</div>\n"
                    
            elif os.path.isdir(path):
                self.tree+= """<div id= "Directory">"""
                self.tree+= "<div id= \"DirectoryName\" onclick= \"ShowHideByAttribute('name', 'DirectoryContent"+ path[8:]+ i
                self.tree+= "', 'hide');\"><img src= \"img/dir_ico\"> "+ i+ "</div>\n"
                self.tree+= "<div id= \"DirectoryContent\" name= \"DirectoryContent"+ path[8:]+ i+ "\">\n"
                self.Add(path+ "/"+ i)
                self.tree+= "</div>\n</div>"

    def DeleteTree(self, path): 
        "Recursively through the directory structure and delete all files."
        
        for i in os.listdir(path):
            if os.path.isfile(path+ "/"+ i):
                os.remove(path+ "/"+ i)
                    
            elif os.path.isdir(path):
                self.Add(path+ "/"+ i)

        os.rmdir(path)

class Img(object):
    "Class for images."

    def dir_ico(self):
        "Returns small directory ico."
        return ReturnFile("EditorSystem/Img/dir_ico.bmp")
    dir_ico.exposed= True

    def file_ico(self):
        "Returns small directory ico."
        return ReturnFile("EditorSystem/Img/file_ico.bmp")
    file_ico.exposed= True


## Main program
System= Core
System.img= Img()

cherrypy.quickstart(System(), '/', "EditorSystem/Config.cfg")







