1 | import gi |
---|
2 | gi.require_version('Gtk', '3.0') |
---|
3 | from gi.repository import Gtk,GdkPixbuf,GLib,GObject |
---|
4 | import urllib2 |
---|
5 | import threading |
---|
6 | import os |
---|
7 | import ImageManager |
---|
8 | import Core |
---|
9 | |
---|
10 | class ScreenshotNeo(Gtk.Stack): |
---|
11 | |
---|
12 | def __init__(self): |
---|
13 | |
---|
14 | Gtk.Stack.__init__(self) |
---|
15 | self.set_transition_type(Gtk.StackTransitionType.CROSSFADE) |
---|
16 | self.set_transition_duration(250) |
---|
17 | |
---|
18 | self.image_dir="/tmp/.lliurex-store/" |
---|
19 | if not os.path.exists(self.image_dir): |
---|
20 | os.makedirs(self.image_dir) |
---|
21 | |
---|
22 | self.spinner=Gtk.Spinner() |
---|
23 | #self.spinner.set_size_request(x,y) |
---|
24 | self.spinner.start() |
---|
25 | |
---|
26 | hbox=Gtk.HBox() |
---|
27 | hbox.pack_start(self.spinner,True,True,0) |
---|
28 | |
---|
29 | self.image=Gtk.Image() |
---|
30 | self.add_titled(hbox,"spinner","Spinner") |
---|
31 | self.add_titled(self.image,"image","Image") |
---|
32 | |
---|
33 | self.image_info={} |
---|
34 | self.image_info["image_url"]="" |
---|
35 | self.image_info["video_url"]="" |
---|
36 | self.image_info["image_id"]="" |
---|
37 | self.image_info["pkg_name"]="" |
---|
38 | self.image_info["image_path"]="" |
---|
39 | self.image_info["x"]=-1 |
---|
40 | self.image_info["y"]=-1 |
---|
41 | self.image_info["aspect_ratio"]=True |
---|
42 | self.image_info["custom_frame"]=False |
---|
43 | self.image_info["force_label"]=False |
---|
44 | self.image_info["pixbuf"]=None |
---|
45 | |
---|
46 | self.show_all() |
---|
47 | |
---|
48 | #def init |
---|
49 | |
---|
50 | def set_image_info(self,image_info): |
---|
51 | |
---|
52 | self.image_info["pkg_name"]=image_info.setdefault("pkg_name","") |
---|
53 | self.image_info["image_path"]=image_info.setdefault("image_path","") |
---|
54 | self.image_info["x"]=image_info.setdefault("x",-1) |
---|
55 | self.image_info["y"]=image_info.setdefault("y",-1) |
---|
56 | self.image_info["aspect_ratio"]=image_info.setdefault("aspect_ratio",True) |
---|
57 | self.image_info["pixbuf"]=image_info.setdefault("pixbuf",None) |
---|
58 | self.image_info["image_url"]=image_info.setdefault("image_url","") |
---|
59 | self.image_info["image_id"]=image_info.setdefault("image_id","") |
---|
60 | self.image_info["custom_frame"]=image_info.setdefault("custom_frame",False) |
---|
61 | self.image_info["force_label"]=image_info.setdefault("force_label",False) |
---|
62 | |
---|
63 | #def set_image_info |
---|
64 | |
---|
65 | def set_from_file(self,image_info): |
---|
66 | |
---|
67 | |
---|
68 | self.set_image_info(image_info) |
---|
69 | |
---|
70 | x=self.image_info["x"] |
---|
71 | y=self.image_info["x"] |
---|
72 | aspect_ratio=self.image_info["aspect_ratio"] |
---|
73 | file_path=self.image_info["image_path"] |
---|
74 | |
---|
75 | if os.path.exists(file_path): |
---|
76 | |
---|
77 | if x==-1 and y==-1: |
---|
78 | |
---|
79 | self.image.set_from_file(file_path) |
---|
80 | pixbuf=self.image.get_pixbuf() |
---|
81 | self.image_info["x"]=pixbuf.get_width() |
---|
82 | self.image_info["y"]=pixbuf.get_height() |
---|
83 | self.set_visible_child_name("image") |
---|
84 | return True |
---|
85 | |
---|
86 | else: |
---|
87 | tmp=ImageManager.scale_image(file_path,x,y,aspect_ratio) |
---|
88 | pixbuf=tmp.get_pixbuf() |
---|
89 | self.image_info["x"]=pixbuf.get_width() |
---|
90 | self.image_info["y"]=pixbuf.get_height() |
---|
91 | self.image.set_from_pixbuf(tmp.get_pixbuf()) |
---|
92 | self.set_visible_child_name("image") |
---|
93 | return True |
---|
94 | |
---|
95 | return False |
---|
96 | |
---|
97 | #def set_from_file |
---|
98 | |
---|
99 | def set_from_pixbuf(self,image_info): |
---|
100 | |
---|
101 | self.set_image_info(image_info) |
---|
102 | |
---|
103 | x=self.image_info["x"] |
---|
104 | y=self.image_info["x"] |
---|
105 | pixbuf=self.image_info["pixbuf"] |
---|
106 | aspect_ratio=self.image_info["aspect_ratio"] |
---|
107 | |
---|
108 | if pixbuf!=None: |
---|
109 | |
---|
110 | if x==-1 and y==-1: |
---|
111 | |
---|
112 | self.image.set_from_pixbuf(pixbuf) |
---|
113 | self.image_info["x"]=pixbuf.get_width() |
---|
114 | self.image_info["y"]=pixbuf.get_height() |
---|
115 | self.set_visible_child_name("image") |
---|
116 | |
---|
117 | else: |
---|
118 | |
---|
119 | pixbuf=ImageManager.scale_pixbuf(pixbuf,x,y,aspect_ratio) |
---|
120 | self.image.set_from_pixbuf(pixbuf) |
---|
121 | self.image_info["x"]=pixbuf.get_width() |
---|
122 | self.image_info["y"]=pixbuf.get_height() |
---|
123 | self.set_visible_child_name("image") |
---|
124 | |
---|
125 | |
---|
126 | return True |
---|
127 | |
---|
128 | return False |
---|
129 | |
---|
130 | #set_from_pixbuf |
---|
131 | |
---|
132 | def wait_for_image(self): |
---|
133 | |
---|
134 | while self.thread.is_alive(): |
---|
135 | return True |
---|
136 | |
---|
137 | |
---|
138 | if os.path.exists(self.image_dir+self.image_info["image_id"]): |
---|
139 | self.set_from_file(self.image_info) |
---|
140 | |
---|
141 | return False |
---|
142 | |
---|
143 | #def _download |
---|
144 | |
---|
145 | def download_image(self,image_info): |
---|
146 | |
---|
147 | self.set_image_info(image_info) |
---|
148 | |
---|
149 | x=self.image_info["x"] |
---|
150 | y=self.image_info["x"] |
---|
151 | |
---|
152 | self.set_visible_child_name("spinner") |
---|
153 | |
---|
154 | self.spinner.set_size_request(x,y) |
---|
155 | |
---|
156 | GLib.timeout_add(500,self.wait_for_image) |
---|
157 | |
---|
158 | self.thread=threading.Thread(target=self.download_image_thread) |
---|
159 | self.thread.daemon=True |
---|
160 | self.thread.start() |
---|
161 | |
---|
162 | #def download_image |
---|
163 | |
---|
164 | def download_image_thread(self): |
---|
165 | |
---|
166 | if not os.path.exists(self.image_dir+self.image_info["image_id"]): |
---|
167 | |
---|
168 | header = { |
---|
169 | 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' |
---|
170 | } |
---|
171 | |
---|
172 | try: |
---|
173 | |
---|
174 | req=urllib2.Request(self.image_info["image_url"],headers=header) |
---|
175 | res=urllib2.urlopen(req) |
---|
176 | f=open(self.image_dir+self.image_info["image_id"],"w") |
---|
177 | f.write(res.read()) |
---|
178 | f.close() |
---|
179 | self.image_info["image_path"]=self.image_dir+self.image_info["image_id"] |
---|
180 | |
---|
181 | if self.image_info["custom_frame"] or self.image_info["force_label"]: |
---|
182 | ImageManager.create_banner(self.image_dir+self.image_info["image_id"],self.image_info["x"],self.image_info["y"],self.image_info["pkg_name"],self.image_info["custom_frame"],self.image_info["image_path"]) |
---|
183 | |
---|
184 | except Exception as e: |
---|
185 | self.image_info["image_path"]="/usr/share/icons/Vibrancy-Colors/mimetypes/256/package.svg" |
---|
186 | self.image_info["custom_frame"]=True |
---|
187 | self.create_banner_from_file(self.image_info) |
---|
188 | |
---|
189 | else: |
---|
190 | self.image_info["image_path"]=self.image_dir+self.image_info["image_id"] |
---|
191 | |
---|
192 | return True |
---|
193 | |
---|
194 | #def download-image-thread |
---|
195 | |
---|
196 | def create_banner_from_file(self,image_info,output_file=None): |
---|
197 | |
---|
198 | file_name=image_info.setdefault("image_path") |
---|
199 | x=image_info.setdefault("x") |
---|
200 | y=image_info.setdefault("y") |
---|
201 | custom_frame=image_info.setdefault("custom_frame",False) |
---|
202 | txt=image_info.setdefault("pkg_name","") |
---|
203 | |
---|
204 | ret=ImageManager.create_banner(file_name,x,y,txt,custom_frame,output_file) |
---|
205 | |
---|
206 | if output_file ==None: |
---|
207 | image_info["pixbuf"]=ret[1].get_pixbuf() |
---|
208 | self.set_from_pixbuf(image_info) |
---|
209 | else: |
---|
210 | image_info["image_path"]=output_file |
---|
211 | self.set_from_file(image_info) |
---|
212 | |
---|
213 | |
---|
214 | #def create_banner_from_file |
---|
215 | |
---|
216 | def create_banner_from_url(self,image_info): |
---|
217 | |
---|
218 | self.set_image_info(self.image_info) |
---|
219 | self.download_image(image_info) |
---|
220 | |
---|
221 | #def create_banner_from_url |
---|
222 | |
---|
223 | def set_video_info(self,video_info): |
---|
224 | |
---|
225 | video_info.setdefault("video_url","") |
---|
226 | video_info.setdefault("image_url","") |
---|
227 | video_info.setdefault("image_id","") |
---|
228 | video_info.setdefault("x",-1) |
---|
229 | video_info.setdefault("y",-1) |
---|
230 | |
---|
231 | |
---|
232 | self.set_image_info(video_info) |
---|
233 | |
---|
234 | self.image_info["video_url"]=video_info.setdefault("video_url") |
---|
235 | self.image_info["image_url"]=video_info.setdefault("image_url") |
---|
236 | self.image_info["image_id"]=video_info.setdefault("image_id") |
---|
237 | self.image_info["x"]=video_info.setdefault("x") |
---|
238 | self.image_info["y"]=video_info.setdefault("y") |
---|
239 | self.image_info["pkg_name"]=video_info.setdefault("pkg_name","") |
---|
240 | |
---|
241 | self.download_image(video_info) |
---|
242 | |
---|
243 | #def set_video_info |
---|
244 | |
---|
245 | |
---|
246 | #class ScreenshotNeo |
---|
247 | |
---|
248 | |
---|
249 | if __name__=="__main__": |
---|
250 | |
---|
251 | w=Gtk.Window() |
---|
252 | w.connect("destroy",Gtk.main_quit) |
---|
253 | |
---|
254 | hbox=Gtk.HBox() |
---|
255 | vbox=Gtk.VBox() |
---|
256 | hbox.pack_start(vbox,False,False,0) |
---|
257 | w.add(hbox) |
---|
258 | |
---|
259 | s=ScreenshotNeo() |
---|
260 | info={} |
---|
261 | info["image_path"]="gearth2.png" |
---|
262 | info["x"]=300 |
---|
263 | info["y"]=300 |
---|
264 | info["aspect_ratio"]=True |
---|
265 | info["pkg_name"]="a" |
---|
266 | s.set_from_file(info) |
---|
267 | vbox.pack_start(Gtk.Label("load from file aspect_ratio=True"),False,False,0) |
---|
268 | vbox.pack_start(s,False,False,0) |
---|
269 | |
---|
270 | s=ScreenshotNeo() |
---|
271 | info={} |
---|
272 | info["image_path"]="gearth2.png" |
---|
273 | info["x"]=100 |
---|
274 | info["y"]=100 |
---|
275 | info["aspect_ratio"]=False |
---|
276 | info["pkg_name"]="a" |
---|
277 | s.set_from_file(info) |
---|
278 | vbox.pack_start(Gtk.Label("load from file aspect_ratio=False"),False,False,0) |
---|
279 | vbox.pack_start(s,False,False,0) |
---|
280 | |
---|
281 | s=ScreenshotNeo() |
---|
282 | info={} |
---|
283 | info["pixbuf"]=Gtk.Image.new_from_file("gearth3.png").get_pixbuf() |
---|
284 | info["aspect_ratio"]=True |
---|
285 | info["pkg_name"]="a" |
---|
286 | s.set_from_pixbuf(info) |
---|
287 | vbox.pack_start(Gtk.Label("load from pixbuf aspect_ratio=True"),False,False,0) |
---|
288 | vbox.pack_start(s,False,False,0) |
---|
289 | |
---|
290 | s=ScreenshotNeo() |
---|
291 | info={} |
---|
292 | info["pixbuf"]=Gtk.Image.new_from_file("gearth3.png").get_pixbuf() |
---|
293 | info["aspect_ratio"]=False |
---|
294 | info["pkg_name"]="a" |
---|
295 | info["x"]=100 |
---|
296 | info["y"]=100 |
---|
297 | s.set_from_pixbuf(info) |
---|
298 | vbox.pack_start(Gtk.Label("load from pixbuf aspect_ratio=False"),False,False,0) |
---|
299 | vbox.pack_start(s,False,False,0) |
---|
300 | |
---|
301 | s=ScreenshotNeo() |
---|
302 | info={} |
---|
303 | info["image_url"]="http://www.hdbloggers.net/wp-content/uploads/2016/10/Wallpaper-4K.jpg" |
---|
304 | info["image_id"]="wallpaper" |
---|
305 | info["aspect_ratio"]=True |
---|
306 | info["pkg_name"]="a" |
---|
307 | info["x"]=300 |
---|
308 | info["y"]=300 |
---|
309 | s.download_image(info) |
---|
310 | vbox.pack_start(Gtk.Label("url aspect_ratio=True"),False,False,0) |
---|
311 | vbox.pack_start(s,False,False,0) |
---|
312 | |
---|
313 | s=ScreenshotNeo() |
---|
314 | info={} |
---|
315 | info["image_url"]="http://no.existo.com/asd.jpg" |
---|
316 | info["image_id"]="wallpaper2" |
---|
317 | info["aspect_ratio"]=True |
---|
318 | info["pkg_name"]="Package name" |
---|
319 | info["x"]=128 |
---|
320 | info["y"]=128 |
---|
321 | s.download_image(info) |
---|
322 | vbox.pack_start(Gtk.Label("url 404 aspect_ratio=True"),False,False,0) |
---|
323 | vbox.pack_start(s,False,False,0) |
---|
324 | |
---|
325 | vbox1=Gtk.VBox() |
---|
326 | hbox.pack_start(vbox1,False,False,0) |
---|
327 | |
---|
328 | s=ScreenshotNeo() |
---|
329 | info={} |
---|
330 | info["image_path"]="/usr/share/pixmaps/firefox.png" |
---|
331 | info["image_id"]="firefox" |
---|
332 | info["aspect_ratio"]=True |
---|
333 | info["pkg_name"]="Firefox" |
---|
334 | info["custom_frame"]=True |
---|
335 | info["x"]=300 |
---|
336 | info["y"]=300 |
---|
337 | s.create_banner_from_file(info) |
---|
338 | vbox1.pack_start(Gtk.Label("create banner from file. resize_image=True"),False,False,0) |
---|
339 | vbox1.pack_start(s,False,False,0) |
---|
340 | |
---|
341 | s=ScreenshotNeo() |
---|
342 | info={} |
---|
343 | info["image_path"]="/home/cless/svn/xenial/lliurex-artwork-icons/trunk/fuentes/future-green/apps/scite.svg" |
---|
344 | info["image_id"]="scite" |
---|
345 | info["aspect_ratio"]=True |
---|
346 | info["pkg_name"]="SciTe" |
---|
347 | info["custom_frame"]=False |
---|
348 | info["x"]=200 |
---|
349 | info["y"]=200 |
---|
350 | s.create_banner_from_file(info) |
---|
351 | vbox1.pack_start(Gtk.Label("create banner from file. resize_image=False"),False,False,0) |
---|
352 | vbox1.pack_start(s,False,False,0) |
---|
353 | |
---|
354 | |
---|
355 | s=ScreenshotNeo() |
---|
356 | info={} |
---|
357 | info["image_url"]="http://androidportal.zoznam.sk/wp-content/uploads/2013/01/2013-purple-640x569.png" |
---|
358 | info["image_id"]="testing" |
---|
359 | info["aspect_ratio"]=True |
---|
360 | info["pkg_name"]="Custom banner" |
---|
361 | info["custom_frame"]=False |
---|
362 | info["force_label"]=True |
---|
363 | info["x"]=400 |
---|
364 | info["y"]=400 |
---|
365 | |
---|
366 | s.create_banner_from_url(info) |
---|
367 | vbox1.pack_start(Gtk.Label("create banner from URL. resize_image=False"),False,False,0) |
---|
368 | vbox1.pack_start(s,False,False,0) |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | w.show_all() |
---|
373 | |
---|
374 | GObject.threads_init() |
---|
375 | Gtk.main() |
---|
376 | |
---|