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.4-1"
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"
592 irow = [comic,number,link,url,filename,title]
596 splt2 = string.rsplit(url, "/", 1)
599 if filename == self.db[0]['filename']:
600 print "already exists! Break break break!"
604 irow = [comic,number,link,url,filename,title]
610 class ComicHomePlugin(hildondesktop.HomePluginItem):
611 # __gtype_name__ = 'ComicHomePlugin'
613 hildondesktop.HomePluginItem.__init__(self)
614 global supports_alpha
615 self.set_settings(True)
616 self.active_comics = self.get_active_comics()
617 self.sizes = self.get_size_settings()
618 self.new_width = self.sizes['width']
619 self.new_height = self.sizes['height']
620 self.connect("show-settings", self.show_options)
621 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
622 self.osso_rpc = osso.Rpc(self.osso_c)
624 if len(self.active_comics) < 1:
625 self.active_comics = ['xkcd']
626 self.db = ComicDb(self.active_comics[self.keypointer])
627 self.comicname = comics[self.active_comics[self.keypointer]]['name']
628 self.set_name = "comicwidget"
629 screen = self.get_screen()
630 colormap = screen.get_rgba_colormap()
632 colormap = screen.get_rgb_colormap()
633 supports_alpha = False
635 supports_alpha = True
636 self.set_colormap(colormap)
637 self.set_app_paintable(True)
639 self.set_size_request(self.sizes['width'], self.sizes['height'])
641 self.vbox = gtk.VBox()
642 self.hbox = gtk.HBox()
643 self.label = gtk.Label()
644 self.label.set_alignment(xalign = 0, yalign = 0.5)
645 self.label.set_use_markup(True)
646 titl = str(self.db.db[self.db.currentcomic]['title'])
648 titl = titl[:19] + "..."
649 self.label.set_markup(self.get_markup())
652 self.e_goweb = gtk.EventBox()
653 self.e_goweb.set_name('goweb')
654 self.e_goweb.set_size_request(220, 48)
655 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
656 # it will change to be a cairo thing when I have the time.
657 self.e_open = gtk.EventBox()
658 self.e_open.set_name('open')
659 self.e_switch = gtk.EventBox()
660 self.e_switch.set_name('switch')
661 self.e_switch.set_size_request(48, 48)
662 self.e_next = gtk.EventBox()
663 self.e_next.set_name('next')
664 self.e_next.set_size_request(48, 48)
665 self.e_prev = gtk.EventBox()
666 self.e_prev.set_name('prev')
667 self.e_prev.set_size_request(48, 48)
668 self.e_up = gtk.EventBox()
669 self.e_up.set_name('up')
670 self.e_up.set_size_request(48, 48)
671 self.e_down = gtk.EventBox()
672 self.e_down.set_name('down')
673 self.e_down.set_size_request(48, 48)
674 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
675 self.comic_image.show()
678 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
680 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
681 'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
682 'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
683 'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
684 'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
685 'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
686 'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
687 'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
688 'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
689 'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
694 self.e_goweb.add(self.label)
695 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
696 self.e_goweb.connect("button-release-event", self.view_comic)
698 self.e_open.add(self.comic_image)
700 # self.e_switch.add(self.switch)
701 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
702 self.e_switch.connect("button-press-event", self.button_press)
703 self.e_switch.connect("button-release-event", self.button_release)
704 self.e_switch.connect("leave-notify-event", self.button_release)
707 # self.e_next.add(self.next)
708 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
709 self.e_next.connect("button-press-event", self.button_press)
710 self.e_next.connect("button-release-event", self.button_release)
711 self.e_next.connect("leave-notify-event", self.button_release)
713 #self.e_prev.add(self.prev)
714 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
715 self.e_prev.connect("button-press-event", self.button_press)
716 self.e_prev.connect("button-release-event", self.button_release)
717 self.e_prev.connect("leave-notify-event", self.button_release)
719 #self.e_up.add(self.up)
720 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
721 self.e_up.connect("button-press-event", self.button_press)
722 self.e_up.connect("button-release-event", self.button_release)
723 self.e_up.connect("leave-notify-event", self.button_release)
726 #self.e_down.add(self.down)
727 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
728 self.e_down.connect("button-press-event", self.button_press)
729 self.e_down.connect("button-release-event", self.button_release)
730 self.e_down.connect("leave-notify-event", self.button_release)
732 self.vbox.pack_start(self.e_open,False,False,0)
733 self.vbox.pack_end(self.hbox,False,False,0)
735 self.hbox.pack_start(self.e_switch,False,False,0)
736 self.hbox.pack_start(self.e_goweb,False,False,10)
737 self.hbox.pack_end(self.e_next,False,False,0)
738 self.hbox.pack_end(self.e_prev,False,False,0)
739 self.hbox.pack_end(self.e_down,False,False,0)
740 self.hbox.pack_end(self.e_up,False,False,0)
746 # **************** Cairo draw functions ***************
748 def realize(self, widget):
749 global supports_alpha
751 # To check if the display supports alpha channels, get the colormap
752 screen = self.get_screen()
753 colormap = screen.get_rgba_colormap()
755 print 'Your screen does not support alpha channels!'
756 colormap = screen.get_rgb_colormap()
757 supports_alpha = False
759 # print 'Your screen supports alpha channels!'
760 supports_alpha = True
762 # Now we have a colormap appropriate for the screen, use it
763 self.set_colormap(colormap)
767 def do_expose_event(widget, event):
768 cr = widget.window.cairo_create()
769 widget.draw(widget.e_up, 0)
770 widget.draw(widget.e_down, 0)
771 widget.draw(widget.e_prev,0)
772 widget.draw(widget.e_next, 0)
773 widget.draw(widget.e_switch, 0)
774 # widget.writeit(widget.e_goweb, "asdf")
775 cr.set_operator(cairo.OPERATOR_OVER)
776 region = gtk.gdk.region_rectangle(event.area)
778 bg_color=gtk.gdk.color_parse('#000000')
780 cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
782 cr.set_source_rgb (0.0, 0.0, 0.0)
787 # Function that draws a button preloaded in the images dictionary.
788 def draw(self, target, postfix):
789 cr_e = target.window.cairo_create()
790 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
791 cr_e.set_operator(cairo.OPERATOR_SOURCE)
793 cr_e.set_operator(cairo.OPERATOR_OVER)
794 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
797 # ********** Event handlers *************
800 # Most events happen on release, so I moved all of them into the same event handler.
801 def button_release(self, widget, event):
802 if event.type == gtk.gdk.BUTTON_RELEASE:
803 func = widget.get_name()
804 print "pressed: " + func
806 self.e_open.remove(self.comic_image)
807 self.imgvpos = self.imgvpos - 80
810 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
811 self.e_open.add(self.comic_image)
812 self.e_open.show_all()
815 self.e_open.remove(self.comic_image)
816 self.imgvpos = self.imgvpos + 80
817 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
818 self.e_open.add(self.comic_image)
819 self.e_open.show_all()
822 self.db.currentcomic -= 1
824 self.e_open.remove(self.comic_image)
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()
828 self.e_goweb.remove(self.label)
829 titl = str(self.db.db[self.db.currentcomic]['title'])
831 titl = titl[:19] + "..."
832 self.label.set_markup(self.get_markup())
833 self.e_goweb.add(self.label)
834 self.e_goweb.show_all()
837 self.db.currentcomic += 1
839 self.e_open.remove(self.comic_image)
840 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
841 self.e_open.add(self.comic_image)
842 self.e_open.show_all()
843 self.e_goweb.remove(self.label)
844 titl = str(self.db.db[self.db.currentcomic]['title'])
846 titl = titl[:19] + "..."
847 self.label.set_markup(self.get_markup())
848 self.e_goweb.add(self.label)
849 self.e_goweb.show_all()
851 elif func == 'switch':
852 self.active_comics = self.get_active_comics()
855 #sys.stdout = sys.__stdout__
857 #sys.stderr = sys.__stderr__
858 print "active comics: " + str(self.active_comics)
859 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
862 print "switching to " + self.active_comics[self.keypointer]
863 self.comicname = comics[self.active_comics[self.keypointer]]['name']
864 self.db = ComicDb(self.active_comics[self.keypointer])
865 self.e_open.remove(self.comic_image)
866 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
867 self.e_goweb.remove(self.label)
868 titl = str(self.db.db[self.db.currentcomic]['title'])
870 titl = titl[:19] + "..."
871 self.label.set_markup(self.get_markup())
872 self.e_goweb.add(self.label)
873 self.e_goweb.show_all()
874 self.e_open.add(self.comic_image)
875 self.e_open.show_all()
879 self.draw(widget, "0")
881 # this one only switches to pressed button image
882 def button_press(self, widget, event):
883 if not event.type == gtk.gdk.BUTTON_PRESS:
885 self.draw(widget, "1")
887 # this one cancels a button press when pointer is moved out from it after press.
888 def button_out(self, widget, event):
889 if not event.type == gtk.gdk.BUTTON_PRESS:
891 self.draw(widget, "0")
893 # no fancy cairo stuff here. simply launches url in browser.
894 def view_comic(self, widget, event):
895 if not event.type == gtk.gdk.BUTTON_RELEASE:
897 link = self.db.get_link()
898 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
902 # this function modifies the image to fit the widget and then returns it.
904 def get_resized_pixmap(self, filename, vpos):
905 print "vpos: " + str(vpos)
907 width = int(self.sizes['width'])
908 height = int(self.sizes['height'])
910 maxheight = height - 48
912 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
913 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
914 parent_buf.fill(0xffffffff)
916 # get correct width/height
919 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
920 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
921 print "scaled height: " + str(scaled_buf.get_height())
922 if scaled_buf.get_height() > maxheight:
923 if scaled_buf.get_height() - (vpos + maxheight) < 0:
924 vpos = scaled_buf.get_height() - maxheight
925 scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
926 # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
929 tmpy = maxheight - scaled_buf.get_height()
933 scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
935 # test = parent_buf.render_pixmap_and_mask(0)
936 # parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
937 retimg.set_from_pixbuf(parent_buf)
938 print retimg.get_pixel_size()
939 print "vpos after fixing image: " + str(self.imgvpos)
942 def screen_changed(self, widget, old_screen=None):
943 global supports_alpha
945 # To check if the display supports alpha channels, get the colormap
946 screen = self.get_screen()
947 colormap = screen.get_rgba_colormap()
949 print 'Your screen does not support alpha channels!'
950 colormap = screen.get_rgb_colormap()
951 supports_alpha = False
953 # print 'Your screen supports alpha channels!'
954 supports_alpha = True
956 # Now we have a colormap appropriate for the screen, use it
957 self.set_colormap(colormap)
962 def get_active_comics(self):
965 if os.path.isfile(activecomics) == True:
966 print "found " + activecomics
967 dbf = open(activecomics, 'r')
968 dbr = csv.reader(dbf)
977 print "file " + activecomics + " not found"
978 if not os.path.exists(dbdir):
979 print "path not found: " + dbdir
983 print "settings dir creation failed. Exiting."
986 # dbf = file(activecomics, 'w')
988 dbf = open(activecomics, 'w')
990 print "error storing settings"
992 dbw = csv.writer(dbf)
993 dbw.writerow(comics.keys())
1000 def get_markup(self):
1001 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1002 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>'
1004 return '<span size="12000" face="monospace"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1007 #check if settings file exists
1009 #if not, write defaults
1011 # ************************* OPTION DIALOGS ********************************
1013 def show_options(self, widget):
1014 print "loading options dialog"
1015 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1017 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1018 about_button.set_text("About", "See Author, Copyright and License information")
1019 about_button.set_alignment(0,0,0,0)
1020 about_button.connect("clicked", self.show_about)
1022 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1023 comics_button.set_text("Comics", "Choose which comics to view")
1024 comics_button.set_alignment(0,0,0,0)
1025 comics_button.connect("clicked", self.show_comics)
1027 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1028 # size_button.set_text("Change size", "Change the size of the widget")
1029 # size_button.set_alignment(0,0,0,0)
1030 # size_button.connect("clicked", self.show_size)
1032 print "adding buttons to dialog, starting with comics"
1033 dialog.vbox.pack_start(comics_button, True, True, 0)
1034 print "adding about button"
1035 dialog.vbox.pack_start(about_button, True, True, 0)
1036 # dialog.vbox.pack_start(size_button, True, True, 0)
1042 def show_about(self, widget):
1043 print "in about dialog"
1044 dialog = gtk.AboutDialog()
1045 dialog.set_title("About")
1046 dialog.set_name("Comic widget")
1047 dialog.set_version(APP_VERSION)
1048 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1049 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."])
1050 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1051 dialog.set_comments("Silly rabbit, tricks are for kids.")
1052 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/>.""")
1053 dialog.set_wrap_license(True)
1058 def show_comics(self, widget, data = None):
1059 print "in comics dialog! yay!"
1060 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1061 comiclist = comics.keys()
1065 self.liststore = gtk.ListStore(str,str,bool)
1066 for comicid in comiclist:
1068 for acomic in self.active_comics:
1069 if acomic == comicid:
1073 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1075 self.liststore.append([" " + comics[comicid]['name'],comicid, active])
1076 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1077 self.treeview.set_model(self.liststore)
1078 self.treeview.set_reorderable(True)
1079 tvcolumn = gtk.TreeViewColumn('Column 0')
1080 self.treeview.append_column(tvcolumn)
1081 cell = gtk.CellRendererText()
1082 tvcolumn.pack_start(cell, True)
1083 tvcolumn.add_attribute(cell, 'text', 0)
1085 ts = hildon.PannableArea()
1087 ts.set_size_request(-1, 280)
1088 ts.add(self.treeview)
1089 selection = self.treeview.get_selection()
1091 dialog.vbox.pack_start(ts,True,True,0)
1093 selection.connect("changed", self.pick_comic, comiclist)
1097 def pick_comic(self, widget, data):
1098 selected = widget.get_selected()
1099 if selected == None:
1102 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1103 if selected[0].get_value(selected[1],2):
1104 self.remove_comic(selected[0].get_value(selected[1],1))
1105 splt = selected[0].get_value(selected[1],0)[1:]
1106 selected[0].set_value(selected[1],0," " + splt)
1107 selected[0].set_value(selected[1],2,False)
1109 self.add_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,True)
1115 def add_comic(self, comicid):
1116 if os.path.isfile(activecomics) == True:
1117 print "added " + comicid + " to " + str(self.active_comics)
1119 found = self.active_comics.index(comicid)
1121 self.active_comics.append(comicid)
1122 dbf = open(activecomics, 'w')
1123 dbw = csv.writer(dbf)
1124 dbw.writerow(self.active_comics)
1127 def remove_comic(self, comicid):
1128 if os.path.isfile(activecomics) == True:
1129 print "removing " + comicid + " from " + str(self.active_comics)
1131 del self.active_comics[self.active_comics.index(comicid)]
1134 dbf = open(activecomics, 'w')
1135 dbw = csv.writer(dbf)
1136 dbw.writerow(self.active_comics)
1140 def get_size_settings(self):
1141 # defaults = {'width':480,'height':230}
1142 # options: w 798 700 600 500 480 400
1143 # options: h 420 360 310 270 240 200
1144 print "checking to see if config file is available"
1145 if os.path.isfile(sizefile) == True:
1146 print "found " + sizefile
1147 dbf = open(sizefile, 'r')
1148 dbr = csv.reader(dbf)
1153 print "empty config file"
1157 sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1158 if sizes['width'] > 798:
1159 sizes['width'] = 798
1160 if sizes['width'] < 400:
1161 sizes['width'] = 400
1162 if sizes['height'] > 420:
1163 sizes['height'] = 420
1164 if sizes['height'] < 200:
1165 sizes['height'] = 200
1172 print "no config file"
1175 hd_plugin_type = ComicHomePlugin
1178 if __name__ == "__main__":
1180 gobject.type_register(hd_plugin_type)
1181 obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")