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
25 APP_VERSION = "0.3.2-2"
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 and Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
37 "wulff":{"name":"Wulffmorgenthaler","link":"http://wulffmorgenthaler.com/","start":"edd3411b-96ca-4d93-bd5f-0cf1deb67c8a","dbfile":dbdir + "comicdb.wulff.csv"},
40 defaults = {'width':480,'height':230}
41 #defaults = {'width':480,'height':240}
42 sizefile = "/home/user/.comic-widget/size.cfg"
47 # handling of the comics
49 def __init__(self, comic):
51 self.start = comics[self.comic]["start"]
52 self.dbfile = comics[self.comic]["dbfile"]
54 #if db file exist, read it
55 # if os.path.isfile(self.dbfile) == True:
56 # dbf = open(self.dbfile, 'r')
58 dbf = self.get_dbfile()
59 dbr = csv.DictReader(dbf)
71 #if db file exist, read it
72 print "fetching " + self.dbfile
73 if os.path.isfile(self.dbfile) == True:
74 print "found " + self.dbfile
75 dbf = open(self.dbfile, 'r')
79 print "file " + self.dbfile + " not found"
80 if not os.path.exists(dbdir):
81 print "path not found: " + dbdir
85 print "comic db creation failed on mkdir"
88 shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
90 print "comic db creation failed on copy"
91 print "creating new..."
92 dbf = open(self.dbfile, 'w')
93 dbf.write('comic,id,link,url,filename,title\n')
96 if os.path.isfile(self.dbfile) == True:
97 dbf = open(self.dbfile, 'r')
100 print "comic db creation failed after copy"
104 print str(self.currentcomic)
105 if self.currentcomic < 0:
106 self.currentcomic = 0
108 fetchid = self.db[self.currentcomic]['id']
110 fetchid = comics[self.comic]['start']
111 self.fetch_newer(self.comic, fetchid)
113 if len(self.db) < (self.currentcomic + 1):
114 self.currentcomic = len(self.db) - 1
116 fetchid = self.db[self.currentcomic]['id']
118 fetchid = comics[self.comic]['start']
119 self.fetch_earlier(self.comic, fetchid)
121 self.currentcomic = len(self.db) - 1
123 dbrow = self.db[self.currentcomic]
124 filename = comiccache + self.comic + "/" + dbrow['filename']
125 print filename + "\n"
127 if os.path.isfile(filename):
128 print "found, returning " + filename + "\n"
132 print filename + " not found\nretrieving " + dbrow['url'] + "\n"
133 if not os.path.exists(comiccache + self.comic + "/"):
134 print "create dir " + comiccache + self.comic + "/"
136 os.makedirs(comiccache + self.comic + "/")
139 print "comic db creation failed on mkdir"
140 urllib.urlretrieve(dbrow['url'], filename)
144 def get_current_row(self):
145 return self.db[self.currentcomic]
148 print str(self.currentcomic)
149 if self.currentcomic < 0:
151 self.currentcomic = 0
152 if len(self.db) < (self.currentcomic + 1):
153 self.currentcomic -= 1
154 return self.db[self.currentcomic]['link']
158 def insert_row(self, irow):
159 dbf = open(self.dbfile, 'a')
160 dbw = csv.writer(dbf)
164 def insert_row_first(self, irow):
165 dbf = open(self.dbfile, 'w')
166 dbf.write('comic,id,link,url,filename,title\n')
167 dbw = csv.writer(dbf)
170 dbf = open(self.dbfile, 'a')
171 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
179 # if len(self.db) < 1:
180 # self.currentcomic = -1
181 # self.fetch_latest_std(self.comic, self.start)
182 # elif self.currentcomic == 0 or self.currentcomic < 0:
183 # self.fetch_latest_std(self.comic, self.db[0]['id'])
184 # elif self.currentcomic == (len(self.db) - 1):
185 # self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
186 dbf = open(self.dbfile, 'r')
187 dbr = csv.DictReader(dbf)
190 self.db.insert(0,row)
195 def fetch_earlier(self, comic, earliest):
196 print "fetch before, " + comic + " earliest"
197 if comic == "cyanide" or comic == "wulff":
198 comicid = self.get_prev_id(comic,earliest)
200 print "already at first comic"
202 print "got " + comicid + " as the one before current..."
203 elif len(earliest) == 10:
205 dt = string.split(earliest, "-")
206 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
207 earlier = d - datetime.timedelta( 1 )
208 comicid = earlier.isoformat()
210 comicid = int(earliest) - 1
212 irow = self.get_irow(comic, comicid)
213 if irow and irow[0] == "skip":
214 print "problem with this one, fetching " + str(irow[2]) + " instead."
215 irow = self.get_irow(comic, irow[2])
217 if irow and len(irow) > 3:
220 print "\ninserting...\n"
221 self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
223 print "No comic found at " + comicid
229 def fetch_newer(self, comic, newest):
230 if comic == "cyanide" or comic == "wulff":
231 comicid = self.get_next_id(comic,newest)
233 print "already at last comic"
235 elif len(newest) == 10:
237 dt = string.split(newest, "-")
238 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
239 newest = d + datetime.timedelta( 1 )
240 comicid = newest.isoformat()
242 comicid = int(newest) + 1
244 irow = self.get_irow(comic, comicid)
245 if irow and irow[0] == "skip":
246 print "problem with this one, fetching " + str(irow[1]) + " instead."
247 irow = self.get_irow(comic, irow[1])
249 if irow and len(irow) > 3:
252 print "\ninserting...\n"
253 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
255 print "No comic found at " + str(comicid)
260 def fetch_latest_std(self, comic, latest):
261 print "fetching new after " + str(comic) + " " + str(latest)
264 if comic == 'cyanide':
265 next = self.get_next_id(comic, latest)
271 if len(str(latest)) == 10:
274 dt = string.split(latest, "-")
275 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
276 newer = d + datetime.timedelta( 1 )
277 comicid = newer.isoformat()
279 comicid = int(latest) + 1
282 lasturl = self.db[0]['url']
286 irow = self.get_irow(comic, comicid)
288 print "got irow: " + str(irow)
289 if str(irow[0]) == 'skip':
290 print "skipping this one..."
293 if irow[3] == lasturl:
294 print "Looping the same, break break break!"
298 self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
304 dt = string.split(comicid, "-")
305 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
306 newer = d + datetime.timedelta( 1 )
307 comicid = newer.isoformat()
318 def get_next_id(self, comic, number):
319 if comic == 'cyanide':
320 link = "http://www.explosm.net/comics/" + str(number) + "/"
321 print "link: " + link
323 f = urllib2.urlopen(link)
327 print "got hcode = " + str(hcode) + "\n"
331 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
335 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
337 print "no 'next' found"
339 splt = string.split(splt[1], '/">Next >', 1)
341 print "no 'next' found"
344 print "got next: " + splt[0]
347 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
348 print "link: " + link
350 f = urllib2.urlopen(link)
354 print "got hcode = " + str(hcode) + "\n"
361 splt = string.split(s, '<a href="/default.aspx?id=', 1)
362 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
363 print "no previous found"
364 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
365 print "no next found!"
368 splt = string.split(splt[1], '<a href="/default.aspx?id=', 1)
370 print "no next found! At newest comic!"
372 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
373 print "no next found!"
379 def get_prev_id(self, comic, number):
380 if comic == 'cyanide':
381 link = "http://www.explosm.net/comics/" + str(number) + "/"
382 print "link: " + link
384 f = urllib2.urlopen(link)
388 print "got hcode = " + str(hcode) + "\n"
392 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
396 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
398 print "no 'prev' found"
400 splt = string.split(splt[1], '/">< Previous', 1)
401 if len(splt) < 2 or len(splt[0]) > 5:
402 print "no 'next' found"
405 print "got previous: " + splt[0]
408 link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
409 print "link: " + link
411 f = urllib2.urlopen(link)
415 print "got hcode = " + str(hcode) + "\n"
422 splt = string.split(s, '<a href="/default.aspx?id=', 1)
423 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
426 print "no previous found"
436 def get_irow(self, comic, number):
438 if len(str(number)) == 10:
443 print "number is now: " + str(number) + "\n"
445 link = "http://xkcd.org/" + str(number) + "/"
446 elif comic == 'sinfest':
447 link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
448 elif comic == 'dilbert':
449 link = "http://dilbert.com/" + str(number) + "/"
451 link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
452 elif comic == 'cyanide':
453 link = "http://www.explosm.net/comics/" + str(number) + "/"
454 elif comic == 'wulff':
455 link = "http://wulffmorgenthaler.com/default.aspx?id=" + str(number)
459 print "link: " + link
461 f = urllib2.urlopen(link)
465 print "got hcode = " + str(hcode) + "\n"
470 # they look like: comic,id,link,url,filename,title
471 # We already have comic, number/id and link
476 # This should be done with regex but...
477 splt = string.split(s, "<h3>Image URL (for hotlinking/embedding): ", 1)
478 splt2 = string.split(splt[1], "</h3>", 1)
480 splt = string.split(splt[0], "<h1>", 1)
481 splt = string.split(splt[1], "</h1>", 1)
485 elif comic == 'sinfest':
488 splt = string.split(s, 'height="107"', 1)
489 splt = string.split(splt[1], '<img src="', 1)
490 splt = string.split(splt[1], '" alt="', 1)
493 print "Fake 404! Break break break!"
495 splt = string.split(splt[1], '" border="0" />', 1)
498 elif comic == 'dilbert':
501 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
502 splt = string.split(splt[1], '" />', 1)
503 url = "http://dilbert.com" + splt[0]
505 print "Fake 404! Break break break!"
513 splt = string.split(s, '<title>PHD Comics: ', 1)
514 splt = string.split(splt[1], '</title>', 1)
516 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
517 splt = string.split(splt[1], ' align=top>', 1)
520 print "Fake 404! Break break break!"
523 elif comic == 'cyanide':
527 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
532 splt = string.split(splt[1], '/">< ', 1)
534 print "got prev: " + prev
535 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
540 splt = string.split(splt[1], '/">Next ></a>', 1)
541 if len(splt[0]) > 10:
545 print "got next: " + next
547 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
551 splt = string.split(splt[1], ' <b>by <a href="', 1)
554 print "title wrong..."
556 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
558 print "a video? Try skipping"
559 return ['skip',next,prev]
561 splt = string.split(splt[1], '">', 1)
562 url = "http://www.explosm" + splt[0]
563 splt2 = string.rsplit(url, "/", 1)
565 irow = [comic,number,link,url,filename,title,next,prev]
567 elif comic == 'wulff':
571 # <img src="pics/dates/month/2.gif" alt="" />
572 # <img src="pics/dates/day/4.gif" alt="" />
573 # <img src="pics/dates/komma.gif" alt="" />
574 # <img src="pics/dates/year/2010.gif" alt="" />
575 splt = string.split(s, '<img src="pics/dates/month/', 1)
576 if not splt[1][1:2] == ".":
579 title = "0" + splt[1][:1]
580 splt = string.split(splt[1], '<img src="pics/dates/day/', 1)
581 if not splt[1][1:2] == ".":
582 title = title + "-" + splt[1][:2]
584 title = title + "-0" + splt[1][:1]
585 splt = string.split(splt[1], '<img src="pics/dates/year/', 1)
586 title = splt[1][:4] + "-" + title
588 splt = string.split(splt[1], 'id="ctl00_content_Strip1_imgStrip" class="strip" src="striphandler.ashx?stripid=', 1)
589 number = splt[1][:36]
590 url = "http://wulffmorgenthaler.com/striphandler.ashx?stripid=" + number
591 filename = number + ".gif"
594 splt2 = string.rsplit(url, "/", 1)
597 if filename == self.db[0]['filename']:
598 print "already exists! Break break break!"
602 irow = [comic,number,link,url,filename,title]
608 class ComicHomePlugin(hildondesktop.HomePluginItem):
609 # __gtype_name__ = 'ComicHomePlugin'
611 hildondesktop.HomePluginItem.__init__(self)
612 global supports_alpha
613 self.set_settings(True)
614 self.active_comics = self.get_active_comics()
615 self.sizes = self.get_size_settings()
616 self.new_width = self.sizes['width']
617 self.new_height = self.sizes['height']
618 self.connect("show-settings", self.show_options)
619 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
620 self.osso_rpc = osso.Rpc(self.osso_c)
622 if len(self.active_comics) < 1:
623 self.active_comics = ['xkcd']
624 self.db = ComicDb(self.active_comics[self.keypointer])
625 self.comicname = comics[self.active_comics[self.keypointer]]['name']
626 self.set_name = "comicwidget"
627 screen = self.get_screen()
628 colormap = screen.get_rgba_colormap()
630 colormap = screen.get_rgb_colormap()
631 supports_alpha = False
633 supports_alpha = True
634 self.set_colormap(colormap)
635 self.set_app_paintable(True)
637 self.set_size_request(self.sizes['width'], self.sizes['height'])
639 self.vbox = gtk.VBox()
640 self.hbox = gtk.HBox()
641 self.label = gtk.Label()
642 self.label.set_alignment(xalign = 0, yalign = 0.5)
643 self.label.set_use_markup(True)
644 titl = str(self.db.db[self.db.currentcomic]['title'])
646 titl = titl[:19] + "..."
647 self.label.set_markup(self.get_markup())
650 self.e_goweb = gtk.EventBox()
651 self.e_goweb.set_name('goweb')
652 self.e_goweb.set_size_request(220, 48)
653 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
654 # it will change to be a cairo thing when I have the time.
655 self.e_open = gtk.EventBox()
656 self.e_open.set_name('open')
657 self.e_switch = gtk.EventBox()
658 self.e_switch.set_name('switch')
659 self.e_switch.set_size_request(48, 48)
660 self.e_next = gtk.EventBox()
661 self.e_next.set_name('next')
662 self.e_next.set_size_request(48, 48)
663 self.e_prev = gtk.EventBox()
664 self.e_prev.set_name('prev')
665 self.e_prev.set_size_request(48, 48)
666 self.e_up = gtk.EventBox()
667 self.e_up.set_name('up')
668 self.e_up.set_size_request(48, 48)
669 self.e_down = gtk.EventBox()
670 self.e_down.set_name('down')
671 self.e_down.set_size_request(48, 48)
672 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
673 self.comic_image.show()
676 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
678 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
679 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
680 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
681 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
682 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
683 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
684 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
685 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
686 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
687 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
692 self.e_goweb.add(self.label)
693 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
694 self.e_goweb.connect("button-release-event", self.view_comic)
696 self.e_open.add(self.comic_image)
698 # self.e_switch.add(self.switch)
699 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
700 self.e_switch.connect("button-press-event", self.button_press)
701 self.e_switch.connect("button-release-event", self.button_release)
702 self.e_switch.connect("leave-notify-event", self.button_release)
705 # self.e_next.add(self.next)
706 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
707 self.e_next.connect("button-press-event", self.button_press)
708 self.e_next.connect("button-release-event", self.button_release)
709 self.e_next.connect("leave-notify-event", self.button_release)
711 #self.e_prev.add(self.prev)
712 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
713 self.e_prev.connect("button-press-event", self.button_press)
714 self.e_prev.connect("button-release-event", self.button_release)
715 self.e_prev.connect("leave-notify-event", self.button_release)
717 #self.e_up.add(self.up)
718 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
719 self.e_up.connect("button-press-event", self.button_press)
720 self.e_up.connect("button-release-event", self.button_release)
721 self.e_up.connect("leave-notify-event", self.button_release)
724 #self.e_down.add(self.down)
725 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
726 self.e_down.connect("button-press-event", self.button_press)
727 self.e_down.connect("button-release-event", self.button_release)
728 self.e_down.connect("leave-notify-event", self.button_release)
730 self.vbox.pack_start(self.e_open,False,False,0)
731 self.vbox.pack_end(self.hbox,False,False,0)
733 self.hbox.pack_start(self.e_switch,False,False,0)
734 self.hbox.pack_start(self.e_goweb,False,False,10)
735 self.hbox.pack_end(self.e_next,False,False,0)
736 self.hbox.pack_end(self.e_prev,False,False,0)
737 self.hbox.pack_end(self.e_down,False,False,0)
738 self.hbox.pack_end(self.e_up,False,False,0)
744 # **************** Cairo draw functions ***************
746 def realize(self, widget):
747 global supports_alpha
749 # To check if the display supports alpha channels, get the colormap
750 screen = self.get_screen()
751 colormap = screen.get_rgba_colormap()
753 print 'Your screen does not support alpha channels!'
754 colormap = screen.get_rgb_colormap()
755 supports_alpha = False
757 # print 'Your screen supports alpha channels!'
758 supports_alpha = True
760 # Now we have a colormap appropriate for the screen, use it
761 self.set_colormap(colormap)
765 def do_expose_event(widget, event):
766 cr = widget.window.cairo_create()
767 widget.draw(widget.e_up, 0)
768 widget.draw(widget.e_down, 0)
769 widget.draw(widget.e_prev,0)
770 widget.draw(widget.e_next, 0)
771 widget.draw(widget.e_switch, 0)
772 # widget.writeit(widget.e_goweb, "asdf")
773 cr.set_operator(cairo.OPERATOR_OVER)
774 region = gtk.gdk.region_rectangle(event.area)
776 bg_color=gtk.gdk.color_parse('#000000')
778 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
780 cr.set_source_rgb (0.0, 0.0, 0.0)
785 # Function that draws a button preloaded in the images dictionary.
786 def draw(self, target, postfix):
787 cr_e = target.window.cairo_create()
788 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
789 cr_e.set_operator(cairo.OPERATOR_SOURCE)
791 cr_e.set_operator(cairo.OPERATOR_OVER)
792 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
795 # ********** Event handlers *************
798 # Most events happen on release, so I moved all of them into the same event handler.
799 def button_release(self, widget, event):
800 if event.type == gtk.gdk.BUTTON_RELEASE:
801 func = widget.get_name()
802 print "pressed: " + func
804 self.e_open.remove(self.comic_image)
805 self.imgvpos = self.imgvpos - 80
808 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
809 self.e_open.add(self.comic_image)
810 self.e_open.show_all()
813 self.e_open.remove(self.comic_image)
814 self.imgvpos = self.imgvpos + 80
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.db.currentcomic -= 1
822 self.e_open.remove(self.comic_image)
823 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
824 self.e_open.add(self.comic_image)
825 self.e_open.show_all()
826 self.e_goweb.remove(self.label)
827 titl = str(self.db.db[self.db.currentcomic]['title'])
829 titl = titl[:19] + "..."
830 self.label.set_markup(self.get_markup())
831 self.e_goweb.add(self.label)
832 self.e_goweb.show_all()
835 self.db.currentcomic += 1
837 self.e_open.remove(self.comic_image)
838 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
839 self.e_open.add(self.comic_image)
840 self.e_open.show_all()
841 self.e_goweb.remove(self.label)
842 titl = str(self.db.db[self.db.currentcomic]['title'])
844 titl = titl[:19] + "..."
845 self.label.set_markup(self.get_markup())
846 self.e_goweb.add(self.label)
847 self.e_goweb.show_all()
849 elif func == 'switch':
850 self.active_comics = self.get_active_comics()
853 #sys.stdout = sys.__stdout__
855 #sys.stderr = sys.__stderr__
856 print "active comics: " + str(self.active_comics)
857 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
860 print "switching to " + self.active_comics[self.keypointer]
861 self.comicname = comics[self.active_comics[self.keypointer]]['name']
862 self.db = ComicDb(self.active_comics[self.keypointer])
863 self.e_open.remove(self.comic_image)
864 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
865 self.e_goweb.remove(self.label)
866 titl = str(self.db.db[self.db.currentcomic]['title'])
868 titl = titl[:19] + "..."
869 self.label.set_markup(self.get_markup())
870 self.e_goweb.add(self.label)
871 self.e_goweb.show_all()
872 self.e_open.add(self.comic_image)
873 self.e_open.show_all()
877 self.draw(widget, "0")
879 # this one only switches to pressed button image
880 def button_press(self, widget, event):
881 if not event.type == gtk.gdk.BUTTON_PRESS:
883 self.draw(widget, "1")
885 # this one cancels a button press when pointer is moved out from it after press.
886 def button_out(self, widget, event):
887 if not event.type == gtk.gdk.BUTTON_PRESS:
889 self.draw(widget, "0")
891 # no fancy cairo stuff here. simply launches url in browser.
892 def view_comic(self, widget, event):
893 if not event.type == gtk.gdk.BUTTON_RELEASE:
895 link = self.db.get_link()
896 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
900 # this function modifies the image to fit the widget and then returns it.
902 def get_resized_pixmap(self, filename, vpos):
903 print "vpos: " + str(vpos)
905 width = int(self.sizes['width'])
906 height = int(self.sizes['height'])
908 maxheight = height - 48
910 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
911 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
912 parent_buf.fill(0xffffffff)
914 # get correct width/height
917 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
918 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
919 print "scaled height: " + str(scaled_buf.get_height())
920 if scaled_buf.get_height() > maxheight:
921 if scaled_buf.get_height() - (vpos + maxheight) < 0:
922 vpos = scaled_buf.get_height() - maxheight
923 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
924 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
927 tmpy = maxheight - scaled_buf.get_height()
931 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
933 # test = parent_buf.render_pixmap_and_mask(0)
934 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
935 retimg.set_from_pixbuf(parent_buf)
936 print retimg.get_pixel_size()
937 print "vpos after fixing image: " + str(self.imgvpos)
940 def screen_changed(self, widget, old_screen=None):
941 global supports_alpha
943 # To check if the display supports alpha channels, get the colormap
944 screen = self.get_screen()
945 colormap = screen.get_rgba_colormap()
947 print 'Your screen does not support alpha channels!'
948 colormap = screen.get_rgb_colormap()
949 supports_alpha = False
951 # print 'Your screen supports alpha channels!'
952 supports_alpha = True
954 # Now we have a colormap appropriate for the screen, use it
955 self.set_colormap(colormap)
960 def get_active_comics(self):
963 if os.path.isfile(activecomics) == True:
964 print "found " + activecomics
965 dbf = open(activecomics, 'r')
966 dbr = csv.reader(dbf)
975 print "file " + activecomics + " not found"
976 if not os.path.exists(dbdir):
977 print "path not found: " + dbdir
981 print "settings dir creation failed. Exiting."
984 # dbf = file(activecomics, 'w')
986 dbf = open(activecomics, 'w')
988 print "error storing settings"
990 dbw = csv.writer(dbf)
991 dbw.writerow(comics.keys())
998 def get_markup(self):
999 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1000 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>'
1002 return '<span size="12000" face="monospace"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1005 #check if settings file exists
1007 #if not, write defaults
1009 # ************************* OPTION DIALOGS ********************************
1011 def show_options(self, widget):
1012 print "loading options dialog"
1013 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1015 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1016 about_button.set_text("About", "See Author, Copyright and License information")
1017 about_button.set_alignment(0,0,0,0)
1018 about_button.connect("clicked", self.show_about)
1020 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1021 comics_button.set_text("Comics", "Choose which comics to view")
1022 comics_button.set_alignment(0,0,0,0)
1023 comics_button.connect("clicked", self.show_comics)
1025 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1026 # size_button.set_text("Change size", "Change the size of the widget")
1027 # size_button.set_alignment(0,0,0,0)
1028 # size_button.connect("clicked", self.show_size)
1030 print "adding buttons to dialog, starting with comics"
1031 dialog.vbox.pack_start(comics_button, True, True, 0)
1032 print "adding about button"
1033 dialog.vbox.pack_start(about_button, True, True, 0)
1034 # dialog.vbox.pack_start(size_button, True, True, 0)
1040 def show_about(self, widget):
1041 print "in about dialog"
1042 dialog = gtk.AboutDialog()
1043 dialog.set_title("About")
1044 dialog.set_name("Comic widget")
1045 dialog.set_version(APP_VERSION)
1046 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1047 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."])
1048 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1049 dialog.set_comments("Silly rabbit, tricks are for kids.")
1050 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/>.""")
1051 dialog.set_wrap_license(True)
1056 def show_comics(self, widget, data = None):
1057 print "in comics dialog! yay!"
1058 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1059 comiclist = comics.keys()
1063 self.liststore = gtk.ListStore(str,str,bool)
1064 for comicid in comiclist:
1066 for acomic in self.active_comics:
1067 if acomic == comicid:
1071 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1073 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1074 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1075 self.treeview.set_model(self.liststore)
1076 self.treeview.set_reorderable(True)
1077 tvcolumn = gtk.TreeViewColumn('Column 0')
1078 self.treeview.append_column(tvcolumn)
1079 cell = gtk.CellRendererText()
1080 tvcolumn.pack_start(cell, True)
1081 tvcolumn.add_attribute(cell, 'text', 0)
1083 ts = hildon.PannableArea()
1085 ts.set_size_request(-1, 280)
1086 ts.add(self.treeview)
1087 selection = self.treeview.get_selection()
1089 dialog.vbox.pack_start(ts,True,True,0)
1091 selection.connect("changed", self.pick_comic, comiclist)
1095 def pick_comic(self, widget, data):
1096 from print_r import print_r
1097 selected = widget.get_selected()
1098 if selected == None:
1101 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1102 if selected[0].get_value(selected[1],2):
1103 self.remove_comic(selected[0].get_value(selected[1],1))
1104 splt = selected[0].get_value(selected[1],0)[1:]
1105 selected[0].set_value(selected[1],0," " + splt)
1106 selected[0].set_value(selected[1],2,False)
1108 self.add_comic(selected[0].get_value(selected[1],1))
1109 splt = selected[0].get_value(selected[1],0)[1:]
1110 selected[0].set_value(selected[1],0,"*" + splt)
1111 selected[0].set_value(selected[1],2,True)
1114 def add_comic(self, comicid):
1115 if os.path.isfile(activecomics) == True:
1116 print "added " + comicid + " to " + str(self.active_comics)
1118 found = self.active_comics.index(comicid)
1120 self.active_comics.append(comicid)
1121 dbf = open(activecomics, 'w')
1122 dbw = csv.writer(dbf)
1123 dbw.writerow(self.active_comics)
1126 def remove_comic(self, comicid):
1127 if os.path.isfile(activecomics) == True:
1128 print "removing " + comicid + " from " + str(self.active_comics)
1130 del self.active_comics[self.active_comics.index(comicid)]
1133 dbf = open(activecomics, 'w')
1134 dbw = csv.writer(dbf)
1135 dbw.writerow(self.active_comics)
1139 def get_size_settings(self):
1140 # defaults = {'width':480,'height':230}
1141 # options: w 798 700 600 500 480 400
1142 # options: h 420 360 310 270 240 200
1143 print "checking to see if config file is available"
1144 if os.path.isfile(sizefile) == True:
1145 print "found " + sizefile
1146 dbf = open(sizefile, 'r')
1147 dbr = csv.reader(dbf)
1152 print "empty config file"
1156 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1157 if sizes['width'] > 798:
1158 sizes['width'] = 798
1159 if sizes['width'] < 400:
1160 sizes['width'] = 400
1161 if sizes['height'] > 420:
1162 sizes['height'] = 420
1163 if sizes['height'] < 200:
1164 sizes['height'] = 200
1171 print "no config file"
1174 hd_plugin_type = ComicHomePlugin
1177 if __name__ == "__main__":
1179 gobject.type_register(hd_plugin_type)
1180 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")