17 socket.setdefaulttimeout(10)
19 supports_alpha = False
22 # sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
23 # sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
24 # print "Start logging!"
26 # constants. dbfile is the location of the csv
27 # comiccache is the location of the images
28 APP_VERSION = "0.3.4-2"
30 basedbdir = "/opt/comic-widget/db/"
31 imagedir = "/opt/comic-widget/images/"
32 dbdir = "/home/user/.comic-widget/"
33 activecomics = dbdir + "activecomics.cfg"
34 comiccache = "/home/user/MyDocs/.comics/"
35 comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + "comicdb.xkcd.csv"},
36 "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + "comicdb.sinfest.csv"},
37 "phd":{"name":"PHD Comics","link":"http://www.phdcomics.com/","start":1240,"dbfile":dbdir + "comicdb.phd.csv"},
38 "dilbert":{"name":"Dilbert","link":"http://dilbert.com/","start":"2009-01-01","dbfile":dbdir + "comicdb.dilbert.csv"},
39 "cyanide":{"name":"Cyanide and Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
40 "wulff":{"name":"Wulffmorgenthaler","link":"http://wulffmorgenthaler.com/","start":"edd3411b-96ca-4d93-bd5f-0cf1deb67c8a","dbfile":dbdir + "comicdb.wulff.csv"},
43 defaults = {'width':480,'height':230}
44 #defaults = {'width':480,'height':240}
45 sizefile = "/home/user/.comic-widget/size.cfg"
50 # handling of the comics
52 def __init__(self, comic):
54 self.start = comics[self.comic]["start"]
55 self.dbfile = comics[self.comic]["dbfile"]
57 #if db file exist, read it
58 # if os.path.isfile(self.dbfile) == True:
59 # dbf = open(self.dbfile, 'r')
61 dbf = self.get_dbfile()
62 dbr = csv.DictReader(dbf)
74 #if db file exist, read it
75 print "fetching " + self.dbfile
76 if os.path.isfile(self.dbfile) == True:
77 print "found " + self.dbfile
78 dbf = open(self.dbfile, 'r')
82 print "file " + self.dbfile + " not found"
83 if not os.path.exists(dbdir):
84 print "path not found: " + dbdir
88 print "comic db creation failed on mkdir"
91 shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
93 print "comic db creation failed on copy"
94 print "creating new..."
95 dbf = open(self.dbfile, 'w')
96 dbf.write('comic,id,link,url,filename,title\n')
99 if os.path.isfile(self.dbfile) == True:
100 dbf = open(self.dbfile, 'r')
103 print "comic db creation failed after copy"
107 print str(self.currentcomic)
108 if self.currentcomic < 0:
109 self.currentcomic = 0
111 fetchid = self.db[self.currentcomic]['id']
113 fetchid = comics[self.comic]['start']
114 self.fetch_newer(self.comic, fetchid)
116 if len(self.db) < (self.currentcomic + 1):
117 self.currentcomic = len(self.db) - 1
119 fetchid = self.db[self.currentcomic]['id']
121 fetchid = comics[self.comic]['start']
122 self.fetch_earlier(self.comic, fetchid)
124 self.currentcomic = len(self.db) - 1
126 dbrow = self.db[self.currentcomic]
127 filename = comiccache + self.comic + "/" + dbrow['filename']
128 print filename + "\n"
130 if os.path.isfile(filename):
131 print "found, returning " + filename + "\n"
135 print filename + " not found\nretrieving " + dbrow['url'] + "\n"
136 if not os.path.exists(comiccache + self.comic + "/"):
137 print "create dir " + comiccache + self.comic + "/"
139 os.makedirs(comiccache + self.comic + "/")
142 print "comic db creation failed on mkdir"
145 urllib.urlretrieve(dbrow['url'], filename)
147 if os.path.isfile(filename):
152 def get_current_row(self):
153 return self.db[self.currentcomic]
156 print str(self.currentcomic)
157 if self.currentcomic < 0:
159 self.currentcomic = 0
160 if len(self.db) < (self.currentcomic + 1):
161 self.currentcomic -= 1
162 return self.db[self.currentcomic]['link']
166 def insert_row(self, irow):
167 dbf = open(self.dbfile, 'a')
168 dbw = csv.writer(dbf)
172 def insert_row_first(self, irow):
173 dbf = open(self.dbfile, 'w')
174 dbf.write('comic,id,link,url,filename,title\n')
175 dbw = csv.writer(dbf)
178 dbf = open(self.dbfile, 'a')
179 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
187 # if len(self.db) < 1:
188 # self.currentcomic = -1
189 # self.fetch_latest_std(self.comic, self.start)
190 # elif self.currentcomic == 0 or self.currentcomic < 0:
191 # self.fetch_latest_std(self.comic, self.db[0]['id'])
192 # elif self.currentcomic == (len(self.db) - 1):
193 # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
194 dbf = open(self.dbfile, 'r')
195 dbr = csv.DictReader(dbf)
198 self.db.insert(0,row)
203 def fetch_earlier(self, comic, earliest):
204 print "fetch before, " + comic + " earliest"
205 if comic == "cyanide" or comic == "wulff":
206 comicid = self.get_prev_id(comic,earliest)
208 print "already at first comic"
210 print "got " + comicid + " as the one before current..."
211 elif len(earliest) == 10:
213 dt = string.split(earliest, "-")
214 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
215 earlier = d - datetime.timedelta( 1 )
216 comicid = earlier.isoformat()
218 comicid = int(earliest) - 1
220 irow = self.get_irow(comic, comicid)
221 if irow and irow[0] == "skip":
222 print "problem with this one, fetching " + str(irow[2]) + " instead."
223 irow = self.get_irow(comic, irow[2])
225 if irow and len(irow) > 3:
228 print "\ninserting...\n"
229 self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
231 print "No comic found at " + comicid
237 def fetch_newer(self, comic, newest):
238 if comic == "cyanide" or comic == "wulff":
239 comicid = self.get_next_id(comic,newest)
241 print "already at last comic"
243 elif len(newest) == 10:
245 dt = string.split(newest, "-")
246 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
247 newest = d + datetime.timedelta( 1 )
248 comicid = newest.isoformat()
250 comicid = int(newest) + 1
252 irow = self.get_irow(comic, comicid)
253 if irow and irow[0] == "skip":
254 print "problem with this one, fetching " + str(irow[1]) + " instead."
255 irow = self.get_irow(comic, irow[1])
257 if irow and len(irow) > 3:
260 print "\ninserting...\n"
261 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
263 print "No comic found at " + str(comicid)
268 def fetch_latest_std(self, comic, latest):
269 print "fetching new after " + str(comic) + " " + str(latest)
272 if comic == 'cyanide':
273 next = self.get_next_id(comic, latest)
279 if len(str(latest)) == 10:
282 dt = string.split(latest, "-")
283 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
284 newer = d + datetime.timedelta( 1 )
285 comicid = newer.isoformat()
287 comicid = int(latest) + 1
290 lasturl = self.db[0]['url']
294 irow = self.get_irow(comic, comicid)
296 print "got irow: " + str(irow)
297 if str(irow[0]) == 'skip':
298 print "skipping this one..."
301 if irow[3] == lasturl:
302 print "Looping the same, break break break!"
306 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
312 dt = string.split(comicid, "-")
313 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
314 newer = d + datetime.timedelta( 1 )
315 comicid = newer.isoformat()
326 def get_next_id(self, comic, number):
327 if comic == 'cyanide':
328 link = "http://www.explosm.net/comics/" + str(number) + "/"
329 print "link: " + link
331 f = urllib2.urlopen(link)
335 print "got hcode = " + str(hcode) + "\n"
339 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
343 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
345 print "no 'next' found"
347 splt = string.split(splt[1], '/">Next >', 1)
349 print "no 'next' found"
352 print "got next: " + splt[0]
355 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
356 print "link: " + link
358 f = urllib2.urlopen(link)
362 print "got hcode = " + str(hcode) + "\n"
369 splt = string.split(s, '<a href="/default.aspx?id=', 1)
370 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
371 print "no previous found"
372 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
373 print "no next found!"
376 splt = string.split(splt[1], '<a href="/default.aspx?id=', 1)
378 print "no next found! At newest comic!"
380 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
381 print "no next found!"
387 def get_prev_id(self, comic, number):
388 if comic == 'cyanide':
389 link = "http://www.explosm.net/comics/" + str(number) + "/"
390 print "link: " + link
392 f = urllib2.urlopen(link)
396 print "got hcode = " + str(hcode) + "\n"
400 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
404 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
406 print "no 'prev' found"
408 splt = string.split(splt[1], '/">< Previous', 1)
409 if len(splt) < 2 or len(splt[0]) > 5:
410 print "no 'next' found"
413 print "got previous: " + splt[0]
416 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
417 print "link: " + link
419 f = urllib2.urlopen(link)
423 print "got hcode = " + str(hcode) + "\n"
430 splt = string.split(s, '<a href="/default.aspx?id=', 1)
431 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
434 print "no previous found"
444 def get_irow(self, comic, number):
446 if len(str(number)) == 10:
451 print "number is now: " + str(number) + "\n"
453 link = "http://xkcd.org/" + str(number) + "/"
454 elif comic == 'sinfest':
455 link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
456 elif comic == 'dilbert':
457 link = "http://dilbert.com/" + str(number) + "/"
459 link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
460 elif comic == 'cyanide':
461 link = "http://www.explosm.net/comics/" + str(number) + "/"
462 elif comic == 'wulff':
463 link = "http://wulffmorgenthaler.com/default.aspx?id=" + str(number)
467 print "link: " + link
469 f = urllib2.urlopen(link)
473 print "got hcode = " + str(hcode) + "\n"
478 # they look like: comic,id,link,url,filename,title
479 # We already have comic, number/id and link
484 # This should be done with regex but...
485 splt = string.split(s, "<h3>Image URL (for hotlinking/embedding): ", 1)
486 splt2 = string.split(splt[1], "</h3>", 1)
488 splt = string.split(splt[0], "<h1>", 1)
489 splt = string.split(splt[1], "</h1>", 1)
493 elif comic == 'sinfest':
496 splt = string.split(s, 'height="107"', 1)
497 splt = string.split(splt[1], '<img src="', 1)
498 splt = string.split(splt[1], '" alt="', 1)
501 print "Fake 404! Break break break!"
503 splt = string.split(splt[1], '" border="0" />', 1)
506 elif comic == 'dilbert':
509 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
510 splt = string.split(splt[1], '" />', 1)
511 url = "http://dilbert.com" + splt[0]
513 print "Fake 404! Break break break!"
521 splt = string.split(s, '<title>PHD Comics: ', 1)
522 splt = string.split(splt[1], '</title>', 1)
524 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
525 splt = string.split(splt[1], ' align=top>', 1)
528 print "Fake 404! Break break break!"
531 elif comic == 'cyanide':
535 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
540 splt = string.split(splt[1], '/">< ', 1)
542 print "got prev: " + prev
543 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
548 splt = string.split(splt[1], '/">Next ></a>', 1)
549 if len(splt[0]) > 10:
553 print "got next: " + next
555 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
559 splt = string.split(splt[1], ' <b>by <a href="', 1)
562 print "title wrong..."
564 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
566 print "a video? Try skipping"
567 return ['skip',next,prev]
569 splt = string.split(splt[1], '">', 1)
570 url = "http://www.explosm" + splt[0]
571 splt2 = string.rsplit(url, "/", 1)
573 irow = [comic,number,link,url,filename,title,next,prev]
575 elif comic == 'wulff':
579 # <img src="pics/dates/month/2.gif" alt="" />
580 # <img src="pics/dates/day/4.gif" alt="" />
581 # <img src="pics/dates/komma.gif" alt="" />
582 # <img src="pics/dates/year/2010.gif" alt="" />
583 splt = string.split(s, '<img src="pics/dates/month/', 1)
584 if not splt[1][1:2] == ".":
587 title = "0" + splt[1][:1]
588 splt = string.split(splt[1], '<img src="pics/dates/day/', 1)
589 if not splt[1][1:2] == ".":
590 title = title + "-" + splt[1][:2]
592 title = title + "-0" + splt[1][:1]
593 splt = string.split(splt[1], '<img src="pics/dates/year/', 1)
594 title = splt[1][:4] + "-" + title
596 splt = string.split(splt[1], 'id="ctl00_content_Strip1_imgStrip" class="strip" src="striphandler.ashx?stripid=', 1)
597 number = splt[1][:36]
598 url = "http://wulffmorgenthaler.com/striphandler.ashx?stripid=" + number
599 filename = number + ".gif"
600 irow = [comic,number,link,url,filename,title]
604 splt2 = string.rsplit(url, "/", 1)
607 if filename == self.db[0]['filename']:
608 print "already exists! Break break break!"
612 irow = [comic,number,link,url,filename,title]
618 class ComicHomePlugin(hildondesktop.HomePluginItem):
619 # __gtype_name__ = 'ComicHomePlugin'
621 hildondesktop.HomePluginItem.__init__(self)
622 global supports_alpha
623 self.set_settings(True)
624 self.active_comics = self.get_active_comics()
625 self.sizes = self.get_size_settings()
626 self.new_width = self.sizes['width']
627 self.new_height = self.sizes['height']
628 self.connect("show-settings", self.show_options)
629 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
630 self.osso_rpc = osso.Rpc(self.osso_c)
632 if len(self.active_comics) < 1:
633 self.active_comics = ['xkcd']
634 self.db = ComicDb(self.active_comics[self.keypointer])
635 self.comicname = comics[self.active_comics[self.keypointer]]['name']
636 self.set_name = "comicwidget"
637 screen = self.get_screen()
638 colormap = screen.get_rgba_colormap()
640 colormap = screen.get_rgb_colormap()
641 supports_alpha = False
643 supports_alpha = True
644 self.set_colormap(colormap)
645 self.set_app_paintable(True)
647 self.set_size_request(self.sizes['width'], self.sizes['height'])
649 self.vbox = gtk.VBox()
650 self.hbox = gtk.HBox()
651 self.label = gtk.Label()
652 self.label.set_alignment(xalign = 0, yalign = 0.5)
653 self.label.set_use_markup(True)
654 self.label.set_markup(self.get_markup())
657 self.e_goweb = gtk.EventBox()
658 self.e_goweb.set_name('goweb')
659 self.e_goweb.set_size_request(self.sizes['width'] - 250, 48)
660 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
661 # it will change to be a cairo thing when I have the time.
662 self.e_open = gtk.EventBox()
663 self.e_open.set_name('open')
664 self.e_switch = gtk.EventBox()
665 self.e_switch.set_name('switch')
666 self.e_switch.set_size_request(48, 48)
667 self.e_next = gtk.EventBox()
668 self.e_next.set_name('next')
669 self.e_next.set_size_request(48, 48)
670 self.e_prev = gtk.EventBox()
671 self.e_prev.set_name('prev')
672 self.e_prev.set_size_request(48, 48)
673 self.e_up = gtk.EventBox()
674 self.e_up.set_name('up')
675 self.e_up.set_size_request(48, 48)
676 self.e_down = gtk.EventBox()
677 self.e_down.set_name('down')
678 self.e_down.set_size_request(48, 48)
679 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
680 self.comic_image.show()
683 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
685 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
686 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
687 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
688 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
689 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
690 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
691 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
692 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
693 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
694 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
699 self.e_goweb.add(self.label)
700 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
701 self.e_goweb.connect("button-release-event", self.view_comic)
703 self.e_open.add(self.comic_image)
705 # self.e_switch.add(self.switch)
706 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
707 self.e_switch.connect("button-press-event", self.button_press)
708 self.e_switch.connect("button-release-event", self.button_release)
709 self.e_switch.connect("leave-notify-event", self.button_release)
712 # self.e_next.add(self.next)
713 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
714 self.e_next.connect("button-press-event", self.button_press)
715 self.e_next.connect("button-release-event", self.button_release)
716 self.e_next.connect("leave-notify-event", self.button_release)
718 #self.e_prev.add(self.prev)
719 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
720 self.e_prev.connect("button-press-event", self.button_press)
721 self.e_prev.connect("button-release-event", self.button_release)
722 self.e_prev.connect("leave-notify-event", self.button_release)
724 #self.e_up.add(self.up)
725 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
726 self.e_up.connect("button-press-event", self.button_press)
727 self.e_up.connect("button-release-event", self.button_release)
728 self.e_up.connect("leave-notify-event", self.button_release)
731 #self.e_down.add(self.down)
732 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
733 self.e_down.connect("button-press-event", self.button_press)
734 self.e_down.connect("button-release-event", self.button_release)
735 self.e_down.connect("leave-notify-event", self.button_release)
737 self.vbox.pack_start(self.e_open,False,False,0)
738 self.vbox.pack_end(self.hbox,False,False,0)
740 self.hbox.pack_start(self.e_switch,False,False,0)
741 self.hbox.pack_start(self.e_goweb,False,False,5)
742 self.hbox.pack_end(self.e_next,False,False,0)
743 self.hbox.pack_end(self.e_prev,False,False,0)
744 self.hbox.pack_end(self.e_down,False,False,0)
745 self.hbox.pack_end(self.e_up,False,False,0)
751 # **************** Cairo draw functions ***************
753 def realize(self, widget):
754 global supports_alpha
756 # To check if the display supports alpha channels, get the colormap
757 screen = self.get_screen()
758 colormap = screen.get_rgba_colormap()
760 print 'Your screen does not support alpha channels!'
761 colormap = screen.get_rgb_colormap()
762 supports_alpha = False
764 # print 'Your screen supports alpha channels!'
765 supports_alpha = True
767 # Now we have a colormap appropriate for the screen, use it
768 self.set_colormap(colormap)
772 def do_expose_event(widget, event):
773 cr = widget.window.cairo_create()
774 widget.draw(widget.e_up, 0)
775 widget.draw(widget.e_down, 0)
776 widget.draw(widget.e_prev,0)
777 widget.draw(widget.e_next, 0)
778 widget.draw(widget.e_switch, 0)
779 # widget.writeit(widget.e_goweb, "asdf")
780 cr.set_operator(cairo.OPERATOR_OVER)
781 region = gtk.gdk.region_rectangle(event.area)
783 bg_color=gtk.gdk.color_parse('#000000')
785 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
787 cr.set_source_rgb (0.0, 0.0, 0.0)
792 # Function that draws a button preloaded in the images dictionary.
793 def draw(self, target, postfix):
794 cr_e = target.window.cairo_create()
795 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
796 cr_e.set_operator(cairo.OPERATOR_SOURCE)
798 cr_e.set_operator(cairo.OPERATOR_OVER)
799 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
802 # ********** Event handlers *************
805 # Most events happen on release, so I moved all of them into the same event handler.
806 def button_release(self, widget, event):
807 if event.type == gtk.gdk.BUTTON_RELEASE:
808 func = widget.get_name()
809 print "pressed: " + func
811 self.e_open.remove(self.comic_image)
812 self.imgvpos = self.imgvpos - ((self.sizes['height'] - 48) / 2.1)
815 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
816 self.e_open.add(self.comic_image)
817 self.e_open.show_all()
820 self.e_open.remove(self.comic_image)
821 self.imgvpos = self.imgvpos + ((self.sizes['height'] - 48) / 2.1)
822 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
823 self.e_open.add(self.comic_image)
824 self.e_open.show_all()
827 self.db.currentcomic -= 1
829 self.e_open.remove(self.comic_image)
830 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
831 self.e_open.add(self.comic_image)
832 self.e_open.show_all()
833 self.e_goweb.remove(self.label)
834 titl = str(self.db.db[self.db.currentcomic]['title'])
836 titl = titl[:19] + "..."
837 self.label.set_markup(self.get_markup())
838 self.e_goweb.add(self.label)
839 self.e_goweb.show_all()
842 self.db.currentcomic += 1
844 self.e_open.remove(self.comic_image)
845 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
846 self.e_open.add(self.comic_image)
847 self.e_open.show_all()
848 self.e_goweb.remove(self.label)
849 titl = str(self.db.db[self.db.currentcomic]['title'])
851 titl = titl[:19] + "..."
852 self.label.set_markup(self.get_markup())
853 self.e_goweb.add(self.label)
854 self.e_goweb.show_all()
856 elif func == 'switch':
857 self.active_comics = self.get_active_comics()
860 #sys.stdout = sys.__stdout__
862 #sys.stderr = sys.__stderr__
863 print "active comics: " + str(self.active_comics)
864 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
867 print "switching to " + self.active_comics[self.keypointer]
868 self.comicname = comics[self.active_comics[self.keypointer]]['name']
869 self.db = ComicDb(self.active_comics[self.keypointer])
870 self.e_open.remove(self.comic_image)
871 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
872 self.e_goweb.remove(self.label)
873 titl = str(self.db.db[self.db.currentcomic]['title'])
875 titl = titl[:19] + "..."
876 self.label.set_markup(self.get_markup())
877 self.e_goweb.add(self.label)
878 self.e_goweb.show_all()
879 self.e_open.add(self.comic_image)
880 self.e_open.show_all()
884 self.draw(widget, "0")
886 # this one only switches to pressed button image
887 def button_press(self, widget, event):
888 if not event.type == gtk.gdk.BUTTON_PRESS:
890 self.draw(widget, "1")
892 # this one cancels a button press when pointer is moved out from it after press.
893 def button_out(self, widget, event):
894 if not event.type == gtk.gdk.BUTTON_PRESS:
896 self.draw(widget, "0")
898 # no fancy cairo stuff here. simply launches url in browser.
899 def view_comic(self, widget, event):
900 if not event.type == gtk.gdk.BUTTON_RELEASE:
902 link = self.db.get_link()
903 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
907 # this function modifies the image to fit the widget and then returns it.
909 def get_resized_pixmap(self, filename, vpos):
910 print "vpos: " + str(vpos)
912 width = int(self.sizes['width'])
913 height = int(self.sizes['height'])
915 maxheight = height - 48
917 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
918 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
919 parent_buf.fill(0xffffffff)
921 # get correct width/height
924 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
925 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
926 print "scaled height: " + str(scaled_buf.get_height())
927 if scaled_buf.get_height() > maxheight:
928 if scaled_buf.get_height() - (vpos + maxheight) < 0:
929 vpos = scaled_buf.get_height() - maxheight
930 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
931 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
934 tmpy = maxheight - scaled_buf.get_height()
938 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
940 # test = parent_buf.render_pixmap_and_mask(0)
941 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
942 retimg.set_from_pixbuf(parent_buf)
943 print retimg.get_pixel_size()
944 print "vpos after fixing image: " + str(self.imgvpos)
947 def screen_changed(self, widget, old_screen=None):
948 global supports_alpha
950 # To check if the display supports alpha channels, get the colormap
951 screen = self.get_screen()
952 colormap = screen.get_rgba_colormap()
954 print 'Your screen does not support alpha channels!'
955 colormap = screen.get_rgb_colormap()
956 supports_alpha = False
958 # print 'Your screen supports alpha channels!'
959 supports_alpha = True
961 # Now we have a colormap appropriate for the screen, use it
962 self.set_colormap(colormap)
967 def get_active_comics(self):
970 if os.path.isfile(activecomics) == True:
971 print "found " + activecomics
972 dbf = open(activecomics, 'r')
973 dbr = csv.reader(dbf)
982 print "file " + activecomics + " not found"
983 if not os.path.exists(dbdir):
984 print "path not found: " + dbdir
988 print "settings dir creation failed. Exiting."
991 # dbf = file(activecomics, 'w')
993 dbf = open(activecomics, 'w')
995 print "error storing settings"
997 dbw = csv.writer(dbf)
998 dbw.writerow(comics.keys())
1005 def get_markup(self):
1006 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1007 return '<span size="12000" face="arial"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1009 return '<span size="12000" face="arial"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1012 #check if settings file exists
1014 #if not, write defaults
1016 # ************************* OPTION DIALOGS ********************************
1018 def show_options(self, widget):
1019 print "loading options dialog"
1020 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1022 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1023 about_button.set_text("About", "See Author, Copyright and License information")
1024 about_button.set_alignment(0,0,0,0)
1025 about_button.connect("clicked", self.show_about)
1027 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1028 comics_button.set_text("Comics", "Choose which comics to view")
1029 comics_button.set_alignment(0,0,0,0)
1030 comics_button.connect("clicked", self.show_comics)
1032 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1033 # size_button.set_text("Change size", "Change the size of the widget")
1034 # size_button.set_alignment(0,0,0,0)
1035 # size_button.connect("clicked", self.show_size)
1037 print "adding buttons to dialog, starting with comics"
1038 dialog.vbox.pack_start(comics_button, True, True, 0)
1039 print "adding about button"
1040 dialog.vbox.pack_start(about_button, True, True, 0)
1041 # dialog.vbox.pack_start(size_button, True, True, 0)
1047 def show_about(self, widget):
1048 print "in about dialog"
1049 dialog = gtk.AboutDialog()
1050 dialog.set_title("About")
1051 dialog.set_name("Comic widget")
1052 dialog.set_version(APP_VERSION)
1053 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1054 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."])
1055 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1056 dialog.set_comments("Silly rabbit, tricks are for kids.")
1057 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/>.""")
1058 dialog.set_wrap_license(True)
1063 def show_comics(self, widget, data = None):
1064 print "in comics dialog! yay!"
1065 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1066 comiclist = comics.keys()
1070 self.liststore = gtk.ListStore(str,str,bool)
1071 for comicid in comiclist:
1073 for acomic in self.active_comics:
1074 if acomic == comicid:
1078 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1080 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1081 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1082 self.treeview.set_model(self.liststore)
1083 self.treeview.set_reorderable(True)
1084 tvcolumn = gtk.TreeViewColumn('Column 0')
1085 self.treeview.append_column(tvcolumn)
1086 cell = gtk.CellRendererText()
1087 tvcolumn.pack_start(cell, True)
1088 tvcolumn.add_attribute(cell, 'text', 0)
1090 ts = hildon.PannableArea()
1092 ts.set_size_request(-1, 280)
1093 ts.add(self.treeview)
1094 selection = self.treeview.get_selection()
1096 dialog.vbox.pack_start(ts,True,True,0)
1098 selection.connect("changed", self.pick_comic, comiclist)
1102 def pick_comic(self, widget, data):
1103 selected = widget.get_selected()
1104 if selected == None:
1107 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1108 if selected[0].get_value(selected[1],2):
1109 self.remove_comic(selected[0].get_value(selected[1],1))
1110 splt = selected[0].get_value(selected[1],0)[1:]
1111 selected[0].set_value(selected[1],0," " + splt)
1112 selected[0].set_value(selected[1],2,False)
1114 self.add_comic(selected[0].get_value(selected[1],1))
1115 splt = selected[0].get_value(selected[1],0)[1:]
1116 selected[0].set_value(selected[1],0,"*" + splt)
1117 selected[0].set_value(selected[1],2,True)
1120 def add_comic(self, comicid):
1121 if os.path.isfile(activecomics) == True:
1122 print "added " + comicid + " to " + str(self.active_comics)
1124 found = self.active_comics.index(comicid)
1126 self.active_comics.append(comicid)
1127 dbf = open(activecomics, 'w')
1128 dbw = csv.writer(dbf)
1129 dbw.writerow(self.active_comics)
1132 def remove_comic(self, comicid):
1133 if os.path.isfile(activecomics) == True:
1134 print "removing " + comicid + " from " + str(self.active_comics)
1136 del self.active_comics[self.active_comics.index(comicid)]
1139 dbf = open(activecomics, 'w')
1140 dbw = csv.writer(dbf)
1141 dbw.writerow(self.active_comics)
1145 def get_size_settings(self):
1146 # defaults = {'width':480,'height':230}
1147 # options: w 798 700 600 500 480 400
1148 # options: h 420 360 310 270 240 200
1149 print "checking to see if config file is available"
1150 if os.path.isfile(sizefile) == True:
1151 print "found " + sizefile
1152 dbf = open(sizefile, 'r')
1153 dbr = csv.reader(dbf)
1158 print "empty config file"
1162 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1163 if sizes['width'] > 798:
1164 sizes['width'] = 798
1165 if sizes['width'] < 400:
1166 sizes['width'] = 400
1167 if sizes['height'] > 420:
1168 sizes['height'] = 420
1169 if sizes['height'] < 200:
1170 sizes['height'] = 200
1177 print "no config file"
1180 hd_plugin_type = ComicHomePlugin
1183 if __name__ == "__main__":
1185 gobject.type_register(hd_plugin_type)
1186 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")