import fixes
[enigma2-plugins.git] / webinterface / src / WebChilds / Notepad.py
1 from os import path as os_path, \
2         listdir as os_listdir, \
3         system as os_system, \
4         stat as os_stat, \
5         mkdir as os_mkdir
6 from twisted.web2 import resource, responsecode, http,http_headers
7
8 class NotepadResource(resource.Resource):
9     DIR = "/etc/enigma2/notes/"
10     
11     def render(self, req):
12         self.args = req.args
13         
14         self.format = "xml"
15         if self.getArg("format"):
16             if self.getArg("format") == "html":
17                 self.format = "html"
18         
19         if self.getArg("show"):
20             return self.do_show(self.getArg("show"))
21         elif self.getArg("save"):
22             return self.do_save()
23         elif self.getArg("create"):
24             return self.do_create()
25         else:
26             return self.do_index()
27             
28     def send(self,content):
29         if self.format == "xml":
30             ctype = http_headers.MimeType('application', 'xhtml+xml', (('charset', 'UTF-8'),))
31         else:
32             ctype = http_headers.MimeType('text', 'html', (('charset', 'UTF-8'),))
33         return http.Response(responsecode.OK,{'content-type': ctype},content)                
34
35     def do_index(self):
36         content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
37         content += "<e2noteslist>\n"
38         for note in self.getNotes():
39             content += "<e2note>\n"
40             content += "<e2notename>%s</e2notename>\n"%note[0]
41             content += "<e2notesize>%s</e2notesize>\n"%note[1]
42             content += "<e2notemtime>%s</e2notemtime>\n"%note[2]
43             content += "<e2notectime>%s</e2notectime>\n"%note[3]
44             content += "</e2note>\n"
45         content += "</e2noteslist>"
46         return self.send(content)
47
48     def do_show(self,filename,was_saved=False,oldfilename=False):
49
50         if os_path.exists(self.DIR+filename) is not True:
51             return self.errorFileNotFound(self.DIR+filename)
52         else:    
53             details = self.getNotesDetails(filename)
54             fp = open(self.DIR+filename)
55             con = fp.read()
56             fp.close()
57             content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
58             content += "<e2notedetails>\n"
59             if was_saved:
60                 content += "<e2notesaved>True</e2notesaved>\n"
61             if oldfilename is not False:
62                 content += "<e2notenameold>%s</e2notenameold>\n"%oldfilename
63                     
64             content += "<e2notename>%s</e2notename>\n"%details[0]
65             content += "<e2notesize>%s</e2notesize>\n"%details[1]
66             content += "<e2notemtime>%s</e2notemtime>\n"%details[2]
67             content += "<e2notectime>%s</e2notectime>\n"%details[3]
68             content += "<e2notecontent>%s</e2notecontent>\n"%con
69             content += "</e2notedetails>"
70         return self.send(content)
71     
72     def do_save(self):
73         filename = self.getArg("save")
74         filenamenew = self.getArg("namenew")
75         content = self.getArg("content")
76         if os_path.exists(self.DIR+filename) is not True:
77             return self.errorFileNotFound(self.DIR+self.getArg("save"))
78         else:  
79             newname = False  
80             if filename != filenamenew:
81                 os_system("mv '%s' '%s' " %(self.DIR+filename,self.DIR+filenamenew))
82                 newname = filename
83                 filename = filenamenew
84                 
85             fp = open(self.DIR+filename,"w")
86             fp.write(content)
87             fp.close()
88             return self.do_show(filename,was_saved=True,oldfilename=newname)
89     def do_create(self):
90         import random
91         r = str(random.randrange(100000,999999))
92         fp = open(self.DIR+'note_'+r,"w")
93         fp.write("")
94         fp.close()
95         return self.send("file created")
96
97     def getNotes(self):
98         list = []
99         if self.check_dir(force_create=True):
100             for i in os_listdir(self.DIR):
101                 list.append(self.getNotesDetails(i))
102         return list
103
104     def getNotesDetails(self,file):
105         fstat = os_stat(self.DIR+file)
106         size = fstat[-4]
107         mtime = fstat[-3]
108         ctime = fstat[-1]
109         return (file,size,mtime,ctime)
110     
111     def check_dir(self,force_create=False):
112         if os_path.isdir(self.DIR):
113             return True
114         elif os_path.isdir(self.DIR) is False and force_create is True:
115             return os_mkdir(self.DIR)
116         else:
117             return False
118     
119     
120     def getArg(self,key):
121         if self.args.has_key(key):
122             return self.args[key][0]
123         else:
124             return False
125
126     def errorFileNotFound(self,filename):
127         content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"        
128         content += "<e2note>\n"
129         content += "<e2result>False</e2result>\n"
130         content += "<e2resulttext>File '%s' not found</e2resulttext>\n" % (filename)
131         content += "</e2note>\n"
132         return self.send(content)