trying to fix reboot bug
[comic-widget] / comic-widget.py
index 311db0e..56a2b2a 100644 (file)
@@ -8,16 +8,20 @@ import string
 import os
 import osso
 import cairo
-import pango
+import datetime
+
+supports_alpha = False
 
 # constants. dbfile is the location of the csv
 # comiccache is the location of the images
+APP_VERSION = "0.2.5"
 
 imagedir = "/home/user/comic-widget/images/"
 dbdir = "/home/user/comic-widget/db/"
 comiccache = "/home/user/MyDocs/comics/"
 comics = {"xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + ".comicdb.xkcd.csv"},
-               "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + ".comicdb.sinfest.csv"}}
+               "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + ".comicdb.sinfest.csv"},
+               "dilbert":{"name":"Dilbert","link":"http://dilbert.com.com/","start":"2009-01-01","dbfile":dbdir + ".comicdb.dilbert.csv"}}
 
 # handling of the comics
 class ComicDb():
@@ -45,7 +49,7 @@ class ComicDb():
                for row in dbr:
                        self.db.insert(0,row)
                dbf.close()
-               self.currentcomic = 0
+               self.currentcomic = 0           
 
        def get_comic(self):
                print str(self.currentcomic)
@@ -120,28 +124,73 @@ class ComicDb():
                dbf.close()
 
        def fetch_earlier(self, comic, earliest):
-               number = int(earliest) - 1
-               irow = self.get_irow(comic, number)
+               if len(earliest) == 10:
+                       # date id.
+                       dt = string.split(earliest, "-")
+                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                       earlier = d - datetime.timedelta( 1 )
+                       comicid = earlier.isoformat()
+               else:
+                       comicid = int(earliest) - 1
+
+               irow = self.get_irow(comic, comicid)
                if irow:
                        print "got irow: "
                        print irow
                        print "\ninserting first...\n"
                        self.insert_row_first(irow)
                else:
-                       print "No comic found at " + number
+                       print "No comic found at " + comicid
+
+       def fetch_latest_std(self, comic, latest):
+               print "fetching new after " + str(comic) + " " + str(latest)
+               dateid = False
+               if len(latest) == 10:
+                       # date id.
+                       dateid = True
+                       dt = string.split(latest, "-")
+                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                       newer = d + datetime.timedelta( 1 )
+                       comicid = newer.isoformat()
+               else:
+                       comicid = int(latest) + 1
+
+
+               while True:
+                       irow = self.get_irow(comic, comicid)
+                       if irow:
+                               print "got irow: " + str(irow)
+                               print "inserting..."
+                               self.insert_row(irow)
+                               if dateid:
+                                       dt = string.split(comicid, "-")
+                                       d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
+                                       newer = d + datetime.timedelta( 1 )
+                                       comicid = newer.isoformat()
+                               else:
+                                       comicid += 1
+                       else:
+                               break
+
 
        def get_irow(self, comic, number):
-               if number < 0:
-                       return False
-               print "number is now: " + str(number) + "\n"
+               dateid = False
+               if len(str(number)) == 10:
+                       dateid = True
+               else:
+                       if number < 0:
+                               return False
+                       print "number is now: " + str(number) + "\n"
                if comic == 'xkcd':
                        link = "http://xkcd.org/" + str(number) + "/"
                elif comic == 'sinfest':
                        link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
+               elif comic == 'dilbert':
+                       link = "http://dilbert.com/" + str(number) + "/"
                else:
                        return False
 
-               print "link: " + link + "\n"
+               print "link: " + link
                try:
                        f = urllib2.urlopen(link)
                        hcode = f.code
@@ -179,32 +228,32 @@ class ComicDb():
                                        return False
                                splt = string.split(splt[1], '" border="0" />', 1)
                                title = splt[0]
+
+                       elif comic == 'dilbert':
+                               s = f.read()
+                               f.close()
+                               splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
+                               splt = string.split(splt[1], '" />', 1)
+                               url = "http://dilbert.com" + splt[0]
+                               if len(url) < 50:
+                                       print "Fake 404! Break break break!"
+                                       return False
+                               title = ""
+
                        splt2 = string.rsplit(url, "/", 1)
                        filename = splt2[1]
                        irow = [comic,number,link,url,filename,title]
                        return irow
-                        print "got irow: "
 
-        def fetch_latest_std(self, comic, latest):
 
-                print "fetching new after " + str(comic) + " " + str(latest) + "\n"
-                number = int(latest) + 1
-                while True:
-                       irow = self.get_irow(comic, number)
-                       if irow:
-                               print "got irow: "
-                               print irow
-                               print "\ninserting...\n"
-                               self.insert_row(irow)
-                               number += 1
-                       else:
-                               break
-
-# ------------
+# ------------UI
 
 class ComicHomePlugin(hildondesktop.HomePluginItem):
        def __init__(self):
                hildondesktop.HomePluginItem.__init__(self)
+               global supports_alpha
+               self.set_settings(True)
+               self.connect("show-settings", self.show_options)
                self.osso_c = osso.Context("comic-widget", "0.0.1", False)
                self.osso_rpc = osso.Rpc(self.osso_c)
                self.keys = comics.keys()
@@ -214,6 +263,11 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                self.set_name = "comicwidget"
                screen = self.get_screen()
                colormap = screen.get_rgba_colormap()
+               if colormap == None:
+                       colormap = screen.get_rgb_colormap()
+                       supports_alpha = False
+               else:
+                       supports_alpha = True
                self.set_colormap(colormap)
                self.set_app_paintable(True)
                #setup internal area
@@ -324,11 +378,24 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
 
        # **************** Cairo draw functions ***************
 
-       def realize(self):
+       def realize(self, widget):
+               global supports_alpha
+
+               # To check if the display supports alpha channels, get the colormap
                screen = self.get_screen()
                colormap = screen.get_rgba_colormap()
+               if colormap == None:
+                       print 'Your screen does not support alpha channels!'
+                       colormap = screen.get_rgb_colormap()
+                       supports_alpha = False
+               else:
+               #      print 'Your screen supports alpha channels!'
+                       supports_alpha = True
+
+               # Now we have a colormap appropriate for the screen, use it
                self.set_colormap(colormap)
-               return None
+
+               return False
 
        def do_expose_event(widget, event):
                cr = widget.window.cairo_create()
@@ -342,7 +409,11 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                region = gtk.gdk.region_rectangle(event.area)
                cr.region(region)
                bg_color=gtk.gdk.color_parse('#000000')
-               cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
+               if supports_alpha:
+                       cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
+               else:
+                       cr.set_source_rgb (0.0, 0.0, 0.0)
+                       
                cr.fill_preserve()
                return False
 
@@ -411,7 +482,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                                self.e_goweb.show_all()
 
                        elif func == 'switch':
-                               self.keypointer = (self.keypointer + 1) % 2
+                               self.keypointer = (self.keypointer + 1) % 3
                                self.imgvpos = 0
                                self.db = []
                                print "switching to " + self.keys[self.keypointer]
@@ -428,7 +499,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                                self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
                                self.e_open.add(self.comic_image)
                                self.e_open.show_all()  
-                       
+
                        else:
                                return False                    
                self.draw(widget, "0")
@@ -487,6 +558,55 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                print "vpos after fixing image: " + str(self.imgvpos)
                return retimg
 
+       def screen_changed(self, widget, old_screen=None):
+               global supports_alpha
+
+               # To check if the display supports alpha channels, get the colormap
+               screen = self.get_screen()
+               colormap = screen.get_rgba_colormap()
+               if colormap == None:
+                       print 'Your screen does not support alpha channels!'
+                       colormap = screen.get_rgb_colormap()
+                       supports_alpha = False
+               else:
+               #      print 'Your screen supports alpha channels!'
+                       supports_alpha = True
+
+               # Now we have a colormap appropriate for the screen, use it
+               self.set_colormap(colormap)
+
+               return False
+
+# ************************* OPTIONS ********************************
+
+       def show_options(self, widget):
+               dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
+
+               about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               about_button.set_text("About", "See Author, Copyright and License information")
+               about_button.set_alignment(0,0,0,0)
+               about_button.connect("clicked", self.show_about)
+
+               dialog.vbox.pack_start(about_button, True, True, 0)
+
+               dialog.show_all()
+               dialog.run()
+               dialog.destroy()
+
+       def show_about(self, widget):
+               dialog = gtk.AboutDialog()
+               dialog.set_title("About")
+               dialog.set_name("Comic widget")
+               dialog.set_version(APP_VERSION)
+               dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
+               dialog.set_authors(["Marcus Wikstrom <mece@ovi.com>\n\nSpecial thanks to Benoit Hervier, Daniel Would and Brent Chiodo\nfor some nice code to sample."])
+               # dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/usr/share/touchsearch/icon.png"))
+               # dialog.set_comments("Thanks to Benoit Hervier, Daniel Would and Brent Chiodo for some nice code to sample.")
+               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/>.""")
+               dialog.set_wrap_license(True)
+               dialog.show_all()
+               dialog.run()
+               dialog.destroy()
 
 hd_plugin_type = ComicHomePlugin