1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import os.path |
---|
6 | import signal |
---|
7 | import gi |
---|
8 | gi.require_version('Gtk','3.0') |
---|
9 | from gi.repository import Gtk, Gdk,GObject,GLib |
---|
10 | from subprocess import call |
---|
11 | import subprocess |
---|
12 | import locale |
---|
13 | import shutil |
---|
14 | import gettext |
---|
15 | |
---|
16 | _=gettext.gettext |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | gettext.textdomain('lliurex-easy-sites') |
---|
21 | |
---|
22 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
23 | |
---|
24 | class LliurexEasySites: |
---|
25 | |
---|
26 | EASY_SITES_GLADE="/usr/share/lliurex-easy-sites/rsrc/lliurex-easy-sites.glade" |
---|
27 | EASY_SITES_GATHERER_GLADE="/usr/share/lliurex-easy-sites/rsrc/lliurex-easy-sites-gatherer.glade" |
---|
28 | |
---|
29 | def __init__(self): |
---|
30 | |
---|
31 | self.easy_sites={} |
---|
32 | self.button_counter=0 |
---|
33 | self.scroll_me=False |
---|
34 | |
---|
35 | #def init |
---|
36 | |
---|
37 | |
---|
38 | def close_window(self,widget): |
---|
39 | |
---|
40 | Gtk.main_quit() |
---|
41 | sys.exit(0) |
---|
42 | |
---|
43 | #def close_window |
---|
44 | |
---|
45 | |
---|
46 | def gatherer_close_window(self,widget): |
---|
47 | |
---|
48 | self.windowGatherer.destroy() |
---|
49 | |
---|
50 | #def close_window |
---|
51 | |
---|
52 | |
---|
53 | def gatherer_add_site(self,widget): |
---|
54 | |
---|
55 | |
---|
56 | print(self.title.text) |
---|
57 | |
---|
58 | #def gatherer_add_site(self,widget) |
---|
59 | |
---|
60 | |
---|
61 | def easy_sites_click(self,widget,id): |
---|
62 | |
---|
63 | print self.easy_sites[id] |
---|
64 | self.delete_button(id) |
---|
65 | |
---|
66 | #def easy_sites_click |
---|
67 | |
---|
68 | |
---|
69 | def addButtonSite(self,widget,easy_site=None): |
---|
70 | |
---|
71 | self.build_gatherer() |
---|
72 | |
---|
73 | #def addButtonSite |
---|
74 | |
---|
75 | |
---|
76 | def add_site(self,easy_site): |
---|
77 | |
---|
78 | self.button_counter+=1 |
---|
79 | id=self.button_counter |
---|
80 | # Create gtkBox |
---|
81 | self.site_hbox=Gtk.HBox(True,1) |
---|
82 | self.site_hbox.show() |
---|
83 | |
---|
84 | self.easy_sites[id]={} |
---|
85 | self.easy_sites[id]["name"]=easy_site.setdefault("name","ola") |
---|
86 | self.easy_sites[id]["description"]=easy_site.setdefault("description","ola") |
---|
87 | #self.easy_sites[id]["button"]=Gtk.Button("Delete "+ self.easy_sites[id]["name"]) |
---|
88 | image = Gtk.Image(stock=Gtk.STOCK_DELETE) |
---|
89 | self.easy_sites[id]["button"] = Gtk.Button(label="Delete "+ self.easy_sites[id]["name"], image=image) |
---|
90 | self.easy_sites[id]["button"].show() |
---|
91 | self.easy_sites[id]["button"].connect("clicked",self.easy_sites_click,id) |
---|
92 | |
---|
93 | # A partir de aqui con Miguel |
---|
94 | self.label_info=Gtk.Label(self.easy_sites[id]["name"]) |
---|
95 | self.label_info.show() |
---|
96 | |
---|
97 | # Esto anyade a la caja |
---|
98 | self.site_hbox.pack_start(self.label_info, True, True,1) |
---|
99 | self.site_hbox.pack_start(self.easy_sites[id]["button"],False,False,5) |
---|
100 | |
---|
101 | self.boxButtons.pack_start(self.site_hbox, False,False,5) |
---|
102 | |
---|
103 | self.scroll_me=True |
---|
104 | |
---|
105 | #def add_site() |
---|
106 | |
---|
107 | |
---|
108 | def delete_button(self,id): |
---|
109 | """ |
---|
110 | Delete button action |
---|
111 | """ |
---|
112 | site_to_remove=self.easy_sites[id]["name"] |
---|
113 | print(site_to_remove) |
---|
114 | |
---|
115 | dialog = Gtk.MessageDialog(self.window,Gtk.DialogFlags.MODAL,Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Are you sure that you want remove ") |
---|
116 | dialog.format_secondary_text(str(site_to_remove)) |
---|
117 | response = dialog.run() |
---|
118 | if response == Gtk.ResponseType.YES: |
---|
119 | try: |
---|
120 | subprocess.check_output(["lliurex-easy-sites-cli", "remove",site_to_remove]) |
---|
121 | try: |
---|
122 | #self.boxButtons.remove(self.easy_sites[id]["button"]) |
---|
123 | self.boxButtons.remove(self.easy_sites[id]["button"].get_parent()) |
---|
124 | except Exception as e: |
---|
125 | print("No tenemos ni cochina idea") |
---|
126 | |
---|
127 | except Exception as e: |
---|
128 | print(str(e)) |
---|
129 | elif response == Gtk.ResponseType.NO: |
---|
130 | print ("QUESTION dialog closed by clicking NO button") |
---|
131 | dialog.destroy() |
---|
132 | |
---|
133 | #def delete_button |
---|
134 | |
---|
135 | |
---|
136 | def button_add_site(self,widget): |
---|
137 | """ |
---|
138 | Method to recollect information from gtk entries |
---|
139 | and create the site. |
---|
140 | """ |
---|
141 | description="" |
---|
142 | title=self.entryTitle.get_text() |
---|
143 | path=self.filechooserbuttonPath.get_filename() |
---|
144 | description="easy-site" |
---|
145 | if title != None and path != None: |
---|
146 | print("[ DEBUG ] "+ title +" Path "+ path + " Description "+description ) |
---|
147 | easy_site={} |
---|
148 | easy_site["name"]=title |
---|
149 | easy_site["path"]=path |
---|
150 | easy_site["description"]=description |
---|
151 | try: |
---|
152 | # Execute the script |
---|
153 | os.system("lliurex-easy-sites-cli create %s %s %s"%(title,easy_site["path"],easy_site["description"])) |
---|
154 | #subprocess.check_output(["lliurex-easy-sites-cli", "create",title,easy_site["path"],easy_site["description"]]) |
---|
155 | except Exception as e: |
---|
156 | print (str(e)) |
---|
157 | lliurexEasySites.add_site(easy_site) |
---|
158 | |
---|
159 | self.windowGatherer.destroy() |
---|
160 | |
---|
161 | #def button_add_site |
---|
162 | |
---|
163 | def build_gatherer(self): |
---|
164 | """ |
---|
165 | Build a gatherer window information for a custom site |
---|
166 | This gui replenish info like: |
---|
167 | Title |
---|
168 | Path |
---|
169 | Description |
---|
170 | """ |
---|
171 | |
---|
172 | builderGatherer = Gtk.Builder() |
---|
173 | builderGatherer.set_translation_domain('lliurex-easy-sites') |
---|
174 | builderGatherer.add_from_file(self.EASY_SITES_GATHERER_GLADE) |
---|
175 | |
---|
176 | self.windowGatherer=builderGatherer.get_object("windowGatherer") |
---|
177 | self.windowGatherer.connect("destroy",lliurexEasySites.gatherer_close_window) |
---|
178 | |
---|
179 | # Title |
---|
180 | self.entryTitle=builderGatherer.get_object("entryTitle") |
---|
181 | self.filechooserbuttonPath=builderGatherer.get_object("filechooserbuttonPath") |
---|
182 | # Remove the filechooserbuttonPath previosluy selected |
---|
183 | self.filechooserbuttonPath.set_filename("") |
---|
184 | |
---|
185 | #self.entryDescription=builderGatherer.get_object("entryDescription") |
---|
186 | |
---|
187 | self.buttonAccept=builderGatherer.get_object("buttonAccept") |
---|
188 | self.buttonAccept.connect("clicked",lliurexEasySites.button_add_site) |
---|
189 | |
---|
190 | self.windowGatherer.show_all() |
---|
191 | |
---|
192 | #def build_gatherer(self) |
---|
193 | |
---|
194 | def build_gui(self): |
---|
195 | """ |
---|
196 | Create the gui for the LliureX Easy Sites |
---|
197 | """ |
---|
198 | |
---|
199 | builder = Gtk.Builder() |
---|
200 | builder.set_translation_domain('lliurex-easy-sites') |
---|
201 | builder.add_from_file(self.EASY_SITES_GLADE) |
---|
202 | |
---|
203 | self.window=builder.get_object("MainWindow") |
---|
204 | self.window.connect("destroy",lliurexEasySites.close_window) |
---|
205 | |
---|
206 | # Afig button |
---|
207 | self.buttonAdd=builder.get_object("buttonAdd") |
---|
208 | self.buttonAdd.connect("clicked",self.addButtonSite) |
---|
209 | |
---|
210 | #boxButtons |
---|
211 | self.boxButtons=builder.get_object("boxButtons") |
---|
212 | self.boxButtons.connect("size-allocate",self.boxButtons_changed) |
---|
213 | |
---|
214 | |
---|
215 | self.scroll=builder.get_object("scrolledwindow1") |
---|
216 | |
---|
217 | self.window.show_all() |
---|
218 | |
---|
219 | #def build_gui |
---|
220 | |
---|
221 | def boxButtons_changed(self,widget,event): |
---|
222 | |
---|
223 | if self.scroll_me: |
---|
224 | adj = self.scroll.get_vadjustment() |
---|
225 | adj.set_value( adj.get_upper() - adj.get_page_size() ) |
---|
226 | self.scroll_me=False |
---|
227 | |
---|
228 | def replenish_sites(self): |
---|
229 | """ |
---|
230 | Replenish sites from system |
---|
231 | """ |
---|
232 | lista=subprocess.check_output(["lliurex-easy-sites-cli", "list"]).strip("\n") |
---|
233 | print(lista) |
---|
234 | for item in lista.split("\n"): |
---|
235 | if item!="": |
---|
236 | easy_site={} |
---|
237 | easy_site["name"]=str(item) |
---|
238 | lliurexEasySites.add_site(easy_site) |
---|
239 | # def replenish sites |
---|
240 | |
---|
241 | |
---|
242 | #class LliurexEasySites: |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | if __name__== "__main__": |
---|
247 | |
---|
248 | lliurexEasySites=LliurexEasySites() |
---|
249 | lliurexEasySites.build_gui() |
---|
250 | lliurexEasySites.replenish_sites() |
---|
251 | """ |
---|
252 | easy_site={} |
---|
253 | easy_site["name"]="Una web muy chula" |
---|
254 | |
---|
255 | lliurexEasySites.add_site(easy_site) |
---|
256 | easy_site["name"]="Otra web no tan guay" |
---|
257 | |
---|
258 | lliurexEasySites.add_site(easy_site) |
---|
259 | """ |
---|
260 | GObject.threads_init() |
---|
261 | Gtk.main() |
---|
262 | GObject.threads_leave() |
---|