16 supports_alpha = False
19 # sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
20 # sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
21 # print "Start logging!"
23 # constants. dbfile is the location of the csv
24 # comiccache is the location of the images
27 basedbdir = "/opt/comic-widget/db/"
28 imagedir = "/opt/comic-widget/images/"
29 dbdir = "/home/user/.comic-widget/"
30 activecomics = dbdir + "activecomics.cfg"
31 comiccache = "/home/user/MyDocs/.comics/"
32 comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + "comicdb.xkcd.csv"},
33 "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + "comicdb.sinfest.csv"},
34 "phd":{"name":"PHD Comics","link":"http://www.phdcomics.com/","start":1240,"dbfile":dbdir + "comicdb.phd.csv"},
35 "dilbert":{"name":"Dilbert","link":"http://dilbert.com/","start":"2009-01-01","dbfile":dbdir + "comicdb.dilbert.csv"},
36 "cyanide":{"name":"Cyanide&Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
43 # handling of the comics
45 def __init__(self, comic):
47 self.start = comics[self.comic]["start"]
48 self.dbfile = comics[self.comic]["dbfile"]
50 #if db file exist, read it
51 # if os.path.isfile(self.dbfile) == True:
52 # dbf = open(self.dbfile, 'r')
54 dbf = self.get_dbfile()
55 dbr = csv.DictReader(dbf)
67 #if db file exist, read it
68 print "fetching " + self.dbfile
69 if os.path.isfile(self.dbfile) == True:
70 print "found " + self.dbfile
71 dbf = open(self.dbfile, 'r')
75 print "file " + self.dbfile + " not found"
76 if not os.path.exists(dbdir):
77 print "path not found: " + dbdir
81 print "comic db creation failed on mkdir"
84 shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
86 print "comic db creation failed on copy"
87 print "creating new..."
88 dbf = open(self.dbfile, 'w')
89 dbf.write('comic,id,link,url,filename,title\n')
92 if os.path.isfile(self.dbfile) == True:
93 dbf = open(self.dbfile, 'r')
96 print "comic db creation failed after copy"
100 print str(self.currentcomic)
101 if self.currentcomic < 0:
102 self.currentcomic = 0
104 fetchid = self.db[self.currentcomic]['id']
106 fetchid = comics[self.comic]['start']
107 self.fetch_newer(self.comic, fetchid)
109 if len(self.db) < (self.currentcomic + 1):
110 self.currentcomic = len(self.db) - 1
112 fetchid = self.db[self.currentcomic]['id']
114 fetchid = comics[self.comic]['start']
115 self.fetch_earlier(self.comic, fetchid)
117 self.currentcomic = len(self.db) - 1
119 dbrow = self.db[self.currentcomic]
120 filename = comiccache + self.comic + "/" + dbrow['filename']
121 print filename + "\n"
123 if os.path.isfile(filename):
124 print "found, returning " + filename + "\n"
128 print filename + " not found\nretrieving " + dbrow['url'] + "\n"
129 if not os.path.exists(comiccache + self.comic + "/"):
130 print "create dir " + comiccache + self.comic + "/"
132 os.makedirs(comiccache + self.comic + "/")
135 print "comic db creation failed on mkdir"
136 urllib.urlretrieve(dbrow['url'], filename)
140 def get_current_row(self):
141 return self.db[self.currentcomic]
144 print str(self.currentcomic)
145 if self.currentcomic < 0:
147 self.currentcomic = 0
148 if len(self.db) < (self.currentcomic + 1):
149 self.currentcomic -= 1
150 return self.db[self.currentcomic]['link']
154 def insert_row(self, irow):
155 dbf = open(self.dbfile, 'a')
156 dbw = csv.writer(dbf)
160 def insert_row_first(self, irow):
161 dbf = open(self.dbfile, 'w')
162 dbf.write('comic,id,link,url,filename,title\n')
163 dbw = csv.writer(dbf)
166 dbf = open(self.dbfile, 'a')
167 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
175 # if len(self.db) < 1:
176 # self.currentcomic = -1
177 # self.fetch_latest_std(self.comic, self.start)
178 # elif self.currentcomic == 0 or self.currentcomic < 0:
179 # self.fetch_latest_std(self.comic, self.db[0]['id'])
180 # elif self.currentcomic == (len(self.db) - 1):
181 # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
182 dbf = open(self.dbfile, 'r')
183 dbr = csv.DictReader(dbf)
186 self.db.insert(0,row)
191 def fetch_earlier(self, comic, earliest):
192 print "fetch before, " + comic + " earliest"
193 if comic == "cyanide":
194 comicid = self.get_prev_id(comic,earliest)
195 print "got " + comicid + " as the one before current..."
196 elif len(earliest) == 10:
198 dt = string.split(earliest, "-")
199 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
200 earlier = d - datetime.timedelta( 1 )
201 comicid = earlier.isoformat()
203 comicid = int(earliest) - 1
205 irow = self.get_irow(comic, comicid)
206 if irow and irow[0] == "skip":
207 print "problem with this one, fetching " + str(irow[2]) + " instead."
208 irow = self.get_irow(comic, irow[2])
210 if irow and len(irow) > 3:
213 print "\ninserting...\n"
214 self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
216 print "No comic found at " + comicid
222 def fetch_newer(self, comic, newest):
223 if comic == "cyanide":
224 comicid = self.get_next_id(comic,newest)
225 elif len(newest) == 10:
227 dt = string.split(newest, "-")
228 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
229 newest = d + datetime.timedelta( 1 )
230 comicid = newest.isoformat()
232 comicid = int(newest) + 1
234 irow = self.get_irow(comic, comicid)
235 if irow and irow[0] == "skip":
236 print "problem with this one, fetching " + str(irow[1]) + " instead."
237 irow = self.get_irow(comic, irow[1])
239 if irow and len(irow) > 3:
242 print "\ninserting...\n"
243 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
245 print "No comic found at " + str(comicid)
250 def fetch_latest_std(self, comic, latest):
251 print "fetching new after " + str(comic) + " " + str(latest)
254 if comic == 'cyanide':
255 next = self.get_next_id(comic, latest)
261 if len(str(latest)) == 10:
264 dt = string.split(latest, "-")
265 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
266 newer = d + datetime.timedelta( 1 )
267 comicid = newer.isoformat()
269 comicid = int(latest) + 1
272 lasturl = self.db[0]['url']
276 irow = self.get_irow(comic, comicid)
278 print "got irow: " + str(irow)
279 if str(irow[0]) == 'skip':
280 print "skipping this one..."
283 if irow[3] == lasturl:
284 print "Looping the same, break break break!"
288 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
294 dt = string.split(comicid, "-")
295 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
296 newer = d + datetime.timedelta( 1 )
297 comicid = newer.isoformat()
308 def get_next_id(self, comic, number):
309 if comic == 'cyanide':
310 link = "http://www.explosm.net/comics/" + str(number) + "/"
311 print "link: " + link
313 f = urllib2.urlopen(link)
317 print "got hcode = " + str(hcode) + "\n"
321 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
325 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
327 print "no 'next' found"
329 splt = string.split(splt[1], '/">Next >', 1)
331 print "no 'next' found"
334 print "got next: " + splt[0]
337 def get_prev_id(self, comic, number):
338 if comic == 'cyanide':
339 link = "http://www.explosm.net/comics/" + str(number) + "/"
340 print "link: " + link
342 f = urllib2.urlopen(link)
346 print "got hcode = " + str(hcode) + "\n"
350 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
354 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
356 print "no 'prev' found"
358 splt = string.split(splt[1], '/">< Previous', 1)
359 if len(splt) < 2 or len(splt[0]) > 5:
360 print "no 'next' found"
363 print "got previous: " + splt[0]
370 def get_irow(self, comic, number):
372 if len(str(number)) == 10:
377 print "number is now: " + str(number) + "\n"
379 link = "http://xkcd.org/" + str(number) + "/"
380 elif comic == 'sinfest':
381 link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
382 elif comic == 'dilbert':
383 link = "http://dilbert.com/" + str(number) + "/"
385 link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
386 elif comic == 'cyanide':
387 link = "http://www.explosm.net/comics/" + str(number) + "/"
391 print "link: " + link
393 f = urllib2.urlopen(link)
397 print "got hcode = " + str(hcode) + "\n"
402 # they look like: comic,id,link,url,filename,title
403 # We already have comic, number/id and link
408 # This should be done with regex but...
409 splt = string.split(s, "<h3>Image URL (for hotlinking/embedding): ", 1)
410 splt2 = string.split(splt[1], "</h3>", 1)
412 splt = string.split(splt[0], "<h1>", 1)
413 splt = string.split(splt[1], "</h1>", 1)
417 elif comic == 'sinfest':
420 splt = string.split(s, 'height="107"', 1)
421 splt = string.split(splt[1], '<img src="', 1)
422 splt = string.split(splt[1], '" alt="', 1)
425 print "Fake 404! Break break break!"
427 splt = string.split(splt[1], '" border="0" />', 1)
430 elif comic == 'dilbert':
433 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
434 splt = string.split(splt[1], '" />', 1)
435 url = "http://dilbert.com" + splt[0]
437 print "Fake 404! Break break break!"
445 splt = string.split(s, '<title>PHD Comics: ', 1)
446 splt = string.split(splt[1], '</title>', 1)
448 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
449 splt = string.split(splt[1], ' align=top>', 1)
452 print "Fake 404! Break break break!"
455 elif comic == 'cyanide':
459 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
464 splt = string.split(splt[1], '/">< ', 1)
466 print "got prev: " + prev
467 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
472 splt = string.split(splt[1], '/">Next ></a>', 1)
473 if len(splt[0]) > 10:
477 print "got next: " + next
479 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
483 splt = string.split(splt[1], ' <b>by <a href="', 1)
486 print "title wrong..."
488 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
490 print "a video? Try skipping"
491 return ['skip',next,prev]
493 splt = string.split(splt[1], '">', 1)
494 url = "http://www.explosm" + splt[0]
495 splt2 = string.rsplit(url, "/", 1)
497 irow = [comic,number,link,url,filename,title,next,prev]
500 splt2 = string.rsplit(url, "/", 1)
502 if filename == self.db[0]['filename']:
503 print "already exists! Break break break!"
505 irow = [comic,number,link,url,filename,title]
511 class ComicHomePlugin(hildondesktop.HomePluginItem):
512 # __gtype_name__ = 'ComicHomePlugin'
514 hildondesktop.HomePluginItem.__init__(self)
515 global supports_alpha
516 self.set_settings(True)
517 self.active_comics = self.get_active_comics()
518 self.connect("show-settings", self.show_options)
519 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
520 self.osso_rpc = osso.Rpc(self.osso_c)
522 if len(self.active_comics) < 1:
523 self.active_comics = ['xkcd']
524 self.db = ComicDb(self.active_comics[self.keypointer])
525 self.comicname = comics[self.active_comics[self.keypointer]]['name']
526 self.set_name = "comicwidget"
527 screen = self.get_screen()
528 colormap = screen.get_rgba_colormap()
530 colormap = screen.get_rgb_colormap()
531 supports_alpha = False
533 supports_alpha = True
534 self.set_colormap(colormap)
535 self.set_app_paintable(True)
537 self.set_size_request(480, 230)
539 self.vbox = gtk.VBox()
540 self.hbox = gtk.HBox()
541 self.label = gtk.Label()
542 self.label.set_alignment(xalign = 0, yalign = 0.5)
543 self.label.set_use_markup(True)
544 titl = str(self.db.db[self.db.currentcomic]['title'])
546 titl = titl[:19] + "..."
547 self.label.set_markup('<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '</span>')
550 self.e_goweb = gtk.EventBox()
551 self.e_goweb.set_name('goweb')
552 self.e_goweb.set_size_request(220, 48)
553 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
554 # it will change to be a cairo thing when I have the time.
555 self.e_open = gtk.EventBox()
556 self.e_open.set_name('open')
557 self.e_switch = gtk.EventBox()
558 self.e_switch.set_name('switch')
559 self.e_switch.set_size_request(48, 48)
560 self.e_next = gtk.EventBox()
561 self.e_next.set_name('next')
562 self.e_next.set_size_request(48, 48)
563 self.e_prev = gtk.EventBox()
564 self.e_prev.set_name('prev')
565 self.e_prev.set_size_request(48, 48)
566 self.e_up = gtk.EventBox()
567 self.e_up.set_name('up')
568 self.e_up.set_size_request(48, 48)
569 self.e_down = gtk.EventBox()
570 self.e_down.set_name('down')
571 self.e_down.set_size_request(48, 48)
572 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
573 self.comic_image.show()
576 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
578 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
579 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
580 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
581 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
582 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
583 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
584 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
585 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
586 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
587 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
592 self.e_goweb.add(self.label)
593 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
594 self.e_goweb.connect("button-release-event", self.view_comic)
596 self.e_open.add(self.comic_image)
598 # self.e_switch.add(self.switch)
599 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
600 self.e_switch.connect("button-press-event", self.button_press)
601 self.e_switch.connect("button-release-event", self.button_release)
602 self.e_switch.connect("leave-notify-event", self.button_release)
605 # self.e_next.add(self.next)
606 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
607 self.e_next.connect("button-press-event", self.button_press)
608 self.e_next.connect("button-release-event", self.button_release)
609 self.e_next.connect("leave-notify-event", self.button_release)
611 #self.e_prev.add(self.prev)
612 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
613 self.e_prev.connect("button-press-event", self.button_press)
614 self.e_prev.connect("button-release-event", self.button_release)
615 self.e_prev.connect("leave-notify-event", self.button_release)
617 #self.e_up.add(self.up)
618 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
619 self.e_up.connect("button-press-event", self.button_press)
620 self.e_up.connect("button-release-event", self.button_release)
621 self.e_up.connect("leave-notify-event", self.button_release)
624 #self.e_down.add(self.down)
625 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
626 self.e_down.connect("button-press-event", self.button_press)
627 self.e_down.connect("button-release-event", self.button_release)
628 self.e_down.connect("leave-notify-event", self.button_release)
630 self.vbox.pack_start(self.e_open,False,False,0)
631 self.vbox.pack_end(self.hbox,False,False,0)
633 self.hbox.pack_start(self.e_switch,False,False,0)
634 self.hbox.pack_start(self.e_goweb,False,False,10)
635 self.hbox.pack_end(self.e_next,False,False,0)
636 self.hbox.pack_end(self.e_prev,False,False,0)
637 self.hbox.pack_end(self.e_down,False,False,0)
638 self.hbox.pack_end(self.e_up,False,False,0)
642 # **************** Cairo draw functions ***************
644 def realize(self, widget):
645 global supports_alpha
647 # To check if the display supports alpha channels, get the colormap
648 screen = self.get_screen()
649 colormap = screen.get_rgba_colormap()
651 print 'Your screen does not support alpha channels!'
652 colormap = screen.get_rgb_colormap()
653 supports_alpha = False
655 # print 'Your screen supports alpha channels!'
656 supports_alpha = True
658 # Now we have a colormap appropriate for the screen, use it
659 self.set_colormap(colormap)
663 def do_expose_event(widget, event):
664 cr = widget.window.cairo_create()
665 widget.draw(widget.e_up, 0)
666 widget.draw(widget.e_down, 0)
667 widget.draw(widget.e_prev,0)
668 widget.draw(widget.e_next, 0)
669 widget.draw(widget.e_switch, 0)
670 # widget.writeit(widget.e_goweb, "asdf")
671 cr.set_operator(cairo.OPERATOR_OVER)
672 region = gtk.gdk.region_rectangle(event.area)
674 bg_color=gtk.gdk.color_parse('#000000')
676 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
678 cr.set_source_rgb (0.0, 0.0, 0.0)
683 # Function that draws a button preloaded in the images dictionary.
684 def draw(self, target, postfix):
685 cr_e = target.window.cairo_create()
686 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
687 cr_e.set_operator(cairo.OPERATOR_SOURCE)
689 cr_e.set_operator(cairo.OPERATOR_OVER)
690 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
693 # ********** Event handlers *************
696 # Most events happen on release, so I moved all of them into the same event handler.
697 def button_release(self, widget, event):
698 if event.type == gtk.gdk.BUTTON_RELEASE:
699 func = widget.get_name()
700 print "pressed: " + func
702 self.e_open.remove(self.comic_image)
703 self.imgvpos = self.imgvpos - 80
706 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
707 self.e_open.add(self.comic_image)
708 self.e_open.show_all()
711 self.e_open.remove(self.comic_image)
712 self.imgvpos = self.imgvpos + 80
713 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
714 self.e_open.add(self.comic_image)
715 self.e_open.show_all()
718 self.db.currentcomic -= 1
720 self.e_open.remove(self.comic_image)
721 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
722 self.e_open.add(self.comic_image)
723 self.e_open.show_all()
724 self.e_goweb.remove(self.label)
725 titl = str(self.db.db[self.db.currentcomic]['title'])
727 titl = titl[:19] + "..."
728 self.label.set_markup('<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '</span>')
729 self.e_goweb.add(self.label)
730 self.e_goweb.show_all()
733 self.db.currentcomic += 1
735 self.e_open.remove(self.comic_image)
736 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
737 self.e_open.add(self.comic_image)
738 self.e_open.show_all()
739 self.e_goweb.remove(self.label)
740 titl = str(self.db.db[self.db.currentcomic]['title'])
742 titl = titl[:19] + "..."
743 self.label.set_markup('<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '</span>')
744 self.e_goweb.add(self.label)
745 self.e_goweb.show_all()
747 elif func == 'switch':
748 self.active_comics = self.get_active_comics()
751 #sys.stdout = sys.__stdout__
753 #sys.stderr = sys.__stderr__
754 print "active comics: " + str(self.active_comics)
755 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
758 print "switching to " + self.active_comics[self.keypointer]
759 self.comicname = comics[self.active_comics[self.keypointer]]['name']
760 self.db = ComicDb(self.active_comics[self.keypointer])
761 self.e_open.remove(self.comic_image)
762 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
763 self.e_goweb.remove(self.label)
764 titl = str(self.db.db[self.db.currentcomic]['title'])
766 titl = titl[:19] + "..."
767 self.label.set_markup('<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '</span>')
768 self.e_goweb.add(self.label)
769 self.e_goweb.show_all()
770 self.e_open.add(self.comic_image)
771 self.e_open.show_all()
775 self.draw(widget, "0")
777 # this one only switches to pressed button image
778 def button_press(self, widget, event):
779 if not event.type == gtk.gdk.BUTTON_PRESS:
781 self.draw(widget, "1")
783 # this one cancels a button press when pointer is moved out from it after press.
784 def button_out(self, widget, event):
785 if not event.type == gtk.gdk.BUTTON_PRESS:
787 self.draw(widget, "0")
789 # no fancy cairo stuff here. simply launches url in browser.
790 def view_comic(self, widget, event):
791 if not event.type == gtk.gdk.BUTTON_RELEASE:
793 link = self.db.get_link()
794 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
798 # this function modifies the image to fit the widget and then returns it.
800 def get_resized_pixmap(self, filename, vpos):
801 print "vpos: " + str(vpos)
805 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
806 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, 480, maxheight)
807 parent_buf.fill(0xffffffff)
809 # get correct width/height
812 new_height = int((478.0 / pixbuf.get_width()) * pixbuf.get_height())
813 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
814 print "scaled height: " + str(scaled_buf.get_height())
815 if scaled_buf.get_height() > maxheight:
816 if scaled_buf.get_height() - (vpos + maxheight) < 0:
817 vpos = scaled_buf.get_height() - maxheight
818 scaled_buf.copy_area(0,vpos,478,maxheight,parent_buf,1,0)
819 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
822 tmpy = maxheight - scaled_buf.get_height()
826 scaled_buf.copy_area(0,0,478,scaled_buf.get_height(),parent_buf,1,tmpy)
828 # test = parent_buf.render_pixmap_and_mask(0)
829 parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
830 retimg.set_from_pixbuf(parent_buf)
831 print retimg.get_pixel_size()
832 print "vpos after fixing image: " + str(self.imgvpos)
835 def screen_changed(self, widget, old_screen=None):
836 global supports_alpha
838 # To check if the display supports alpha channels, get the colormap
839 screen = self.get_screen()
840 colormap = screen.get_rgba_colormap()
842 print 'Your screen does not support alpha channels!'
843 colormap = screen.get_rgb_colormap()
844 supports_alpha = False
846 # print 'Your screen supports alpha channels!'
847 supports_alpha = True
849 # Now we have a colormap appropriate for the screen, use it
850 self.set_colormap(colormap)
855 def get_active_comics(self):
858 if os.path.isfile(activecomics) == True:
859 print "found " + activecomics
860 dbf = open(activecomics, 'r')
861 dbr = csv.reader(dbf)
870 print "file " + activecomics + " not found"
871 if not os.path.exists(dbdir):
872 print "path not found: " + dbdir
876 print "settings dir creation failed. Exiting."
879 # dbf = file(activecomics, 'w')
881 dbf = open(activecomics, 'w')
883 print "error storing settings"
885 dbw = csv.writer(dbf)
886 dbw.writerow(comics.keys())
895 #check if settings file exists
897 #if not, write defaults
899 # ************************* OPTION DIALOGS ********************************
901 def show_options(self, widget):
902 print "loading options dialog"
903 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
905 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
906 about_button.set_text("About", "See Author, Copyright and License information")
907 about_button.set_alignment(0,0,0,0)
908 about_button.connect("clicked", self.show_about)
910 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
911 comics_button.set_text("Comics", "Choose which comics to view")
912 comics_button.set_alignment(0,0,0,0)
913 comics_button.connect("clicked", self.show_comics)
915 print "adding buttons to dialog, starting with comics"
916 dialog.vbox.pack_start(comics_button, True, True, 0)
917 print "adding about button"
918 dialog.vbox.pack_start(about_button, True, True, 0)
924 def show_about(self, widget):
925 print "in about dialog"
926 dialog = gtk.AboutDialog()
927 dialog.set_title("About")
928 dialog.set_name("Comic widget")
929 dialog.set_version(APP_VERSION)
930 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
931 dialog.set_authors(["Marcus Wikstrom <mece@ovi.com>\nLogo by Martin Wikstrom\n\nSpecial thanks to all the great python developers on t.m.o who share their code, and the helpful wizards on #maemo."])
932 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
933 dialog.set_comments("Silly rabbit, tricks are for kids.")
934 dialog.set_license("""This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License. <http://www.gnu.org/licenses/>.""")
935 dialog.set_wrap_license(True)
940 def show_comics(self, widget):
941 print "in comics dialog! yay!"
942 dialog = gtk.Dialog("Configure Search Engines", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
943 comiclist = comics.keys()
947 for comicid in comiclist:
948 buttonlist[comicid] = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
949 print buttonlist[comicid].get_name()
951 print buttonlist[comicid].set_alignment(0, 0.5, 0, 0.5)
953 for acomic in self.active_comics:
954 if acomic == comicid:
958 buttonlist[comicid].set_text(comics[comicid]['name'] + " *", comicid + ": click to remove")
959 self.connlist[comicid] = buttonlist[comicid].connect("clicked", self.remove_comic)
961 buttonlist[comicid].set_text(comics[comicid]['name'], comicid + ": click to add")
962 self.connlist[comicid] = buttonlist[comicid].connect("clicked", self.add_comic)
963 dialog.vbox.pack_start(buttonlist[comicid], True, True, 0)
970 def add_comic(self, widget):
971 print "add " + widget.get_title()
972 splt = string.split(widget.get_value(), ':', 1)
974 if os.path.isfile(activecomics) == True:
975 print "added " + comicid + " to " + str(self.active_comics)
977 found = self.active_comics.index(comicid)
979 self.active_comics.append(comicid)
980 dbf = open(activecomics, 'w')
981 dbw = csv.writer(dbf)
982 dbw.writerow(self.active_comics)
984 widget.set_text(comics[comicid]['name'] + " *", comicid + ": click to remove")
985 widget.disconnect(self.connlist[comicid])
986 self.connlist[comicid] = widget.connect("clicked", self.remove_comic)
988 def remove_comic(self, widget):
989 print "remove " + widget.get_title()
990 if len(self.active_comics) == 1:
992 splt = string.split(widget.get_value(), ':', 1)
994 if os.path.isfile(activecomics) == True:
995 print "removing " + comicid + " from " + str(self.active_comics)
997 del self.active_comics[self.active_comics.index(comicid)]
1000 dbf = open(activecomics, 'w')
1001 dbw = csv.writer(dbf)
1002 dbw.writerow(self.active_comics)
1004 widget.set_text(comics[comicid]['name'], comicid + ": click to add")
1005 widget.disconnect(self.connlist[comicid])
1006 self.connlist[comicid] = widget.connect("clicked", self.add_comic)
1008 hd_plugin_type = ComicHomePlugin
1011 if __name__ == "__main__":
1013 gobject.type_register(hd_plugin_type)
1014 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")