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 titl = str(self.db.db[self.db.currentcomic]['title'])
656 titl = titl[:19] + "..."
657 self.label.set_markup(self.get_markup())
660 self.e_goweb = gtk.EventBox()
661 self.e_goweb.set_name('goweb')
662 self.e_goweb.set_size_request(220, 48)
663 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
664 # it will change to be a cairo thing when I have the time.
665 self.e_open = gtk.EventBox()
666 self.e_open.set_name('open')
667 self.e_switch = gtk.EventBox()
668 self.e_switch.set_name('switch')
669 self.e_switch.set_size_request(48, 48)
670 self.e_next = gtk.EventBox()
671 self.e_next.set_name('next')
672 self.e_next.set_size_request(48, 48)
673 self.e_prev = gtk.EventBox()
674 self.e_prev.set_name('prev')
675 self.e_prev.set_size_request(48, 48)
676 self.e_up = gtk.EventBox()
677 self.e_up.set_name('up')
678 self.e_up.set_size_request(48, 48)
679 self.e_down = gtk.EventBox()
680 self.e_down.set_name('down')
681 self.e_down.set_size_request(48, 48)
682 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
683 self.comic_image.show()
686 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
688 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
689 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
690 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
691 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
692 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
693 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
694 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
695 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
696 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
697 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
702 self.e_goweb.add(self.label)
703 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
704 self.e_goweb.connect("button-release-event", self.view_comic)
706 self.e_open.add(self.comic_image)
708 # self.e_switch.add(self.switch)
709 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
710 self.e_switch.connect("button-press-event", self.button_press)
711 self.e_switch.connect("button-release-event", self.button_release)
712 self.e_switch.connect("leave-notify-event", self.button_release)
715 # self.e_next.add(self.next)
716 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
717 self.e_next.connect("button-press-event", self.button_press)
718 self.e_next.connect("button-release-event", self.button_release)
719 self.e_next.connect("leave-notify-event", self.button_release)
721 #self.e_prev.add(self.prev)
722 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
723 self.e_prev.connect("button-press-event", self.button_press)
724 self.e_prev.connect("button-release-event", self.button_release)
725 self.e_prev.connect("leave-notify-event", self.button_release)
727 #self.e_up.add(self.up)
728 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
729 self.e_up.connect("button-press-event", self.button_press)
730 self.e_up.connect("button-release-event", self.button_release)
731 self.e_up.connect("leave-notify-event", self.button_release)
734 #self.e_down.add(self.down)
735 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
736 self.e_down.connect("button-press-event", self.button_press)
737 self.e_down.connect("button-release-event", self.button_release)
738 self.e_down.connect("leave-notify-event", self.button_release)
740 self.vbox.pack_start(self.e_open,False,False,0)
741 self.vbox.pack_end(self.hbox,False,False,0)
743 self.hbox.pack_start(self.e_switch,False,False,0)
744 self.hbox.pack_start(self.e_goweb,False,False,10)
745 self.hbox.pack_end(self.e_next,False,False,0)
746 self.hbox.pack_end(self.e_prev,False,False,0)
747 self.hbox.pack_end(self.e_down,False,False,0)
748 self.hbox.pack_end(self.e_up,False,False,0)
754 # **************** Cairo draw functions ***************
756 def realize(self, widget):
757 global supports_alpha
759 # To check if the display supports alpha channels, get the colormap
760 screen = self.get_screen()
761 colormap = screen.get_rgba_colormap()
763 print 'Your screen does not support alpha channels!'
764 colormap = screen.get_rgb_colormap()
765 supports_alpha = False
767 # print 'Your screen supports alpha channels!'
768 supports_alpha = True
770 # Now we have a colormap appropriate for the screen, use it
771 self.set_colormap(colormap)
775 def do_expose_event(widget, event):
776 cr = widget.window.cairo_create()
777 widget.draw(widget.e_up, 0)
778 widget.draw(widget.e_down, 0)
779 widget.draw(widget.e_prev,0)
780 widget.draw(widget.e_next, 0)
781 widget.draw(widget.e_switch, 0)
782 # widget.writeit(widget.e_goweb, "asdf")
783 cr.set_operator(cairo.OPERATOR_OVER)
784 region = gtk.gdk.region_rectangle(event.area)
786 bg_color=gtk.gdk.color_parse('#000000')
788 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
790 cr.set_source_rgb (0.0, 0.0, 0.0)
795 # Function that draws a button preloaded in the images dictionary.
796 def draw(self, target, postfix):
797 cr_e = target.window.cairo_create()
798 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
799 cr_e.set_operator(cairo.OPERATOR_SOURCE)
801 cr_e.set_operator(cairo.OPERATOR_OVER)
802 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
805 # ********** Event handlers *************
808 # Most events happen on release, so I moved all of them into the same event handler.
809 def button_release(self, widget, event):
810 if event.type == gtk.gdk.BUTTON_RELEASE:
811 func = widget.get_name()
812 print "pressed: " + func
814 self.e_open.remove(self.comic_image)
815 self.imgvpos = self.imgvpos - 80
818 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
819 self.e_open.add(self.comic_image)
820 self.e_open.show_all()
823 self.e_open.remove(self.comic_image)
824 self.imgvpos = self.imgvpos + 80
825 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
826 self.e_open.add(self.comic_image)
827 self.e_open.show_all()
830 self.db.currentcomic -= 1
832 self.e_open.remove(self.comic_image)
833 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
834 self.e_open.add(self.comic_image)
835 self.e_open.show_all()
836 self.e_goweb.remove(self.label)
837 titl = str(self.db.db[self.db.currentcomic]['title'])
839 titl = titl[:19] + "..."
840 self.label.set_markup(self.get_markup())
841 self.e_goweb.add(self.label)
842 self.e_goweb.show_all()
845 self.db.currentcomic += 1
847 self.e_open.remove(self.comic_image)
848 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
849 self.e_open.add(self.comic_image)
850 self.e_open.show_all()
851 self.e_goweb.remove(self.label)
852 titl = str(self.db.db[self.db.currentcomic]['title'])
854 titl = titl[:19] + "..."
855 self.label.set_markup(self.get_markup())
856 self.e_goweb.add(self.label)
857 self.e_goweb.show_all()
859 elif func == 'switch':
860 self.active_comics = self.get_active_comics()
863 #sys.stdout = sys.__stdout__
865 #sys.stderr = sys.__stderr__
866 print "active comics: " + str(self.active_comics)
867 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
870 print "switching to " + self.active_comics[self.keypointer]
871 self.comicname = comics[self.active_comics[self.keypointer]]['name']
872 self.db = ComicDb(self.active_comics[self.keypointer])
873 self.e_open.remove(self.comic_image)
874 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
875 self.e_goweb.remove(self.label)
876 titl = str(self.db.db[self.db.currentcomic]['title'])
878 titl = titl[:19] + "..."
879 self.label.set_markup(self.get_markup())
880 self.e_goweb.add(self.label)
881 self.e_goweb.show_all()
882 self.e_open.add(self.comic_image)
883 self.e_open.show_all()
887 self.draw(widget, "0")
889 # this one only switches to pressed button image
890 def button_press(self, widget, event):
891 if not event.type == gtk.gdk.BUTTON_PRESS:
893 self.draw(widget, "1")
895 # this one cancels a button press when pointer is moved out from it after press.
896 def button_out(self, widget, event):
897 if not event.type == gtk.gdk.BUTTON_PRESS:
899 self.draw(widget, "0")
901 # no fancy cairo stuff here. simply launches url in browser.
902 def view_comic(self, widget, event):
903 if not event.type == gtk.gdk.BUTTON_RELEASE:
905 link = self.db.get_link()
906 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
910 # this function modifies the image to fit the widget and then returns it.
912 def get_resized_pixmap(self, filename, vpos):
913 print "vpos: " + str(vpos)
915 width = int(self.sizes['width'])
916 height = int(self.sizes['height'])
918 maxheight = height - 48
920 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
921 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
922 parent_buf.fill(0xffffffff)
924 # get correct width/height
927 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
928 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
929 print "scaled height: " + str(scaled_buf.get_height())
930 if scaled_buf.get_height() > maxheight:
931 if scaled_buf.get_height() - (vpos + maxheight) < 0:
932 vpos = scaled_buf.get_height() - maxheight
933 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
934 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
937 tmpy = maxheight - scaled_buf.get_height()
941 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
943 # test = parent_buf.render_pixmap_and_mask(0)
944 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
945 retimg.set_from_pixbuf(parent_buf)
946 print retimg.get_pixel_size()
947 print "vpos after fixing image: " + str(self.imgvpos)
950 def screen_changed(self, widget, old_screen=None):
951 global supports_alpha
953 # To check if the display supports alpha channels, get the colormap
954 screen = self.get_screen()
955 colormap = screen.get_rgba_colormap()
957 print 'Your screen does not support alpha channels!'
958 colormap = screen.get_rgb_colormap()
959 supports_alpha = False
961 # print 'Your screen supports alpha channels!'
962 supports_alpha = True
964 # Now we have a colormap appropriate for the screen, use it
965 self.set_colormap(colormap)
970 def get_active_comics(self):
973 if os.path.isfile(activecomics) == True:
974 print "found " + activecomics
975 dbf = open(activecomics, 'r')
976 dbr = csv.reader(dbf)
985 print "file " + activecomics + " not found"
986 if not os.path.exists(dbdir):
987 print "path not found: " + dbdir
991 print "settings dir creation failed. Exiting."
994 # dbf = file(activecomics, 'w')
996 dbf = open(activecomics, 'w')
998 print "error storing settings"
1000 dbw = csv.writer(dbf)
1001 dbw.writerow(comics.keys())
1008 def get_markup(self):
1009 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1010 return '<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1012 return '<span size="12000" face="monospace"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1015 #check if settings file exists
1017 #if not, write defaults
1019 # ************************* OPTION DIALOGS ********************************
1021 def show_options(self, widget):
1022 print "loading options dialog"
1023 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1025 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1026 about_button.set_text("About", "See Author, Copyright and License information")
1027 about_button.set_alignment(0,0,0,0)
1028 about_button.connect("clicked", self.show_about)
1030 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1031 comics_button.set_text("Comics", "Choose which comics to view")
1032 comics_button.set_alignment(0,0,0,0)
1033 comics_button.connect("clicked", self.show_comics)
1035 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1036 # size_button.set_text("Change size", "Change the size of the widget")
1037 # size_button.set_alignment(0,0,0,0)
1038 # size_button.connect("clicked", self.show_size)
1040 print "adding buttons to dialog, starting with comics"
1041 dialog.vbox.pack_start(comics_button, True, True, 0)
1042 print "adding about button"
1043 dialog.vbox.pack_start(about_button, True, True, 0)
1044 # dialog.vbox.pack_start(size_button, True, True, 0)
1050 def show_about(self, widget):
1051 print "in about dialog"
1052 dialog = gtk.AboutDialog()
1053 dialog.set_title("About")
1054 dialog.set_name("Comic widget")
1055 dialog.set_version(APP_VERSION)
1056 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1057 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."])
1058 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1059 dialog.set_comments("Silly rabbit, tricks are for kids.")
1060 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/>.""")
1061 dialog.set_wrap_license(True)
1066 def show_comics(self, widget, data = None):
1067 print "in comics dialog! yay!"
1068 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1069 comiclist = comics.keys()
1073 self.liststore = gtk.ListStore(str,str,bool)
1074 for comicid in comiclist:
1076 for acomic in self.active_comics:
1077 if acomic == comicid:
1081 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1083 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1084 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1085 self.treeview.set_model(self.liststore)
1086 self.treeview.set_reorderable(True)
1087 tvcolumn = gtk.TreeViewColumn('Column 0')
1088 self.treeview.append_column(tvcolumn)
1089 cell = gtk.CellRendererText()
1090 tvcolumn.pack_start(cell, True)
1091 tvcolumn.add_attribute(cell, 'text', 0)
1093 ts = hildon.PannableArea()
1095 ts.set_size_request(-1, 280)
1096 ts.add(self.treeview)
1097 selection = self.treeview.get_selection()
1099 dialog.vbox.pack_start(ts,True,True,0)
1101 selection.connect("changed", self.pick_comic, comiclist)
1105 def pick_comic(self, widget, data):
1106 selected = widget.get_selected()
1107 if selected == None:
1110 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1111 if selected[0].get_value(selected[1],2):
1112 self.remove_comic(selected[0].get_value(selected[1],1))
1113 splt = selected[0].get_value(selected[1],0)[1:]
1114 selected[0].set_value(selected[1],0," " + splt)
1115 selected[0].set_value(selected[1],2,False)
1117 self.add_comic(selected[0].get_value(selected[1],1))
1118 splt = selected[0].get_value(selected[1],0)[1:]
1119 selected[0].set_value(selected[1],0,"*" + splt)
1120 selected[0].set_value(selected[1],2,True)
1123 def add_comic(self, comicid):
1124 if os.path.isfile(activecomics) == True:
1125 print "added " + comicid + " to " + str(self.active_comics)
1127 found = self.active_comics.index(comicid)
1129 self.active_comics.append(comicid)
1130 dbf = open(activecomics, 'w')
1131 dbw = csv.writer(dbf)
1132 dbw.writerow(self.active_comics)
1135 def remove_comic(self, comicid):
1136 if os.path.isfile(activecomics) == True:
1137 print "removing " + comicid + " from " + str(self.active_comics)
1139 del self.active_comics[self.active_comics.index(comicid)]
1142 dbf = open(activecomics, 'w')
1143 dbw = csv.writer(dbf)
1144 dbw.writerow(self.active_comics)
1148 def get_size_settings(self):
1149 # defaults = {'width':480,'height':230}
1150 # options: w 798 700 600 500 480 400
1151 # options: h 420 360 310 270 240 200
1152 print "checking to see if config file is available"
1153 if os.path.isfile(sizefile) == True:
1154 print "found " + sizefile
1155 dbf = open(sizefile, 'r')
1156 dbr = csv.reader(dbf)
1161 print "empty config file"
1165 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1166 if sizes['width'] > 798:
1167 sizes['width'] = 798
1168 if sizes['width'] < 400:
1169 sizes['width'] = 400
1170 if sizes['height'] > 420:
1171 sizes['height'] = 420
1172 if sizes['height'] < 200:
1173 sizes['height'] = 200
1180 print "no config file"
1183 hd_plugin_type = ComicHomePlugin
1186 if __name__ == "__main__":
1188 gobject.type_register(hd_plugin_type)
1189 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")