Add manifest to manifest
[gpytage.git] / subfile.py
blobdc7f3cc75b4fb887aca0f24816ca75c2d0f638f1
1 #!/usr/bin/env python
3 # GPytage subfile module
5 ############################################################################
6 # Copyright (C) 2008 by Kenneth Prugh #
7 # ken69267@gmail.com #
8 # #
9 # This program is free software; you can redistribute it and#or modify #
10 # it under the terms of the GNU General Public License as published by #
11 # the Free Software Foundation under version 2 of the license. #
12 # #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
17 # #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program; if not, write to the #
20 # Free Software Foundation, Inc., #
21 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
22 ############################################################################
24 import pygtk; pygtk.require("2.0")
25 import gtk
26 import gtk.glade
27 import datastore
28 from window import title, unsavedDialog, window
29 from save import SaveFile
30 from helper import folder_scan, folder_walk
31 from config import get_config_path, config_files
33 def new(window):
34 """ Spawn the new subfile dialog """
35 gladefile = "glade/newsubfile.glade"
36 wTree = gtk.glade.XML(gladefile)
37 newd = wTree.get_widget("newfile")
38 dirs,files = folder_scan()
39 cb = wTree.get_widget("ncb")
41 model = gtk.ListStore(str)
42 cb.set_model(model)
43 cell = gtk.CellRendererText()
44 cb.pack_start(cell)
45 cb.add_attribute(cell, 'text', 0)
47 for i in dirs:
48 cb.append_text(i)
50 cb.set_active(0)
52 ftext = wTree.get_widget("aentry")
54 addb = wTree.get_widget("addb")
55 closeb = wTree.get_widget("closeb")
57 addb.connect("clicked", add_subfile, cb, ftext, newd, window)
58 closeb.connect("clicked", close_subfile, newd)
60 if dirs == []:
61 sbar = wTree.get_widget("sbar")
62 smsg = sbar.get_context_id("standard message")
63 sbar.pop(smsg)
64 sbar.push(smsg, "Error: No parent directories found")
65 sbar.show()
67 newd.show_all()
68 newd.run()
70 def close_subfile(arg, newd):
71 """ Close subfile dialog """
72 newd.hide()
74 def add_subfile(arg, cb, ftext, newd, window):
75 model = cb.get_model()
76 index = cb.get_active()
77 if index >= 0: # prevent index errors
78 # next line gets an index error when trying to add a subfile to a non existent sets dir. (Or if selection is blank)
79 cbselection = model[index][0] #current selection
80 ftextselection = ftext.get_text()
81 Success = False
82 if len(ftextselection):
83 addToMemory(cbselection, ftextselection)
84 newd.hide()
86 def addToMemory(parent, filename):
87 """ Adds new subfile to memory """
88 datastore.datastore.foreach(findMatch, [parent, filename])
89 msg= '#This file was created by GPytage'
90 datastore.lists[filename] = gtk.ListStore(str, str, bool, str)
91 datastore.lists[filename].append([msg, None, True, parent]) #rightpanel stuff
92 title("* GPytage")
94 def findMatch(model, path, iter, user_data):
95 print user_data[0], user_data[1]
96 print model.get_value(iter, 0).strip('*')
97 if model.get_value(iter, 0).strip('*') == user_data[0]:
98 edited_file = "*%s" % user_data[1]
99 model.append(iter, [edited_file, None, False, user_data[0]])
101 def convert(window):
102 """ Spawn the convert file dialog """
103 gladefile = "glade/convertfile.glade"
104 wTree = gtk.glade.XML(gladefile)
105 convertd = wTree.get_widget("convertd")
107 dirs,files = folder_scan()
108 cb = wTree.get_widget("ncb")
110 model = gtk.ListStore(str)
111 cb.set_model(model)
112 cell = gtk.CellRendererText()
113 cb.pack_start(cell)
114 cb.add_attribute(cell, 'text', 0)
116 for i in files:
117 cb.append_text(i)
118 cb.set_active(0)
120 ftext = wTree.get_widget("aentry")
122 convertb = wTree.get_widget("convertb")
123 closeb = wTree.get_widget("closeb")
125 convertb.connect("clicked", convertFile, cb, ftext, convertd, window)
126 closeb.connect("clicked", close_subfile, convertd)
128 if files == []:
129 sbar = wTree.get_widget("sbar")
130 smsg = sbar.get_context_id("standard message")
131 sbar.pop(smsg)
132 sbar.push(smsg, "Error: No files detected")
133 sbar.show()
135 convertd.show_all()
136 convertd.run()
138 def convertFile(arg, cb, ftext, convertd, window):
139 """ Convert Top level file to directory with subfile """
140 #Currently I don't see how to do such a change "in memory", so the change must be done live and probably call the evil reload() nuke
141 model = cb.get_model()
142 index = cb.get_active()
143 if index >= 0: # prevent index errors
144 cbselection = model[index][0] #current selected item
145 ftextselection = ftext.get_text()
146 if window.get_title() != "GPytage":
147 status, uD = unsavedDialog()
148 if status == -8:
149 uD.hide()
150 elif status == 1:
151 SaveFile().save()
152 uD.hide()
153 else:
154 uD.hide()
155 return
156 if len(ftextselection):
157 #create an old file
158 nfile = ftextselection
159 from shutil import move
160 from os import mkdir
161 from config import get_config_path
162 from helper import reload
163 pconfig = get_config_path()
164 move(pconfig+cbselection, pconfig+nfile) #rename the file
165 mkdir(pconfig+cbselection) #create the parent directory
166 move(pconfig+nfile, "%s/%s" %(pconfig+cbselection,nfile))
167 reload() #sigh
168 convertd.hide()
170 def delete(window):
171 """ Spawn the delete subfile dialog """
172 gladefile = "glade/deletefile.glade"
173 wTree = gtk.glade.XML(gladefile)
174 deld = wTree.get_widget("deld")
176 dirs,files = folder_scan()
178 cb = wTree.get_widget("ncb")
180 model = gtk.ListStore(str)
181 cb.set_model(model)
182 cell = gtk.CellRendererText()
183 cb.pack_start(cell)
184 cb.add_attribute(cell, 'text', 0)
186 subfiles = []
187 for i in dirs:
188 data = folder_walk(i)
189 for i in data:
190 subfiles.append(i)
192 for i in subfiles:
193 cb.append_text(i)
194 cb.set_active(0)
196 if subfiles == []:
197 sbar = wTree.get_widget("sbar")
198 smsg = sbar.get_context_id("standard message")
199 sbar.pop(smsg)
200 sbar.push(smsg, "Error: No files detected")
201 sbar.show()
203 remb = wTree.get_widget("delb")
204 closeb = wTree.get_widget("closeb")
206 remb.connect("clicked", deleteFile, cb, deld, window)
207 closeb.connect("clicked", close_subfile, deld)
209 deld.show_all()
210 deld.run()
212 def deleteFile(arg, cb, deld, window):
213 """ Delete subfile """
214 model = cb.get_model()
215 index = cb.get_active()
216 if index >= 0: # prevent index errors
217 if window.get_title() != "GPytage":
218 status, uD = unsavedDialog()
219 if status == -8:
220 uD.hide()
221 elif status == 1:
222 SaveFile().save()
223 uD.hide()
224 else:
225 uD.hide()
226 return
227 from os import remove
228 from config import get_config_path
229 from helper import reload
230 pconfig = get_config_path() # /
232 global ddata
233 ddata = None
234 def findMatch(model, path, iter, user_data):
235 """ Get path, iter for the file to be deleted """
236 if model.get_value(iter, 0).strip('*') == user_data[0]:
237 global ddata
238 ddata = [model, path, iter]
239 return True
240 datastore.datastore.foreach(findMatch, [model[index][0]])
241 if ddata:
242 model = ddata[0]
243 path = ddata[1]
244 iter = ddata[2]
245 filePath = pconfig+model.get_value(iter, 3)+'/'+model.get_value(iter, 0).strip('*')
246 remove(filePath)
247 print "deleteFILE: %s DELETED" % filePath
248 reload()
249 deld.hide()